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/>.
21 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
25 #include <boost/thread/locks.hpp>
26 #include <boost/thread/shared_mutex.hpp>
36 #include "session.hpp"
37 #include "devicemanager.hpp"
39 #include "data/analog.hpp"
40 #include "data/analogsegment.hpp"
41 #include "data/decoderstack.hpp"
42 #include "data/logic.hpp"
43 #include "data/logicsegment.hpp"
44 #include "data/signalbase.hpp"
45 #include "data/decode/decoder.hpp"
47 #include "devices/hardwaredevice.hpp"
48 #include "devices/inputfile.hpp"
49 #include "devices/sessionfile.hpp"
51 #include "toolbars/mainbar.hpp"
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"
59 #include <libsigrokcxx/libsigrokcxx.hpp>
62 #include <libsigrokdecode/libsigrokdecode.h>
65 using boost::shared_lock;
66 using boost::shared_mutex;
67 using boost::unique_lock;
69 using std::dynamic_pointer_cast;
71 using std::lock_guard;
76 using std::recursive_mutex;
78 using std::shared_ptr;
80 using std::unordered_set;
84 using sigrok::Channel;
85 using sigrok::ChannelType;
86 using sigrok::ConfigKey;
87 using sigrok::DatafeedCallbackFunction;
90 using sigrok::InputFormat;
93 using sigrok::OutputFormat;
95 using sigrok::PacketPayload;
96 using sigrok::Session;
97 using sigrok::SessionDevice;
99 using Glib::VariantBase;
103 Session::Session(DeviceManager &device_manager, QString name) :
104 device_manager_(device_manager),
107 capture_state_(Stopped),
114 // Stop and join to the thread
118 DeviceManager& Session::device_manager()
120 return device_manager_;
123 const DeviceManager& Session::device_manager() const
125 return device_manager_;
128 shared_ptr<sigrok::Session> Session::session() const
131 return shared_ptr<sigrok::Session>();
132 return device_->session();
135 shared_ptr<devices::Device> Session::device() const
140 QString Session::name() const
145 void Session::set_name(QString name)
147 if (default_name_.isEmpty())
148 default_name_ = name;
155 const std::list< std::shared_ptr<views::ViewBase> > Session::views() const
160 std::shared_ptr<views::ViewBase> Session::main_view() const
165 void Session::set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar)
167 main_bar_ = main_bar;
170 shared_ptr<pv::toolbars::MainBar> Session::main_bar() const
175 void Session::save_settings(QSettings &settings) const
177 map<string, string> dev_info;
178 list<string> key_list;
179 int stacks = 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.push_back("vendor");
190 key_list.push_back("model");
191 key_list.push_back("version");
192 key_list.push_back("serial_num");
193 key_list.push_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 shared_ptr<pv::data::DecoderStack> decoder_stack =
224 base->decoder_stack();
225 std::shared_ptr<data::decode::Decoder> top_decoder =
226 decoder_stack->stack().front();
228 settings.beginGroup("decoder_stack" + QString::number(stacks++));
229 settings.setValue("id", top_decoder->decoder()->id);
230 settings.setValue("name", top_decoder->decoder()->name);
235 settings.beginGroup(base->internal_name());
236 base->save_settings(settings);
241 settings.setValue("decoder_stacks", stacks);
243 // Save view states and their signal settings
244 // Note: main_view must be saved as view0
245 settings.beginGroup("view" + QString::number(views++));
246 main_view_->save_settings(settings);
249 for (shared_ptr<views::ViewBase> view : views_) {
250 if (view != main_view_) {
251 settings.beginGroup("view" + QString::number(views++));
252 view->save_settings(settings);
257 settings.setValue("views", views);
261 void Session::restore_settings(QSettings &settings)
263 shared_ptr<devices::Device> device;
265 QString device_type = settings.value("device_type").toString();
267 if (device_type == "hardware") {
268 map<string, string> dev_info;
269 list<string> key_list;
271 // Re-select last used device if possible but only if it's not demo
272 settings.beginGroup("device");
273 key_list.push_back("vendor");
274 key_list.push_back("model");
275 key_list.push_back("version");
276 key_list.push_back("serial_num");
277 key_list.push_back("connection_id");
279 for (string key : key_list) {
280 const QString k = QString::fromStdString(key);
281 if (!settings.contains(k))
284 const string value = settings.value(k).toString().toStdString();
286 dev_info.insert(std::make_pair(key, value));
289 if (dev_info.count("model") > 0)
290 device = device_manager_.find_device_from_info(dev_info);
298 if (device_type == "sessionfile") {
299 settings.beginGroup("device");
300 QString filename = settings.value("filename").toString();
303 if (QFileInfo(filename).isReadable()) {
304 device = std::make_shared<devices::SessionFile>(device_manager_.context(),
305 filename.toStdString());
308 // TODO Perform error handling
309 start_capture([](QString infoMessage) { (void)infoMessage; });
311 set_name(QFileInfo(filename).fileName());
317 for (shared_ptr<data::SignalBase> base : signalbases_) {
318 settings.beginGroup(base->internal_name());
319 base->restore_settings(settings);
325 int stacks = settings.value("decoder_stacks").toInt();
327 for (int i = 0; i < stacks; i++) {
328 settings.beginGroup("decoder_stack" + QString::number(i++));
330 QString id = settings.value("id").toString();
331 add_decoder(srd_decoder_get_by_id(id.toStdString().c_str()));
338 int views = settings.value("views").toInt();
340 for (int i = 0; i < views; i++) {
341 settings.beginGroup("view" + QString::number(i));
344 views::ViewType type = (views::ViewType)settings.value("type").toInt();
345 add_view(name_, type, this);
346 views_.back()->restore_settings(settings);
348 main_view_->restore_settings(settings);
355 void Session::select_device(shared_ptr<devices::Device> device)
361 set_default_device();
362 } catch (const QString &e) {
363 main_bar_->session_error(tr("Failed to Select Device"),
364 tr("Failed to Select Device"));
368 void Session::set_device(shared_ptr<devices::Device> device)
372 // Ensure we are not capturing before setting the device
380 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
381 name_ = default_name_;
384 // Remove all stored data
385 for (std::shared_ptr<views::ViewBase> view : views_) {
386 view->clear_signals();
388 view->clear_decode_signals();
391 for (const shared_ptr<data::SignalData> d : all_signal_data_)
393 all_signal_data_.clear();
394 signalbases_.clear();
395 cur_logic_segment_.reset();
397 for (auto entry : cur_analog_segments_) {
398 shared_ptr<sigrok::Channel>(entry.first).reset();
399 shared_ptr<data::AnalogSegment>(entry.second).reset();
406 device_ = std::move(device);
410 } catch (const QString &e) {
416 device_->session()->add_datafeed_callback([=]
417 (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
418 data_feed_in(device, packet);
425 void Session::set_default_device()
427 const list< shared_ptr<devices::HardwareDevice> > &devices =
428 device_manager_.devices();
433 // Try and find the demo device and select that by default
434 const auto iter = std::find_if(devices.begin(), devices.end(),
435 [] (const shared_ptr<devices::HardwareDevice> &d) {
436 return d->hardware_device()->driver()->name() ==
438 set_device((iter == devices.end()) ? devices.front() : *iter);
441 void Session::load_init_file(const std::string &file_name,
442 const std::string &format)
444 shared_ptr<InputFormat> input_format;
446 if (!format.empty()) {
447 const map<string, shared_ptr<InputFormat> > formats =
448 device_manager_.context()->input_formats();
449 const auto iter = find_if(formats.begin(), formats.end(),
450 [&](const pair<string, shared_ptr<InputFormat> > f) {
451 return f.first == format; });
452 if (iter == formats.end()) {
453 main_bar_->session_error(tr("Error"),
454 tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
458 input_format = (*iter).second;
461 load_file(QString::fromStdString(file_name), input_format);
464 void Session::load_file(QString file_name,
465 std::shared_ptr<sigrok::InputFormat> format,
466 const std::map<std::string, Glib::VariantBase> &options)
468 const QString errorMessage(
469 QString("Failed to load file %1").arg(file_name));
473 set_device(shared_ptr<devices::Device>(
474 new devices::InputFile(
475 device_manager_.context(),
476 file_name.toStdString(),
479 set_device(shared_ptr<devices::Device>(
480 new devices::SessionFile(
481 device_manager_.context(),
482 file_name.toStdString())));
484 main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
485 set_default_device();
486 main_bar_->update_device_list();
490 main_bar_->update_device_list();
492 start_capture([&, errorMessage](QString infoMessage) {
493 main_bar_->session_error(errorMessage, infoMessage); });
495 set_name(QFileInfo(file_name).fileName());
498 Session::capture_state Session::get_capture_state() const
500 lock_guard<mutex> lock(sampling_mutex_);
501 return capture_state_;
504 void Session::start_capture(function<void (const QString)> error_handler)
507 error_handler(tr("No active device set, can't start acquisition."));
513 // Check that at least one channel is enabled
514 const shared_ptr<sigrok::Device> sr_dev = device_->device();
516 const auto channels = sr_dev->channels();
517 if (!std::any_of(channels.begin(), channels.end(),
518 [](shared_ptr<Channel> channel) {
519 return channel->enabled(); })) {
520 error_handler(tr("No channels enabled."));
526 for (const shared_ptr<data::SignalData> d : all_signal_data_)
529 // Revert name back to default name (e.g. "Untitled-1") as the data is gone
530 name_ = default_name_;
534 sampling_thread_ = std::thread(
535 &Session::sample_thread_proc, this, error_handler);
538 void Session::stop_capture()
540 if (get_capture_state() != Stopped)
543 // Check that sampling stopped
544 if (sampling_thread_.joinable())
545 sampling_thread_.join();
548 void Session::register_view(std::shared_ptr<views::ViewBase> view)
550 if (views_.empty()) {
554 views_.push_back(view);
559 void Session::deregister_view(std::shared_ptr<views::ViewBase> view)
561 views_.remove_if([&](std::shared_ptr<views::ViewBase> v) {
562 return v == view; });
564 if (views_.empty()) {
567 // Without a view there can be no main bar
572 bool Session::has_view(std::shared_ptr<views::ViewBase> view)
574 for (std::shared_ptr<views::ViewBase> v : views_)
581 double Session::get_samplerate() const
583 double samplerate = 0.0;
585 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
587 const vector< shared_ptr<pv::data::Segment> > segments =
589 for (const shared_ptr<pv::data::Segment> &s : segments)
590 samplerate = std::max(samplerate, s->samplerate());
592 // If there is no sample rate given we use samples as unit
593 if (samplerate == 0.0)
599 const std::unordered_set< std::shared_ptr<data::SignalBase> >
600 Session::signalbases() const
606 bool Session::add_decoder(srd_decoder *const dec)
608 map<const srd_channel*, shared_ptr<data::SignalBase> > channels;
609 shared_ptr<data::DecoderStack> decoder_stack;
612 // Create the decoder
613 decoder_stack = shared_ptr<data::DecoderStack>(
614 new data::DecoderStack(*this, dec));
616 // Make a list of all the channels
617 std::vector<const srd_channel*> all_channels;
618 for (const GSList *i = dec->channels; i; i = i->next)
619 all_channels.push_back((const srd_channel*)i->data);
620 for (const GSList *i = dec->opt_channels; i; i = i->next)
621 all_channels.push_back((const srd_channel*)i->data);
623 // Auto select the initial channels
624 for (const srd_channel *pdch : all_channels)
625 for (shared_ptr<data::SignalBase> b : signalbases_) {
626 if (b->type() == ChannelType::LOGIC) {
627 if (QString::fromUtf8(pdch->name).toLower().
628 contains(b->name().toLower()))
633 assert(decoder_stack);
634 assert(!decoder_stack->stack().empty());
635 assert(decoder_stack->stack().front());
636 decoder_stack->stack().front()->set_channels(channels);
638 // Create the decode signal
639 shared_ptr<data::SignalBase> signalbase =
640 shared_ptr<data::SignalBase>(new data::SignalBase(nullptr));
642 signalbase->set_decoder_stack(decoder_stack);
643 signalbases_.insert(signalbase);
645 for (std::shared_ptr<views::ViewBase> view : views_)
646 view->add_decode_signal(signalbase);
647 } catch (std::runtime_error e) {
653 // Do an initial decode
654 decoder_stack->begin_decode();
659 void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase)
661 signalbases_.erase(signalbase);
663 for (std::shared_ptr<views::ViewBase> view : views_)
664 view->remove_decode_signal(signalbase);
670 void Session::set_capture_state(capture_state state)
675 lock_guard<mutex> lock(sampling_mutex_);
676 changed = capture_state_ != state;
677 capture_state_ = state;
681 capture_state_changed(state);
684 void Session::update_signals()
687 signalbases_.clear();
689 for (std::shared_ptr<views::ViewBase> view : views_) {
690 view->clear_signals();
692 view->clear_decode_signals();
698 lock_guard<recursive_mutex> lock(data_mutex_);
700 const shared_ptr<sigrok::Device> sr_dev = device_->device();
702 signalbases_.clear();
704 for (std::shared_ptr<views::ViewBase> view : views_) {
705 view->clear_signals();
707 view->clear_decode_signals();
713 // Detect what data types we will receive
714 auto channels = sr_dev->channels();
715 unsigned int logic_channel_count = std::count_if(
716 channels.begin(), channels.end(),
717 [] (shared_ptr<Channel> channel) {
718 return channel->type() == ChannelType::LOGIC; });
720 // Create data containers for the logic data segments
722 lock_guard<recursive_mutex> data_lock(data_mutex_);
724 if (logic_channel_count == 0) {
726 } else if (!logic_data_ ||
727 logic_data_->num_channels() != logic_channel_count) {
728 logic_data_.reset(new data::Logic(
729 logic_channel_count));
734 // Make the signals list
735 for (std::shared_ptr<views::ViewBase> viewbase : views_) {
736 views::TraceView::View *trace_view =
737 qobject_cast<views::TraceView::View*>(viewbase.get());
740 unordered_set< shared_ptr<views::TraceView::Signal> >
741 prev_sigs(trace_view->signals());
742 trace_view->clear_signals();
744 for (auto channel : sr_dev->channels()) {
745 shared_ptr<data::SignalBase> signalbase;
746 shared_ptr<views::TraceView::Signal> signal;
748 // Find the channel in the old signals
749 const auto iter = std::find_if(
750 prev_sigs.cbegin(), prev_sigs.cend(),
751 [&](const shared_ptr<views::TraceView::Signal> &s) {
752 return s->base()->channel() == channel;
754 if (iter != prev_sigs.end()) {
755 // Copy the signal from the old set to the new
757 trace_view->add_signal(signal);
759 // Find the signalbase for this channel if possible
761 for (const shared_ptr<data::SignalBase> b : signalbases_)
762 if (b->channel() == channel)
765 switch(channel->type()->id()) {
766 case SR_CHANNEL_LOGIC:
768 signalbase = shared_ptr<data::SignalBase>(
769 new data::SignalBase(channel));
770 signalbases_.insert(signalbase);
772 all_signal_data_.insert(logic_data_);
773 signalbase->set_data(logic_data_);
776 signal = shared_ptr<views::TraceView::Signal>(
777 new views::TraceView::LogicSignal(*this,
778 device_, signalbase));
779 trace_view->add_signal(signal);
782 case SR_CHANNEL_ANALOG:
785 signalbase = shared_ptr<data::SignalBase>(
786 new data::SignalBase(channel));
787 signalbases_.insert(signalbase);
789 shared_ptr<data::Analog> data(new data::Analog());
790 all_signal_data_.insert(data);
791 signalbase->set_data(data);
794 signal = shared_ptr<views::TraceView::Signal>(
795 new views::TraceView::AnalogSignal(
797 trace_view->add_signal(signal);
813 shared_ptr<data::SignalBase> Session::signalbase_from_channel(
814 shared_ptr<sigrok::Channel> channel) const
816 for (shared_ptr<data::SignalBase> sig : signalbases_) {
818 if (sig->channel() == channel)
821 return shared_ptr<data::SignalBase>();
824 void Session::sample_thread_proc(function<void (const QString)> error_handler)
826 assert(error_handler);
831 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
833 out_of_memory_ = false;
838 error_handler(e.what());
842 set_capture_state(device_->session()->trigger() ?
843 AwaitingTrigger : Running);
846 set_capture_state(Stopped);
848 // Confirm that SR_DF_END was received
849 if (cur_logic_segment_) {
850 qDebug("SR_DF_END was not received.");
855 error_handler(tr("Out of memory, acquisition stopped."));
858 void Session::feed_in_header()
860 cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
863 void Session::feed_in_meta(shared_ptr<Meta> meta)
865 for (auto entry : meta->config()) {
866 switch (entry.first->id()) {
867 case SR_CONF_SAMPLERATE:
868 // We can't rely on the header to always contain the sample rate,
869 // so in case it's supplied via a meta packet, we use it.
870 if (!cur_samplerate_)
871 cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
873 /// @todo handle samplerate changes
876 // Unknown metadata is not an error.
884 void Session::feed_in_trigger()
886 // The channel containing most samples should be most accurate
887 uint64_t sample_count = 0;
890 for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
892 uint64_t temp_count = 0;
894 const vector< shared_ptr<pv::data::Segment> > segments =
896 for (const shared_ptr<pv::data::Segment> &s : segments)
897 temp_count += s->get_sample_count();
899 if (temp_count > sample_count)
900 sample_count = temp_count;
904 trigger_event(sample_count / get_samplerate());
907 void Session::feed_in_frame_begin()
909 if (cur_logic_segment_ || !cur_analog_segments_.empty())
913 void Session::feed_in_logic(shared_ptr<Logic> logic)
915 lock_guard<recursive_mutex> lock(data_mutex_);
917 const size_t sample_count = logic->data_length() / logic->unit_size();
920 // The only reason logic_data_ would not have been created is
921 // if it was not possible to determine the signals when the
922 // device was created.
926 if (!cur_logic_segment_) {
927 // This could be the first packet after a trigger
928 set_capture_state(Running);
930 // Create a new data segment
931 cur_logic_segment_ = shared_ptr<data::LogicSegment>(
932 new data::LogicSegment(
933 logic, cur_samplerate_, sample_count));
934 logic_data_->push_segment(cur_logic_segment_);
936 // @todo Putting this here means that only listeners querying
937 // for logic will be notified. Currently the only user of
938 // frame_began is DecoderStack, but in future we need to signal
939 // this after both analog and logic sweeps have begun.
942 // Append to the existing data segment
943 cur_logic_segment_->append_payload(logic);
949 void Session::feed_in_analog(shared_ptr<Analog> analog)
951 lock_guard<recursive_mutex> lock(data_mutex_);
953 const vector<shared_ptr<Channel>> channels = analog->channels();
954 const unsigned int channel_count = channels.size();
955 const size_t sample_count = analog->num_samples() / channel_count;
956 const float *data = static_cast<const float *>(analog->data_pointer());
957 bool sweep_beginning = false;
959 if (signalbases_.empty())
962 for (auto channel : channels) {
963 shared_ptr<data::AnalogSegment> segment;
965 // Try to get the segment of the channel
966 const map< shared_ptr<Channel>, shared_ptr<data::AnalogSegment> >::
967 iterator iter = cur_analog_segments_.find(channel);
968 if (iter != cur_analog_segments_.end())
969 segment = (*iter).second;
971 // If no segment was found, this means we haven't
972 // created one yet. i.e. this is the first packet
973 // in the sweep containing this segment.
974 sweep_beginning = true;
976 // Create a segment, keep it in the maps of channels
977 segment = shared_ptr<data::AnalogSegment>(
978 new data::AnalogSegment(
979 cur_samplerate_, sample_count));
980 cur_analog_segments_[channel] = segment;
982 // Find the analog data associated with the channel
983 shared_ptr<data::SignalBase> base = signalbase_from_channel(channel);
986 shared_ptr<data::Analog> data(base->analog_data());
989 // Push the segment into the analog data.
990 data->push_segment(segment);
995 // Append the samples in the segment
996 segment->append_interleaved_samples(data++, sample_count,
1000 if (sweep_beginning) {
1001 // This could be the first packet after a trigger
1002 set_capture_state(Running);
1008 void Session::data_feed_in(shared_ptr<sigrok::Device> device,
1009 shared_ptr<Packet> packet)
1014 assert(device == device_->device());
1017 switch (packet->type()->id()) {
1023 feed_in_meta(dynamic_pointer_cast<Meta>(packet->payload()));
1030 case SR_DF_FRAME_BEGIN:
1031 feed_in_frame_begin();
1036 feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
1037 } catch (std::bad_alloc) {
1038 out_of_memory_ = true;
1045 feed_in_analog(dynamic_pointer_cast<Analog>(packet->payload()));
1046 } catch (std::bad_alloc) {
1047 out_of_memory_ = true;
1055 lock_guard<recursive_mutex> lock(data_mutex_);
1056 cur_logic_segment_.reset();
1057 cur_analog_segments_.clear();