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