]> sigrok.org Git - pulseview.git/blame - pv/devicemanager.cpp
main: Use only the domain, not the whole URL.
[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
29#include <libsigrok/libsigrok.h>
30
819f4c25
JH
31using std::list;
32using std::map;
33using std::ostringstream;
34using std::runtime_error;
f9abf97e 35using std::shared_ptr;
819f4c25 36using std::string;
107ca6d3
JH
37
38namespace pv {
39
40DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
41 _sr_ctx(sr_ctx)
42{
43 init_drivers();
44 scan_all_drivers();
45}
46
47DeviceManager::~DeviceManager()
48{
49 release_devices();
50}
51
85843b14 52const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
107ca6d3
JH
53{
54 return _devices;
55}
56
85843b14 57list< shared_ptr<device::Device> > DeviceManager::driver_scan(
107ca6d3
JH
58 struct sr_dev_driver *const driver, GSList *const drvopts)
59{
85843b14 60 list< shared_ptr<device::Device> > driver_devices;
107ca6d3
JH
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.
f46e495e 66 auto i = _devices.begin();
107ca6d3 67 while (i != _devices.end()) {
19adbc2c 68 if ((*i)->dev_inst()->driver == driver)
107ca6d3
JH
69 i = _devices.erase(i);
70 else
71 i++;
72 }
73
dc0867ff
JH
74 // Release this driver and all it's attached devices
75 release_driver(driver);
107ca6d3
JH
76
77 // Do the scan
78 GSList *const devices = sr_driver_scan(driver, drvopts);
79 for (GSList *l = devices; l; l = l->next)
85843b14 80 driver_devices.push_back(shared_ptr<device::Device>(
921b90c0 81 new device::Device((sr_dev_inst*)l->data)));
107ca6d3
JH
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
93void 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
106void DeviceManager::release_devices()
107{
dc0867ff 108 // Release all the used devices
d9aecf1f 109 for (shared_ptr<device::Device> dev : _devices) {
996b7c9d
JH
110 assert(dev);
111 dev->release();
112 }
dc0867ff
JH
113
114 // Clear all the drivers
107ca6d3
JH
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
120void 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
dc0867ff
JH
128void DeviceManager::release_driver(struct sr_dev_driver *const driver)
129{
d9aecf1f 130 for (shared_ptr<device::Device> dev : _devices) {
996b7c9d
JH
131 assert(dev);
132 if(dev->dev_inst()->driver == driver)
133 dev->release();
134 }
dc0867ff
JH
135
136 // Clear all the old device instances from this driver
137 sr_dev_clear(driver);
138}
139
85843b14
JH
140bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
141 shared_ptr<device::Device> b)
107ca6d3 142{
19adbc2c
JH
143 assert(a);
144 assert(b);
145 return a->format_device_title().compare(b->format_device_title()) < 0;
107ca6d3
JH
146}
147
148} // namespace pv