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