]> sigrok.org Git - pulseview.git/blame - pv/devicemanager.cpp
TraceView: Restore vertical offset
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
107ca6d3
JH
18 */
19
2acdb232 20#include "devicemanager.hpp"
f65cd27b 21#include "session.hpp"
107ca6d3
JH
22
23#include <cassert>
3084ed4b 24#include <functional>
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
3084ed4b 39using std::bind;
e8d00928 40using std::dynamic_pointer_cast;
819f4c25
JH
41using std::list;
42using std::map;
e71eb81c
JH
43using std::placeholders::_1;
44using std::placeholders::_2;
e8d00928 45using std::remove_if;
819f4c25 46using std::runtime_error;
f9abf97e 47using std::shared_ptr;
819f4c25 48using std::string;
e8d00928
ML
49using std::vector;
50
51using Glib::VariantBase;
52
53using sigrok::ConfigKey;
54using sigrok::Context;
55using sigrok::Driver;
e8d00928 56using sigrok::SessionDevice;
107ca6d3
JH
57
58namespace pv {
59
e8d00928 60DeviceManager::DeviceManager(shared_ptr<Context> context) :
8dbbc7f0 61 context_(context)
107ca6d3 62{
e8d00928
ML
63 for (auto entry : context->drivers())
64 driver_scan(entry.second, map<const ConfigKey *, VariantBase>());
107ca6d3
JH
65}
66
72d85f36
JH
67const std::shared_ptr<sigrok::Context>& DeviceManager::context() const
68{
69 return context_;
70}
71
e8d00928
ML
72shared_ptr<Context> DeviceManager::context()
73{
8dbbc7f0 74 return context_;
e8d00928
ML
75}
76
da30ecb7
JH
77const list< shared_ptr<devices::HardwareDevice> >&
78DeviceManager::devices() const
107ca6d3 79{
8dbbc7f0 80 return devices_;
107ca6d3
JH
81}
82
da30ecb7
JH
83list< shared_ptr<devices::HardwareDevice> >
84DeviceManager::driver_scan(
e8d00928 85 shared_ptr<Driver> driver, map<const ConfigKey *, VariantBase> drvopts)
107ca6d3 86{
da30ecb7 87 list< shared_ptr<devices::HardwareDevice> > driver_devices;
107ca6d3
JH
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.
da30ecb7
JH
93 devices_.remove_if([&](shared_ptr<devices::HardwareDevice> device) {
94 return device->hardware_device()->driver() == driver; });
107ca6d3
JH
95
96 // Do the scan
e8d00928 97 auto devices = driver->scan(drvopts);
107ca6d3 98
a4cf020a 99 // Add the scanned devices to the main list, set display names and sort.
da30ecb7
JH
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 }
a4cf020a 105
da30ecb7
JH
106 devices_.insert(devices_.end(), driver_devices.begin(),
107 driver_devices.end());
3084ed4b
JH
108 devices_.sort(bind(&DeviceManager::compare_devices, this, _1, _2));
109 driver_devices.sort(bind(
110 &DeviceManager::compare_devices, this, _1, _2));
107ca6d3
JH
111
112 return driver_devices;
113}
114
e8d00928 115const map<string, string> DeviceManager::get_device_info(
da30ecb7 116 shared_ptr<devices::Device> device)
e8d00928
ML
117{
118 map<string, string> result;
119
120 assert(device);
121
da30ecb7
JH
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();
e8d00928
ML
133
134 return result;
135}
136
da30ecb7 137const shared_ptr<devices::HardwareDevice> DeviceManager::find_device_from_info(
6842b5fc
SA
138 const map<string, string> search_info)
139{
da30ecb7 140 shared_ptr<devices::HardwareDevice> last_resort_dev;
6842b5fc
SA
141 map<string, string> dev_info;
142
da30ecb7 143 for (shared_ptr<devices::HardwareDevice> dev : devices_) {
6842b5fc 144 assert(dev);
e8d00928 145 dev_info = get_device_info(dev);
6842b5fc
SA
146
147 // If present, vendor and model always have to match.
148 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
2ad82c2e
UH
149 if (dev_info.at("vendor") != search_info.at("vendor"))
150 continue;
6842b5fc
SA
151
152 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
2ad82c2e
UH
153 if (dev_info.at("model") != search_info.at("model"))
154 continue;
6842b5fc
SA
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
3084ed4b 188bool DeviceManager::compare_devices(shared_ptr<devices::Device> a,
2ad82c2e
UH
189 shared_ptr<devices::Device> b)
190{
19adbc2c
JH
191 assert(a);
192 assert(b);
3084ed4b 193 return a->display_name(*this).compare(b->display_name(*this)) < 0;
107ca6d3
JH
194}
195
196} // namespace pv