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