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