]>
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 | ||
21 | #include "devicemanager.h" | |
dc0867ff | 22 | #include "sigsession.h" |
107ca6d3 JH |
23 | |
24 | #include <cassert> | |
107ca6d3 | 25 | #include <stdexcept> |
e8d00928 | 26 | #include <sstream> |
107ca6d3 | 27 | #include <string> |
e8d00928 | 28 | #include <vector> |
107ca6d3 | 29 | |
e8d00928 | 30 | #include <libsigrok/libsigrok.hpp> |
107ca6d3 | 31 | |
e8d00928 ML |
32 | #include <boost/filesystem.hpp> |
33 | ||
34 | using std::dynamic_pointer_cast; | |
819f4c25 JH |
35 | using std::list; |
36 | using std::map; | |
e8d00928 ML |
37 | using std::ostringstream; |
38 | using std::remove_if; | |
819f4c25 | 39 | using std::runtime_error; |
f9abf97e | 40 | using std::shared_ptr; |
819f4c25 | 41 | using std::string; |
e8d00928 ML |
42 | using std::vector; |
43 | ||
44 | using Glib::VariantBase; | |
45 | ||
46 | using sigrok::ConfigKey; | |
47 | using sigrok::Context; | |
48 | using sigrok::Driver; | |
49 | using sigrok::Device; | |
50 | using sigrok::HardwareDevice; | |
51 | using sigrok::SessionDevice; | |
107ca6d3 JH |
52 | |
53 | namespace pv { | |
54 | ||
e8d00928 ML |
55 | DeviceManager::DeviceManager(shared_ptr<Context> context) : |
56 | _context(context) | |
107ca6d3 | 57 | { |
e8d00928 ML |
58 | for (auto entry : context->drivers()) |
59 | driver_scan(entry.second, map<const ConfigKey *, VariantBase>()); | |
107ca6d3 JH |
60 | } |
61 | ||
62 | DeviceManager::~DeviceManager() | |
63 | { | |
107ca6d3 JH |
64 | } |
65 | ||
e8d00928 ML |
66 | shared_ptr<Context> DeviceManager::context() |
67 | { | |
68 | return _context; | |
69 | } | |
70 | ||
71 | const list< shared_ptr<HardwareDevice> >& DeviceManager::devices() const | |
107ca6d3 JH |
72 | { |
73 | return _devices; | |
74 | } | |
75 | ||
e8d00928 ML |
76 | list< shared_ptr<HardwareDevice> > DeviceManager::driver_scan( |
77 | shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts) | |
107ca6d3 | 78 | { |
e8d00928 | 79 | list< shared_ptr<HardwareDevice> > driver_devices; |
107ca6d3 JH |
80 | |
81 | assert(driver); | |
82 | ||
83 | // Remove any device instances from this driver from the device | |
84 | // list. They will not be valid after the scan. | |
cbe9f991 SA |
85 | _devices.remove_if([&](shared_ptr<HardwareDevice> device) { |
86 | return device->driver() == driver; }); | |
107ca6d3 JH |
87 | |
88 | // Do the scan | |
e8d00928 ML |
89 | auto devices = driver->scan(drvopts); |
90 | driver_devices.insert(driver_devices.end(), devices.begin(), devices.end()); | |
107ca6d3 JH |
91 | driver_devices.sort(compare_devices); |
92 | ||
93 | // Add the scanned devices to the main list | |
94 | _devices.insert(_devices.end(), driver_devices.begin(), | |
95 | driver_devices.end()); | |
96 | _devices.sort(compare_devices); | |
97 | ||
98 | return driver_devices; | |
99 | } | |
100 | ||
e8d00928 ML |
101 | const map<string, string> DeviceManager::get_device_info( |
102 | shared_ptr<Device> device) | |
103 | { | |
104 | map<string, string> result; | |
105 | ||
106 | assert(device); | |
107 | ||
108 | if (device->vendor().length() > 0) | |
109 | result["vendor"] = device->vendor(); | |
110 | if (device->model().length() > 0) | |
111 | result["model"] = device->model(); | |
112 | if (device->version().length() > 0) | |
113 | result["version"] = device->version(); | |
114 | if (device->serial_number().length() > 0) | |
115 | result["serial_num"] = device->serial_number(); | |
116 | if (device->connection_id().length() > 0) | |
117 | result["connection_id"] = device->connection_id(); | |
118 | ||
119 | return result; | |
120 | } | |
121 | ||
122 | const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info( | |
6842b5fc SA |
123 | const map<string, string> search_info) |
124 | { | |
e8d00928 | 125 | shared_ptr<HardwareDevice> last_resort_dev; |
6842b5fc SA |
126 | map<string, string> dev_info; |
127 | ||
128 | last_resort_dev = NULL; | |
129 | ||
e8d00928 | 130 | for (shared_ptr<HardwareDevice> dev : _devices) { |
6842b5fc | 131 | assert(dev); |
e8d00928 | 132 | dev_info = get_device_info(dev); |
6842b5fc SA |
133 | |
134 | // If present, vendor and model always have to match. | |
135 | if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0) | |
136 | if (dev_info.at("vendor") != search_info.at("vendor")) continue; | |
137 | ||
138 | if (dev_info.count("model") > 0 && search_info.count("model") > 0) | |
139 | if (dev_info.at("model") != search_info.at("model")) continue; | |
140 | ||
141 | // Most unique match: vendor/model/serial_num (but don't match a S/N of 0) | |
142 | if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0") | |
143 | && search_info.count("serial_num") > 0) | |
144 | if (dev_info.at("serial_num") == search_info.at("serial_num") && | |
145 | dev_info.at("serial_num") != "0") | |
146 | return dev; | |
147 | ||
148 | // Second best match: vendor/model/connection_id | |
149 | if (dev_info.count("connection_id") > 0 && | |
150 | search_info.count("connection_id") > 0) | |
151 | if (dev_info.at("connection_id") == search_info.at("connection_id")) | |
152 | return dev; | |
153 | ||
154 | // Last resort: vendor/model/version | |
155 | if (dev_info.count("version") > 0 && | |
156 | search_info.count("version") > 0) | |
157 | if (dev_info.at("version") == search_info.at("version") && | |
158 | dev_info.at("version") != "0") | |
159 | return dev; | |
160 | ||
161 | // For this device, we merely have a vendor/model match. | |
162 | last_resort_dev = dev; | |
163 | } | |
164 | ||
165 | // If there wasn't even a vendor/model/version match, we end up here. | |
166 | // This is usually the case for devices with only vendor/model data. | |
167 | // The selected device may be wrong with multiple such devices attached | |
168 | // but it is the best we can do at this point. After all, there may be | |
169 | // only one such device and we do want to select it in this case. | |
170 | return last_resort_dev; | |
171 | } | |
172 | ||
e8d00928 | 173 | string DeviceManager::device_description(shared_ptr<Device> device) |
107ca6d3 | 174 | { |
e8d00928 | 175 | auto session_device = dynamic_pointer_cast<SessionDevice>(device); |
107ca6d3 | 176 | |
e8d00928 ML |
177 | if (session_device) |
178 | return boost::filesystem::path( | |
179 | session_device->parent()->filename()).filename().string(); | |
dc0867ff | 180 | |
e8d00928 | 181 | ostringstream s; |
107ca6d3 | 182 | |
e8d00928 ML |
183 | vector<string> parts = {device->vendor(), device->model(), |
184 | device->version(), device->serial_number()}; | |
107ca6d3 | 185 | |
e8d00928 ML |
186 | for (size_t i = 0; i < parts.size(); i++) |
187 | { | |
188 | if (parts[i].length() > 0) | |
189 | { | |
190 | if (i != 0) | |
191 | s << " "; | |
192 | s << parts[i]; | |
193 | } | |
996b7c9d | 194 | } |
dc0867ff | 195 | |
e8d00928 ML |
196 | if (device->serial_number().length() == 0 && |
197 | device->connection_id().length() > 0) | |
198 | s << " " << device->connection_id(); | |
199 | ||
200 | return s.str(); | |
dc0867ff JH |
201 | } |
202 | ||
e8d00928 ML |
203 | bool DeviceManager::compare_devices(shared_ptr<HardwareDevice> a, |
204 | shared_ptr<HardwareDevice> b) | |
107ca6d3 | 205 | { |
19adbc2c JH |
206 | assert(a); |
207 | assert(b); | |
e8d00928 ML |
208 | |
209 | return device_description(a).compare(device_description(b)) < 0; | |
107ca6d3 JH |
210 | } |
211 | ||
212 | } // namespace pv |