]>
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 JH |
38 | #include <pv/devices/hardwaredevice.hpp> |
39 | ||
3084ed4b | 40 | using std::bind; |
819f4c25 JH |
41 | using std::list; |
42 | using std::map; | |
e71eb81c JH |
43 | using std::placeholders::_1; |
44 | using std::placeholders::_2; | |
f9abf97e | 45 | using std::shared_ptr; |
819f4c25 | 46 | using std::string; |
bc9177b3 | 47 | using std::unique_ptr; |
e8d00928 ML |
48 | |
49 | using Glib::VariantBase; | |
50 | ||
51 | using sigrok::ConfigKey; | |
52 | using sigrok::Context; | |
53 | using sigrok::Driver; | |
107ca6d3 JH |
54 | |
55 | namespace pv { | |
56 | ||
e8d00928 | 57 | DeviceManager::DeviceManager(shared_ptr<Context> context) : |
8dbbc7f0 | 58 | context_(context) |
107ca6d3 | 59 | { |
bc9177b3 SA |
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 | ||
e8d00928 | 71 | driver_scan(entry.second, map<const ConfigKey *, VariantBase>()); |
bc9177b3 SA |
72 | |
73 | progress->setValue(entry_num++); | |
74 | QApplication::processEvents(); | |
75 | if (progress->wasCanceled()) | |
76 | break; | |
77 | } | |
107ca6d3 JH |
78 | } |
79 | ||
6f925ba9 | 80 | const shared_ptr<sigrok::Context>& DeviceManager::context() const |
72d85f36 JH |
81 | { |
82 | return context_; | |
83 | } | |
84 | ||
e8d00928 ML |
85 | shared_ptr<Context> DeviceManager::context() |
86 | { | |
8dbbc7f0 | 87 | return context_; |
e8d00928 ML |
88 | } |
89 | ||
da30ecb7 JH |
90 | const list< shared_ptr<devices::HardwareDevice> >& |
91 | DeviceManager::devices() const | |
107ca6d3 | 92 | { |
8dbbc7f0 | 93 | return devices_; |
107ca6d3 JH |
94 | } |
95 | ||
da30ecb7 JH |
96 | list< shared_ptr<devices::HardwareDevice> > |
97 | DeviceManager::driver_scan( | |
e8d00928 | 98 | shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts) |
107ca6d3 | 99 | { |
da30ecb7 | 100 | list< shared_ptr<devices::HardwareDevice> > driver_devices; |
107ca6d3 JH |
101 | |
102 | assert(driver); | |
103 | ||
104 | // Remove any device instances from this driver from the device | |
105 | // list. They will not be valid after the scan. | |
da30ecb7 JH |
106 | devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) { |
107 | return device->hardware_device()->driver() == driver; }); | |
107ca6d3 JH |
108 | |
109 | // Do the scan | |
e8d00928 | 110 | auto devices = driver->scan(drvopts); |
107ca6d3 | 111 | |
a4cf020a | 112 | // Add the scanned devices to the main list, set display names and sort. |
da30ecb7 JH |
113 | for (shared_ptr<sigrok::HardwareDevice> device : devices) { |
114 | const shared_ptr<devices::HardwareDevice> d( | |
115 | new devices::HardwareDevice(context_, device)); | |
116 | driver_devices.push_back(d); | |
117 | } | |
a4cf020a | 118 | |
da30ecb7 JH |
119 | devices_.insert(devices_.end(), driver_devices.begin(), |
120 | driver_devices.end()); | |
3084ed4b JH |
121 | devices_.sort(bind(&DeviceManager::compare_devices, this, _1, _2)); |
122 | driver_devices.sort(bind( | |
123 | &DeviceManager::compare_devices, this, _1, _2)); | |
107ca6d3 JH |
124 | |
125 | return driver_devices; | |
126 | } | |
127 | ||
e8d00928 | 128 | const map<string, string> DeviceManager::get_device_info( |
da30ecb7 | 129 | shared_ptr<devices::Device> device) |
e8d00928 ML |
130 | { |
131 | map<string, string> result; | |
132 | ||
133 | assert(device); | |
134 | ||
da30ecb7 JH |
135 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
136 | if (sr_dev->vendor().length() > 0) | |
137 | result["vendor"] = sr_dev->vendor(); | |
138 | if (sr_dev->model().length() > 0) | |
139 | result["model"] = sr_dev->model(); | |
140 | if (sr_dev->version().length() > 0) | |
141 | result["version"] = sr_dev->version(); | |
142 | if (sr_dev->serial_number().length() > 0) | |
143 | result["serial_num"] = sr_dev->serial_number(); | |
144 | if (sr_dev->connection_id().length() > 0) | |
145 | result["connection_id"] = sr_dev->connection_id(); | |
e8d00928 ML |
146 | |
147 | return result; | |
148 | } | |
149 | ||
da30ecb7 | 150 | const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info( |
6842b5fc SA |
151 | const map<string, string> search_info) |
152 | { | |
da30ecb7 | 153 | shared_ptr<devices::HardwareDevice> last_resort_dev; |
6842b5fc SA |
154 | map<string, string> dev_info; |
155 | ||
da30ecb7 | 156 | for (shared_ptr<devices::HardwareDevice> dev : devices_) { |
6842b5fc | 157 | assert(dev); |
e8d00928 | 158 | dev_info = get_device_info(dev); |
6842b5fc SA |
159 | |
160 | // If present, vendor and model always have to match. | |
161 | if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0) | |
2ad82c2e UH |
162 | if (dev_info.at("vendor") != search_info.at("vendor")) |
163 | continue; | |
6842b5fc SA |
164 | |
165 | if (dev_info.count("model") > 0 && search_info.count("model") > 0) | |
2ad82c2e UH |
166 | if (dev_info.at("model") != search_info.at("model")) |
167 | continue; | |
6842b5fc SA |
168 | |
169 | // Most unique match: vendor/model/serial_num (but don't match a S/N of 0) | |
170 | if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0") | |
171 | && search_info.count("serial_num") > 0) | |
172 | if (dev_info.at("serial_num") == search_info.at("serial_num") && | |
173 | dev_info.at("serial_num") != "0") | |
174 | return dev; | |
175 | ||
176 | // Second best match: vendor/model/connection_id | |
177 | if (dev_info.count("connection_id") > 0 && | |
178 | search_info.count("connection_id") > 0) | |
179 | if (dev_info.at("connection_id") == search_info.at("connection_id")) | |
180 | return dev; | |
181 | ||
182 | // Last resort: vendor/model/version | |
183 | if (dev_info.count("version") > 0 && | |
184 | search_info.count("version") > 0) | |
185 | if (dev_info.at("version") == search_info.at("version") && | |
186 | dev_info.at("version") != "0") | |
187 | return dev; | |
188 | ||
189 | // For this device, we merely have a vendor/model match. | |
190 | last_resort_dev = dev; | |
191 | } | |
192 | ||
193 | // If there wasn't even a vendor/model/version match, we end up here. | |
194 | // This is usually the case for devices with only vendor/model data. | |
195 | // The selected device may be wrong with multiple such devices attached | |
196 | // but it is the best we can do at this point. After all, there may be | |
197 | // only one such device and we do want to select it in this case. | |
198 | return last_resort_dev; | |
199 | } | |
200 | ||
3084ed4b | 201 | bool DeviceManager::compare_devices(shared_ptr<devices::Device> a, |
2ad82c2e UH |
202 | shared_ptr<devices::Device> b) |
203 | { | |
19adbc2c JH |
204 | assert(a); |
205 | assert(b); | |
3084ed4b | 206 | return a->display_name(*this).compare(b->display_name(*this)) < 0; |
107ca6d3 JH |
207 | } |
208 | ||
209 | } // namespace pv |