]> sigrok.org Git - pulseview.git/blame_incremental - pv/session.cpp
MainWindow: Enable dock nesting
[pulseview.git] / pv / session.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
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
21#ifdef _WIN32
22// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
23#define NOGDI
24#define NORESOURCE
25#endif
26#include <boost/thread/locks.hpp>
27#include <boost/thread/shared_mutex.hpp>
28
29#include <QFileInfo>
30
31#include <cassert>
32#include <mutex>
33#include <stdexcept>
34
35#include <sys/stat.h>
36
37#include "session.hpp"
38#include "devicemanager.hpp"
39
40#include "data/analog.hpp"
41#include "data/analogsegment.hpp"
42#include "data/decoderstack.hpp"
43#include "data/logic.hpp"
44#include "data/logicsegment.hpp"
45#include "data/signalbase.hpp"
46#include "data/decode/decoder.hpp"
47
48#include "devices/hardwaredevice.hpp"
49#include "devices/sessionfile.hpp"
50
51#include "toolbars/mainbar.hpp"
52
53#include "view/analogsignal.hpp"
54#include "view/decodetrace.hpp"
55#include "view/logicsignal.hpp"
56#include "view/signal.hpp"
57#include "view/view.hpp"
58
59#include <libsigrokcxx/libsigrokcxx.hpp>
60
61#ifdef ENABLE_DECODE
62#include <libsigrokdecode/libsigrokdecode.h>
63#endif
64
65using boost::shared_lock;
66using boost::shared_mutex;
67using boost::unique_lock;
68
69using std::dynamic_pointer_cast;
70using std::function;
71using std::lock_guard;
72using std::list;
73using std::map;
74using std::mutex;
75using std::recursive_mutex;
76using std::set;
77using std::shared_ptr;
78using std::string;
79using std::unordered_set;
80using std::vector;
81
82using sigrok::Analog;
83using sigrok::Channel;
84using sigrok::ChannelType;
85using sigrok::ConfigKey;
86using sigrok::DatafeedCallbackFunction;
87using sigrok::Error;
88using sigrok::Header;
89using sigrok::Logic;
90using sigrok::Meta;
91using sigrok::Packet;
92using sigrok::PacketPayload;
93using sigrok::Session;
94using sigrok::SessionDevice;
95
96using Glib::VariantBase;
97using Glib::Variant;
98
99namespace pv {
100Session::Session(DeviceManager &device_manager, QString name) :
101 device_manager_(device_manager),
102 name_(name),
103 capture_state_(Stopped),
104 cur_samplerate_(0)
105{
106}
107
108Session::~Session()
109{
110 // Stop and join to the thread
111 stop_capture();
112}
113
114DeviceManager& Session::device_manager()
115{
116 return device_manager_;
117}
118
119const DeviceManager& Session::device_manager() const
120{
121 return device_manager_;
122}
123
124shared_ptr<sigrok::Session> Session::session() const
125{
126 if (!device_)
127 return shared_ptr<sigrok::Session>();
128 return device_->session();
129}
130
131shared_ptr<devices::Device> Session::device() const
132{
133 return device_;
134}
135
136QString Session::name() const
137{
138 return name_;
139}
140
141void Session::set_name(QString name)
142{
143 if (default_name_.isEmpty())
144 default_name_ = name;
145
146 name_ = name;
147
148 name_changed();
149}
150
151std::shared_ptr<pv::view::View> Session::main_view() const
152{
153 return main_view_;
154}
155
156void Session::set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar)
157{
158 main_bar_ = main_bar;
159}
160
161shared_ptr<pv::toolbars::MainBar> Session::main_bar() const
162{
163 return main_bar_;
164}
165
166void Session::save_settings(QSettings &settings) const
167{
168 map<string, string> dev_info;
169 list<string> key_list;
170 int stacks = 0, views = 0;
171
172 if (device_) {
173 shared_ptr<devices::HardwareDevice> hw_device =
174 dynamic_pointer_cast< devices::HardwareDevice >(device_);
175
176 if (hw_device) {
177 settings.setValue("device_type", "hardware");
178 settings.beginGroup("device");
179
180 key_list.push_back("vendor");
181 key_list.push_back("model");
182 key_list.push_back("version");
183 key_list.push_back("serial_num");
184 key_list.push_back("connection_id");
185
186 dev_info = device_manager_.get_device_info(device_);
187
188 for (string key : key_list) {
189 if (dev_info.count(key))
190 settings.setValue(QString::fromUtf8(key.c_str()),
191 QString::fromUtf8(dev_info.at(key).c_str()));
192 else
193 settings.remove(QString::fromUtf8(key.c_str()));
194 }
195
196 settings.endGroup();
197 }
198
199 shared_ptr<devices::SessionFile> sessionfile_device =
200 dynamic_pointer_cast< devices::SessionFile >(device_);
201
202 if (sessionfile_device) {
203 settings.setValue("device_type", "sessionfile");
204 settings.beginGroup("device");
205 settings.setValue("filename", QString::fromStdString(
206 sessionfile_device->full_name()));
207 settings.endGroup();
208 }
209
210 // Save channels and decoders
211 for (shared_ptr<data::SignalBase> base : signalbases_) {
212#ifdef ENABLE_DECODE
213 if (base->is_decode_signal()) {
214 shared_ptr<pv::data::DecoderStack> decoder_stack =
215 base->decoder_stack();
216 std::shared_ptr<data::decode::Decoder> top_decoder =
217 decoder_stack->stack().front();
218
219 settings.beginGroup("decoder_stack" + QString::number(stacks++));
220 settings.setValue("id", top_decoder->decoder()->id);
221 settings.setValue("name", top_decoder->decoder()->name);
222 settings.endGroup();
223 } else
224#endif
225 {
226 settings.beginGroup(base->internal_name());
227 base->save_settings(settings);
228 settings.endGroup();
229 }
230 }
231
232 settings.setValue("decoder_stacks", stacks);
233
234 // Save view states and their signal settings
235 // Note: main_view must be saved as view0
236 settings.beginGroup("view" + QString::number(views++));
237 main_view_->save_settings(settings);
238 settings.endGroup();
239
240 for (shared_ptr<view::View> view : views_) {
241 if (view != main_view_) {
242 settings.beginGroup("view" + QString::number(views++));
243 view->save_settings(settings);
244 settings.endGroup();
245 }
246 }
247
248 settings.setValue("views", views);
249 }
250}
251
252void Session::restore_settings(QSettings &settings)
253{
254 shared_ptr<devices::Device> device;
255
256 QString device_type = settings.value("device_type").toString();
257
258 if (device_type == "hardware") {
259 map<string, string> dev_info;
260 list<string> key_list;
261
262 // Re-select last used device if possible but only if it's not demo
263 settings.beginGroup("device");
264 key_list.push_back("vendor");
265 key_list.push_back("model");
266 key_list.push_back("version");
267 key_list.push_back("serial_num");
268 key_list.push_back("connection_id");
269
270 for (string key : key_list) {
271 const QString k = QString::fromStdString(key);
272 if (!settings.contains(k))
273 continue;
274
275 const string value = settings.value(k).toString().toStdString();
276 if (!value.empty())
277 dev_info.insert(std::make_pair(key, value));
278 }
279
280 if (dev_info.count("model") > 0)
281 device = device_manager_.find_device_from_info(dev_info);
282
283 if (device)
284 set_device(device);
285
286 settings.endGroup();
287 }
288
289 if (device_type == "sessionfile") {
290 settings.beginGroup("device");
291 QString filename = settings.value("filename").toString();
292 settings.endGroup();
293
294 if (QFileInfo(filename).isReadable()) {
295 device = std::make_shared<devices::SessionFile>(device_manager_.context(),
296 filename.toStdString());
297 set_device(device);
298 set_name(filename);
299
300 // TODO Perform error handling
301 start_capture([](QString infoMessage) { (void)infoMessage; });
302 }
303 }
304
305 if (device) {
306 // Restore channels
307 for (shared_ptr<data::SignalBase> base : signalbases_) {
308 settings.beginGroup(base->internal_name());
309 base->restore_settings(settings);
310 settings.endGroup();
311 }
312
313 // Restore decoders
314#ifdef ENABLE_DECODE
315 int stacks = settings.value("decoder_stacks").toInt();
316
317 for (int i = 0; i < stacks; i++) {
318 settings.beginGroup("decoder_stack" + QString::number(i++));
319
320 QString id = settings.value("id").toString();
321 add_decoder(srd_decoder_get_by_id(id.toStdString().c_str()));
322
323 settings.endGroup();
324 }
325#endif
326
327 // Restore views
328 int views = settings.value("views").toInt();
329
330 for (int i = 0; i < views; i++) {
331 settings.beginGroup("view" + QString::number(i));
332
333 if (i > 0) {
334 view::ViewType type = (view::ViewType)settings.value("type").toInt();
335 add_view(name_, type, this);
336 views_.back()->restore_settings(settings);
337 } else
338 main_view_->restore_settings(settings);
339
340 settings.endGroup();
341 }
342 }
343}
344
345void Session::set_device(shared_ptr<devices::Device> device)
346{
347 assert(device);
348
349 // Ensure we are not capturing before setting the device
350 stop_capture();
351
352 if (device_)
353 device_->close();
354
355 device_.reset();
356
357 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
358 name_ = default_name_;
359 name_changed();
360
361 // Remove all stored data
362 for (std::shared_ptr<pv::view::View> view : views_) {
363 view->clear_signals();
364#ifdef ENABLE_DECODE
365 view->clear_decode_traces();
366#endif
367 }
368 for (const shared_ptr<data::SignalData> d : all_signal_data_)
369 d->clear();
370 all_signal_data_.clear();
371 signalbases_.clear();
372 cur_logic_segment_.reset();
373
374 for (auto entry : cur_analog_segments_) {
375 shared_ptr<sigrok::Channel>(entry.first).reset();
376 shared_ptr<data::AnalogSegment>(entry.second).reset();
377 }
378
379 logic_data_.reset();
380
381 signals_changed();
382
383 device_ = std::move(device);
384
385 try {
386 device_->open();
387 } catch (const QString &e) {
388 device_.reset();
389 device_changed();
390 throw;
391 }
392
393 device_->session()->add_datafeed_callback([=]
394 (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
395 data_feed_in(device, packet);
396 });
397
398 update_signals();
399 device_changed();
400}
401
402void Session::set_default_device()
403{
404 const list< shared_ptr<devices::HardwareDevice> > &devices =
405 device_manager_.devices();
406
407 if (devices.empty())
408 return;
409
410 // Try and find the demo device and select that by default
411 const auto iter = std::find_if(devices.begin(), devices.end(),
412 [] (const shared_ptr<devices::HardwareDevice> &d) {
413 return d->hardware_device()->driver()->name() ==
414 "demo"; });
415 set_device((iter == devices.end()) ? devices.front() : *iter);
416}
417
418Session::capture_state Session::get_capture_state() const
419{
420 lock_guard<mutex> lock(sampling_mutex_);
421 return capture_state_;
422}
423
424void Session::start_capture(function<void (const QString)> error_handler)
425{
426 if (!device_) {
427 error_handler(tr("No active device set, can't start acquisition."));
428 return;
429 }
430
431 stop_capture();
432
433 // Check that at least one channel is enabled
434 const shared_ptr<sigrok::Device> sr_dev = device_->device();
435 if (sr_dev) {
436 const auto channels = sr_dev->channels();
437 if (!std::any_of(channels.begin(), channels.end(),
438 [](shared_ptr<Channel> channel) {
439 return channel->enabled(); })) {
440 error_handler(tr("No channels enabled."));
441 return;
442 }
443 }
444
445 // Clear signal data
446 for (const shared_ptr<data::SignalData> d : all_signal_data_)
447 d->clear();
448
449 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
450 name_ = default_name_;
451 name_changed();
452
453 // Begin the session
454 sampling_thread_ = std::thread(
455 &Session::sample_thread_proc, this, error_handler);
456}
457
458void Session::stop_capture()
459{
460 if (get_capture_state() != Stopped)
461 device_->stop();
462
463 // Check that sampling stopped
464 if (sampling_thread_.joinable())
465 sampling_thread_.join();
466}
467
468void Session::register_view(std::shared_ptr<pv::view::View> view)
469{
470 if (views_.empty()) {
471 main_view_ = view;
472 }
473
474 views_.push_back(view);
475}
476
477void Session::deregister_view(std::shared_ptr<pv::view::View> view)
478{
479 views_.remove_if([&](std::shared_ptr<pv::view::View> v) {
480 return v == view; });
481
482 if (views_.empty()) {
483 main_view_.reset();
484
485 // Without a view there can be no main bar
486 main_bar_.reset();
487 }
488}
489
490bool Session::has_view(std::shared_ptr<pv::view::View> view)
491{
492 for (std::shared_ptr<pv::view::View> v : views_)
493 if (v == view)
494 return true;
495
496 return false;
497}
498
499double Session::get_samplerate() const
500{
501 double samplerate = 0.0;
502
503 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
504 assert(d);
505 const vector< shared_ptr<pv::data::Segment> > segments =
506 d->segments();
507 for (const shared_ptr<pv::data::Segment> &s : segments)
508 samplerate = std::max(samplerate, s->samplerate());
509 }
510 // If there is no sample rate given we use samples as unit
511 if (samplerate == 0.0)
512 samplerate = 1.0;
513
514 return samplerate;
515}
516
517const std::unordered_set< std::shared_ptr<data::SignalBase> >
518 Session::signalbases() const
519{
520 return signalbases_;
521}
522
523#ifdef ENABLE_DECODE
524bool Session::add_decoder(srd_decoder *const dec)
525{
526 map<const srd_channel*, shared_ptr<data::SignalBase> > channels;
527 shared_ptr<data::DecoderStack> decoder_stack;
528
529 try {
530 // Create the decoder
531 decoder_stack = shared_ptr<data::DecoderStack>(
532 new data::DecoderStack(*this, dec));
533
534 // Make a list of all the channels
535 std::vector<const srd_channel*> all_channels;
536 for (const GSList *i = dec->channels; i; i = i->next)
537 all_channels.push_back((const srd_channel*)i->data);
538 for (const GSList *i = dec->opt_channels; i; i = i->next)
539 all_channels.push_back((const srd_channel*)i->data);
540
541 // Auto select the initial channels
542 for (const srd_channel *pdch : all_channels)
543 for (shared_ptr<data::SignalBase> b : signalbases_) {
544 if (b->type() == ChannelType::LOGIC) {
545 if (QString::fromUtf8(pdch->name).toLower().
546 contains(b->name().toLower()))
547 channels[pdch] = b;
548 }
549 }
550
551 assert(decoder_stack);
552 assert(!decoder_stack->stack().empty());
553 assert(decoder_stack->stack().front());
554 decoder_stack->stack().front()->set_channels(channels);
555
556 // Create the decode signal
557 shared_ptr<data::SignalBase> signalbase =
558 shared_ptr<data::SignalBase>(new data::SignalBase(nullptr));
559
560 signalbase->set_decoder_stack(decoder_stack);
561 signalbases_.insert(signalbase);
562
563 for (std::shared_ptr<pv::view::View> view : views_)
564 view->add_decode_trace(signalbase);
565 } catch (std::runtime_error e) {
566 return false;
567 }
568
569 signals_changed();
570
571 // Do an initial decode
572 decoder_stack->begin_decode();
573
574 return true;
575}
576
577void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase)
578{
579 for (std::shared_ptr<pv::view::View> view : views_)
580 view->remove_decode_trace(signalbase);
581}
582#endif
583
584void Session::set_capture_state(capture_state state)
585{
586 bool changed;
587
588 {
589 lock_guard<mutex> lock(sampling_mutex_);
590 changed = capture_state_ != state;
591 capture_state_ = state;
592 }
593
594 if (changed)
595 capture_state_changed(state);
596}
597
598void Session::update_signals()
599{
600 if (!device_) {
601 signalbases_.clear();
602 logic_data_.reset();
603 for (std::shared_ptr<pv::view::View> view : views_) {
604 view->clear_signals();
605#ifdef ENABLE_DECODE
606 view->clear_decode_traces();
607#endif
608 }
609 return;
610 }
611
612 lock_guard<recursive_mutex> lock(data_mutex_);
613
614 const shared_ptr<sigrok::Device> sr_dev = device_->device();
615 if (!sr_dev) {
616 signalbases_.clear();
617 logic_data_.reset();
618 for (std::shared_ptr<pv::view::View> view : views_) {
619 view->clear_signals();
620#ifdef ENABLE_DECODE
621 view->clear_decode_traces();
622#endif
623 }
624 return;
625 }
626
627 // Detect what data types we will receive
628 auto channels = sr_dev->channels();
629 unsigned int logic_channel_count = std::count_if(
630 channels.begin(), channels.end(),
631 [] (shared_ptr<Channel> channel) {
632 return channel->type() == ChannelType::LOGIC; });
633
634 // Create data containers for the logic data segments
635 {
636 lock_guard<recursive_mutex> data_lock(data_mutex_);
637
638 if (logic_channel_count == 0) {
639 logic_data_.reset();
640 } else if (!logic_data_ ||
641 logic_data_->num_channels() != logic_channel_count) {
642 logic_data_.reset(new data::Logic(
643 logic_channel_count));
644 assert(logic_data_);
645 }
646 }
647
648 // Make the signals list
649 for (std::shared_ptr<pv::view::View> view : views_) {
650 unordered_set< shared_ptr<view::Signal> > prev_sigs(view->signals());
651 view->clear_signals();
652
653 for (auto channel : sr_dev->channels()) {
654 shared_ptr<data::SignalBase> signalbase;
655 shared_ptr<view::Signal> signal;
656
657 // Find the channel in the old signals
658 const auto iter = std::find_if(
659 prev_sigs.cbegin(), prev_sigs.cend(),
660 [&](const shared_ptr<view::Signal> &s) {
661 return s->base()->channel() == channel;
662 });
663 if (iter != prev_sigs.end()) {
664 // Copy the signal from the old set to the new
665 signal = *iter;
666 } else {
667 // Find the signalbase for this channel if possible
668 signalbase.reset();
669 for (const shared_ptr<data::SignalBase> b : signalbases_)
670 if (b->channel() == channel)
671 signalbase = b;
672
673 switch(channel->type()->id()) {
674 case SR_CHANNEL_LOGIC:
675 if (!signalbase) {
676 signalbase = shared_ptr<data::SignalBase>(
677 new data::SignalBase(channel));
678 signalbases_.insert(signalbase);
679
680 all_signal_data_.insert(logic_data_);
681 signalbase->set_data(logic_data_);
682 }
683
684 signal = shared_ptr<view::Signal>(
685 new view::LogicSignal(*this,
686 device_, signalbase));
687 view->add_signal(signal);
688 break;
689
690 case SR_CHANNEL_ANALOG:
691 {
692 if (!signalbase) {
693 signalbase = shared_ptr<data::SignalBase>(
694 new data::SignalBase(channel));
695 signalbases_.insert(signalbase);
696
697 shared_ptr<data::Analog> data(new data::Analog());
698 all_signal_data_.insert(data);
699 signalbase->set_data(data);
700 }
701
702 signal = shared_ptr<view::Signal>(
703 new view::AnalogSignal(
704 *this, signalbase));
705 view->add_signal(signal);
706 break;
707 }
708
709 default:
710 assert(0);
711 break;
712 }
713 }
714 }
715 }
716
717 signals_changed();
718}
719
720shared_ptr<data::SignalBase> Session::signalbase_from_channel(
721 shared_ptr<sigrok::Channel> channel) const
722{
723 for (shared_ptr<data::SignalBase> sig : signalbases_) {
724 assert(sig);
725 if (sig->channel() == channel)
726 return sig;
727 }
728 return shared_ptr<data::SignalBase>();
729}
730
731void Session::sample_thread_proc(function<void (const QString)> error_handler)
732{
733 assert(error_handler);
734
735 if (!device_)
736 return;
737
738 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
739
740 out_of_memory_ = false;
741
742 try {
743 device_->start();
744 } catch (Error e) {
745 error_handler(e.what());
746 return;
747 }
748
749 set_capture_state(device_->session()->trigger() ?
750 AwaitingTrigger : Running);
751
752 device_->run();
753 set_capture_state(Stopped);
754
755 // Confirm that SR_DF_END was received
756 if (cur_logic_segment_) {
757 qDebug("SR_DF_END was not received.");
758 assert(0);
759 }
760
761 if (out_of_memory_)
762 error_handler(tr("Out of memory, acquisition stopped."));
763}
764
765void Session::feed_in_header()
766{
767 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
768}
769
770void Session::feed_in_meta(shared_ptr<Meta> meta)
771{
772 for (auto entry : meta->config()) {
773 switch (entry.first->id()) {
774 case SR_CONF_SAMPLERATE:
775 // We can't rely on the header to always contain the sample rate,
776 // so in case it's supplied via a meta packet, we use it.
777 if (!cur_samplerate_)
778 cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
779
780 /// @todo handle samplerate changes
781 break;
782 default:
783 // Unknown metadata is not an error.
784 break;
785 }
786 }
787
788 signals_changed();
789}
790
791void Session::feed_in_trigger()
792{
793 // The channel containing most samples should be most accurate
794 uint64_t sample_count = 0;
795
796 {
797 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
798 assert(d);
799 uint64_t temp_count = 0;
800
801 const vector< shared_ptr<pv::data::Segment> > segments =
802 d->segments();
803 for (const shared_ptr<pv::data::Segment> &s : segments)
804 temp_count += s->get_sample_count();
805
806 if (temp_count > sample_count)
807 sample_count = temp_count;
808 }
809 }
810
811 trigger_event(sample_count / get_samplerate());
812}
813
814void Session::feed_in_frame_begin()
815{
816 if (cur_logic_segment_ || !cur_analog_segments_.empty())
817 frame_began();
818}
819
820void Session::feed_in_logic(shared_ptr<Logic> logic)
821{
822 lock_guard<recursive_mutex> lock(data_mutex_);
823
824 const size_t sample_count = logic->data_length() / logic->unit_size();
825
826 if (!logic_data_) {
827 // The only reason logic_data_ would not have been created is
828 // if it was not possible to determine the signals when the
829 // device was created.
830 update_signals();
831 }
832
833 if (!cur_logic_segment_) {
834 // This could be the first packet after a trigger
835 set_capture_state(Running);
836
837 // Create a new data segment
838 cur_logic_segment_ = shared_ptr<data::LogicSegment>(
839 new data::LogicSegment(
840 logic, cur_samplerate_, sample_count));
841 logic_data_->push_segment(cur_logic_segment_);
842
843 // @todo Putting this here means that only listeners querying
844 // for logic will be notified. Currently the only user of
845 // frame_began is DecoderStack, but in future we need to signal
846 // this after both analog and logic sweeps have begun.
847 frame_began();
848 } else {
849 // Append to the existing data segment
850 cur_logic_segment_->append_payload(logic);
851 }
852
853 data_received();
854}
855
856void Session::feed_in_analog(shared_ptr<Analog> analog)
857{
858 lock_guard<recursive_mutex> lock(data_mutex_);
859
860 const vector<shared_ptr<Channel>> channels = analog->channels();
861 const unsigned int channel_count = channels.size();
862 const size_t sample_count = analog->num_samples() / channel_count;
863 const float *data = static_cast<const float *>(analog->data_pointer());
864 bool sweep_beginning = false;
865
866 if (signalbases_.empty())
867 update_signals();
868
869 for (auto channel : channels) {
870 shared_ptr<data::AnalogSegment> segment;
871
872 // Try to get the segment of the channel
873 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >::
874 iterator iter = cur_analog_segments_.find(channel);
875 if (iter != cur_analog_segments_.end())
876 segment = (*iter).second;
877 else {
878 // If no segment was found, this means we haven't
879 // created one yet. i.e. this is the first packet
880 // in the sweep containing this segment.
881 sweep_beginning = true;
882
883 // Create a segment, keep it in the maps of channels
884 segment = shared_ptr<data::AnalogSegment>(
885 new data::AnalogSegment(
886 cur_samplerate_, sample_count));
887 cur_analog_segments_[channel] = segment;
888
889 // Find the analog data associated with the channel
890 shared_ptr<data::SignalBase> base = signalbase_from_channel(channel);
891 assert(base);
892
893 shared_ptr<data::Analog> data(base->analog_data());
894 assert(data);
895
896 // Push the segment into the analog data.
897 data->push_segment(segment);
898 }
899
900 assert(segment);
901
902 // Append the samples in the segment
903 segment->append_interleaved_samples(data++, sample_count,
904 channel_count);
905 }
906
907 if (sweep_beginning) {
908 // This could be the first packet after a trigger
909 set_capture_state(Running);
910 }
911
912 data_received();
913}
914
915void Session::data_feed_in(shared_ptr<sigrok::Device> device,
916 shared_ptr<Packet> packet)
917{
918 (void)device;
919
920 assert(device);
921 assert(device == device_->device());
922 assert(packet);
923
924 switch (packet->type()->id()) {
925 case SR_DF_HEADER:
926 feed_in_header();
927 break;
928
929 case SR_DF_META:
930 feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
931 break;
932
933 case SR_DF_TRIGGER:
934 feed_in_trigger();
935 break;
936
937 case SR_DF_FRAME_BEGIN:
938 feed_in_frame_begin();
939 break;
940
941 case SR_DF_LOGIC:
942 try {
943 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
944 } catch (std::bad_alloc) {
945 out_of_memory_ = true;
946 device_->stop();
947 }
948 break;
949
950 case SR_DF_ANALOG:
951 try {
952 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
953 } catch (std::bad_alloc) {
954 out_of_memory_ = true;
955 device_->stop();
956 }
957 break;
958
959 case SR_DF_END:
960 {
961 {
962 lock_guard<recursive_mutex> lock(data_mutex_);
963 cur_logic_segment_.reset();
964 cur_analog_segments_.clear();
965 }
966 frame_ended();
967 break;
968 }
969 default:
970 break;
971 }
972}
973
974} // namespace pv