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
23 #include <libsigrokcxx/libsigrokcxx.hpp>
27 #include "connect.hpp"
29 #include <pv/devicemanager.hpp>
30 #include <pv/devices/hardwaredevice.hpp>
34 using std::shared_ptr;
39 using Glib::VariantBase;
41 using sigrok::ConfigKey;
45 using pv::devices::HardwareDevice;
50 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
52 device_manager_(device_manager),
57 serial_devices_(&form_),
58 tcp_endpoint_(&form_),
59 tcp_endpoint_layout_(&tcp_endpoint_),
60 tcp_host_(&tcp_endpoint_),
61 tcp_port_(&tcp_endpoint_),
62 scan_button_(tr("&Scan for devices using driver above"), this),
64 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
67 setWindowTitle(tr("Connect to Device"));
69 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
70 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
73 connect(&drivers_, SIGNAL(activated(int)),
74 this, SLOT(device_selected(int)));
76 form_.setLayout(&form_layout_);
77 form_layout_.addRow(tr("&Driver"), &drivers_);
79 form_layout_.addRow(tr("Serial &Port"), &serial_devices_);
80 serial_devices_.setEditable(true);
82 tcp_host_.setPlaceholderText("192.168.1.100");
83 tcp_endpoint_layout_.addWidget(&tcp_host_);
84 tcp_endpoint_layout_.addWidget(new QLabel(":"));
85 tcp_port_.setRange(1, 65535);
86 tcp_port_.setValue(5555);
87 tcp_endpoint_layout_.addWidget(&tcp_port_);
88 tcp_endpoint_layout_.setContentsMargins(0, 0, 0, 0);
89 form_layout_.addRow(tr("TCP &Endpoint"), &tcp_endpoint_);
93 connect(&scan_button_, SIGNAL(pressed()),
94 this, SLOT(scan_pressed()));
97 layout_.addWidget(&form_);
98 layout_.addWidget(&scan_button_);
99 layout_.addWidget(&device_list_);
100 layout_.addWidget(&button_box_);
103 shared_ptr<HardwareDevice> Connect::get_selected_device() const
105 const QListWidgetItem *const item = device_list_.currentItem();
107 return shared_ptr<HardwareDevice>();
109 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
112 void Connect::populate_drivers()
114 for (auto entry : device_manager_.context()->drivers()) {
115 auto name = entry.first;
116 auto driver = entry.second;
118 * We currently only support devices that can deliver
119 * samples at a fixed samplerate i.e. oscilloscopes and
121 * @todo Add support for non-monotonic devices i.e. DMMs
124 const auto keys = driver->config_keys();
126 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
127 keys.count(ConfigKey::OSCILLOSCOPE);
129 if (supported_device)
130 drivers_.addItem(QString("%1 (%2)").arg(
131 driver->long_name().c_str(), name.c_str()),
132 qVariantFromValue(driver));
136 void Connect::populate_serials(shared_ptr<Driver> driver)
138 serial_devices_.clear();
139 for (auto serial : device_manager_.context()->serials(driver))
140 serial_devices_.addItem(QString("%1 (%2)").arg(
141 serial.first.c_str(), serial.second.c_str()),
142 QString::fromStdString(serial.first));
145 void Connect::unset_connection()
147 device_list_.clear();
148 serial_devices_.hide();
149 form_layout_.labelForField(&serial_devices_)->hide();
150 tcp_endpoint_.hide();
151 form_layout_.labelForField(&tcp_endpoint_)->hide();
152 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
155 void Connect::set_serial_connection(shared_ptr<Driver> driver)
157 populate_serials(driver);
158 serial_devices_.show();
159 form_layout_.labelForField(&serial_devices_)->show();
162 void Connect::set_tcp_connection(shared_ptr<Driver> driver)
166 tcp_endpoint_.show();
167 form_layout_.labelForField(&tcp_endpoint_)->show();
170 void Connect::scan_pressed()
172 device_list_.clear();
174 const int index = drivers_.currentIndex();
178 shared_ptr<Driver> driver =
179 drivers_.itemData(index).value<shared_ptr<Driver>>();
183 map<const ConfigKey *, VariantBase> drvopts;
185 if (serial_devices_.isVisible()) {
187 const int index = serial_devices_.currentIndex();
188 if (index >= 0 && index < serial_devices_.count() &&
189 serial_devices_.currentText() == serial_devices_.itemText(index))
190 serial = serial_devices_.itemData(index).value<QString>();
192 serial = serial_devices_.currentText();
193 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
194 serial.toUtf8().constData());
197 if (tcp_endpoint_.isVisible()) {
198 QString host = tcp_host_.text();
199 QString port = tcp_port_.text();
200 if(!host.isEmpty()) {
201 QString conn = QString("tcp-raw/%1/%2").arg(host, port);
202 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
203 conn.toUtf8().constData());
207 const list< shared_ptr<HardwareDevice> > devices =
208 device_manager_.driver_scan(driver, drvopts);
210 for (shared_ptr<HardwareDevice> device : devices) {
213 QString text = QString::fromStdString(
214 device->display_name(device_manager_));
215 text += QString(" with %1 channels").arg(
216 device->device()->channels().size());
218 QListWidgetItem *const item = new QListWidgetItem(text,
220 item->setData(Qt::UserRole, qVariantFromValue(device));
221 device_list_.addItem(item);
224 device_list_.setCurrentRow(0);
225 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
228 void Connect::device_selected(int index)
230 shared_ptr<Driver> driver =
231 drivers_.itemData(index).value<shared_ptr<Driver>>();
235 if (driver->scan_options().count(ConfigKey::SERIALCOMM))
236 set_serial_connection(driver);
238 if (driver->name() == "rigol-ds") // NBNB
239 set_tcp_connection(driver);
242 } // namespace dialogs