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