]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Add -s / --settings parameter to load a session setup file
[pulseview.git] / pv / session.cpp
index fba49be8900139f5405b7e4f6d461d235004aa8f..91e39756456045ccc37dd46ae66c8c9f53b4c8f3 100644 (file)
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+#ifdef ENABLE_FLOW
+#include <gstreamermm.h>
+#include <libsigrokflow/libsigrokflow.hpp>
+#endif
+
 #ifdef ENABLE_DECODE
 #include <libsigrokdecode/libsigrokdecode.h>
 #include "data/decodesignal.hpp"
@@ -74,6 +79,9 @@ using std::recursive_mutex;
 using std::runtime_error;
 using std::shared_ptr;
 using std::string;
+#ifdef ENABLE_FLOW
+using std::unique_lock;
+#endif
 using std::unique_ptr;
 using std::unordered_set;
 using std::vector;
@@ -91,6 +99,12 @@ using sigrok::Session;
 
 using Glib::VariantBase;
 
+#ifdef ENABLE_FLOW
+using Gst::Bus;
+using Gst::ElementFactory;
+using Gst::Pipeline;
+#endif
+
 namespace pv {
 
 shared_ptr<sigrok::Context> Session::sr_context;
@@ -173,11 +187,49 @@ bool Session::data_saved() const
        return data_saved_;
 }
 
+void Session::save_setup(QSettings &settings) const
+{
+       int decode_signals = 0, views = 0;
+
+       // Save channels and decoders
+       for (const shared_ptr<data::SignalBase>& base : signalbases_) {
+#ifdef ENABLE_DECODE
+               if (base->is_decode_signal()) {
+                       settings.beginGroup("decode_signal" + QString::number(decode_signals++));
+                       base->save_settings(settings);
+                       settings.endGroup();
+               } else
+#endif
+               {
+                       settings.beginGroup(base->internal_name());
+                       base->save_settings(settings);
+                       settings.endGroup();
+               }
+       }
+
+       settings.setValue("decode_signals", decode_signals);
+
+       // Save view states and their signal settings
+       // Note: main_view must be saved as view0
+       settings.beginGroup("view" + QString::number(views++));
+       main_view_->save_settings(settings);
+       settings.endGroup();
+
+       for (const shared_ptr<views::ViewBase>& view : views_) {
+               if (view != main_view_) {
+                       settings.beginGroup("view" + QString::number(views++));
+                       view->save_settings(settings);
+                       settings.endGroup();
+               }
+       }
+
+       settings.setValue("views", views);
+}
+
 void Session::save_settings(QSettings &settings) const
 {
        map<string, string> dev_info;
        list<string> key_list;
-       int decode_signals = 0, views = 0;
 
        if (device_) {
                shared_ptr<devices::HardwareDevice> hw_device =
@@ -195,7 +247,7 @@ void Session::save_settings(QSettings &settings) const
 
                        dev_info = device_manager_.get_device_info(device_);
 
-                       for (string key : key_list) {
+                       for (string& key : key_list) {
                                if (dev_info.count(key))
                                        settings.setValue(QString::fromUtf8(key.c_str()),
                                                        QString::fromUtf8(dev_info.at(key).c_str()));
@@ -227,39 +279,45 @@ void Session::save_settings(QSettings &settings) const
                        settings.endGroup();
                }
 
-               // Save channels and decoders
-               for (shared_ptr<data::SignalBase> base : signalbases_) {
+               save_setup(settings);
+       }
+}
+
+void Session::restore_setup(QSettings &settings)
+{
+       // Restore channels
+       for (shared_ptr<data::SignalBase> base : signalbases_) {
+               settings.beginGroup(base->internal_name());
+               base->restore_settings(settings);
+               settings.endGroup();
+       }
+
+       // Restore decoders
 #ifdef ENABLE_DECODE
-                       if (base->is_decode_signal()) {
-                               settings.beginGroup("decode_signal" + QString::number(decode_signals++));
-                               base->save_settings(settings);
-                               settings.endGroup();
-                       } else
+       int decode_signals = settings.value("decode_signals").toInt();
+
+       for (int i = 0; i < decode_signals; i++) {
+               settings.beginGroup("decode_signal" + QString::number(i));
+               shared_ptr<data::DecodeSignal> signal = add_decode_signal();
+               signal->restore_settings(settings);
+               settings.endGroup();
+       }
 #endif
-                       {
-                               settings.beginGroup(base->internal_name());
-                               base->save_settings(settings);
-                               settings.endGroup();
-                       }
-               }
 
-               settings.setValue("decode_signals", decode_signals);
+       // Restore views
+       int views = settings.value("views").toInt();
 
-               // Save view states and their signal settings
-               // Note: main_view must be saved as view0
-               settings.beginGroup("view" + QString::number(views++));
-               main_view_->save_settings(settings);
-               settings.endGroup();
+       for (int i = 0; i < views; i++) {
+               settings.beginGroup("view" + QString::number(i));
 
-               for (shared_ptr<views::ViewBase> view : views_) {
-                       if (view != main_view_) {
-                               settings.beginGroup("view" + QString::number(views++));
-                               view->save_settings(settings);
-                               settings.endGroup();
-                       }
-               }
+               if (i > 0) {
+                       views::ViewType type = (views::ViewType)settings.value("type").toInt();
+                       add_view(name_, type, this);
+                       views_.back()->restore_settings(settings);
+               } else
+                       main_view_->restore_settings(settings);
 
-               settings.setValue("views", views);
+               settings.endGroup();
        }
 }
 
@@ -331,42 +389,8 @@ void Session::restore_settings(QSettings &settings)
                }
        }
 
-       if (device) {
-               // Restore channels
-               for (shared_ptr<data::SignalBase> base : signalbases_) {
-                       settings.beginGroup(base->internal_name());
-                       base->restore_settings(settings);
-                       settings.endGroup();
-               }
-
-               // Restore decoders
-#ifdef ENABLE_DECODE
-               int decode_signals = settings.value("decode_signals").toInt();
-
-               for (int i = 0; i < decode_signals; i++) {
-                       settings.beginGroup("decode_signal" + QString::number(i));
-                       shared_ptr<data::DecodeSignal> signal = add_decode_signal();
-                       signal->restore_settings(settings);
-                       settings.endGroup();
-               }
-#endif
-
-               // Restore views
-               int views = settings.value("views").toInt();
-
-               for (int i = 0; i < views; i++) {
-                       settings.beginGroup("view" + QString::number(i));
-
-                       if (i > 0) {
-                               views::ViewType type = (views::ViewType)settings.value("type").toInt();
-                               add_view(name_, type, this);
-                               views_.back()->restore_settings(settings);
-                       } else
-                               main_view_->restore_settings(settings);
-
-                       settings.endGroup();
-               }
-       }
+       if (device)
+               restore_setup(settings);
 }
 
 void Session::select_device(shared_ptr<devices::Device> device)
@@ -405,13 +429,13 @@ void Session::set_device(shared_ptr<devices::Device> device)
 #endif
                view->reset_view_state();
        }
-       for (const shared_ptr<data::SignalData> d : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& d : all_signal_data_)
                d->clear();
        all_signal_data_.clear();
        signalbases_.clear();
        cur_logic_segment_.reset();
 
-       for (auto entry : cur_analog_segments_) {
+       for (auto& entry : cur_analog_segments_) {
                shared_ptr<sigrok::Channel>(entry.first).reset();
                shared_ptr<data::AnalogSegment>(entry.second).reset();
        }
@@ -470,7 +494,7 @@ Session::input_format_options(vector<string> user_spec,
 {
        map<string, Glib::VariantBase> result;
 
-       for (auto entry : user_spec) {
+       for (auto& entry : user_spec) {
                /*
                 * Split key=value specs. Accept entries without separator
                 * (for simplified boolean specifications).
@@ -501,7 +525,9 @@ Session::input_format_options(vector<string> user_spec,
        return result;
 }
 
-void Session::load_init_file(const string &file_name, const string &format)
+void Session::load_init_file(const string &file_name,
+       const string &format,
+       const string &setup_file_name)
 {
        shared_ptr<InputFormat> input_format;
        map<string, Glib::VariantBase> input_opts;
@@ -525,10 +551,13 @@ void Session::load_init_file(const string &file_name, const string &format)
                        input_format->options());
        }
 
-       load_file(QString::fromStdString(file_name), input_format, input_opts);
+       load_file(QString::fromStdString(file_name),
+               QString::fromStdString(setup_file_name),
+               input_format, input_opts);
 }
 
 void Session::load_file(QString file_name,
+       QString setup_file_name,
        shared_ptr<sigrok::InputFormat> format,
        const map<string, Glib::VariantBase> &options)
 {
@@ -558,6 +587,18 @@ void Session::load_file(QString file_name,
                return;
        }
 
+       // Default the setup filename with a .pvs extension if none is provided
+       if (setup_file_name.isEmpty() || setup_file_name.isNull()) {
+               setup_file_name = file_name;
+               setup_file_name.truncate(setup_file_name.lastIndexOf('.'));
+               setup_file_name.append(".pvs");
+       }
+       // Auto-load the setup if one exists
+       if (QFileInfo::exists(setup_file_name) && QFileInfo(setup_file_name).isReadable()) {
+               QSettings settings_storage(setup_file_name, QSettings::IniFormat);
+               restore_setup(settings_storage);
+       }
+
        main_bar_->update_device_list();
 
        start_capture([&, errorMessage](QString infoMessage) {
@@ -594,7 +635,7 @@ void Session::start_capture(function<void (const QString)> error_handler)
        }
 
        // Clear signal data
-       for (const shared_ptr<data::SignalData> d : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& d : all_signal_data_)
                d->clear();
 
        trigger_list_.clear();
@@ -643,7 +684,7 @@ void Session::register_view(shared_ptr<views::ViewBase> view)
                qobject_cast<views::trace::View*>(view.get());
 
        if (trace_view) {
-               for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
+               for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) {
                        const int sb_exists = count_if(
                                view_signalbases.cbegin(), view_signalbases.cend(),
                                [&](const shared_ptr<data::SignalBase> &sb) {
@@ -684,7 +725,7 @@ void Session::deregister_view(shared_ptr<views::ViewBase> view)
 
 bool Session::has_view(shared_ptr<views::ViewBase> view)
 {
-       for (shared_ptr<views::ViewBase> v : views_)
+       for (shared_ptr<views::ViewBase>& v : views_)
                if (v == view)
                        return true;
 
@@ -695,11 +736,11 @@ double Session::get_samplerate() const
 {
        double samplerate = 0.0;
 
-       for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+       for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) {
                assert(d);
                const vector< shared_ptr<pv::data::Segment> > segments =
                        d->segments();
-               for (const shared_ptr<pv::data::Segment> &s : segments)
+               for (const shared_ptr<pv::data::Segment>s : segments)
                        samplerate = max(samplerate, s->samplerate());
        }
        // If there is no sample rate given we use samples as unit
@@ -714,7 +755,7 @@ uint32_t Session::get_segment_count() const
        uint32_t value = 0;
 
        // Find the highest number of segments
-       for (shared_ptr<data::SignalData> data : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& data : all_signal_data_)
                if (data->get_segment_count() > value)
                        value = data->get_segment_count();
 
@@ -725,7 +766,7 @@ vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const
 {
        vector<util::Timestamp> result;
 
-       for (pair<uint32_t, util::Timestamp> entry : trigger_list_)
+       for (const pair<uint32_t, util::Timestamp>& entry : trigger_list_)
                if (entry.first == segment_id)
                        result.push_back(entry.second);
 
@@ -741,7 +782,7 @@ bool Session::all_segments_complete(uint32_t segment_id) const
 {
        bool all_complete = true;
 
-       for (shared_ptr<data::SignalBase> base : signalbases_)
+       for (const shared_ptr<data::SignalBase>& base : signalbases_)
                if (!base->segment_is_complete(segment_id))
                        all_complete = false;
 
@@ -760,7 +801,7 @@ shared_ptr<data::DecodeSignal> Session::add_decode_signal()
                signalbases_.insert(signal);
 
                // Add the decode signal to all views
-               for (shared_ptr<views::ViewBase> view : views_)
+               for (shared_ptr<views::ViewBase>& view : views_)
                        view->add_decode_signal(signal);
        } catch (runtime_error& e) {
                remove_decode_signal(signal);
@@ -776,7 +817,7 @@ void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
 {
        signalbases_.erase(signal);
 
-       for (shared_ptr<views::ViewBase> view : views_)
+       for (shared_ptr<views::ViewBase>& view : views_)
                view->remove_decode_signal(signal);
 
        signals_changed();
@@ -787,6 +828,11 @@ void Session::set_capture_state(capture_state state)
 {
        bool changed;
 
+       if (state == Running)
+               acq_time_.start();
+       if (state == Stopped)
+               qDebug("Acquisition took %.2f s", acq_time_.elapsed() / 1000.);
+
        {
                lock_guard<mutex> lock(sampling_mutex_);
                changed = capture_state_ != state;
@@ -802,7 +848,7 @@ void Session::update_signals()
        if (!device_) {
                signalbases_.clear();
                logic_data_.reset();
-               for (shared_ptr<views::ViewBase> view : views_) {
+               for (shared_ptr<views::ViewBase>& view : views_) {
                        view->clear_signals();
 #ifdef ENABLE_DECODE
                        view->clear_decode_signals();
@@ -817,7 +863,7 @@ void Session::update_signals()
        if (!sr_dev) {
                signalbases_.clear();
                logic_data_.reset();
-               for (shared_ptr<views::ViewBase> view : views_) {
+               for (shared_ptr<views::ViewBase>& view : views_) {
                        view->clear_signals();
 #ifdef ENABLE_DECODE
                        view->clear_decode_signals();
@@ -848,7 +894,7 @@ void Session::update_signals()
        }
 
        // Make the signals list
-       for (shared_ptr<views::ViewBase> viewbase : views_) {
+       for (shared_ptr<views::ViewBase>& viewbase : views_) {
                views::trace::View *trace_view =
                        qobject_cast<views::trace::View*>(viewbase.get());
 
@@ -874,7 +920,7 @@ void Session::update_signals()
                                } else {
                                        // Find the signalbase for this channel if possible
                                        signalbase.reset();
-                                       for (const shared_ptr<data::SignalBase> b : signalbases_)
+                                       for (const shared_ptr<data::SignalBase>& b : signalbases_)
                                                if (b->channel() == channel)
                                                        signalbase = b;
 
@@ -947,10 +993,43 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
 {
        assert(error_handler);
 
+#ifdef ENABLE_FLOW
+       pipeline_ = Pipeline::create();
+
+       source_ = ElementFactory::create_element("filesrc", "source");
+       sink_ = RefPtr<AppSink>::cast_dynamic(ElementFactory::create_element("appsink", "sink"));
+
+       pipeline_->add(source_)->add(sink_);
+       source_->link(sink_);
+
+       source_->set_property("location", Glib::ustring("/tmp/dummy_binary"));
+
+       sink_->set_property("emit-signals", TRUE);
+       sink_->signal_new_sample().connect(sigc::mem_fun(*this, &Session::on_gst_new_sample));
+
+       // Get the bus from the pipeline and add a bus watch to the default main context
+       RefPtr<Bus> bus = pipeline_->get_bus();
+       bus->add_watch(sigc::mem_fun(this, &Session::on_gst_bus_message));
+
+       // Start pipeline and Wait until it finished processing
+       pipeline_done_interrupt_ = false;
+       pipeline_->set_state(Gst::STATE_PLAYING);
+
+       unique_lock<mutex> pipeline_done_lock_(pipeline_done_mutex_);
+       pipeline_done_cond_.wait(pipeline_done_lock_);
+
+       // Let the pipeline free all resources
+       pipeline_->set_state(Gst::STATE_NULL);
+
+#else
        if (!device_)
                return;
 
-       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+       try {
+               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+       } catch (Error& e) {
+               cur_samplerate_ = 0;
+       }
 
        out_of_memory_ = false;
 
@@ -978,6 +1057,10 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
                error_handler(e.what());
                set_capture_state(Stopped);
                return;
+       } catch (QString& e) {
+               error_handler(e);
+               set_capture_state(Stopped);
+               return;
        }
 
        set_capture_state(Stopped);
@@ -985,6 +1068,7 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
        // Confirm that SR_DF_END was received
        if (cur_logic_segment_)
                qDebug() << "WARNING: SR_DF_END was not received.";
+#endif
 
        // Optimize memory usage
        free_unused_memory();
@@ -1002,12 +1086,11 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
 
 void Session::free_unused_memory()
 {
-       for (shared_ptr<data::SignalData> data : all_signal_data_) {
+       for (const shared_ptr<data::SignalData>& data : all_signal_data_) {
                const vector< shared_ptr<data::Segment> > segments = data->segments();
 
-               for (shared_ptr<data::Segment> segment : segments) {
+               for (const shared_ptr<data::Segment>& segment : segments)
                        segment->free_unused_memory();
-               }
        }
 }
 
@@ -1045,7 +1128,7 @@ void Session::signal_segment_completed()
 {
        int segment_id = 0;
 
-       for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
+       for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) {
                // We only care about analog and logic channels, not derived ones
                if (signalbase->type() == data::SignalBase::AnalogChannel) {
                        segment_id = signalbase->analog_data()->get_segment_count() - 1;
@@ -1062,6 +1145,49 @@ void Session::signal_segment_completed()
                segment_completed(segment_id);
 }
 
+#ifdef ENABLE_FLOW
+bool Session::on_gst_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message)
+{
+       (void)bus;
+
+       if ((message->get_source() == pipeline_) && \
+               ((message->get_message_type() == Gst::MESSAGE_EOS)))
+               pipeline_done_cond_.notify_one();
+
+       // TODO Also evaluate MESSAGE_STREAM_STATUS to receive error notifications
+
+       return true;
+}
+
+Gst::FlowReturn Session::on_gst_new_sample()
+{
+       RefPtr<Gst::Sample> sample = sink_->pull_sample();
+       RefPtr<Gst::Buffer> buf = sample->get_buffer();
+
+       for (uint32_t block_id = 0; block_id < buf->n_memory(); block_id++) {
+               RefPtr<Gst::Memory> buf_mem = buf->get_memory(block_id);
+               Gst::MapInfo mapinfo;
+               buf_mem->map(mapinfo, Gst::MAP_READ);
+
+               shared_ptr<sigrok::Packet> logic_packet =
+                       sr_context->create_logic_packet(mapinfo.get_data(), buf->get_size(), 1);
+
+               try {
+                       feed_in_logic(dynamic_pointer_cast<sigrok::Logic>(logic_packet->payload()));
+               } catch (bad_alloc&) {
+                       out_of_memory_ = true;
+                       device_->stop();
+                       buf_mem->unmap(mapinfo);
+                       return Gst::FLOW_ERROR;
+               }
+
+               buf_mem->unmap(mapinfo);
+       }
+
+       return Gst::FLOW_OK;
+}
+#endif
+
 void Session::feed_in_header()
 {
        // Nothing to do here for now
@@ -1069,7 +1195,7 @@ void Session::feed_in_header()
 
 void Session::feed_in_meta(shared_ptr<Meta> meta)
 {
-       for (auto entry : meta->config()) {
+       for (auto& entry : meta->config()) {
                switch (entry.first->id()) {
                case SR_CONF_SAMPLERATE:
                        cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
@@ -1089,7 +1215,7 @@ void Session::feed_in_trigger()
        uint64_t sample_count = 0;
 
        {
-               for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+               for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) {
                        assert(d);
                        uint64_t temp_count = 0;
 
@@ -1139,7 +1265,7 @@ void Session::feed_in_frame_end()
                if (cur_logic_segment_)
                        cur_logic_segment_->set_complete();
 
-               for (auto entry : cur_analog_segments_) {
+               for (auto& entry : cur_analog_segments_) {
                        shared_ptr<data::AnalogSegment> segment = entry.second;
                        segment->set_complete();
                }
@@ -1160,8 +1286,15 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
                return;
        }
 
+       if (logic->unit_size() > 8)
+               throw QString(tr("Can't handle more than 64 logic channels."));
+
        if (!cur_samplerate_)
-               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               try {
+                       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               } catch (Error& e) {
+                       // Do nothing
+               }
 
        lock_guard<recursive_mutex> lock(data_mutex_);
 
@@ -1198,23 +1331,25 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
        }
 
        if (!cur_samplerate_)
-               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               try {
+                       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               } catch (Error& e) {
+                       // Do nothing
+               }
 
        lock_guard<recursive_mutex> lock(data_mutex_);
 
        const vector<shared_ptr<Channel>> channels = analog->channels();
-       const unsigned int channel_count = channels.size();
-       const size_t sample_count = analog->num_samples() / channel_count;
        bool sweep_beginning = false;
 
-       unique_ptr<float[]> data(new float[analog->num_samples()]);
+       unique_ptr<float[]> data(new float[analog->num_samples() * channels.size()]);
        analog->get_data_as_float(data.get());
 
        if (signalbases_.empty())
                update_signals();
 
        float *channel_data = data.get();
-       for (auto channel : channels) {
+       for (auto& channel : channels) {
                shared_ptr<data::AnalogSegment> segment;
 
                // Try to get the segment of the channel
@@ -1249,8 +1384,8 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
                assert(segment);
 
                // Append the samples in the segment
-               segment->append_interleaved_samples(channel_data++, sample_count,
-                       channel_count);
+               segment->append_interleaved_samples(channel_data++, analog->num_samples(),
+                       channels.size());
        }
 
        if (sweep_beginning) {
@@ -1319,7 +1454,7 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                        if (cur_logic_segment_)
                                cur_logic_segment_->set_complete();
 
-                       for (auto entry : cur_analog_segments_) {
+                       for (auto& entry : cur_analog_segments_) {
                                shared_ptr<data::AnalogSegment> segment = entry.second;
                                segment->set_complete();
                        }
@@ -1339,4 +1474,17 @@ void Session::on_data_saved()
        data_saved_ = true;
 }
 
+#ifdef ENABLE_DECODE
+void Session::on_new_decoders_selected(vector<const srd_decoder*> decoders)
+{
+       assert(decoders.size() > 0);
+
+       shared_ptr<data::DecodeSignal> signal = add_decode_signal();
+
+       if (signal)
+               for (const srd_decoder* d : decoders)
+                       signal->stack_decoder(d);
+}
+#endif
+
 } // namespace pv