From: Joel Holdsworth Date: Sat, 22 Feb 2014 09:23:33 +0000 (+0000) Subject: Moved DeviceManager::use_device and release_device into DevInst X-Git-Tag: pulseview-0.2.0~54 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=996b7c9da9b5cb56413e829217e1e7d7d7d520da;p=pulseview.git Moved DeviceManager::use_device and release_device into DevInst --- diff --git a/pv/device/devinst.cpp b/pv/device/devinst.cpp index 5fe29517..adbf8968 100644 --- a/pv/device/devinst.cpp +++ b/pv/device/devinst.cpp @@ -26,11 +26,14 @@ #include "devinst.h" +#include + namespace pv { namespace device { DevInst::DevInst(sr_dev_inst *sdi) : - _sdi(sdi) + _sdi(sdi), + _owner(NULL) { assert(_sdi); } @@ -40,6 +43,28 @@ sr_dev_inst* DevInst::dev_inst() const return _sdi; } +void DevInst::use(SigSession *owner) +{ + assert(owner); + assert(!_owner); + _owner = owner; + sr_dev_open(_sdi); +} + +void DevInst::release() +{ + if (_owner) { + _owner->release_device(this); + _owner = NULL; + sr_dev_close(_sdi); + } +} + +SigSession* DevInst::owner() const +{ + return _owner; +} + GVariant* DevInst::get_config(const sr_probe_group *group, int key) { GVariant *data = NULL; diff --git a/pv/device/devinst.h b/pv/device/devinst.h index 6e81f49c..9098027e 100644 --- a/pv/device/devinst.h +++ b/pv/device/devinst.h @@ -36,6 +36,9 @@ struct sr_probe; struct sr_probe_group; namespace pv { + +class SigSession; + namespace device { class DevInst : public QObject @@ -48,6 +51,12 @@ protected: public: sr_dev_inst* dev_inst() const; + void use(SigSession *owner); + + void release(); + + SigSession* owner() const; + virtual std::string format_device_title() const = 0; GVariant* get_config(const sr_probe_group *group, int key); @@ -71,6 +80,7 @@ signals: protected: sr_dev_inst *const _sdi; + SigSession *_owner; }; } // device diff --git a/pv/devicemanager.cpp b/pv/devicemanager.cpp index 5844e80e..fb487a81 100644 --- a/pv/devicemanager.cpp +++ b/pv/devicemanager.cpp @@ -26,6 +26,8 @@ #include #include +#include + #include using boost::shared_ptr; @@ -54,30 +56,6 @@ const list< shared_ptr >& DeviceManager::devices() const return _devices; } -void DeviceManager::use_device(shared_ptr dev_inst, - SigSession *owner) -{ - assert(dev_inst); - assert(owner); - - _used_devices[dev_inst] = owner; - - sr_dev_open(dev_inst->dev_inst()); -} - -void DeviceManager::release_device(shared_ptr dev_inst) -{ - assert(dev_inst); - - // Notify the owner, and remove the device from the used device list - map< shared_ptr, pv::SigSession*>::const_iterator - iter = _used_devices.find(dev_inst); - assert(iter != _used_devices.end()); - - (*iter).second->release_device(dev_inst); - _used_devices.erase(dev_inst); -} - list< shared_ptr > DeviceManager::driver_scan( struct sr_dev_driver *const driver, GSList *const drvopts) { @@ -130,11 +108,10 @@ void DeviceManager::init_drivers() void DeviceManager::release_devices() { // Release all the used devices - for (map, SigSession*>::iterator i = - _used_devices.begin(); i != _used_devices.end(); i++) - release_device((*i).first); - - _used_devices.clear(); + BOOST_FOREACH(shared_ptr dev, _devices) { + assert(dev); + dev->release(); + } // Clear all the drivers sr_dev_driver **const drivers = sr_driver_list(); @@ -152,20 +129,11 @@ void DeviceManager::scan_all_drivers() void DeviceManager::release_driver(struct sr_dev_driver *const driver) { - assert(driver); - for (map, SigSession*>::iterator i = - _used_devices.begin(); i != _used_devices.end(); i++) - if((*i).first->dev_inst()->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->dev_inst()); - } + BOOST_FOREACH(shared_ptr dev, _devices) { + assert(dev); + if(dev->dev_inst()->driver == driver) + dev->release(); + } // Clear all the old device instances from this driver sr_dev_clear(driver); diff --git a/pv/devicemanager.h b/pv/devicemanager.h index 2ce523dc..acef8a42 100644 --- a/pv/devicemanager.h +++ b/pv/devicemanager.h @@ -24,7 +24,6 @@ #include #include -#include #include #include @@ -50,11 +49,6 @@ public: const std::list< boost::shared_ptr >& devices() const; - void use_device(boost::shared_ptr dev_inst, - SigSession *owner); - - void release_device(boost::shared_ptr dev_inst); - std::list< boost::shared_ptr > driver_scan( struct sr_dev_driver *const driver, GSList *const drvopts = NULL); @@ -74,8 +68,6 @@ private: private: struct sr_context *const _sr_ctx; std::list< boost::shared_ptr > _devices; - std::map< boost::shared_ptr, pv::SigSession*> - _used_devices; }; } // namespace pv diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index aab5be7d..90fb2798 100644 --- a/pv/sigsession.cpp +++ b/pv/sigsession.cpp @@ -80,9 +80,7 @@ SigSession::~SigSession() if (_sampling_thread.joinable()) _sampling_thread.join(); - shared_ptr device(dynamic_pointer_cast(_dev_inst)); - if (device) - _device_manager.release_device(device); + _dev_inst->release(); // TODO: This should not be necessary _session = NULL; @@ -100,21 +98,20 @@ void SigSession::set_device(shared_ptr dev_inst) // Ensure we are not capturing before setting the device stop_capture(); - shared_ptr old_device(dynamic_pointer_cast(_dev_inst)); - if (old_device) - _device_manager.release_device(old_device); + if (_dev_inst) + _dev_inst->release(); - shared_ptr new_device(dynamic_pointer_cast(dev_inst)); - if (new_device) - _device_manager.use_device(new_device, this); + if (dev_inst) + dev_inst->use(this); _dev_inst = dev_inst; update_signals(dev_inst); } -void SigSession::release_device(shared_ptr dev_inst) +void SigSession::release_device(device::DevInst *dev_inst) { (void)dev_inst; + assert(_dev_inst.get() == dev_inst); assert(_capture_state == Stopped); _dev_inst = shared_ptr(); diff --git a/pv/sigsession.h b/pv/sigsession.h index 6b2e7599..bed7474d 100644 --- a/pv/sigsession.h +++ b/pv/sigsession.h @@ -83,7 +83,7 @@ public: */ void set_device(boost::shared_ptr dev_inst); - void release_device(boost::shared_ptr dev_inst); + void release_device(device::DevInst *dev_inst); void load_file(const std::string &name, boost::function error_handler);