X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=bindings%2Fcxx%2Fclasses.cpp;h=a0b161a62a35aee683894cfe87b0b8c521d8d42a;hb=5089a14345442bb87d0970efc0bc10235a530d60;hp=c915c8a7479920a23069a8c45dfe02983a766456;hpb=c7bc82ffa1b09a228a8395049e2b691cd7bd85f8;p=libsigrok.git
diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp
index c915c8a7..a0b161a6 100644
--- a/bindings/cxx/classes.cpp
+++ b/bindings/cxx/classes.cpp
@@ -17,7 +17,13 @@
* along with this program. If not, see .
*/
-#include "libsigrokcxx/libsigrokcxx.hpp"
+/* Needed for isascii(), as used in the GNU libstdc++ headers */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+
+#include
+#include
#include
#include
@@ -33,65 +39,109 @@ static void check(int result)
}
/** Helper function to obtain valid strings from possibly null input. */
-static const char *valid_string(const char *input)
+static inline const char *valid_string(const char *input)
{
- if (input != NULL)
- return input;
- else
- return "";
+ return (input) ? input : "";
}
/** Helper function to convert between map and GHashTable */
-static GHashTable *map_to_hash_variant(map input)
+static GHashTable *map_to_hash_variant(const map &input)
{
- auto output = g_hash_table_new_full(
- g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
- for (auto entry : input)
+ auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+ reinterpret_cast(&g_variant_unref));
+ for (const auto &entry : input)
g_hash_table_insert(output,
g_strdup(entry.first.c_str()),
entry.second.gobj_copy());
- return output;
+ return output;
}
Error::Error(int result) : result(result)
{
}
-const char *Error::what() const throw()
+const char *Error::what() const noexcept
{
return sr_strerror(result);
}
-Error::~Error() throw()
+Error::~Error() noexcept
+{
+}
+
+ResourceReader::~ResourceReader()
{
}
+SR_PRIV int ResourceReader::open_callback(struct sr_resource *res,
+ const char *name, void *cb_data) noexcept
+{
+ try {
+ auto *const reader = static_cast(cb_data);
+ reader->open(res, name);
+ } catch (const Error &err) {
+ return err.result;
+ } catch (...) {
+ return SR_ERR;
+ }
+ return SR_OK;
+}
+
+SR_PRIV int ResourceReader::close_callback(struct sr_resource *res,
+ void *cb_data) noexcept
+{
+ try {
+ auto *const reader = static_cast(cb_data);
+ reader->close(res);
+ } catch (const Error &err) {
+ return err.result;
+ } catch (...) {
+ return SR_ERR;
+ }
+ return SR_OK;
+}
+
+SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res,
+ void *buf, size_t count, void *cb_data) noexcept
+{
+ try {
+ auto *const reader = static_cast(cb_data);
+ return reader->read(res, buf, count);
+ } catch (const Error &err) {
+ return err.result;
+ } catch (...) {
+ return SR_ERR;
+ }
+}
+
shared_ptr Context::create()
{
- return shared_ptr(new Context(), Context::Deleter());
+ return shared_ptr{new Context{}, default_delete{}};
}
Context::Context() :
- UserOwned(_structure),
- _session(NULL)
+ _structure(nullptr),
+ _session(nullptr)
{
check(sr_init(&_structure));
- struct sr_dev_driver **driver_list = sr_driver_list();
- if (driver_list)
- for (int i = 0; driver_list[i]; i++)
- _drivers[driver_list[i]->name] =
- new Driver(driver_list[i]);
- const struct sr_input_module **input_list = sr_input_list();
- if (input_list)
- for (int i = 0; input_list[i]; i++)
- _input_formats[sr_input_id_get(input_list[i])] =
- new InputFormat(input_list[i]);
- const struct sr_output_module **output_list = sr_output_list();
- if (output_list)
- for (int i = 0; output_list[i]; i++)
- _output_formats[sr_output_id_get(output_list[i])] =
- new OutputFormat(output_list[i]);
+ if (struct sr_dev_driver **driver_list = sr_driver_list(_structure))
+ for (int i = 0; driver_list[i]; i++) {
+ unique_ptr driver {new Driver{driver_list[i]}};
+ _drivers.emplace(driver->name(), move(driver));
+ }
+
+ if (const struct sr_input_module **input_list = sr_input_list())
+ for (int i = 0; input_list[i]; i++) {
+ unique_ptr input {new InputFormat{input_list[i]}};
+ _input_formats.emplace(input->name(), move(input));
+ }
+
+ if (const struct sr_output_module **output_list = sr_output_list())
+ for (int i = 0; output_list[i]; i++) {
+ unique_ptr output {new OutputFormat{output_list[i]}};
+ _output_formats.emplace(output->name(), move(output));
+ }
}
string Context::package_version()
@@ -107,11 +157,10 @@ string Context::lib_version()
map> Context::drivers()
{
map> result;
- for (auto entry: _drivers)
- {
- auto name = entry.first;
- auto driver = entry.second;
- result[name] = driver->get_shared_pointer(this);
+ 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()));
}
return result;
}
@@ -119,11 +168,10 @@ map> Context::drivers()
map> Context::input_formats()
{
map> result;
- for (auto entry: _input_formats)
- {
- auto name = entry.first;
- auto input_format = entry.second;
- result[name] = input_format->get_shared_pointer(this);
+ 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()));
}
return result;
}
@@ -131,27 +179,20 @@ map> Context::input_formats()
map> Context::output_formats()
{
map> result;
- for (auto entry: _output_formats)
- {
- auto name = entry.first;
- auto output_format = entry.second;
- result[name] = output_format->get_shared_pointer(this);
+ 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()));
}
return result;
}
Context::~Context()
{
- for (auto entry : _drivers)
- delete entry.second;
- for (auto entry : _input_formats)
- delete entry.second;
- for (auto entry : _output_formats)
- delete entry.second;
check(sr_exit(_structure));
}
-const LogLevel *Context::log_level()
+const LogLevel *Context::log_level() const
{
return LogLevel::get(sr_log_loglevel_get());
}
@@ -161,35 +202,17 @@ void Context::set_log_level(const LogLevel *level)
check(sr_log_loglevel_set(level->id()));
}
-string Context::log_domain()
-{
- return valid_string(sr_log_logdomain_get());
-}
-
-void Context::set_log_domain(string value)
-{
- check(sr_log_logdomain_set(value.c_str()));
-}
-
-static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args)
+static int call_log_callback(void *cb_data, int loglevel,
+ const char *format, va_list args) noexcept
{
- va_list args_copy;
- va_copy(args_copy, args);
- int length = vsnprintf(NULL, 0, format, args_copy);
- va_end(args_copy);
- char *buf = (char *) g_malloc(length + 1);
- vsprintf(buf, format, args);
- string message(buf, length);
- g_free(buf);
+ const unique_ptr
+ message {g_strdup_vprintf(format, args), &g_free};
- LogCallbackFunction callback = *((LogCallbackFunction *) cb_data);
+ auto *const callback = static_cast(cb_data);
- try
- {
- callback(LogLevel::get(loglevel), message);
- }
- catch (Error e)
- {
+ try {
+ (*callback)(LogLevel::get(loglevel), message.get());
+ } catch (Error e) {
return e.result;
}
@@ -198,27 +221,41 @@ static int call_log_callback(void *cb_data, int loglevel, const char *format, va
void Context::set_log_callback(LogCallbackFunction callback)
{
- _log_callback = callback;
+ _log_callback = move(callback);
check(sr_log_callback_set(call_log_callback, &_log_callback));
-}
+}
void Context::set_log_callback_default()
{
check(sr_log_callback_set_default());
_log_callback = nullptr;
-}
+}
+
+void Context::set_resource_reader(ResourceReader *reader)
+{
+ if (reader) {
+ check(sr_resource_set_hooks(_structure,
+ &ResourceReader::open_callback,
+ &ResourceReader::close_callback,
+ &ResourceReader::read_callback, reader));
+ } else {
+ check(sr_resource_set_hooks(_structure,
+ nullptr, nullptr, nullptr, nullptr));
+ }
+}
shared_ptr Context::create_session()
{
- return shared_ptr(
- new Session(shared_from_this()), Session::Deleter());
+ return shared_ptr{new Session{shared_from_this()},
+ default_delete{}};
}
shared_ptr Context::create_user_device(
string vendor, string model, string version)
{
- return shared_ptr(
- new UserDevice(vendor, model, version), UserDevice::Deleter());
+ return shared_ptr{
+ new UserDevice{move(vendor), move(model), move(version)},
+ default_delete{}};
}
shared_ptr Context::create_header_packet(Glib::TimeVal start_time)
@@ -230,27 +267,27 @@ shared_ptr Context::create_header_packet(Glib::TimeVal start_time)
auto packet = g_new(struct sr_datafeed_packet, 1);
packet->type = SR_DF_HEADER;
packet->payload = header;
- return shared_ptr(new Packet(nullptr, packet), Packet::Deleter());
+ return shared_ptr{new Packet{nullptr, packet},
+ default_delete{}};
}
shared_ptr Context::create_meta_packet(
map config)
{
auto meta = g_new0(struct sr_datafeed_meta, 1);
- for (auto input : config)
- {
- auto key = input.first;
- auto value = input.second;
- auto output = g_new(struct sr_config, 1);
+ for (const auto &input : config) {
+ const auto &key = input.first;
+ const auto &value = input.second;
+ auto *const output = g_new(struct sr_config, 1);
output->key = key->id();
- output->data = value.gobj();
- g_variant_ref(output->data);
+ output->data = value.gobj_copy();
meta->config = g_slist_append(meta->config, output);
}
auto packet = g_new(struct sr_datafeed_packet, 1);
packet->type = SR_DF_META;
packet->payload = meta;
- return shared_ptr(new Packet(nullptr, packet), Packet::Deleter());
+ return shared_ptr{new Packet{nullptr, packet},
+ default_delete{}};
}
shared_ptr Context::create_logic_packet(
@@ -263,38 +300,68 @@ shared_ptr Context::create_logic_packet(
auto packet = g_new(struct sr_datafeed_packet, 1);
packet->type = SR_DF_LOGIC;
packet->payload = logic;
- return shared_ptr(new Packet(nullptr, packet), Packet::Deleter());
+ return shared_ptr{new Packet{nullptr, packet}, default_delete{}};
}
shared_ptr Context::create_analog_packet(
vector > channels,
- float *data_pointer, unsigned int num_samples, const Quantity *mq,
+ 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);
- for (auto channel : channels)
- analog->channels = g_slist_append(analog->channels, channel->_structure);
+ 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);
+ meaning->mq = static_cast(mq->id());
+ meaning->unit = static_cast(unit->id());
+ 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->mq = mq->id();
- analog->unit = unit->id();
- analog->mqflags = QuantityFlag::mask_from_flags(mqflags);
- analog->data = data_pointer;
+ 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), Packet::Deleter());
+ return shared_ptr{new Packet{nullptr, packet}, default_delete{}};
}
shared_ptr Context::load_session(string filename)
{
- return shared_ptr(
- new Session(shared_from_this(), filename), Session::Deleter());
+ return shared_ptr{
+ new Session{shared_from_this(), move(filename)},
+ default_delete{}};
}
shared_ptr Context::create_trigger(string name)
{
- return shared_ptr(
- new Trigger(shared_from_this(), name), Trigger::Deleter());
+ return shared_ptr{
+ new Trigger{shared_from_this(), move(name)},
+ default_delete{}};
}
shared_ptr Context::open_file(string filename)
@@ -302,8 +369,9 @@ shared_ptr Context::open_file(string filename)
const struct sr_input *input;
check(sr_input_scan_file(filename.c_str(), &input));
- return shared_ptr(
- new Input(shared_from_this(), input), Input::Deleter());
+ return shared_ptr{
+ new Input{shared_from_this(), input},
+ default_delete{}};
}
shared_ptr Context::open_stream(string header)
@@ -314,27 +382,29 @@ shared_ptr Context::open_stream(string header)
auto ret = sr_input_scan_buffer(gstr, &input);
g_string_free(gstr, true);
check(ret);
- return shared_ptr(
- new Input(shared_from_this(), input), Input::Deleter());
+ return shared_ptr{
+ new Input{shared_from_this(), input},
+ default_delete{}};
}
-map Context::serials(shared_ptr driver)
+map Context::serials(shared_ptr driver) const
{
- GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL);
+ GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr);
map serials;
for (GSList *serial = serial_list; serial; serial = serial->next) {
- struct sr_serial_port *port = (sr_serial_port *) serial->data;
+ auto *const port = static_cast(serial->data);
serials[string(port->name)] = string(port->description);
}
- g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free);
+ g_slist_free_full(serial_list,
+ reinterpret_cast(&sr_serial_free));
return serials;
}
Driver::Driver(struct sr_dev_driver *structure) :
- ParentOwned(structure),
- Configurable(structure, NULL, NULL),
+ Configurable(structure, nullptr, nullptr),
+ _structure(structure),
_initialized(false)
{
}
@@ -343,35 +413,45 @@ Driver::~Driver()
{
}
-string Driver::name()
+string Driver::name() const
{
return valid_string(_structure->name);
}
-string Driver::long_name()
+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(
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 = NULL;
- for (auto entry : options)
- {
- auto key = entry.first;
- auto value = entry.second;
- auto config = g_new(struct sr_config, 1);
+ GSList *option_list = nullptr;
+ for (const auto &entry : options) {
+ const auto &key = entry.first;
+ const auto &value = entry.second;
+ auto *const config = g_new(struct sr_config, 1);
config->key = key->id();
- config->data = value.gobj();
+ config->data = const_cast(value.gobj());
option_list = g_slist_append(option_list, config);
}
@@ -384,12 +464,12 @@ vector> Driver::scan(
/* Create device objects. */
vector> result;
- for (GSList *device = device_list; device; device = device->next)
- {
- auto sdi = (struct sr_dev_inst *) device->data;
- result.push_back(shared_ptr(
- new HardwareDevice(shared_from_this(), sdi),
- HardwareDevice::Deleter()));
+ 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},
+ default_delete{}};
+ result.push_back(move(hwdev));
}
/* Free GSList returned from scan. */
@@ -412,7 +492,23 @@ Configurable::~Configurable()
{
}
-Glib::VariantBase Configurable::config_get(const ConfigKey *key)
+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;
check(sr_config_get(
@@ -421,128 +517,87 @@ Glib::VariantBase Configurable::config_get(const ConfigKey *key)
return Glib::VariantBase(data);
}
-void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
+void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value)
{
check(sr_config_set(
config_sdi, config_channel_group,
- key->id(), value.gobj()));
-}
-
-Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
-{
- GVariant *data;
- check(sr_config_list(
- config_driver, config_sdi, config_channel_group,
- key->id(), &data));
- return Glib::VariantContainerBase(data);
+ key->id(), const_cast(value.gobj())));
}
-map> Configurable::config_keys(const ConfigKey *key)
+set Configurable::config_capabilities(const ConfigKey *key) const
{
- 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));
-
- opts = (const uint32_t *) 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;
- }
+ set result;
- 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 Capability *capability) const
{
- GVariant *gvar_opts;
- gsize num_opts;
- const uint32_t *opts;
+ int caps = sr_dev_config_capabilities_list(config_sdi,
+ config_channel_group, key->id());
- if (sr_config_list(config_driver, config_sdi, config_channel_group,
- index_key->id(), &gvar_opts) != SR_OK)
- return false;
-
- 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] & SR_CONF_MASK) == (uint32_t) key->id())
- {
- g_variant_unref(gvar_opts);
- return true;
- }
- }
-
- 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, NULL),
+ Configurable(sr_dev_inst_driver_get(structure), structure, nullptr),
_structure(structure)
{
- for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next)
- {
- auto channel = (struct sr_channel *) entry->data;
- _channels[channel] = new Channel(channel);
+ 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)
- {
- auto group = (struct sr_channel_group *) entry->data;
- _channel_groups[group->name] = new ChannelGroup(this, group);
+ 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));
}
}
Device::~Device()
{
- for (auto entry : _channels)
- delete entry.second;
- for (auto entry : _channel_groups)
- delete entry.second;
}
-string Device::vendor()
+string Device::vendor() const
{
return valid_string(sr_dev_inst_vendor_get(_structure));
}
-string Device::model()
+string Device::model() const
{
return valid_string(sr_dev_inst_model_get(_structure));
}
-string Device::version()
+string Device::version() const
{
return valid_string(sr_dev_inst_version_get(_structure));
}
-string Device::serial_number()
+string Device::serial_number() const
{
return valid_string(sr_dev_inst_sernum_get(_structure));
}
-string Device::connection_id()
+string Device::connection_id() const
{
return valid_string(sr_dev_inst_connid_get(_structure));
}
@@ -550,27 +605,26 @@ string Device::connection_id()
vector> Device::channels()
{
vector> result;
- for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next)
- result.push_back(
- _channels[(struct sr_channel *) channel->data]->get_shared_pointer(
- get_shared_from_this()));
+ for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) {
+ auto *const ch = static_cast(channel->data);
+ result.push_back(_channels[ch]->share_owned_by(get_shared_from_this()));
+ }
return result;
}
shared_ptr Device::get_channel(struct sr_channel *ptr)
{
- return _channels[ptr]->get_shared_pointer(get_shared_from_this());
+ return _channels[ptr]->share_owned_by(get_shared_from_this());
}
map>
Device::channel_groups()
{
map> result;
- for (auto entry: _channel_groups)
- {
- auto name = entry.first;
- auto channel_group = entry.second;
- result[name] = channel_group->get_shared_pointer(get_shared_from_this());
+ 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()));
}
return result;
}
@@ -587,9 +641,8 @@ void Device::close()
HardwareDevice::HardwareDevice(shared_ptr driver,
struct sr_dev_inst *structure) :
- UserOwned(structure),
Device(structure),
- _driver(driver)
+ _driver(move(driver))
{
}
@@ -608,9 +661,8 @@ shared_ptr HardwareDevice::driver()
}
UserDevice::UserDevice(string vendor, string model, string version) :
- UserOwned(sr_dev_inst_user_new(
- vendor.c_str(), model.c_str(), version.c_str())),
- Device(UserOwned::_structure)
+ Device(sr_dev_inst_user_new(
+ vendor.c_str(), model.c_str(), version.c_str()))
{
}
@@ -628,15 +680,15 @@ shared_ptr UserDevice::add_channel(unsigned int index,
{
check(sr_dev_inst_channel_add(Device::_structure,
index, type->id(), name.c_str()));
- struct sr_channel *structure = (struct sr_channel *)
- g_slist_last(sr_dev_inst_channels_get(Device::_structure))->data;
- Channel *channel = new Channel(structure);
- _channels[structure] = channel;
- return get_channel(structure);
+ GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure));
+ auto *const ch = static_cast(last->data);
+ unique_ptr channel {new Channel{ch}};
+ _channels.emplace(ch, move(channel));
+ return get_channel(ch);
}
Channel::Channel(struct sr_channel *structure) :
- ParentOwned(structure),
+ _structure(structure),
_type(ChannelType::get(_structure->type))
{
}
@@ -645,81 +697,82 @@ Channel::~Channel()
{
}
-string Channel::name()
+string Channel::name() const
{
return valid_string(_structure->name);
}
void Channel::set_name(string name)
{
- check(sr_dev_channel_name_set(_parent->_structure,
- _structure->index, name.c_str()));
+ check(sr_dev_channel_name_set(_structure, name.c_str()));
}
-const ChannelType *Channel::type()
+const ChannelType *Channel::type() const
{
return ChannelType::get(_structure->type);
}
-bool Channel::enabled()
+bool Channel::enabled() const
{
return _structure->enabled;
}
void Channel::set_enabled(bool value)
{
- check(sr_dev_channel_enable(_parent->_structure, _structure->index, value));
+ check(sr_dev_channel_enable(_structure, value));
}
-unsigned int Channel::index()
+unsigned int Channel::index() const
{
return _structure->index;
}
-ChannelGroup::ChannelGroup(Device *device,
+ChannelGroup::ChannelGroup(const Device *device,
struct sr_channel_group *structure) :
- ParentOwned(structure),
Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
{
- for (GSList *entry = structure->channels; entry; entry = entry->next)
- _channels.push_back(device->_channels[(struct sr_channel *)entry->data]);
+ for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) {
+ auto *const ch = static_cast(entry->data);
+ /* Note: This relies on Device::_channels to keep the Channel
+ * objects around over the lifetime of the ChannelGroup. */
+ _channels.push_back(device->_channels.find(ch)->second.get());
+ }
}
ChannelGroup::~ChannelGroup()
{
}
-string ChannelGroup::name()
+string ChannelGroup::name() const
{
- return valid_string(_structure->name);
+ return valid_string(config_channel_group->name);
}
vector> ChannelGroup::channels()
{
vector> result;
- for (auto channel : _channels)
- result.push_back(channel->get_shared_pointer(_parent));
+ for (const auto &channel : _channels)
+ result.push_back(channel->share_owned_by(_parent));
return result;
}
-Trigger::Trigger(shared_ptr context, string name) :
- UserOwned(sr_trigger_new(name.c_str())),
- _context(context)
+Trigger::Trigger(shared_ptr context, string name) :
+ _structure(sr_trigger_new(name.c_str())),
+ _context(move(context))
{
- for (auto stage = _structure->stages; stage; stage = stage->next)
- _stages.push_back(
- new TriggerStage((struct sr_trigger_stage *) stage->data));
+ for (auto *stage = _structure->stages; stage; stage = stage->next) {
+ unique_ptr ts {new TriggerStage{
+ static_cast(stage->data)}};
+ _stages.push_back(move(ts));
+ }
}
Trigger::~Trigger()
{
- for (auto stage: _stages)
- delete stage;
-
sr_trigger_free(_structure);
}
-string Trigger::name()
+string Trigger::name() const
{
return _structure->name;
}
@@ -727,30 +780,28 @@ string Trigger::name()
vector> Trigger::stages()
{
vector> result;
- for (auto stage : _stages)
- result.push_back(stage->get_shared_pointer(this));
+ for (const auto &stage : _stages)
+ result.push_back(stage->share_owned_by(shared_from_this()));
return result;
}
shared_ptr Trigger::add_stage()
{
- auto stage = new TriggerStage(sr_trigger_stage_add(_structure));
- _stages.push_back(stage);
- return stage->get_shared_pointer(this);
+ unique_ptr stage {new TriggerStage{sr_trigger_stage_add(_structure)}};
+ _stages.push_back(move(stage));
+ return _stages.back()->share_owned_by(shared_from_this());
}
-TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
- ParentOwned(structure)
+TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
+ _structure(structure)
{
}
TriggerStage::~TriggerStage()
{
- for (auto match : _matches)
- delete match;
}
-
-int TriggerStage::number()
+
+int TriggerStage::number() const
{
return _structure->stage;
}
@@ -758,8 +809,8 @@ int TriggerStage::number()
vector> TriggerStage::matches()
{
vector> result;
- for (auto match : _matches)
- result.push_back(match->get_shared_pointer(this));
+ for (const auto &match : _matches)
+ result.push_back(match->share_owned_by(shared_from_this()));
return result;
}
@@ -768,21 +819,23 @@ void TriggerStage::add_match(shared_ptr channel,
{
check(sr_trigger_match_add(_structure,
channel->_structure, type->id(), value));
- _matches.push_back(new TriggerMatch(
- (struct sr_trigger_match *) g_slist_last(
- _structure->matches)->data, channel));
+ GSList *const last = g_slist_last(_structure->matches);
+ unique_ptr match {new TriggerMatch{
+ static_cast(last->data),
+ move(channel)}};
+ _matches.push_back(move(match));
}
void TriggerStage::add_match(shared_ptr channel,
const TriggerMatchType *type)
{
- add_match(channel, type, NAN);
+ add_match(move(channel), type, NAN);
}
TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
shared_ptr channel) :
- ParentOwned(structure),
- _channel(channel)
+ _structure(structure),
+ _channel(move(channel))
{
}
@@ -795,19 +848,19 @@ shared_ptr TriggerMatch::channel()
return _channel;
}
-const TriggerMatchType *TriggerMatch::type()
+const TriggerMatchType *TriggerMatch::type() const
{
return TriggerMatchType::get(_structure->match);
}
-float TriggerMatch::value()
+float TriggerMatch::value() const
{
return _structure->value;
}
DatafeedCallbackData::DatafeedCallbackData(Session *session,
DatafeedCallbackFunction callback) :
- _callback(callback),
+ _callback(move(callback)),
_session(session)
{
}
@@ -816,61 +869,11 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *pkt)
{
auto device = _session->get_device(sdi);
- auto packet = shared_ptr(new Packet(device, pkt), Packet::Deleter());
- _callback(device, packet);
-}
-
-SourceCallbackData::SourceCallbackData(shared_ptr source) :
- _source(source)
-{
-}
-
-bool SourceCallbackData::run(int revents)
-{
- return _source->_callback((Glib::IOCondition) revents);
-}
-
-shared_ptr EventSource::create(int fd, Glib::IOCondition events,
- int timeout, SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_FD;
- result->_fd = fd;
- result->_events = events;
- return shared_ptr(result, EventSource::Deleter());
-}
-
-shared_ptr EventSource::create(Glib::PollFD pollfd, int timeout,
- SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_POLLFD;
- result->_pollfd = pollfd;
- return shared_ptr(result, EventSource::Deleter());
-}
-
-shared_ptr EventSource::create(Glib::RefPtr channel,
- Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_IOCHANNEL;
- result->_channel = channel;
- result->_events = events;
- return shared_ptr(result, EventSource::Deleter());
-}
-
-EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
- _timeout(timeout),
- _callback(callback)
-{
-}
-
-EventSource::~EventSource()
-{
+ shared_ptr packet {new Packet{device, pkt}, default_delete{}};
+ _callback(move(device), move(packet));
}
SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
- ParentOwned(structure),
Device(structure)
{
}
@@ -885,27 +888,25 @@ shared_ptr SessionDevice::get_shared_from_this()
}
Session::Session(shared_ptr context) :
- UserOwned(_structure),
- _context(context),
- _saving(false)
+ _structure(nullptr),
+ _context(move(context))
{
- check(sr_session_new(&_structure));
+ check(sr_session_new(_context->_structure, &_structure));
_context->_session = this;
}
Session::Session(shared_ptr context, string filename) :
- UserOwned(_structure),
- _context(context),
- _filename(filename),
- _saving(false)
+ _structure(nullptr),
+ _context(move(context)),
+ _filename(move(filename))
{
- check(sr_session_load(filename.c_str(), &_structure));
+ check(sr_session_load(_context->_structure, _filename.c_str(), &_structure));
GSList *dev_list;
check(sr_session_dev_list(_structure, &dev_list));
- for (GSList *dev = dev_list; dev; dev = dev->next)
- {
- auto sdi = (struct sr_dev_inst *) dev->data;
- _owned_devices[sdi] = new SessionDevice(sdi);
+ for (GSList *dev = dev_list; dev; dev = dev->next) {
+ auto *const sdi = static_cast(dev->data);
+ unique_ptr device {new SessionDevice{sdi}};
+ _owned_devices.emplace(sdi, move(device));
}
_context->_session = this;
}
@@ -913,22 +914,13 @@ Session::Session(shared_ptr context, string filename) :
Session::~Session()
{
check(sr_session_destroy(_structure));
-
- for (auto callback : _datafeed_callbacks)
- delete callback;
-
- 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));
+ _owned_devices[sdi]->share_owned_by(shared_from_this()));
else if (_other_devices.count(sdi))
return _other_devices[sdi];
else
@@ -937,8 +929,9 @@ shared_ptr Session::get_device(const struct sr_dev_inst *sdi)
void Session::add_device(shared_ptr device)
{
- check(sr_session_dev_add(_structure, device->_structure));
- _other_devices[device->_structure] = device;
+ const auto dev_struct = device->_structure;
+ check(sr_session_dev_add(_structure, dev_struct));
+ _other_devices[dev_struct] = move(device);
}
vector> Session::devices()
@@ -946,9 +939,8 @@ vector> Session::devices()
GSList *dev_list;
check(sr_session_dev_list(_structure, &dev_list));
vector> result;
- for (GSList *dev = dev_list; dev; dev = dev->next)
- {
- auto sdi = (struct sr_dev_inst *) dev->data;
+ for (GSList *dev = dev_list; dev; dev = dev->next) {
+ auto *const sdi = static_cast(dev->data);
result.push_back(get_device(sdi));
}
return result;
@@ -975,176 +967,53 @@ void Session::stop()
check(sr_session_stop(_structure));
}
-void Session::begin_save(string filename)
+bool Session::is_running() const
{
- _saving = true;
- _save_initialized = false;
- _save_filename = filename;
- _save_samplerate = 0;
+ const int ret = sr_session_is_running(_structure);
+ if (ret < 0)
+ throw Error{ret};
+ return (ret != 0);
}
-void Session::append(shared_ptr packet)
+static void session_stopped_callback(void *data) noexcept
{
- if (!_saving)
- throw Error(SR_ERR);
-
- switch (packet->_structure->type)
- {
- case SR_DF_META:
- {
- auto meta = (const struct sr_datafeed_meta *)
- packet->_structure->payload;
-
- for (auto l = meta->config; l; l = l->next)
- {
- auto config = (struct sr_config *) l->data;
- if (config->key == SR_CONF_SAMPLERATE)
- _save_samplerate = g_variant_get_uint64(config->data);
- }
-
- break;
- }
- case SR_DF_LOGIC:
- {
- if (_save_samplerate == 0)
- {
- GVariant *samplerate;
-
- check(sr_config_get(sr_dev_inst_driver_get(packet->_device->_structure),
- packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
- &samplerate));
-
- _save_samplerate = g_variant_get_uint64(samplerate);
-
- g_variant_unref(samplerate);
- }
-
- if (!_save_initialized)
- {
- vector> save_channels;
-
- for (auto channel : packet->_device->channels())
- if (channel->_structure->enabled &&
- channel->_structure->type == SR_CHANNEL_LOGIC)
- save_channels.push_back(channel);
-
- auto channels = g_new(char *, save_channels.size());
-
- int i = 0;
- for (auto channel : save_channels)
- channels[i++] = channel->_structure->name;
- channels[i] = NULL;
-
- int ret = sr_session_save_init(_structure, _save_filename.c_str(),
- _save_samplerate, channels);
-
- g_free(channels);
-
- if (ret != SR_OK)
- throw Error(ret);
-
- _save_initialized = true;
- }
-
- auto logic = (const struct sr_datafeed_logic *)
- packet->_structure->payload;
-
- check(sr_session_append(_structure, _save_filename.c_str(),
- (uint8_t *) logic->data, logic->unitsize,
- logic->length / logic->unitsize));
- }
- }
+ auto *const callback = static_cast(data);
+ (*callback)();
}
-void Session::append(void *data, size_t length, unsigned int unit_size)
+void Session::set_stopped_callback(SessionStoppedCallback callback)
{
- check(sr_session_append(_structure, _save_filename.c_str(),
- (uint8_t *) data, unit_size, length));
+ _stopped_callback = move(callback);
+ if (_stopped_callback)
+ check(sr_session_stopped_callback_set(_structure,
+ &session_stopped_callback, &_stopped_callback));
+ else
+ check(sr_session_stopped_callback_set(_structure,
+ nullptr, nullptr));
}
static void datafeed_callback(const struct sr_dev_inst *sdi,
- const struct sr_datafeed_packet *pkt, void *cb_data)
+ const struct sr_datafeed_packet *pkt, void *cb_data) noexcept
{
auto callback = static_cast(cb_data);
callback->run(sdi, pkt);
}
-
+
void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
{
- auto cb_data = new DatafeedCallbackData(this, callback);
+ unique_ptr cb_data
+ {new DatafeedCallbackData{this, move(callback)}};
check(sr_session_datafeed_callback_add(_structure,
- datafeed_callback, cb_data));
- _datafeed_callbacks.push_back(cb_data);
+ &datafeed_callback, cb_data.get()));
+ _datafeed_callbacks.push_back(move(cb_data));
}
-void Session::remove_datafeed_callbacks(void)
+void Session::remove_datafeed_callbacks()
{
check(sr_session_datafeed_callback_remove_all(_structure));
- for (auto callback : _datafeed_callbacks)
- delete callback;
_datafeed_callbacks.clear();
}
-static int source_callback(int fd, int revents, void *cb_data)
-{
- (void) fd;
- auto callback = (SourceCallbackData *) cb_data;
- return callback->run(revents);
-}
-
-void Session::add_source(shared_ptr source)
-{
- if (_source_callbacks.count(source) == 1)
- throw Error(SR_ERR_ARG);
-
- auto cb_data = new SourceCallbackData(source);
-
- switch (source->_type)
- {
- case EventSource::SOURCE_FD:
- check(sr_session_source_add(_structure, source->_fd, source->_events,
- source->_timeout, source_callback, cb_data));
- break;
- case EventSource::SOURCE_POLLFD:
- check(sr_session_source_add_pollfd(_structure,
- source->_pollfd.gobj(), source->_timeout, source_callback,
- cb_data));
- break;
- case EventSource::SOURCE_IOCHANNEL:
- check(sr_session_source_add_channel(_structure,
- source->_channel->gobj(), source->_events, source->_timeout,
- source_callback, cb_data));
- break;
- }
-
- _source_callbacks[source] = cb_data;
-}
-
-void Session::remove_source(shared_ptr source)
-{
- if (_source_callbacks.count(source) == 0)
- throw Error(SR_ERR_ARG);
-
- switch (source->_type)
- {
- case EventSource::SOURCE_FD:
- check(sr_session_source_remove(_structure, source->_fd));
- break;
- case EventSource::SOURCE_POLLFD:
- check(sr_session_source_remove_pollfd(_structure,
- source->_pollfd.gobj()));
- break;
- case EventSource::SOURCE_IOCHANNEL:
- check(sr_session_source_remove_channel(_structure,
- source->_channel->gobj()));
- break;
- }
-
- delete _source_callbacks[source];
-
- _source_callbacks.erase(source);
-}
-
shared_ptr Session::trigger()
{
return _trigger;
@@ -1154,13 +1023,13 @@ void Session::set_trigger(shared_ptr trigger)
{
if (!trigger)
// Set NULL trigger, i.e. remove any trigger from the session.
- check(sr_session_trigger_set(_structure, NULL));
+ check(sr_session_trigger_set(_structure, nullptr));
else
check(sr_session_trigger_set(_structure, trigger->_structure));
- _trigger = trigger;
+ _trigger = move(trigger);
}
-string Session::filename()
+string Session::filename() const
{
return _filename;
}
@@ -1172,44 +1041,39 @@ shared_ptr Session::context()
Packet::Packet(shared_ptr device,
const struct sr_datafeed_packet *structure) :
- UserOwned(structure),
- _device(device)
+ _structure(structure),
+ _device(move(device))
{
switch (structure->type)
{
case SR_DF_HEADER:
- _payload = new Header(
+ _payload.reset(new Header{
static_cast(
- structure->payload));
+ structure->payload)});
break;
case SR_DF_META:
- _payload = new Meta(
+ _payload.reset(new Meta{
static_cast(
- structure->payload));
+ structure->payload)});
break;
case SR_DF_LOGIC:
- _payload = new Logic(
+ _payload.reset(new Logic{
static_cast(
- structure->payload));
+ structure->payload)});
break;
case SR_DF_ANALOG:
- _payload = new Analog(
+ _payload.reset(new Analog{
static_cast(
- structure->payload));
- break;
- default:
- _payload = nullptr;
+ structure->payload)});
break;
}
}
Packet::~Packet()
{
- if (_payload)
- delete _payload;
}
-const PacketType *Packet::type()
+const PacketType *Packet::type() const
{
return PacketType::get(_structure->type);
}
@@ -1217,7 +1081,7 @@ const PacketType *Packet::type()
shared_ptr Packet::payload()
{
if (_payload)
- return _payload->get_shared_pointer(this);
+ return _payload->share_owned_by(shared_from_this());
else
throw Error(SR_ERR_NA);
}
@@ -1231,8 +1095,8 @@ PacketPayload::~PacketPayload()
}
Header::Header(const struct sr_datafeed_header *structure) :
- ParentOwned(structure),
- PacketPayload()
+ PacketPayload(),
+ _structure(structure)
{
}
@@ -1240,18 +1104,18 @@ Header::~Header()
{
}
-shared_ptr Header::get_shared_pointer(Packet *_parent)
+shared_ptr Header::share_owned_by(shared_ptr _parent)
{
return static_pointer_cast(
- ParentOwned::get_shared_pointer(_parent));
+ ParentOwned::share_owned_by(_parent));
}
-int Header::feed_version()
+int Header::feed_version() const
{
return _structure->feed_version;
}
-Glib::TimeVal Header::start_time()
+Glib::TimeVal Header::start_time() const
{
return Glib::TimeVal(
_structure->starttime.tv_sec,
@@ -1259,8 +1123,8 @@ Glib::TimeVal Header::start_time()
}
Meta::Meta(const struct sr_datafeed_meta *structure) :
- ParentOwned(structure),
- PacketPayload()
+ PacketPayload(),
+ _structure(structure)
{
}
@@ -1268,26 +1132,25 @@ Meta::~Meta()
{
}
-shared_ptr Meta::get_shared_pointer(Packet *_parent)
+shared_ptr Meta::share_owned_by(shared_ptr _parent)
{
return static_pointer_cast(
- ParentOwned::get_shared_pointer(_parent));
+ ParentOwned::share_owned_by(_parent));
}
-map Meta::config()
+map Meta::config() const
{
map result;
- for (auto l = _structure->config; l; l = l->next)
- {
- auto config = (struct sr_config *) l->data;
- result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data);
+ 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, true);
}
return result;
}
Logic::Logic(const struct sr_datafeed_logic *structure) :
- ParentOwned(structure),
- PacketPayload()
+ PacketPayload(),
+ _structure(structure)
{
}
@@ -1295,10 +1158,10 @@ Logic::~Logic()
{
}
-shared_ptr Logic::get_shared_pointer(Packet *_parent)
+shared_ptr Logic::share_owned_by(shared_ptr _parent)
{
return static_pointer_cast(
- ParentOwned::get_shared_pointer(_parent));
+ ParentOwned::share_owned_by(_parent));
}
void *Logic::data_pointer()
@@ -1306,19 +1169,19 @@ void *Logic::data_pointer()
return _structure->data;
}
-size_t Logic::data_length()
+size_t Logic::data_length() const
{
return _structure->length;
}
-unsigned int Logic::unit_size()
+unsigned int Logic::unit_size() const
{
return _structure->unitsize;
}
Analog::Analog(const struct sr_datafeed_analog *structure) :
- ParentOwned(structure),
- PacketPayload()
+ PacketPayload(),
+ _structure(structure)
{
}
@@ -1326,18 +1189,23 @@ Analog::~Analog()
{
}
-shared_ptr Analog::get_shared_pointer(Packet *_parent)
+shared_ptr Analog::share_owned_by(shared_ptr _parent)
{
return static_pointer_cast(
- ParentOwned::get_shared_pointer(_parent));
+ ParentOwned::share_owned_by(_parent));
}
-float *Analog::data_pointer()
+void *Analog::data_pointer()
{
return _structure->data;
}
-unsigned int Analog::num_samples()
+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;
}
@@ -1345,29 +1213,154 @@ unsigned int Analog::num_samples()
vector> Analog::channels()
{
vector> result;
- for (auto l = _structure->channels; l; l = l->next)
- result.push_back(_parent->_device->get_channel(
- (struct sr_channel *)l->data));
+ for (auto l = _structure->meaning->channels; l; l = l->next) {
+ auto *const ch = static_cast(l->data);
+ result.push_back(_parent->_device->get_channel(ch));
+ }
return result;
}
-const Quantity *Analog::mq()
+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);
+}
+
+const Unit *Analog::unit() const
+{
+ return Unit::get(_structure->meaning->unit);
+}
+
+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
{
- return Quantity::get(_structure->mq);
+ 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;
}
-const Unit *Analog::unit()
+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)
{
- return Unit::get(_structure->unit);
}
-vector Analog::mq_flags()
+Rational::~Rational()
{
- return QuantityFlag::flags_from_mask(_structure->mqflags);
+}
+
+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) :
- ParentOwned(structure)
+ _structure(structure)
{
}
@@ -1375,17 +1368,17 @@ InputFormat::~InputFormat()
{
}
-string InputFormat::name()
+string InputFormat::name() const
{
return valid_string(sr_input_id_get(_structure));
}
-string InputFormat::description()
+string InputFormat::description() const
{
return valid_string(sr_input_description_get(_structure));
}
-vector InputFormat::extensions()
+vector InputFormat::extensions() const
{
vector exts;
for (const char *const *e = sr_input_extensions_get(_structure);
@@ -1396,15 +1389,17 @@ vector InputFormat::extensions()
map> InputFormat::options()
{
- const struct sr_option **options = sr_input_options_get(_structure);
map> result;
- if (options)
- {
- auto option_array = shared_ptr(
- options, sr_input_options_free);
- for (int i = 0; options[i]; i++)
- result[options[i]->id] = shared_ptr