X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=8316b9bd1944c7a3c37ab77f111f50f6a154fcf3;hp=c5ce63864359eeb08fe6a956debbc001dd2f3a21;hb=ea7a83a437e60168d396fac20ff1eda04a06d100;hpb=a98729a742ebbb9df9dbb9ce8aae1cc4c3f1e00d diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index c5ce6386..8316b9bd 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -18,8 +18,9 @@ */ /* Needed for isascii(), as used in the GNU libstdc++ headers */ +/* Needed in strutil.c for POSIX.1-2008 locale functions */ #ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#define _XOPEN_SOURCE 700 #endif #include @@ -101,7 +102,7 @@ SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, return SR_OK; } -SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res, +SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res, void *buf, size_t count, void *cb_data) noexcept { try { @@ -157,8 +158,7 @@ string Context::lib_version() map> Context::drivers() { map> result; - for (const auto &entry: _drivers) - { + for (const auto &entry: _drivers) { const auto &name = entry.first; const auto &driver = entry.second; result.emplace(name, driver->share_owned_by(shared_from_this())); @@ -169,8 +169,7 @@ map> Context::drivers() map> Context::input_formats() { map> result; - for (const auto &entry: _input_formats) - { + for (const auto &entry: _input_formats) { const auto &name = entry.first; const auto &input_format = entry.second; result.emplace(name, input_format->share_owned_by(shared_from_this())); @@ -178,11 +177,36 @@ map> Context::input_formats() return result; } +shared_ptr Context::input_format_match(string filename) +{ + const struct sr_input *input; + const struct sr_input_module *imod; + int rc; + + /* + * Have the input module looked up for the specified file. + * Failed lookup (or "successful lookup" with an empty result) + * are non-fatal. Free the sr_input that was created by the + * lookup routine, but grab the input module kind and return an + * InputFormat instance to the application. This works because + * the application passes a filename, no input data got buffered + * in the sr_input that we release. + */ + input = NULL; + rc = sr_input_scan_file(filename.c_str(), &input); + if (rc != SR_OK) + return nullptr; + if (!input) + return nullptr; + imod = sr_input_module_get(input); + sr_input_free(input); + return shared_ptr{new InputFormat{imod}, default_delete{}}; +} + map> Context::output_formats() { map> result; - for (const auto &entry: _output_formats) - { + for (const auto &entry: _output_formats) { const auto &name = entry.first; const auto &output_format = entry.second; result.emplace(name, output_format->share_owned_by(shared_from_this())); @@ -213,12 +237,9 @@ static int call_log_callback(void *cb_data, int loglevel, auto *const callback = static_cast(cb_data); - try - { + try { (*callback)(LogLevel::get(loglevel), message.get()); - } - catch (Error e) - { + } catch (Error &e) { return e.result; } @@ -278,11 +299,10 @@ shared_ptr Context::create_header_packet(Glib::TimeVal start_time) } shared_ptr Context::create_meta_packet( - const map &config) + map config) { auto meta = g_new0(struct sr_datafeed_meta, 1); - for (const auto &input : config) - { + for (const auto &input : config) { const auto &key = input.first; const auto &value = input.second; auto *const output = g_new(struct sr_config, 1); @@ -311,28 +331,60 @@ shared_ptr Context::create_logic_packet( } shared_ptr Context::create_analog_packet( - const vector > &channels, - float *data_pointer, unsigned int num_samples, const Quantity *mq, - const Unit *unit, const vector &mqflags) + vector > channels, + const float *data_pointer, unsigned int num_samples, const Quantity *mq, + const Unit *unit, vector mqflags) { auto analog = g_new0(struct sr_datafeed_analog, 1); auto meaning = g_new0(struct sr_analog_meaning, 1); + auto encoding = g_new0(struct sr_analog_encoding, 1); + auto spec = g_new0(struct sr_analog_spec, 1); analog->meaning = meaning; for (const auto &channel : channels) meaning->channels = g_slist_append(meaning->channels, channel->_structure); - analog->num_samples = num_samples; meaning->mq = static_cast(mq->id()); meaning->unit = static_cast(unit->id()); - meaning->mqflags = static_cast(QuantityFlag::mask_from_flags(mqflags)); - analog->data = data_pointer; + meaning->mqflags = static_cast(QuantityFlag::mask_from_flags(move(mqflags))); + + analog->encoding = encoding; + + encoding->unitsize = sizeof(float); + encoding->is_signed = TRUE; + encoding->is_float = TRUE; +#ifdef WORDS_BIGENDIAN + encoding->is_bigendian = TRUE; +#else + encoding->is_bigendian = FALSE; +#endif + encoding->digits = 0; + encoding->is_digits_decimal = FALSE; + encoding->scale.p = 1; + encoding->scale.q = 1; + encoding->offset.p = 0; + encoding->offset.q = 1; + + analog->spec = spec; + + spec->spec_digits = 0; + + analog->num_samples = num_samples; + analog->data = (float*)data_pointer; auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_ANALOG; packet->payload = analog; return shared_ptr{new Packet{nullptr, packet}, default_delete{}}; } +shared_ptr Context::create_end_packet() +{ + auto packet = g_new(struct sr_datafeed_packet, 1); + packet->type = SR_DF_END; + return shared_ptr{new Packet{nullptr, packet}, + default_delete{}}; +} + shared_ptr Context::load_session(string filename) { return shared_ptr{ @@ -406,20 +458,30 @@ string Driver::long_name() const return valid_string(_structure->longname); } +set Driver::scan_options() const +{ + GArray *opts = sr_driver_scan_options_list(_structure); + set result; + if (opts) { + for (guint i = 0; i < opts->len; i++) + result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i))); + g_array_free(opts, TRUE); + } + return result; +} + vector> Driver::scan( - const map &options) + map options) { /* Initialise the driver if not yet done. */ - if (!_initialized) - { + if (!_initialized) { check(sr_driver_init(_parent->_structure, _structure)); _initialized = true; } /* Translate scan options to GSList of struct sr_config pointers. */ GSList *option_list = nullptr; - for (const auto &entry : options) - { + for (const auto &entry : options) { const auto &key = entry.first; const auto &value = entry.second; auto *const config = g_new(struct sr_config, 1); @@ -437,8 +499,7 @@ vector> Driver::scan( /* Create device objects. */ vector> result; - for (GSList *device = device_list; device; device = device->next) - { + for (GSList *device = device_list; device; device = device->next) { auto *const sdi = static_cast(device->data); shared_ptr hwdev { new HardwareDevice{shared_from_this(), sdi}, @@ -466,6 +527,22 @@ Configurable::~Configurable() { } +set Configurable::config_keys() const +{ + GArray *opts; + set result; + + opts = sr_dev_options(config_driver, config_sdi, config_channel_group); + + if (opts) { + for (guint i = 0; i < opts->len; i++) + result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i))); + g_array_free(opts, TRUE); + } + + return result; +} + Glib::VariantBase Configurable::config_get(const ConfigKey *key) const { GVariant *data; @@ -482,88 +559,49 @@ void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &val key->id(), const_cast(value.gobj()))); } -Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const +set Configurable::config_capabilities(const ConfigKey *key) const { - GVariant *data; - check(sr_config_list( - config_driver, config_sdi, config_channel_group, - key->id(), &data)); - return Glib::VariantContainerBase(data); -} - -map> Configurable::config_keys(const ConfigKey *key) -{ - GVariant *gvar_opts; - gsize num_opts; - const uint32_t *opts; - map> result; + int caps = sr_dev_config_capabilities_list(config_sdi, + config_channel_group, key->id()); - check(sr_config_list( - config_driver, config_sdi, config_channel_group, - key->id(), &gvar_opts)); + set result; - opts = static_cast(g_variant_get_fixed_array( - gvar_opts, &num_opts, sizeof(uint32_t))); - - for (gsize i = 0; i < num_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); + for (auto cap: Capability::values()) + if (caps & cap->id()) + result.insert(cap); return result; } bool Configurable::config_check(const ConfigKey *key, - const ConfigKey *index_key) const + const Capability *capability) const { - GVariant *gvar_opts; - gsize num_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 = static_cast(g_variant_get_fixed_array( - gvar_opts, &num_opts, sizeof(uint32_t))); - - for (gsize i = 0; i < num_opts; i++) - { - if ((opts[i] & SR_CONF_MASK) == unsigned(key->id())) - { - g_variant_unref(gvar_opts); - return true; - } - } + int caps = sr_dev_config_capabilities_list(config_sdi, + config_channel_group, key->id()); - g_variant_unref(gvar_opts); + return (caps & capability->id()); +} - return false; +Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const +{ + GVariant *data; + check(sr_config_list( + config_driver, config_sdi, config_channel_group, + key->id(), &data)); + return Glib::VariantContainerBase(data); } Device::Device(struct sr_dev_inst *structure) : Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), _structure(structure) { - for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) - { + for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) { auto *const ch = static_cast(entry->data); unique_ptr channel {new Channel{ch}}; _channels.emplace(ch, move(channel)); } - for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) - { + for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) { auto *const cg = static_cast(entry->data); unique_ptr group {new ChannelGroup{this, cg}}; _channel_groups.emplace(group->name(), move(group)); @@ -571,7 +609,8 @@ Device::Device(struct sr_dev_inst *structure) : } Device::~Device() -{} +{ +} string Device::vendor() const { @@ -617,8 +656,7 @@ map> Device::channel_groups() { map> result; - for (const auto &entry: _channel_groups) - { + for (const auto &entry: _channel_groups) { const auto &name = entry.first; const auto &channel_group = entry.second; result.emplace(name, channel_group->share_owned_by(get_shared_from_this())); @@ -753,7 +791,7 @@ vector> ChannelGroup::channels() return result; } -Trigger::Trigger(shared_ptr context, string name) : +Trigger::Trigger(shared_ptr context, string name) : _structure(sr_trigger_new(name.c_str())), _context(move(context)) { @@ -797,7 +835,7 @@ TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : TriggerStage::~TriggerStage() { } - + int TriggerStage::number() const { return _structure->stage; @@ -906,6 +944,7 @@ Session::Session(shared_ptr context, string filename) : _owned_devices.emplace(sdi, move(device)); } _context->_session = this; + g_slist_free(dev_list); } Session::~Session() @@ -940,6 +979,7 @@ vector> Session::devices() auto *const sdi = static_cast(dev->data); result.push_back(get_device(sdi)); } + g_slist_free(dev_list); return result; } @@ -1140,7 +1180,7 @@ map Meta::config() const map result; for (auto l = _structure->config; l; l = l->next) { auto *const config = static_cast(l->data); - result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); + result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true); } return result; } @@ -1197,6 +1237,11 @@ void *Analog::data_pointer() return _structure->data; } +void Analog::get_data_as_float(float *dest) +{ + check(sr_analog_to_float(_structure, dest)); +} + unsigned int Analog::num_samples() const { return _structure->num_samples; @@ -1212,6 +1257,58 @@ vector> Analog::channels() return result; } +unsigned int Analog::unitsize() const +{ + return _structure->encoding->unitsize; +} + +bool Analog::is_signed() const +{ + return _structure->encoding->is_signed; +} + +bool Analog::is_float() const +{ + return _structure->encoding->is_float; +} + +bool Analog::is_bigendian() const +{ + return _structure->encoding->is_bigendian; +} + +int Analog::digits() const +{ + return _structure->encoding->digits; +} + +bool Analog::is_digits_decimal() const +{ + return _structure->encoding->is_digits_decimal; +} + +shared_ptr Analog::scale() +{ + unique_ptr scale; + scale.reset(new Rational(&(_structure->encoding->scale))); + + if (scale) + return scale->share_owned_by(shared_from_this()); + else + throw Error(SR_ERR_NA); +} + +shared_ptr Analog::offset() +{ + unique_ptr offset; + offset.reset(new Rational(&(_structure->encoding->offset))); + + if (offset) + return offset->share_owned_by(shared_from_this()); + else + throw Error(SR_ERR_NA); +} + const Quantity *Analog::mq() const { return Quantity::get(_structure->meaning->mq); @@ -1227,6 +1324,78 @@ vector Analog::mq_flags() const return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); } +shared_ptr Analog::get_logic_via_threshold(float threshold, + uint8_t *data_ptr) const +{ + auto datafeed = g_new(struct sr_datafeed_logic, 1); + datafeed->length = num_samples(); + datafeed->unitsize = 1; + + if (data_ptr) + datafeed->data = data_ptr; + else + datafeed->data = g_malloc(datafeed->length); + + shared_ptr logic = + shared_ptr{new Logic{datafeed}, default_delete{}}; + + check(sr_a2l_threshold(_structure, threshold, + (uint8_t*)datafeed->data, datafeed->length)); + + return logic; +} + +shared_ptr Analog::get_logic_via_schmitt_trigger(float lo_thr, + float hi_thr, uint8_t *state, uint8_t *data_ptr) const +{ + auto datafeed = g_new(struct sr_datafeed_logic, 1); + datafeed->length = num_samples(); + datafeed->unitsize = 1; + + if (data_ptr) + datafeed->data = data_ptr; + else + datafeed->data = g_malloc(datafeed->length); + + shared_ptr logic = + shared_ptr{new Logic{datafeed}, default_delete{}}; + + check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state, + (uint8_t*)datafeed->data, datafeed->length)); + + return logic; +} + +Rational::Rational(const struct sr_rational *structure) : + _structure(structure) +{ +} + +Rational::~Rational() +{ +} + +shared_ptr Rational::share_owned_by(shared_ptr _parent) +{ + return static_pointer_cast( + ParentOwned::share_owned_by(_parent)); +} + +int64_t Rational::numerator() const +{ + return _structure->p; +} + +uint64_t Rational::denominator() const +{ + return _structure->q; +} + +float Rational::value() const +{ + return (float)(_structure->p) / (float)(_structure->q); +} + InputFormat::InputFormat(const struct sr_input_module *structure) : _structure(structure) { @@ -1259,8 +1428,7 @@ map> InputFormat::options() { map> result; - if (const struct sr_option **options = sr_input_options_get(_structure)) - { + if (const struct sr_option **options = sr_input_options_get(_structure)) { shared_ptr option_array {options, &sr_input_options_free}; for (int i = 0; options[i]; i++) { @@ -1274,7 +1442,7 @@ map> InputFormat::options() } shared_ptr InputFormat::create_input( - const map &options) + map options) { auto input = sr_input_new(_structure, map_to_hash_variant(options)); if (!input) @@ -1290,8 +1458,7 @@ Input::Input(shared_ptr context, const struct sr_input *structure) : shared_ptr Input::device() { - if (!_device) - { + if (!_device) { auto sdi = sr_input_dev_inst_get(_structure); if (!sdi) throw Error(SR_ERR_NA); @@ -1305,7 +1472,7 @@ void Input::send(void *data, size_t length) { auto gstr = g_string_new_len(static_cast(data), length); auto ret = sr_input_send(_structure, gstr); - g_string_free(gstr, false); + g_string_free(gstr, true); check(ret); } @@ -1314,6 +1481,11 @@ void Input::end() check(sr_input_end(_structure)); } +void Input::reset() +{ + check(sr_input_reset(_structure)); +} + Input::~Input() { sr_input_free(_structure); @@ -1376,6 +1548,28 @@ vector Option::values() const return result; } +Glib::VariantBase Option::parse_string(string value) +{ + enum sr_datatype dt; + Glib::VariantBase dflt = default_value(); + GVariant *tmpl = dflt.gobj(); + + if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) { + dt = SR_T_UINT64; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) { + dt = SR_T_STRING; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) { + dt = SR_T_BOOL; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) { + dt = SR_T_FLOAT; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) { + dt = SR_T_INT32; + } else { + throw Error(SR_ERR_BUG); + } + return ConfigKey::parse_string(value, dt); +} + OutputFormat::OutputFormat(const struct sr_output_module *structure) : _structure(structure) { @@ -1408,8 +1602,7 @@ map> OutputFormat::options() { map> result; - if (const struct sr_option **options = sr_output_options_get(_structure)) - { + if (const struct sr_option **options = sr_output_options_get(_structure)) { shared_ptr option_array {options, &sr_output_options_free}; for (int i = 0; options[i]; i++) { @@ -1423,18 +1616,18 @@ map> OutputFormat::options() } shared_ptr OutputFormat::create_output( - shared_ptr device, const map &options) + shared_ptr device, map options) { return shared_ptr{ - new Output{shared_from_this(), move(device), options}, + new Output{shared_from_this(), move(device), move(options)}, default_delete{}}; } shared_ptr OutputFormat::create_output(string filename, - shared_ptr device, const map &options) + shared_ptr device, map options) { return shared_ptr{ - new Output{move(filename), shared_from_this(), move(device), options}, + new Output{move(filename), shared_from_this(), move(device), move(options)}, default_delete{}}; } @@ -1444,22 +1637,22 @@ bool OutputFormat::test_flag(const OutputFlag *flag) const } Output::Output(shared_ptr format, - shared_ptr device, const map &options) : + shared_ptr device, map options) : _structure(sr_output_new(format->_structure, map_to_hash_variant(options), device->_structure, nullptr)), _format(move(format)), _device(move(device)), - _options(options) + _options(move(options)) { } Output::Output(string filename, shared_ptr format, - shared_ptr device, const map &options) : + shared_ptr device, map options) : _structure(sr_output_new(format->_structure, map_to_hash_variant(options), device->_structure, filename.c_str())), _format(move(format)), _device(move(device)), - _options(options) + _options(move(options)) { } @@ -1468,18 +1661,20 @@ Output::~Output() check(sr_output_free(_structure)); } +shared_ptr Output::format() +{ + return _format; +} + string Output::receive(shared_ptr packet) { GString *out; check(sr_output_send(_structure, packet->_structure, &out)); - if (out) - { + if (out) { auto result = string(out->str, out->str + out->len); g_string_free(out, true); return result; - } - else - { + } else { return string(); } }