]> sigrok.org Git - pulseview.git/blame_incremental - pv/devicemanager.cpp
Wrapped sr_dev_inst in a class: pv::DevInst
[pulseview.git] / pv / devicemanager.cpp
... / ...
CommitLineData
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 "devinst.h"
23#include "sigsession.h"
24
25#include <cassert>
26#include <stdexcept>
27#include <string>
28
29#include <libsigrok/libsigrok.h>
30
31using boost::shared_ptr;
32using std::list;
33using std::map;
34using std::ostringstream;
35using std::runtime_error;
36using std::string;
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
52const list< shared_ptr<pv::DevInst> >& DeviceManager::devices() const
53{
54 return _devices;
55}
56
57void DeviceManager::use_device(shared_ptr<DevInst> dev_inst, SigSession *owner)
58{
59 assert(dev_inst);
60 assert(owner);
61
62 _used_devices[dev_inst] = owner;
63
64 sr_dev_open(dev_inst->dev_inst());
65}
66
67void DeviceManager::release_device(shared_ptr<DevInst> dev_inst)
68{
69 assert(dev_inst);
70
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());
75
76 (*iter).second->release_device(dev_inst);
77 _used_devices.erase(dev_inst);
78}
79
80list< shared_ptr<DevInst> > DeviceManager::driver_scan(
81 struct sr_dev_driver *const driver, GSList *const drvopts)
82{
83 list< shared_ptr<DevInst> > driver_devices;
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.
89 list< shared_ptr<DevInst> >::iterator i = _devices.begin();
90 while (i != _devices.end()) {
91 if ((*i)->dev_inst()->driver == driver)
92 i = _devices.erase(i);
93 else
94 i++;
95 }
96
97 // Release this driver and all it's attached devices
98 release_driver(driver);
99
100 // Do the scan
101 GSList *const devices = sr_driver_scan(driver, drvopts);
102 for (GSList *l = devices; l; l = l->next)
103 driver_devices.push_back(shared_ptr<DevInst>(
104 new DevInst((sr_dev_inst*)l->data)));
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{
131 // Release all the used devices
132 for (map<shared_ptr<DevInst>, SigSession*>::iterator i =
133 _used_devices.begin(); i != _used_devices.end(); i++)
134 release_device((*i).first);
135
136 _used_devices.clear();
137
138 // Clear all the drivers
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
152void DeviceManager::release_driver(struct sr_dev_driver *const driver)
153{
154 assert(driver);
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)
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
166 sr_dev_close((*i).first->dev_inst());
167 }
168
169 // Clear all the old device instances from this driver
170 sr_dev_clear(driver);
171}
172
173bool DeviceManager::compare_devices(shared_ptr<DevInst> a,
174 shared_ptr<DevInst> b)
175{
176 assert(a);
177 assert(b);
178 return a->format_device_title().compare(b->format_device_title()) < 0;
179}
180
181} // namespace pv