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