]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
9663c82b JH |
18 | */ |
19 | ||
f9abf97e JH |
20 | #include <cassert> |
21 | ||
fe3a1c21 | 22 | #include <libsigrokcxx/libsigrokcxx.hpp> |
19adbc2c | 23 | |
1ebe875c | 24 | #include <QGroupBox> |
aca9aa83 | 25 | #include <QLabel> |
1ebe875c | 26 | #include <QRadioButton> |
26f209b7 | 27 | |
2acdb232 | 28 | #include "connect.hpp" |
9663c82b | 29 | |
da30ecb7 JH |
30 | #include <pv/devicemanager.hpp> |
31 | #include <pv/devices/hardwaredevice.hpp> | |
9663c82b | 32 | |
819f4c25 | 33 | using std::list; |
e8d00928 | 34 | using std::map; |
f9abf97e | 35 | using std::shared_ptr; |
819f4c25 | 36 | using std::string; |
107ca6d3 | 37 | |
e8d00928 ML |
38 | using Glib::ustring; |
39 | using Glib::Variant; | |
40 | using Glib::VariantBase; | |
41 | ||
42 | using sigrok::ConfigKey; | |
43 | using sigrok::Driver; | |
da30ecb7 JH |
44 | |
45 | using pv::devices::HardwareDevice; | |
c9de8b32 | 46 | |
9663c82b JH |
47 | namespace pv { |
48 | namespace dialogs { | |
49 | ||
107ca6d3 | 50 | Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : |
9663c82b | 51 | QDialog(parent), |
8dbbc7f0 JH |
52 | device_manager_(device_manager), |
53 | layout_(this), | |
54 | form_(this), | |
55 | form_layout_(&form_), | |
56 | drivers_(&form_), | |
34750abe | 57 | serial_devices_(&form_), |
be7645e8 | 58 | scan_button_(tr("&Scan for devices using driver above"), this), |
8dbbc7f0 JH |
59 | device_list_(this), |
60 | button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | |
9663c82b JH |
61 | Qt::Horizontal, this) |
62 | { | |
63 | setWindowTitle(tr("Connect to Device")); | |
64 | ||
8dbbc7f0 JH |
65 | connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept())); |
66 | connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject())); | |
9663c82b JH |
67 | |
68 | populate_drivers(); | |
1ebe875c | 69 | connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int))); |
9663c82b | 70 | |
8dbbc7f0 | 71 | form_.setLayout(&form_layout_); |
9663c82b | 72 | |
1ebe875c SA |
73 | QVBoxLayout *vbox_drv = new QVBoxLayout; |
74 | vbox_drv->addWidget(&drivers_); | |
75 | QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver")); | |
76 | groupbox_drv->setLayout(vbox_drv); | |
77 | form_layout_.addRow(groupbox_drv); | |
78 | ||
79 | QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this); | |
80 | QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this); | |
81 | QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this); | |
9663c82b | 82 | |
1ebe875c SA |
83 | radiobtn_usb->setChecked(true); |
84 | ||
926934b8 SA |
85 | serial_config_ = new QWidget(); |
86 | QHBoxLayout *serial_config_layout = new QHBoxLayout(serial_config_); | |
87 | ||
1ebe875c | 88 | serial_devices_.setEditable(true); |
926934b8 SA |
89 | serial_devices_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
90 | ||
91 | serial_baudrate_.setEditable(true); | |
92 | serial_baudrate_.addItem(""); | |
93 | serial_baudrate_.addItem("921600"); | |
94 | serial_baudrate_.addItem("115200"); | |
95 | serial_baudrate_.addItem("57600"); | |
96 | serial_baudrate_.addItem("19200"); | |
97 | serial_baudrate_.addItem("9600"); | |
98 | ||
99 | serial_config_layout->addWidget(&serial_devices_); | |
100 | serial_config_layout->addWidget(&serial_baudrate_); | |
101 | serial_config_layout->addWidget(new QLabel("baud")); | |
7a5535dc | 102 | serial_config_->setEnabled(false); |
1ebe875c SA |
103 | |
104 | tcp_config_ = new QWidget(); | |
105 | QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_); | |
106 | tcp_host_ = new QLineEdit; | |
107 | tcp_host_->setText("192.168.1.100"); | |
108 | tcp_config_layout->addWidget(tcp_host_); | |
109 | tcp_config_layout->addWidget(new QLabel(":")); | |
110 | tcp_port_ = new QSpinBox; | |
111 | tcp_port_->setRange(1, 65535); | |
112 | tcp_port_->setValue(5555); | |
113 | tcp_config_layout->addWidget(tcp_port_); | |
521bac13 | 114 | |
1ebe875c | 115 | tcp_config_layout->addSpacing(30); |
521bac13 SA |
116 | tcp_config_layout->addWidget(new QLabel(tr("Protocol:"))); |
117 | tcp_protocol_ = new QComboBox(); | |
118 | tcp_protocol_->addItem("Raw TCP", QVariant("tcp-raw/%1/%2")); | |
119 | tcp_protocol_->addItem("VXI", QVariant("vxi/%1/%2")); | |
120 | tcp_config_layout->addWidget(tcp_protocol_); | |
1ebe875c SA |
121 | tcp_config_layout->setContentsMargins(0, 0, 0, 0); |
122 | tcp_config_->setEnabled(false); | |
123 | ||
76f33d4c SA |
124 | // Let the device list occupy only the minimum space needed |
125 | device_list_.setMaximumHeight(device_list_.minimumSizeHint().height()); | |
126 | ||
1ebe875c SA |
127 | QVBoxLayout *vbox_if = new QVBoxLayout; |
128 | vbox_if->addWidget(radiobtn_usb); | |
129 | vbox_if->addWidget(radiobtn_serial); | |
926934b8 | 130 | vbox_if->addWidget(serial_config_); |
1ebe875c SA |
131 | vbox_if->addWidget(radiobtn_tcp); |
132 | vbox_if->addWidget(tcp_config_); | |
133 | ||
134 | QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface")); | |
135 | groupbox_if->setLayout(vbox_if); | |
136 | form_layout_.addRow(groupbox_if); | |
137 | ||
138 | QVBoxLayout *vbox_scan = new QVBoxLayout; | |
139 | vbox_scan->addWidget(&scan_button_); | |
140 | QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices")); | |
141 | groupbox_scan->setLayout(vbox_scan); | |
142 | form_layout_.addRow(groupbox_scan); | |
143 | ||
144 | QVBoxLayout *vbox_select = new QVBoxLayout; | |
145 | vbox_select->addWidget(&device_list_); | |
146 | QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device")); | |
147 | groupbox_select->setLayout(vbox_select); | |
148 | form_layout_.addRow(groupbox_select); | |
26f209b7 | 149 | |
9663c82b JH |
150 | unset_connection(); |
151 | ||
1ebe875c SA |
152 | connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool))); |
153 | connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool))); | |
154 | connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed())); | |
c9de8b32 | 155 | |
8dbbc7f0 | 156 | setLayout(&layout_); |
1ebe875c | 157 | |
8dbbc7f0 | 158 | layout_.addWidget(&form_); |
8dbbc7f0 | 159 | layout_.addWidget(&button_box_); |
9663c82b JH |
160 | } |
161 | ||
e8d00928 | 162 | shared_ptr<HardwareDevice> Connect::get_selected_device() const |
5eb0fa13 | 163 | { |
8dbbc7f0 | 164 | const QListWidgetItem *const item = device_list_.currentItem(); |
5eb0fa13 | 165 | if (!item) |
e8d00928 | 166 | return shared_ptr<HardwareDevice>(); |
19adbc2c | 167 | |
e8d00928 | 168 | return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>(); |
5eb0fa13 JH |
169 | } |
170 | ||
9663c82b JH |
171 | void Connect::populate_drivers() |
172 | { | |
f4ab4b5c | 173 | for (auto& entry : device_manager_.context()->drivers()) { |
e8d00928 ML |
174 | auto name = entry.first; |
175 | auto driver = entry.second; | |
30938303 JH |
176 | /** |
177 | * We currently only support devices that can deliver | |
178 | * samples at a fixed samplerate i.e. oscilloscopes and | |
179 | * logic analysers. | |
180 | * @todo Add support for non-monotonic devices i.e. DMMs | |
181 | * and sensors. | |
182 | */ | |
7bb0fbf4 ML |
183 | const auto keys = driver->config_keys(); |
184 | ||
185 | bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) | | |
186 | keys.count(ConfigKey::OSCILLOSCOPE); | |
30938303 | 187 | |
793f8096 | 188 | if (supported_device) |
8dbbc7f0 | 189 | drivers_.addItem(QString("%1 (%2)").arg( |
744aa24f | 190 | driver->long_name().c_str(), name.c_str()), |
e8d00928 | 191 | qVariantFromValue(driver)); |
30938303 | 192 | } |
9663c82b JH |
193 | } |
194 | ||
34750abe AJ |
195 | void Connect::populate_serials(shared_ptr<Driver> driver) |
196 | { | |
197 | serial_devices_.clear(); | |
f4ab4b5c | 198 | for (auto& serial : device_manager_.context()->serials(driver)) |
34750abe | 199 | serial_devices_.addItem(QString("%1 (%2)").arg( |
744aa24f | 200 | serial.first.c_str(), serial.second.c_str()), |
34750abe AJ |
201 | QString::fromStdString(serial.first)); |
202 | } | |
203 | ||
c9de8b32 JH |
204 | void Connect::unset_connection() |
205 | { | |
8dbbc7f0 | 206 | device_list_.clear(); |
8dbbc7f0 | 207 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(true); |
c9de8b32 JH |
208 | } |
209 | ||
1ebe875c | 210 | void Connect::serial_toggled(bool checked) |
c9de8b32 | 211 | { |
1ebe875c | 212 | serial_devices_.setEnabled(checked); |
926934b8 | 213 | serial_baudrate_.setEnabled(checked); |
58abb3a3 | 214 | serial_config_->setEnabled(checked); |
c9de8b32 JH |
215 | } |
216 | ||
1ebe875c | 217 | void Connect::tcp_toggled(bool checked) |
26f209b7 | 218 | { |
1ebe875c | 219 | tcp_config_->setEnabled(checked); |
26f209b7 | 220 | } |
221 | ||
c9de8b32 JH |
222 | void Connect::scan_pressed() |
223 | { | |
8dbbc7f0 | 224 | device_list_.clear(); |
c9de8b32 | 225 | |
8dbbc7f0 | 226 | const int index = drivers_.currentIndex(); |
793f8096 | 227 | if (index == -1) |
c9de8b32 JH |
228 | return; |
229 | ||
e8d00928 | 230 | shared_ptr<Driver> driver = |
8dbbc7f0 | 231 | drivers_.itemData(index).value<shared_ptr<Driver>>(); |
c9de8b32 | 232 | |
e8d00928 | 233 | assert(driver); |
c9de8b32 | 234 | |
e8d00928 | 235 | map<const ConfigKey *, VariantBase> drvopts; |
c9de8b32 | 236 | |
926934b8 | 237 | if (serial_config_->isEnabled()) { |
34750abe AJ |
238 | QString serial; |
239 | const int index = serial_devices_.currentIndex(); | |
240 | if (index >= 0 && index < serial_devices_.count() && | |
241 | serial_devices_.currentText() == serial_devices_.itemText(index)) | |
b571a8e7 | 242 | serial = serial_devices_.itemData(index).toString(); |
34750abe AJ |
243 | else |
244 | serial = serial_devices_.currentText(); | |
926934b8 | 245 | |
e8d00928 | 246 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( |
34750abe | 247 | serial.toUtf8().constData()); |
926934b8 SA |
248 | |
249 | // Set baud rate if specified | |
250 | if (serial_baudrate_.currentText().length() > 0) | |
251 | drvopts[ConfigKey::SERIALCOMM] = Variant<ustring>::create( | |
252 | QString("%1/8n1").arg(serial_baudrate_.currentText()).toUtf8().constData()); | |
34750abe | 253 | } |
c9de8b32 | 254 | |
1ebe875c SA |
255 | if (tcp_config_->isEnabled()) { |
256 | QString host = tcp_host_->text(); | |
257 | QString port = tcp_port_->text(); | |
258 | if (!host.isEmpty()) { | |
521bac13 SA |
259 | QString conn = |
260 | tcp_protocol_->itemData(tcp_protocol_->currentIndex()).toString(); | |
261 | ||
262 | conn = conn.arg(host, port); | |
1ebe875c | 263 | |
26f209b7 | 264 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( |
265 | conn.toUtf8().constData()); | |
266 | } | |
267 | } | |
268 | ||
da30ecb7 | 269 | const list< shared_ptr<HardwareDevice> > devices = |
8dbbc7f0 | 270 | device_manager_.driver_scan(driver, drvopts); |
c9de8b32 | 271 | |
f4ab4b5c | 272 | for (const shared_ptr<HardwareDevice>& device : devices) { |
e8d00928 | 273 | assert(device); |
19adbc2c | 274 | |
76f33d4c SA |
275 | QString text = QString::fromStdString(device->display_name(device_manager_)); |
276 | text += QString(" with %1 channels").arg(device->device()->channels().size()); | |
c9de8b32 | 277 | |
76f33d4c | 278 | QListWidgetItem *const item = new QListWidgetItem(text, &device_list_); |
e8d00928 | 279 | item->setData(Qt::UserRole, qVariantFromValue(device)); |
8dbbc7f0 | 280 | device_list_.addItem(item); |
c9de8b32 JH |
281 | } |
282 | ||
8dbbc7f0 JH |
283 | device_list_.setCurrentRow(0); |
284 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0); | |
c9de8b32 JH |
285 | } |
286 | ||
1ebe875c | 287 | void Connect::driver_selected(int index) |
9663c82b | 288 | { |
e8d00928 | 289 | shared_ptr<Driver> driver = |
8dbbc7f0 | 290 | drivers_.itemData(index).value<shared_ptr<Driver>>(); |
9663c82b JH |
291 | |
292 | unset_connection(); | |
293 | ||
1ebe875c | 294 | populate_serials(driver); |
9663c82b JH |
295 | } |
296 | ||
9663c82b JH |
297 | } // namespace dialogs |
298 | } // namespace pv |