]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
2acdb232 | 21 | #include "devicemanager.hpp" |
f65cd27b | 22 | #include "session.hpp" |
107ca6d3 JH |
23 | |
24 | #include <cassert> | |
107ca6d3 | 25 | #include <stdexcept> |
e8d00928 | 26 | #include <sstream> |
107ca6d3 | 27 | #include <string> |
e8d00928 | 28 | #include <vector> |
107ca6d3 | 29 | |
fe3a1c21 | 30 | #include <libsigrokcxx/libsigrokcxx.hpp> |
107ca6d3 | 31 | |
88fc0565 | 32 | #include <boost/algorithm/string/join.hpp> |
e8d00928 ML |
33 | #include <boost/filesystem.hpp> |
34 | ||
88fc0565 JH |
35 | using boost::algorithm::join; |
36 | ||
e8d00928 | 37 | using std::dynamic_pointer_cast; |
819f4c25 JH |
38 | using std::list; |
39 | using std::map; | |
e8d00928 | 40 | using std::remove_if; |
819f4c25 | 41 | using std::runtime_error; |
f9abf97e | 42 | using std::shared_ptr; |
819f4c25 | 43 | using std::string; |
e8d00928 ML |
44 | using std::vector; |
45 | ||
46 | using Glib::VariantBase; | |
47 | ||
48 | using sigrok::ConfigKey; | |
49 | using sigrok::Context; | |
50 | using sigrok::Driver; | |
51 | using sigrok::Device; | |
52 | using sigrok::HardwareDevice; | |
53 | using sigrok::SessionDevice; | |
107ca6d3 JH |
54 | |
55 | namespace pv { | |
56 | ||
e8d00928 | 57 | DeviceManager::DeviceManager(shared_ptr<Context> context) : |
8dbbc7f0 | 58 | context_(context) |
107ca6d3 | 59 | { |
e8d00928 ML |
60 | for (auto entry : context->drivers()) |
61 | driver_scan(entry.second, map<const ConfigKey *, VariantBase>()); | |
107ca6d3 JH |
62 | } |
63 | ||
64 | DeviceManager::~DeviceManager() | |
65 | { | |
107ca6d3 JH |
66 | } |
67 | ||
72d85f36 JH |
68 | const std::shared_ptr<sigrok::Context>& DeviceManager::context() const |
69 | { | |
70 | return context_; | |
71 | } | |
72 | ||
e8d00928 ML |
73 | shared_ptr<Context> DeviceManager::context() |
74 | { | |
8dbbc7f0 | 75 | return context_; |
e8d00928 ML |
76 | } |
77 | ||
78 | const list< shared_ptr<HardwareDevice> >& DeviceManager::devices() const | |
107ca6d3 | 79 | { |
8dbbc7f0 | 80 | return devices_; |
107ca6d3 JH |
81 | } |
82 | ||
e8d00928 ML |
83 | list< shared_ptr<HardwareDevice> > DeviceManager::driver_scan( |
84 | shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts) | |
107ca6d3 | 85 | { |
e8d00928 | 86 | list< shared_ptr<HardwareDevice> > driver_devices; |
107ca6d3 JH |
87 | |
88 | assert(driver); | |
89 | ||
90 | // Remove any device instances from this driver from the device | |
91 | // list. They will not be valid after the scan. | |
8dbbc7f0 | 92 | devices_.remove_if([&](shared_ptr<HardwareDevice> device) { |
cbe9f991 | 93 | return device->driver() == driver; }); |
107ca6d3 JH |
94 | |
95 | // Do the scan | |
e8d00928 ML |
96 | auto devices = driver->scan(drvopts); |
97 | driver_devices.insert(driver_devices.end(), devices.begin(), devices.end()); | |
107ca6d3 | 98 | |
a4cf020a | 99 | // Add the scanned devices to the main list, set display names and sort. |
8dbbc7f0 | 100 | devices_.insert(devices_.end(), driver_devices.begin(), |
107ca6d3 | 101 | driver_devices.end()); |
a4cf020a | 102 | |
8dbbc7f0 | 103 | for (shared_ptr<Device> device : devices_) |
602bff3a | 104 | build_display_name(device); |
a4cf020a | 105 | |
8dbbc7f0 | 106 | devices_.sort([&](shared_ptr<Device> a, shared_ptr<Device> b) |
a4cf020a SA |
107 | { return compare_devices(a, b); }); |
108 | ||
8dbbc7f0 | 109 | // As the display names depend on the complete devices_ list, |
a4cf020a | 110 | // we need to recompute them. However, there is no need to |
8dbbc7f0 | 111 | // recomute all names of the devices_ list since only the |
a4cf020a SA |
112 | // devices that use the given driver can be affected. |
113 | for (shared_ptr<Device> device : driver_devices) | |
602bff3a | 114 | build_display_name(device); |
a4cf020a SA |
115 | |
116 | driver_devices.sort([&](shared_ptr<Device> a, shared_ptr<Device> b) | |
117 | { return compare_devices(a, b); }); | |
107ca6d3 JH |
118 | |
119 | return driver_devices; | |
120 | } | |
121 | ||
e8d00928 ML |
122 | const map<string, string> DeviceManager::get_device_info( |
123 | shared_ptr<Device> device) | |
124 | { | |
125 | map<string, string> result; | |
126 | ||
127 | assert(device); | |
128 | ||
129 | if (device->vendor().length() > 0) | |
130 | result["vendor"] = device->vendor(); | |
131 | if (device->model().length() > 0) | |
132 | result["model"] = device->model(); | |
133 | if (device->version().length() > 0) | |
134 | result["version"] = device->version(); | |
135 | if (device->serial_number().length() > 0) | |
136 | result["serial_num"] = device->serial_number(); | |
137 | if (device->connection_id().length() > 0) | |
138 | result["connection_id"] = device->connection_id(); | |
139 | ||
140 | return result; | |
141 | } | |
142 | ||
143 | const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info( | |
6842b5fc SA |
144 | const map<string, string> search_info) |
145 | { | |
e8d00928 | 146 | shared_ptr<HardwareDevice> last_resort_dev; |
6842b5fc SA |
147 | map<string, string> dev_info; |
148 | ||
149 | last_resort_dev = NULL; | |
150 | ||
8dbbc7f0 | 151 | for (shared_ptr<HardwareDevice> dev : devices_) { |
6842b5fc | 152 | assert(dev); |
e8d00928 | 153 | dev_info = get_device_info(dev); |
6842b5fc SA |
154 | |
155 | // If present, vendor and model always have to match. | |
156 | if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0) | |
157 | if (dev_info.at("vendor") != search_info.at("vendor")) continue; | |
158 | ||
159 | if (dev_info.count("model") > 0 && search_info.count("model") > 0) | |
160 | if (dev_info.at("model") != search_info.at("model")) continue; | |
161 | ||
162 | // Most unique match: vendor/model/serial_num (but don't match a S/N of 0) | |
163 | if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0") | |
164 | && search_info.count("serial_num") > 0) | |
165 | if (dev_info.at("serial_num") == search_info.at("serial_num") && | |
166 | dev_info.at("serial_num") != "0") | |
167 | return dev; | |
168 | ||
169 | // Second best match: vendor/model/connection_id | |
170 | if (dev_info.count("connection_id") > 0 && | |
171 | search_info.count("connection_id") > 0) | |
172 | if (dev_info.at("connection_id") == search_info.at("connection_id")) | |
173 | return dev; | |
174 | ||
175 | // Last resort: vendor/model/version | |
176 | if (dev_info.count("version") > 0 && | |
177 | search_info.count("version") > 0) | |
178 | if (dev_info.at("version") == search_info.at("version") && | |
179 | dev_info.at("version") != "0") | |
180 | return dev; | |
181 | ||
182 | // For this device, we merely have a vendor/model match. | |
183 | last_resort_dev = dev; | |
184 | } | |
185 | ||
186 | // If there wasn't even a vendor/model/version match, we end up here. | |
187 | // This is usually the case for devices with only vendor/model data. | |
188 | // The selected device may be wrong with multiple such devices attached | |
189 | // but it is the best we can do at this point. After all, there may be | |
190 | // only one such device and we do want to select it in this case. | |
191 | return last_resort_dev; | |
192 | } | |
193 | ||
602bff3a | 194 | void DeviceManager::build_display_name(shared_ptr<Device> device) |
107ca6d3 | 195 | { |
e8d00928 | 196 | auto session_device = dynamic_pointer_cast<SessionDevice>(device); |
a4cf020a | 197 | auto hardware_device = dynamic_pointer_cast<HardwareDevice>(device); |
107ca6d3 | 198 | |
602bff3a SA |
199 | if (session_device) { |
200 | full_names_[device] = display_names_[device] = | |
201 | boost::filesystem::path( | |
e8d00928 | 202 | session_device->parent()->filename()).filename().string(); |
602bff3a SA |
203 | return; |
204 | } | |
dc0867ff | 205 | |
602bff3a SA |
206 | // First, build the device's full name. It always contains all |
207 | // possible information. | |
208 | vector<string> parts = {device->vendor(), device->model(), | |
209 | device->version(), device->serial_number()}; | |
210 | ||
211 | if (device->connection_id().length() > 0) | |
212 | parts.push_back("("+device->connection_id()+")"); | |
213 | ||
88fc0565 | 214 | full_names_[device] = join(parts, " "); |
602bff3a SA |
215 | |
216 | // Next, build the display name. It only contains fields as required. | |
a4cf020a SA |
217 | |
218 | // If we can find another device with the same model/vendor then | |
219 | // we have at least two such devices and need to distinguish them. | |
7ddb8da6 JH |
220 | const bool multiple_dev = hardware_device && any_of( |
221 | devices_.begin(), devices_.end(), | |
222 | [&](shared_ptr<HardwareDevice> dev) { | |
a4cf020a | 223 | return (dev->vendor() == hardware_device->vendor() && |
7ddb8da6 JH |
224 | dev->model() == hardware_device->model()) && |
225 | dev != hardware_device; | |
226 | } ); | |
a4cf020a | 227 | |
602bff3a | 228 | parts = {device->vendor(), device->model()}; |
a4cf020a SA |
229 | |
230 | if (multiple_dev) { | |
231 | parts.push_back(device->version()); | |
232 | parts.push_back(device->serial_number()); | |
233 | ||
234 | if ((device->serial_number().length() == 0) && | |
235 | (device->connection_id().length() > 0)) | |
236 | parts.push_back("("+device->connection_id()+")"); | |
237 | } | |
107ca6d3 | 238 | |
88fc0565 | 239 | display_names_[device] = join(parts, " "); |
dc0867ff JH |
240 | } |
241 | ||
a4cf020a SA |
242 | const std::string DeviceManager::get_display_name(std::shared_ptr<sigrok::Device> dev) |
243 | { | |
8dbbc7f0 | 244 | return display_names_[dev]; |
a4cf020a SA |
245 | } |
246 | ||
602bff3a SA |
247 | const std::string DeviceManager::get_full_name(std::shared_ptr<sigrok::Device> dev) |
248 | { | |
249 | return full_names_[dev]; | |
250 | } | |
251 | ||
a4cf020a SA |
252 | void DeviceManager::update_display_name(std::shared_ptr<sigrok::Device> dev) |
253 | { | |
602bff3a | 254 | build_display_name(dev); |
a4cf020a SA |
255 | } |
256 | ||
257 | bool DeviceManager::compare_devices(shared_ptr<Device> a, | |
258 | shared_ptr<Device> b) | |
107ca6d3 | 259 | { |
19adbc2c JH |
260 | assert(a); |
261 | assert(b); | |
e8d00928 | 262 | |
8dbbc7f0 | 263 | return display_names_[a].compare(display_names_[b]) < 0; |
107ca6d3 JH |
264 | } |
265 | ||
266 | } // namespace pv |