X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=inline;f=bindings%2Fcxx%2Fclasses.cpp;h=a0b161a62a35aee683894cfe87b0b8c521d8d42a;hb=aedbf89d85cc7efa1263f5e210c3b78097ad471a;hp=986b2c1e1da3fe60daa96ffbb0b15f9d71434f6d;hpb=ca3291e3ee7415c4c4505164ec931b91ea15cefe;p=libsigrok.git
diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp
index 986b2c1e..a0b161a6 100644
--- a/bindings/cxx/classes.cpp
+++ b/bindings/cxx/classes.cpp
@@ -17,9 +17,16 @@
* along with this program. If not, see .
*/
-#include "libsigrok/libsigrok.hpp"
+/* Needed for isascii(), as used in the GNU libstdc++ headers */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+
+#include
+#include
#include
+#include
namespace sigrok
{
@@ -32,165 +39,180 @@ 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_variant_hash, g_variant_equal, g_free,
- (void (*)(void *))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() :
- session(NULL)
-{
- 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]);
-}
-
-string Context::get_package_version()
+ _structure(nullptr),
+ _session(nullptr)
+{
+ check(sr_init(&_structure));
+
+ 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()
{
return sr_package_version_string_get();
}
-string Context::get_lib_version()
+string Context::lib_version()
{
return sr_lib_version_string_get();
}
-map> Context::get_drivers()
+map> Context::drivers()
{
map> result;
- for (auto entry: drivers)
- {
- auto name = entry.first;
- auto driver = entry.second;
- result[name] = static_pointer_cast(
- 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;
}
-map> Context::get_input_formats()
+map> Context::input_formats()
{
map> result;
- for (auto entry: input_formats)
- {
- auto name = entry.first;
- auto input_format = entry.second;
- result[name] = static_pointer_cast(
- 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;
}
-map> Context::get_output_formats()
+map> Context::output_formats()
{
map> result;
- for (auto entry: output_formats)
- {
- auto name = entry.first;
- auto output_format = entry.second;
- result[name] = static_pointer_cast(
- 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));
+ check(sr_exit(_structure));
}
-const LogLevel *Context::get_log_level()
+const LogLevel *Context::log_level() const
{
return LogLevel::get(sr_log_loglevel_get());
}
void Context::set_log_level(const LogLevel *level)
{
- check(sr_log_loglevel_set(level->get_id()));
+ check(sr_log_loglevel_set(level->id()));
}
-string Context::get_log_domain()
+static int call_log_callback(void *cb_data, int loglevel,
+ const char *format, va_list args) noexcept
{
- 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)
-{
- 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;
}
@@ -199,124 +221,260 @@ static int call_log_callback(void *cb_data, int loglevel, const char *format, va
void Context::set_log_callback(LogCallbackFunction callback)
{
- log_callback = callback;
- check(sr_log_callback_set(call_log_callback, &log_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;
-}
+ _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{move(vendor), move(model), move(version)},
+ default_delete{}};
+}
+
+shared_ptr Context::create_header_packet(Glib::TimeVal start_time)
+{
+ auto header = g_new(struct sr_datafeed_header, 1);
+ header->feed_version = 1;
+ header->starttime.tv_sec = start_time.tv_sec;
+ header->starttime.tv_usec = start_time.tv_usec;
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_HEADER;
+ packet->payload = header;
+ 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 (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_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},
+ default_delete{}};
+}
+
+shared_ptr Context::create_logic_packet(
+ void *data_pointer, size_t data_length, unsigned int unit_size)
+{
+ auto logic = g_new(struct sr_datafeed_logic, 1);
+ logic->length = data_length;
+ logic->unitsize = unit_size;
+ logic->data = data_pointer;
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_LOGIC;
+ packet->payload = logic;
+ return shared_ptr{new Packet{nullptr, packet}, default_delete{}};
+}
+
+shared_ptr Context::create_analog_packet(
+ 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);
+ 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->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::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)
{
- auto input = sr_input_scan_file(filename.c_str());
- if (!input)
- throw Error(SR_ERR_NA);
- return shared_ptr(
- new Input(shared_from_this(), input), Input::Deleter());
+ const struct sr_input *input;
+
+ check(sr_input_scan_file(filename.c_str(), &input));
+ return shared_ptr{
+ new Input{shared_from_this(), input},
+ default_delete{}};
}
shared_ptr Context::open_stream(string header)
{
+ const struct sr_input *input;
+
auto gstr = g_string_new(header.c_str());
- auto input = sr_input_scan_buffer(gstr);
- g_string_free(gstr, false);
- if (!input)
- throw Error(SR_ERR_NA);
- return shared_ptr(
- new Input(shared_from_this(), input), Input::Deleter());
+ auto ret = sr_input_scan_buffer(gstr, &input);
+ g_string_free(gstr, true);
+ check(ret);
+ return shared_ptr{
+ new Input{shared_from_this(), input},
+ default_delete{}};
+}
+
+map Context::serials(shared_ptr driver) const
+{
+ GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr);
+ map serials;
+
+ for (GSList *serial = serial_list; serial; serial = serial->next) {
+ auto *const port = static_cast(serial->data);
+ serials[string(port->name)] = string(port->description);
+ }
+
+ g_slist_free_full(serial_list,
+ reinterpret_cast(&sr_serial_free));
+ return serials;
}
Driver::Driver(struct sr_dev_driver *structure) :
- StructureWrapper(structure),
- initialized(false)
+ Configurable(structure, nullptr, nullptr),
+ _structure(structure),
+ _initialized(false)
{
}
Driver::~Driver()
{
- for (auto device : devices)
- delete device;
}
-string Driver::get_name()
+string Driver::name() const
+{
+ return valid_string(_structure->name);
+}
+
+string Driver::long_name() const
{
- return valid_string(structure->name);
+ return valid_string(_structure->longname);
}
-string Driver::get_long_name()
+set Driver::scan_options() const
{
- return valid_string(structure->longname);
+ 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)
- {
- check(sr_driver_init(parent->structure, structure));
- initialized = true;
+ if (!_initialized) {
+ check(sr_driver_init(_parent->_structure, _structure));
+ _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)
- {
- auto key = entry.first;
- auto value = entry.second;
- auto config = g_new(struct sr_config, 1);
- config->key = key->get_id();
- config->data = value.gobj();
+ 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 = const_cast(value.gobj());
option_list = g_slist_append(option_list, config);
}
/* Run scan. */
- GSList *device_list = sr_driver_scan(structure, option_list);
+ GSList *device_list = sr_driver_scan(_structure, option_list);
/* Free option list. */
g_slist_free_full(option_list, g_free);
+
/* Create device objects. */
- for (GSList *device = device_list; device; device = device->next)
- {
- auto sdi = (struct sr_dev_inst *) device->data;
- devices.push_back(new HardwareDevice(this, sdi));
+ vector> result;
+ 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. */
g_slist_free(device_list);
- /* Create list of shared pointers to device instances for return. */
- vector> result;
- for (auto device : devices)
- result.push_back(static_pointer_cast(
- device->get_shared_pointer(parent)));
return result;
}
@@ -334,127 +492,157 @@ 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(
config_driver, config_sdi, config_channel_group,
- key->get_id(), &data));
+ key->id(), &data));
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->get_id(), value.gobj()));
+ key->id(), const_cast(value.gobj())));
}
-Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
+set Configurable::config_capabilities(const ConfigKey *key) const
+{
+ int caps = sr_dev_config_capabilities_list(config_sdi,
+ config_channel_group, key->id());
+
+ set result;
+
+ for (auto cap: Capability::values())
+ if (caps & cap->id())
+ result.insert(cap);
+
+ return result;
+}
+
+bool Configurable::config_check(const ConfigKey *key,
+ const Capability *capability) const
+{
+ int caps = sr_dev_config_capabilities_list(config_sdi,
+ config_channel_group, key->id());
+
+ return (caps & capability->id());
+}
+
+Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const
{
GVariant *data;
check(sr_config_list(
config_driver, config_sdi, config_channel_group,
- key->get_id(), &data));
+ key->id(), &data));
return Glib::VariantContainerBase(data);
}
Device::Device(struct sr_dev_inst *structure) :
- Configurable(structure->driver, structure, NULL),
- StructureWrapper(structure)
+ Configurable(sr_dev_inst_driver_get(structure), structure, nullptr),
+ _structure(structure)
{
- for (GSList *entry = structure->channels; 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 = structure->channel_groups; 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::get_description()
+string Device::vendor() const
{
- ostringstream s;
-
- vector parts =
- {get_vendor(), get_model(), get_version()};
-
- for (string part : parts)
- if (part.length() > 0)
- s << part;
+ return valid_string(sr_dev_inst_vendor_get(_structure));
+}
- return s.str();
+string Device::model() const
+{
+ return valid_string(sr_dev_inst_model_get(_structure));
}
-string Device::get_vendor()
+string Device::version() const
{
- return valid_string(structure->vendor);
+ return valid_string(sr_dev_inst_version_get(_structure));
}
-string Device::get_model()
+string Device::serial_number() const
{
- return valid_string(structure->model);
+ return valid_string(sr_dev_inst_sernum_get(_structure));
}
-string Device::get_version()
+string Device::connection_id() const
{
- return valid_string(structure->version);
+ return valid_string(sr_dev_inst_connid_get(_structure));
}
-vector> Device::get_channels()
+vector> Device::channels()
{
vector> result;
- for (auto entry : channels)
- result.push_back(static_pointer_cast(
- entry.second->get_shared_pointer(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 static_pointer_cast(
- channels[ptr]->get_shared_pointer(this));
+ return _channels[ptr]->share_owned_by(get_shared_from_this());
}
map>
-Device::get_channel_groups()
+Device::channel_groups()
{
map> result;
- for (auto entry: channel_groups)
- {
- auto name = entry.first;
- auto channel_group = entry.second;
- result[name] = static_pointer_cast(
- channel_group->get_shared_pointer(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;
}
void Device::open()
{
- check(sr_dev_open(structure));
+ check(sr_dev_open(_structure));
}
void Device::close()
{
- check(sr_dev_close(structure));
+ check(sr_dev_close(_structure));
}
-HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
+HardwareDevice::HardwareDevice(shared_ptr driver,
+ struct sr_dev_inst *structure) :
Device(structure),
- driver(driver)
+ _driver(move(driver))
{
}
@@ -462,14 +650,46 @@ HardwareDevice::~HardwareDevice()
{
}
-shared_ptr HardwareDevice::get_driver()
+shared_ptr HardwareDevice::get_shared_from_this()
+{
+ return static_pointer_cast(shared_from_this());
+}
+
+shared_ptr HardwareDevice::driver()
+{
+ return _driver;
+}
+
+UserDevice::UserDevice(string vendor, string model, string version) :
+ Device(sr_dev_inst_user_new(
+ vendor.c_str(), model.c_str(), version.c_str()))
+{
+}
+
+UserDevice::~UserDevice()
+{
+}
+
+shared_ptr UserDevice::get_shared_from_this()
+{
+ return static_pointer_cast(shared_from_this());
+}
+
+shared_ptr UserDevice::add_channel(unsigned int index,
+ const ChannelType *type, string name)
{
- return static_pointer_cast(driver->get_shared_pointer(parent));
+ check(sr_dev_inst_channel_add(Device::_structure,
+ index, type->id(), name.c_str()));
+ 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) :
- StructureWrapper(structure),
- type(ChannelType::get(structure->type))
+ _structure(structure),
+ _type(ChannelType::get(_structure->type))
{
}
@@ -477,139 +697,145 @@ Channel::~Channel()
{
}
-string Channel::get_name()
+string Channel::name() const
{
- return valid_string(structure->name);
+ 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::get_type()
+const ChannelType *Channel::type() const
{
- return ChannelType::get(structure->type);
+ return ChannelType::get(_structure->type);
}
-bool Channel::get_enabled()
+bool Channel::enabled() const
{
- return structure->enabled;
+ 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::get_index()
+unsigned int Channel::index() const
{
- return structure->index;
+ return _structure->index;
}
-ChannelGroup::ChannelGroup(Device *device,
+ChannelGroup::ChannelGroup(const Device *device,
struct sr_channel_group *structure) :
- StructureWrapper(structure),
- Configurable(device->structure->driver, device->structure, 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::get_name()
+string ChannelGroup::name() const
{
- return valid_string(structure->name);
+ return valid_string(config_channel_group->name);
}
-vector> ChannelGroup::get_channels()
+vector> ChannelGroup::channels()
{
vector> result;
- for (auto channel : channels)
- result.push_back(static_pointer_cast(
- 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) :
- structure(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);
+ sr_trigger_free(_structure);
}
-string Trigger::get_name()
+string Trigger::name() const
{
- return structure->name;
+ return _structure->name;
}
-vector> Trigger::get_stages()
+vector> Trigger::stages()
{
vector> result;
- for (auto stage : stages)
- result.push_back(static_pointer_cast(
- 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 static_pointer_cast(
- 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) :
- StructureWrapper(structure)
+TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
+ _structure(structure)
{
}
TriggerStage::~TriggerStage()
{
- for (auto match : matches)
- delete match;
}
-
-int TriggerStage::get_number()
+
+int TriggerStage::number() const
{
- return structure->stage;
+ return _structure->stage;
}
-vector> TriggerStage::get_matches()
+vector> TriggerStage::matches()
{
vector> result;
- for (auto match : matches)
- result.push_back(static_pointer_cast(
- match->get_shared_pointer(this)));
+ for (const auto &match : _matches)
+ result.push_back(match->share_owned_by(shared_from_this()));
return result;
}
-void TriggerStage::add_match(shared_ptr channel, const TriggerMatchType *type, float value)
+void TriggerStage::add_match(shared_ptr channel,
+ const TriggerMatchType *type, float value)
{
- check(sr_trigger_match_add(structure, channel->structure, type->get_id(), value));
- matches.push_back(new TriggerMatch(
- (struct sr_trigger_match *) g_slist_last(structure->matches)->data, channel));
+ check(sr_trigger_match_add(_structure,
+ channel->_structure, type->id(), value));
+ 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)
+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) :
- StructureWrapper(structure), channel(channel)
+TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
+ shared_ptr channel) :
+ _structure(structure),
+ _channel(move(channel))
{
}
@@ -617,375 +843,247 @@ TriggerMatch::~TriggerMatch()
{
}
-shared_ptr TriggerMatch::get_channel()
+shared_ptr TriggerMatch::channel()
{
- return channel;
+ return _channel;
}
-const TriggerMatchType *TriggerMatch::get_type()
+const TriggerMatchType *TriggerMatch::type() const
{
- return TriggerMatchType::get(structure->match);
+ return TriggerMatchType::get(_structure->match);
}
-float TriggerMatch::get_value()
+float TriggerMatch::value() const
{
- return structure->value;
+ return _structure->value;
}
DatafeedCallbackData::DatafeedCallbackData(Session *session,
DatafeedCallbackFunction callback) :
- callback(callback), session(session)
+ _callback(move(callback)),
+ _session(session)
{
}
void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *pkt)
{
- auto device = session->devices[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);
+ auto device = _session->get_device(sdi);
+ shared_ptr packet {new Packet{device, pkt}, default_delete{}};
+ _callback(move(device), move(packet));
}
-shared_ptr EventSource::create(int fd, Glib::IOCondition events,
- int timeout, SourceCallbackFunction callback)
+SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
+ Device(structure)
{
- 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)
+SessionDevice::~SessionDevice()
{
- 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 SessionDevice::get_shared_from_this()
{
+ return static_pointer_cast(shared_from_this());
}
Session::Session(shared_ptr context) :
- context(context), saving(false)
+ _structure(nullptr),
+ _context(move(context))
{
- check(sr_session_new(&structure));
- context->session = this;
+ check(sr_session_new(_context->_structure, &_structure));
+ _context->_session = this;
}
Session::Session(shared_ptr context, string filename) :
- context(context), saving(false)
+ _structure(nullptr),
+ _context(move(context)),
+ _filename(move(filename))
{
- check(sr_session_load(filename.c_str(), &structure));
- context->session = this;
+ 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 *const sdi = static_cast(dev->data);
+ unique_ptr device {new SessionDevice{sdi}};
+ _owned_devices.emplace(sdi, move(device));
+ }
+ _context->_session = this;
}
Session::~Session()
{
- check(sr_session_destroy(structure));
-
- for (auto callback : datafeed_callbacks)
- delete callback;
+ check(sr_session_destroy(_structure));
+}
- for (auto entry : source_callbacks)
- 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]->share_owned_by(shared_from_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;
+ const auto dev_struct = device->_structure;
+ check(sr_session_dev_add(_structure, dev_struct));
+ _other_devices[dev_struct] = move(device);
}
-vector> Session::get_devices()
+vector> Session::devices()
{
GSList *dev_list;
- check(sr_session_dev_list(structure, &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;
- if (devices.count(sdi) == 0)
- devices[sdi] = shared_ptr(
- new Device(sdi), Device::Deleter());
- result.push_back(devices[sdi]);
+ for (GSList *dev = dev_list; dev; dev = dev->next) {
+ auto *const sdi = static_cast(dev->data);
+ result.push_back(get_device(sdi));
}
return result;
}
void Session::remove_devices()
{
- devices.clear();
- check(sr_session_dev_remove_all(structure));
+ _other_devices.clear();
+ check(sr_session_dev_remove_all(_structure));
}
void Session::start()
{
- check(sr_session_start(structure));
+ check(sr_session_start(_structure));
}
void Session::run()
{
- check(sr_session_run(structure));
+ check(sr_session_run(_structure));
}
void Session::stop()
{
- check(sr_session_stop(structure));
+ 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(packet->device->structure->driver,
- 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->get_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);
- check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data));
- datafeed_callbacks.push_back(cb_data);
+ unique_ptr cb_data
+ {new DatafeedCallbackData{this, move(callback)}};
+ check(sr_session_datafeed_callback_add(_structure,
+ &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();
+ check(sr_session_datafeed_callback_remove_all(_structure));
+ _datafeed_callbacks.clear();
}
-static int source_callback(int fd, int revents, void *cb_data)
+shared_ptr Session::trigger()
{
- (void) fd;
- auto callback = (SourceCallbackData *) cb_data;
- return callback->run(revents);
+ return _trigger;
}
-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)
+void Session::set_trigger(shared_ptr trigger)
{
- 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);
+ if (!trigger)
+ // Set NULL trigger, i.e. remove any trigger from the session.
+ check(sr_session_trigger_set(_structure, nullptr));
+ else
+ check(sr_session_trigger_set(_structure, trigger->_structure));
+ _trigger = move(trigger);
}
-shared_ptr Session::get_trigger()
+string Session::filename() const
{
- return trigger;
+ return _filename;
}
-void Session::set_trigger(shared_ptr trigger)
+shared_ptr Session::context()
{
- check(sr_session_trigger_set(structure, trigger->structure));
- this->trigger = trigger;
+ return _context;
}
Packet::Packet(shared_ptr device,
const struct sr_datafeed_packet *structure) :
- structure(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));
+ structure->payload)});
break;
}
}
Packet::~Packet()
{
- if (payload)
- delete payload;
}
-const PacketType *Packet::get_type()
+const PacketType *Packet::type() const
{
- return PacketType::get(structure->type);
+ return PacketType::get(_structure->type);
}
-shared_ptr Packet::get_payload()
+shared_ptr Packet::payload()
{
- return payload->get_shared_pointer(this);
+ if (_payload)
+ return _payload->share_owned_by(shared_from_this());
+ else
+ throw Error(SR_ERR_NA);
}
PacketPayload::PacketPayload()
@@ -998,7 +1096,7 @@ PacketPayload::~PacketPayload()
Header::Header(const struct sr_datafeed_header *structure) :
PacketPayload(),
- StructureWrapper(structure)
+ _structure(structure)
{
}
@@ -1006,21 +1104,27 @@ Header::~Header()
{
}
-int Header::get_feed_version()
+shared_ptr Header::share_owned_by(shared_ptr _parent)
+{
+ return static_pointer_cast(
+ ParentOwned::share_owned_by(_parent));
+}
+
+int Header::feed_version() const
{
- return structure->feed_version;
+ return _structure->feed_version;
}
-Glib::TimeVal Header::get_start_time()
+Glib::TimeVal Header::start_time() const
{
return Glib::TimeVal(
- structure->starttime.tv_sec,
- structure->starttime.tv_usec);
+ _structure->starttime.tv_sec,
+ _structure->starttime.tv_usec);
}
Meta::Meta(const struct sr_datafeed_meta *structure) :
PacketPayload(),
- StructureWrapper(structure)
+ _structure(structure)
{
}
@@ -1028,20 +1132,25 @@ Meta::~Meta()
{
}
-map Meta::get_config()
+shared_ptr Meta::share_owned_by(shared_ptr _parent)
+{
+ return static_pointer_cast(
+ ParentOwned::share_owned_by(_parent));
+}
+
+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) :
PacketPayload(),
- StructureWrapper(structure)
+ _structure(structure)
{
}
@@ -1049,24 +1158,30 @@ Logic::~Logic()
{
}
-void *Logic::get_data_pointer()
+shared_ptr Logic::share_owned_by(shared_ptr _parent)
{
- return structure->data;
+ return static_pointer_cast(
+ ParentOwned::share_owned_by(_parent));
}
-size_t Logic::get_data_length()
+void *Logic::data_pointer()
{
- return structure->length;
+ return _structure->data;
}
-unsigned int Logic::get_unit_size()
+size_t Logic::data_length() const
{
- return structure->unitsize;
+ return _structure->length;
+}
+
+unsigned int Logic::unit_size() const
+{
+ return _structure->unitsize;
}
Analog::Analog(const struct sr_datafeed_analog *structure) :
PacketPayload(),
- StructureWrapper(structure)
+ _structure(structure)
{
}
@@ -1074,42 +1189,178 @@ Analog::~Analog()
{
}
-float *Analog::get_data_pointer()
+shared_ptr Analog::share_owned_by(shared_ptr _parent)
+{
+ return static_pointer_cast(
+ ParentOwned::share_owned_by(_parent));
+}
+
+void *Analog::data_pointer()
{
- return structure->data;
+ return _structure->data;
}
-unsigned int Analog::get_num_samples()
+void Analog::get_data_as_float(float *dest)
{
- return structure->num_samples;
+ check(sr_analog_to_float(_structure, dest));
}
-vector> Analog::get_channels()
+unsigned int Analog::num_samples() const
+{
+ return _structure->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::get_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->mq);
+ return Quantity::get(_structure->meaning->mq);
}
-const Unit *Analog::get_unit()
+const Unit *Analog::unit() const
{
- return Unit::get(structure->unit);
+ return Unit::get(_structure->meaning->unit);
}
-vector Analog::get_mq_flags()
+vector Analog::mq_flags() const
{
- return QuantityFlag::flags_from_mask(structure->mqflags);
+ 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) :
- StructureWrapper(structure)
+ _structure(structure)
{
}
@@ -1117,65 +1368,96 @@ InputFormat::~InputFormat()
{
}
-string InputFormat::get_name()
+string InputFormat::name() const
+{
+ return valid_string(sr_input_id_get(_structure));
+}
+
+string InputFormat::description() const
{
- return valid_string(sr_input_id_get(structure));
+ return valid_string(sr_input_description_get(_structure));
}
-string InputFormat::get_description()
+vector InputFormat::extensions() const
{
- return valid_string(sr_input_description_get(structure));
+ vector exts;
+ for (const char *const *e = sr_input_extensions_get(_structure);
+ e && *e; e++)
+ exts.push_back(*e);
+ return exts;
+}
+
+map> InputFormat::options()
+{
+ map> result;
+
+ 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++) {
+ shared_ptr