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 void DeviceManager::init_drivers()
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) {
93 string("Failed to initialize driver ") +
94 string((*driver)->name));
99 void DeviceManager::release_devices()
101 sr_dev_driver **const drivers = sr_driver_list();
102 for (sr_dev_driver **driver = drivers; *driver; driver++)
103 sr_dev_clear(*driver);
106 void DeviceManager::scan_all_drivers()
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);
114 bool DeviceManager::compare_devices(const sr_dev_inst *const a,
115 const sr_dev_inst *const b)
120 const int vendor_cmp = strcasecmp(a->vendor, b->vendor);
123 else if(vendor_cmp > 0)
126 const int model_cmp = strcasecmp(a->model, b->model);
129 else if(model_cmp > 0)
132 return strcasecmp(a->version, b->version) < 0;