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