*/
#include "devicemanager.h"
+#include "sigsession.h"
#include <cassert>
#include <sstream>
return _devices;
}
+void DeviceManager::use_device(sr_dev_inst *sdi, SigSession *owner)
+{
+ assert(sdi);
+ assert(owner);
+
+ _used_devices[sdi] = owner;
+
+ sr_dev_open(sdi);
+}
+
+void DeviceManager::release_device(sr_dev_inst *sdi)
+{
+ assert(sdi);
+
+ // Notify the owner, and removed the device from the used device list
+ _used_devices[sdi]->release_device(sdi);
+ _used_devices.erase(sdi);
+
+ sr_dev_close(sdi);
+}
+
list<sr_dev_inst*> DeviceManager::driver_scan(
struct sr_dev_driver *const driver, GSList *const drvopts)
{
i++;
}
- // Clear all the old device instances from this driver
- sr_dev_clear(driver);
+ // Release this driver and all it's attached devices
+ release_driver(driver);
// Do the scan
GSList *const devices = sr_driver_scan(driver, drvopts);
void DeviceManager::release_devices()
{
+ // Release all the used devices
+ for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
+ i != _used_devices.end(); i++)
+ release_device((*i).first);
+
+ _used_devices.clear();
+
+ // Clear all the drivers
sr_dev_driver **const drivers = sr_driver_list();
for (sr_dev_driver **driver = drivers; *driver; driver++)
sr_dev_clear(*driver);
driver_scan(*driver);
}
+void DeviceManager::release_driver(struct sr_dev_driver *const driver)
+{
+ assert(driver);
+ for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
+ i != _used_devices.end(); i++)
+ if((*i).first->driver == driver)
+ {
+ // Notify the current owner of the device
+ (*i).second->release_device((*i).first);
+
+ // Remove it from the used device list
+ _used_devices.erase(i);
+
+ // Close the device instance
+ sr_dev_close((*i).first);
+ }
+
+ // Clear all the old device instances from this driver
+ sr_dev_clear(driver);
+}
+
bool DeviceManager::compare_devices(const sr_dev_inst *const a,
const sr_dev_inst *const b)
{
#include <glib.h>
#include <list>
+#include <map>
#include <string>
struct sr_context;
namespace pv {
+class SigSession;
+
class DeviceManager
{
public:
const std::list<sr_dev_inst*>& devices() const;
+ void use_device(sr_dev_inst *sdi, SigSession *owner);
+
+ void release_device(sr_dev_inst *sdi);
+
std::list<sr_dev_inst*> driver_scan(
struct sr_dev_driver *const driver,
GSList *const drvopts = NULL);
private:
void init_drivers();
- static void release_devices();
+ void release_devices();
void scan_all_drivers();
+ void release_driver(struct sr_dev_driver *const driver);
+
static bool compare_devices(const sr_dev_inst *const a,
const sr_dev_inst *const b);
private:
struct sr_context *const _sr_ctx;
std::list<sr_dev_inst*> _devices;
+ std::map<sr_dev_inst*, pv::SigSession*> _used_devices;
};
} // namespace pv
const char *open_file_name,
QWidget *parent) :
QMainWindow(parent),
- _device_manager(device_manager)
+ _device_manager(device_manager),
+ _session(device_manager)
{
setup_ui();
if (open_file_name) {
void MainWindow::on_actionConnect_triggered()
{
dialogs::Connect dlg(this, _device_manager);
- if (!dlg.exec())
- return;
- struct sr_dev_inst *const sdi = dlg.get_selected_device();
+ // If the user selected a device, select it in the device list. Select the
+ // current device otherwise.
+ struct sr_dev_inst *const sdi = dlg.exec() ?
+ dlg.get_selected_device() : _session.get_device();
+
update_device_list(sdi);
}
#include "sigsession.h"
+#include "devicemanager.h"
#include "data/analog.h"
#include "data/analogsnapshot.h"
#include "data/logic.h"
// 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)
{
_session = NULL;
}
+struct sr_dev_inst* SigSession::get_device() const
+{
+ return _sdi;
+}
+
void SigSession::set_device(struct sr_dev_inst *sdi)
{
if (_sdi)
- sr_dev_close(_sdi);
+ _device_manager.release_device(_sdi);
if (sdi)
- sr_dev_open(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)
{
namespace pv {
+class DeviceManager;
+
namespace data {
class Analog;
class AnalogSnapshot;
};
public:
- SigSession();
+ SigSession(DeviceManager &device_manager);
~SigSession();
+ struct sr_dev_inst* get_device() const;
+
/**
* Sets device instance that will be used in the next capture session.
*/
void set_device(struct sr_dev_inst *sdi);
+ void release_device(struct sr_dev_inst *sdi);
+
void load_file(const std::string &name,
boost::function<void (const QString)> error_handler);
const struct sr_datafeed_packet *packet, void *cb_data);
private:
+ DeviceManager &_device_manager;
/**
* The device instance that will be used in the next capture session.