]>
Commit | Line | Data |
---|---|---|
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" | |
22 | ||
23 | #include <cassert> | |
dd63af74 | 24 | #include <sstream> |
107ca6d3 JH |
25 | #include <stdexcept> |
26 | #include <string> | |
27 | ||
28 | #include <libsigrok/libsigrok.h> | |
29 | ||
30 | using namespace std; | |
31 | ||
32 | namespace pv { | |
33 | ||
34 | DeviceManager::DeviceManager(struct sr_context *sr_ctx) : | |
35 | _sr_ctx(sr_ctx) | |
36 | { | |
37 | init_drivers(); | |
38 | scan_all_drivers(); | |
39 | } | |
40 | ||
41 | DeviceManager::~DeviceManager() | |
42 | { | |
43 | release_devices(); | |
44 | } | |
45 | ||
46 | const std::list<sr_dev_inst*>& DeviceManager::devices() const | |
47 | { | |
48 | return _devices; | |
49 | } | |
50 | ||
51 | list<sr_dev_inst*> DeviceManager::driver_scan( | |
52 | struct sr_dev_driver *const driver, GSList *const drvopts) | |
53 | { | |
54 | list<sr_dev_inst*> driver_devices; | |
55 | ||
56 | assert(driver); | |
57 | ||
58 | // Remove any device instances from this driver from the device | |
59 | // list. They will not be valid after the scan. | |
60 | list<sr_dev_inst*>::iterator i = _devices.begin(); | |
61 | while (i != _devices.end()) { | |
62 | if ((*i)->driver == driver) | |
63 | i = _devices.erase(i); | |
64 | else | |
65 | i++; | |
66 | } | |
67 | ||
68 | // Clear all the old device instances from this driver | |
69 | sr_dev_clear(driver); | |
70 | ||
71 | // Do the scan | |
72 | GSList *const devices = sr_driver_scan(driver, drvopts); | |
73 | for (GSList *l = devices; l; l = l->next) | |
74 | driver_devices.push_back((sr_dev_inst*)l->data); | |
75 | g_slist_free(devices); | |
76 | driver_devices.sort(compare_devices); | |
77 | ||
78 | // Add the scanned devices to the main list | |
79 | _devices.insert(_devices.end(), driver_devices.begin(), | |
80 | driver_devices.end()); | |
81 | _devices.sort(compare_devices); | |
82 | ||
83 | return driver_devices; | |
84 | } | |
85 | ||
dd63af74 JH |
86 | string DeviceManager::format_device_title(const sr_dev_inst *const sdi) |
87 | { | |
88 | ostringstream s; | |
89 | ||
90 | assert(sdi); | |
91 | ||
92 | if (sdi->vendor && sdi->vendor[0]) { | |
93 | s << sdi->vendor; | |
94 | if ((sdi->model && sdi->model[0]) || | |
95 | (sdi->version && sdi->version[0])) | |
96 | s << ' '; | |
97 | } | |
98 | ||
99 | if (sdi->model && sdi->model[0]) { | |
100 | s << sdi->model; | |
101 | if (sdi->version && sdi->version[0]) | |
102 | s << ' '; | |
103 | } | |
104 | ||
105 | if (sdi->version && sdi->version[0]) | |
106 | s << sdi->version; | |
107 | ||
108 | return s.str(); | |
109 | } | |
110 | ||
107ca6d3 JH |
111 | void DeviceManager::init_drivers() |
112 | { | |
113 | // Initialise all libsigrok drivers | |
114 | sr_dev_driver **const drivers = sr_driver_list(); | |
115 | for (sr_dev_driver **driver = drivers; *driver; driver++) { | |
116 | if (sr_driver_init(_sr_ctx, *driver) != SR_OK) { | |
117 | throw runtime_error( | |
118 | string("Failed to initialize driver ") + | |
119 | string((*driver)->name)); | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | void DeviceManager::release_devices() | |
125 | { | |
126 | sr_dev_driver **const drivers = sr_driver_list(); | |
127 | for (sr_dev_driver **driver = drivers; *driver; driver++) | |
128 | sr_dev_clear(*driver); | |
129 | } | |
130 | ||
131 | void DeviceManager::scan_all_drivers() | |
132 | { | |
133 | // Scan all drivers for all devices. | |
134 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
135 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) | |
136 | driver_scan(*driver); | |
137 | } | |
138 | ||
139 | bool DeviceManager::compare_devices(const sr_dev_inst *const a, | |
140 | const sr_dev_inst *const b) | |
141 | { | |
dd63af74 | 142 | return format_device_title(a).compare(format_device_title(b)) < 0; |
107ca6d3 JH |
143 | } |
144 | ||
145 | } // namespace pv |