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