X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=41ded8522ba89c6d22aff03d74e9e6c122369674;hb=a42a39ac378534782d44f166b879e84cefa66ed6;hp=42ef6e7d4019fde915a1d9f6f56e3567cbd908f6;hpb=35114c3394679a4da54b930328409b3223507bad;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 42ef6e7d..41ded852 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -256,8 +256,6 @@ Driver::Driver(struct sr_dev_driver *structure) : Driver::~Driver() { - for (auto device : _devices) - delete device; } string Driver::name() @@ -280,11 +278,6 @@ vector> Driver::scan( _initialized = true; } - /* Clear all existing instances. */ - for (auto device : _devices) - delete device; - _devices.clear(); - /* Translate scan options to GSList of struct sr_config pointers. */ GSList *option_list = NULL; for (auto entry : options) @@ -303,20 +296,20 @@ vector> Driver::scan( /* Free option list. */ g_slist_free_full(option_list, g_free); + /* Create device objects. */ + vector> result; for (GSList *device = device_list; device; device = device->next) { auto sdi = (struct sr_dev_inst *) device->data; - _devices.push_back(new HardwareDevice(this, sdi)); + result.push_back(shared_ptr( + new HardwareDevice(shared_from_this(), sdi), + HardwareDevice::Deleter())); } /* Free GSList returned from scan. */ g_slist_free(device_list); - /* Create list of shared pointers to device instances for return. */ - vector> result; - for (auto device : _devices) - result.push_back(device->get_shared_pointer(_parent)); return result; } @@ -359,22 +352,32 @@ Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) return Glib::VariantContainerBase(data); } -vector Configurable::config_keys(const ConfigKey *key) +map> Configurable::config_keys(const ConfigKey *key) { GVariant *gvar_opts; gsize num_opts; - const int32_t *opts; - vector result; + const uint32_t *opts; + map> 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 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); @@ -386,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; @@ -434,20 +437,6 @@ Device::~Device() delete entry.second; } -string Device::description() -{ - ostringstream s; - - vector parts = - {vendor(), model(), version()}; - - for (string part : parts) - if (part.length() > 0) - s << part; - - return s.str(); -} - string Device::vendor() { return valid_string(_structure->vendor); @@ -463,6 +452,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> Device::channels() { vector> result; @@ -501,8 +500,9 @@ void Device::close() check(sr_dev_close(_structure)); } -HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) : - ParentOwned(structure), +HardwareDevice::HardwareDevice(shared_ptr driver, + struct sr_dev_inst *structure) : + UserOwned(structure), Device(structure), _driver(driver) { @@ -519,7 +519,7 @@ shared_ptr HardwareDevice::get_shared_from_this() shared_ptr HardwareDevice::driver() { - return _driver->get_shared_pointer(_parent); + return _driver; } Channel::Channel(struct sr_channel *structure) : @@ -702,7 +702,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(new Packet(device, pkt), Packet::Deleter()); _callback(device, packet); } @@ -783,6 +783,7 @@ Session::Session(shared_ptr context) : Session::Session(shared_ptr context, string filename) : UserOwned(_structure), _context(context), + _filename(filename), _saving(false) { check(sr_session_load(filename.c_str(), &_structure)); @@ -791,9 +792,7 @@ Session::Session(shared_ptr 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(device, - SessionDevice::Deleter()); + _owned_devices[sdi] = new SessionDevice(sdi); } _context->_session = this; } @@ -807,12 +806,26 @@ Session::~Session() for (auto entry : _source_callbacks) delete entry.second; + + for (auto entry : _owned_devices) + delete entry.second; +} + +shared_ptr Session::get_device(const struct sr_dev_inst *sdi) +{ + if (_owned_devices.count(sdi)) + return static_pointer_cast( + _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) { check(sr_session_dev_add(_structure, device->_structure)); - _devices[device->_structure] = device; + _other_devices[device->_structure] = device; } vector> Session::devices() @@ -823,14 +836,14 @@ vector> 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(); + _other_devices.clear(); check(sr_session_dev_remove_all(_structure)); } @@ -1030,6 +1043,11 @@ void Session::set_trigger(shared_ptr trigger) _trigger = trigger; } +string Session::filename() +{ + return _filename; +} + Packet::Packet(shared_ptr device, const struct sr_datafeed_packet *structure) : UserOwned(structure), @@ -1295,11 +1313,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,