]> sigrok.org Git - pulseview.git/blob - pv/devicemanager.cpp
CursorPair: Set font before calling compute_text_size
[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 <libsigrok/libsigrok.hpp>
31
32 #include <boost/algorithm/string/join.hpp>
33 #include <boost/filesystem.hpp>
34
35 using boost::algorithm::join;
36
37 using std::dynamic_pointer_cast;
38 using std::list;
39 using std::map;
40 using std::remove_if;
41 using std::runtime_error;
42 using std::shared_ptr;
43 using std::string;
44 using std::vector;
45
46 using Glib::VariantBase;
47
48 using sigrok::ConfigKey;
49 using sigrok::Context;
50 using sigrok::Driver;
51 using sigrok::Device;
52 using sigrok::HardwareDevice;
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 shared_ptr<Context> DeviceManager::context()
69 {
70         return context_;
71 }
72
73 const list< shared_ptr<HardwareDevice> >& DeviceManager::devices() const
74 {
75         return devices_;
76 }
77
78 list< shared_ptr<HardwareDevice> > DeviceManager::driver_scan(
79         shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
80 {
81         list< shared_ptr<HardwareDevice> > driver_devices;
82
83         assert(driver);
84
85         // Remove any device instances from this driver from the device
86         // list. They will not be valid after the scan.
87         devices_.remove_if([&](shared_ptr<HardwareDevice> device) {
88                 return device->driver() == driver; });
89
90         // Do the scan
91         auto devices = driver->scan(drvopts);
92         driver_devices.insert(driver_devices.end(), devices.begin(), devices.end());
93
94         // Add the scanned devices to the main list, set display names and sort.
95         devices_.insert(devices_.end(), driver_devices.begin(),
96                 driver_devices.end());
97
98         for (shared_ptr<Device> device : devices_)
99                 build_display_name(device);
100
101         devices_.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
102                 { return compare_devices(a, b); });
103
104         // As the display names depend on the complete devices_ list,
105         // we need to recompute them. However, there is no need to
106         // recomute all names of the devices_ list since only the
107         // devices that use the given driver can be affected.
108         for (shared_ptr<Device> device : driver_devices)
109                 build_display_name(device);
110
111         driver_devices.sort([&](shared_ptr<Device> a, shared_ptr<Device> b)
112                 { return compare_devices(a, b); });
113
114         return driver_devices;
115 }
116
117 const map<string, string> DeviceManager::get_device_info(
118         shared_ptr<Device> device)
119 {
120         map<string, string> result;
121
122         assert(device);
123
124         if (device->vendor().length() > 0)
125                 result["vendor"] = device->vendor();
126         if (device->model().length() > 0)
127                 result["model"] = device->model();
128         if (device->version().length() > 0)
129                 result["version"] = device->version();
130         if (device->serial_number().length() > 0)
131                 result["serial_num"] = device->serial_number();
132         if (device->connection_id().length() > 0)
133                 result["connection_id"] = device->connection_id();
134
135         return result;
136 }
137
138 const shared_ptr<HardwareDevice> DeviceManager::find_device_from_info(
139         const map<string, string> search_info)
140 {
141         shared_ptr<HardwareDevice> last_resort_dev;
142         map<string, string> dev_info;
143
144         last_resort_dev = NULL;
145
146         for (shared_ptr<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 void DeviceManager::build_display_name(shared_ptr<Device> device)
190 {
191         auto session_device = dynamic_pointer_cast<SessionDevice>(device);
192         auto hardware_device = dynamic_pointer_cast<HardwareDevice>(device);
193
194         if (session_device) {
195                 full_names_[device] = display_names_[device] =
196                         boost::filesystem::path(
197                         session_device->parent()->filename()).filename().string();
198                 return;
199         }
200
201         // First, build the device's full name. It always contains all
202         // possible information.
203         vector<string> parts = {device->vendor(), device->model(),
204                 device->version(), device->serial_number()};
205
206         if (device->connection_id().length() > 0)
207                 parts.push_back("("+device->connection_id()+")");
208
209         full_names_[device] = join(parts, " ");
210
211         // Next, build the display name. It only contains fields as required.
212
213         // If we can find another device with the same model/vendor then
214         // we have at least two such devices and need to distinguish them.
215         const bool multiple_dev = hardware_device && any_of(
216                 devices_.begin(), devices_.end(),
217                 [&](shared_ptr<HardwareDevice> dev) {
218                         return (dev->vendor() == hardware_device->vendor() &&
219                                 dev->model() == hardware_device->model()) &&
220                                 dev != hardware_device;
221                 } );
222
223         parts = {device->vendor(), device->model()};
224
225         if (multiple_dev) {
226                 parts.push_back(device->version());
227                 parts.push_back(device->serial_number());
228
229                 if ((device->serial_number().length() == 0) &&
230                         (device->connection_id().length() > 0))
231                         parts.push_back("("+device->connection_id()+")");
232         }
233
234         display_names_[device] = join(parts, " ");
235 }
236
237 const std::string DeviceManager::get_display_name(std::shared_ptr<sigrok::Device> dev)
238 {
239         return display_names_[dev];
240 }
241
242 const std::string DeviceManager::get_full_name(std::shared_ptr<sigrok::Device> dev)
243 {
244         return full_names_[dev];
245 }
246
247 void DeviceManager::update_display_name(std::shared_ptr<sigrok::Device> dev)
248 {
249         build_display_name(dev);
250 }
251
252 bool DeviceManager::compare_devices(shared_ptr<Device> a,
253         shared_ptr<Device> b)
254 {
255         assert(a);
256         assert(b);
257
258         return display_names_[a].compare(display_names_[b]) < 0;
259 }
260
261 } // namespace pv