2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
25 #define __STDC_FORMAT_MACROS
27 #include <libsigrok/libsigrok.h>
30 extern sr_context *sr_ctx;
35 Connect::Connect(QWidget *parent) :
41 _serial_device(&_form),
42 _scan_button(tr("Scan for Devices"), this),
44 _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
47 setWindowTitle(tr("Connect to Device"));
49 connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
50 connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
53 connect(&_drivers, SIGNAL(activated(int)),
54 this, SLOT(device_selected(int)));
56 _form.setLayout(&_form_layout);
57 _form_layout.addRow(tr("Driver"), &_drivers);
59 _form_layout.addRow(tr("Serial Port"), &_serial_device);
63 connect(&_scan_button, SIGNAL(pressed()),
64 this, SLOT(scan_pressed()));
67 _layout.addWidget(&_form);
68 _layout.addWidget(&_scan_button);
69 _layout.addWidget(&_device_list);
70 _layout.addWidget(&_button_box);
73 struct sr_dev_inst* Connect::get_selected_device() const
75 const QListWidgetItem *const item = _device_list.currentItem();
79 return (sr_dev_inst*)item->data(Qt::UserRole).value<void*>();
82 void Connect::populate_drivers()
85 const int32_t *hwopts;
86 struct sr_dev_driver **drivers = sr_driver_list();
89 for (int i = 0; drivers[i]; ++i) {
91 * We currently only support devices that can deliver
92 * samples at a fixed samplerate i.e. oscilloscopes and
94 * @todo Add support for non-monotonic devices i.e. DMMs
97 bool supported_device = false;
98 if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS,
99 &gvar_opts, NULL) == SR_OK))
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++)
103 if (hwopts[j] == SR_CONF_SAMPLERATE) {
104 supported_device = true;
108 if (supported_device)
109 _drivers.addItem(QString("%1 (%2)").arg(
110 drivers[i]->longname).arg(drivers[i]->name),
111 qVariantFromValue((void*)drivers[i]));
115 void Connect::unset_connection()
117 _device_list.clear();
118 _serial_device.hide();
119 _form_layout.labelForField(&_serial_device)->hide();
120 _button_box.button(QDialogButtonBox::Ok)->setDisabled(true);
123 void Connect::set_serial_connection()
125 _serial_device.show();
126 _form_layout.labelForField(&_serial_device)->show();
129 void Connect::scan_pressed()
131 _device_list.clear();
133 const int index = _drivers.currentIndex();
137 sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
138 index).value<void*>();
140 GSList *drvopts = NULL;
142 if (_serial_device.isVisible()) {
143 sr_config *const src = (sr_config*)g_try_malloc(sizeof(sr_config));
144 src->key = SR_CONF_CONN;
145 const QByteArray byteArray = _serial_device.text().toUtf8();
146 src->data = g_variant_new_string((const gchar*)byteArray.constData());
147 drvopts = g_slist_append(drvopts, src);
150 GSList *const devices = sr_driver_scan(driver, drvopts);
152 for (GSList *l = devices; l; l = l->next) {
154 sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
157 if (sdi->vendor && sdi->vendor[0])
158 text += QString("%1 ").arg(sdi->vendor);
159 if (sdi->model && sdi->model[0])
160 text += QString("%1 ").arg(sdi->model);
161 if (sdi->version && sdi->version[0])
162 text += QString("%1 ").arg(sdi->version);
164 text += QString("with %1 probes").arg(
165 g_slist_length(sdi->probes));
168 QListWidgetItem *const item = new QListWidgetItem(text,
170 item->setData(Qt::UserRole, qVariantFromValue((void*)sdi));
171 _device_list.addItem(item);
174 g_slist_free(devices);
175 g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
177 _device_list.setCurrentRow(0);
178 _button_box.button(QDialogButtonBox::Ok)->setDisabled(false);
181 void Connect::device_selected(int index)
184 const int32_t *hwopts;
186 sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
187 index).value<void*>();
191 if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS,
192 &gvar_list, NULL) == SR_OK)) {
193 hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list,
194 &num_opts, sizeof(int32_t));
196 for (unsigned int i = 0; i < num_opts; i++) {
198 case SR_CONF_SERIALCOMM:
199 set_serial_connection();
208 g_variant_unref(gvar_list);
212 void Connect::free_drvopts(struct sr_config *src)
214 g_variant_unref(src->data);
218 } // namespace dialogs