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