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