]> sigrok.org Git - pulseview.git/blob - pv/devicemanager.cpp
Added decoder options binding for double values
[pulseview.git] / pv / devicemanager.cpp
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 "device/device.h"
23 #include "sigsession.h"
24
25 #include <cassert>
26 #include <stdexcept>
27 #include <string>
28
29 #include <boost/foreach.hpp>
30
31 #include <libsigrok/libsigrok.h>
32
33 using boost::shared_ptr;
34 using std::list;
35 using std::map;
36 using std::ostringstream;
37 using std::runtime_error;
38 using std::string;
39
40 namespace pv {
41
42 DeviceManager::DeviceManager(struct sr_context *sr_ctx) :
43         _sr_ctx(sr_ctx)
44 {
45         init_drivers();
46         scan_all_drivers();
47 }
48
49 DeviceManager::~DeviceManager()
50 {
51         release_devices();
52 }
53
54 const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
55 {
56         return _devices;
57 }
58
59 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
60         struct sr_dev_driver *const driver, GSList *const drvopts)
61 {
62         list< shared_ptr<device::Device> > driver_devices;
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.
68         list< shared_ptr<device::Device> >::iterator i = _devices.begin();
69         while (i != _devices.end()) {
70                 if ((*i)->dev_inst()->driver == driver)
71                         i = _devices.erase(i);
72                 else
73                         i++;
74         }
75
76         // Release this driver and all it's attached devices
77         release_driver(driver);
78
79         // Do the scan
80         GSList *const devices = sr_driver_scan(driver, drvopts);
81         for (GSList *l = devices; l; l = l->next)
82                 driver_devices.push_back(shared_ptr<device::Device>(
83                         new device::Device((sr_dev_inst*)l->data)));
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
95 void 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
108 void DeviceManager::release_devices()
109 {
110         // Release all the used devices
111         BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
112                 assert(dev);
113                 dev->release();
114         }
115
116         // Clear all the drivers
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
122 void 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
130 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
131 {
132         BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
133                 assert(dev);
134                 if(dev->dev_inst()->driver == driver)
135                         dev->release();
136         }
137
138         // Clear all the old device instances from this driver
139         sr_dev_clear(driver);
140 }
141
142 bool DeviceManager::compare_devices(shared_ptr<device::Device> a,
143         shared_ptr<device::Device> b)
144 {
145         assert(a);
146         assert(b);
147         return a->format_device_title().compare(b->format_device_title()) < 0;
148 }
149
150 } // namespace pv