]> sigrok.org Git - pulseview.git/blobdiff - pv/dialogs/connect.cpp
Fix random clazy warnings
[pulseview.git] / pv / dialogs / connect.cpp
index ea411f156cf33b52f4f86ed61db2ed017a7418f4..084b1142c198c2b24b82ae3da4a00590f3d042c2 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <boost/foreach.hpp>
+#include <cassert>
 
-#include "connect.h"
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
-#include "pv/devicemanager.h"
+#include <QGroupBox>
+#include <QLabel>
+#include <QRadioButton>
 
-extern "C" {
-/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
-#define __STDC_FORMAT_MACROS
-#include <glib.h>
-#include <libsigrok/libsigrok.h>
-}
+#include "connect.hpp"
+
+#include <pv/devicemanager.hpp>
+#include <pv/devices/hardwaredevice.hpp>
+
+using std::list;
+using std::map;
+using std::shared_ptr;
+using std::string;
+
+using Glib::ustring;
+using Glib::Variant;
+using Glib::VariantBase;
 
-using namespace std;
+using sigrok::ConfigKey;
+using sigrok::Driver;
 
-extern sr_context *sr_ctx;
+using pv::devices::HardwareDevice;
 
 namespace pv {
 namespace dialogs {
 
 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
        QDialog(parent),
-       _device_manager(device_manager),
-       _layout(this),
-       _form(this),
-       _form_layout(&_form),
-       _drivers(&_form),
-       _serial_device(&_form),
-       _scan_button(tr("Scan for Devices"), this),
-       _device_list(this),
-       _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
+       device_manager_(device_manager),
+       layout_(this),
+       form_(this),
+       form_layout_(&form_),
+       drivers_(&form_),
+       serial_devices_(&form_),
+       scan_button_(tr("&Scan for devices using driver above"), this),
+       device_list_(this),
+       button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                Qt::Horizontal, this)
 {
        setWindowTitle(tr("Connect to Device"));
 
-       connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
-       connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
+       connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
+       connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
 
        populate_drivers();
-       connect(&_drivers, SIGNAL(activated(int)),
-               this, SLOT(device_selected(int)));
-
-       _form.setLayout(&_form_layout);
-       _form_layout.addRow(tr("Driver"), &_drivers);
-
-       _form_layout.addRow(tr("Serial Port"), &_serial_device);
+       connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
+
+       form_.setLayout(&form_layout_);
+
+       QVBoxLayout *vbox_drv = new QVBoxLayout;
+       vbox_drv->addWidget(&drivers_);
+       QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver"));
+       groupbox_drv->setLayout(vbox_drv);
+       form_layout_.addRow(groupbox_drv);
+
+       QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this);
+       QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this);
+       QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this);
+
+       radiobtn_usb->setChecked(true);
+
+       serial_devices_.setEditable(true);
+       serial_devices_.setEnabled(false);
+
+       tcp_config_ = new QWidget();
+       QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_);
+       tcp_host_ = new QLineEdit;
+       tcp_host_->setText("192.168.1.100");
+       tcp_config_layout->addWidget(tcp_host_);
+       tcp_config_layout->addWidget(new QLabel(":"));
+       tcp_port_ = new QSpinBox;
+       tcp_port_->setRange(1, 65535);
+       tcp_port_->setValue(5555);
+       tcp_config_layout->addWidget(tcp_port_);
+
+       tcp_config_layout->addSpacing(30);
+       tcp_config_layout->addWidget(new QLabel(tr("Protocol:")));
+       tcp_protocol_ = new QComboBox();
+       tcp_protocol_->addItem("Raw TCP", QVariant("tcp-raw/%1/%2"));
+       tcp_protocol_->addItem("VXI", QVariant("vxi/%1/%2"));
+       tcp_config_layout->addWidget(tcp_protocol_);
+       tcp_config_layout->setContentsMargins(0, 0, 0, 0);
+       tcp_config_->setEnabled(false);
+
+       // Let the device list occupy only the minimum space needed
+       device_list_.setMaximumHeight(device_list_.minimumSizeHint().height());
+
+       QVBoxLayout *vbox_if = new QVBoxLayout;
+       vbox_if->addWidget(radiobtn_usb);
+       vbox_if->addWidget(radiobtn_serial);
+       vbox_if->addWidget(&serial_devices_);
+       vbox_if->addWidget(radiobtn_tcp);
+       vbox_if->addWidget(tcp_config_);
+
+       QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
+       groupbox_if->setLayout(vbox_if);
+       form_layout_.addRow(groupbox_if);
+
+       QVBoxLayout *vbox_scan = new QVBoxLayout;
+       vbox_scan->addWidget(&scan_button_);
+       QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices"));
+       groupbox_scan->setLayout(vbox_scan);
+       form_layout_.addRow(groupbox_scan);
+
+       QVBoxLayout *vbox_select = new QVBoxLayout;
+       vbox_select->addWidget(&device_list_);
+       QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device"));
+       groupbox_select->setLayout(vbox_select);
+       form_layout_.addRow(groupbox_select);
 
        unset_connection();
 
-       connect(&_scan_button, SIGNAL(pressed()),
-               this, SLOT(scan_pressed()));
+       connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool)));
+       connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool)));
+       connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed()));
 
-       setLayout(&_layout);
-       _layout.addWidget(&_form);
-       _layout.addWidget(&_scan_button);
-       _layout.addWidget(&_device_list);
-       _layout.addWidget(&_button_box);
+       setLayout(&layout_);
+
+       layout_.addWidget(&form_);
+       layout_.addWidget(&button_box_);
 }
 
-struct sr_dev_inst* Connect::get_selected_device() const
+shared_ptr<HardwareDevice> Connect::get_selected_device() const
 {
-       const QListWidgetItem *const item = _device_list.currentItem();
+       const QListWidgetItem *const item = device_list_.currentItem();
        if (!item)
-               return NULL;
+               return shared_ptr<HardwareDevice>();
 
-       return (sr_dev_inst*)item->data(Qt::UserRole).value<void*>();
+       return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
 }
 
 void Connect::populate_drivers()
 {
-       gsize num_opts = 0;
-       const int32_t *hwopts;
-       struct sr_dev_driver **drivers = sr_driver_list();
-       GVariant *gvar_opts;
-
-       for (int i = 0; drivers[i]; ++i) {
+       for (auto& entry : device_manager_.context()->drivers()) {
+               auto name = entry.first;
+               auto driver = entry.second;
                /**
                 * We currently only support devices that can deliver
                 * samples at a fixed samplerate i.e. oscilloscopes and
@@ -101,119 +164,110 @@ void Connect::populate_drivers()
                 * @todo Add support for non-monotonic devices i.e. DMMs
                 * and sensors.
                 */
-               bool supported_device = false;
-               if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS,
-                               &gvar_opts, NULL) == SR_OK)) {
-                       hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_opts,
-                                       &num_opts, sizeof(int32_t));
-                       for (unsigned int j = 0; j < num_opts; j++)
-                               if (hwopts[j] == SR_CONF_SAMPLERATE) {
-                                       supported_device = true;
-                                       break;
-                               }
-               }
+               const auto keys = driver->config_keys();
+
+               bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
+                       keys.count(ConfigKey::OSCILLOSCOPE);
 
                if (supported_device)
-                       _drivers.addItem(QString("%1 (%2)").arg(
-                               drivers[i]->longname).arg(drivers[i]->name),
-                               qVariantFromValue((void*)drivers[i]));
+                       drivers_.addItem(QString("%1 (%2)").arg(
+                               driver->long_name().c_str(), name.c_str()),
+                               qVariantFromValue(driver));
        }
 }
 
+void Connect::populate_serials(shared_ptr<Driver> driver)
+{
+       serial_devices_.clear();
+       for (auto& serial : device_manager_.context()->serials(driver))
+               serial_devices_.addItem(QString("%1 (%2)").arg(
+                       serial.first.c_str(), serial.second.c_str()),
+                       QString::fromStdString(serial.first));
+}
+
 void Connect::unset_connection()
 {
-       _device_list.clear();
-       _serial_device.hide();
-       _form_layout.labelForField(&_serial_device)->hide();
-       _button_box.button(QDialogButtonBox::Ok)->setDisabled(true);
+       device_list_.clear();
+       button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
 }
 
-void Connect::set_serial_connection()
+void Connect::serial_toggled(bool checked)
 {
-       _serial_device.show();
-       _form_layout.labelForField(&_serial_device)->show();
+       serial_devices_.setEnabled(checked);
+}
+
+void Connect::tcp_toggled(bool checked)
+{
+       tcp_config_->setEnabled(checked);
 }
 
 void Connect::scan_pressed()
 {
-       _device_list.clear();
+       device_list_.clear();
 
-       const int index = _drivers.currentIndex();
+       const int index = drivers_.currentIndex();
        if (index == -1)
                return;
 
-       sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
-               index).value<void*>();
+       shared_ptr<Driver> driver =
+               drivers_.itemData(index).value<shared_ptr<Driver>>();
+
+       assert(driver);
 
-       GSList *drvopts = NULL;
+       map<const ConfigKey *, VariantBase> drvopts;
 
-       if (_serial_device.isVisible()) {
-               sr_config *const src = (sr_config*)g_try_malloc(sizeof(sr_config));
-               src->key = SR_CONF_CONN;
-               const QByteArray byteArray = _serial_device.text().toUtf8();
-               src->data = g_variant_new_string((const gchar*)byteArray.constData());
-               drvopts = g_slist_append(drvopts, src);
+       if (serial_devices_.isEnabled()) {
+               QString serial;
+               const int index = serial_devices_.currentIndex();
+               if (index >= 0 && index < serial_devices_.count() &&
+                   serial_devices_.currentText() == serial_devices_.itemText(index))
+                       serial = serial_devices_.itemData(index).toString();
+               else
+                       serial = serial_devices_.currentText();
+               drvopts[ConfigKey::CONN] = Variant<ustring>::create(
+                       serial.toUtf8().constData());
        }
 
-       const list<sr_dev_inst*> devices = _device_manager.driver_scan(
-               driver, drvopts);
+       if (tcp_config_->isEnabled()) {
+               QString host = tcp_host_->text();
+               QString port = tcp_port_->text();
+               if (!host.isEmpty()) {
+                       QString conn =
+                               tcp_protocol_->itemData(tcp_protocol_->currentIndex()).toString();
 
-       g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
+                       conn = conn.arg(host, port);
 
-       BOOST_FOREACH(sr_dev_inst *const sdi, devices)
-       {
-               const string title = DeviceManager::format_device_title(sdi);
-               QString text(title.c_str());
-               if (sdi->probes) {
-                       text += QString(" with %1 probes").arg(
-                               g_slist_length(sdi->probes));
+                       drvopts[ConfigKey::CONN] = Variant<ustring>::create(
+                               conn.toUtf8().constData());
                }
-
-               QListWidgetItem *const item = new QListWidgetItem(text,
-                       &_device_list);
-               item->setData(Qt::UserRole, qVariantFromValue((void*)sdi));
-               _device_list.addItem(item);
        }
 
-       _device_list.setCurrentRow(0);
-       _button_box.button(QDialogButtonBox::Ok)->setDisabled(false);
-}
-
-void Connect::device_selected(int index)
-{
-       gsize num_opts = 0;
-       const int32_t *hwopts;
-       GVariant *gvar_list;
-       sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
-               index).value<void*>();
-
-       unset_connection();
-
-       if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS,
-                               &gvar_list, NULL) == SR_OK)) {
-               hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list,
-                               &num_opts, sizeof(int32_t));
+       const list< shared_ptr<HardwareDevice> > devices =
+               device_manager_.driver_scan(driver, drvopts);
 
-               for (unsigned int i = 0; i < num_opts; i++) {
-                       switch(hwopts[i]) {
-                       case SR_CONF_SERIALCOMM:
-                               set_serial_connection();
-                               break;
+       for (const shared_ptr<HardwareDevice>& device : devices) {
+               assert(device);
 
-                       default:
-                               continue;
-                       }
+               QString text = QString::fromStdString(device->display_name(device_manager_));
+               text += QString(" with %1 channels").arg(device->device()->channels().size());
 
-                       break;
-               }
-               g_variant_unref(gvar_list);
+               QListWidgetItem *const item = new QListWidgetItem(text, &device_list_);
+               item->setData(Qt::UserRole, qVariantFromValue(device));
+               device_list_.addItem(item);
        }
+
+       device_list_.setCurrentRow(0);
+       button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
 }
 
-void Connect::free_drvopts(struct sr_config *src)
+void Connect::driver_selected(int index)
 {
-       g_variant_unref(src->data);
-       g_free(src);
+       shared_ptr<Driver> driver =
+               drivers_.itemData(index).value<shared_ptr<Driver>>();
+
+       unset_connection();
+
+       populate_serials(driver);
 }
 
 } // namespace dialogs