#include "devinst.h"
+#include <pv/sigsession.h>
+
namespace pv {
namespace device {
DevInst::DevInst(sr_dev_inst *sdi) :
- _sdi(sdi)
+ _sdi(sdi),
+ _owner(NULL)
{
assert(_sdi);
}
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;
struct sr_probe_group;
namespace pv {
+
+class SigSession;
+
namespace device {
class DevInst : public QObject
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);
protected:
sr_dev_inst *const _sdi;
+ SigSession *_owner;
};
} // device
#include <stdexcept>
#include <string>
+#include <boost/foreach.hpp>
+
#include <libsigrok/libsigrok.h>
using boost::shared_ptr;
return _devices;
}
-void DeviceManager::use_device(shared_ptr<device::Device> 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<device::Device> dev_inst)
-{
- assert(dev_inst);
-
- // Notify the owner, and remove the device from the used device list
- map< shared_ptr<device::Device>, 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<device::Device> > DeviceManager::driver_scan(
struct sr_dev_driver *const driver, GSList *const drvopts)
{
void DeviceManager::release_devices()
{
// Release all the used devices
- for (map<shared_ptr<device::Device>, SigSession*>::iterator i =
- _used_devices.begin(); i != _used_devices.end(); i++)
- release_device((*i).first);
-
- _used_devices.clear();
+ BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
+ assert(dev);
+ dev->release();
+ }
// Clear all the drivers
sr_dev_driver **const drivers = sr_driver_list();
void DeviceManager::release_driver(struct sr_dev_driver *const driver)
{
- assert(driver);
- for (map<shared_ptr<device::Device>, 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<device::Device> 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);
#include <glib.h>
#include <list>
-#include <map>
#include <string>
#include <boost/shared_ptr.hpp>
const std::list< boost::shared_ptr<pv::device::Device> >&
devices() const;
- void use_device(boost::shared_ptr<pv::device::Device> dev_inst,
- SigSession *owner);
-
- void release_device(boost::shared_ptr<pv::device::Device> dev_inst);
-
std::list< boost::shared_ptr<pv::device::Device> > driver_scan(
struct sr_dev_driver *const driver,
GSList *const drvopts = NULL);
private:
struct sr_context *const _sr_ctx;
std::list< boost::shared_ptr<pv::device::Device> > _devices;
- std::map< boost::shared_ptr<pv::device::Device>, pv::SigSession*>
- _used_devices;
};
} // namespace pv
if (_sampling_thread.joinable())
_sampling_thread.join();
- shared_ptr<Device> device(dynamic_pointer_cast<Device>(_dev_inst));
- if (device)
- _device_manager.release_device(device);
+ _dev_inst->release();
// TODO: This should not be necessary
_session = NULL;
// Ensure we are not capturing before setting the device
stop_capture();
- shared_ptr<Device> old_device(dynamic_pointer_cast<Device>(_dev_inst));
- if (old_device)
- _device_manager.release_device(old_device);
+ if (_dev_inst)
+ _dev_inst->release();
- shared_ptr<Device> new_device(dynamic_pointer_cast<Device>(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<device::DevInst> 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<device::DevInst>();
*/
void set_device(boost::shared_ptr<device::DevInst> dev_inst);
- void release_device(boost::shared_ptr<device::DevInst> dev_inst);
+ void release_device(device::DevInst *dev_inst);
void load_file(const std::string &name,
boost::function<void (const QString)> error_handler);