bindings: Overhaul packet & payload classes.

This commit is contained in:
Martin Ling 2014-07-20 02:20:13 +01:00
parent 7009a3921a
commit 2928f47d64
4 changed files with 160 additions and 36 deletions

View File

@ -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<Packet>(new Packet(pkt), Packet::Deleter());
auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
callback(device, packet);
}
@ -893,11 +893,23 @@ void Session::set_trigger(shared_ptr<Trigger> trigger)
this->trigger = trigger;
}
Packet::Packet(const struct sr_datafeed_packet *structure) :
structure(structure)
Packet::Packet(shared_ptr<Device> device,
const struct sr_datafeed_packet *structure) :
structure(structure),
device(device)
{
switch (structure->type)
{
case SR_DF_HEADER:
payload = new Header(
static_cast<const struct sr_datafeed_header *>(
structure->payload));
break;
case SR_DF_META:
payload = new Meta(
static_cast<const struct sr_datafeed_meta *>(
structure->payload));
break;
case SR_DF_LOGIC:
payload = new Logic(
static_cast<const struct sr_datafeed_logic *>(
@ -908,9 +920,6 @@ Packet::Packet(const struct sr_datafeed_packet *structure) :
static_cast<const struct sr_datafeed_analog *>(
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<PacketPayload> 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<uint8_t *>(structure->data),
static_cast<uint8_t *>(structure->data) + structure->length) {}
Header::Header(const struct sr_datafeed_header *structure) :
PacketPayload(),
StructureWrapper<Packet, const struct sr_datafeed_header>(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<Packet, const struct sr_datafeed_meta>(structure)
{
}
Meta::~Meta()
{
}
map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
{
map<const ConfigKey *, Glib::VariantBase> 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<Packet, const struct sr_datafeed_logic>(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<Packet, const struct sr_datafeed_analog>(structure)
{
}
@ -967,21 +1025,25 @@ Analog::~Analog()
{
}
void *Analog::get_data()
float *Analog::get_data_pointer()
{
return structure->data;
}
size_t Analog::get_data_size()
{
return structure->num_samples * sizeof(float);
}
unsigned int Analog::get_num_samples()
{
return structure->num_samples;
}
vector<shared_ptr<Channel>> Analog::get_channels()
{
vector<shared_ptr<Channel>> 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()
{
return Quantity::get(structure->mq);

View File

@ -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<Packet>
{
public:
/** Type of this packet. */
const PacketType *get_type();
/** Payload of this packet. */
PacketPayload *get_payload();
shared_ptr<PacketPayload> get_payload();
protected:
Packet(const struct sr_datafeed_packet *structure);
Packet(shared_ptr<Device> device,
const struct sr_datafeed_packet *structure);
~Packet();
const struct sr_datafeed_packet *structure;
shared_ptr<Device> 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<PacketPayload> get_shared_pointer(Packet *parent) {
return static_pointer_cast<PacketPayload>(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 packet with logic data */
class SR_API Logic : public PacketPayload
/** Payload of a datafeed header packet */
class SR_API Header : public PacketPayload,
public StructureWrapper<Packet, const struct sr_datafeed_header>
{
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<Packet, const struct sr_datafeed_meta>
{
public:
map<const ConfigKey *, Glib::VariantBase> get_config();
protected:
Meta(const struct sr_datafeed_meta *structure);
~Meta();
const struct sr_datafeed_meta *structure;
map<const ConfigKey *, Glib::VariantBase> config;
friend class Packet;
};
/** Payload of a datafeed packet with logic data */
class SR_API Logic : public PacketPayload,
public StructureWrapper<Packet, const struct sr_datafeed_logic>
{
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<uint8_t> 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<Packet, const struct sr_datafeed_analog>
{
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<shared_ptr<Channel> > 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;
};

View File

@ -157,6 +157,10 @@ STRING_TO_SHARED_PTR_MAP(OutputFormat)
MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
%typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
%typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
"java.util.Map<ConfigKey,Variant>"
%typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
jclass HashMap = jenv->FindClass("java/util/HashMap");
jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");

View File

@ -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<std::string, std::shared_ptr<sigrok::OutputFormat> >
map_string_OutputFormat;
typedef std::map<std::string, std::shared_ptr<sigrok::ChannelGroup> >
map_string_ChannelGroup;
typedef std::map<const sigrok::ConfigKey *, Glib::VariantBase>
map_ConfigKey_Variant;
}
%attributeval(sigrok::Context,
@ -221,9 +225,12 @@ typedef std::map<std::string, std::shared_ptr<sigrok::ChannelGroup> >
devices, get_devices);
%attributestring(sigrok::Session,
std::shared_ptr<sigrok::Trigger>, trigger, get_trigger, set_trigger)
std::shared_ptr<sigrok::Trigger>, trigger, get_trigger, set_trigger);
%attribute(sigrok::Packet, sigrok::PacketPayload *, payload, get_payload);
%attributestring(sigrok::Packet,
std::shared_ptr<sigrok::PacketPayload>, 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);