]>
Commit | Line | Data |
---|---|---|
9663c82b JH |
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 | ||
f9abf97e JH |
21 | #include <cassert> |
22 | ||
19adbc2c JH |
23 | #include <libsigrok/libsigrok.h> |
24 | ||
9663c82b JH |
25 | #include "connect.h" |
26 | ||
107ca6d3 | 27 | #include "pv/devicemanager.h" |
85843b14 | 28 | #include "pv/device/device.h" |
107ca6d3 | 29 | |
9663c82b JH |
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 | ||
819f4c25 | 37 | using std::list; |
f9abf97e | 38 | using std::shared_ptr; |
819f4c25 | 39 | using std::string; |
107ca6d3 | 40 | |
c9de8b32 JH |
41 | extern sr_context *sr_ctx; |
42 | ||
9663c82b JH |
43 | namespace pv { |
44 | namespace dialogs { | |
45 | ||
107ca6d3 | 46 | Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : |
9663c82b | 47 | QDialog(parent), |
107ca6d3 | 48 | _device_manager(device_manager), |
9663c82b JH |
49 | _layout(this), |
50 | _form(this), | |
51 | _form_layout(&_form), | |
52 | _drivers(&_form), | |
53 | _serial_device(&_form), | |
c9de8b32 JH |
54 | _scan_button(tr("Scan for Devices"), this), |
55 | _device_list(this), | |
9663c82b JH |
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 | ||
c9de8b32 JH |
75 | connect(&_scan_button, SIGNAL(pressed()), |
76 | this, SLOT(scan_pressed())); | |
77 | ||
9663c82b JH |
78 | setLayout(&_layout); |
79 | _layout.addWidget(&_form); | |
c9de8b32 JH |
80 | _layout.addWidget(&_scan_button); |
81 | _layout.addWidget(&_device_list); | |
9663c82b JH |
82 | _layout.addWidget(&_button_box); |
83 | } | |
84 | ||
85843b14 | 85 | shared_ptr<device::Device> Connect::get_selected_device() const |
5eb0fa13 JH |
86 | { |
87 | const QListWidgetItem *const item = _device_list.currentItem(); | |
88 | if (!item) | |
85843b14 | 89 | return shared_ptr<device::Device>(); |
19adbc2c JH |
90 | |
91 | const sr_dev_inst *const sdi = (sr_dev_inst*)item->data( | |
92 | Qt::UserRole).value<void*>(); | |
93 | assert(sdi); | |
5eb0fa13 | 94 | |
f46e495e | 95 | const auto iter = _device_map.find(sdi); |
19adbc2c JH |
96 | assert(iter != _device_map.end()); |
97 | ||
98 | return (*iter).second; | |
5eb0fa13 JH |
99 | } |
100 | ||
9663c82b JH |
101 | void Connect::populate_drivers() |
102 | { | |
2b82262f BV |
103 | gsize num_opts = 0; |
104 | const int32_t *hwopts; | |
9663c82b | 105 | struct sr_dev_driver **drivers = sr_driver_list(); |
2b82262f BV |
106 | GVariant *gvar_opts; |
107 | ||
30938303 JH |
108 | for (int i = 0; drivers[i]; ++i) { |
109 | /** | |
110 | * We currently only support devices that can deliver | |
111 | * samples at a fixed samplerate i.e. oscilloscopes and | |
112 | * logic analysers. | |
113 | * @todo Add support for non-monotonic devices i.e. DMMs | |
114 | * and sensors. | |
115 | */ | |
116 | bool supported_device = false; | |
68162c29 BV |
117 | if ((sr_config_list(drivers[i], NULL, NULL, |
118 | SR_CONF_DEVICE_OPTIONS, &gvar_opts) == SR_OK)) { | |
2b82262f BV |
119 | hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_opts, |
120 | &num_opts, sizeof(int32_t)); | |
121 | for (unsigned int j = 0; j < num_opts; j++) | |
793f8096 | 122 | if (hwopts[j] == SR_CONF_SAMPLERATE) { |
30938303 JH |
123 | supported_device = true; |
124 | break; | |
125 | } | |
ef6d8458 | 126 | } |
30938303 | 127 | |
793f8096 | 128 | if (supported_device) |
30938303 JH |
129 | _drivers.addItem(QString("%1 (%2)").arg( |
130 | drivers[i]->longname).arg(drivers[i]->name), | |
131 | qVariantFromValue((void*)drivers[i])); | |
132 | } | |
9663c82b JH |
133 | } |
134 | ||
c9de8b32 JH |
135 | void Connect::unset_connection() |
136 | { | |
137 | _device_list.clear(); | |
19adbc2c | 138 | _device_map.clear(); |
c9de8b32 JH |
139 | _serial_device.hide(); |
140 | _form_layout.labelForField(&_serial_device)->hide(); | |
f60ec39f | 141 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(true); |
c9de8b32 JH |
142 | } |
143 | ||
144 | void Connect::set_serial_connection() | |
145 | { | |
146 | _serial_device.show(); | |
147 | _form_layout.labelForField(&_serial_device)->show(); | |
148 | } | |
149 | ||
150 | void Connect::scan_pressed() | |
151 | { | |
152 | _device_list.clear(); | |
19adbc2c | 153 | _device_map.clear(); |
c9de8b32 JH |
154 | |
155 | const int index = _drivers.currentIndex(); | |
793f8096 | 156 | if (index == -1) |
c9de8b32 JH |
157 | return; |
158 | ||
159 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( | |
160 | index).value<void*>(); | |
161 | ||
162 | GSList *drvopts = NULL; | |
163 | ||
164 | if (_serial_device.isVisible()) { | |
165 | sr_config *const src = (sr_config*)g_try_malloc(sizeof(sr_config)); | |
166 | src->key = SR_CONF_CONN; | |
167 | const QByteArray byteArray = _serial_device.text().toUtf8(); | |
2b82262f | 168 | src->data = g_variant_new_string((const gchar*)byteArray.constData()); |
c9de8b32 JH |
169 | drvopts = g_slist_append(drvopts, src); |
170 | } | |
171 | ||
85843b14 | 172 | const list< shared_ptr<device::Device> > devices = |
94574501 | 173 | _device_manager.driver_scan(driver, drvopts); |
c9de8b32 | 174 | |
107ca6d3 | 175 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); |
c9de8b32 | 176 | |
d9aecf1f | 177 | for (shared_ptr<device::Device> dev_inst : devices) |
dd63af74 | 178 | { |
19adbc2c JH |
179 | assert(dev_inst); |
180 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
181 | assert(sdi); | |
182 | ||
183 | const string title = dev_inst->format_device_title(); | |
27e8df22 | 184 | QString text = QString::fromUtf8(title.c_str()); |
19adbc2c | 185 | |
4871ed92 | 186 | if (sdi->channels) { |
8bd26d8b | 187 | text += QString(" with %1 channels").arg( |
4871ed92 | 188 | g_slist_length(sdi->channels)); |
c9de8b32 JH |
189 | } |
190 | ||
5eb0fa13 JH |
191 | QListWidgetItem *const item = new QListWidgetItem(text, |
192 | &_device_list); | |
193 | item->setData(Qt::UserRole, qVariantFromValue((void*)sdi)); | |
194 | _device_list.addItem(item); | |
19adbc2c | 195 | _device_map[sdi] = dev_inst; |
c9de8b32 JH |
196 | } |
197 | ||
f60ec39f | 198 | _device_list.setCurrentRow(0); |
f4413378 | 199 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0); |
c9de8b32 JH |
200 | } |
201 | ||
9663c82b JH |
202 | void Connect::device_selected(int index) |
203 | { | |
2b82262f BV |
204 | gsize num_opts = 0; |
205 | const int32_t *hwopts; | |
206 | GVariant *gvar_list; | |
9663c82b JH |
207 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( |
208 | index).value<void*>(); | |
209 | ||
210 | unset_connection(); | |
211 | ||
68162c29 BV |
212 | if ((sr_config_list(driver, NULL, NULL, |
213 | SR_CONF_SCAN_OPTIONS, &gvar_list) == SR_OK)) { | |
2b82262f BV |
214 | hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list, |
215 | &num_opts, sizeof(int32_t)); | |
9663c82b | 216 | |
2b82262f | 217 | for (unsigned int i = 0; i < num_opts; i++) { |
9663c82b JH |
218 | switch(hwopts[i]) { |
219 | case SR_CONF_SERIALCOMM: | |
220 | set_serial_connection(); | |
221 | break; | |
222 | ||
223 | default: | |
224 | continue; | |
225 | } | |
226 | ||
227 | break; | |
228 | } | |
2b82262f | 229 | g_variant_unref(gvar_list); |
9663c82b JH |
230 | } |
231 | } | |
232 | ||
c9de8b32 | 233 | void Connect::free_drvopts(struct sr_config *src) |
9663c82b | 234 | { |
2b82262f | 235 | g_variant_unref(src->data); |
c9de8b32 | 236 | g_free(src); |
9663c82b JH |
237 | } |
238 | ||
9663c82b JH |
239 | } // namespace dialogs |
240 | } // namespace pv |