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