Use libsigrok C++ bindings (patch version 7).
[pulseview.git] / pv / dialogs / connect.cpp
index 51de735e00e1d5e24eb1ad6d5e0dab13ee877f15..19dfde8d1a3f227065e39cd1499b2300212f3d8a 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <cassert>
+
+#include <libsigrok/libsigrok.hpp>
+
 #include "connect.h"
 
-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 "pv/devicemanager.h"
+
+using std::list;
+using std::map;
+using std::shared_ptr;
+using std::string;
 
-extern sr_context *sr_ctx;
+using Glib::ustring;
+using Glib::Variant;
+using Glib::VariantBase;
+
+using sigrok::ConfigKey;
+using sigrok::Driver;
+using sigrok::Error;
+using sigrok::HardwareDevice;
 
 namespace pv {
 namespace dialogs {
 
-Connect::Connect(QWidget *parent) :
+Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
        QDialog(parent),
+       _device_manager(device_manager),
        _layout(this),
        _form(this),
        _form_layout(&_form),
@@ -70,20 +82,20 @@ Connect::Connect(QWidget *parent) :
        _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();
        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()
 {
-       const int *hwopts;
-       struct sr_dev_driver **drivers = sr_driver_list();
-       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
@@ -91,19 +103,13 @@ 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,
-                       (const void **)&hwopts, NULL) == SR_OK) && hwopts)
-                       for (int j = 0; hwopts[j]; j++)
-                               if(hwopts[j] == SR_CONF_SAMPLERATE) {
-                                       supported_device = true;
-                                       break;
-                               }
-
-               if(supported_device)
+               bool supported_device = driver->config_check(
+                       ConfigKey::SAMPLERATE, ConfigKey::DEVICE_OPTIONS);
+
+               if (supported_device)
                        _drivers.addItem(QString("%1 (%2)").arg(
-                               drivers[i]->longname).arg(drivers[i]->name),
-                               qVariantFromValue((void*)drivers[i]));
+                               driver->long_name().c_str()).arg(name.c_str()),
+                               qVariantFromValue(driver));
        }
 }
 
@@ -126,83 +132,50 @@ void Connect::scan_pressed()
        _device_list.clear();
 
        const int index = _drivers.currentIndex();
-       if(index == -1)
+       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>>();
 
-       GSList *drvopts = NULL;
+       assert(driver);
 
-       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->value = g_strdup((const gchar*)byteArray.constData());
-               drvopts = g_slist_append(drvopts, src);
-       }
+       map<const ConfigKey *, VariantBase> drvopts;
 
-       GSList *const devices = sr_driver_scan(driver, drvopts);
+       if (_serial_device.isVisible())
+               drvopts[ConfigKey::CONN] = Variant<ustring>::create(
+                       _serial_device.text().toUtf8().constData());
 
-       for (GSList *l = devices; l; l = l->next) {
+       list< shared_ptr<HardwareDevice> > devices =
+               _device_manager.driver_scan(driver, drvopts);
 
-               sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
+       for (shared_ptr<HardwareDevice> device : devices)
+       {
+               assert(device);
 
-               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));
-               }
+               QString text = QString::fromStdString(
+                       _device_manager.device_description(device));
+               text += QString(" with %1 channels").arg(device->channels().size());
 
                QListWidgetItem *const item = new QListWidgetItem(text,
                        &_device_list);
-               item->setData(Qt::UserRole, qVariantFromValue((void*)sdi));
+               item->setData(Qt::UserRole, qVariantFromValue(device));
                _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);
+       _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0);
 }
 
 void Connect::device_selected(int index)
 {
-       const int *hwopts;
-       sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
-               index).value<void*>();
+       shared_ptr<Driver> driver =
+               _drivers.itemData(index).value<shared_ptr<Driver>>();
 
        unset_connection();
 
-       if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS,
-               (const void **)&hwopts, NULL) == SR_OK) && hwopts) {
-
-               for (int i = 0; hwopts[i]; i++) {
-                       switch(hwopts[i]) {
-                       case SR_CONF_SERIALCOMM:
-                               set_serial_connection();
-                               break;
-
-                       default:
-                               continue;
-                       }
-
-                       break;
-               }
-       }
-}
-
-void Connect::free_drvopts(struct sr_config *src)
-{
-       g_free((void *)src->value);
-       g_free(src);
+       if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
+                       set_serial_connection();
 }
 
 } // namespace dialogs