]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
Return sensible Device::description() for session and input devices.
[libsigrok.git] / bindings / cxx / classes.cpp
index 43350b06faf1db3b021e928058dae88fd4551b70..9013e41ae9d44ad004cfe2a9b218e742a719432e 100644 (file)
@@ -352,22 +352,32 @@ Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
        return Glib::VariantContainerBase(data);
 }
 
-vector<const ConfigKey *> Configurable::config_keys(const ConfigKey *key)
+map<const ConfigKey *, set<Capability>> Configurable::config_keys(const ConfigKey *key)
 {
        GVariant *gvar_opts;
        gsize num_opts;
-       const int32_t *opts;
-       vector<const ConfigKey *> result;
+       const uint32_t *opts;
+       map<const ConfigKey *, set<Capability>> result;
 
        check(sr_config_list(
                config_driver, config_sdi, config_channel_group,
                key->id(), &gvar_opts));
 
-       opts = (const int32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(int32_t));
+       opts = (const uint32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t));
 
        for (gsize i = 0; i < num_opts; i++)
-               result.push_back(ConfigKey::get(opts[i]));
+       {
+               auto key = ConfigKey::get(opts[i] & SR_CONF_MASK);
+               set<Capability> capabilities;
+               if (opts[i] & SR_CONF_GET)
+                       capabilities.insert(GET);
+               if (opts[i] & SR_CONF_SET)
+                       capabilities.insert(SET);
+               if (opts[i] & SR_CONF_LIST)
+                       capabilities.insert(LIST);
+               result[key] = capabilities;
+       }
 
        g_variant_unref(gvar_opts);
 
@@ -379,18 +389,18 @@ bool Configurable::config_check(const ConfigKey *key,
 {
        GVariant *gvar_opts;
        gsize num_opts;
-       const int32_t *opts;
+       const uint32_t *opts;
 
        if (sr_config_list(config_driver, config_sdi, config_channel_group,
                        index_key->id(), &gvar_opts) != SR_OK)
                return false;
 
-       opts = (const int32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(int32_t));
+       opts = (const uint32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t));
 
        for (gsize i = 0; i < num_opts; i++)
        {
-               if (opts[i] == key->id())
+               if ((opts[i] & SR_CONF_MASK) == key->id())
                {
                        g_variant_unref(gvar_opts);
                        return true;
@@ -432,11 +442,20 @@ string Device::description()
        ostringstream s;
 
        vector<string> parts =
-               {vendor(), model(), version()};
+               {vendor(), model(), version(), serial_number()};
+
+       for (size_t i = 0; i < parts.size(); i++)
+       {
+               if (parts[i].length() > 0)
+               {
+                       if (i != 0)
+                               s << " ";
+                       s << parts[i];
+               }
+       }
 
-       for (string part : parts)
-               if (part.length() > 0)
-                       s << part;
+       if (serial_number().length() == 0 && connection_id().length() > 0)
+               s << " " << connection_id();
 
        return s.str();
 }
@@ -456,6 +475,16 @@ string Device::version()
        return valid_string(_structure->version);
 }
 
+string Device::serial_number()
+{
+       return valid_string(_structure->serial_num);
+}
+
+string Device::connection_id()
+{
+       return valid_string(_structure->connection_id);
+}
+
 vector<shared_ptr<Channel>> Device::channels()
 {
        vector<shared_ptr<Channel>> result;
@@ -696,7 +725,7 @@ DatafeedCallbackData::DatafeedCallbackData(Session *session,
 void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
        const struct sr_datafeed_packet *pkt)
 {
-       auto device = _session->_devices[sdi];
+       auto device = _session->get_device(sdi);
        auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
        _callback(device, packet);
 }
@@ -760,6 +789,11 @@ SessionDevice::~SessionDevice()
 {
 }
 
+string SessionDevice::description()
+{
+       return _parent->_filename;
+}
+
 shared_ptr<Device> SessionDevice::get_shared_from_this()
 {
        return static_pointer_cast<Device>(shared_from_this());
@@ -777,6 +811,7 @@ Session::Session(shared_ptr<Context> context) :
 Session::Session(shared_ptr<Context> context, string filename) :
        UserOwned(_structure),
        _context(context),
+       _filename(filename),
        _saving(false)
 {
        check(sr_session_load(filename.c_str(), &_structure));
@@ -785,9 +820,7 @@ Session::Session(shared_ptr<Context> context, string filename) :
        for (GSList *dev = dev_list; dev; dev = dev->next)
        {
                auto sdi = (struct sr_dev_inst *) dev->data;
-               auto device = new SessionDevice(sdi);
-               _devices[sdi] = shared_ptr<SessionDevice>(device,
-                       SessionDevice::Deleter());
+               _owned_devices[sdi] = new SessionDevice(sdi);
        }
        _context->_session = this;
 }
@@ -801,12 +834,26 @@ Session::~Session()
 
        for (auto entry : _source_callbacks)
                delete entry.second;
+
+       for (auto entry : _owned_devices)
+               delete entry.second;
+}
+
+shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
+{
+       if (_owned_devices.count(sdi))
+               return static_pointer_cast<Device>(
+                       _owned_devices[sdi]->get_shared_pointer(this));
+       else if (_other_devices.count(sdi))
+               return _other_devices[sdi];
+       else
+               throw Error(SR_ERR_BUG);
 }
 
 void Session::add_device(shared_ptr<Device> device)
 {
        check(sr_session_dev_add(_structure, device->_structure));
-       _devices[device->_structure] = device;
+       _other_devices[device->_structure] = device;
 }
 
 vector<shared_ptr<Device>> Session::devices()
@@ -817,14 +864,24 @@ vector<shared_ptr<Device>> Session::devices()
        for (GSList *dev = dev_list; dev; dev = dev->next)
        {
                auto sdi = (struct sr_dev_inst *) dev->data;
-               result.push_back(_devices[sdi]);
+               result.push_back(get_device(sdi));
        }
        return result;
 }
 
 void Session::remove_devices()
 {
-       _devices.clear();
+       for (auto entry : _owned_devices)
+       {
+               // We own this device. Make sure it's not referenced.
+               auto device = entry.second;
+               auto ptr = device->get_shared_pointer(this);
+               if (ptr.use_count() > 1)
+                       throw Error(SR_ERR_BUG);
+               delete device;
+       }
+       _owned_devices.clear();
+       _other_devices.clear();
        check(sr_session_dev_remove_all(_structure));
 }
 
@@ -1289,11 +1346,16 @@ void Input::send(string data)
        check(ret);
 }
 
+void Input::end()
+{
+       check(sr_input_end(_structure));
+}
+
 Input::~Input()
 {
        if (_device)
                delete _device;
-       check(sr_input_free(_structure));
+       sr_input_free(_structure);
 }
 
 InputDevice::InputDevice(shared_ptr<Input> input,
@@ -1308,6 +1370,11 @@ InputDevice::~InputDevice()
 {
 }
 
+string InputDevice::description()
+{
+       return "<input data>";
+}
+
 shared_ptr<Device> InputDevice::get_shared_from_this()
 {
        return static_pointer_cast<Device>(shared_from_this());