X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=a00f2967e45161e56b0d2df829cfa9c915ede926;hb=d370545d6071690e7a8cc747db1134ced091e0de;hp=0916a2dafb84c1d1ec9f28e39488eb03688106ab;hpb=60f6b00144d5b5a2446e55f0389009a468a79a7d;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 0916a2da..a00f2967 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 @@ -42,7 +48,7 @@ static const char *valid_string(const char *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); @@ -50,7 +56,7 @@ static GHashTable *map_to_hash_variant(map 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) @@ -66,6 +72,50 @@ Error::~Error() throw() { } +ResourceReader::~ResourceReader() +{ +} + +SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, + const char *name, void *cb_data) +{ + 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) +{ + 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 ssize_t ResourceReader::read_callback(const struct sr_resource *res, + void *buf, size_t count, void *cb_data) +{ + 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()); @@ -77,7 +127,7 @@ Context::Context() : { check(sr_init(&_structure)); - struct sr_dev_driver **driver_list = sr_driver_list(); + struct sr_dev_driver **driver_list = sr_driver_list(_structure); if (driver_list) for (int i = 0; driver_list[i]; i++) _drivers[driver_list[i]->name] = @@ -161,16 +211,6 @@ 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) { va_list args_copy; @@ -198,15 +238,28 @@ 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() { @@ -234,7 +287,7 @@ shared_ptr Context::create_header_packet(Glib::TimeVal start_time) } shared_ptr Context::create_meta_packet( - map config) + const map &config) { auto meta = g_new0(struct sr_datafeed_meta, 1); for (auto input : config) @@ -267,17 +320,21 @@ shared_ptr Context::create_logic_packet( } shared_ptr Context::create_analog_packet( - vector > channels, + const vector > &channels, float *data_pointer, unsigned int num_samples, const Quantity *mq, - const Unit *unit, vector mqflags) + const Unit *unit, const vector &mqflags) { auto analog = g_new0(struct sr_datafeed_analog, 1); + auto meaning = g_new0(struct sr_analog_meaning, 1); + + analog->meaning = meaning; + for (auto channel : channels) - analog->channels = g_slist_append(analog->channels, channel->_structure); + meaning->channels = g_slist_append(meaning->channels, channel->_structure); analog->num_samples = num_samples; - analog->mq = mq->id(); - analog->unit = unit->id(); - analog->mqflags = QuantityFlag::mask_from_flags(mqflags); + meaning->mq = (sr_mq)mq->id(); + meaning->unit = (sr_unit)unit->id(); + meaning->mqflags = (sr_mqflag)QuantityFlag::mask_from_flags(mqflags); analog->data = data_pointer; auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_ANALOG; @@ -288,13 +345,13 @@ shared_ptr Context::create_analog_packet( shared_ptr Context::load_session(string filename) { return shared_ptr( - new Session(shared_from_this(), filename), Session::Deleter()); + new Session(shared_from_this(), move(filename)), Session::Deleter()); } shared_ptr Context::create_trigger(string name) { return shared_ptr( - new Trigger(shared_from_this(), name), Trigger::Deleter()); + new Trigger(shared_from_this(), move(name)), Trigger::Deleter()); } shared_ptr Context::open_file(string filename) @@ -354,7 +411,7 @@ string Driver::long_name() } vector> Driver::scan( - map options) + const map &options) { /* Initialise the driver if not yet done. */ if (!_initialized) @@ -421,11 +478,11 @@ 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())); + key->id(), const_cast(value.gobj()))); } Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) @@ -589,7 +646,7 @@ HardwareDevice::HardwareDevice(shared_ptr driver, struct sr_dev_inst *structure) : UserOwned(structure), Device(structure), - _driver(driver) + _driver(move(driver)) { } @@ -703,7 +760,7 @@ vector> ChannelGroup::channels() Trigger::Trigger(shared_ptr context, string name) : UserOwned(sr_trigger_new(name.c_str())), - _context(context) + _context(move(context)) { for (auto stage = _structure->stages; stage; stage = stage->next) _stages.push_back( @@ -769,19 +826,19 @@ void TriggerStage::add_match(shared_ptr channel, channel->_structure, type->id(), value)); _matches.push_back(new TriggerMatch( (struct sr_trigger_match *) g_slist_last( - _structure->matches)->data, channel)); + _structure->matches)->data, move(channel))); } 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) + _channel(move(channel)) { } @@ -806,7 +863,7 @@ float TriggerMatch::value() DatafeedCallbackData::DatafeedCallbackData(Session *session, DatafeedCallbackFunction callback) : - _callback(callback), + _callback(move(callback)), _session(session) { } @@ -816,56 +873,7 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, { 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() -{ + _callback(move(device), move(packet)); } SessionDevice::SessionDevice(struct sr_dev_inst *structure) : @@ -885,20 +893,18 @@ shared_ptr SessionDevice::get_shared_from_this() Session::Session(shared_ptr context) : UserOwned(_structure), - _context(context), - _saving(false) + _context(move(context)) { - check(sr_session_new(context->_structure, &_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) + _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) @@ -916,9 +922,6 @@ Session::~Session() for (auto callback : _datafeed_callbacks) delete callback; - for (auto entry : _source_callbacks) - delete entry.second; - for (auto entry : _owned_devices) delete entry.second; } @@ -936,8 +939,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() @@ -974,91 +978,29 @@ 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) { - 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, @@ -1070,13 +1012,13 @@ static void datafeed_callback(const struct sr_dev_inst *sdi, void Session::add_datafeed_callback(DatafeedCallbackFunction callback) { - auto cb_data = new DatafeedCallbackData(this, callback); + auto cb_data = new DatafeedCallbackData(this, move(callback)); check(sr_session_datafeed_callback_add(_structure, datafeed_callback, cb_data)); _datafeed_callbacks.push_back(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) @@ -1084,66 +1026,6 @@ void Session::remove_datafeed_callbacks(void) _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; @@ -1156,7 +1038,7 @@ void Session::set_trigger(shared_ptr trigger) check(sr_session_trigger_set(_structure, NULL)); else check(sr_session_trigger_set(_structure, trigger->_structure)); - _trigger = trigger; + _trigger = move(trigger); } string Session::filename() @@ -1172,7 +1054,7 @@ shared_ptr Session::context() Packet::Packet(shared_ptr device, const struct sr_datafeed_packet *structure) : UserOwned(structure), - _device(device) + _device(move(device)) { switch (structure->type) { @@ -1331,7 +1213,7 @@ shared_ptr Analog::get_shared_pointer(Packet *_parent) ParentOwned::get_shared_pointer(_parent)); } -float *Analog::data_pointer() +void *Analog::data_pointer() { return _structure->data; } @@ -1344,7 +1226,7 @@ unsigned int Analog::num_samples() vector> Analog::channels() { vector> result; - for (auto l = _structure->channels; l; l = l->next) + for (auto l = _structure->meaning->channels; l; l = l->next) result.push_back(_parent->_device->get_channel( (struct sr_channel *)l->data)); return result; @@ -1352,17 +1234,17 @@ vector> Analog::channels() const Quantity *Analog::mq() { - return Quantity::get(_structure->mq); + return Quantity::get(_structure->meaning->mq); } const Unit *Analog::unit() { - return Unit::get(_structure->unit); + return Unit::get(_structure->meaning->unit); } vector Analog::mq_flags() { - return QuantityFlag::flags_from_mask(_structure->mqflags); + return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); } InputFormat::InputFormat(const struct sr_input_module *structure) : @@ -1409,7 +1291,7 @@ map> InputFormat::options() } shared_ptr InputFormat::create_input( - map options) + const map &options) { auto input = sr_input_new(_structure, map_to_hash_variant(options)); if (!input) @@ -1420,7 +1302,7 @@ shared_ptr InputFormat::create_input( Input::Input(shared_ptr context, const struct sr_input *structure) : UserOwned(structure), - _context(context), + _context(move(context)), _device(nullptr) { } @@ -1438,9 +1320,9 @@ shared_ptr Input::device() return _device->get_shared_pointer(shared_from_this()); } -void Input::send(string data) +void Input::send(void *data, size_t length) { - auto gstr = g_string_new(data.c_str()); + auto gstr = g_string_new_len((gchar *)data, length); auto ret = sr_input_send(_structure, gstr); g_string_free(gstr, false); check(ret); @@ -1462,7 +1344,7 @@ InputDevice::InputDevice(shared_ptr input, struct sr_dev_inst *structure) : ParentOwned(structure), Device(structure), - _input(input) + _input(move(input)) { } @@ -1478,7 +1360,7 @@ shared_ptr InputDevice::get_shared_from_this() Option::Option(const struct sr_option *structure, shared_ptr structure_array) : UserOwned(structure), - _structure_array(structure_array) + _structure_array(move(structure_array)) { } @@ -1558,19 +1440,42 @@ map> OutputFormat::options() } shared_ptr OutputFormat::create_output( - shared_ptr device, map options) + shared_ptr device, const map &options) +{ + return shared_ptr( + new Output(shared_from_this(), move(device), options), + Output::Deleter()); +} + +shared_ptr OutputFormat::create_output(string filename, + shared_ptr device, const map &options) { return shared_ptr( - new Output(shared_from_this(), device, options), + new Output(move(filename), shared_from_this(), move(device), options), Output::Deleter()); } +bool OutputFormat::test_flag(const OutputFlag *flag) +{ + return sr_output_test_flag(_structure, flag->id()); +} + Output::Output(shared_ptr format, - shared_ptr device, map options) : + shared_ptr device, const map &options) : + UserOwned(sr_output_new(format->_structure, + map_to_hash_variant(options), device->_structure, NULL)), + _format(move(format)), + _device(move(device)), + _options(options) +{ +} + +Output::Output(string filename, shared_ptr format, + shared_ptr device, const map &options) : UserOwned(sr_output_new(format->_structure, - map_to_hash_variant(options), device->_structure)), - _format(format), - _device(device), + map_to_hash_variant(options), device->_structure, filename.c_str())), + _format(move(format)), + _device(move(device)), _options(options) { } @@ -1596,6 +1501,6 @@ string Output::receive(shared_ptr packet) } } -#include "enums.cpp" +#include }