2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include "devicemanager.hpp"
31 #include "session.hpp"
33 #include "data/analog.hpp"
34 #include "data/analogsegment.hpp"
35 #include "data/decode/decoder.hpp"
36 #include "data/logic.hpp"
37 #include "data/logicsegment.hpp"
38 #include "data/signalbase.hpp"
40 #include "devices/hardwaredevice.hpp"
41 #include "devices/inputfile.hpp"
42 #include "devices/sessionfile.hpp"
44 #include "toolbars/mainbar.hpp"
46 #include "views/trace/analogsignal.hpp"
47 #include "views/trace/decodetrace.hpp"
48 #include "views/trace/logicsignal.hpp"
49 #include "views/trace/signal.hpp"
50 #include "views/trace/view.hpp"
52 #include <libsigrokcxx/libsigrokcxx.hpp>
55 #include <libsigrokdecode/libsigrokdecode.h>
56 #include "data/decodesignal.hpp"
60 using std::dynamic_pointer_cast;
63 using std::lock_guard;
66 using std::make_shared;
72 using std::recursive_mutex;
73 using std::runtime_error;
74 using std::shared_ptr;
76 using std::unique_ptr;
77 using std::unordered_set;
81 using sigrok::Channel;
82 using sigrok::ConfigKey;
83 using sigrok::DatafeedCallbackFunction;
85 using sigrok::InputFormat;
89 using sigrok::Session;
91 using Glib::VariantBase;
95 shared_ptr<sigrok::Context> Session::sr_context;
97 Session::Session(DeviceManager &device_manager, QString name) :
98 device_manager_(device_manager),
101 capture_state_(Stopped),
109 // Stop and join to the thread
113 DeviceManager& Session::device_manager()
115 return device_manager_;
118 const DeviceManager& Session::device_manager() const
120 return device_manager_;
123 shared_ptr<sigrok::Session> Session::session() const
126 return shared_ptr<sigrok::Session>();
127 return device_->session();
130 shared_ptr<devices::Device> Session::device() const
135 QString Session::name() const
140 void Session::set_name(QString name)
142 if (default_name_.isEmpty())
143 default_name_ = name;
150 const list< shared_ptr<views::ViewBase> > Session::views() const
155 shared_ptr<views::ViewBase> Session::main_view() const
160 void Session::set_main_bar(shared_ptr<pv::toolbars::MainBar> main_bar)
162 main_bar_ = main_bar;
165 shared_ptr<pv::toolbars::MainBar> Session::main_bar() const
170 bool Session::data_saved() const
175 void Session::save_settings(QSettings &settings) const
177 map<string, string> dev_info;
178 list<string> key_list;
179 int decode_signals = 0, views = 0;
182 shared_ptr<devices::HardwareDevice> hw_device =
183 dynamic_pointer_cast< devices::HardwareDevice >(device_);
186 settings.setValue("device_type", "hardware");
187 settings.beginGroup("device");
189 key_list.emplace_back("vendor");
190 key_list.emplace_back("model");
191 key_list.emplace_back("version");
192 key_list.emplace_back("serial_num");
193 key_list.emplace_back("connection_id");
195 dev_info = device_manager_.get_device_info(device_);
197 for (string key : key_list) {
198 if (dev_info.count(key))
199 settings.setValue(QString::fromUtf8(key.c_str()),
200 QString::fromUtf8(dev_info.at(key).c_str()));
202 settings.remove(QString::fromUtf8(key.c_str()));
208 shared_ptr<devices::SessionFile> sessionfile_device =
209 dynamic_pointer_cast< devices::SessionFile >(device_);
211 if (sessionfile_device) {
212 settings.setValue("device_type", "sessionfile");
213 settings.beginGroup("device");
214 settings.setValue("filename", QString::fromStdString(
215 sessionfile_device->full_name()));
219 // Save channels and decoders
220 for (shared_ptr<data::SignalBase> base : signalbases_) {
222 if (base->is_decode_signal()) {
223 settings.beginGroup("decode_signal" + QString::number(decode_signals++));
224 base->save_settings(settings);
229 settings.beginGroup(base->internal_name());
230 base->save_settings(settings);
235 settings.setValue("decode_signals", decode_signals);
237 // Save view states and their signal settings
238 // Note: main_view must be saved as view0
239 settings.beginGroup("view" + QString::number(views++));
240 main_view_->save_settings(settings);
243 for (shared_ptr<views::ViewBase> view : views_) {
244 if (view != main_view_) {
245 settings.beginGroup("view" + QString::number(views++));
246 view->save_settings(settings);
251 settings.setValue("views", views);
255 void Session::restore_settings(QSettings &settings)
257 shared_ptr<devices::Device> device;
259 QString device_type = settings.value("device_type").toString();
261 if (device_type == "hardware") {
262 map<string, string> dev_info;
263 list<string> key_list;
265 // Re-select last used device if possible but only if it's not demo
266 settings.beginGroup("device");
267 key_list.emplace_back("vendor");
268 key_list.emplace_back("model");
269 key_list.emplace_back("version");
270 key_list.emplace_back("serial_num");
271 key_list.emplace_back("connection_id");
273 for (string key : key_list) {
274 const QString k = QString::fromStdString(key);
275 if (!settings.contains(k))
278 const string value = settings.value(k).toString().toStdString();
280 dev_info.insert(make_pair(key, value));
283 if (dev_info.count("model") > 0)
284 device = device_manager_.find_device_from_info(dev_info);
292 if (device_type == "sessionfile") {
293 settings.beginGroup("device");
294 QString filename = settings.value("filename").toString();
297 if (QFileInfo(filename).isReadable()) {
298 device = make_shared<devices::SessionFile>(device_manager_.context(),
299 filename.toStdString());
302 // TODO Perform error handling
303 start_capture([](QString infoMessage) { (void)infoMessage; });
305 set_name(QFileInfo(filename).fileName());
311 for (shared_ptr<data::SignalBase> base : signalbases_) {
312 settings.beginGroup(base->internal_name());
313 base->restore_settings(settings);
319 int decode_signals = settings.value("decode_signals").toInt();
321 for (int i = 0; i < decode_signals; i++) {
322 settings.beginGroup("decode_signal" + QString::number(i));
323 shared_ptr<data::DecodeSignal> signal = add_decode_signal();
324 signal->restore_settings(settings);
330 int views = settings.value("views").toInt();
332 for (int i = 0; i < views; i++) {
333 settings.beginGroup("view" + QString::number(i));
336 views::ViewType type = (views::ViewType)settings.value("type").toInt();
337 add_view(name_, type, this);
338 views_.back()->restore_settings(settings);
340 main_view_->restore_settings(settings);
347 void Session::select_device(shared_ptr<devices::Device> device)
353 set_default_device();
354 } catch (const QString &e) {
355 main_bar_->session_error(tr("Failed to select device"), e);
359 void Session::set_device(shared_ptr<devices::Device> device)
363 // Ensure we are not capturing before setting the device
371 // Revert name back to default name (e.g. "Session 1") as the data is gone
372 name_ = default_name_;
375 // Remove all stored data
376 for (shared_ptr<views::ViewBase> view : views_) {
377 view->clear_signals();
379 view->clear_decode_signals();
382 for (const shared_ptr<data::SignalData> d : all_signal_data_)
384 all_signal_data_.clear();
385 signalbases_.clear();
386 cur_logic_segment_.reset();
388 for (auto entry : cur_analog_segments_) {
389 shared_ptr<sigrok::Channel>(entry.first).reset();
390 shared_ptr<data::AnalogSegment>(entry.second).reset();
397 device_ = move(device);
401 } catch (const QString &e) {
403 main_bar_->session_error(tr("Failed to open device"), e);
407 device_->session()->add_datafeed_callback([=]
408 (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
409 data_feed_in(device, packet);
418 void Session::set_default_device()
420 const list< shared_ptr<devices::HardwareDevice> > &devices =
421 device_manager_.devices();
426 // Try and find the demo device and select that by default
427 const auto iter = find_if(devices.begin(), devices.end(),
428 [] (const shared_ptr<devices::HardwareDevice> &d) {
429 return d->hardware_device()->driver()->name() == "demo"; });
430 set_device((iter == devices.end()) ? devices.front() : *iter);
434 * Convert generic options to data types that are specific to InputFormat.
436 * @param[in] user_spec Vector of tokenized words, string format.
437 * @param[in] fmt_opts Input format's options, result of InputFormat::options().
439 * @return Map of options suitable for InputFormat::create_input().
441 map<string, Glib::VariantBase>
442 Session::input_format_options(vector<string> user_spec,
443 map<string, shared_ptr<Option>> fmt_opts)
445 map<string, Glib::VariantBase> result;
447 for (auto entry : user_spec) {
449 * Split key=value specs. Accept entries without separator
450 * (for simplified boolean specifications).
453 size_t pos = entry.find("=");
454 if (pos == std::string::npos) {
458 key = entry.substr(0, pos);
459 val = entry.substr(pos + 1);
463 * Skip user specifications that are not a member of the
464 * format's set of supported options. Have the text input
465 * spec converted to the required input format specific
468 auto found = fmt_opts.find(key);
469 if (found == fmt_opts.end())
471 shared_ptr<Option> opt = found->second;
472 result[key] = opt->parse_string(val);
478 void Session::load_init_file(const string &file_name, const string &format)
480 shared_ptr<InputFormat> input_format;
481 map<string, Glib::VariantBase> input_opts;
483 if (!format.empty()) {
484 const map<string, shared_ptr<InputFormat> > formats =
485 device_manager_.context()->input_formats();
486 auto user_opts = pv::util::split_string(format, ":");
487 string user_name = user_opts.front();
488 user_opts.erase(user_opts.begin());
489 const auto iter = find_if(formats.begin(), formats.end(),
490 [&](const pair<string, shared_ptr<InputFormat> > f) {
491 return f.first == user_name; });
492 if (iter == formats.end()) {
493 main_bar_->session_error(tr("Error"),
494 tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
497 input_format = (*iter).second;
498 input_opts = input_format_options(user_opts,
499 input_format->options());
502 load_file(QString::fromStdString(file_name), input_format, input_opts);
505 void Session::load_file(QString file_name,
506 shared_ptr<sigrok::InputFormat> format,
507 const map<string, Glib::VariantBase> &options)
509 const QString errorMessage(
510 QString("Failed to load file %1").arg(file_name));
514 set_device(shared_ptr<devices::Device>(
515 new devices::InputFile(
516 device_manager_.context(),
517 file_name.toStdString(),
520 set_device(shared_ptr<devices::Device>(
521 new devices::SessionFile(
522 device_manager_.context(),
523 file_name.toStdString())));
525 main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
526 set_default_device();
527 main_bar_->update_device_list();
531 main_bar_->update_device_list();
533 start_capture([&, errorMessage](QString infoMessage) {
534 main_bar_->session_error(errorMessage, infoMessage); });
536 set_name(QFileInfo(file_name).fileName());
539 Session::capture_state Session::get_capture_state() const
541 lock_guard<mutex> lock(sampling_mutex_);
542 return capture_state_;
545 void Session::start_capture(function<void (const QString)> error_handler)
548 error_handler(tr("No active device set, can't start acquisition."));
554 // Check that at least one channel is enabled
555 const shared_ptr<sigrok::Device> sr_dev = device_->device();
557 const auto channels = sr_dev->channels();
558 if (!any_of(channels.begin(), channels.end(),
559 [](shared_ptr<Channel> channel) {
560 return channel->enabled(); })) {
561 error_handler(tr("No channels enabled."));
567 for (const shared_ptr<data::SignalData> d : all_signal_data_)
570 trigger_list_.clear();
572 // Revert name back to default name (e.g. "Session 1") for real devices
573 // as the (possibly saved) data is gone. File devices keep their name.
574 shared_ptr<devices::HardwareDevice> hw_device =
575 dynamic_pointer_cast< devices::HardwareDevice >(device_);
578 name_ = default_name_;
583 sampling_thread_ = std::thread(
584 &Session::sample_thread_proc, this, error_handler);
587 void Session::stop_capture()
589 if (get_capture_state() != Stopped)
592 // Check that sampling stopped
593 if (sampling_thread_.joinable())
594 sampling_thread_.join();
597 void Session::register_view(shared_ptr<views::ViewBase> view)
599 if (views_.empty()) {
603 views_.push_back(view);
605 // Add all device signals
608 // Add all other signals
609 unordered_set< shared_ptr<data::SignalBase> > view_signalbases =
612 views::trace::View *trace_view =
613 qobject_cast<views::trace::View*>(view.get());
616 for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
617 const int sb_exists = count_if(
618 view_signalbases.cbegin(), view_signalbases.cend(),
619 [&](const shared_ptr<data::SignalBase> &sb) {
620 return sb == signalbase;
622 // Add the signal to the view as it doesn't have it yet
624 switch (signalbase->type()) {
625 case data::SignalBase::AnalogChannel:
626 case data::SignalBase::LogicChannel:
627 case data::SignalBase::DecodeChannel:
629 trace_view->add_decode_signal(
630 dynamic_pointer_cast<data::DecodeSignal>(signalbase));
633 case data::SignalBase::MathChannel:
643 void Session::deregister_view(shared_ptr<views::ViewBase> view)
645 views_.remove_if([&](shared_ptr<views::ViewBase> v) { return v == view; });
647 if (views_.empty()) {
650 // Without a view there can be no main bar
655 bool Session::has_view(shared_ptr<views::ViewBase> view)
657 for (shared_ptr<views::ViewBase> v : views_)
664 double Session::get_samplerate() const
666 double samplerate = 0.0;
668 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
670 const vector< shared_ptr<pv::data::Segment> > segments =
672 for (const shared_ptr<pv::data::Segment> &s : segments)
673 samplerate = max(samplerate, s->samplerate());
675 // If there is no sample rate given we use samples as unit
676 if (samplerate == 0.0)
682 uint32_t Session::get_segment_count() const
686 // Find the highest number of segments
687 for (shared_ptr<data::SignalData> data : all_signal_data_)
688 if (data->get_segment_count() > value)
689 value = data->get_segment_count();
694 vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const
696 vector<util::Timestamp> result;
698 for (pair<uint32_t, util::Timestamp> entry : trigger_list_)
699 if (entry.first == segment_id)
700 result.push_back(entry.second);
705 const unordered_set< shared_ptr<data::SignalBase> > Session::signalbases() const
710 bool Session::all_segments_complete(uint32_t segment_id) const
712 bool all_complete = true;
714 for (shared_ptr<data::SignalBase> base : signalbases_)
715 if (!base->segment_is_complete(segment_id))
716 all_complete = false;
722 shared_ptr<data::DecodeSignal> Session::add_decode_signal()
724 shared_ptr<data::DecodeSignal> signal;
727 // Create the decode signal
728 signal = make_shared<data::DecodeSignal>(*this);
730 signalbases_.insert(signal);
732 // Add the decode signal to all views
733 for (shared_ptr<views::ViewBase> view : views_)
734 view->add_decode_signal(signal);
735 } catch (runtime_error e) {
736 remove_decode_signal(signal);
745 void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
747 signalbases_.erase(signal);
749 for (shared_ptr<views::ViewBase> view : views_)
750 view->remove_decode_signal(signal);
756 void Session::set_capture_state(capture_state state)
761 lock_guard<mutex> lock(sampling_mutex_);
762 changed = capture_state_ != state;
763 capture_state_ = state;
767 capture_state_changed(state);
770 void Session::update_signals()
773 signalbases_.clear();
775 for (shared_ptr<views::ViewBase> view : views_) {
776 view->clear_signals();
778 view->clear_decode_signals();
784 lock_guard<recursive_mutex> lock(data_mutex_);
786 const shared_ptr<sigrok::Device> sr_dev = device_->device();
788 signalbases_.clear();
790 for (shared_ptr<views::ViewBase> view : views_) {
791 view->clear_signals();
793 view->clear_decode_signals();
799 // Detect what data types we will receive
800 auto channels = sr_dev->channels();
801 unsigned int logic_channel_count = count_if(
802 channels.begin(), channels.end(),
803 [] (shared_ptr<Channel> channel) {
804 return channel->type() == sigrok::ChannelType::LOGIC; });
806 // Create data containers for the logic data segments
808 lock_guard<recursive_mutex> data_lock(data_mutex_);
810 if (logic_channel_count == 0) {
812 } else if (!logic_data_ ||
813 logic_data_->num_channels() != logic_channel_count) {
814 logic_data_.reset(new data::Logic(
815 logic_channel_count));
820 // Make the signals list
821 for (shared_ptr<views::ViewBase> viewbase : views_) {
822 views::trace::View *trace_view =
823 qobject_cast<views::trace::View*>(viewbase.get());
826 unordered_set< shared_ptr<views::trace::Signal> >
827 prev_sigs(trace_view->signals());
828 trace_view->clear_signals();
830 for (auto channel : sr_dev->channels()) {
831 shared_ptr<data::SignalBase> signalbase;
832 shared_ptr<views::trace::Signal> signal;
834 // Find the channel in the old signals
835 const auto iter = find_if(
836 prev_sigs.cbegin(), prev_sigs.cend(),
837 [&](const shared_ptr<views::trace::Signal> &s) {
838 return s->base()->channel() == channel;
840 if (iter != prev_sigs.end()) {
841 // Copy the signal from the old set to the new
843 trace_view->add_signal(signal);
845 // Find the signalbase for this channel if possible
847 for (const shared_ptr<data::SignalBase> b : signalbases_)
848 if (b->channel() == channel)
851 switch(channel->type()->id()) {
852 case SR_CHANNEL_LOGIC:
854 signalbase = make_shared<data::SignalBase>(channel,
855 data::SignalBase::LogicChannel);
856 signalbases_.insert(signalbase);
858 all_signal_data_.insert(logic_data_);
859 signalbase->set_data(logic_data_);
861 connect(this, SIGNAL(capture_state_changed(int)),
862 signalbase.get(), SLOT(on_capture_state_changed(int)));
865 signal = shared_ptr<views::trace::Signal>(
866 new views::trace::LogicSignal(*this,
867 device_, signalbase));
868 trace_view->add_signal(signal);
871 case SR_CHANNEL_ANALOG:
874 signalbase = make_shared<data::SignalBase>(channel,
875 data::SignalBase::AnalogChannel);
876 signalbases_.insert(signalbase);
878 shared_ptr<data::Analog> data(new data::Analog());
879 all_signal_data_.insert(data);
880 signalbase->set_data(data);
882 connect(this, SIGNAL(capture_state_changed(int)),
883 signalbase.get(), SLOT(on_capture_state_changed(int)));
886 signal = shared_ptr<views::trace::Signal>(
887 new views::trace::AnalogSignal(
889 trace_view->add_signal(signal);
905 shared_ptr<data::SignalBase> Session::signalbase_from_channel(
906 shared_ptr<sigrok::Channel> channel) const
908 for (shared_ptr<data::SignalBase> sig : signalbases_) {
910 if (sig->channel() == channel)
913 return shared_ptr<data::SignalBase>();
916 void Session::sample_thread_proc(function<void (const QString)> error_handler)
918 assert(error_handler);
923 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
925 out_of_memory_ = false;
928 lock_guard<recursive_mutex> lock(data_mutex_);
929 cur_logic_segment_.reset();
930 cur_analog_segments_.clear();
932 highest_segment_id_ = -1;
933 frame_began_ = false;
938 error_handler(e.what());
942 set_capture_state(device_->session()->trigger() ?
943 AwaitingTrigger : Running);
948 error_handler(e.what());
949 set_capture_state(Stopped);
953 set_capture_state(Stopped);
955 // Confirm that SR_DF_END was received
956 if (cur_logic_segment_) {
957 qDebug("SR_DF_END was not received.");
961 // Optimize memory usage
962 free_unused_memory();
964 // We now have unsaved data unless we just "captured" from a file
965 shared_ptr<devices::File> file_device =
966 dynamic_pointer_cast<devices::File>(device_);
972 error_handler(tr("Out of memory, acquisition stopped."));
975 void Session::free_unused_memory()
977 for (shared_ptr<data::SignalData> data : all_signal_data_) {
978 const vector< shared_ptr<data::Segment> > segments = data->segments();
980 for (shared_ptr<data::Segment> segment : segments) {
981 segment->free_unused_memory();
986 void Session::signal_new_segment()
988 int new_segment_id = 0;
990 if ((cur_logic_segment_ != nullptr) || !cur_analog_segments_.empty()) {
992 // Determine new frame/segment number, assuming that all
993 // signals have the same number of frames/segments
994 if (cur_logic_segment_) {
995 new_segment_id = logic_data_->get_segment_count() - 1;
997 shared_ptr<sigrok::Channel> any_channel =
998 (*cur_analog_segments_.begin()).first;
1000 shared_ptr<data::SignalBase> base = signalbase_from_channel(any_channel);
1003 shared_ptr<data::Analog> data(base->analog_data());
1006 new_segment_id = data->get_segment_count() - 1;
1010 if (new_segment_id > highest_segment_id_) {
1011 highest_segment_id_ = new_segment_id;
1012 new_segment(highest_segment_id_);
1016 void Session::signal_segment_completed()
1020 for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
1021 // We only care about analog and logic channels, not derived ones
1022 if (signalbase->type() == data::SignalBase::AnalogChannel) {
1023 segment_id = signalbase->analog_data()->get_segment_count() - 1;
1027 if (signalbase->type() == data::SignalBase::LogicChannel) {
1028 segment_id = signalbase->logic_data()->get_segment_count() - 1;
1033 if (segment_id >= 0)
1034 segment_completed(segment_id);
1037 void Session::feed_in_header()
1039 // Nothing to do here for now
1042 void Session::feed_in_meta(shared_ptr<Meta> meta)
1044 for (auto entry : meta->config()) {
1045 switch (entry.first->id()) {
1046 case SR_CONF_SAMPLERATE:
1047 // We can't rely on the header to always contain the sample rate,
1048 // so in case it's supplied via a meta packet, we use it.
1049 if (!cur_samplerate_)
1050 cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
1052 /// @todo handle samplerate changes
1055 // Unknown metadata is not an error.
1063 void Session::feed_in_trigger()
1065 // The channel containing most samples should be most accurate
1066 uint64_t sample_count = 0;
1069 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
1071 uint64_t temp_count = 0;
1073 const vector< shared_ptr<pv::data::Segment> > segments =
1075 for (const shared_ptr<pv::data::Segment> &s : segments)
1076 temp_count += s->get_sample_count();
1078 if (temp_count > sample_count)
1079 sample_count = temp_count;
1083 // If no frame began then this is a trigger for a new segment
1084 const uint32_t segment_id =
1085 (frame_began_) ? highest_segment_id_ : (highest_segment_id_ + 1);
1087 util::Timestamp timestamp = sample_count / get_samplerate();
1088 trigger_list_.emplace_back(segment_id, timestamp);
1089 trigger_event(segment_id, timestamp);
1092 void Session::feed_in_frame_begin()
1094 frame_began_ = true;
1097 void Session::feed_in_frame_end()
1103 lock_guard<recursive_mutex> lock(data_mutex_);
1105 if (cur_logic_segment_)
1106 cur_logic_segment_->set_complete();
1108 for (auto entry : cur_analog_segments_) {
1109 shared_ptr<data::AnalogSegment> segment = entry.second;
1110 segment->set_complete();
1113 cur_logic_segment_.reset();
1114 cur_analog_segments_.clear();
1117 frame_began_ = false;
1119 signal_segment_completed();
1122 void Session::feed_in_logic(shared_ptr<Logic> logic)
1124 if (!cur_samplerate_)
1125 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
1127 lock_guard<recursive_mutex> lock(data_mutex_);
1130 // The only reason logic_data_ would not have been created is
1131 // if it was not possible to determine the signals when the
1132 // device was created.
1136 if (!cur_logic_segment_) {
1137 // This could be the first packet after a trigger
1138 set_capture_state(Running);
1140 // Create a new data segment
1141 cur_logic_segment_ = make_shared<data::LogicSegment>(
1142 *logic_data_, logic_data_->get_segment_count(),
1143 logic->unit_size(), cur_samplerate_);
1144 logic_data_->push_segment(cur_logic_segment_);
1146 signal_new_segment();
1149 cur_logic_segment_->append_payload(logic);
1154 void Session::feed_in_analog(shared_ptr<Analog> analog)
1156 if (!cur_samplerate_)
1157 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
1159 lock_guard<recursive_mutex> lock(data_mutex_);
1161 const vector<shared_ptr<Channel>> channels = analog->channels();
1162 const unsigned int channel_count = channels.size();
1163 const size_t sample_count = analog->num_samples() / channel_count;
1164 bool sweep_beginning = false;
1166 unique_ptr<float[]> data(new float[analog->num_samples()]);
1167 analog->get_data_as_float(data.get());
1169 if (signalbases_.empty())
1172 float *channel_data = data.get();
1173 for (auto channel : channels) {
1174 shared_ptr<data::AnalogSegment> segment;
1176 // Try to get the segment of the channel
1177 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >::
1178 iterator iter = cur_analog_segments_.find(channel);
1179 if (iter != cur_analog_segments_.end())
1180 segment = (*iter).second;
1182 // If no segment was found, this means we haven't
1183 // created one yet. i.e. this is the first packet
1184 // in the sweep containing this segment.
1185 sweep_beginning = true;
1187 // Find the analog data associated with the channel
1188 shared_ptr<data::SignalBase> base = signalbase_from_channel(channel);
1191 shared_ptr<data::Analog> data(base->analog_data());
1194 // Create a segment, keep it in the maps of channels
1195 segment = make_shared<data::AnalogSegment>(
1196 *data, data->get_segment_count(), cur_samplerate_);
1197 cur_analog_segments_[channel] = segment;
1199 // Push the segment into the analog data.
1200 data->push_segment(segment);
1202 signal_new_segment();
1207 // Append the samples in the segment
1208 segment->append_interleaved_samples(channel_data++, sample_count,
1212 if (sweep_beginning) {
1213 // This could be the first packet after a trigger
1214 set_capture_state(Running);
1220 void Session::data_feed_in(shared_ptr<sigrok::Device> device,
1221 shared_ptr<Packet> packet)
1226 assert(device == device_->device());
1229 switch (packet->type()->id()) {
1235 feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
1244 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
1245 } catch (bad_alloc) {
1246 out_of_memory_ = true;
1253 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
1254 } catch (bad_alloc) {
1255 out_of_memory_ = true;
1260 case SR_DF_FRAME_BEGIN:
1261 feed_in_frame_begin();
1264 case SR_DF_FRAME_END:
1265 feed_in_frame_end();
1269 // Strictly speaking, this is performed when a frame end marker was
1270 // received, so there's no point doing this again. However, not all
1271 // devices use frames, and for those devices, we need to do it here.
1273 lock_guard<recursive_mutex> lock(data_mutex_);
1274 cur_logic_segment_.reset();
1275 cur_analog_segments_.clear();
1284 void Session::on_data_saved()