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 <libsigrok/libsigrok.h>
33 using std::ostringstream;
34 using std::runtime_error;
35 using std::shared_ptr;
40 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
47 DeviceManager::~DeviceManager()
52 const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
57 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
58 struct sr_dev_driver *const driver, GSList *const drvopts)
60 list< shared_ptr<device::Device> > driver_devices;
64 // Remove any device instances from this driver from the device
65 // list. They will not be valid after the scan.
66 auto i = _devices.begin();
67 while (i != _devices.end()) {
68 if ((*i)->dev_inst()->driver == driver)
69 i = _devices.erase(i);
74 // Release this driver and all it's attached devices
75 release_driver(driver);
78 GSList *const devices = sr_driver_scan(driver, drvopts);
79 for (GSList *l = devices; l; l = l->next)
80 driver_devices.push_back(shared_ptr<device::Device>(
81 new device::Device((sr_dev_inst*)l->data)));
82 g_slist_free(devices);
83 driver_devices.sort(compare_devices);
85 // Add the scanned devices to the main list
86 _devices.insert(_devices.end(), driver_devices.begin(),
87 driver_devices.end());
88 _devices.sort(compare_devices);
90 return driver_devices;
93 void DeviceManager::init_drivers()
95 // Initialise all libsigrok drivers
96 sr_dev_driver **const drivers = sr_driver_list();
97 for (sr_dev_driver **driver = drivers; *driver; driver++) {
98 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
100 string("Failed to initialize driver ") +
101 string((*driver)->name));
106 void DeviceManager::release_devices()
108 // Release all the used devices
109 for (shared_ptr<device::Device> dev : _devices) {
114 // Clear all the drivers
115 sr_dev_driver **const drivers = sr_driver_list();
116 for (sr_dev_driver **driver = drivers; *driver; driver++)
117 sr_dev_clear(*driver);
120 void DeviceManager::scan_all_drivers()
122 // Scan all drivers for all devices.
123 struct sr_dev_driver **const drivers = sr_driver_list();
124 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
125 driver_scan(*driver);
128 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
130 for (shared_ptr<device::Device> dev : _devices) {
132 if(dev->dev_inst()->driver == driver)
136 // Clear all the old device instances from this driver
137 sr_dev_clear(driver);
140 bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
141 shared_ptr<device::Device> b)
145 return a->format_device_title().compare(b->format_device_title()) < 0;