]> sigrok.org Git - pulseview.git/blame - pv/devicemanager.cpp
devices::Device: Moved in full_name and display_name
[pulseview.git] / pv / devicemanager.cpp
CommitLineData
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>
107ca6d3 25#include <stdexcept>
e8d00928 26#include <sstream>
107ca6d3 27#include <string>
e8d00928 28#include <vector>
107ca6d3 29
fe3a1c21 30#include <libsigrokcxx/libsigrokcxx.hpp>
107ca6d3 31
88fc0565 32#include <boost/algorithm/string/join.hpp>
e8d00928
ML
33#include <boost/filesystem.hpp>
34
da30ecb7
JH
35#include <pv/devices/hardwaredevice.hpp>
36
88fc0565
JH
37using boost::algorithm::join;
38
e8d00928 39using std::dynamic_pointer_cast;
819f4c25
JH
40using std::list;
41using std::map;
e8d00928 42using std::remove_if;
819f4c25 43using std::runtime_error;
f9abf97e 44using std::shared_ptr;
819f4c25 45using std::string;
e8d00928
ML
46using std::vector;
47
48using Glib::VariantBase;
49
50using sigrok::ConfigKey;
51using sigrok::Context;
52using sigrok::Driver;
e8d00928 53using sigrok::SessionDevice;
107ca6d3
JH
54
55namespace pv {
56
e8d00928 57DeviceManager::DeviceManager(shared_ptr<Context> context) :
8dbbc7f0 58 context_(context)
107ca6d3 59{
e8d00928
ML
60 for (auto entry : context->drivers())
61 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
107ca6d3
JH
62}
63
64DeviceManager::~DeviceManager()
65{
107ca6d3
JH
66}
67
72d85f36
JH
68const std::shared_ptr<sigrok::Context>& DeviceManager::context() const
69{
70 return context_;
71}
72
e8d00928
ML
73shared_ptr<Context> DeviceManager::context()
74{
8dbbc7f0 75 return context_;
e8d00928
ML
76}
77
da30ecb7
JH
78const list< shared_ptr<devices::HardwareDevice> >&
79DeviceManager::devices() const
107ca6d3 80{
8dbbc7f0 81 return devices_;
107ca6d3
JH
82}
83
da30ecb7
JH
84list< shared_ptr<devices::HardwareDevice> >
85DeviceManager::driver_scan(
e8d00928 86 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
107ca6d3 87{
da30ecb7 88 list< shared_ptr<devices::HardwareDevice> > driver_devices;
107ca6d3
JH
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.
da30ecb7
JH
94 devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) {
95 return device->hardware_device()->driver() == driver; });
107ca6d3
JH
96
97 // Do the scan
e8d00928 98 auto devices = driver->scan(drvopts);
107ca6d3 99
a4cf020a 100 // Add the scanned devices to the main list, set display names and sort.
da30ecb7
JH
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 }
a4cf020a 106
da30ecb7 107 for (shared_ptr<devices::HardwareDevice> device : driver_devices)
602bff3a 108 build_display_name(device);
a4cf020a 109
da30ecb7
JH
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)
a4cf020a 114 { return compare_devices(a, b); });
107ca6d3
JH
115
116 return driver_devices;
117}
118
e8d00928 119const map<string, string> DeviceManager::get_device_info(
da30ecb7 120 shared_ptr<devices::Device> device)
e8d00928
ML
121{
122 map<string, string> result;
123
124 assert(device);
125
da30ecb7
JH
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();
e8d00928
ML
137
138 return result;
139}
140
da30ecb7 141const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info(
6842b5fc
SA
142 const map<string, string> search_info)
143{
da30ecb7 144 shared_ptr<devices::HardwareDevice> last_resort_dev;
6842b5fc
SA
145 map<string, string> dev_info;
146
da30ecb7 147 for (shared_ptr<devices::HardwareDevice> dev : devices_) {
6842b5fc 148 assert(dev);
e8d00928 149 dev_info = get_device_info(dev);
6842b5fc
SA
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
da30ecb7 190void DeviceManager::build_display_name(shared_ptr<devices::Device> device)
107ca6d3 191{
da30ecb7
JH
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);
107ca6d3 195
602bff3a
SA
196 if (session_device) {
197 full_names_[device] = display_names_[device] =
198 boost::filesystem::path(
e8d00928 199 session_device->parent()->filename()).filename().string();
602bff3a
SA
200 return;
201 }
dc0867ff 202
602bff3a
SA
203 // First, build the device's full name. It always contains all
204 // possible information.
da30ecb7
JH
205 vector<string> parts = {sr_dev->vendor(), sr_dev->model(),
206 sr_dev->version(), sr_dev->serial_number()};
602bff3a 207
da30ecb7
JH
208 if (sr_dev->connection_id().length() > 0)
209 parts.push_back("("+sr_dev->connection_id()+")");
602bff3a 210
88fc0565 211 full_names_[device] = join(parts, " ");
602bff3a
SA
212
213 // Next, build the display name. It only contains fields as required.
a4cf020a
SA
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.
7ddb8da6
JH
217 const bool multiple_dev = hardware_device && any_of(
218 devices_.begin(), devices_.end(),
da30ecb7
JH
219 [&](shared_ptr<devices::HardwareDevice> dev) {
220 return (dev->device()->vendor() == hardware_device->vendor() &&
221 dev->device()->model() == hardware_device->model()) &&
222 dev != device;
7ddb8da6 223 } );
a4cf020a 224
da30ecb7 225 parts = {sr_dev->vendor(), sr_dev->model()};
a4cf020a
SA
226
227 if (multiple_dev) {
da30ecb7
JH
228 parts.push_back(sr_dev->version());
229 parts.push_back(sr_dev->serial_number());
a4cf020a 230
da30ecb7
JH
231 if ((sr_dev->serial_number().length() == 0) &&
232 (sr_dev->connection_id().length() > 0))
233 parts.push_back("("+sr_dev->connection_id()+")");
a4cf020a 234 }
107ca6d3 235
88fc0565 236 display_names_[device] = join(parts, " ");
dc0867ff
JH
237}
238
da30ecb7
JH
239const std::string DeviceManager::get_display_name(
240 std::shared_ptr<devices::Device> dev)
a4cf020a 241{
8dbbc7f0 242 return display_names_[dev];
a4cf020a
SA
243}
244
da30ecb7
JH
245const std::string DeviceManager::get_full_name(
246 std::shared_ptr<devices::Device> dev)
602bff3a
SA
247{
248 return full_names_[dev];
249}
250
da30ecb7
JH
251void DeviceManager::update_display_name(
252 std::shared_ptr<devices::Device> dev)
a4cf020a 253{
602bff3a 254 build_display_name(dev);
a4cf020a
SA
255}
256
da30ecb7
JH
257bool DeviceManager::compare_devices(
258 shared_ptr<devices::Device> a, shared_ptr<devices::Device> b)
107ca6d3 259{
19adbc2c
JH
260 assert(a);
261 assert(b);
e8d00928 262
8dbbc7f0 263 return display_names_[a].compare(display_names_[b]) < 0;
107ca6d3
JH
264}
265
266} // namespace pv