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