]> sigrok.org Git - pulseview.git/blobdiff - pv/sigsession.cpp
pv::DeviceManager now manages opening/closing devices
[pulseview.git] / pv / sigsession.cpp
index 9c03b882ffc9c02cc174be25d3c2f22218a9003c..5ab30ab1299e2e7bb81475053049eb46f189c3e1 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "sigsession.h"
 
+#include "devicemanager.h"
 #include "data/analog.h"
 #include "data/analogsnapshot.h"
 #include "data/logic.h"
@@ -39,7 +40,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,6 +61,28 @@ SigSession::~SigSession()
        _session = NULL;
 }
 
+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)
 {
@@ -73,15 +98,20 @@ SigSession::capture_state SigSession::get_capture_state() const
        return _capture_state;
 }
 
-void SigSession::start_capture(struct sr_dev_inst *sdi,
-       uint64_t record_length,
+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) {
+       for (l = _sdi->probes; l; l = l->next) {
                sr_probe *const probe = (sr_probe*)l->data;
                assert(probe);
                if (probe->enabled)
@@ -95,7 +125,7 @@ void SigSession::start_capture(struct sr_dev_inst *sdi,
 
        // Begin the session
        _sampling_thread.reset(new boost::thread(
-               &SigSession::sample_thread_proc, this, sdi,
+               &SigSession::sample_thread_proc, this, _sdi,
                record_length, error_handler));
 }
 
@@ -148,9 +178,13 @@ void SigSession::load_thread_proc(const string name,
        set_capture_state(Running);
 
        sr_session_run();
-       sr_session_stop();
+       sr_session_destroy();
 
        set_capture_state(Stopped);
+
+       // Confirm that SR_DF_END was received
+       assert(!_cur_logic_snapshot);
+       assert(!_cur_analog_snapshot);
 }
 
 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
@@ -189,6 +223,10 @@ 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)
@@ -221,7 +259,11 @@ void SigSession::feed_in_header(const sr_dev_inst *sdi)
 
        const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
                &gvar, sdi);
-       assert(ret == SR_OK);
+       if (ret != SR_OK) {
+               qDebug("Failed to get samplerate\n");
+               return;
+       }
+
        sample_rate = g_variant_get_uint64(gvar);
        g_variant_unref(gvar);