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