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