]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
Bindings: Flesh out the analog payload bindings
[libsigrok.git] / bindings / cxx / classes.cpp
index eabe36a38654f65e7710f4cfeb10cfb6482ff165..5c0ae3d0a0fc7ccbdc3756f49aaa511cdfa6d45c 100644 (file)
@@ -101,7 +101,7 @@ SR_PRIV int ResourceReader::close_callback(struct sr_resource *res,
        return SR_OK;
 }
 
-SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res,
+SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res,
                void *buf, size_t count, void *cb_data) noexcept
 {
        try {
@@ -128,19 +128,19 @@ Context::Context() :
        if (struct sr_dev_driver **driver_list = sr_driver_list(_structure))
                for (int i = 0; driver_list[i]; i++) {
                        unique_ptr<Driver> driver {new Driver{driver_list[i]}};
-                       _drivers.emplace(driver->name(), move(driver));
+                       _drivers.insert(make_pair(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<InputFormat> input {new InputFormat{input_list[i]}};
-                       _input_formats.emplace(input->name(), move(input));
+                       _input_formats.insert(make_pair(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<OutputFormat> output {new OutputFormat{output_list[i]}};
-                       _output_formats.emplace(output->name(), move(output));
+                       _output_formats.insert(make_pair(output->name(), move(output)));
                }
 }
 
@@ -161,7 +161,7 @@ map<string, shared_ptr<Driver>> Context::drivers()
        {
                const auto &name = entry.first;
                const auto &driver = entry.second;
-               result.emplace(name, driver->share_owned_by(shared_from_this()));
+               result.insert({name, driver->share_owned_by(shared_from_this())});
        }
        return result;
 }
@@ -173,7 +173,7 @@ map<string, shared_ptr<InputFormat>> Context::input_formats()
        {
                const auto &name = entry.first;
                const auto &input_format = entry.second;
-               result.emplace(name, input_format->share_owned_by(shared_from_this()));
+               result.insert({name, input_format->share_owned_by(shared_from_this())});
        }
        return result;
 }
@@ -185,7 +185,7 @@ map<string, shared_ptr<OutputFormat>> Context::output_formats()
        {
                const auto &name = entry.first;
                const auto &output_format = entry.second;
-               result.emplace(name, output_format->share_owned_by(shared_from_this()));
+               result.insert({name, output_format->share_owned_by(shared_from_this())});
        }
        return result;
 }
@@ -317,15 +317,39 @@ shared_ptr<Packet> Context::create_analog_packet(
 {
        auto analog = g_new0(struct sr_datafeed_analog, 1);
        auto meaning = g_new0(struct sr_analog_meaning, 1);
+       auto encoding = g_new0(struct sr_analog_encoding, 1);
+       auto spec = g_new0(struct sr_analog_spec, 1);
 
        analog->meaning = meaning;
 
        for (const auto &channel : channels)
                meaning->channels = g_slist_append(meaning->channels, channel->_structure);
-       analog->num_samples = num_samples;
        meaning->mq = static_cast<sr_mq>(mq->id());
        meaning->unit = static_cast<sr_unit>(unit->id());
        meaning->mqflags = static_cast<sr_mqflag>(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 = data_pointer;
        auto packet = g_new(struct sr_datafeed_packet, 1);
        packet->type = SR_DF_ANALOG;
@@ -408,11 +432,13 @@ string Driver::long_name() const
 
 set<const ConfigKey *> Driver::scan_options() const
 {
-       GArray *opts = sr_driver_scan_options(_structure);
+       GArray *opts = sr_driver_scan_options_list(_structure);
        set<const ConfigKey *> result;
-       for (guint i = 0; i < opts->len; i++)
-               result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
-       g_array_free(opts, TRUE);
+       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;
 }
 
@@ -483,10 +509,11 @@ set<const ConfigKey *> Configurable::config_keys() const
 
        opts = sr_dev_options(config_driver, config_sdi, config_channel_group);
 
-       for (guint i = 0; i < opts->len; i++)
-               result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
-
-       g_array_free(opts, TRUE);
+       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;
 }
@@ -547,14 +574,14 @@ Device::Device(struct sr_dev_inst *structure) :
        {
                auto *const ch = static_cast<struct sr_channel *>(entry->data);
                unique_ptr<Channel> channel {new Channel{ch}};
-               _channels.emplace(ch, move(channel));
+               _channels.insert(make_pair(ch, move(channel)));
        }
 
        for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next)
        {
                auto *const cg = static_cast<struct sr_channel_group *>(entry->data);
                unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}};
-               _channel_groups.emplace(group->name(), move(group));
+               _channel_groups.insert(make_pair(group->name(), move(group)));
        }
 }
 
@@ -609,7 +636,7 @@ Device::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()));
+               result.insert({name, channel_group->share_owned_by(get_shared_from_this())});
        }
        return result;
 }
@@ -668,7 +695,7 @@ shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
        GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure));
        auto *const ch = static_cast<struct sr_channel *>(last->data);
        unique_ptr<Channel> channel {new Channel{ch}};
-       _channels.emplace(ch, move(channel));
+       _channels.insert(make_pair(ch, move(channel)));
        return get_channel(ch);
 }
 
@@ -891,7 +918,7 @@ Session::Session(shared_ptr<Context> context, string filename) :
        for (GSList *dev = dev_list; dev; dev = dev->next) {
                auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
                unique_ptr<SessionDevice> device {new SessionDevice{sdi}};
-               _owned_devices.emplace(sdi, move(device));
+               _owned_devices.insert(make_pair(sdi, move(device)));
        }
        _context->_session = this;
 }
@@ -1200,6 +1227,58 @@ vector<shared_ptr<Channel>> Analog::channels()
        return result;
 }
 
+unsigned int Analog::unitsize() const
+{
+       return _structure->encoding->unitsize;
+}
+
+bool Analog::is_signed() const
+{
+       return _structure->encoding->is_signed;
+}
+
+bool Analog::is_float() const
+{
+       return _structure->encoding->is_float;
+}
+
+bool Analog::is_bigendian() const
+{
+       return _structure->encoding->is_bigendian;
+}
+
+int Analog::digits() const
+{
+       return _structure->encoding->digits;
+}
+
+bool Analog::is_digits_decimal() const
+{
+       return _structure->encoding->is_digits_decimal;
+}
+
+shared_ptr<Rational> Analog::scale()
+{
+       unique_ptr<Rational> 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<Rational> Analog::offset()
+{
+       unique_ptr<Rational> 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);
@@ -1215,6 +1294,36 @@ vector<const QuantityFlag *> Analog::mq_flags() const
        return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
 }
 
+Rational::Rational(const struct sr_rational *structure) :
+       _structure(structure)
+{
+}
+
+Rational::~Rational()
+{
+}
+
+shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent)
+{
+       return static_pointer_cast<Rational>(
+               ParentOwned::share_owned_by(_parent));
+}
+
+int64_t Rational::numerator() const
+{
+       return _structure->p;
+}
+
+uint64_t Rational::denominator() const
+{
+       return _structure->q;
+}
+
+float Rational::value() const
+{
+       return (float)(_structure->p) / (float)(_structure->q);
+}
+
 InputFormat::InputFormat(const struct sr_input_module *structure) :
        _structure(structure)
 {
@@ -1255,7 +1364,7 @@ map<string, shared_ptr<Option>> InputFormat::options()
                        shared_ptr<Option> opt {
                                new Option{options[i], option_array},
                                default_delete<Option>{}};
-                       result.emplace(opt->id(), move(opt));
+                       result.insert({opt->id(), move(opt)});
                }
        }
        return result;
@@ -1302,6 +1411,11 @@ void Input::end()
        check(sr_input_end(_structure));
 }
 
+void Input::reset()
+{
+       check(sr_input_reset(_structure));
+}
+
 Input::~Input()
 {
        sr_input_free(_structure);
@@ -1404,7 +1518,7 @@ map<string, shared_ptr<Option>> OutputFormat::options()
                        shared_ptr<Option> opt {
                                new Option{options[i], option_array},
                                default_delete<Option>{}};
-                       result.emplace(opt->id(), move(opt));
+                       result.insert({opt->id(), move(opt)});
                }
        }
        return result;