]> sigrok.org Git - pulseview.git/blobdiff - pv/dialogs/connect.cpp
Adjust pv::dialogs::Connect to GVariant-based sr_config_* functions
[pulseview.git] / pv / dialogs / connect.cpp
index 284ce07240c2a279d247252aac081cab66333d07..e205c45047c8c07b97bdaa944577b8add97bdf10 100644 (file)
@@ -27,6 +27,8 @@ extern "C" {
 #include <libsigrok/libsigrok.h>
 }
 
+extern sr_context *sr_ctx;
+
 namespace pv {
 namespace dialogs {
 
@@ -37,6 +39,8 @@ Connect::Connect(QWidget *parent) :
        _form_layout(&_form),
        _drivers(&_form),
        _serial_device(&_form),
+       _scan_button(tr("Scan for Devices"), this),
+       _device_list(this),
        _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                Qt::Horizontal, this)
 {
@@ -56,15 +60,32 @@ Connect::Connect(QWidget *parent) :
 
        unset_connection();
 
+       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);
 }
 
+struct sr_dev_inst* Connect::get_selected_device() const
+{
+       const QListWidgetItem *const item = _device_list.currentItem();
+       if (!item)
+               return NULL;
+
+       return (sr_dev_inst*)item->data(Qt::UserRole).value<void*>();
+}
+
 void Connect::populate_drivers()
 {
-       const int *hwopts;
+       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) {
                /**
                 * We currently only support devices that can deliver
@@ -75,33 +96,104 @@ void Connect::populate_drivers()
                 */
                bool supported_device = false;
                if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS,
-                       (const void **)&hwopts, NULL) == SR_OK) && hwopts)
-                       for (int j = 0; hwopts[j]; j++)
-                               if(hwopts[j] == SR_CONF_SAMPLERATE) {
+                               &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;
                                }
 
-               if(supported_device)
+               if (supported_device)
                        _drivers.addItem(QString("%1 (%2)").arg(
                                drivers[i]->longname).arg(drivers[i]->name),
                                qVariantFromValue((void*)drivers[i]));
        }
 }
 
+void Connect::unset_connection()
+{
+       _device_list.clear();
+       _serial_device.hide();
+       _form_layout.labelForField(&_serial_device)->hide();
+       _button_box.button(QDialogButtonBox::Ok)->setDisabled(true);
+}
+
+void Connect::set_serial_connection()
+{
+       _serial_device.show();
+       _form_layout.labelForField(&_serial_device)->show();
+}
+
+void Connect::scan_pressed()
+{
+       _device_list.clear();
+
+       const int index = _drivers.currentIndex();
+       if (index == -1)
+               return;
+
+       sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
+               index).value<void*>();
+
+       GSList *drvopts = NULL;
+
+       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);
+       }
+
+       GSList *const devices = sr_driver_scan(driver, drvopts);
+
+       for (GSList *l = devices; l; l = l->next) {
+
+               sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
+
+               QString text;
+               if (sdi->vendor && sdi->vendor[0])
+                       text += QString("%1 ").arg(sdi->vendor);
+               if (sdi->model && sdi->model[0])
+                       text += QString("%1 ").arg(sdi->model);
+               if (sdi->version && sdi->version[0])
+                       text += QString("%1 ").arg(sdi->version);
+               if (sdi->probes) {
+                       text += QString("with %1 probes").arg(
+                               g_slist_length(sdi->probes));
+               }
+
+               QListWidgetItem *const item = new QListWidgetItem(text,
+                       &_device_list);
+               item->setData(Qt::UserRole, qVariantFromValue((void*)sdi));
+               _device_list.addItem(item);
+       }
+
+       g_slist_free(devices);
+       g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
+
+       _device_list.setCurrentRow(0);
+       _button_box.button(QDialogButtonBox::Ok)->setDisabled(false);
+}
+
 void Connect::device_selected(int index)
 {
-       const int *hwopts;
-       const struct sr_hwcap_option *hwo;
+       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,
-               (const void **)&hwopts, NULL) == SR_OK) && hwopts) {
+                               &gvar_list, NULL) == SR_OK)) {
+               hwopts = (const int32_t *)g_variant_get_fixed_array(gvar_list,
+                               &num_opts, sizeof(int32_t));
 
-               for (int i = 0; hwopts[i]; i++) {
+               for (unsigned int i = 0; i < num_opts; i++) {
                        switch(hwopts[i]) {
                        case SR_CONF_SERIALCOMM:
                                set_serial_connection();
@@ -113,21 +205,15 @@ void Connect::device_selected(int index)
 
                        break;
                }
+               g_variant_unref(gvar_list);
        }
 }
 
-void Connect::unset_connection()
-{
-       _serial_device.hide();
-       _form_layout.labelForField(&_serial_device)->hide();
-}
-
-void Connect::set_serial_connection()
+void Connect::free_drvopts(struct sr_config *src)
 {
-       _serial_device.show();
-       _form_layout.labelForField(&_serial_device)->show();
+       g_variant_unref(src->data);
+       g_free(src);
 }
 
-
 } // namespace dialogs
 } // namespace pv