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