Moved session creation into DevInst objects
authorJoel Holdsworth <joel@airwebreathe.org.uk>
Sat, 22 Feb 2014 12:45:36 +0000 (12:45 +0000)
committerJoel Holdsworth <joel@airwebreathe.org.uk>
Sat, 1 Mar 2014 11:02:11 +0000 (11:02 +0000)
15 files changed:
CMakeLists.txt
pv/device/device.cpp
pv/device/device.h
pv/device/devinst.cpp
pv/device/devinst.h
pv/device/file.cpp [new file with mode: 0644]
pv/device/file.h [new file with mode: 0644]
pv/device/inputfile.cpp [new file with mode: 0644]
pv/device/inputfile.h [new file with mode: 0644]
pv/device/sessionfile.cpp [new file with mode: 0644]
pv/device/sessionfile.h [new file with mode: 0644]
pv/mainwindow.cpp
pv/sigsession.cpp
pv/sigsession.h
test/CMakeLists.txt

index 58c86c455f742b1a5918fd6a4187cccfb8bbf1bc..ebff3729f1b113a73ebbba68b1fa607ea07d6151 100644 (file)
@@ -128,7 +128,10 @@ set(pulseview_SOURCES
        pv/data/signaldata.cpp
        pv/data/snapshot.cpp
        pv/device/device.cpp
+       pv/device/file.cpp
        pv/device/devinst.cpp
+       pv/device/inputfile.cpp
+       pv/device/sessionfile.cpp
        pv/dialogs/about.cpp
        pv/dialogs/connect.cpp
        pv/dialogs/storeprogress.cpp
index 4a3ec35f1869364c04f02da03f2577c36cdcf7f8..e70be02592f0c58e3c2b59340068b39478f94842 100644 (file)
@@ -41,6 +41,28 @@ sr_dev_inst* Device::dev_inst() const
        return _sdi;
 }
 
+void Device::use(SigSession *owner) throw(QString)
+{
+       DevInst::use(owner);
+
+       sr_session_new();
+
+       assert(_sdi);
+       sr_dev_open(_sdi);
+       if (sr_session_dev_add(_sdi) != SR_OK)
+               throw QString(tr("Failed to use device."));
+}
+
+void Device::release()
+{
+       if (_owner) {
+               DevInst::release();
+               sr_session_destroy();
+       }
+
+       sr_dev_close(_sdi);
+}
+
 std::string Device::format_device_title() const
 {
        ostringstream s;
index fcf74c31acb1a46b792418ecbcede244859a93a3..b9e17297eadd1428945648f683cd3bbcc12642aa 100644 (file)
@@ -33,6 +33,10 @@ public:
 
        sr_dev_inst* dev_inst() const;
 
+       void use(SigSession *owner) throw(QString);
+
+       void release();
+
        std::string format_device_title() const;
 
        bool is_trigger_enabled() const;
index 7440995193be1222eff7d433e65a16e6a7eb0ad1..86595ae79069a9c7e2b9e33700e1c5eee7be8661 100644 (file)
@@ -36,12 +36,11 @@ DevInst::DevInst() :
 {
 }
 
-void DevInst::use(SigSession *owner)
+void DevInst::use(SigSession *owner) throw(QString)
 {
        assert(owner);
        assert(!_owner);
        _owner = owner;
-       sr_dev_open(dev_inst());
 }
 
 void DevInst::release()
@@ -49,7 +48,6 @@ void DevInst::release()
        if (_owner) {
                _owner->release_device(this);
                _owner = NULL;
-               sr_dev_close(dev_inst());
        }
 }
 
@@ -61,6 +59,7 @@ SigSession* DevInst::owner() const
 GVariant* DevInst::get_config(const sr_probe_group *group, int key)
 {
        GVariant *data = NULL;
+       assert(_owner);
        sr_dev_inst *const sdi = dev_inst();
        assert(sdi);
        if (sr_config_get(sdi->driver, sdi, group, key, &data) != SR_OK)
@@ -70,6 +69,7 @@ GVariant* DevInst::get_config(const sr_probe_group *group, int key)
 
 bool DevInst::set_config(const sr_probe_group *group, int key, GVariant *data)
 {
+       assert(_owner);
        sr_dev_inst *const sdi = dev_inst();
        assert(sdi);
        if(sr_config_set(sdi, group, key, data) == SR_OK) {
@@ -82,6 +82,7 @@ bool DevInst::set_config(const sr_probe_group *group, int key, GVariant *data)
 GVariant* DevInst::list_config(const sr_probe_group *group, int key)
 {
        GVariant *data = NULL;
+       assert(_owner);
        sr_dev_inst *const sdi = dev_inst();
        assert(sdi);
        if (sr_config_list(sdi->driver, sdi, group, key, &data) != SR_OK)
@@ -91,6 +92,7 @@ GVariant* DevInst::list_config(const sr_probe_group *group, int key)
 
 void DevInst::enable_probe(const sr_probe *probe, bool enable)
 {
+       assert(_owner);
        sr_dev_inst *const sdi = dev_inst();
        assert(sdi);
        for (const GSList *p = sdi->probes; p; p = p->next)
@@ -122,5 +124,16 @@ bool DevInst::is_trigger_enabled() const
        return false;
 }
 
+void DevInst::start()
+{
+       if (sr_session_start() != SR_OK)
+               throw tr("Failed to start session.");
+}
+
+void DevInst::run()
+{
+       sr_session_run();
+}
+
 } // device
 } // pv
index 5ee387477a7e0119ddb7331bc4271496687c916a..8932f1807f542b07242a48f6836565985a886229 100644 (file)
@@ -51,9 +51,9 @@ protected:
 public:
        virtual sr_dev_inst* dev_inst() const = 0;
 
-       void use(SigSession *owner);
+       virtual void use(SigSession *owner) throw(QString);
 
-       void release();
+       virtual void release();
 
        SigSession* owner() const;
 
@@ -77,6 +77,11 @@ public:
 
        virtual bool is_trigger_enabled() const;
 
+public:
+       virtual void start();
+
+       virtual void run();
+
 signals:
        void config_changed();
 
diff --git a/pv/device/file.cpp b/pv/device/file.cpp
new file mode 100644 (file)
index 0000000..190b538
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include "file.h"
+#include "inputfile.h"
+#include "sessionfile.h"
+
+#include <libsigrok/libsigrok.h>
+
+using std::string;
+
+namespace pv {
+namespace device {
+
+File::File(const std::string path) :
+       _path(path)
+{
+}
+
+std::string File::format_device_title() const
+{
+       return _path;
+}
+
+File* File::create(const string &name)
+{
+       if (sr_session_load(name.c_str()) == SR_OK) {
+               GSList *devlist = NULL;
+               sr_session_dev_list(&devlist);
+               sr_session_destroy();
+
+               if (devlist) {
+                       sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
+                       g_slist_free(devlist);
+                       if (sdi) {
+                               sr_dev_close(sdi);
+                               sr_dev_clear(sdi->driver);
+                               return new SessionFile(name);
+                       }
+               }
+       }
+
+       return new InputFile(name);
+}
+
+} // device
+} // pv
diff --git a/pv/device/file.h b/pv/device/file.h
new file mode 100644 (file)
index 0000000..4a45a82
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef PULSEVIEW_PV_DEVICE_FILE_H
+#define PULSEVIEW_PV_DEVICE_FILE_H
+
+#include <string>
+
+#include "devinst.h"
+
+namespace pv {
+namespace device {
+
+class File : public DevInst
+{
+protected:
+       File(const std::string path);
+
+public:
+       static File* create(const std::string &name);
+
+public:
+       std::string format_device_title() const;
+
+protected:
+       const std::string _path;
+};
+
+} // device
+} // pv
+
+#endif // PULSEVIEW_PV_DEVICE_FILE_H
diff --git a/pv/device/inputfile.cpp b/pv/device/inputfile.cpp
new file mode 100644 (file)
index 0000000..55a0fa5
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include <cassert>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "inputfile.h"
+
+#include <libsigrok/libsigrok.h>
+
+using std::string;
+
+namespace pv {
+namespace device {
+
+InputFile::InputFile(const std::string &path) :
+       File(path),
+       _input(NULL)
+{
+}
+
+sr_dev_inst* InputFile::dev_inst() const
+{
+       assert(_input);
+       return _input->sdi;
+}
+
+void InputFile::use(SigSession *owner) throw(QString)
+{
+       assert(!_input);
+
+       _input = load_input_file_format(_path, NULL);
+       File::use(owner);
+
+       sr_session_new();
+
+       if (sr_session_dev_add(_input->sdi) != SR_OK)
+               throw tr("Failed to add session device.");
+}
+
+void InputFile::release()
+{
+       if (!_owner)
+               return;
+
+       assert(_input);
+       File::release();
+       sr_dev_close(_input->sdi);
+       sr_session_destroy();
+       _input = NULL;
+}
+
+sr_input_format* InputFile::determine_input_file_format(
+       const string &filename)
+{
+       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;
+       }
+
+       /* 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;
+       }
+
+       /* 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* InputFile::load_input_file_format(const string &filename,
+       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. */
+               throw tr("Failed to load file");
+       }
+
+       if (stat(filename.c_str(), &st) == -1)
+               throw tr("Failed to load file");
+
+       /* Initialize the input module. */
+       if (!(in = new sr_input)) {
+               throw tr("Failed to allocate input module.");
+       }
+
+       in->format = format;
+       in->param = NULL;
+       if (in->format->init &&
+               in->format->init(in, filename.c_str()) != SR_OK) {
+               throw tr("Failed to load file");
+       }
+
+       return in;
+}
+
+void InputFile::start()
+{
+}
+
+void InputFile::run()
+{
+       assert(_input);
+       assert(_input->format);
+       assert(_input->format->loadfile);
+       _input->format->loadfile(_input, _path.c_str());
+}
+
+} // device
+} // pv
diff --git a/pv/device/inputfile.h b/pv/device/inputfile.h
new file mode 100644 (file)
index 0000000..150418d
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef PULSEVIEW_PV_DEVICE_INPUTFILE_H
+#define PULSEVIEW_PV_DEVICE_INPUTFILE_H
+
+#include "file.h"
+
+#include <string>
+
+struct sr_input;
+struct sr_input_format;
+
+namespace pv {
+namespace device {
+
+class InputFile : public File
+{
+public:
+       InputFile(const std::string &path);
+
+       sr_dev_inst* dev_inst() const;
+
+       virtual void use(SigSession *owner) throw(QString);
+
+       virtual void release();
+
+       virtual void start();
+
+       virtual void run();
+
+private:
+       /**
+        * 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.
+        */
+       static sr_input_format* determine_input_file_format(
+               const std::string &filename);
+
+       static sr_input* load_input_file_format(const std::string &filename,
+               sr_input_format *format);
+private:
+       sr_input *_input;
+};
+
+} // device
+} // pv
+
+#endif // PULSEVIEW_PV_DEVICE_INPUTFILE_H
diff --git a/pv/device/sessionfile.cpp b/pv/device/sessionfile.cpp
new file mode 100644 (file)
index 0000000..ffdeb67
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include "sessionfile.h"
+
+#include <libsigrok/libsigrok.h>
+
+namespace pv {
+namespace device {
+
+SessionFile::SessionFile(const std::string &path) :
+       File(path),
+       _sdi(NULL)
+{
+}
+
+sr_dev_inst* SessionFile::dev_inst() const
+{
+       return _sdi;
+}
+
+void SessionFile::use(SigSession *owner) throw(QString)
+{
+       assert(!_sdi);
+
+       if (sr_session_load(_path.c_str()) != SR_OK)
+               throw tr("Failed to open file.\n");
+
+       GSList *devlist = NULL;
+       sr_session_dev_list(&devlist);
+
+       if (!devlist || !devlist->data) {
+               if (devlist)
+                       g_slist_free(devlist);
+               throw tr("Failed to start session.");
+       }
+
+       _sdi = (sr_dev_inst*)devlist->data;
+       g_slist_free(devlist);
+
+       File::use(owner);
+}
+
+void SessionFile::release()
+{
+       if (!_owner)
+               return;
+
+       assert(_sdi);
+       File::release();
+       sr_dev_close(_sdi);
+       sr_dev_clear(_sdi->driver);
+       sr_session_destroy();
+       _sdi = NULL;
+}
+
+} // device
+} // pv
diff --git a/pv/device/sessionfile.h b/pv/device/sessionfile.h
new file mode 100644 (file)
index 0000000..c0ed958
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef PULSEVIEW_PV_DEVICE_SESSIONFILE_H
+#define PULSEVIEW_PV_DEVICE_SESSIONFILE_H
+
+#include "file.h"
+
+namespace pv {
+namespace device {
+
+class SessionFile : public File
+{
+public:
+       SessionFile(const std::string &path);
+
+       sr_dev_inst* dev_inst() const;
+
+       virtual void use(SigSession *owner) throw(QString);
+
+       virtual void release();
+
+private:
+       sr_dev_inst *_sdi;
+};
+
+} // device
+} // pv
+
+#endif // PULSEVIEW_PV_DEVICE_SESSIONFILE_H
index 829c95e994e2691db500fdbdfa5706be3352921e..57e9c51f8d51b647e8ea6164d61834c1d832d11a 100644 (file)
@@ -322,9 +322,15 @@ void MainWindow::load_file(QString file_name)
        const QString errorMessage(
                QString("Failed to load file %1").arg(file_name));
        const QString infoMessage;
-       _session.load_file(file_name.toStdString(),
-               boost::bind(&MainWindow::session_error, this,
-                       errorMessage, infoMessage));
+
+       try {
+               _session.set_file(file_name.toStdString());
+       } catch(QString e) {
+               show_session_error(tr("Failed to load ") + file_name, e);
+       }
+
+       _session.start_capture(boost::bind(&MainWindow::session_error, this,
+               errorMessage, infoMessage));
 }
 
 void MainWindow::show_session_error(
index 9af7df74e7a5f967023a20a62c4f6e68ed3d8c16..5cc999829f7bd1748aeb07b2732eed883f5d438c 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "devicemanager.h"
 #include "device/device.h"
+#include "device/file.h"
 
 #include "data/analog.h"
 #include "data/analogsnapshot.h"
@@ -91,21 +92,35 @@ shared_ptr<device::DevInst> SigSession::get_device() const
        return _dev_inst;
 }
 
-void SigSession::set_device(shared_ptr<device::DevInst> dev_inst)
+void SigSession::set_device(
+       shared_ptr<device::DevInst> dev_inst) throw(QString)
 {
        using pv::device::Device;
 
        // Ensure we are not capturing before setting the device
        stop_capture();
 
-       if (_dev_inst)
+       if (_dev_inst) {
+               sr_session_datafeed_callback_remove_all();
                _dev_inst->release();
+       }
+
+       _dev_inst = dev_inst;
+       _decode_traces.clear();
 
-       if (dev_inst)
+       if (dev_inst) {
                dev_inst->use(this);
+               sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
+               update_signals(dev_inst);
+       }
+}
 
-       _dev_inst = dev_inst;
-       update_signals(dev_inst);
+void SigSession::set_file(const string &name) throw(QString)
+{
+       // Deslect the old device, because file type detection in File::create
+       // destorys the old session inside libsigrok.
+       set_device(shared_ptr<device::DevInst>());
+       set_device(shared_ptr<device::DevInst>(device::File::create(name)));
 }
 
 void SigSession::release_device(device::DevInst *dev_inst)
@@ -117,51 +132,6 @@ void SigSession::release_device(device::DevInst *dev_inst)
        _dev_inst = shared_ptr<device::DevInst>();
 }
 
-void SigSession::load_file(const string &name,
-       function<void (const QString)> error_handler)
-{
-       stop_capture();
-
-       if (sr_session_load(name.c_str()) == SR_OK) {
-               GSList *devlist = NULL;
-               sr_session_dev_list(&devlist);
-
-               if (!devlist || !devlist->data ||
-                       sr_session_start() != SR_OK) {
-                       error_handler(tr("Failed to start session."));
-                       return;
-               }
-
-               shared_ptr<device::DevInst> dev_inst(
-                       new device::Device((sr_dev_inst*)devlist->data));
-               g_slist_free(devlist);
-
-               _decode_traces.clear();
-               update_signals(dev_inst);
-               read_sample_rate(dev_inst->dev_inst());
-
-               _sampling_thread = boost::thread(
-                       &SigSession::load_session_thread_proc, this,
-                       error_handler);
-
-       } else {
-               sr_input *in = NULL;
-
-               if (!(in = load_input_file_format(name.c_str(),
-                       error_handler)))
-                       return;
-
-               _decode_traces.clear();
-               update_signals(shared_ptr<device::DevInst>(
-                       new device::Device(in->sdi)));
-               read_sample_rate(in->sdi);
-
-               _sampling_thread = boost::thread(
-                       &SigSession::load_input_thread_proc, this,
-                       name, in, error_handler);
-       }
-}
-
 SigSession::capture_state SigSession::get_capture_state() const
 {
        lock_guard<mutex> lock(_sampling_mutex);
@@ -317,82 +287,6 @@ void SigSession::set_capture_state(capture_state state)
                capture_state_changed(state);
 }
 
-/**
- * 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)
-{
-       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;
-       }
-
-       /* 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;
-       }
-
-       /* 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::update_signals(shared_ptr<device::DevInst> dev_inst)
 {
        assert(dev_inst);
@@ -518,49 +412,6 @@ void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
        }
 }
 
-void SigSession::load_session_thread_proc(
-       function<void (const QString)> error_handler)
-{
-       (void)error_handler;
-
-       sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
-
-       set_capture_state(Running);
-
-       sr_session_run();
-
-       sr_session_destroy();
-       set_capture_state(Stopped);
-
-       // Confirm that SR_DF_END was received
-       assert(!_cur_logic_snapshot);
-       assert(_cur_analog_snapshots.empty());
-}
-
-void SigSession::load_input_thread_proc(const string name,
-       sr_input *in, function<void (const QString)> error_handler)
-{
-       (void)error_handler;
-
-       assert(in);
-       assert(in->format);
-
-       sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
-
-       set_capture_state(Running);
-
-       in->format->loadfile(in, name.c_str());
-
-       sr_session_destroy();
-       set_capture_state(Stopped);
-
-       // Confirm that SR_DF_END was received
-       assert(!_cur_logic_snapshot);
-       assert(_cur_analog_snapshots.empty());
-
-       delete in;
-}
-
 void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
        function<void (const QString)> error_handler)
 {
@@ -568,26 +419,19 @@ void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
        assert(dev_inst->dev_inst());
        assert(error_handler);
 
-       sr_session_new();
-       sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
+       read_sample_rate(dev_inst->dev_inst());
 
-       if (sr_session_dev_add(dev_inst->dev_inst()) != SR_OK) {
-               error_handler(tr("Failed to use device."));
-               sr_session_destroy();
-               return;
-       }
-
-       if (sr_session_start() != SR_OK) {
-               error_handler(tr("Failed to start session."));
+       try {
+               dev_inst->start();
+       } catch(const QString e) {
+               error_handler(e);
                return;
        }
 
        set_capture_state(dev_inst->is_trigger_enabled() ?
                AwaitingTrigger : Running);
 
-       sr_session_run();
-       sr_session_destroy();
-
+       dev_inst->run();
        set_capture_state(Stopped);
 
        // Confirm that SR_DF_END was received
index 39e4af6802818a1e3fa950eb5058704d22b85b31..da72015fc7935ffe03efbc846eec85368c92eec3 100644 (file)
@@ -81,12 +81,13 @@ public:
        /**
         * Sets device instance that will be used in the next capture session.
         */
-       void set_device(boost::shared_ptr<device::DevInst> dev_inst);
+       void set_device(boost::shared_ptr<device::DevInst> dev_inst)
+               throw(QString);
 
-       void release_device(device::DevInst *dev_inst);
+       void set_file(const std::string &name)
+               throw(QString);
 
-       void load_file(const std::string &name,
-               boost::function<void (const QString)> error_handler);
+       void release_device(device::DevInst *dev_inst);
 
        capture_state get_capture_state() const;
 
@@ -134,12 +135,6 @@ private:
                boost::function<void (const QString)> error_handler,
                sr_input_format *format = NULL);
 
-       void load_session_thread_proc(
-               boost::function<void (const QString)> error_handler);
-
-       void load_input_thread_proc(const std::string name, sr_input *in,
-               boost::function<void (const QString)> error_handler);
-
        void sample_thread_proc(boost::shared_ptr<device::DevInst> dev_inst,
                boost::function<void (const QString)> error_handler);
 
index 5e064a027f04840031f3a30cccb8b7d836c14759..66dd7e51d3702721b9f757729dfea0fc8dc4dfda 100644 (file)
@@ -59,6 +59,9 @@ set(pulseview_TEST_SOURCES
        ${PROJECT_SOURCE_DIR}/pv/data/signaldata.cpp
        ${PROJECT_SOURCE_DIR}/pv/device/device.cpp
        ${PROJECT_SOURCE_DIR}/pv/device/devinst.cpp
+       ${PROJECT_SOURCE_DIR}/pv/device/file.cpp
+       ${PROJECT_SOURCE_DIR}/pv/device/inputfile.cpp
+       ${PROJECT_SOURCE_DIR}/pv/device/sessionfile.cpp
        ${PROJECT_SOURCE_DIR}/pv/prop/int.cpp
        ${PROJECT_SOURCE_DIR}/pv/prop/property.cpp
        ${PROJECT_SOURCE_DIR}/pv/prop/string.cpp