]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
Update bindings for new input API.
[libsigrok.git] / bindings / cxx / classes.cpp
index 2ae0f29839b9256c19572f8fbece25cdf23a8550..986b2c1e1da3fe60daa96ffbb0b15f9d71434f6d 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "libsigrok/libsigrok.hpp"
 
+#include <sstream>
+
 namespace sigrok
 {
 
@@ -38,16 +40,16 @@ static const char *valid_string(const char *input)
                return "";
 }
 
-/** Helper function to convert between map<string, string> and GHashTable */
-
-static GHashTable *map_to_hash(map<string, string> input)
+/** Helper function to convert between map<string, VariantBase> and GHashTable */
+static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input)
 {
        auto output = g_hash_table_new_full(
-               g_str_hash, g_str_equal, g_free, g_free);
+               g_variant_hash, g_variant_equal, g_free,
+               (void (*)(void *))g_variant_unref);
        for (auto entry : input)
                g_hash_table_insert(output,
                        g_strdup(entry.first.c_str()),
-                       g_strdup(entry.second.c_str()));
+                       entry.second.gobj_copy());
     return output;
 }
 
@@ -78,15 +80,15 @@ Context::Context() :
                for (int i = 0; driver_list[i]; i++)
                        drivers[driver_list[i]->name] =
                                new Driver(driver_list[i]);
-       struct sr_input_format **input_list = sr_input_list();
+       const struct sr_input_module **input_list = sr_input_list();
        if (input_list)
                for (int i = 0; input_list[i]; i++)
-                       input_formats[input_list[i]->id] =
+                       input_formats[sr_input_id_get(input_list[i])] =
                                new InputFormat(input_list[i]);
-       struct sr_output_format **output_list = sr_output_list();
+       const struct sr_output_module **output_list = sr_output_list();
        if (output_list)
                for (int i = 0; output_list[i]; i++)
-                       output_formats[output_list[i]->id] =
+                       output_formats[sr_output_id_get(output_list[i])] =
                                new OutputFormat(output_list[i]);
 }
 
@@ -225,6 +227,26 @@ shared_ptr<Trigger> Context::create_trigger(string name)
                new Trigger(shared_from_this(), name), Trigger::Deleter());
 }
 
+shared_ptr<Input> Context::open_file(string filename)
+{
+       auto input = sr_input_scan_file(filename.c_str());
+       if (!input)
+               throw Error(SR_ERR_NA);
+       return shared_ptr<Input>(
+               new Input(shared_from_this(), input), Input::Deleter());
+}
+
+shared_ptr<Input> Context::open_stream(string header)
+{
+       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<Input>(
+               new Input(shared_from_this(), input), Input::Deleter());
+}
+
 Driver::Driver(struct sr_dev_driver *structure) :
        StructureWrapper<Context, struct sr_dev_driver>(structure),
        initialized(false)
@@ -328,13 +350,13 @@ void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
                key->get_id(), value.gobj()));
 }
 
-Glib::VariantBase Configurable::config_list(const ConfigKey *key)
+Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
 {
        GVariant *data;
        check(sr_config_list(
                config_driver, config_sdi, config_channel_group,
                key->get_id(), &data));
-       return Glib::VariantBase(data);
+       return Glib::VariantContainerBase(data);
 }
 
 Device::Device(struct sr_dev_inst *structure) :
@@ -346,12 +368,34 @@ Device::Device(struct sr_dev_inst *structure) :
                auto channel = (struct sr_channel *) entry->data;
                channels[channel] = new Channel(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);
+       }
 }
 
 Device::~Device()
 {
        for (auto entry : channels)
                delete entry.second;
+       for (auto entry : channel_groups)
+               delete entry.second;
+}
+
+string Device::get_description()
+{
+       ostringstream s;
+
+       vector<string> parts =
+               {get_vendor(), get_model(), get_version()};
+
+       for (string part : parts)
+               if (part.length() > 0)
+                       s << part;
+
+       return s.str();
 }
 
 string Device::get_vendor()
@@ -384,6 +428,20 @@ shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
                channels[ptr]->get_shared_pointer(this));
 }
 
+map<string, shared_ptr<ChannelGroup>>
+Device::get_channel_groups()
+{
+       map<string, shared_ptr<ChannelGroup>> result;
+       for (auto entry: channel_groups)
+       {
+               auto name = entry.first;
+               auto channel_group = entry.second;
+               result[name] = static_pointer_cast<ChannelGroup>(
+                       channel_group->get_shared_pointer(this));
+       }
+       return result;
+}
+
 void Device::open()
 {
        check(sr_dev_open(structure));
@@ -398,17 +456,10 @@ HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
        Device(structure),
        driver(driver)
 {
-       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);
-       }
 }
 
 HardwareDevice::~HardwareDevice()
 {
-       for (auto entry : channel_groups)
-               delete entry.second;
 }
 
 shared_ptr<Driver> HardwareDevice::get_driver()
@@ -416,20 +467,6 @@ shared_ptr<Driver> HardwareDevice::get_driver()
        return static_pointer_cast<Driver>(driver->get_shared_pointer(parent));
 }
 
-map<string, shared_ptr<ChannelGroup>>
-HardwareDevice::get_channel_groups()
-{
-       map<string, shared_ptr<ChannelGroup>> result;
-       for (auto entry: channel_groups)
-       {
-               auto name = entry.first;
-               auto channel_group = entry.second;
-               result[name] = static_pointer_cast<ChannelGroup>(
-                       channel_group->get_shared_pointer(this));
-       }
-       return result;
-}
-
 Channel::Channel(struct sr_channel *structure) :
        StructureWrapper<Device, struct sr_channel>(structure),
        type(ChannelType::get(structure->type))
@@ -465,9 +502,14 @@ void Channel::set_enabled(bool value)
        check(sr_dev_channel_enable(parent->structure, structure->index, value));
 }
 
-ChannelGroup::ChannelGroup(HardwareDevice *device,
+unsigned int Channel::get_index()
+{
+       return structure->index;
+}
+
+ChannelGroup::ChannelGroup(Device *device,
                struct sr_channel_group *structure) :
-       StructureWrapper<HardwareDevice, struct sr_channel_group>(structure),
+       StructureWrapper<Device, struct sr_channel_group>(structure),
        Configurable(device->structure->driver, device->structure, structure)
 {
        for (GSList *entry = structure->channels; entry; entry = entry->next)
@@ -728,7 +770,7 @@ void Session::begin_save(string filename)
        save_samplerate = 0;
 }
 
-void Session::append(shared_ptr<Device> device, shared_ptr<Packet> packet)
+void Session::append(shared_ptr<Packet> packet)
 {
        if (!saving)
                throw Error(SR_ERR);
@@ -755,8 +797,9 @@ void Session::append(shared_ptr<Device> device, shared_ptr<Packet> packet)
                        {
                                GVariant *samplerate;
 
-                               check(sr_config_get(device->structure->driver,
-                                       device->structure, NULL, SR_CONF_SAMPLERATE, &samplerate));
+                               check(sr_config_get(packet->device->structure->driver,
+                                       packet->device->structure, NULL, SR_CONF_SAMPLERATE,
+                                       &samplerate));
 
                                save_samplerate = g_variant_get_uint64(samplerate);
 
@@ -767,7 +810,7 @@ void Session::append(shared_ptr<Device> device, shared_ptr<Packet> packet)
                        {
                                vector<shared_ptr<Channel>> save_channels;
 
-                               for (auto channel : device->get_channels())
+                               for (auto channel : packet->device->get_channels())
                                        if (channel->structure->enabled &&
                                                        channel->structure->type == SR_CHANNEL_LOGIC)
                                                save_channels.push_back(channel);
@@ -800,6 +843,12 @@ void Session::append(shared_ptr<Device> device, shared_ptr<Packet> packet)
        }
 }
 
+void Session::append(void *data, size_t length, unsigned int unit_size)
+{
+       check(sr_session_append(structure, save_filename.c_str(),
+               (uint8_t *) data, unit_size, length));
+}
+
 static void datafeed_callback(const struct sr_dev_inst *sdi,
        const struct sr_datafeed_packet *pkt, void *cb_data)
 {
@@ -1059,8 +1108,8 @@ vector<const QuantityFlag *> Analog::get_mq_flags()
        return QuantityFlag::flags_from_mask(structure->mqflags);
 }
 
-InputFormat::InputFormat(struct sr_input_format *structure) :
-       StructureWrapper<Context, struct sr_input_format>(structure)
+InputFormat::InputFormat(const struct sr_input_module *structure) :
+       StructureWrapper<Context, const struct sr_input_module>(structure)
 {
 }
 
@@ -1070,56 +1119,111 @@ InputFormat::~InputFormat()
 
 string InputFormat::get_name()
 {
-       return valid_string(structure->id);
+       return valid_string(sr_input_id_get(structure));
 }
 
 string InputFormat::get_description()
 {
-       return valid_string(structure->description);
+       return valid_string(sr_input_description_get(structure));
+}
+
+shared_ptr<Input> InputFormat::create_input(
+       map<string, Glib::VariantBase> options)
+{
+       auto input = sr_input_new(structure, map_to_hash_variant(options));
+       if (!input)
+               throw Error(SR_ERR_ARG);
+       return shared_ptr<Input>(
+               new Input(parent->shared_from_this(), input), Input::Deleter());
 }
 
-bool InputFormat::format_match(string filename)
+Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
+       structure(structure),
+       context(context),
+       device(nullptr)
 {
-       return structure->format_match(filename.c_str());
 }
 
-shared_ptr<InputFileDevice> InputFormat::open_file(string filename,
-               map<string, string> options)
+shared_ptr<InputDevice> Input::get_device()
 {
-       auto input = g_new(struct sr_input, 1);
-       input->param = map_to_hash(options);
+       if (!device)
+       {
+               auto sdi = sr_input_dev_inst_get(structure);
+               if (!sdi)
+                       throw Error(SR_ERR_NA);
+               device = new InputDevice(shared_from_this(), sdi);
+       }
 
-       /** Run initialisation. */
-       check(structure->init(input, filename.c_str()));
+       return static_pointer_cast<InputDevice>(
+               device->get_shared_pointer(context->shared_from_this()));
+}
 
-       /** Create virtual device. */
-       return shared_ptr<InputFileDevice>(new InputFileDevice(
-               static_pointer_cast<InputFormat>(shared_from_this()), input, filename),
-               InputFileDevice::Deleter());
+void Input::send(string data)
+{
+       auto gstr = g_string_new(data.c_str());
+       auto ret = sr_input_send(structure, gstr);
+       g_string_free(gstr, false);
+       check(ret);
 }
 
-InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format,
-               struct sr_input *input, string filename) :
-       Device(input->sdi),
-       input(input),
-       format(format),
-       filename(filename)
+Input::~Input()
+{
+       if (device)
+               delete device;
+       check(sr_input_free(structure));
+}
+
+InputDevice::InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi) :
+       Device(sdi),
+       input(input)
 {
 }
 
-InputFileDevice::~InputFileDevice()
+InputDevice::~InputDevice()
 {
-       g_hash_table_unref(input->param);
-       g_free(input);
 }
 
-void InputFileDevice::load()
+Option::Option(const struct sr_option *structure,
+               shared_ptr<const struct sr_option *> structure_array) :
+       structure(structure),
+       structure_array(structure_array)
 {
-       check(format->structure->loadfile(input, filename.c_str()));
 }
 
-OutputFormat::OutputFormat(struct sr_output_format *structure) :
-       StructureWrapper<Context, struct sr_output_format>(structure)
+Option::~Option()
+{
+}
+
+string Option::get_id()
+{
+       return valid_string(structure->id);
+}
+
+string Option::get_name()
+{
+       return valid_string(structure->name);
+}
+
+string Option::get_description()
+{
+       return valid_string(structure->desc);
+}
+
+Glib::VariantBase Option::get_default_value()
+{
+       return Glib::VariantBase(structure->def, true);
+}
+
+vector<Glib::VariantBase> Option::get_values()
+{
+       vector<Glib::VariantBase> result;
+       for (auto l = structure->values; l; l = l->next)
+               result.push_back(Glib::VariantBase((GVariant *) l->data, true));
+       return result;
+}
+
+OutputFormat::OutputFormat(const struct sr_output_module *structure) :
+       StructureWrapper<Context, const struct sr_output_module>(structure)
 {
 }
 
@@ -1129,16 +1233,28 @@ OutputFormat::~OutputFormat()
 
 string OutputFormat::get_name()
 {
-       return valid_string(structure->id);
+       return valid_string(sr_output_id_get(structure));
 }
 
 string OutputFormat::get_description()
 {
-       return valid_string(structure->description);
+       return valid_string(sr_output_description_get(structure));
+}
+
+map<string, shared_ptr<Option>> OutputFormat::get_options()
+{
+       const struct sr_option **options = sr_output_options_get(structure);
+       auto option_array = shared_ptr<const struct sr_option *>(
+               options, sr_output_options_free);
+       map<string, shared_ptr<Option>> result;
+       for (int i = 0; options[i]; i++)
+               result[options[i]->id] = shared_ptr<Option>(
+                       new Option(options[i], option_array), Option::Deleter());
+       return result;
 }
 
 shared_ptr<Output> OutputFormat::create_output(
-       shared_ptr<Device> device, map<string, string> options)
+       shared_ptr<Device> device, map<string, Glib::VariantBase> options)
 {
        return shared_ptr<Output>(
                new Output(
@@ -1148,16 +1264,15 @@ shared_ptr<Output> OutputFormat::create_output(
 }
 
 Output::Output(shared_ptr<OutputFormat> format,
-               shared_ptr<Device> device, map<string, string> options) :
+               shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
        structure(sr_output_new(format->structure,
-               map_to_hash(options), device->structure)),
+               map_to_hash_variant(options), device->structure)),
        format(format), device(device), options(options)
 {
 }
 
 Output::~Output()
 {
-       g_hash_table_unref(structure->params);
        check(sr_output_free(structure));
 }