]> sigrok.org Git - pulseview.git/blame_incremental - pv/devicemanager.cpp
Move trace view files
[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
40using std::bind;
41using std::list;
42using std::map;
43using std::placeholders::_1;
44using std::placeholders::_2;
45using std::shared_ptr;
46using std::string;
47using std::unique_ptr;
48
49using Glib::VariantBase;
50
51using sigrok::ConfigKey;
52using sigrok::Context;
53using sigrok::Driver;
54
55namespace pv {
56
57DeviceManager::DeviceManager(shared_ptr<Context> context) :
58 context_(context)
59{
60 unique_ptr<QProgressDialog> progress(new QProgressDialog("",
61 QObject::tr("Cancel"), 0, context->drivers().size()));
62 progress->setWindowModality(Qt::WindowModal);
63 progress->setMinimumDuration(1); // To show the dialog immediately
64
65 int entry_num = 1;
66
67 for (auto entry : context->drivers()) {
68 progress->setLabelText(QObject::tr("Scanning for %1...")
69 .arg(QString::fromStdString(entry.first)));
70
71 /**
72 * We currently only support devices that can deliver
73 * samples at a fixed samplerate i.e. oscilloscopes and
74 * logic analysers.
75 * @todo Add support for non-monotonic devices i.e. DMMs
76 * and sensors.
77 */
78 const auto keys = (entry.second)->config_keys();
79
80 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
81 keys.count(ConfigKey::OSCILLOSCOPE);
82
83 if (supported_device)
84 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
85
86 progress->setValue(entry_num++);
87 QApplication::processEvents();
88 if (progress->wasCanceled())
89 break;
90 }
91}
92
93const shared_ptr<sigrok::Context>& DeviceManager::context() const
94{
95 return context_;
96}
97
98shared_ptr<Context> DeviceManager::context()
99{
100 return context_;
101}
102
103const list< shared_ptr<devices::HardwareDevice> >&
104DeviceManager::devices() const
105{
106 return devices_;
107}
108
109list< shared_ptr<devices::HardwareDevice> >
110DeviceManager::driver_scan(
111 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
112{
113 list< shared_ptr<devices::HardwareDevice> > driver_devices;
114
115 assert(driver);
116
117 // Remove any device instances from this driver from the device
118 // list. They will not be valid after the scan.
119 devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) {
120 return device->hardware_device()->driver() == driver; });
121
122 // Do the scan
123 auto devices = driver->scan(drvopts);
124
125 // Add the scanned devices to the main list, set display names and sort.
126 for (shared_ptr<sigrok::HardwareDevice> device : devices) {
127 const shared_ptr<devices::HardwareDevice> d(
128 new devices::HardwareDevice(context_, device));
129 driver_devices.push_back(d);
130 }
131
132 devices_.insert(devices_.end(), driver_devices.begin(),
133 driver_devices.end());
134 devices_.sort(bind(&DeviceManager::compare_devices, this, _1, _2));
135 driver_devices.sort(bind(
136 &DeviceManager::compare_devices, this, _1, _2));
137
138 return driver_devices;
139}
140
141const map<string, string> DeviceManager::get_device_info(
142 shared_ptr<devices::Device> device)
143{
144 map<string, string> result;
145
146 assert(device);
147
148 const shared_ptr<sigrok::Device> sr_dev = device->device();
149 if (sr_dev->vendor().length() > 0)
150 result["vendor"] = sr_dev->vendor();
151 if (sr_dev->model().length() > 0)
152 result["model"] = sr_dev->model();
153 if (sr_dev->version().length() > 0)
154 result["version"] = sr_dev->version();
155 if (sr_dev->serial_number().length() > 0)
156 result["serial_num"] = sr_dev->serial_number();
157 if (sr_dev->connection_id().length() > 0)
158 result["connection_id"] = sr_dev->connection_id();
159
160 return result;
161}
162
163const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info(
164 const map<string, string> search_info)
165{
166 shared_ptr<devices::HardwareDevice> last_resort_dev;
167 map<string, string> dev_info;
168
169 for (shared_ptr<devices::HardwareDevice> dev : devices_) {
170 assert(dev);
171 dev_info = get_device_info(dev);
172
173 // If present, vendor and model always have to match.
174 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
175 if (dev_info.at("vendor") != search_info.at("vendor"))
176 continue;
177
178 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
179 if (dev_info.at("model") != search_info.at("model"))
180 continue;
181
182 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
183 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
184 && search_info.count("serial_num") > 0)
185 if (dev_info.at("serial_num") == search_info.at("serial_num") &&
186 dev_info.at("serial_num") != "0")
187 return dev;
188
189 // Second best match: vendor/model/connection_id
190 if (dev_info.count("connection_id") > 0 &&
191 search_info.count("connection_id") > 0)
192 if (dev_info.at("connection_id") == search_info.at("connection_id"))
193 return dev;
194
195 // Last resort: vendor/model/version
196 if (dev_info.count("version") > 0 &&
197 search_info.count("version") > 0)
198 if (dev_info.at("version") == search_info.at("version") &&
199 dev_info.at("version") != "0")
200 return dev;
201
202 // For this device, we merely have a vendor/model match.
203 last_resort_dev = dev;
204 }
205
206 // If there wasn't even a vendor/model/version match, we end up here.
207 // This is usually the case for devices with only vendor/model data.
208 // The selected device may be wrong with multiple such devices attached
209 // but it is the best we can do at this point. After all, there may be
210 // only one such device and we do want to select it in this case.
211 return last_resort_dev;
212}
213
214bool DeviceManager::compare_devices(shared_ptr<devices::Device> a,
215 shared_ptr<devices::Device> b)
216{
217 assert(a);
218 assert(b);
219 return a->display_name(*this).compare(b->display_name(*this)) < 0;
220}
221
222} // namespace pv