Update bindings for new input API.
This commit is contained in:
parent
b84cba4dbf
commit
ca3291e3ee
|
@ -40,18 +40,6 @@ static const char *valid_string(const char *input)
|
|||
return "";
|
||||
}
|
||||
|
||||
/** Helper function to convert between map<string, string> and GHashTable */
|
||||
static GHashTable *map_to_hash_string(map<string, string> input)
|
||||
{
|
||||
auto output = g_hash_table_new_full(
|
||||
g_str_hash, g_str_equal, g_free, g_free);
|
||||
for (auto entry : input)
|
||||
g_hash_table_insert(output,
|
||||
g_strdup(entry.first.c_str()),
|
||||
g_strdup(entry.second.c_str()));
|
||||
return output;
|
||||
}
|
||||
|
||||
/** Helper function to convert between map<string, VariantBase> and GHashTable */
|
||||
static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input)
|
||||
{
|
||||
|
@ -92,10 +80,10 @@ 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]);
|
||||
const struct sr_output_module **output_list = sr_output_list();
|
||||
if (output_list)
|
||||
|
@ -239,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)
|
||||
|
@ -1100,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)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1111,52 +1119,68 @@ 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));
|
||||
}
|
||||
|
||||
bool InputFormat::format_match(string filename)
|
||||
shared_ptr<Input> InputFormat::create_input(
|
||||
map<string, Glib::VariantBase> options)
|
||||
{
|
||||
return structure->format_match(filename.c_str());
|
||||
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());
|
||||
}
|
||||
|
||||
shared_ptr<InputFileDevice> InputFormat::open_file(string filename,
|
||||
map<string, string> options)
|
||||
{
|
||||
auto input = g_new(struct sr_input, 1);
|
||||
input->param = map_to_hash_string(options);
|
||||
|
||||
/** Run initialisation. */
|
||||
check(structure->init(input, filename.c_str()));
|
||||
|
||||
/** Create virtual device. */
|
||||
return shared_ptr<InputFileDevice>(new InputFileDevice(
|
||||
static_pointer_cast<InputFormat>(shared_from_this()), input, filename),
|
||||
InputFileDevice::Deleter());
|
||||
}
|
||||
|
||||
InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format,
|
||||
struct sr_input *input, string filename) :
|
||||
Device(input->sdi),
|
||||
input(input),
|
||||
format(format),
|
||||
filename(filename)
|
||||
Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
|
||||
structure(structure),
|
||||
context(context),
|
||||
device(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
InputFileDevice::~InputFileDevice()
|
||||
shared_ptr<InputDevice> Input::get_device()
|
||||
{
|
||||
g_hash_table_unref(input->param);
|
||||
g_free(input);
|
||||
if (!device)
|
||||
{
|
||||
auto sdi = sr_input_dev_inst_get(structure);
|
||||
if (!sdi)
|
||||
throw Error(SR_ERR_NA);
|
||||
device = new InputDevice(shared_from_this(), sdi);
|
||||
}
|
||||
|
||||
return static_pointer_cast<InputDevice>(
|
||||
device->get_shared_pointer(context->shared_from_this()));
|
||||
}
|
||||
|
||||
void InputFileDevice::load()
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
InputDevice::~InputDevice()
|
||||
{
|
||||
check(format->structure->loadfile(input, filename.c_str()));
|
||||
}
|
||||
|
||||
Option::Option(const struct sr_option *structure,
|
||||
|
|
|
@ -108,7 +108,8 @@ class SR_API PacketType;
|
|||
class SR_API Quantity;
|
||||
class SR_API Unit;
|
||||
class SR_API QuantityFlag;
|
||||
class SR_API InputFileDevice;
|
||||
class SR_API Input;
|
||||
class SR_API InputDevice;
|
||||
class SR_API Output;
|
||||
class SR_API DataType;
|
||||
class SR_API Option;
|
||||
|
@ -214,6 +215,12 @@ public:
|
|||
/** Create a new trigger.
|
||||
* @param name Name string for new trigger. */
|
||||
shared_ptr<Trigger> create_trigger(string name);
|
||||
/** Open an input file.
|
||||
* @param filename File name string. */
|
||||
shared_ptr<Input> open_file(string filename);
|
||||
/** Open an input stream based on header data.
|
||||
* @param header Initial data from stream. */
|
||||
shared_ptr<Input> open_stream(string header);
|
||||
protected:
|
||||
struct sr_context *structure;
|
||||
map<string, Driver *> drivers;
|
||||
|
@ -724,51 +731,68 @@ protected:
|
|||
|
||||
/** An input format supported by the library */
|
||||
class SR_API InputFormat :
|
||||
public StructureWrapper<Context, struct sr_input_format>
|
||||
public StructureWrapper<Context, const struct sr_input_module>
|
||||
{
|
||||
public:
|
||||
/** Name of this input format. */
|
||||
string get_name();
|
||||
/** Description of this input format. */
|
||||
string get_description();
|
||||
/** Check whether a given file matches this input format.
|
||||
* @param filename File name string. */
|
||||
bool format_match(string filename);
|
||||
/** Open a file using this input format.
|
||||
* @param filename File name string.
|
||||
* @param options Mapping of (option name, value) strings. */
|
||||
shared_ptr<InputFileDevice> open_file(string filename,
|
||||
map<string, string> options = {});
|
||||
/** Options supported by this input format. */
|
||||
map<string, shared_ptr<Option> > get_options();
|
||||
/** Create an input using this input format.
|
||||
* @param options Mapping of (option name, value) pairs. */
|
||||
shared_ptr<Input> create_input(map<string, Glib::VariantBase> options = {});
|
||||
protected:
|
||||
InputFormat(struct sr_input_format *structure);
|
||||
InputFormat(const struct sr_input_module *structure);
|
||||
~InputFormat();
|
||||
friend class Context;
|
||||
friend class InputFileDevice;
|
||||
friend class InputDevice;
|
||||
};
|
||||
|
||||
/** A virtual device associated with an input file */
|
||||
class SR_API InputFileDevice : public Device
|
||||
/** An input instance (an input format applied to a file or stream) */
|
||||
class SR_API Input : public enable_shared_from_this<Input>
|
||||
{
|
||||
public:
|
||||
/** Load data from file. */
|
||||
void load();
|
||||
/** Virtual device associated with this input. */
|
||||
shared_ptr<InputDevice> get_device();
|
||||
/** Send next stream data.
|
||||
* @param data Next stream data. */
|
||||
void send(string data);
|
||||
protected:
|
||||
InputFileDevice(shared_ptr<InputFormat> format,
|
||||
struct sr_input *input, string filename);
|
||||
~InputFileDevice();
|
||||
struct sr_input *input;
|
||||
shared_ptr<InputFormat> format;
|
||||
string filename;
|
||||
Input(shared_ptr<Context> context, const struct sr_input *structure);
|
||||
~Input();
|
||||
const struct sr_input *structure;
|
||||
shared_ptr<Context> context;
|
||||
InputDevice *device;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
class Deleter
|
||||
{
|
||||
public:
|
||||
void operator()(InputFileDevice *device) { delete device; }
|
||||
void operator()(Input *input) { delete input; }
|
||||
};
|
||||
friend class Deleter;
|
||||
friend class Context;
|
||||
friend class InputFormat;
|
||||
};
|
||||
|
||||
/** A virtual device associated with an input */
|
||||
class SR_API InputDevice : public Device
|
||||
{
|
||||
protected:
|
||||
InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
|
||||
~InputDevice();
|
||||
shared_ptr<Input> input;
|
||||
/** Deleter needed to allow shared_ptr use with protected destructor. */
|
||||
class Deleter
|
||||
{
|
||||
public:
|
||||
void operator()(InputDevice *device) { delete device; }
|
||||
};
|
||||
friend class Deleter;
|
||||
friend class Input;
|
||||
};
|
||||
|
||||
/** An option used by an output format */
|
||||
class SR_API Option
|
||||
{
|
||||
|
|
|
@ -224,6 +224,15 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
|
|||
}
|
||||
}
|
||||
|
||||
/* Support InputFormat.create_input() with no options. */
|
||||
%extend sigrok::InputFormat {
|
||||
std::shared_ptr<sigrok::Input> create_input()
|
||||
{
|
||||
std::map<std::string, Glib::VariantBase> options;
|
||||
return $self->create_input(options);
|
||||
}
|
||||
}
|
||||
|
||||
/* Support OutputFormat.create_output(device) with no options. */
|
||||
%extend sigrok::OutputFormat {
|
||||
std::shared_ptr<sigrok::Output> create_output(
|
||||
|
|
|
@ -354,7 +354,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
|
|||
|
||||
/* Ignore these methods, we will override them below. */
|
||||
%ignore sigrok::Driver::scan;
|
||||
%ignore sigrok::InputFormat::open_file;
|
||||
%ignore sigrok::InputFormat::create_input;
|
||||
%ignore sigrok::OutputFormat::create_output;
|
||||
|
||||
%include "doc.i"
|
||||
|
@ -394,21 +394,22 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
|
|||
Driver.scan = _Driver_scan
|
||||
}
|
||||
|
||||
/* Support InputFormat.open_file() with keyword arguments. */
|
||||
/* Support InputFormat.create_input() with keyword arguments. */
|
||||
%extend sigrok::InputFormat
|
||||
{
|
||||
std::shared_ptr<sigrok::InputFileDevice> _open_file_kwargs(std::string filename, PyObject *dict)
|
||||
std::shared_ptr<sigrok::Input> _create_input_kwargs(PyObject *dict)
|
||||
{
|
||||
return $self->open_file(filename, dict_to_map_string(dict));
|
||||
return $self->create_input(
|
||||
dict_to_map_options(dict, $self->get_options()));
|
||||
}
|
||||
}
|
||||
|
||||
%pythoncode
|
||||
{
|
||||
def _InputFormat_open_file(self, filename, **kwargs):
|
||||
return self._open_file_kwargs(filename, kwargs)
|
||||
def _InputFormat_create_input(self, **kwargs):
|
||||
return self._create_input(kwargs)
|
||||
|
||||
InputFormat.open_file = _InputFormat_open_file
|
||||
InputFormat.create_input = _InputFormat_create_input
|
||||
}
|
||||
|
||||
/* Support OutputFormat.create_output() with keyword arguments. */
|
||||
|
|
|
@ -75,7 +75,8 @@ template< class T > class enable_shared_from_this;
|
|||
%shared_ptr(sigrok::Analog);
|
||||
%shared_ptr(sigrok::Logic);
|
||||
%shared_ptr(sigrok::InputFormat);
|
||||
%shared_ptr(sigrok::InputFileDevice);
|
||||
%shared_ptr(sigrok::Input);
|
||||
%shared_ptr(sigrok::InputDevice);
|
||||
%shared_ptr(sigrok::Option);
|
||||
%shared_ptr(sigrok::OutputFormat);
|
||||
%shared_ptr(sigrok::Output);
|
||||
|
@ -190,6 +191,9 @@ typedef std::map<const sigrok::ConfigKey *, Glib::VariantBase>
|
|||
%attributestring(sigrok::InputFormat,
|
||||
std::string, description, get_description);
|
||||
|
||||
%attributestring(sigrok::Input,
|
||||
std::shared_ptr<sigrok::InputDevice>, device, get_device);
|
||||
|
||||
%attributestring(sigrok::Option,
|
||||
std::string, id, get_id);
|
||||
%attributestring(sigrok::Option,
|
||||
|
|
Loading…
Reference in New Issue