]>
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> | |
24 | #include <cstring> | |
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 | ||
86 | void DeviceManager::init_drivers() | |
87 | { | |
88 | // Initialise all libsigrok drivers | |
89 | sr_dev_driver **const drivers = sr_driver_list(); | |
90 | for (sr_dev_driver **driver = drivers; *driver; driver++) { | |
91 | if (sr_driver_init(_sr_ctx, *driver) != SR_OK) { | |
92 | throw runtime_error( | |
93 | string("Failed to initialize driver ") + | |
94 | string((*driver)->name)); | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | void DeviceManager::release_devices() | |
100 | { | |
101 | sr_dev_driver **const drivers = sr_driver_list(); | |
102 | for (sr_dev_driver **driver = drivers; *driver; driver++) | |
103 | sr_dev_clear(*driver); | |
104 | } | |
105 | ||
106 | void DeviceManager::scan_all_drivers() | |
107 | { | |
108 | // Scan all drivers for all devices. | |
109 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
110 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) | |
111 | driver_scan(*driver); | |
112 | } | |
113 | ||
114 | bool DeviceManager::compare_devices(const sr_dev_inst *const a, | |
115 | const sr_dev_inst *const b) | |
116 | { | |
117 | assert(a); | |
118 | assert(b); | |
119 | ||
120 | const int vendor_cmp = strcasecmp(a->vendor, b->vendor); | |
121 | if(vendor_cmp < 0) | |
122 | return true; | |
123 | else if(vendor_cmp > 0) | |
124 | return false; | |
125 | ||
126 | const int model_cmp = strcasecmp(a->model, b->model); | |
127 | if(model_cmp < 0) | |
128 | return true; | |
129 | else if(model_cmp > 0) | |
130 | return false; | |
131 | ||
132 | return strcasecmp(a->version, b->version) < 0; | |
133 | } | |
134 | ||
135 | } // namespace pv |