From: Martin Ling Date: Sun, 20 Jul 2014 01:20:13 +0000 (+0100) Subject: bindings: Overhaul packet & payload classes. X-Git-Tag: libsigrok-0.4.0~1202 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2928f47d6404e51c2dda1842c43db578cb1d6cdd;p=libsigrok.git bindings: Overhaul packet & payload classes. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 285025be..2ae0f298 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -600,7 +600,7 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *pkt) { auto device = session->devices[sdi]; - auto packet = shared_ptr(new Packet(pkt), Packet::Deleter()); + auto packet = shared_ptr(new Packet(device, pkt), Packet::Deleter()); callback(device, packet); } @@ -893,11 +893,23 @@ void Session::set_trigger(shared_ptr trigger) this->trigger = trigger; } -Packet::Packet(const struct sr_datafeed_packet *structure) : - structure(structure) +Packet::Packet(shared_ptr device, + const struct sr_datafeed_packet *structure) : + structure(structure), + device(device) { switch (structure->type) { + case SR_DF_HEADER: + payload = new Header( + static_cast( + structure->payload)); + break; + case SR_DF_META: + payload = new Meta( + static_cast( + structure->payload)); + break; case SR_DF_LOGIC: payload = new Logic( static_cast( @@ -908,9 +920,6 @@ Packet::Packet(const struct sr_datafeed_packet *structure) : static_cast( structure->payload)); break; - default: - payload = NULL; - break; } } @@ -925,9 +934,9 @@ const PacketType *Packet::get_type() return PacketType::get(structure->type); } -PacketPayload *Packet::get_payload() +shared_ptr Packet::get_payload() { - return payload; + return payload->get_shared_pointer(this); } PacketPayload::PacketPayload() @@ -938,28 +947,77 @@ PacketPayload::~PacketPayload() { } -Logic::Logic(const struct sr_datafeed_logic *structure) : PacketPayload(), - structure(structure), - data(static_cast(structure->data), - static_cast(structure->data) + structure->length) {} +Header::Header(const struct sr_datafeed_header *structure) : + PacketPayload(), + StructureWrapper(structure) +{ +} + +Header::~Header() +{ +} + +int Header::get_feed_version() +{ + return structure->feed_version; +} + +Glib::TimeVal Header::get_start_time() +{ + return Glib::TimeVal( + structure->starttime.tv_sec, + structure->starttime.tv_usec); +} + +Meta::Meta(const struct sr_datafeed_meta *structure) : + PacketPayload(), + StructureWrapper(structure) +{ +} + +Meta::~Meta() +{ +} + +map Meta::get_config() +{ + map result; + for (auto l = structure->config; l; l = l->next) + { + auto config = (struct sr_config *) l->data; + result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); + } + return result; +} + +Logic::Logic(const struct sr_datafeed_logic *structure) : + PacketPayload(), + StructureWrapper(structure) +{ +} Logic::~Logic() { } -void *Logic::get_data() +void *Logic::get_data_pointer() { return structure->data; } -size_t Logic::get_data_size() +size_t Logic::get_data_length() { return structure->length; } +unsigned int Logic::get_unit_size() +{ + return structure->unitsize; +} + Analog::Analog(const struct sr_datafeed_analog *structure) : PacketPayload(), - structure(structure) + StructureWrapper(structure) { } @@ -967,19 +1025,23 @@ Analog::~Analog() { } -void *Analog::get_data() +float *Analog::get_data_pointer() { return structure->data; } -size_t Analog::get_data_size() +unsigned int Analog::get_num_samples() { - return structure->num_samples * sizeof(float); + return structure->num_samples; } -unsigned int Analog::get_num_samples() +vector> Analog::get_channels() { - return structure->num_samples; + vector> result; + for (auto l = structure->channels; l; l = l->next) + result.push_back(parent->device->get_channel( + (struct sr_channel *)l->data)); + return result; } const Quantity *Analog::get_mq() diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 3deb29e6..32045597 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -304,6 +304,7 @@ protected: friend class Channel; friend class ChannelGroup; friend class Output; + friend class Analog; }; /** A real hardware device, connected via a driver */ @@ -546,17 +547,19 @@ protected: }; /** A packet on the session datafeed */ -class SR_API Packet +class SR_API Packet : public enable_shared_from_this { public: /** Type of this packet. */ const PacketType *get_type(); /** Payload of this packet. */ - PacketPayload *get_payload(); + shared_ptr get_payload(); protected: - Packet(const struct sr_datafeed_packet *structure); + Packet(shared_ptr device, + const struct sr_datafeed_packet *structure); ~Packet(); const struct sr_datafeed_packet *structure; + shared_ptr device; PacketPayload *payload; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter @@ -568,6 +571,10 @@ protected: friend class Session; friend class Output; friend class DatafeedCallbackData; + friend class Header; + friend class Meta; + friend class Logic; + friend class Analog; }; /** Abstract base class for datafeed packet payloads */ @@ -576,31 +583,77 @@ class SR_API PacketPayload protected: PacketPayload(); virtual ~PacketPayload() = 0; - virtual void *get_data() = 0; - virtual size_t get_data_size() = 0; + shared_ptr get_shared_pointer(Packet *parent) { + return static_pointer_cast(get_shared_pointer(parent)); + } + /** Deleter needed to allow shared_ptr use with protected destructor. */ + class Deleter + { + public: + void operator()(PacketPayload *payload) { delete payload; } + }; + friend class Deleter; friend class Packet; friend class Output; }; +/** Payload of a datafeed header packet */ +class SR_API Header : public PacketPayload, + public StructureWrapper +{ +public: + int get_feed_version(); + Glib::TimeVal get_start_time(); +protected: + Header(const struct sr_datafeed_header *structure); + ~Header(); + const struct sr_datafeed_header *structure; + friend class Packet; +}; + +/** Payload of a datafeed metadata packet */ +class SR_API Meta : public PacketPayload, + public StructureWrapper +{ +public: + map get_config(); +protected: + Meta(const struct sr_datafeed_meta *structure); + ~Meta(); + const struct sr_datafeed_meta *structure; + map config; + friend class Packet; +}; + /** Payload of a datafeed packet with logic data */ -class SR_API Logic : public PacketPayload +class SR_API Logic : public PacketPayload, + public StructureWrapper { +public: + /* Pointer to data. */ + void *get_data_pointer(); + /* Data length in bytes. */ + size_t get_data_length(); + /* Size of each sample in bytes. */ + unsigned int get_unit_size(); protected: Logic(const struct sr_datafeed_logic *structure); ~Logic(); const struct sr_datafeed_logic *structure; - vector data; - void *get_data(); - size_t get_data_size(); friend class Packet; }; /** Payload of a datafeed packet with analog data */ -class SR_API Analog : public PacketPayload +class SR_API Analog : public PacketPayload, + public StructureWrapper { public: + /** Pointer to data. */ + float *get_data_pointer(); /** Number of samples in this packet. */ unsigned int get_num_samples(); + /** Channels for which this packet contains data. */ + vector > get_channels(); /** Measured quantity of the samples in this packet. */ const Quantity *get_mq(); /** Unit of the samples in this packet. */ @@ -611,8 +664,6 @@ protected: Analog(const struct sr_datafeed_analog *structure); ~Analog(); const struct sr_datafeed_analog *structure; - void *get_data(); - size_t get_data_size(); friend class Packet; }; diff --git a/bindings/java/org/sigrok/core/classes/classes.i b/bindings/java/org/sigrok/core/classes/classes.i index 20e55d57..5fdbc83e 100644 --- a/bindings/java/org/sigrok/core/classes/classes.i +++ b/bindings/java/org/sigrok/core/classes/classes.i @@ -157,6 +157,10 @@ STRING_TO_SHARED_PTR_MAP(OutputFormat) MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) +%typemap(jni) std::map "jobject" +%typemap(jtype) std::map + "java.util.Map" + %typemap(out) std::map { jclass HashMap = jenv->FindClass("java/util/HashMap"); jmethodID HashMap_init = jenv->GetMethodID(HashMap, "", "()V"); diff --git a/bindings/swig/classes.i b/bindings/swig/classes.i index 5efa0ba8..445fdfbb 100644 --- a/bindings/swig/classes.i +++ b/bindings/swig/classes.i @@ -70,6 +70,8 @@ template< class T > class enable_shared_from_this; %shared_ptr(sigrok::Session); %shared_ptr(sigrok::Packet); %shared_ptr(sigrok::PacketPayload); +%shared_ptr(sigrok::Header); +%shared_ptr(sigrok::Meta); %shared_ptr(sigrok::Analog); %shared_ptr(sigrok::Logic); %shared_ptr(sigrok::InputFormat); @@ -143,6 +145,8 @@ typedef std::map > map_string_OutputFormat; typedef std::map > map_string_ChannelGroup; +typedef std::map + map_ConfigKey_Variant; } %attributeval(sigrok::Context, @@ -221,9 +225,12 @@ typedef std::map > devices, get_devices); %attributestring(sigrok::Session, - std::shared_ptr, trigger, get_trigger, set_trigger) + std::shared_ptr, trigger, get_trigger, set_trigger); -%attribute(sigrok::Packet, sigrok::PacketPayload *, payload, get_payload); +%attributestring(sigrok::Packet, + std::shared_ptr, payload, get_payload); + +%attributeval(sigrok::Meta, map_ConfigKey_Variant, config, get_config); %attribute(sigrok::Analog, int, num_samples, get_num_samples); %attribute(sigrok::Analog, const sigrok::Quantity *, mq, get_mq);