* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "connect.h"
+#include <cassert>
-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 <libsigrok/libsigrok.hpp>
+
+#include "connect.hpp"
+
+#include "pv/devicemanager.hpp"
+
+using std::list;
+using std::map;
+using std::shared_ptr;
+using std::string;
+
+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),
- _layout(this),
- _form(this),
- _form_layout(&_form),
- _drivers(&_form),
- _serial_device(&_form),
- _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
+ 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,
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)),
+ connect(&drivers_, SIGNAL(activated(int)),
this, SLOT(device_selected(int)));
- _form.setLayout(&_form_layout);
- _form_layout.addRow(tr("Driver"), &_drivers);
+ form_.setLayout(&form_layout_);
+ form_layout_.addRow(tr("Driver"), &drivers_);
- _form_layout.addRow(tr("Serial Port"), &_serial_device);
+ form_layout_.addRow(tr("Serial Port"), &serial_device_);
unset_connection();
- setLayout(&_layout);
- _layout.addWidget(&_form);
- _layout.addWidget(&_button_box);
+ 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_);
+}
+
+shared_ptr<HardwareDevice> Connect::get_selected_device() const
+{
+ const QListWidgetItem *const item = device_list_.currentItem();
+ if (!item)
+ return shared_ptr<HardwareDevice>();
+
+ return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
}
void Connect::populate_drivers()
{
- struct sr_dev_driver **drivers = sr_driver_list();
- for (int i = 0; drivers[i]; ++i)
- _drivers.addItem(QString("%1 (%2)").arg(
- drivers[i]->longname).arg(drivers[i]->name),
- qVariantFromValue((void*)drivers[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
+ * logic analysers.
+ * @todo Add support for non-monotonic devices i.e. DMMs
+ * and sensors.
+ */
+ bool supported_device = driver->config_check(
+ ConfigKey::LOGIC_ANALYZER, ConfigKey::DEVICE_OPTIONS) |
+ driver->config_check(
+ ConfigKey::OSCILLOSCOPE, ConfigKey::DEVICE_OPTIONS);
+
+ if (supported_device)
+ drivers_.addItem(QString("%1 (%2)").arg(
+ driver->long_name().c_str()).arg(name.c_str()),
+ qVariantFromValue(driver));
+ }
}
-void Connect::device_selected(int index)
+void Connect::unset_connection()
{
- const int *hwopts;
- const struct sr_hwcap_option *hwo;
- sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
- index).value<void*>();
+ device_list_.clear();
+ serial_device_.hide();
+ form_layout_.labelForField(&serial_device_)->hide();
+ button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
+}
- unset_connection();
+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;
+
+ shared_ptr<Driver> driver =
+ drivers_.itemData(index).value<shared_ptr<Driver>>();
- if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS,
- (const void **)&hwopts, NULL) == SR_OK) && hwopts) {
+ assert(driver);
- for (int i = 0; hwopts[i]; i++) {
- switch(hwopts[i]) {
- case SR_CONF_SERIALCOMM:
- set_serial_connection();
- break;
+ map<const ConfigKey *, VariantBase> drvopts;
- default:
- continue;
- }
+ if (serial_device_.isVisible())
+ drvopts[ConfigKey::CONN] = Variant<ustring>::create(
+ serial_device_.text().toUtf8().constData());
- break;
- }
+ list< shared_ptr<HardwareDevice> > devices =
+ device_manager_.driver_scan(driver, drvopts);
+
+ for (shared_ptr<HardwareDevice> device : devices)
+ {
+ assert(device);
+
+ QString text = QString::fromStdString(
+ device_manager_.get_display_name(device));
+ text += QString(" with %1 channels").arg(device->channels().size());
+
+ QListWidgetItem *const item = new QListWidgetItem(text,
+ &device_list_);
+ item->setData(Qt::UserRole, qVariantFromValue(device));
+ device_list_.addItem(item);
}
-}
-void Connect::unset_connection()
-{
- _serial_device.hide();
- _form_layout.labelForField(&_serial_device)->hide();
+ device_list_.setCurrentRow(0);
+ button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
}
-void Connect::set_serial_connection()
+void Connect::device_selected(int index)
{
- _serial_device.show();
- _form_layout.labelForField(&_serial_device)->show();
-}
+ shared_ptr<Driver> driver =
+ drivers_.itemData(index).value<shared_ptr<Driver>>();
+
+ unset_connection();
+ if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
+ set_serial_connection();
+}
} // namespace dialogs
} // namespace pv