]> sigrok.org Git - pulseview.git/blob - pv/devicemanager.cpp
Add serial_num/connection_id handling and save/restore last device
[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.h"
22 #include "device/device.h"
23 #include "sigsession.h"
24
25 #include <cassert>
26 #include <stdexcept>
27 #include <string>
28
29 #include <libsigrok/libsigrok.h>
30
31 using std::list;
32 using std::map;
33 using std::runtime_error;
34 using std::shared_ptr;
35 using std::string;
36
37 namespace pv {
38
39 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
40         _sr_ctx(sr_ctx)
41 {
42         init_drivers();
43         scan_all_drivers();
44 }
45
46 DeviceManager::~DeviceManager()
47 {
48         release_devices();
49 }
50
51 const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
52 {
53         return _devices;
54 }
55
56 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
57         struct sr_dev_driver *const driver, GSList *const drvopts)
58 {
59         list< shared_ptr<device::Device> > driver_devices;
60
61         assert(driver);
62
63         // Remove any device instances from this driver from the device
64         // list. They will not be valid after the scan.
65         auto i = _devices.begin();
66         while (i != _devices.end()) {
67                 if ((*i)->dev_inst()->driver == driver)
68                         i = _devices.erase(i);
69                 else
70                         i++;
71         }
72
73         // Release this driver and all it's attached devices
74         release_driver(driver);
75
76         // Do the scan
77         GSList *const devices = sr_driver_scan(driver, drvopts);
78         for (GSList *l = devices; l; l = l->next)
79                 driver_devices.push_back(shared_ptr<device::Device>(
80                         new device::Device((sr_dev_inst*)l->data)));
81         g_slist_free(devices);
82         driver_devices.sort(compare_devices);
83
84         // Add the scanned devices to the main list
85         _devices.insert(_devices.end(), driver_devices.begin(),
86                 driver_devices.end());
87         _devices.sort(compare_devices);
88
89         return driver_devices;
90 }
91
92 const shared_ptr<device::Device> DeviceManager::find_device_from_info(
93         const map<string, string> search_info)
94 {
95         shared_ptr<device::Device> last_resort_dev;
96         map<string, string> dev_info;
97
98         last_resort_dev = NULL;
99
100         for (shared_ptr<device::Device> dev : _devices) {
101                 assert(dev);
102                 dev_info = dev->get_device_info();
103
104                 // If present, vendor and model always have to match.
105                 if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
106                         if (dev_info.at("vendor") != search_info.at("vendor")) continue;
107
108                 if (dev_info.count("model") > 0 && search_info.count("model") > 0)
109                         if (dev_info.at("model") != search_info.at("model")) continue;
110
111                 // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
112                 if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
113                                 && search_info.count("serial_num") > 0)
114                         if (dev_info.at("serial_num") == search_info.at("serial_num") &&
115                                         dev_info.at("serial_num") != "0")
116                                 return dev;
117
118                 // Second best match: vendor/model/connection_id
119                 if (dev_info.count("connection_id") > 0 &&
120                         search_info.count("connection_id") > 0)
121                         if (dev_info.at("connection_id") == search_info.at("connection_id"))
122                                 return dev;
123
124                 // Last resort: vendor/model/version
125                 if (dev_info.count("version") > 0 &&
126                         search_info.count("version") > 0)
127                         if (dev_info.at("version") == search_info.at("version") &&
128                                         dev_info.at("version") != "0")
129                                 return dev;
130
131                 // For this device, we merely have a vendor/model match.
132                 last_resort_dev = dev;
133         }
134
135         // If there wasn't even a vendor/model/version match, we end up here.
136         // This is usually the case for devices with only vendor/model data.
137         // The selected device may be wrong with multiple such devices attached
138         // but it is the best we can do at this point. After all, there may be
139         // only one such device and we do want to select it in this case.
140         return last_resort_dev;
141 }
142
143 void DeviceManager::init_drivers()
144 {
145         // Initialise all libsigrok drivers
146         sr_dev_driver **const drivers = sr_driver_list();
147         for (sr_dev_driver **driver = drivers; *driver; driver++) {
148                 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
149                         throw runtime_error(
150                                 string("Failed to initialize driver ") +
151                                 string((*driver)->name));
152                 }
153         }
154 }
155
156 void DeviceManager::release_devices()
157 {
158         // Release all the used devices
159         for (shared_ptr<device::Device> dev : _devices) {
160                 assert(dev);
161                 dev->release();
162         }
163
164         // Clear all the drivers
165         sr_dev_driver **const drivers = sr_driver_list();
166         for (sr_dev_driver **driver = drivers; *driver; driver++)
167                 sr_dev_clear(*driver);
168 }
169
170 void DeviceManager::scan_all_drivers()
171 {
172         // Scan all drivers for all devices.
173         struct sr_dev_driver **const drivers = sr_driver_list();
174         for (struct sr_dev_driver **driver = drivers; *driver; driver++)
175                 driver_scan(*driver);
176 }
177
178 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
179 {
180         for (shared_ptr<device::Device> dev : _devices) {
181                 assert(dev);
182                 if(dev->dev_inst()->driver == driver)
183                         dev->release();
184         }
185
186         // Clear all the old device instances from this driver
187         sr_dev_clear(driver);
188 }
189
190 bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
191         shared_ptr<device::Device> b)
192 {
193         assert(a);
194         assert(b);
195         return a->format_device_title().compare(b->format_device_title()) < 0;
196 }
197
198 } // namespace pv