1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* This file is part of the DSLogic-gui project.
* DSLogic-gui is based on PulseView.
*
* Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
* Copyright (C) 2013 DreamSourceLab <dreamsourcelab@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "devicemanager.h"
#include "sigsession.h"
#include <cassert>
#include <sstream>
#include <stdexcept>
#include <string>
#include <QtGui/QApplication>
#include <QObject>
#include <QDebug>
#include <QDir>
#include <libsigrok4DSLogic/libsigrok.h>
using namespace std;
char config_path[256];
namespace pv {
DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
_sr_ctx(sr_ctx)
{
init_drivers();
scan_all_drivers();
}
DeviceManager::~DeviceManager()
{
release_devices();
}
const std::list<sr_dev_inst*>& DeviceManager::devices() const
{
return _devices;
}
int DeviceManager::use_device(sr_dev_inst *sdi, SigSession *owner)
{
assert(sdi);
assert(owner);
if (sr_dev_open(sdi) != SR_OK)
return SR_ERR;
_used_devices[sdi] = owner;
return SR_OK;
}
void DeviceManager::release_device(sr_dev_inst *sdi)
{
assert(sdi);
// Notify the owner, and removed the device from the used device list
_used_devices[sdi]->release_device(sdi);
_used_devices.erase(sdi);
sr_dev_close(sdi);
}
list<sr_dev_inst*> DeviceManager::driver_scan(
struct sr_dev_driver *const driver, GSList *const drvopts)
{
list<sr_dev_inst*> driver_devices;
assert(driver);
// Remove any device instances from this driver from the device
// list. They will not be valid after the scan.
list<sr_dev_inst*>::iterator i = _devices.begin();
while (i != _devices.end()) {
if ((*i)->driver == driver)
i = _devices.erase(i);
else
i++;
}
// Release this driver and all it's attached devices
release_driver(driver);
// Check If DSLogic driver
if (strcmp(driver->name, "DSLogic") == 0) {
QDir dir(QApplication::applicationDirPath());
if (!dir.cd("res"))
return driver_devices;
std::string str = dir.absolutePath().toStdString() + "/";
strcpy(config_path, str.c_str());
}
// Do the scan
GSList *const devices = sr_driver_scan(driver, drvopts);
for (GSList *l = devices; l; l = l->next)
driver_devices.push_back((sr_dev_inst*)l->data);
g_slist_free(devices);
driver_devices.sort(compare_devices);
// Add the scanned devices to the main list
_devices.insert(_devices.end(), driver_devices.begin(),
driver_devices.end());
_devices.sort(compare_devices);
return driver_devices;
}
string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
{
ostringstream s;
assert(sdi);
if (sdi->vendor && sdi->vendor[0]) {
s << sdi->vendor;
if ((sdi->model && sdi->model[0]) ||
(sdi->version && sdi->version[0]))
s << ' ';
}
if (sdi->model && sdi->model[0]) {
s << sdi->model;
if (sdi->version && sdi->version[0])
s << ' ';
}
if (sdi->version && sdi->version[0])
s << sdi->version;
return s.str();
}
void DeviceManager::init_drivers()
{
// Initialise all libsigrok drivers
sr_dev_driver **const drivers = sr_driver_list();
for (sr_dev_driver **driver = drivers; *driver; driver++) {
if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
throw runtime_error(
string("Failed to initialize driver ") +
string((*driver)->name));
}
}
}
void DeviceManager::release_devices()
{
// Release all the used devices
for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
i != _used_devices.end();)
release_device((*i++).first);
_used_devices.clear();
// Clear all the drivers
sr_dev_driver **const drivers = sr_driver_list();
for (sr_dev_driver **driver = drivers; *driver; driver++)
sr_dev_clear(*driver);
}
void DeviceManager::scan_all_drivers()
{
// Scan all drivers for all devices.
struct sr_dev_driver **const drivers = sr_driver_list();
for (struct sr_dev_driver **driver = drivers; *driver; driver++)
driver_scan(*driver);
}
void DeviceManager::release_driver(struct sr_dev_driver *const driver)
{
assert(driver);
// map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
// while(i != _used_devices.end()) {
// if((*i).first->driver == driver)
// {
// // Notify the current owner of the device
// (*i).second->release_device((*i).first);
// // Close the device instance
// sr_dev_close((*i).first);
// // Remove it from the used device list
// i = _used_devices.erase(i);
// } else {
// i++;
// }
// }
for (map<sr_dev_inst*, SigSession*>::iterator i = _used_devices.begin();
i != _used_devices.end();)
if((*i).first->driver == driver)
{
// Notify the current owner of the device
(*i).second->release_device((*i).first);
// Close the device instance
sr_dev_close((*i).first);
// Remove it from the used device list
_used_devices.erase(i++);
} else {
i++;
}
// Clear all the old device instances from this driver
sr_dev_clear(driver);
}
bool DeviceManager::compare_devices(const sr_dev_inst *const a,
const sr_dev_inst *const b)
{
return format_device_title(a).compare(format_device_title(b)) < 0;
}
int DeviceManager::test_device(sr_dev_inst *sdi)
{
assert(sdi);
return sdi->driver->dev_test(sdi);
}
} // namespace pv
|