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