]> sigrok.org Git - pulseview.git/blame - pv/devicemanager.cpp
Added pv::device::Device
[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
19adbc2c 31using boost::shared_ptr;
819f4c25
JH
32using std::list;
33using std::map;
34using std::ostringstream;
35using std::runtime_error;
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
94574501 52const list< shared_ptr<pv::device::DevInst> >& DeviceManager::devices() const
107ca6d3
JH
53{
54 return _devices;
55}
56
94574501
JH
57void DeviceManager::use_device(shared_ptr<device::DevInst> dev_inst,
58 SigSession *owner)
dc0867ff 59{
19adbc2c 60 assert(dev_inst);
dc0867ff
JH
61 assert(owner);
62
19adbc2c 63 _used_devices[dev_inst] = owner;
dc0867ff 64
19adbc2c 65 sr_dev_open(dev_inst->dev_inst());
dc0867ff
JH
66}
67
94574501 68void DeviceManager::release_device(shared_ptr<device::DevInst> dev_inst)
dc0867ff 69{
19adbc2c 70 assert(dev_inst);
dc0867ff 71
19adbc2c 72 // Notify the owner, and remove the device from the used device list
94574501
JH
73 map< shared_ptr<device::DevInst>, pv::SigSession*>::const_iterator
74 iter = _used_devices.find(dev_inst);
19adbc2c 75 assert(iter != _used_devices.end());
dc0867ff 76
19adbc2c
JH
77 (*iter).second->release_device(dev_inst);
78 _used_devices.erase(dev_inst);
dc0867ff
JH
79}
80
94574501 81list< shared_ptr<device::DevInst> > DeviceManager::driver_scan(
107ca6d3
JH
82 struct sr_dev_driver *const driver, GSList *const drvopts)
83{
94574501 84 list< shared_ptr<device::DevInst> > driver_devices;
107ca6d3
JH
85
86 assert(driver);
87
88 // Remove any device instances from this driver from the device
89 // list. They will not be valid after the scan.
94574501 90 list< shared_ptr<device::DevInst> >::iterator i = _devices.begin();
107ca6d3 91 while (i != _devices.end()) {
19adbc2c 92 if ((*i)->dev_inst()->driver == driver)
107ca6d3
JH
93 i = _devices.erase(i);
94 else
95 i++;
96 }
97
dc0867ff
JH
98 // Release this driver and all it's attached devices
99 release_driver(driver);
107ca6d3
JH
100
101 // Do the scan
102 GSList *const devices = sr_driver_scan(driver, drvopts);
103 for (GSList *l = devices; l; l = l->next)
94574501 104 driver_devices.push_back(shared_ptr<device::DevInst>(
921b90c0 105 new device::Device((sr_dev_inst*)l->data)));
107ca6d3
JH
106 g_slist_free(devices);
107 driver_devices.sort(compare_devices);
108
109 // Add the scanned devices to the main list
110 _devices.insert(_devices.end(), driver_devices.begin(),
111 driver_devices.end());
112 _devices.sort(compare_devices);
113
114 return driver_devices;
115}
116
117void DeviceManager::init_drivers()
118{
119 // Initialise all libsigrok drivers
120 sr_dev_driver **const drivers = sr_driver_list();
121 for (sr_dev_driver **driver = drivers; *driver; driver++) {
122 if (sr_driver_init(_sr_ctx, *driver) != SR_OK) {
123 throw runtime_error(
124 string("Failed to initialize driver ") +
125 string((*driver)->name));
126 }
127 }
128}
129
130void DeviceManager::release_devices()
131{
dc0867ff 132 // Release all the used devices
94574501 133 for (map<shared_ptr<device::DevInst>, SigSession*>::iterator i =
19adbc2c 134 _used_devices.begin(); i != _used_devices.end(); i++)
dc0867ff
JH
135 release_device((*i).first);
136
137 _used_devices.clear();
138
139 // Clear all the drivers
107ca6d3
JH
140 sr_dev_driver **const drivers = sr_driver_list();
141 for (sr_dev_driver **driver = drivers; *driver; driver++)
142 sr_dev_clear(*driver);
143}
144
145void DeviceManager::scan_all_drivers()
146{
147 // Scan all drivers for all devices.
148 struct sr_dev_driver **const drivers = sr_driver_list();
149 for (struct sr_dev_driver **driver = drivers; *driver; driver++)
150 driver_scan(*driver);
151}
152
dc0867ff
JH
153void DeviceManager::release_driver(struct sr_dev_driver *const driver)
154{
155 assert(driver);
94574501 156 for (map<shared_ptr<device::DevInst>, SigSession*>::iterator i =
19adbc2c
JH
157 _used_devices.begin(); i != _used_devices.end(); i++)
158 if((*i).first->dev_inst()->driver == driver)
dc0867ff
JH
159 {
160 // Notify the current owner of the device
161 (*i).second->release_device((*i).first);
162
163 // Remove it from the used device list
164 _used_devices.erase(i);
165
166 // Close the device instance
19adbc2c 167 sr_dev_close((*i).first->dev_inst());
dc0867ff
JH
168 }
169
170 // Clear all the old device instances from this driver
171 sr_dev_clear(driver);
172}
173
94574501
JH
174bool DeviceManager::compare_devices(shared_ptr<device::DevInst> a,
175 shared_ptr<device::DevInst> b)
107ca6d3 176{
19adbc2c
JH
177 assert(a);
178 assert(b);
179 return a->format_device_title().compare(b->format_device_title()) < 0;
107ca6d3
JH
180}
181
182} // namespace pv