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 "sigsession.h"
29 #include <libsigrok/libsigrok.h>
33 using std::ostringstream;
34 using std::runtime_error;
39 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
46 DeviceManager::~DeviceManager()
51 const std::list<sr_dev_inst*>& DeviceManager::devices() const
56 void DeviceManager::use_device(sr_dev_inst *sdi, SigSession *owner)
61 _used_devices[sdi] = owner;
66 void DeviceManager::release_device(sr_dev_inst *sdi)
70 // Notify the owner, and removed the device from the used device list
71 _used_devices[sdi]->release_device(sdi);
72 _used_devices.erase(sdi);
77 list<sr_dev_inst*> DeviceManager::driver_scan(
78 struct sr_dev_driver *const driver, GSList *const drvopts)
80 list<sr_dev_inst*> driver_devices;
84 // Remove any device instances from this driver from the device
85 // list. They will not be valid after the scan.
86 list<sr_dev_inst*>::iterator i = _devices.begin();
87 while (i != _devices.end()) {
88 if ((*i)->driver == driver)
89 i = _devices.erase(i);
94 // Release this driver and all it's attached devices
95 release_driver(driver);
98 GSList *const devices = sr_driver_scan(driver, drvopts);
99 for (GSList *l = devices; l; l = l->next)
100 driver_devices.push_back((sr_dev_inst*)l->data);
101 g_slist_free(devices);
102 driver_devices.sort(compare_devices);
104 // Add the scanned devices to the main list
105 _devices.insert(_devices.end(), driver_devices.begin(),
106 driver_devices.end());
107 _devices.sort(compare_devices);
109 return driver_devices;
112 string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
118 if (sdi->vendor && sdi->vendor[0]) {
120 if ((sdi->model && sdi->model[0]) ||
121 (sdi->version && sdi->version[0]))
125 if (sdi->model && sdi->model[0]) {
127 if (sdi->version && sdi->version[0])
131 if (sdi->version && sdi->version[0])
137 void DeviceManager::init_drivers()
139 // Initialise all libsigrok drivers
140 sr_dev_driver **const drivers = sr_driver_list();
141 for (sr_dev_driver **driver = drivers; *driver; driver++) {
142 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
144 string("Failed to initialize driver ") +
145 string((*driver)->name));
150 void DeviceManager::release_devices()
152 // Release all the used devices
153 for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
154 i != _used_devices.end(); i++)
155 release_device((*i).first);
157 _used_devices.clear();
159 // Clear all the drivers
160 sr_dev_driver **const drivers = sr_driver_list();
161 for (sr_dev_driver **driver = drivers; *driver; driver++)
162 sr_dev_clear(*driver);
165 void DeviceManager::scan_all_drivers()
167 // Scan all drivers for all devices.
168 struct sr_dev_driver **const drivers = sr_driver_list();
169 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
170 driver_scan(*driver);
173 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
176 for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
177 i != _used_devices.end(); i++)
178 if((*i).first->driver == driver)
180 // Notify the current owner of the device
181 (*i).second->release_device((*i).first);
183 // Remove it from the used device list
184 _used_devices.erase(i);
186 // Close the device instance
187 sr_dev_close((*i).first);
190 // Clear all the old device instances from this driver
191 sr_dev_clear(driver);
194 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
195 const sr_dev_inst *const b)
197 return format_device_title(a).compare(format_device_title(b)) < 0;