]>
Commit | Line | Data |
---|---|---|
2953961c | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
2953961c | 3 | * |
1bc6525b | 4 | * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk> |
2953961c JH |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
35750e4d UH |
21 | #ifdef _WIN32 |
22 | // Windows: Avoid boost/thread namespace pollution (which includes windows.h). | |
23 | #define NOGDI | |
24 | #define NORESOURCE | |
25 | #endif | |
e71eb81c JH |
26 | #include <boost/thread/locks.hpp> |
27 | #include <boost/thread/shared_mutex.hpp> | |
28 | ||
269528f5 | 29 | #ifdef ENABLE_DECODE |
4e5a4405 | 30 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 31 | #endif |
4e5a4405 | 32 | |
f65cd27b | 33 | #include "session.hpp" |
2953961c | 34 | |
2acdb232 | 35 | #include "devicemanager.hpp" |
119aff65 | 36 | |
2acdb232 | 37 | #include "data/analog.hpp" |
f3d66e52 | 38 | #include "data/analogsegment.hpp" |
2acdb232 JH |
39 | #include "data/decoderstack.hpp" |
40 | #include "data/logic.hpp" | |
f3d66e52 | 41 | #include "data/logicsegment.hpp" |
2acdb232 | 42 | #include "data/decode/decoder.hpp" |
82c7f640 | 43 | |
da30ecb7 JH |
44 | #include "devices/hardwaredevice.hpp" |
45 | #include "devices/sessionfile.hpp" | |
46 | ||
2acdb232 JH |
47 | #include "view/analogsignal.hpp" |
48 | #include "view/decodetrace.hpp" | |
49 | #include "view/logicsignal.hpp" | |
28a4c9c5 | 50 | |
3b68d03d JH |
51 | #include <cassert> |
52 | #include <mutex> | |
e92cd4e4 JH |
53 | #include <stdexcept> |
54 | ||
728e5ef7 JH |
55 | #include <sys/stat.h> |
56 | ||
79efbc53 JH |
57 | #include <QDebug> |
58 | ||
fe3a1c21 | 59 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 60 | |
aca64cac JH |
61 | using boost::shared_lock; |
62 | using boost::shared_mutex; | |
63 | using boost::unique_lock; | |
64 | ||
f9abf97e | 65 | using std::dynamic_pointer_cast; |
d2344534 | 66 | using std::function; |
3b68d03d | 67 | using std::lock_guard; |
d873f4d6 | 68 | using std::list; |
819f4c25 | 69 | using std::map; |
aca64cac | 70 | using std::mutex; |
8524a597 | 71 | using std::recursive_mutex; |
02412f0b | 72 | using std::set; |
f9abf97e | 73 | using std::shared_ptr; |
819f4c25 | 74 | using std::string; |
78b0af3e | 75 | using std::unordered_set; |
819f4c25 | 76 | using std::vector; |
28a4c9c5 | 77 | |
e8d00928 ML |
78 | using sigrok::Analog; |
79 | using sigrok::Channel; | |
80 | using sigrok::ChannelType; | |
81 | using sigrok::ConfigKey; | |
82 | using sigrok::DatafeedCallbackFunction; | |
e8d00928 | 83 | using sigrok::Error; |
e8d00928 ML |
84 | using sigrok::Header; |
85 | using sigrok::Logic; | |
86 | using sigrok::Meta; | |
87 | using sigrok::Packet; | |
88 | using sigrok::PacketPayload; | |
89 | using sigrok::Session; | |
90 | using sigrok::SessionDevice; | |
91 | ||
92 | using Glib::VariantBase; | |
93 | using Glib::Variant; | |
51e77110 | 94 | |
e8d00928 | 95 | namespace pv { |
2b81ae46 | 96 | Session::Session(DeviceManager &device_manager) : |
8dbbc7f0 | 97 | device_manager_(device_manager), |
ff008de6 JH |
98 | capture_state_(Stopped), |
99 | cur_samplerate_(0) | |
2953961c | 100 | { |
2953961c JH |
101 | } |
102 | ||
2b81ae46 | 103 | Session::~Session() |
2953961c | 104 | { |
04463625 | 105 | // Stop and join to the thread |
5b7cf66c | 106 | stop_capture(); |
2953961c JH |
107 | } |
108 | ||
2b81ae46 | 109 | DeviceManager& Session::device_manager() |
4cb0b033 | 110 | { |
8dbbc7f0 | 111 | return device_manager_; |
4cb0b033 JH |
112 | } |
113 | ||
2b81ae46 | 114 | const DeviceManager& Session::device_manager() const |
4cb0b033 | 115 | { |
8dbbc7f0 | 116 | return device_manager_; |
4cb0b033 JH |
117 | } |
118 | ||
da30ecb7 | 119 | shared_ptr<sigrok::Session> Session::session() const |
1f4caa77 | 120 | { |
da30ecb7 JH |
121 | if (!device_) |
122 | return shared_ptr<sigrok::Session>(); | |
123 | return device_->session(); | |
1f4caa77 JH |
124 | } |
125 | ||
da30ecb7 | 126 | shared_ptr<devices::Device> Session::device() const |
dc0867ff | 127 | { |
8dbbc7f0 | 128 | return device_; |
dc0867ff JH |
129 | } |
130 | ||
da30ecb7 | 131 | void Session::set_device(shared_ptr<devices::Device> device) |
d64d1596 | 132 | { |
da30ecb7 JH |
133 | assert(device); |
134 | ||
82596f46 JH |
135 | // Ensure we are not capturing before setting the device |
136 | stop_capture(); | |
137 | ||
4d6c6ea3 SA |
138 | if (device_) |
139 | device_->close(); | |
140 | ||
86123e2e SA |
141 | device_.reset(); |
142 | ||
143 | // Remove all traces | |
144 | signals_.clear(); | |
4b0d7046 SA |
145 | cur_logic_segment_.reset(); |
146 | ||
147 | for (auto entry : cur_analog_segments_) { | |
148 | shared_ptr<sigrok::Channel>(entry.first).reset(); | |
149 | shared_ptr<data::AnalogSegment>(entry.second).reset(); | |
150 | } | |
151 | ||
152 | logic_data_.reset(); | |
86123e2e | 153 | decode_traces_.clear(); |
4b0d7046 | 154 | |
86123e2e SA |
155 | signals_changed(); |
156 | ||
da30ecb7 | 157 | device_ = std::move(device); |
4d6c6ea3 | 158 | device_->open(); |
da30ecb7 JH |
159 | device_->session()->add_datafeed_callback([=] |
160 | (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) { | |
161 | data_feed_in(device, packet); | |
162 | }); | |
ae2d1bc5 | 163 | |
4b8e787d | 164 | update_signals(); |
20f81a58 | 165 | device_selected(); |
ae2d1bc5 | 166 | } |
85843b14 | 167 | |
2b81ae46 | 168 | void Session::set_default_device() |
6fd0b639 | 169 | { |
da30ecb7 | 170 | const list< shared_ptr<devices::HardwareDevice> > &devices = |
8dbbc7f0 | 171 | device_manager_.devices(); |
6fd0b639 | 172 | |
da30ecb7 JH |
173 | if (devices.empty()) |
174 | return; | |
dc0867ff | 175 | |
da30ecb7 JH |
176 | // Try and find the demo device and select that by default |
177 | const auto iter = std::find_if(devices.begin(), devices.end(), | |
178 | [] (const shared_ptr<devices::HardwareDevice> &d) { | |
179 | return d->hardware_device()->driver()->name() == | |
180 | "demo"; }); | |
181 | set_device((iter == devices.end()) ? devices.front() : *iter); | |
dc0867ff JH |
182 | } |
183 | ||
2b81ae46 | 184 | Session::capture_state Session::get_capture_state() const |
5b7cf66c | 185 | { |
8dbbc7f0 JH |
186 | lock_guard<mutex> lock(sampling_mutex_); |
187 | return capture_state_; | |
5b7cf66c JH |
188 | } |
189 | ||
2b81ae46 | 190 | void Session::start_capture(function<void (const QString)> error_handler) |
2e2946fe | 191 | { |
5b7cf66c JH |
192 | stop_capture(); |
193 | ||
6ac6242b | 194 | // Check that at least one channel is enabled |
da30ecb7 | 195 | assert(device_); |
e6c1382e JH |
196 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
197 | if (sr_dev) { | |
198 | const auto channels = sr_dev->channels(); | |
199 | if (!std::any_of(channels.begin(), channels.end(), | |
200 | [](shared_ptr<Channel> channel) { | |
201 | return channel->enabled(); })) { | |
202 | error_handler(tr("No channels enabled.")); | |
203 | return; | |
204 | } | |
9c48fa57 JH |
205 | } |
206 | ||
53ea4c6c | 207 | // Clear signal data |
33994eb4 | 208 | for (const shared_ptr<data::SignalData> d : get_data()) |
53ea4c6c SA |
209 | d->clear(); |
210 | ||
9c48fa57 | 211 | // Begin the session |
8dbbc7f0 | 212 | sampling_thread_ = std::thread( |
2b81ae46 | 213 | &Session::sample_thread_proc, this, device_, |
19adbc2c | 214 | error_handler); |
2e2946fe JH |
215 | } |
216 | ||
2b81ae46 | 217 | void Session::stop_capture() |
5b7cf66c | 218 | { |
04463625 | 219 | if (get_capture_state() != Stopped) |
da30ecb7 | 220 | device_->stop(); |
5b7cf66c JH |
221 | |
222 | // Check that sampling stopped | |
8dbbc7f0 JH |
223 | if (sampling_thread_.joinable()) |
224 | sampling_thread_.join(); | |
5b7cf66c JH |
225 | } |
226 | ||
2b81ae46 | 227 | set< shared_ptr<data::SignalData> > Session::get_data() const |
02412f0b | 228 | { |
8dbbc7f0 | 229 | shared_lock<shared_mutex> lock(signals_mutex_); |
02412f0b | 230 | set< shared_ptr<data::SignalData> > data; |
8dbbc7f0 | 231 | for (const shared_ptr<view::Signal> sig : signals_) { |
02412f0b JH |
232 | assert(sig); |
233 | data.insert(sig->data()); | |
234 | } | |
235 | ||
236 | return data; | |
237 | } | |
238 | ||
2220e942 SA |
239 | double Session::get_samplerate() const |
240 | { | |
241 | double samplerate = 0.0; | |
242 | ||
243 | for (const shared_ptr<pv::data::SignalData> d : get_data()) { | |
244 | assert(d); | |
245 | const vector< shared_ptr<pv::data::Segment> > segments = | |
246 | d->segments(); | |
247 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
248 | samplerate = std::max(samplerate, s->samplerate()); | |
249 | } | |
250 | ||
251 | // If there is no sample rate given we use samples as unit | |
252 | if (samplerate == 0.0) | |
253 | samplerate = 1.0; | |
254 | ||
255 | return samplerate; | |
256 | } | |
257 | ||
bf914698 | 258 | const unordered_set< shared_ptr<view::Signal> > Session::signals() const |
2e2946fe | 259 | { |
bf914698 | 260 | shared_lock<shared_mutex> lock(signals_mutex_); |
8dbbc7f0 | 261 | return signals_; |
2e2946fe JH |
262 | } |
263 | ||
269528f5 | 264 | #ifdef ENABLE_DECODE |
2b81ae46 | 265 | bool Session::add_decoder(srd_decoder *const dec) |
82c7f640 | 266 | { |
6ac6242b | 267 | map<const srd_channel*, shared_ptr<view::LogicSignal> > channels; |
7491a29f | 268 | shared_ptr<data::DecoderStack> decoder_stack; |
4e5a4405 | 269 | |
2ad82c2e | 270 | try { |
8dbbc7f0 | 271 | lock_guard<boost::shared_mutex> lock(signals_mutex_); |
e0fc5810 | 272 | |
4e5a4405 | 273 | // Create the decoder |
7491a29f | 274 | decoder_stack = shared_ptr<data::DecoderStack>( |
bdc5c3b0 | 275 | new data::DecoderStack(*this, dec)); |
4e5a4405 | 276 | |
6ac6242b ML |
277 | // Make a list of all the channels |
278 | std::vector<const srd_channel*> all_channels; | |
f3290553 | 279 | for (const GSList *i = dec->channels; i; i = i->next) |
6ac6242b | 280 | all_channels.push_back((const srd_channel*)i->data); |
f3290553 | 281 | for (const GSList *i = dec->opt_channels; i; i = i->next) |
6ac6242b | 282 | all_channels.push_back((const srd_channel*)i->data); |
d2f46d27 | 283 | |
6ac6242b ML |
284 | // Auto select the initial channels |
285 | for (const srd_channel *pdch : all_channels) | |
2ad82c2e | 286 | for (shared_ptr<view::Signal> s : signals_) { |
4e5a4405 JH |
287 | shared_ptr<view::LogicSignal> l = |
288 | dynamic_pointer_cast<view::LogicSignal>(s); | |
8bd26d8b | 289 | if (l && QString::fromUtf8(pdch->name). |
27e8df22 | 290 | toLower().contains( |
0a47889b | 291 | l->name().toLower())) |
6ac6242b | 292 | channels[pdch] = l; |
4e5a4405 | 293 | } |
4e5a4405 | 294 | |
7491a29f JH |
295 | assert(decoder_stack); |
296 | assert(!decoder_stack->stack().empty()); | |
297 | assert(decoder_stack->stack().front()); | |
6ac6242b | 298 | decoder_stack->stack().front()->set_channels(channels); |
4e5a4405 JH |
299 | |
300 | // Create the decode signal | |
b9329558 | 301 | shared_ptr<view::DecodeTrace> d( |
7491a29f | 302 | new view::DecodeTrace(*this, decoder_stack, |
8dbbc7f0 JH |
303 | decode_traces_.size())); |
304 | decode_traces_.push_back(d); | |
2ad82c2e | 305 | } catch (std::runtime_error e) { |
e92cd4e4 JH |
306 | return false; |
307 | } | |
308 | ||
82c7f640 | 309 | signals_changed(); |
e92cd4e4 | 310 | |
7491a29f JH |
311 | // Do an initial decode |
312 | decoder_stack->begin_decode(); | |
313 | ||
e92cd4e4 | 314 | return true; |
82c7f640 JH |
315 | } |
316 | ||
2b81ae46 | 317 | vector< shared_ptr<view::DecodeTrace> > Session::get_decode_signals() const |
38eeddea | 318 | { |
8dbbc7f0 JH |
319 | shared_lock<shared_mutex> lock(signals_mutex_); |
320 | return decode_traces_; | |
38eeddea JH |
321 | } |
322 | ||
2b81ae46 | 323 | void Session::remove_decode_signal(view::DecodeTrace *signal) |
c51482b3 | 324 | { |
8dbbc7f0 | 325 | for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) |
2ad82c2e | 326 | if ((*i).get() == signal) { |
8dbbc7f0 | 327 | decode_traces_.erase(i); |
c51482b3 JH |
328 | signals_changed(); |
329 | return; | |
330 | } | |
331 | } | |
269528f5 | 332 | #endif |
c51482b3 | 333 | |
2b81ae46 | 334 | void Session::set_capture_state(capture_state state) |
6ac96c2e | 335 | { |
896936e5 TS |
336 | bool changed; |
337 | ||
338 | { | |
339 | lock_guard<mutex> lock(sampling_mutex_); | |
340 | changed = capture_state_ != state; | |
341 | capture_state_ = state; | |
342 | } | |
343 | ||
f3290553 | 344 | if (changed) |
2b49eeb0 | 345 | capture_state_changed(state); |
6ac96c2e JH |
346 | } |
347 | ||
ffe00119 | 348 | void Session::update_signals() |
b087ba7f | 349 | { |
076eefa4 SA |
350 | if (!device_) { |
351 | signals_.clear(); | |
352 | logic_data_.reset(); | |
353 | return; | |
354 | } | |
e6c1382e JH |
355 | |
356 | lock_guard<recursive_mutex> lock(data_mutex_); | |
b087ba7f | 357 | |
ffe00119 | 358 | const shared_ptr<sigrok::Device> sr_dev = device_->device(); |
c6412b47 JH |
359 | if (!sr_dev) { |
360 | signals_.clear(); | |
361 | logic_data_.reset(); | |
362 | return; | |
363 | } | |
364 | ||
b087ba7f | 365 | // Detect what data types we will receive |
c6412b47 | 366 | auto channels = sr_dev->channels(); |
e8d00928 ML |
367 | unsigned int logic_channel_count = std::count_if( |
368 | channels.begin(), channels.end(), | |
369 | [] (shared_ptr<Channel> channel) { | |
370 | return channel->type() == ChannelType::LOGIC; }); | |
b087ba7f | 371 | |
f3d66e52 | 372 | // Create data containers for the logic data segments |
b087ba7f | 373 | { |
8524a597 | 374 | lock_guard<recursive_mutex> data_lock(data_mutex_); |
b087ba7f | 375 | |
3917ba77 JH |
376 | if (logic_channel_count == 0) { |
377 | logic_data_.reset(); | |
378 | } else if (!logic_data_ || | |
379 | logic_data_->num_channels() != logic_channel_count) { | |
8dbbc7f0 | 380 | logic_data_.reset(new data::Logic( |
6ac6242b | 381 | logic_channel_count)); |
8dbbc7f0 | 382 | assert(logic_data_); |
b087ba7f | 383 | } |
b087ba7f JH |
384 | } |
385 | ||
386 | // Make the Signals list | |
fbd3e234 | 387 | { |
8dbbc7f0 | 388 | unique_lock<shared_mutex> lock(signals_mutex_); |
b087ba7f | 389 | |
3917ba77 | 390 | unordered_set< shared_ptr<view::Signal> > prev_sigs(signals_); |
8dbbc7f0 | 391 | signals_.clear(); |
b087ba7f | 392 | |
c6412b47 | 393 | for (auto channel : sr_dev->channels()) { |
39e680c2 | 394 | shared_ptr<view::Signal> signal; |
39e680c2 | 395 | |
3917ba77 JH |
396 | // Find the channel in the old signals |
397 | const auto iter = std::find_if( | |
398 | prev_sigs.cbegin(), prev_sigs.cend(), | |
399 | [&](const shared_ptr<view::Signal> &s) { | |
400 | return s->channel() == channel; | |
401 | }); | |
402 | if (iter != prev_sigs.end()) { | |
403 | // Copy the signal from the old set to the new | |
404 | signal = *iter; | |
405 | auto logic_signal = dynamic_pointer_cast< | |
406 | view::LogicSignal>(signal); | |
407 | if (logic_signal) | |
408 | logic_signal->set_logic_data( | |
409 | logic_data_); | |
410 | } else { | |
411 | // Create a new signal | |
412 | switch(channel->type()->id()) { | |
413 | case SR_CHANNEL_LOGIC: | |
414 | signal = shared_ptr<view::Signal>( | |
415 | new view::LogicSignal(*this, | |
ffe00119 | 416 | device_, channel, |
3917ba77 JH |
417 | logic_data_)); |
418 | break; | |
419 | ||
420 | case SR_CHANNEL_ANALOG: | |
421 | { | |
422 | shared_ptr<data::Analog> data( | |
423 | new data::Analog()); | |
424 | signal = shared_ptr<view::Signal>( | |
425 | new view::AnalogSignal( | |
426 | *this, channel, data)); | |
427 | break; | |
428 | } | |
429 | ||
430 | default: | |
431 | assert(0); | |
432 | break; | |
433 | } | |
b087ba7f | 434 | } |
39e680c2 JH |
435 | |
436 | assert(signal); | |
78b0af3e | 437 | signals_.insert(signal); |
b087ba7f | 438 | } |
fbd3e234 | 439 | } |
b087ba7f JH |
440 | |
441 | signals_changed(); | |
442 | } | |
443 | ||
2b81ae46 | 444 | shared_ptr<view::Signal> Session::signal_from_channel( |
e8d00928 | 445 | shared_ptr<Channel> channel) const |
3ddcc083 | 446 | { |
8dbbc7f0 JH |
447 | lock_guard<boost::shared_mutex> lock(signals_mutex_); |
448 | for (shared_ptr<view::Signal> sig : signals_) { | |
3ddcc083 | 449 | assert(sig); |
6ac6242b | 450 | if (sig->channel() == channel) |
3ddcc083 JH |
451 | return sig; |
452 | } | |
453 | return shared_ptr<view::Signal>(); | |
454 | } | |
455 | ||
da30ecb7 | 456 | void Session::sample_thread_proc(shared_ptr<devices::Device> device, |
f2edb557 | 457 | function<void (const QString)> error_handler) |
274d4f13 | 458 | { |
e8d00928 | 459 | assert(device); |
f2edb557 | 460 | assert(error_handler); |
be73bdfa | 461 | |
45f0c7c9 UH |
462 | (void)device; |
463 | ||
b48daed6 | 464 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
274d4f13 | 465 | |
e7216ae0 SA |
466 | out_of_memory_ = false; |
467 | ||
ae2d1bc5 | 468 | try { |
5237f0c5 | 469 | device_->start(); |
2ad82c2e | 470 | } catch (Error e) { |
e8d00928 | 471 | error_handler(e.what()); |
eec446e1 JH |
472 | return; |
473 | } | |
474 | ||
da30ecb7 | 475 | set_capture_state(device_->session()->trigger() ? |
07dcf561 | 476 | AwaitingTrigger : Running); |
eec446e1 | 477 | |
da30ecb7 | 478 | device_->run(); |
eec446e1 | 479 | set_capture_state(Stopped); |
1a2fe44c JH |
480 | |
481 | // Confirm that SR_DF_END was received | |
2ad82c2e | 482 | if (cur_logic_segment_) { |
bb2cdfff JH |
483 | qDebug("SR_DF_END was not received."); |
484 | assert(0); | |
485 | } | |
e7216ae0 SA |
486 | |
487 | if (out_of_memory_) | |
488 | error_handler(tr("Out of memory, acquisition stopped.")); | |
eec446e1 JH |
489 | } |
490 | ||
da30ecb7 | 491 | void Session::feed_in_header() |
eec446e1 | 492 | { |
b48daed6 | 493 | cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE); |
be73bdfa JH |
494 | } |
495 | ||
da30ecb7 | 496 | void Session::feed_in_meta(shared_ptr<Meta> meta) |
be73bdfa | 497 | { |
e8d00928 ML |
498 | for (auto entry : meta->config()) { |
499 | switch (entry.first->id()) { | |
be73bdfa | 500 | case SR_CONF_SAMPLERATE: |
8a7b603b SA |
501 | // We can't rely on the header to always contain the sample rate, |
502 | // so in case it's supplied via a meta packet, we use it. | |
503 | if (!cur_samplerate_) | |
504 | cur_samplerate_ = g_variant_get_uint64(entry.second.gobj()); | |
505 | ||
be73bdfa | 506 | /// @todo handle samplerate changes |
be73bdfa JH |
507 | break; |
508 | default: | |
509 | // Unknown metadata is not an error. | |
510 | break; | |
511 | } | |
aba1dd16 | 512 | } |
74043039 JH |
513 | |
514 | signals_changed(); | |
aba1dd16 JH |
515 | } |
516 | ||
48257a69 SA |
517 | void Session::feed_in_trigger() |
518 | { | |
519 | // The channel containing most samples should be most accurate | |
520 | uint64_t sample_count = 0; | |
521 | ||
522 | for (const shared_ptr<pv::data::SignalData> d : get_data()) { | |
523 | assert(d); | |
524 | uint64_t temp_count = 0; | |
525 | ||
526 | const vector< shared_ptr<pv::data::Segment> > segments = | |
527 | d->segments(); | |
528 | for (const shared_ptr<pv::data::Segment> &s : segments) | |
529 | temp_count += s->get_sample_count(); | |
530 | ||
531 | if (temp_count > sample_count) | |
532 | sample_count = temp_count; | |
533 | } | |
534 | ||
535 | trigger_event(sample_count / get_samplerate()); | |
536 | } | |
537 | ||
2b81ae46 | 538 | void Session::feed_in_frame_begin() |
82f50b10 | 539 | { |
f3d66e52 | 540 | if (cur_logic_segment_ || !cur_analog_segments_.empty()) |
82f50b10 JH |
541 | frame_began(); |
542 | } | |
543 | ||
2b81ae46 | 544 | void Session::feed_in_logic(shared_ptr<Logic> logic) |
9c112671 | 545 | { |
8524a597 | 546 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 547 | |
ff4bf6bd SA |
548 | const size_t sample_count = logic->data_length() / logic->unit_size(); |
549 | ||
2ad82c2e | 550 | if (!logic_data_) { |
e6c1382e JH |
551 | // The only reason logic_data_ would not have been created is |
552 | // if it was not possible to determine the signals when the | |
553 | // device was created. | |
554 | update_signals(); | |
79efbc53 | 555 | } |
be73bdfa | 556 | |
2ad82c2e | 557 | if (!cur_logic_segment_) { |
bb2cdfff | 558 | // This could be the first packet after a trigger |
2b49eeb0 JH |
559 | set_capture_state(Running); |
560 | ||
f3d66e52 JH |
561 | // Create a new data segment |
562 | cur_logic_segment_ = shared_ptr<data::LogicSegment>( | |
563 | new data::LogicSegment( | |
ff4bf6bd | 564 | logic, cur_samplerate_, sample_count)); |
f3d66e52 | 565 | logic_data_->push_segment(cur_logic_segment_); |
82f50b10 JH |
566 | |
567 | // @todo Putting this here means that only listeners querying | |
568 | // for logic will be notified. Currently the only user of | |
569 | // frame_began is DecoderStack, but in future we need to signal | |
570 | // this after both analog and logic sweeps have begun. | |
571 | frame_began(); | |
2ad82c2e | 572 | } else { |
f3d66e52 JH |
573 | // Append to the existing data segment |
574 | cur_logic_segment_->append_payload(logic); | |
9c112671 JH |
575 | } |
576 | ||
1f374035 | 577 | data_received(); |
9c112671 JH |
578 | } |
579 | ||
2b81ae46 | 580 | void Session::feed_in_analog(shared_ptr<Analog> analog) |
aba1dd16 | 581 | { |
8524a597 | 582 | lock_guard<recursive_mutex> lock(data_mutex_); |
79efbc53 | 583 | |
e8d00928 ML |
584 | const vector<shared_ptr<Channel>> channels = analog->channels(); |
585 | const unsigned int channel_count = channels.size(); | |
586 | const size_t sample_count = analog->num_samples() / channel_count; | |
475f4d08 | 587 | const float *data = static_cast<const float *>(analog->data_pointer()); |
bb2cdfff | 588 | bool sweep_beginning = false; |
be73bdfa | 589 | |
2ad82c2e | 590 | if (signals_.empty()) |
a3f678a7 | 591 | update_signals(); |
a3f678a7 | 592 | |
2ad82c2e | 593 | for (auto channel : channels) { |
f3d66e52 | 594 | shared_ptr<data::AnalogSegment> segment; |
2b49eeb0 | 595 | |
f3d66e52 JH |
596 | // Try to get the segment of the channel |
597 | const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >:: | |
598 | iterator iter = cur_analog_segments_.find(channel); | |
599 | if (iter != cur_analog_segments_.end()) | |
600 | segment = (*iter).second; | |
2ad82c2e | 601 | else { |
39ccf9c3 | 602 | // If no segment was found, this means we haven't |
bb2cdfff | 603 | // created one yet. i.e. this is the first packet |
f3d66e52 | 604 | // in the sweep containing this segment. |
bb2cdfff JH |
605 | sweep_beginning = true; |
606 | ||
f3d66e52 JH |
607 | // Create a segment, keep it in the maps of channels |
608 | segment = shared_ptr<data::AnalogSegment>( | |
609 | new data::AnalogSegment( | |
ff4bf6bd | 610 | cur_samplerate_, sample_count)); |
f3d66e52 | 611 | cur_analog_segments_[channel] = segment; |
bb2cdfff | 612 | |
ff4bf6bd | 613 | // Find the analog data associated with the channel |
bb2cdfff JH |
614 | shared_ptr<view::AnalogSignal> sig = |
615 | dynamic_pointer_cast<view::AnalogSignal>( | |
6ac6242b | 616 | signal_from_channel(channel)); |
bb2cdfff JH |
617 | assert(sig); |
618 | ||
619 | shared_ptr<data::Analog> data(sig->analog_data()); | |
620 | assert(data); | |
621 | ||
f3d66e52 JH |
622 | // Push the segment into the analog data. |
623 | data->push_segment(segment); | |
bb2cdfff JH |
624 | } |
625 | ||
f3d66e52 | 626 | assert(segment); |
bb2cdfff | 627 | |
f3d66e52 JH |
628 | // Append the samples in the segment |
629 | segment->append_interleaved_samples(data++, sample_count, | |
6ac6242b | 630 | channel_count); |
aba1dd16 | 631 | } |
bb2cdfff JH |
632 | |
633 | if (sweep_beginning) { | |
634 | // This could be the first packet after a trigger | |
635 | set_capture_state(Running); | |
aba1dd16 JH |
636 | } |
637 | ||
1f374035 | 638 | data_received(); |
aba1dd16 | 639 | } |
9c112671 | 640 | |
da30ecb7 JH |
641 | void Session::data_feed_in(shared_ptr<sigrok::Device> device, |
642 | shared_ptr<Packet> packet) | |
b0e1d01d | 643 | { |
da30ecb7 JH |
644 | (void)device; |
645 | ||
e8d00928 | 646 | assert(device); |
da30ecb7 | 647 | assert(device == device_->device()); |
b0e1d01d JH |
648 | assert(packet); |
649 | ||
e8d00928 | 650 | switch (packet->type()->id()) { |
b0e1d01d | 651 | case SR_DF_HEADER: |
da30ecb7 | 652 | feed_in_header(); |
b0e1d01d JH |
653 | break; |
654 | ||
be73bdfa | 655 | case SR_DF_META: |
da30ecb7 | 656 | feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload())); |
aba1dd16 JH |
657 | break; |
658 | ||
48257a69 SA |
659 | case SR_DF_TRIGGER: |
660 | feed_in_trigger(); | |
661 | break; | |
662 | ||
82f50b10 JH |
663 | case SR_DF_FRAME_BEGIN: |
664 | feed_in_frame_begin(); | |
665 | break; | |
666 | ||
2e2946fe | 667 | case SR_DF_LOGIC: |
e7216ae0 SA |
668 | try { |
669 | feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload())); | |
670 | } catch (std::bad_alloc) { | |
671 | out_of_memory_ = true; | |
672 | device_->stop(); | |
673 | } | |
2953961c JH |
674 | break; |
675 | ||
aba1dd16 | 676 | case SR_DF_ANALOG: |
e7216ae0 SA |
677 | try { |
678 | feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload())); | |
679 | } catch (std::bad_alloc) { | |
680 | out_of_memory_ = true; | |
681 | device_->stop(); | |
682 | } | |
aba1dd16 JH |
683 | break; |
684 | ||
2953961c | 685 | case SR_DF_END: |
2e2946fe JH |
686 | { |
687 | { | |
8524a597 | 688 | lock_guard<recursive_mutex> lock(data_mutex_); |
f3d66e52 JH |
689 | cur_logic_segment_.reset(); |
690 | cur_analog_segments_.clear(); | |
2e2946fe | 691 | } |
1f374035 | 692 | frame_ended(); |
2953961c JH |
693 | break; |
694 | } | |
adb0a983 ML |
695 | default: |
696 | break; | |
2e2946fe | 697 | } |
2953961c JH |
698 | } |
699 | ||
51e77110 | 700 | } // namespace pv |