]> sigrok.org Git - pulseview.git/blob - pv/devicemanager.cpp
SweepTimingWidget implemented show_125_list
[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 "sigsession.h"
23
24 #include <cassert>
25 #include <sstream>
26 #include <stdexcept>
27 #include <string>
28
29 #include <libsigrok/libsigrok.h>
30
31 using std::list;
32 using std::map;
33 using std::ostringstream;
34 using std::runtime_error;
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 std::list<sr_dev_inst*>& DeviceManager::devices() const
52 {
53         return _devices;
54 }
55
56 void DeviceManager::use_device(sr_dev_inst *sdi, SigSession *owner)
57 {
58         assert(sdi);
59         assert(owner);
60
61         _used_devices[sdi] = owner;
62
63         sr_dev_open(sdi);
64 }
65
66 void DeviceManager::release_device(sr_dev_inst *sdi)
67 {
68         assert(sdi);
69
70         // Notify the owner, and removed the device from the used device list
71         _used_devices[sdi]->release_device(sdi);
72         _used_devices.erase(sdi);
73
74         sr_dev_close(sdi);
75 }
76
77 list<sr_dev_inst*> DeviceManager::driver_scan(
78         struct sr_dev_driver *const driver, GSList *const drvopts)
79 {
80         list<sr_dev_inst*> driver_devices;
81
82         assert(driver);
83
84         // Remove any device instances from this driver from the device
85         // list. They will not be valid after the scan.
86         list<sr_dev_inst*>::iterator i = _devices.begin();
87         while (i != _devices.end()) {
88                 if ((*i)->driver == driver)
89                         i = _devices.erase(i);
90                 else
91                         i++;
92         }
93
94         // Release this driver and all it's attached devices
95         release_driver(driver);
96
97         // Do the scan
98         GSList *const devices = sr_driver_scan(driver, drvopts);
99         for (GSList *l = devices; l; l = l->next)
100                 driver_devices.push_back((sr_dev_inst*)l->data);
101         g_slist_free(devices);
102         driver_devices.sort(compare_devices);
103
104         // Add the scanned devices to the main list
105         _devices.insert(_devices.end(), driver_devices.begin(),
106                 driver_devices.end());
107         _devices.sort(compare_devices);
108
109         return driver_devices;
110 }
111
112 string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
113 {
114         ostringstream s;
115
116         assert(sdi);
117
118         if (sdi->vendor && sdi->vendor[0]) {
119                 s << sdi->vendor;
120                 if ((sdi->model && sdi->model[0]) ||
121                         (sdi->version && sdi->version[0]))
122                         s << ' ';
123         }
124
125         if (sdi->model && sdi->model[0]) {
126                 s << sdi->model;
127                 if (sdi->version && sdi->version[0])
128                         s << ' ';
129         }
130
131         if (sdi->version && sdi->version[0])
132                 s << sdi->version;
133
134         return s.str();
135 }
136
137 void DeviceManager::init_drivers()
138 {
139         // Initialise all libsigrok drivers
140         sr_dev_driver **const drivers = sr_driver_list();
141         for (sr_dev_driver **driver = drivers; *driver; driver++) {
142                 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
143                         throw runtime_error(
144                                 string("Failed to initialize driver ") +
145                                 string((*driver)->name));
146                 }
147         }
148 }
149
150 void DeviceManager::release_devices()
151 {
152         // Release all the used devices
153         for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
154                 i != _used_devices.end(); i++)
155                 release_device((*i).first);
156
157         _used_devices.clear();
158
159         // Clear all the drivers
160         sr_dev_driver **const drivers = sr_driver_list();
161         for (sr_dev_driver **driver = drivers; *driver; driver++)
162                 sr_dev_clear(*driver);
163 }
164
165 void DeviceManager::scan_all_drivers()
166 {
167         // Scan all drivers for all devices.
168         struct sr_dev_driver **const drivers = sr_driver_list();
169         for (struct sr_dev_driver **driver = drivers; *driver; driver++)
170                 driver_scan(*driver);
171 }
172
173 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
174 {
175         assert(driver);
176         for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
177                 i != _used_devices.end(); i++)
178                 if((*i).first->driver == driver)
179                 {
180                         // Notify the current owner of the device
181                         (*i).second->release_device((*i).first);
182
183                         // Remove it from the used device list
184                         _used_devices.erase(i);
185
186                         // Close the device instance
187                         sr_dev_close((*i).first);
188                 }
189
190         // Clear all the old device instances from this driver
191         sr_dev_clear(driver);
192 }
193
194 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
195         const sr_dev_inst *const b)
196 {
197         return format_device_title(a).compare(format_device_title(b)) < 0;
198 }
199
200 } // namespace pv