python: Finish output format support.

This commit is contained in:
Martin Ling 2013-12-17 00:42:33 +00:00 committed by Bert Vermeulen
parent f0e764de7b
commit 409d85b3ac
2 changed files with 99 additions and 5 deletions

View File

@ -26,7 +26,7 @@ import itertools
__all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log', __all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log',
'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag', 'ConfigKey', 'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag', 'ConfigKey',
'ProbeType', 'Probe', 'ProbeGroup', 'InputFormat', 'OutputFormat', 'ProbeType', 'Probe', 'ProbeGroup', 'InputFormat', 'OutputFormat',
'InputFile'] 'InputFile', 'Output']
class Error(Exception): class Error(Exception):
@ -446,7 +446,7 @@ class InputFile(object):
self.struct = sr_input() self.struct = sr_input()
self.struct.format = self.format.struct self.struct.format = self.format.struct
self.struct.param = g_hash_table_new_full( self.struct.param = g_hash_table_new_full(
g_str_hash, g_str_equal, g_free, g_free) g_str_hash_ptr, g_str_equal_ptr, g_free_ptr, g_free_ptr)
for key, value in kwargs.items(): for key, value in kwargs.items():
g_hash_table_insert(self.struct.param, g_strdup(key), g_strdup(str(value))) g_hash_table_insert(self.struct.param, g_strdup(key), g_strdup(str(value)))
check(self.format.struct.call_init(self.struct, self.filename)) check(self.format.struct.call_init(self.struct, self.filename))
@ -471,6 +471,58 @@ class OutputFormat(object):
def description(self): def description(self):
return self.struct.description return self.struct.description
class Output(object):
def __init__(self, format, device, param=None):
self.format = format
self.device = device
self.param = param
self.struct = sr_output()
self.struct.format = self.format.struct
self.struct.sdi = self.device.struct
self.struct.param = param
check(self.format.struct.call_init(self.struct))
def receive(self, packet):
output_buf_ptr = new_uint8_ptr_ptr()
output_len_ptr = new_uint64_ptr()
using_obsolete_api = False
if self.format.struct.event and packet.type in (
PacketType.TRIGGER, PacketType.FRAME_BEGIN,
PacketType.FRAME_END, PacketType.END):
check(self.format.struct.call_event(self.struct, packet.type.id,
output_buf_ptr, output_len_ptr))
using_obsolete_api = True
elif self.format.struct.data and packet.type.id == self.format.struct.df_type:
check(self.format.struct.call_data(self.struct,
packet.payload.struct.data, packet.payload.struct.length,
output_buf_ptr, output_len_ptr))
using_obsolete_api = True
if using_obsolete_api:
output_buf = uint8_ptr_ptr_value(output_buf_ptr)
output_len = uint64_ptr_value(output_len_ptr)
result = cdata(output_buf, output_len)
g_free(output_buf)
return result
if self.format.struct.receive:
out_ptr = new_gstring_ptr_ptr()
check(self.format.struct.call_receive(self.struct, self.device.struct,
packet.struct, out_ptr))
out = gstring_ptr_ptr_value(out_ptr)
if out:
result = out.str
g_string_free(out, True)
return result
return None
def __del__(self):
check(self.format.struct.call_cleanup(self.struct))
class EnumValue(object): class EnumValue(object):
_enum_values = {} _enum_values = {}

View File

@ -26,6 +26,7 @@
%} %}
typedef void *gpointer; typedef void *gpointer;
typedef int gboolean;
typedef struct _GSList GSList; typedef struct _GSList GSList;
@ -58,12 +59,25 @@ GHashTable *g_hash_table_new_full(GHashFunc hash_func, GEqualFunc key_equal_func
void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value); void g_hash_table_insert(GHashTable *hash_table, gpointer key, gpointer value);
void g_hash_table_destroy(GHashTable *hash_table); void g_hash_table_destroy(GHashTable *hash_table);
%constant guint g_str_hash(gconstpointer v); %callback("%s_ptr");
%constant gboolean g_str_equal(gconstpointer v1, gconstpointer v2);; guint g_str_hash(gconstpointer v);
%constant void g_free(gpointer mem); gboolean g_str_equal(gconstpointer v1, gconstpointer v2);;
void g_free(gpointer mem);
%nocallback;
gchar *g_strdup(const char *str); gchar *g_strdup(const char *str);
typedef struct _GString GString;
struct _GString
{
char *str;
gsize len;
gsize allocated_len;
};
gchar *g_string_free(GString *string, gboolean free_segment);
%include "libsigrok/libsigrok.h" %include "libsigrok/libsigrok.h"
#undef SR_API #undef SR_API
#define SR_API #define SR_API
@ -72,6 +86,9 @@ gchar *g_strdup(const char *str);
%include "libsigrok/version.h" %include "libsigrok/version.h"
%array_class(float, float_array); %array_class(float, float_array);
%pointer_functions(uint8_t *, uint8_ptr_ptr);
%pointer_functions(uint64_t, uint64_ptr);
%pointer_functions(GString *, gstring_ptr_ptr);
%pointer_functions(GVariant *, gvariant_ptr_ptr); %pointer_functions(GVariant *, gvariant_ptr_ptr);
%array_functions(GVariant *, gvariant_ptr_array); %array_functions(GVariant *, gvariant_ptr_array);
%pointer_functions(struct sr_context *, sr_context_ptr_ptr); %pointer_functions(struct sr_context *, sr_context_ptr_ptr);
@ -97,3 +114,28 @@ gchar *g_strdup(const char *str);
return $self->loadfile(in, filename); return $self->loadfile(in, filename);
} }
} }
%extend sr_output_format {
int call_init(struct sr_output *o) {
return $self->init(o);
}
int call_event(struct sr_output *o, int event_type, uint8_t **data_out,
uint64_t *length_out) {
return $self->event(o, event_type, data_out, length_out);
}
int call_data(struct sr_output *o, const void *data_in,
uint64_t length_in, uint8_t **data_out, uint64_t *length_out) {
return $self->data(o, data_in, length_in, data_out, length_out);
}
int call_receive(struct sr_output *o, const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *packet, GString **out) {
return $self->receive(o, sdi, packet, out);
}
int call_cleanup(struct sr_output *o) {
return $self->cleanup(o);
}
}