]> sigrok.org Git - pulseview.git/blobdiff - pv/sigsession.cpp
Initial support for input file formats
[pulseview.git] / pv / sigsession.cpp
index afe6f955a1e1e51c6aed5990e5d4dc5009220146..3e0801bf1f6ffa295829f64bb35d5d337cca14cf 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "sigsession.h"
 
+#include "devicemanager.h"
 #include "data/analog.h"
 #include "data/analogsnapshot.h"
 #include "data/logic.h"
 #include "view/analogsignal.h"
 #include "view/logicsignal.h"
 
-#include <QDebug>
-
 #include <assert.h>
 
+#include <sys/stat.h>
+
+#include <QDebug>
+
 using namespace boost;
 using namespace std;
 
@@ -39,7 +42,9 @@ namespace pv {
 // TODO: This should not be necessary
 SigSession* SigSession::_session = NULL;
 
-SigSession::SigSession() :
+SigSession::SigSession(DeviceManager &device_manager) :
+       _device_manager(device_manager),
+       _sdi(NULL),
        _capture_state(Stopped)
 {
        // TODO: This should not be necessary
@@ -58,11 +63,35 @@ SigSession::~SigSession()
        _session = NULL;
 }
 
-void SigSession::load_file(const string &name)
+struct sr_dev_inst* SigSession::get_device() const
+{
+       return _sdi;
+}
+
+void SigSession::set_device(struct sr_dev_inst *sdi)
+{
+       if (_sdi)
+               _device_manager.release_device(_sdi);
+       if (sdi)
+               _device_manager.use_device(sdi, this);
+       _sdi = sdi;
+}
+
+void SigSession::release_device(struct sr_dev_inst *sdi)
+{
+       (void)sdi;
+
+       assert(_capture_state == Stopped);
+       _sdi = NULL;
+}
+
+void SigSession::load_file(const string &name,
+       function<void (const QString)> error_handler)
 {
        stop_capture();
        _sampling_thread.reset(new boost::thread(
-               &SigSession::load_thread_proc, this, name));
+               &SigSession::load_thread_proc, this, name,
+               error_handler));
 }
 
 SigSession::capture_state SigSession::get_capture_state() const
@@ -71,14 +100,35 @@ SigSession::capture_state SigSession::get_capture_state() const
        return _capture_state;
 }
 
-void SigSession::start_capture(struct sr_dev_inst *sdi,
-       uint64_t record_length, uint64_t sample_rate)
+void SigSession::start_capture(uint64_t record_length,
+       function<void (const QString)> error_handler)
 {
        stop_capture();
 
+       // Check that a device instance has been selected.
+       if (!_sdi) {
+               qDebug() << "No device selected";
+               return;
+       }
+
+       // Check that at least one probe is enabled
+       const GSList *l;
+       for (l = _sdi->probes; l; l = l->next) {
+               sr_probe *const probe = (sr_probe*)l->data;
+               assert(probe);
+               if (probe->enabled)
+                       break;
+       }
+
+       if (!l) {
+               error_handler(tr("No probes enabled."));
+               return;
+       }
+
+       // Begin the session
        _sampling_thread.reset(new boost::thread(
-               &SigSession::sample_thread_proc, this, sdi,
-               record_length, sample_rate));
+               &SigSession::sample_thread_proc, this, _sdi,
+               record_length, error_handler));
 }
 
 void SigSession::stop_capture()
@@ -112,60 +162,143 @@ void SigSession::set_capture_state(capture_state state)
        capture_state_changed(state);
 }
 
-void SigSession::load_thread_proc(const string name)
+/**
+ * Attempts to autodetect the format. Failing that
+ * @param filename The filename of the input file.
+ * @return A pointer to the 'struct sr_input_format' that should be used,
+ *         or NULL if no input format was selected or auto-detected.
+ */
+sr_input_format* SigSession::determine_input_file_format(
+       const string &filename)
 {
-       if (sr_session_load(name.c_str()) != SR_OK) {
-               qDebug() << "Failed to load file.";
-               return;
+       int i;
+
+       /* If there are no input formats, return NULL right away. */
+       sr_input_format *const *const inputs = sr_input_list();
+       if (!inputs) {
+               g_critical("No supported input formats available.");
+               return NULL;
        }
 
-       sr_session_datafeed_callback_add(data_feed_in_proc);
+       /* Otherwise, try to find an input module that can handle this file. */
+       for (i = 0; inputs[i]; i++) {
+               if (inputs[i]->format_match(filename.c_str()))
+                       break;
+       }
 
-       if (sr_session_start() != SR_OK) {
-               qDebug() << "Failed to start session.";
-               return;
+       /* Return NULL if no input module wanted to touch this. */
+       if (!inputs[i]) {
+               g_critical("Error: no matching input module found.");
+               return NULL;
+       }
+
+       return inputs[i];
+}
+
+sr_input* SigSession::load_input_file_format(const string &filename,
+       function<void (const QString)> error_handler,
+       sr_input_format *format)
+{
+       struct stat st;
+       sr_input *in;
+
+       if (!format && !(format =
+               determine_input_file_format(filename.c_str()))) {
+               /* The exact cause was already logged. */
+               return NULL;
+       }
+
+       if (stat(filename.c_str(), &st) == -1) {
+               error_handler(tr("Failed to load file"));
+               return NULL;
+       }
+
+       /* Initialize the input module. */
+       if (!(in = new sr_input)) {
+               qDebug("Failed to allocate input module.\n");
+               return NULL;
+       }
+
+       in->format = format;
+       in->param = NULL;
+       if (in->format->init &&
+               in->format->init(in, filename.c_str()) != SR_OK) {
+               qDebug("Input format init failed.\n");
+               return NULL;
+       }
+
+       sr_session_new();
+
+       if (sr_session_dev_add(in->sdi) != SR_OK) {
+               qDebug("Failed to use device.\n");
+               sr_session_destroy();
+               return NULL;
        }
 
+       return in;
+}
+
+void SigSession::load_thread_proc(const string name,
+       function<void (const QString)> error_handler)
+{
+       sr_input *in = NULL;
+
+       if (sr_session_load(name.c_str()) == SR_OK) {
+               if (sr_session_start() != SR_OK) {
+                       error_handler(tr("Failed to start session."));
+                       return;
+               }
+       }
+       else if(!(in = load_input_file_format(name.c_str(), error_handler)))
+               return;
+
+       sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
+
        set_capture_state(Running);
 
-       sr_session_run();
-       sr_session_stop();
+       if(in) {
+               assert(in->format);
+               in->format->loadfile(in, name.c_str());
+       } else
+               sr_session_run();
 
+       sr_session_destroy();
        set_capture_state(Stopped);
+
+       // Confirm that SR_DF_END was received
+       assert(!_cur_logic_snapshot);
+       assert(!_cur_analog_snapshot);
+
+       delete in;
 }
 
 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
-       uint64_t record_length, uint64_t sample_rate)
+       uint64_t record_length,
+       function<void (const QString)> error_handler)
 {
        assert(sdi);
+       assert(error_handler);
 
        sr_session_new();
-       sr_session_datafeed_callback_add(data_feed_in_proc);
+       sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
 
        if (sr_session_dev_add(sdi) != SR_OK) {
-               qDebug() << "Failed to use device.";
+               error_handler(tr("Failed to use device."));
                sr_session_destroy();
                return;
        }
 
        // Set the sample limit
        if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
-               &record_length) != SR_OK) {
-               qDebug() << "Failed to configure time-based sample limit.";
-               sr_session_destroy();
-               return;
-       }
-
-       // Set the samplerate
-       if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
-               &sample_rate) != SR_OK) {
-               qDebug() << "Failed to configure samplerate.";
+               g_variant_new_uint64(record_length)) != SR_OK) {
+               error_handler(tr("Failed to configure "
+                       "time-based sample limit."));
                sr_session_destroy();
                return;
        }
 
        if (sr_session_start() != SR_OK) {
-               qDebug() << "Failed to start session.";
+               error_handler(tr("Failed to start session."));
                return;
        }
 
@@ -175,12 +308,17 @@ void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
        sr_session_destroy();
 
        set_capture_state(Stopped);
+
+       // Confirm that SR_DF_END was received
+       assert(!_cur_logic_snapshot);
+       assert(!_cur_analog_snapshot);
 }
 
 void SigSession::feed_in_header(const sr_dev_inst *sdi)
 {
        shared_ptr<view::Signal> signal;
-       uint64_t *sample_rate;
+       GVariant *gvar;
+       uint64_t sample_rate = 0;
        unsigned int logic_probe_count = 0;
        unsigned int analog_probe_count = 0;
 
@@ -202,9 +340,18 @@ void SigSession::feed_in_header(const sr_dev_inst *sdi)
        }
 
        // Read out the sample rate
-       assert(sdi->driver);
-       assert(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
-               (const void**)&sample_rate, sdi) == SR_OK);
+       if(sdi->driver)
+       {
+               const int ret = sr_config_get(sdi->driver,
+                       SR_CONF_SAMPLERATE, &gvar, sdi);
+               if (ret != SR_OK) {
+                       qDebug("Failed to get samplerate\n");
+                       return;
+               }
+
+               sample_rate = g_variant_get_uint64(gvar);
+               g_variant_unref(gvar);
+       }
 
        // Create data containers for the coming data snapshots
        {
@@ -212,12 +359,12 @@ void SigSession::feed_in_header(const sr_dev_inst *sdi)
 
                if (logic_probe_count != 0) {
                        _logic_data.reset(new data::Logic(
-                               logic_probe_count, *sample_rate));
+                               logic_probe_count, sample_rate));
                        assert(_logic_data);
                }
 
                if (analog_probe_count != 0) {
-                       _analog_data.reset(new data::Analog(*sample_rate));
+                       _analog_data.reset(new data::Analog(sample_rate));
                        assert(_analog_data);
                }
        }
@@ -245,7 +392,7 @@ void SigSession::feed_in_header(const sr_dev_inst *sdi)
                        case SR_PROBE_ANALOG:
                                signal = shared_ptr<view::Signal>(
                                        new view::AnalogSignal(probe->name,
-                                               _analog_data));
+                                               _analog_data, probe->index));
                                break;
                        }
 
@@ -259,6 +406,8 @@ void SigSession::feed_in_header(const sr_dev_inst *sdi)
 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
        const sr_datafeed_meta &meta)
 {
+       (void)sdi;
+
        for (const GSList *l = meta.config; l; l = l->next) {
                const sr_config *const src = (const sr_config*)l->data;
                switch (src->key) {
@@ -276,10 +425,15 @@ void SigSession::feed_in_meta(const sr_dev_inst *sdi,
 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
 {
        lock_guard<mutex> lock(_data_mutex);
-       if (!_cur_logic_snapshot)
+
+       if (!_logic_data)
        {
-               assert(_logic_data);
+               qDebug() << "Unexpected logic packet";
+               return;
+       }
 
+       if (!_cur_logic_snapshot)
+       {
                // Create a new data snapshot
                _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
                        new data::LogicSnapshot(logic));
@@ -297,10 +451,15 @@ void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
 {
        lock_guard<mutex> lock(_data_mutex);
-       if (!_cur_analog_snapshot)
+
+       if(!_analog_data)
        {
-               assert(_analog_data);
+               qDebug() << "Unexpected analog packet";
+               return; // This analog packet was not expected.
+       }
 
+       if (!_cur_analog_snapshot)
+       {
                // Create a new data snapshot
                _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
                        new data::AnalogSnapshot(analog));
@@ -356,8 +515,9 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
 }
 
 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
-       const struct sr_datafeed_packet *packet)
+       const struct sr_datafeed_packet *packet, void *cb_data)
 {
+       (void) cb_data;
        assert(_session);
        _session->data_feed_in(sdi, packet);
 }