#include "device.h"
+using std::list;
+using std::make_pair;
+using std::map;
using std::ostringstream;
using std::string;
assert(_sdi);
- if (_sdi->vendor && _sdi->vendor[0]) {
- s << _sdi->vendor;
- if ((_sdi->model && _sdi->model[0]) ||
- (_sdi->version && _sdi->version[0]))
- s << ' ';
- }
+ if (_sdi->vendor && _sdi->vendor[0])
+ s << _sdi->vendor << " ";
- if (_sdi->model && _sdi->model[0]) {
- s << _sdi->model;
- if (_sdi->version && _sdi->version[0])
- s << ' ';
- }
+ if (_sdi->model && _sdi->model[0])
+ s << _sdi->model << " ";
if (_sdi->version && _sdi->version[0])
- s << _sdi->version;
+ s << _sdi->version << " ";
+
+ // Show connection string only if no serial number is present.
+ if (_sdi->serial_num && _sdi->serial_num[0])
+ s << "(" << _sdi->serial_num << ") ";
+ else if (_sdi->connection_id && _sdi->connection_id[0])
+ s << "[" << _sdi->connection_id << "] ";
+
+ // Remove trailing space.
+ s.seekp(-1, std::ios_base::end);
+ s << std::ends;
return s.str();
}
+map<string, string> Device::get_device_info() const
+{
+ map<string, string> result;
+
+ assert(_sdi);
+
+ if (_sdi->vendor && _sdi->vendor[0])
+ result.insert(make_pair("vendor", _sdi->vendor));
+
+ if (_sdi->model && _sdi->model[0])
+ result.insert(make_pair("model", _sdi->model));
+
+ if (_sdi->version && _sdi->version[0])
+ result.insert(make_pair("version", _sdi->version));
+
+ if (_sdi->serial_num && _sdi->serial_num[0])
+ result.insert(make_pair("serial_num", _sdi->serial_num));
+
+ if (_sdi->connection_id && _sdi->connection_id[0])
+ result.insert(make_pair("connection_id", _sdi->connection_id));
+
+ return result;
+}
+
} // device
} // pv
#ifndef PULSEVIEW_PV_DEVICE_DEVICE_H
#define PULSEVIEW_PV_DEVICE_DEVICE_H
+#include <map>
+#include <string>
+
#include "devinst.h"
namespace pv {
std::string format_device_title() const;
+ std::map<std::string, std::string> get_device_info() const;
+
private:
sr_dev_inst *const _sdi;
};
#ifndef PULSEVIEW_PV_DEVICE_DEVINST_H
#define PULSEVIEW_PV_DEVICE_DEVINST_H
+#include <map>
#include <memory>
#include <string>
virtual std::string format_device_title() const = 0;
+ virtual std::map<std::string, std::string> get_device_info() const = 0;
+
GVariant* get_config(const sr_channel_group *group, int key);
bool set_config(const sr_channel_group *group, int key, GVariant *data);
#include <libsigrok/libsigrok.h>
+using std::make_pair;
+using std::map;
using std::string;
namespace pv {
return boost::filesystem::path(_path).filename().string();
}
+map<string, string> File::get_device_info() const
+{
+ map<string, string> result;
+
+ result.insert(make_pair("vendor", "sigrok"));
+ result.insert(make_pair("model", "file"));
+ result.insert(make_pair("connection_id",
+ boost::filesystem::path(_path).filename().string()));
+
+ return result;
+}
+
File* File::create(const string &name)
{
if (sr_session_load(name.c_str(), &SigSession::_sr_session) == SR_OK) {
public:
std::string format_device_title() const;
+ std::map<std::string, std::string> get_device_info() const;
+
protected:
const std::string _path;
};
using std::list;
using std::map;
-using std::ostringstream;
using std::runtime_error;
using std::shared_ptr;
using std::string;
return driver_devices;
}
+const shared_ptr<device::Device> DeviceManager::find_device_from_info(
+ const map<string, string> search_info)
+{
+ shared_ptr<device::Device> last_resort_dev;
+ map<string, string> dev_info;
+
+ last_resort_dev = NULL;
+
+ for (shared_ptr<device::Device> dev : _devices) {
+ assert(dev);
+ dev_info = dev->get_device_info();
+
+ // If present, vendor and model always have to match.
+ if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
+ if (dev_info.at("vendor") != search_info.at("vendor")) continue;
+
+ if (dev_info.count("model") > 0 && search_info.count("model") > 0)
+ if (dev_info.at("model") != search_info.at("model")) continue;
+
+ // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
+ if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
+ && search_info.count("serial_num") > 0)
+ if (dev_info.at("serial_num") == search_info.at("serial_num") &&
+ dev_info.at("serial_num") != "0")
+ return dev;
+
+ // Second best match: vendor/model/connection_id
+ if (dev_info.count("connection_id") > 0 &&
+ search_info.count("connection_id") > 0)
+ if (dev_info.at("connection_id") == search_info.at("connection_id"))
+ return dev;
+
+ // Last resort: vendor/model/version
+ if (dev_info.count("version") > 0 &&
+ search_info.count("version") > 0)
+ if (dev_info.at("version") == search_info.at("version") &&
+ dev_info.at("version") != "0")
+ return dev;
+
+ // For this device, we merely have a vendor/model match.
+ last_resort_dev = dev;
+ }
+
+ // If there wasn't even a vendor/model/version match, we end up here.
+ // This is usually the case for devices with only vendor/model data.
+ // The selected device may be wrong with multiple such devices attached
+ // but it is the best we can do at this point. After all, there may be
+ // only one such device and we do want to select it in this case.
+ return last_resort_dev;
+}
+
void DeviceManager::init_drivers()
{
// Initialise all libsigrok drivers
#include <glib.h>
#include <list>
+#include <map>
#include <memory>
#include <string>
struct sr_dev_driver *const driver,
GSList *const drvopts = NULL);
+ const std::shared_ptr<device::Device> find_device_from_info(
+ const std::map<std::string, std::string> search_info);
+
private:
void init_drivers();
#include <libsigrok/libsigrok.h>
using std::list;
+using std::map;
using std::shared_ptr;
+using std::string;
namespace pv {
// Setup _session events
connect(&_session, SIGNAL(capture_state_changed(int)), this,
SLOT(capture_state_changed(int)));
-
}
void MainWindow::save_ui_settings()
{
QSettings settings;
+ map<string, string> dev_info;
+ list<string> key_list;
+
settings.beginGroup("MainWindow");
settings.setValue("state", saveState());
settings.setValue("geometry", saveGeometry());
settings.endGroup();
+
+ if (_session.get_device()) {
+ settings.beginGroup("Device");
+ key_list.push_back("vendor");
+ key_list.push_back("model");
+ key_list.push_back("version");
+ key_list.push_back("serial_num");
+ key_list.push_back("connection_id");
+
+ dev_info = _session.get_device()->get_device_info();
+
+ for (string key : key_list) {
+
+ if (dev_info.count(key))
+ settings.setValue(QString::fromUtf8(key.c_str()),
+ QString::fromUtf8(dev_info.at(key).c_str()));
+ else
+ settings.remove(QString::fromUtf8(key.c_str()));
+ }
+
+ settings.endGroup();
+ }
}
void MainWindow::restore_ui_settings()
{
QSettings settings;
+ shared_ptr<pv::device::DevInst> device;
+
+ map<string, string> dev_info;
+ list<string> key_list;
+ string value;
+
settings.beginGroup("MainWindow");
if (settings.contains("geometry")) {
resize(1000, 720);
settings.endGroup();
+
+ // Re-select last used device if possible.
+ settings.beginGroup("Device");
+ key_list.push_back("vendor");
+ key_list.push_back("model");
+ key_list.push_back("version");
+ key_list.push_back("serial_num");
+ key_list.push_back("connection_id");
+
+ for (string key : key_list) {
+ if (!settings.contains(QString::fromUtf8(key.c_str())))
+ continue;
+
+ value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
+
+ if (value.size() > 0)
+ dev_info.insert(std::make_pair(key, value));
+ }
+
+ device = _device_manager.find_device_from_info(dev_info);
+
+ if (device) {
+ _session.set_device(device);
+ update_device_list();
+ }
+
+ settings.endGroup();
}
void MainWindow::session_error(