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