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