]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-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 <boost/foreach.hpp> | |
22 | ||
23 | #include <libsigrok/libsigrok.h> | |
24 | ||
25 | #include "connect.h" | |
26 | ||
27 | #include "pv/devicemanager.h" | |
28 | #include "pv/device/device.h" | |
29 | ||
30 | extern "C" { | |
31 | /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ | |
32 | #define __STDC_FORMAT_MACROS | |
33 | #include <glib.h> | |
34 | #include <libsigrok/libsigrok.h> | |
35 | } | |
36 | ||
37 | using boost::shared_ptr; | |
38 | using std::list; | |
39 | using std::string; | |
40 | ||
41 | extern sr_context *sr_ctx; | |
42 | ||
43 | namespace pv { | |
44 | namespace dialogs { | |
45 | ||
46 | Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : | |
47 | QDialog(parent), | |
48 | _device_manager(device_manager), | |
49 | _layout(this), | |
50 | _form(this), | |
51 | _form_layout(&_form), | |
52 | _drivers(&_form), | |
53 | _serial_device(&_form), | |
54 | _scan_button(tr("Scan for Devices"), this), | |
55 | _device_list(this), | |
56 | _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | |
57 | Qt::Horizontal, this) | |
58 | { | |
59 | setWindowTitle(tr("Connect to Device")); | |
60 | ||
61 | connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept())); | |
62 | connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject())); | |
63 | ||
64 | populate_drivers(); | |
65 | connect(&_drivers, SIGNAL(activated(int)), | |
66 | this, SLOT(device_selected(int))); | |
67 | ||
68 | _form.setLayout(&_form_layout); | |
69 | _form_layout.addRow(tr("Driver"), &_drivers); | |
70 | ||
71 | _form_layout.addRow(tr("Serial Port"), &_serial_device); | |
72 | ||
73 | unset_connection(); | |
74 | ||
75 | connect(&_scan_button, SIGNAL(pressed()), | |
76 | this, SLOT(scan_pressed())); | |
77 | ||
78 | setLayout(&_layout); | |
79 | _layout.addWidget(&_form); | |
80 | _layout.addWidget(&_scan_button); | |
81 | _layout.addWidget(&_device_list); | |
82 | _layout.addWidget(&_button_box); | |
83 | } | |
84 | ||
85 | shared_ptr<device::Device> Connect::get_selected_device() const | |
86 | { | |
87 | const QListWidgetItem *const item = _device_list.currentItem(); | |
88 | if (!item) | |
89 | return shared_ptr<device::Device>(); | |
90 | ||
91 | const sr_dev_inst *const sdi = (sr_dev_inst*)item->data( | |
92 | Qt::UserRole).value<void*>(); | |
93 | assert(sdi); | |
94 | ||
95 | std::map<const sr_dev_inst*, boost::shared_ptr<pv::device::Device> >:: | |
96 | const_iterator iter = _device_map.find(sdi); | |
97 | assert(iter != _device_map.end()); | |
98 | ||
99 | return (*iter).second; | |
100 | } | |
101 | ||
102 | void Connect::populate_drivers() | |
103 | { | |
104 | gsize num_opts = 0; | |
105 | const int32_t *hwopts; | |
106 | struct sr_dev_driver **drivers = sr_driver_list(); | |
107 | GVariant *gvar_opts; | |
108 | ||
109 | for (int i = 0; drivers[i]; ++i) { | |
110 | /** | |
111 | * We currently only support devices that can deliver | |
112 | * samples at a fixed samplerate i.e. oscilloscopes and | |
113 | * logic analysers. | |
114 | * @todo Add support for non-monotonic devices i.e. DMMs | |
115 | * and sensors. | |
116 | */ | |
117 | bool supported_device = false; | |
118 | if ((sr_config_list(drivers[i], NULL, NULL, | |
119 | SR_CONF_DEVICE_OPTIONS, &gvar_opts) == SR_OK)) { | |
120 | hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_opts, | |
121 | &num_opts, sizeof(int32_t)); | |
122 | for (unsigned int j = 0; j < num_opts; j++) | |
123 | if (hwopts[j] == SR_CONF_SAMPLERATE) { | |
124 | supported_device = true; | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | if (supported_device) | |
130 | _drivers.addItem(QString("%1 (%2)").arg( | |
131 | drivers[i]->longname).arg(drivers[i]->name), | |
132 | qVariantFromValue((void*)drivers[i])); | |
133 | } | |
134 | } | |
135 | ||
136 | void Connect::unset_connection() | |
137 | { | |
138 | _device_list.clear(); | |
139 | _device_map.clear(); | |
140 | _serial_device.hide(); | |
141 | _form_layout.labelForField(&_serial_device)->hide(); | |
142 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(true); | |
143 | } | |
144 | ||
145 | void Connect::set_serial_connection() | |
146 | { | |
147 | _serial_device.show(); | |
148 | _form_layout.labelForField(&_serial_device)->show(); | |
149 | } | |
150 | ||
151 | void Connect::scan_pressed() | |
152 | { | |
153 | _device_list.clear(); | |
154 | _device_map.clear(); | |
155 | ||
156 | const int index = _drivers.currentIndex(); | |
157 | if (index == -1) | |
158 | return; | |
159 | ||
160 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( | |
161 | index).value<void*>(); | |
162 | ||
163 | GSList *drvopts = NULL; | |
164 | ||
165 | if (_serial_device.isVisible()) { | |
166 | sr_config *const src = (sr_config*)g_try_malloc(sizeof(sr_config)); | |
167 | src->key = SR_CONF_CONN; | |
168 | const QByteArray byteArray = _serial_device.text().toUtf8(); | |
169 | src->data = g_variant_new_string((const gchar*)byteArray.constData()); | |
170 | drvopts = g_slist_append(drvopts, src); | |
171 | } | |
172 | ||
173 | const list< shared_ptr<device::Device> > devices = | |
174 | _device_manager.driver_scan(driver, drvopts); | |
175 | ||
176 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); | |
177 | ||
178 | BOOST_FOREACH(shared_ptr<device::Device> dev_inst, devices) | |
179 | { | |
180 | assert(dev_inst); | |
181 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
182 | assert(sdi); | |
183 | ||
184 | const string title = dev_inst->format_device_title(); | |
185 | QString text = QString::fromUtf8(title.c_str()); | |
186 | ||
187 | if (sdi->channels) { | |
188 | text += QString(" with %1 channels").arg( | |
189 | g_slist_length(sdi->channels)); | |
190 | } | |
191 | ||
192 | QListWidgetItem *const item = new QListWidgetItem(text, | |
193 | &_device_list); | |
194 | item->setData(Qt::UserRole, qVariantFromValue((void*)sdi)); | |
195 | _device_list.addItem(item); | |
196 | _device_map[sdi] = dev_inst; | |
197 | } | |
198 | ||
199 | _device_list.setCurrentRow(0); | |
200 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0); | |
201 | } | |
202 | ||
203 | void Connect::device_selected(int index) | |
204 | { | |
205 | gsize num_opts = 0; | |
206 | const int32_t *hwopts; | |
207 | GVariant *gvar_list; | |
208 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( | |
209 | index).value<void*>(); | |
210 | ||
211 | unset_connection(); | |
212 | ||
213 | if ((sr_config_list(driver, NULL, NULL, | |
214 | SR_CONF_SCAN_OPTIONS, &gvar_list) == SR_OK)) { | |
215 | hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list, | |
216 | &num_opts, sizeof(int32_t)); | |
217 | ||
218 | for (unsigned int i = 0; i < num_opts; i++) { | |
219 | switch(hwopts[i]) { | |
220 | case SR_CONF_SERIALCOMM: | |
221 | set_serial_connection(); | |
222 | break; | |
223 | ||
224 | default: | |
225 | continue; | |
226 | } | |
227 | ||
228 | break; | |
229 | } | |
230 | g_variant_unref(gvar_list); | |
231 | } | |
232 | } | |
233 | ||
234 | void Connect::free_drvopts(struct sr_config *src) | |
235 | { | |
236 | g_variant_unref(src->data); | |
237 | g_free(src); | |
238 | } | |
239 | ||
240 | } // namespace dialogs | |
241 | } // namespace pv |