2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "devicemanager.h"
28 #include <libsigrok/libsigrok.h>
34 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
41 DeviceManager::~DeviceManager()
46 const std::list<sr_dev_inst*>& DeviceManager::devices() const
51 list<sr_dev_inst*> DeviceManager::driver_scan(
52 struct sr_dev_driver *const driver, GSList *const drvopts)
54 list<sr_dev_inst*> driver_devices;
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);
68 // Clear all the old device instances from this driver
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);
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);
83 return driver_devices;
86 string DeviceManager::format_device_title(const sr_dev_inst *const sdi)
92 if (sdi->vendor && sdi->vendor[0]) {
94 if ((sdi->model && sdi->model[0]) ||
95 (sdi->version && sdi->version[0]))
99 if (sdi->model && sdi->model[0]) {
101 if (sdi->version && sdi->version[0])
105 if (sdi->version && sdi->version[0])
111 void DeviceManager::init_drivers()
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) {
118 string("Failed to initialize driver ") +
119 string((*driver)->name));
124 void DeviceManager::release_devices()
126 sr_dev_driver **const drivers = sr_driver_list();
127 for (sr_dev_driver **driver = drivers; *driver; driver++)
128 sr_dev_clear(*driver);
131 void DeviceManager::scan_all_drivers()
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);
139 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
140 const sr_dev_inst *const b)
142 return format_device_title(a).compare(format_device_title(b)) < 0;