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