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