]> sigrok.org Git - pulseview.git/blame - pv/devicemanager.cpp
DeviceManager: Introduce full device names
[pulseview.git] / pv / devicemanager.cpp
CommitLineData
107ca6d3
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
2acdb232 21#include "devicemanager.hpp"
f65cd27b 22#include "session.hpp"
107ca6d3
JH
23
24#include <cassert>
107ca6d3 25#include <stdexcept>
e8d00928 26#include <sstream>
107ca6d3 27#include <string>
e8d00928 28#include <vector>
107ca6d3 29
e8d00928 30#include <libsigrok/libsigrok.hpp>
107ca6d3 31
e8d00928
ML
32#include <boost/filesystem.hpp>
33
34using std::dynamic_pointer_cast;
819f4c25
JH
35using std::list;
36using std::map;
e8d00928
ML
37using std::ostringstream;
38using std::remove_if;
819f4c25 39using std::runtime_error;
f9abf97e 40using std::shared_ptr;
819f4c25 41using std::string;
e8d00928
ML
42using std::vector;
43
44using Glib::VariantBase;
45
46using sigrok::ConfigKey;
47using sigrok::Context;
48using sigrok::Driver;
49using sigrok::Device;
50using sigrok::HardwareDevice;
51using sigrok::SessionDevice;
107ca6d3
JH
52
53namespace pv {
54
e8d00928 55DeviceManager::DeviceManager(shared_ptr<Context> context) :
8dbbc7f0 56 context_(context)
107ca6d3 57{
e8d00928
ML
58 for (auto entry : context->drivers())
59 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
107ca6d3
JH
60}
61
62DeviceManager::~DeviceManager()
63{
107ca6d3
JH
64}
65
e8d00928
ML
66shared_ptr<Context> DeviceManager::context()
67{
8dbbc7f0 68 return context_;
e8d00928
ML
69}
70
71const list< shared_ptr<HardwareDevice> >& DeviceManager::devices() const
107ca6d3 72{
8dbbc7f0 73 return devices_;
107ca6d3
JH
74}
75
e8d00928
ML
76list< shared_ptr<HardwareDevice> > DeviceManager::driver_scan(
77 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
107ca6d3 78{
e8d00928 79 list< shared_ptr<HardwareDevice> > driver_devices;
107ca6d3
JH
80
81 assert(driver);
82
83 // Remove any device instances from this driver from the device
84 // list. They will not be valid after the scan.
8dbbc7f0 85 devices_.remove_if([&](shared_ptr<HardwareDevice> device) {
cbe9f991 86 return device->driver() == driver; });
107ca6d3
JH
87
88 // Do the scan
e8d00928
ML
89 auto devices = driver->scan(drvopts);
90 driver_devices.insert(driver_devices.end(), devices.begin(), devices.end());
107ca6d3 91
a4cf020a 92 // Add the scanned devices to the main list, set display names and sort.
8dbbc7f0 93 devices_.insert(devices_.end(), driver_devices.begin(),
107ca6d3 94 driver_devices.end());
a4cf020a 95
8dbbc7f0 96 for (shared_ptr<Device> device : devices_)
602bff3a 97 build_display_name(device);
a4cf020a 98
8dbbc7f0 99 devices_.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
a4cf020a
SA
100 { return compare_devices(a, b); });
101
8dbbc7f0 102 // As the display names depend on the complete devices_ list,
a4cf020a 103 // we need to recompute them. However, there is no need to
8dbbc7f0 104 // recomute all names of the devices_ list since only the
a4cf020a
SA
105 // devices that use the given driver can be affected.
106 for (shared_ptr<Device> device : driver_devices)
602bff3a 107 build_display_name(device);
a4cf020a
SA
108
109 driver_devices.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
110 { return compare_devices(a, b); });
107ca6d3
JH
111
112 return driver_devices;
113}
114
e8d00928
ML
115const map<string, string> DeviceManager::get_device_info(
116 shared_ptr<Device> device)
117{
118 map<string, string> result;
119
120 assert(device);
121
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();
132
133 return result;
134}
135
136const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info(
6842b5fc
SA
137 const map<string, string> search_info)
138{
e8d00928 139 shared_ptr<HardwareDevice> last_resort_dev;
6842b5fc
SA
140 map<string, string> dev_info;
141
142 last_resort_dev = NULL;
143
8dbbc7f0 144 for (shared_ptr<HardwareDevice> dev : devices_) {
6842b5fc 145 assert(dev);
e8d00928 146 dev_info = get_device_info(dev);
6842b5fc
SA
147
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;
151
152 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
153 if (dev_info.at("model") != search_info.at("model")) continue;
154
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")
160 return dev;
161
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"))
166 return dev;
167
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")
173 return dev;
174
175 // For this device, we merely have a vendor/model match.
176 last_resort_dev = dev;
177 }
178
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;
185}
186
602bff3a 187void DeviceManager::build_display_name(shared_ptr<Device> device)
107ca6d3 188{
e8d00928 189 auto session_device = dynamic_pointer_cast<SessionDevice>(device);
a4cf020a 190 auto hardware_device = dynamic_pointer_cast<HardwareDevice>(device);
107ca6d3 191
602bff3a
SA
192 if (session_device) {
193 full_names_[device] = display_names_[device] =
194 boost::filesystem::path(
e8d00928 195 session_device->parent()->filename()).filename().string();
602bff3a
SA
196 return;
197 }
dc0867ff 198
e8d00928 199 ostringstream s;
107ca6d3 200
602bff3a
SA
201 // First, build the device's full name. It always contains all
202 // possible information.
203 vector<string> parts = {device->vendor(), device->model(),
204 device->version(), device->serial_number()};
205
206 if (device->connection_id().length() > 0)
207 parts.push_back("("+device->connection_id()+")");
208
209 for (size_t i = 0; i < parts.size(); i++)
210 {
211 if (parts[i].length() > 0)
212 {
213 if (i != 0)
214 s << " ";
215 s << parts[i];
216 }
217 }
218
219 full_names_[device] = s.str();
220
221 // Next, build the display name. It only contains fields as required.
a4cf020a
SA
222 bool multiple_dev = false;
223
224 // If we can find another device with the same model/vendor then
225 // we have at least two such devices and need to distinguish them.
226 if (hardware_device)
8dbbc7f0 227 multiple_dev = any_of(devices_.begin(), devices_.end(),
a4cf020a
SA
228 [&](shared_ptr<HardwareDevice> dev) {
229 return (dev->vendor() == hardware_device->vendor() &&
230 dev->model() == hardware_device->model()) &&
231 dev != hardware_device;
232 } );
233
602bff3a
SA
234 s.str("");
235 parts.clear();
236 parts = {device->vendor(), device->model()};
a4cf020a
SA
237
238 if (multiple_dev) {
239 parts.push_back(device->version());
240 parts.push_back(device->serial_number());
241
242 if ((device->serial_number().length() == 0) &&
243 (device->connection_id().length() > 0))
244 parts.push_back("("+device->connection_id()+")");
245 }
107ca6d3 246
e8d00928
ML
247 for (size_t i = 0; i < parts.size(); i++)
248 {
249 if (parts[i].length() > 0)
250 {
251 if (i != 0)
252 s << " ";
253 s << parts[i];
254 }
996b7c9d 255 }
dc0867ff 256
602bff3a 257 display_names_[device] = s.str();
dc0867ff
JH
258}
259
a4cf020a
SA
260const std::string DeviceManager::get_display_name(std::shared_ptr<sigrok::Device> dev)
261{
8dbbc7f0 262 return display_names_[dev];
a4cf020a
SA
263}
264
602bff3a
SA
265const std::string DeviceManager::get_full_name(std::shared_ptr<sigrok::Device> dev)
266{
267 return full_names_[dev];
268}
269
a4cf020a
SA
270void DeviceManager::update_display_name(std::shared_ptr<sigrok::Device> dev)
271{
602bff3a 272 build_display_name(dev);
a4cf020a
SA
273}
274
275bool DeviceManager::compare_devices(shared_ptr<Device> a,
276 shared_ptr<Device> b)
107ca6d3 277{
19adbc2c
JH
278 assert(a);
279 assert(b);
e8d00928 280
8dbbc7f0 281 return display_names_[a].compare(display_names_[b]) < 0;
107ca6d3
JH
282}
283
284} // namespace pv