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>
35 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
42 DeviceManager::~DeviceManager()
47 const std::list<sr_dev_inst*>& DeviceManager::devices() const
52 void DeviceManager::use_device(sr_dev_inst *sdi, SigSession *owner)
57 _used_devices[sdi] = owner;
62 void DeviceManager::release_device(sr_dev_inst *sdi)
66 // Notify the owner, and removed the device from the used device list
67 _used_devices[sdi]->release_device(sdi);
68 _used_devices.erase(sdi);
73 list<sr_dev_inst*> DeviceManager::driver_scan(
74 struct sr_dev_driver *const driver, GSList *const drvopts)
76 list<sr_dev_inst*> driver_devices;
80 // Remove any device instances from this driver from the device
81 // list. They will not be valid after the scan.
82 list<sr_dev_inst*>::iterator i = _devices.begin();
83 while (i != _devices.end()) {
84 if ((*i)->driver == driver)
85 i = _devices.erase(i);
90 // Release this driver and all it's attached devices
91 release_driver(driver);
94 GSList *const devices = sr_driver_scan(driver, drvopts);
95 for (GSList *l = devices; l; l = l->next)
96 driver_devices.push_back((sr_dev_inst*)l->data);
97 g_slist_free(devices);
98 driver_devices.sort(compare_devices);
100 // Add the scanned devices to the main list
101 _devices.insert(_devices.end(), driver_devices.begin(),
102 driver_devices.end());
103 _devices.sort(compare_devices);
105 return driver_devices;
108 string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
114 if (sdi->vendor && sdi->vendor[0]) {
116 if ((sdi->model && sdi->model[0]) ||
117 (sdi->version && sdi->version[0]))
121 if (sdi->model && sdi->model[0]) {
123 if (sdi->version && sdi->version[0])
127 if (sdi->version && sdi->version[0])
133 void DeviceManager::init_drivers()
135 // Initialise all libsigrok drivers
136 sr_dev_driver **const drivers = sr_driver_list();
137 for (sr_dev_driver **driver = drivers; *driver; driver++) {
138 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
140 string("Failed to initialize driver ") +
141 string((*driver)->name));
146 void DeviceManager::release_devices()
148 // Release all the used devices
149 for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
150 i != _used_devices.end(); i++)
151 release_device((*i).first);
153 _used_devices.clear();
155 // Clear all the drivers
156 sr_dev_driver **const drivers = sr_driver_list();
157 for (sr_dev_driver **driver = drivers; *driver; driver++)
158 sr_dev_clear(*driver);
161 void DeviceManager::scan_all_drivers()
163 // Scan all drivers for all devices.
164 struct sr_dev_driver **const drivers = sr_driver_list();
165 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
166 driver_scan(*driver);
169 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
172 for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
173 i != _used_devices.end(); i++)
174 if((*i).first->driver == driver)
176 // Notify the current owner of the device
177 (*i).second->release_device((*i).first);
179 // Remove it from the used device list
180 _used_devices.erase(i);
182 // Close the device instance
183 sr_dev_close((*i).first);
186 // Clear all the old device instances from this driver
187 sr_dev_clear(driver);
190 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
191 const sr_dev_inst *const b)
193 return format_device_title(a).compare(format_device_title(b)) < 0;