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 remove_if(_devices.begin(), _devices.end(),
86 [&](shared_ptr<HardwareDevice> device) {
87 return device->driver() == driver; });
90 auto devices = driver->scan(drvopts);
91 driver_devices.insert(driver_devices.end(), devices.begin(), devices.end());
92 driver_devices.sort(compare_devices);
94 // Add the scanned devices to the main list
95 _devices.insert(_devices.end(), driver_devices.begin(),
96 driver_devices.end());
97 _devices.sort(compare_devices);
99 return driver_devices;
102 const map<string, string> DeviceManager::get_device_info(
103 shared_ptr<Device> device)
105 map<string, string> result;
109 if (device->vendor().length() > 0)
110 result["vendor"] = device->vendor();
111 if (device->model().length() > 0)
112 result["model"] = device->model();
113 if (device->version().length() > 0)
114 result["version"] = device->version();
115 if (device->serial_number().length() > 0)
116 result["serial_num"] = device->serial_number();
117 if (device->connection_id().length() > 0)
118 result["connection_id"] = device->connection_id();
123 const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info(
124 const map<string, string> search_info)
126 shared_ptr<HardwareDevice> last_resort_dev;
127 map<string, string> dev_info;
129 last_resort_dev = NULL;
131 for (shared_ptr<HardwareDevice> dev : _devices) {
133 dev_info = get_device_info(dev);
135 // If present, vendor and model always have to match.
136 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
137 if (dev_info.at("vendor") != search_info.at("vendor")) continue;
139 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
140 if (dev_info.at("model") != search_info.at("model")) continue;
142 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
143 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
144 && search_info.count("serial_num") > 0)
145 if (dev_info.at("serial_num") == search_info.at("serial_num") &&
146 dev_info.at("serial_num") != "0")
149 // Second best match: vendor/model/connection_id
150 if (dev_info.count("connection_id") > 0 &&
151 search_info.count("connection_id") > 0)
152 if (dev_info.at("connection_id") == search_info.at("connection_id"))
155 // Last resort: vendor/model/version
156 if (dev_info.count("version") > 0 &&
157 search_info.count("version") > 0)
158 if (dev_info.at("version") == search_info.at("version") &&
159 dev_info.at("version") != "0")
162 // For this device, we merely have a vendor/model match.
163 last_resort_dev = dev;
166 // If there wasn't even a vendor/model/version match, we end up here.
167 // This is usually the case for devices with only vendor/model data.
168 // The selected device may be wrong with multiple such devices attached
169 // but it is the best we can do at this point. After all, there may be
170 // only one such device and we do want to select it in this case.
171 return last_resort_dev;
174 string DeviceManager::device_description(shared_ptr<Device> device)
176 auto session_device = dynamic_pointer_cast<SessionDevice>(device);
179 return boost::filesystem::path(
180 session_device->parent()->filename()).filename().string();
184 vector<string> parts = {device->vendor(), device->model(),
185 device->version(), device->serial_number()};
187 for (size_t i = 0; i < parts.size(); i++)
189 if (parts[i].length() > 0)
197 if (device->serial_number().length() == 0 &&
198 device->connection_id().length() > 0)
199 s << " " << device->connection_id();
204 bool DeviceManager::compare_devices(shared_ptr<HardwareDevice> a,
205 shared_ptr<HardwareDevice> b)
210 return device_description(a).compare(device_description(b)) < 0;