]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
Replaced BOOST_FOREACH with C++11 range-based for loops
[pulseview.git] / pv / dialogs / connect.cpp
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 <libsigrok/libsigrok.h>
22
23 #include "connect.h"
24
25 #include "pv/devicemanager.h"
26 #include "pv/device/device.h"
27
28 extern "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
35 using boost::shared_ptr;
36 using std::list;
37 using std::string;
38
39 extern sr_context *sr_ctx;
40
41 namespace pv {
42 namespace dialogs {
43
44 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
45         QDialog(parent),
46         _device_manager(device_manager),
47         _layout(this),
48         _form(this),
49         _form_layout(&_form),
50         _drivers(&_form),
51         _serial_device(&_form),
52         _scan_button(tr("Scan for Devices"), this),
53         _device_list(this),
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
73         connect(&_scan_button, SIGNAL(pressed()),
74                 this, SLOT(scan_pressed()));
75
76         setLayout(&_layout);
77         _layout.addWidget(&_form);
78         _layout.addWidget(&_scan_button);
79         _layout.addWidget(&_device_list);
80         _layout.addWidget(&_button_box);
81 }
82
83 shared_ptr<device::Device> Connect::get_selected_device() const
84 {
85         const QListWidgetItem *const item = _device_list.currentItem();
86         if (!item)
87                 return shared_ptr<device::Device>();
88
89         const sr_dev_inst *const sdi = (sr_dev_inst*)item->data(
90                 Qt::UserRole).value<void*>();
91         assert(sdi);
92
93         const auto iter = _device_map.find(sdi);
94         assert(iter != _device_map.end());
95
96         return (*iter).second;
97 }
98
99 void Connect::populate_drivers()
100 {
101         gsize num_opts = 0;
102         const int32_t *hwopts;
103         struct sr_dev_driver **drivers = sr_driver_list();
104         GVariant *gvar_opts;
105
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;
115                 if ((sr_config_list(drivers[i], NULL, NULL,
116                                 SR_CONF_DEVICE_OPTIONS, &gvar_opts) == SR_OK)) {
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++)
120                                 if (hwopts[j] == SR_CONF_SAMPLERATE) {
121                                         supported_device = true;
122                                         break;
123                                 }
124                 }
125
126                 if (supported_device)
127                         _drivers.addItem(QString("%1 (%2)").arg(
128                                 drivers[i]->longname).arg(drivers[i]->name),
129                                 qVariantFromValue((void*)drivers[i]));
130         }
131 }
132
133 void Connect::unset_connection()
134 {
135         _device_list.clear();
136         _device_map.clear();
137         _serial_device.hide();
138         _form_layout.labelForField(&_serial_device)->hide();
139         _button_box.button(QDialogButtonBox::Ok)->setDisabled(true);
140 }
141
142 void Connect::set_serial_connection()
143 {
144         _serial_device.show();
145         _form_layout.labelForField(&_serial_device)->show();
146 }
147
148 void Connect::scan_pressed()
149 {
150         _device_list.clear();
151         _device_map.clear();
152
153         const int index = _drivers.currentIndex();
154         if (index == -1)
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();
166                 src->data = g_variant_new_string((const gchar*)byteArray.constData());
167                 drvopts = g_slist_append(drvopts, src);
168         }
169
170         const list< shared_ptr<device::Device> > devices =
171                 _device_manager.driver_scan(driver, drvopts);
172
173         g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
174
175         for (shared_ptr<device::Device> dev_inst : devices)
176         {
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();
182                 QString text = QString::fromUtf8(title.c_str());
183
184                 if (sdi->channels) {
185                         text += QString(" with %1 channels").arg(
186                                 g_slist_length(sdi->channels));
187                 }
188
189                 QListWidgetItem *const item = new QListWidgetItem(text,
190                         &_device_list);
191                 item->setData(Qt::UserRole, qVariantFromValue((void*)sdi));
192                 _device_list.addItem(item);
193                 _device_map[sdi] = dev_inst;
194         }
195
196         _device_list.setCurrentRow(0);
197         _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0);
198 }
199
200 void Connect::device_selected(int index)
201 {
202         gsize num_opts = 0;
203         const int32_t *hwopts;
204         GVariant *gvar_list;
205         sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
206                 index).value<void*>();
207
208         unset_connection();
209
210         if ((sr_config_list(driver, NULL, NULL,
211                                 SR_CONF_SCAN_OPTIONS, &gvar_list) == SR_OK)) {
212                 hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list,
213                                 &num_opts, sizeof(int32_t));
214
215                 for (unsigned int i = 0; i < num_opts; i++) {
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                 }
227                 g_variant_unref(gvar_list);
228         }
229 }
230
231 void Connect::free_drvopts(struct sr_config *src)
232 {
233         g_variant_unref(src->data);
234         g_free(src);
235 }
236
237 } // namespace dialogs
238 } // namespace pv