#include "libsigrok/libsigrok.hpp"
+#include <sstream>
+
namespace sigrok
{
}
/** Helper function to convert between map<string, string> and GHashTable */
-
-static GHashTable *map_to_hash(map<string, string> input)
+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);
return output;
}
+/** 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_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()),
+ entry.second.gobj_copy());
+ return output;
+}
+
Error::Error(int result) : result(result)
{
}
for (int i = 0; input_list[i]; i++)
input_formats[input_list[i]->id] =
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]);
}
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) :
for (GSList *entry = structure->channels; entry; entry = entry->next)
{
auto channel = (struct sr_channel *) entry->data;
- channels.push_back(new Channel(channel));
+ 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 channel : channels)
- delete channel;
+ 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()
vector<shared_ptr<Channel>> Device::get_channels()
{
vector<shared_ptr<Channel>> result;
- for (auto channel : channels)
+ for (auto entry : channels)
result.push_back(static_pointer_cast<Channel>(
- channel->get_shared_pointer(this)));
+ entry.second->get_shared_pointer(this)));
+ return result;
+}
+
+shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
+{
+ return static_pointer_cast<Channel>(
+ 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;
}
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()
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))
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)
- {
- auto channel = (struct sr_channel *) entry->data;
- for (auto device_channel : device->channels)
- if (channel == device_channel->structure)
- channels.push_back(device_channel);
- }
+ channels.push_back(device->channels[(struct sr_channel *)entry->data]);
}
ChannelGroup::~ChannelGroup()
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);
}
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);
{
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);
{
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);
}
}
+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)
{
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 *>(
static_cast<const struct sr_datafeed_analog *>(
structure->payload));
break;
- default:
- payload = NULL;
- break;
}
}
Packet::~Packet()
{
- delete payload;
+ if (payload)
+ delete payload;
}
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()
{
}
-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)
{
}
{
}
-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<shared_ptr<Channel>> Analog::get_channels()
{
- return structure->num_samples;
+ 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()
map<string, string> options)
{
auto input = g_new(struct sr_input, 1);
- input->param = map_to_hash(options);
+ input->param = map_to_hash_string(options);
/** Run initialisation. */
check(structure->init(input, filename.c_str()));
check(format->structure->loadfile(input, filename.c_str()));
}
-OutputFormat::OutputFormat(struct sr_output_format *structure) :
- StructureWrapper<Context, struct sr_output_format>(structure)
+Option::Option(const struct sr_option *structure,
+ shared_ptr<const struct sr_option *> structure_array) :
+ structure(structure),
+ structure_array(structure_array)
+{
+}
+
+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)
{
}
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(
}
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));
}