2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "devicemanager.h"
22 #include "device/device.h"
23 #include "sigsession.h"
29 #include <boost/foreach.hpp>
31 #include <libsigrok/libsigrok.h>
33 using boost::shared_ptr;
36 using std::ostringstream;
37 using std::runtime_error;
42 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
49 DeviceManager::~DeviceManager()
54 const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
59 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
60 struct sr_dev_driver *const driver, GSList *const drvopts)
62 list< shared_ptr<device::Device> > driver_devices;
66 // Remove any device instances from this driver from the device
67 // list. They will not be valid after the scan.
68 list< shared_ptr<device::Device> >::iterator i = _devices.begin();
69 while (i != _devices.end()) {
70 if ((*i)->dev_inst()->driver == driver)
71 i = _devices.erase(i);
76 // Release this driver and all it's attached devices
77 release_driver(driver);
80 GSList *const devices = sr_driver_scan(driver, drvopts);
81 for (GSList *l = devices; l; l = l->next)
82 driver_devices.push_back(shared_ptr<device::Device>(
83 new device::Device((sr_dev_inst*)l->data)));
84 g_slist_free(devices);
85 driver_devices.sort(compare_devices);
87 // Add the scanned devices to the main list
88 _devices.insert(_devices.end(), driver_devices.begin(),
89 driver_devices.end());
90 _devices.sort(compare_devices);
92 return driver_devices;
95 void DeviceManager::init_drivers()
97 // Initialise all libsigrok drivers
98 sr_dev_driver **const drivers = sr_driver_list();
99 for (sr_dev_driver **driver = drivers; *driver; driver++) {
100 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
102 string("Failed to initialize driver ") +
103 string((*driver)->name));
108 void DeviceManager::release_devices()
110 // Release all the used devices
111 BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
116 // Clear all the drivers
117 sr_dev_driver **const drivers = sr_driver_list();
118 for (sr_dev_driver **driver = drivers; *driver; driver++)
119 sr_dev_clear(*driver);
122 void DeviceManager::scan_all_drivers()
124 // Scan all drivers for all devices.
125 struct sr_dev_driver **const drivers = sr_driver_list();
126 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
127 driver_scan(*driver);
130 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
132 BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
134 if(dev->dev_inst()->driver == driver)
138 // Clear all the old device instances from this driver
139 sr_dev_clear(driver);
142 bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
143 shared_ptr<device::Device> b)
147 return a->format_device_title().compare(b->format_device_title()) < 0;