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"
30 #include <libsigrok/libsigrok.hpp>
32 #include <boost/filesystem.hpp>
34 using std::dynamic_pointer_cast;
37 using std::ostringstream;
39 using std::runtime_error;
40 using std::shared_ptr;
44 using Glib::VariantBase;
46 using sigrok::ConfigKey;
47 using sigrok::Context;
50 using sigrok::HardwareDevice;
51 using sigrok::SessionDevice;
55 DeviceManager::DeviceManager(shared_ptr<Context> context) :
58 for (auto entry : context->drivers())
59 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
62 DeviceManager::~DeviceManager()
66 shared_ptr<Context> DeviceManager::context()
71 const list< shared_ptr<HardwareDevice> >& DeviceManager::devices() const
76 list< shared_ptr<HardwareDevice> > DeviceManager::driver_scan(
77 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
79 list< shared_ptr<HardwareDevice> > driver_devices;
83 // Remove any device instances from this driver from the device
84 // list. They will not be valid after the scan.
85 _devices.remove_if([&](shared_ptr<HardwareDevice> device) {
86 return device->driver() == driver; });
89 auto devices = driver->scan(drvopts);
90 driver_devices.insert(driver_devices.end(), devices.begin(), devices.end());
92 // Add the scanned devices to the main list, set display names and sort.
93 _devices.insert(_devices.end(), driver_devices.begin(),
94 driver_devices.end());
96 for (shared_ptr<Device> device : _devices)
97 _display_names[device] = build_display_name(device);
99 _devices.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
100 { return compare_devices(a, b); });
102 // As the display names depend on the complete _devices list,
103 // we need to recompute them. However, there is no need to
104 // recomute all names of the _devices list since only the
105 // devices that use the given driver can be affected.
106 for (shared_ptr<Device> device : driver_devices)
107 _display_names[device] = build_display_name(device);
109 driver_devices.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
110 { return compare_devices(a, b); });
112 return driver_devices;
115 const map<string, string> DeviceManager::get_device_info(
116 shared_ptr<Device> device)
118 map<string, string> result;
122 if (device->vendor().length() > 0)
123 result["vendor"] = device->vendor();
124 if (device->model().length() > 0)
125 result["model"] = device->model();
126 if (device->version().length() > 0)
127 result["version"] = device->version();
128 if (device->serial_number().length() > 0)
129 result["serial_num"] = device->serial_number();
130 if (device->connection_id().length() > 0)
131 result["connection_id"] = device->connection_id();
136 const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info(
137 const map<string, string> search_info)
139 shared_ptr<HardwareDevice> last_resort_dev;
140 map<string, string> dev_info;
142 last_resort_dev = NULL;
144 for (shared_ptr<HardwareDevice> dev : _devices) {
146 dev_info = get_device_info(dev);
148 // If present, vendor and model always have to match.
149 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
150 if (dev_info.at("vendor") != search_info.at("vendor")) continue;
152 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
153 if (dev_info.at("model") != search_info.at("model")) continue;
155 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
156 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
157 && search_info.count("serial_num") > 0)
158 if (dev_info.at("serial_num") == search_info.at("serial_num") &&
159 dev_info.at("serial_num") != "0")
162 // Second best match: vendor/model/connection_id
163 if (dev_info.count("connection_id") > 0 &&
164 search_info.count("connection_id") > 0)
165 if (dev_info.at("connection_id") == search_info.at("connection_id"))
168 // Last resort: vendor/model/version
169 if (dev_info.count("version") > 0 &&
170 search_info.count("version") > 0)
171 if (dev_info.at("version") == search_info.at("version") &&
172 dev_info.at("version") != "0")
175 // For this device, we merely have a vendor/model match.
176 last_resort_dev = dev;
179 // If there wasn't even a vendor/model/version match, we end up here.
180 // This is usually the case for devices with only vendor/model data.
181 // The selected device may be wrong with multiple such devices attached
182 // but it is the best we can do at this point. After all, there may be
183 // only one such device and we do want to select it in this case.
184 return last_resort_dev;
187 const string DeviceManager::build_display_name(shared_ptr<Device> device)
189 auto session_device = dynamic_pointer_cast<SessionDevice>(device);
190 auto hardware_device = dynamic_pointer_cast<HardwareDevice>(device);
193 return boost::filesystem::path(
194 session_device->parent()->filename()).filename().string();
198 bool multiple_dev = false;
200 // If we can find another device with the same model/vendor then
201 // we have at least two such devices and need to distinguish them.
203 multiple_dev = any_of(_devices.begin(), _devices.end(),
204 [&](shared_ptr<HardwareDevice> dev) {
205 return (dev->vendor() == hardware_device->vendor() &&
206 dev->model() == hardware_device->model()) &&
207 dev != hardware_device;
210 vector<string> parts = {device->vendor(), device->model()};
213 parts.push_back(device->version());
214 parts.push_back(device->serial_number());
216 if ((device->serial_number().length() == 0) &&
217 (device->connection_id().length() > 0))
218 parts.push_back("("+device->connection_id()+")");
221 for (size_t i = 0; i < parts.size(); i++)
223 if (parts[i].length() > 0)
234 const std::string DeviceManager::get_display_name(std::shared_ptr<sigrok::Device> dev)
236 return _display_names[dev];
239 void DeviceManager::update_display_name(std::shared_ptr<sigrok::Device> dev)
241 _display_names[dev] = build_display_name(dev);
244 bool DeviceManager::compare_devices(shared_ptr<Device> a,
245 shared_ptr<Device> b)
250 return _display_names[a].compare(_display_names[b]) < 0;