]>
Commit | Line | Data |
---|---|---|
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
107ca6d3 JH |
18 | */ |
19 | ||
2acdb232 | 20 | #include "devicemanager.hpp" |
f65cd27b | 21 | #include "session.hpp" |
107ca6d3 JH |
22 | |
23 | #include <cassert> | |
3084ed4b | 24 | #include <functional> |
bc9177b3 | 25 | #include <memory> |
e8d00928 | 26 | #include <sstream> |
aca9aa83 | 27 | #include <stdexcept> |
107ca6d3 JH |
28 | #include <string> |
29 | ||
fe3a1c21 | 30 | #include <libsigrokcxx/libsigrokcxx.hpp> |
107ca6d3 | 31 | |
bc9177b3 SA |
32 | #include <QApplication> |
33 | #include <QObject> | |
34 | #include <QProgressDialog> | |
35 | ||
e8d00928 ML |
36 | #include <boost/filesystem.hpp> |
37 | ||
da30ecb7 | 38 | #include <pv/devices/hardwaredevice.hpp> |
58d8e4c6 | 39 | #include <pv/util.hpp> |
da30ecb7 | 40 | |
3084ed4b | 41 | using std::bind; |
819f4c25 JH |
42 | using std::list; |
43 | using std::map; | |
e71eb81c JH |
44 | using std::placeholders::_1; |
45 | using std::placeholders::_2; | |
f9abf97e | 46 | using std::shared_ptr; |
819f4c25 | 47 | using std::string; |
bc9177b3 | 48 | using std::unique_ptr; |
58d8e4c6 | 49 | using std::vector; |
e8d00928 ML |
50 | |
51 | using Glib::VariantBase; | |
52 | ||
53 | using sigrok::ConfigKey; | |
54 | using sigrok::Context; | |
55 | using sigrok::Driver; | |
107ca6d3 JH |
56 | |
57 | namespace pv { | |
58 | ||
1d6e35dc GS |
59 | DeviceManager::DeviceManager(shared_ptr<Context> context, |
60 | std::string driver, bool do_scan) : | |
8dbbc7f0 | 61 | context_(context) |
107ca6d3 | 62 | { |
bc9177b3 | 63 | unique_ptr<QProgressDialog> progress(new QProgressDialog("", |
58d8e4c6 | 64 | QObject::tr("Cancel"), 0, context->drivers().size() + 1)); |
bc9177b3 SA |
65 | progress->setWindowModality(Qt::WindowModal); |
66 | progress->setMinimumDuration(1); // To show the dialog immediately | |
67 | ||
68 | int entry_num = 1; | |
69 | ||
58d8e4c6 GS |
70 | /* |
71 | * Check the presence of an optional user spec for device scans. | |
72 | * Determine the driver name and options (in generic format) when | |
73 | * applicable. | |
74 | */ | |
75 | std::string user_name; | |
76 | vector<std::string> user_opts; | |
77 | if (!driver.empty()) { | |
78 | user_opts = pv::util::split_string(driver, ":"); | |
79 | user_name = user_opts.front(); | |
80 | user_opts.erase(user_opts.begin()); | |
81 | } | |
82 | ||
83 | /* | |
84 | * Scan for devices. No specific options apply here, this is | |
85 | * best effort auto detection. | |
86 | */ | |
bc9177b3 | 87 | for (auto entry : context->drivers()) { |
1d6e35dc GS |
88 | if (!do_scan) |
89 | break; | |
8d054b91 SA |
90 | |
91 | // Skip drivers we won't scan anyway | |
92 | if (!driver_supported(entry.second)) | |
93 | continue; | |
94 | ||
bc9177b3 SA |
95 | progress->setLabelText(QObject::tr("Scanning for %1...") |
96 | .arg(QString::fromStdString(entry.first))); | |
97 | ||
58d8e4c6 GS |
98 | if (entry.first == user_name) |
99 | continue; | |
941e22a6 | 100 | driver_scan(entry.second, map<const ConfigKey *, VariantBase>()); |
bc9177b3 SA |
101 | |
102 | progress->setValue(entry_num++); | |
103 | QApplication::processEvents(); | |
104 | if (progress->wasCanceled()) | |
105 | break; | |
106 | } | |
58d8e4c6 GS |
107 | |
108 | /* | |
109 | * Optionally run another scan with potentially more specific | |
110 | * options when requested by the user. This is motivated by | |
111 | * several different uses: It can find devices that are not | |
112 | * covered by the above auto detection (UART, TCP). It can | |
113 | * prefer one out of multiple found devices, and have this | |
114 | * device pre-selected for new sessions upon user's request. | |
115 | */ | |
116 | user_spec_device_.reset(); | |
117 | if (!driver.empty()) { | |
118 | shared_ptr<sigrok::Driver> scan_drv; | |
119 | map<const ConfigKey *, VariantBase> scan_opts; | |
120 | ||
121 | /* | |
122 | * Lookup the device driver name. | |
123 | */ | |
124 | map<string, shared_ptr<Driver>> drivers = context->drivers(); | |
125 | auto entry = drivers.find(user_name); | |
126 | scan_drv = (entry != drivers.end()) ? entry->second : nullptr; | |
127 | ||
128 | /* | |
129 | * Convert generic string representation of options | |
130 | * to the driver specific data types. | |
131 | */ | |
132 | if (scan_drv && !user_opts.empty()) { | |
133 | auto drv_opts = scan_drv->scan_options(); | |
134 | scan_opts = drive_scan_options(user_opts, drv_opts); | |
135 | } | |
136 | ||
137 | /* | |
138 | * Run another scan for the specified driver, passing | |
139 | * user provided scan options this time. | |
140 | */ | |
141 | list< shared_ptr<devices::HardwareDevice> > found; | |
142 | if (scan_drv) { | |
143 | found = driver_scan(scan_drv, scan_opts); | |
144 | if (!found.empty()) | |
145 | user_spec_device_ = found.front(); | |
146 | } | |
147 | } | |
148 | progress->setValue(entry_num++); | |
107ca6d3 JH |
149 | } |
150 | ||
6f925ba9 | 151 | const shared_ptr<sigrok::Context>& DeviceManager::context() const |
72d85f36 JH |
152 | { |
153 | return context_; | |
154 | } | |
155 | ||
e8d00928 ML |
156 | shared_ptr<Context> DeviceManager::context() |
157 | { | |
8dbbc7f0 | 158 | return context_; |
e8d00928 ML |
159 | } |
160 | ||
da30ecb7 JH |
161 | const list< shared_ptr<devices::HardwareDevice> >& |
162 | DeviceManager::devices() const | |
107ca6d3 | 163 | { |
8dbbc7f0 | 164 | return devices_; |
107ca6d3 JH |
165 | } |
166 | ||
58d8e4c6 GS |
167 | /** |
168 | * Get the device that was detected with user provided scan options. | |
169 | */ | |
170 | shared_ptr<devices::HardwareDevice> | |
171 | DeviceManager::user_spec_device() const | |
172 | { | |
173 | return user_spec_device_; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Convert generic options to data types that are specific to Driver::scan(). | |
178 | * | |
3083cda8 UH |
179 | * @param[in] user_spec Vector of tokenized words, string format. |
180 | * @param[in] driver_opts Driver's scan options, result of Driver::scan_options(). | |
58d8e4c6 | 181 | * |
3083cda8 | 182 | * @return Map of options suitable for Driver::scan(). |
58d8e4c6 GS |
183 | */ |
184 | map<const ConfigKey *, Glib::VariantBase> | |
185 | DeviceManager::drive_scan_options(vector<string> user_spec, | |
186 | set<const ConfigKey *> driver_opts) | |
187 | { | |
188 | map<const ConfigKey *, Glib::VariantBase> result; | |
189 | ||
190 | for (auto entry : user_spec) { | |
191 | /* | |
192 | * Split key=value specs. Accept entries without separator | |
193 | * (for simplified boolean specifications). | |
194 | */ | |
195 | string key, val; | |
196 | size_t pos = entry.find("="); | |
197 | if (pos == std::string::npos) { | |
198 | key = entry; | |
199 | val = ""; | |
200 | } else { | |
201 | key = entry.substr(0, pos); | |
202 | val = entry.substr(pos + 1); | |
203 | } | |
204 | ||
205 | /* | |
206 | * Skip user specifications that are not a member of the | |
207 | * driver's set of supported options. Have the text format | |
208 | * input spec converted to the required driver specific type. | |
209 | */ | |
210 | const ConfigKey *cfg; | |
211 | try { | |
212 | cfg = ConfigKey::get_by_identifier(key); | |
213 | if (!cfg) | |
214 | continue; | |
215 | if (driver_opts.find(cfg) == driver_opts.end()) | |
216 | continue; | |
217 | } catch (...) { | |
218 | continue; | |
219 | } | |
220 | result[cfg] = cfg->parse_string(val); | |
221 | } | |
222 | ||
223 | return result; | |
224 | } | |
225 | ||
8d054b91 | 226 | bool DeviceManager::driver_supported(shared_ptr<Driver> driver) const |
107ca6d3 | 227 | { |
941e22a6 GS |
228 | /* |
229 | * We currently only support devices that can deliver samples at | |
230 | * a fixed samplerate (i.e. oscilloscopes and logic analysers). | |
231 | * | |
232 | * @todo Add support for non-monotonic devices (DMMs, sensors, etc). | |
233 | */ | |
234 | const auto keys = driver->config_keys(); | |
8d054b91 SA |
235 | |
236 | return keys.count(ConfigKey::LOGIC_ANALYZER) | keys.count(ConfigKey::OSCILLOSCOPE); | |
237 | } | |
238 | ||
239 | list< shared_ptr<devices::HardwareDevice> > | |
240 | DeviceManager::driver_scan( | |
241 | shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts) | |
242 | { | |
243 | list< shared_ptr<devices::HardwareDevice> > driver_devices; | |
244 | ||
245 | assert(driver); | |
246 | ||
247 | if (!driver_supported(driver)) | |
941e22a6 GS |
248 | return driver_devices; |
249 | ||
107ca6d3 JH |
250 | // Remove any device instances from this driver from the device |
251 | // list. They will not be valid after the scan. | |
da30ecb7 JH |
252 | devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) { |
253 | return device->hardware_device()->driver() == driver; }); | |
107ca6d3 JH |
254 | |
255 | // Do the scan | |
e8d00928 | 256 | auto devices = driver->scan(drvopts); |
107ca6d3 | 257 | |
a4cf020a | 258 | // Add the scanned devices to the main list, set display names and sort. |
da30ecb7 JH |
259 | for (shared_ptr<sigrok::HardwareDevice> device : devices) { |
260 | const shared_ptr<devices::HardwareDevice> d( | |
261 | new devices::HardwareDevice(context_, device)); | |
262 | driver_devices.push_back(d); | |
263 | } | |
a4cf020a | 264 | |
da30ecb7 JH |
265 | devices_.insert(devices_.end(), driver_devices.begin(), |
266 | driver_devices.end()); | |
3084ed4b JH |
267 | devices_.sort(bind(&DeviceManager::compare_devices, this, _1, _2)); |
268 | driver_devices.sort(bind( | |
269 | &DeviceManager::compare_devices, this, _1, _2)); | |
107ca6d3 JH |
270 | |
271 | return driver_devices; | |
272 | } | |
273 | ||
e8d00928 | 274 | const map<string, string> DeviceManager::get_device_info( |
da30ecb7 | 275 | shared_ptr<devices::Device> device) |
e8d00928 ML |
276 | { |
277 | map<string, string> result; | |
278 | ||
279 | assert(device); | |
280 | ||
da30ecb7 JH |
281 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
282 | if (sr_dev->vendor().length() > 0) | |
283 | result["vendor"] = sr_dev->vendor(); | |
284 | if (sr_dev->model().length() > 0) | |
285 | result["model"] = sr_dev->model(); | |
286 | if (sr_dev->version().length() > 0) | |
287 | result["version"] = sr_dev->version(); | |
288 | if (sr_dev->serial_number().length() > 0) | |
289 | result["serial_num"] = sr_dev->serial_number(); | |
290 | if (sr_dev->connection_id().length() > 0) | |
291 | result["connection_id"] = sr_dev->connection_id(); | |
e8d00928 ML |
292 | |
293 | return result; | |
294 | } | |
295 | ||
da30ecb7 | 296 | const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info( |
6842b5fc SA |
297 | const map<string, string> search_info) |
298 | { | |
da30ecb7 | 299 | shared_ptr<devices::HardwareDevice> last_resort_dev; |
6842b5fc SA |
300 | map<string, string> dev_info; |
301 | ||
da30ecb7 | 302 | for (shared_ptr<devices::HardwareDevice> dev : devices_) { |
6842b5fc | 303 | assert(dev); |
e8d00928 | 304 | dev_info = get_device_info(dev); |
6842b5fc SA |
305 | |
306 | // If present, vendor and model always have to match. | |
307 | if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0) | |
2ad82c2e UH |
308 | if (dev_info.at("vendor") != search_info.at("vendor")) |
309 | continue; | |
6842b5fc SA |
310 | |
311 | if (dev_info.count("model") > 0 && search_info.count("model") > 0) | |
2ad82c2e UH |
312 | if (dev_info.at("model") != search_info.at("model")) |
313 | continue; | |
6842b5fc SA |
314 | |
315 | // Most unique match: vendor/model/serial_num (but don't match a S/N of 0) | |
316 | if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0") | |
317 | && search_info.count("serial_num") > 0) | |
318 | if (dev_info.at("serial_num") == search_info.at("serial_num") && | |
319 | dev_info.at("serial_num") != "0") | |
320 | return dev; | |
321 | ||
322 | // Second best match: vendor/model/connection_id | |
323 | if (dev_info.count("connection_id") > 0 && | |
324 | search_info.count("connection_id") > 0) | |
325 | if (dev_info.at("connection_id") == search_info.at("connection_id")) | |
326 | return dev; | |
327 | ||
328 | // Last resort: vendor/model/version | |
329 | if (dev_info.count("version") > 0 && | |
330 | search_info.count("version") > 0) | |
331 | if (dev_info.at("version") == search_info.at("version") && | |
332 | dev_info.at("version") != "0") | |
333 | return dev; | |
334 | ||
335 | // For this device, we merely have a vendor/model match. | |
336 | last_resort_dev = dev; | |
337 | } | |
338 | ||
339 | // If there wasn't even a vendor/model/version match, we end up here. | |
340 | // This is usually the case for devices with only vendor/model data. | |
341 | // The selected device may be wrong with multiple such devices attached | |
342 | // but it is the best we can do at this point. After all, there may be | |
343 | // only one such device and we do want to select it in this case. | |
344 | return last_resort_dev; | |
345 | } | |
346 | ||
3084ed4b | 347 | bool DeviceManager::compare_devices(shared_ptr<devices::Device> a, |
2ad82c2e UH |
348 | shared_ptr<devices::Device> b) |
349 | { | |
19adbc2c JH |
350 | assert(a); |
351 | assert(b); | |
3084ed4b | 352 | return a->display_name(*this).compare(b->display_name(*this)) < 0; |
107ca6d3 JH |
353 | } |
354 | ||
355 | } // namespace pv |