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.hpp"
22 #include "session.hpp"
30 #include <libsigrokcxx/libsigrokcxx.hpp>
32 #include <boost/algorithm/string/join.hpp>
33 #include <boost/filesystem.hpp>
35 #include <pv/devices/hardwaredevice.hpp>
37 using boost::algorithm::join;
39 using std::dynamic_pointer_cast;
43 using std::runtime_error;
44 using std::shared_ptr;
48 using Glib::VariantBase;
50 using sigrok::ConfigKey;
51 using sigrok::Context;
53 using sigrok::SessionDevice;
57 DeviceManager::DeviceManager(shared_ptr<Context> context) :
60 for (auto entry : context->drivers())
61 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
64 DeviceManager::~DeviceManager()
68 const std::shared_ptr<sigrok::Context>& DeviceManager::context() const
73 shared_ptr<Context> DeviceManager::context()
78 const list< shared_ptr<devices::HardwareDevice> >&
79 DeviceManager::devices() const
84 list< shared_ptr<devices::HardwareDevice> >
85 DeviceManager::driver_scan(
86 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
88 list< shared_ptr<devices::HardwareDevice> > driver_devices;
92 // Remove any device instances from this driver from the device
93 // list. They will not be valid after the scan.
94 devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) {
95 return device->hardware_device()->driver() == driver; });
98 auto devices = driver->scan(drvopts);
100 // Add the scanned devices to the main list, set display names and sort.
101 for (shared_ptr<sigrok::HardwareDevice> device : devices) {
102 const shared_ptr<devices::HardwareDevice> d(
103 new devices::HardwareDevice(context_, device));
104 driver_devices.push_back(d);
107 for (shared_ptr<devices::HardwareDevice> device : driver_devices)
108 build_display_name(device);
110 devices_.insert(devices_.end(), driver_devices.begin(),
111 driver_devices.end());
112 devices_.sort([&](shared_ptr<devices::Device> a,
113 shared_ptr<devices::Device> b)
114 { return compare_devices(a, b); });
116 return driver_devices;
119 const map<string, string> DeviceManager::get_device_info(
120 shared_ptr<devices::Device> device)
122 map<string, string> result;
126 const shared_ptr<sigrok::Device> sr_dev = device->device();
127 if (sr_dev->vendor().length() > 0)
128 result["vendor"] = sr_dev->vendor();
129 if (sr_dev->model().length() > 0)
130 result["model"] = sr_dev->model();
131 if (sr_dev->version().length() > 0)
132 result["version"] = sr_dev->version();
133 if (sr_dev->serial_number().length() > 0)
134 result["serial_num"] = sr_dev->serial_number();
135 if (sr_dev->connection_id().length() > 0)
136 result["connection_id"] = sr_dev->connection_id();
141 const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info(
142 const map<string, string> search_info)
144 shared_ptr<devices::HardwareDevice> last_resort_dev;
145 map<string, string> dev_info;
147 for (shared_ptr<devices::HardwareDevice> dev : devices_) {
149 dev_info = get_device_info(dev);
151 // If present, vendor and model always have to match.
152 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
153 if (dev_info.at("vendor") != search_info.at("vendor")) continue;
155 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
156 if (dev_info.at("model") != search_info.at("model")) continue;
158 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
159 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
160 && search_info.count("serial_num") > 0)
161 if (dev_info.at("serial_num") == search_info.at("serial_num") &&
162 dev_info.at("serial_num") != "0")
165 // Second best match: vendor/model/connection_id
166 if (dev_info.count("connection_id") > 0 &&
167 search_info.count("connection_id") > 0)
168 if (dev_info.at("connection_id") == search_info.at("connection_id"))
171 // Last resort: vendor/model/version
172 if (dev_info.count("version") > 0 &&
173 search_info.count("version") > 0)
174 if (dev_info.at("version") == search_info.at("version") &&
175 dev_info.at("version") != "0")
178 // For this device, we merely have a vendor/model match.
179 last_resort_dev = dev;
182 // If there wasn't even a vendor/model/version match, we end up here.
183 // This is usually the case for devices with only vendor/model data.
184 // The selected device may be wrong with multiple such devices attached
185 // but it is the best we can do at this point. After all, there may be
186 // only one such device and we do want to select it in this case.
187 return last_resort_dev;
190 void DeviceManager::build_display_name(shared_ptr<devices::Device> device)
192 const shared_ptr<sigrok::Device> sr_dev = device->device();
193 auto session_device = dynamic_pointer_cast<sigrok::SessionDevice>(sr_dev);
194 auto hardware_device = dynamic_pointer_cast<sigrok::HardwareDevice>(sr_dev);
196 if (session_device) {
197 full_names_[device] = display_names_[device] =
198 boost::filesystem::path(
199 session_device->parent()->filename()).filename().string();
203 // First, build the device's full name. It always contains all
204 // possible information.
205 vector<string> parts = {sr_dev->vendor(), sr_dev->model(),
206 sr_dev->version(), sr_dev->serial_number()};
208 if (sr_dev->connection_id().length() > 0)
209 parts.push_back("("+sr_dev->connection_id()+")");
211 full_names_[device] = join(parts, " ");
213 // Next, build the display name. It only contains fields as required.
215 // If we can find another device with the same model/vendor then
216 // we have at least two such devices and need to distinguish them.
217 const bool multiple_dev = hardware_device && any_of(
218 devices_.begin(), devices_.end(),
219 [&](shared_ptr<devices::HardwareDevice> dev) {
220 return (dev->device()->vendor() == hardware_device->vendor() &&
221 dev->device()->model() == hardware_device->model()) &&
225 parts = {sr_dev->vendor(), sr_dev->model()};
228 parts.push_back(sr_dev->version());
229 parts.push_back(sr_dev->serial_number());
231 if ((sr_dev->serial_number().length() == 0) &&
232 (sr_dev->connection_id().length() > 0))
233 parts.push_back("("+sr_dev->connection_id()+")");
236 display_names_[device] = join(parts, " ");
239 const std::string DeviceManager::get_display_name(
240 std::shared_ptr<devices::Device> dev)
242 return display_names_[dev];
245 const std::string DeviceManager::get_full_name(
246 std::shared_ptr<devices::Device> dev)
248 return full_names_[dev];
251 void DeviceManager::update_display_name(
252 std::shared_ptr<devices::Device> dev)
254 build_display_name(dev);
257 bool DeviceManager::compare_devices(
258 shared_ptr<devices::Device> a, shared_ptr<devices::Device> b)
263 return display_names_[a].compare(display_names_[b]) < 0;