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::runtime_error;
34 using std::shared_ptr;
39 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
46 DeviceManager::~DeviceManager()
51 const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
56 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
57 struct sr_dev_driver *const driver, GSList *const drvopts)
59 list< shared_ptr<device::Device> > driver_devices;
63 // Remove any device instances from this driver from the device
64 // list. They will not be valid after the scan.
65 auto i = _devices.begin();
66 while (i != _devices.end()) {
67 if ((*i)->dev_inst()->driver == driver)
68 i = _devices.erase(i);
73 // Release this driver and all it's attached devices
74 release_driver(driver);
77 GSList *const devices = sr_driver_scan(driver, drvopts);
78 for (GSList *l = devices; l; l = l->next)
79 driver_devices.push_back(shared_ptr<device::Device>(
80 new device::Device((sr_dev_inst*)l->data)));
81 g_slist_free(devices);
82 driver_devices.sort(compare_devices);
84 // Add the scanned devices to the main list
85 _devices.insert(_devices.end(), driver_devices.begin(),
86 driver_devices.end());
87 _devices.sort(compare_devices);
89 return driver_devices;
92 const shared_ptr<device::Device> DeviceManager::find_device_from_info(
93 const map<string, string> search_info)
95 shared_ptr<device::Device> last_resort_dev;
96 map<string, string> dev_info;
98 last_resort_dev = NULL;
100 for (shared_ptr<device::Device> dev : _devices) {
102 dev_info = dev->get_device_info();
104 // If present, vendor and model always have to match.
105 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
106 if (dev_info.at("vendor") != search_info.at("vendor")) continue;
108 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
109 if (dev_info.at("model") != search_info.at("model")) continue;
111 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
112 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
113 && search_info.count("serial_num") > 0)
114 if (dev_info.at("serial_num") == search_info.at("serial_num") &&
115 dev_info.at("serial_num") != "0")
118 // Second best match: vendor/model/connection_id
119 if (dev_info.count("connection_id") > 0 &&
120 search_info.count("connection_id") > 0)
121 if (dev_info.at("connection_id") == search_info.at("connection_id"))
124 // Last resort: vendor/model/version
125 if (dev_info.count("version") > 0 &&
126 search_info.count("version") > 0)
127 if (dev_info.at("version") == search_info.at("version") &&
128 dev_info.at("version") != "0")
131 // For this device, we merely have a vendor/model match.
132 last_resort_dev = dev;
135 // If there wasn't even a vendor/model/version match, we end up here.
136 // This is usually the case for devices with only vendor/model data.
137 // The selected device may be wrong with multiple such devices attached
138 // but it is the best we can do at this point. After all, there may be
139 // only one such device and we do want to select it in this case.
140 return last_resort_dev;
143 void DeviceManager::init_drivers()
145 // Initialise all libsigrok drivers
146 sr_dev_driver **const drivers = sr_driver_list();
147 for (sr_dev_driver **driver = drivers; *driver; driver++) {
148 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
150 string("Failed to initialize driver ") +
151 string((*driver)->name));
156 void DeviceManager::release_devices()
158 // Release all the used devices
159 for (shared_ptr<device::Device> dev : _devices) {
164 // Clear all the drivers
165 sr_dev_driver **const drivers = sr_driver_list();
166 for (sr_dev_driver **driver = drivers; *driver; driver++)
167 sr_dev_clear(*driver);
170 void DeviceManager::scan_all_drivers()
172 // Scan all drivers for all devices.
173 struct sr_dev_driver **const drivers = sr_driver_list();
174 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
175 driver_scan(*driver);
178 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
180 for (shared_ptr<device::Device> dev : _devices) {
182 if(dev->dev_inst()->driver == driver)
186 // Clear all the old device instances from this driver
187 sr_dev_clear(driver);
190 bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
191 shared_ptr<device::Device> b)
195 return a->format_device_title().compare(b->format_device_title()) < 0;