libsigrok/bindings/cxx/classes.cpp

1457 lines
31 KiB
C++
Raw Normal View History

2014-07-17 17:39:49 +00:00
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libsigrok/libsigrok.hpp"
#include <sstream>
2014-09-13 19:23:23 +00:00
#include <cmath>
2014-07-17 17:39:49 +00:00
namespace sigrok
{
/** Helper function to translate C errors to C++ exceptions. */
static void check(int result)
{
if (result != SR_OK)
throw Error(result);
}
/** Helper function to obtain valid strings from possibly null input. */
static const char *valid_string(const char *input)
{
if (input != NULL)
return input;
else
return "";
}
2014-07-26 15:03:25 +00:00
/** 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_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
2014-07-26 15:03:25 +00:00
for (auto entry : input)
g_hash_table_insert(output,
g_strdup(entry.first.c_str()),
entry.second.gobj_copy());
return output;
}
2014-07-17 17:39:49 +00:00
Error::Error(int result) : result(result)
{
}
const char *Error::what() const throw()
{
return sr_strerror(result);
}
Error::~Error() throw()
{
}
shared_ptr<Context> Context::create()
{
return shared_ptr<Context>(new Context(), Context::Deleter());
}
Context::Context() :
UserOwned(_structure),
_session(NULL)
2014-07-17 17:39:49 +00:00
{
check(sr_init(&_structure));
2014-07-17 17:39:49 +00:00
struct sr_dev_driver **driver_list = sr_driver_list();
if (driver_list)
for (int i = 0; driver_list[i]; i++)
_drivers[driver_list[i]->name] =
2014-07-17 17:39:49 +00:00
new Driver(driver_list[i]);
2014-08-24 00:40:19 +00:00
const struct sr_input_module **input_list = sr_input_list();
2014-07-17 17:39:49 +00:00
if (input_list)
for (int i = 0; input_list[i]; i++)
_input_formats[sr_input_id_get(input_list[i])] =
2014-07-17 17:39:49 +00:00
new InputFormat(input_list[i]);
2014-07-26 15:03:25 +00:00
const struct sr_output_module **output_list = sr_output_list();
2014-07-17 17:39:49 +00:00
if (output_list)
for (int i = 0; output_list[i]; i++)
_output_formats[sr_output_id_get(output_list[i])] =
2014-07-17 17:39:49 +00:00
new OutputFormat(output_list[i]);
}
string Context::package_version()
2014-07-17 17:39:49 +00:00
{
return sr_package_version_string_get();
}
string Context::lib_version()
2014-07-17 17:39:49 +00:00
{
return sr_lib_version_string_get();
}
map<string, shared_ptr<Driver>> Context::drivers()
2014-07-17 17:39:49 +00:00
{
map<string, shared_ptr<Driver>> result;
for (auto entry: _drivers)
2014-07-17 17:39:49 +00:00
{
auto name = entry.first;
auto driver = entry.second;
result[name] = driver->get_shared_pointer(this);
2014-07-17 17:39:49 +00:00
}
return result;
}
map<string, shared_ptr<InputFormat>> Context::input_formats()
2014-07-17 17:39:49 +00:00
{
map<string, shared_ptr<InputFormat>> result;
for (auto entry: _input_formats)
2014-07-17 17:39:49 +00:00
{
auto name = entry.first;
auto input_format = entry.second;
result[name] = input_format->get_shared_pointer(this);
2014-07-17 17:39:49 +00:00
}
return result;
}
map<string, shared_ptr<OutputFormat>> Context::output_formats()
2014-07-17 17:39:49 +00:00
{
map<string, shared_ptr<OutputFormat>> result;
for (auto entry: _output_formats)
2014-07-17 17:39:49 +00:00
{
auto name = entry.first;
auto output_format = entry.second;
result[name] = output_format->get_shared_pointer(this);
2014-07-17 17:39:49 +00:00
}
return result;
}
Context::~Context()
{
for (auto entry : _drivers)
2014-07-17 17:39:49 +00:00
delete entry.second;
for (auto entry : _input_formats)
2014-07-17 17:39:49 +00:00
delete entry.second;
for (auto entry : _output_formats)
2014-07-17 17:39:49 +00:00
delete entry.second;
check(sr_exit(_structure));
2014-07-17 17:39:49 +00:00
}
const LogLevel *Context::log_level()
2014-07-17 17:39:49 +00:00
{
return LogLevel::get(sr_log_loglevel_get());
}
void Context::set_log_level(const LogLevel *level)
{
check(sr_log_loglevel_set(level->id()));
2014-07-17 17:39:49 +00:00
}
string Context::log_domain()
2014-07-17 17:39:49 +00:00
{
return valid_string(sr_log_logdomain_get());
}
void Context::set_log_domain(string value)
{
check(sr_log_logdomain_set(value.c_str()));
}
static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
int length = vsnprintf(NULL, 0, format, args_copy);
va_end(args_copy);
char *buf = (char *) g_malloc(length + 1);
vsprintf(buf, format, args);
string message(buf, length);
g_free(buf);
LogCallbackFunction callback = *((LogCallbackFunction *) cb_data);
try
{
callback(LogLevel::get(loglevel), message);
}
catch (Error e)
{
return e.result;
}
return SR_OK;
}
void Context::set_log_callback(LogCallbackFunction callback)
{
_log_callback = callback;
check(sr_log_callback_set(call_log_callback, &_log_callback));
2014-07-17 17:39:49 +00:00
}
void Context::set_log_callback_default()
{
check(sr_log_callback_set_default());
_log_callback = nullptr;
2014-07-17 17:39:49 +00:00
}
shared_ptr<Session> Context::create_session()
{
return shared_ptr<Session>(
new Session(shared_from_this()), Session::Deleter());
}
shared_ptr<Session> Context::load_session(string filename)
{
return shared_ptr<Session>(
new Session(shared_from_this(), filename), Session::Deleter());
}
shared_ptr<Trigger> Context::create_trigger(string name)
{
return shared_ptr<Trigger>(
new Trigger(shared_from_this(), name), Trigger::Deleter());
}
2014-08-24 00:40:19 +00:00
shared_ptr<Input> Context::open_file(string filename)
{
const struct sr_input *input;
2014-09-06 16:28:27 +00:00
check(sr_input_scan_file(filename.c_str(), &input));
2014-08-24 00:40:19 +00:00
return shared_ptr<Input>(
new Input(shared_from_this(), input), Input::Deleter());
}
shared_ptr<Input> Context::open_stream(string header)
{
const struct sr_input *input;
2014-08-24 00:40:19 +00:00
auto gstr = g_string_new(header.c_str());
auto ret = sr_input_scan_buffer(gstr, &input);
g_string_free(gstr, true);
check(ret);
2014-08-24 00:40:19 +00:00
return shared_ptr<Input>(
new Input(shared_from_this(), input), Input::Deleter());
}
2014-07-17 17:39:49 +00:00
Driver::Driver(struct sr_dev_driver *structure) :
ParentOwned(structure),
2014-09-06 12:08:29 +00:00
Configurable(structure, NULL, NULL),
_initialized(false)
2014-07-17 17:39:49 +00:00
{
}
Driver::~Driver()
{
}
string Driver::name()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->name);
2014-07-17 17:39:49 +00:00
}
string Driver::long_name()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->longname);
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<HardwareDevice>> Driver::scan(
map<const ConfigKey *, Glib::VariantBase> options)
{
/* Initialise the driver if not yet done. */
if (!_initialized)
2014-07-17 17:39:49 +00:00
{
check(sr_driver_init(_parent->_structure, _structure));
_initialized = true;
2014-07-17 17:39:49 +00:00
}
/* Translate scan options to GSList of struct sr_config pointers. */
GSList *option_list = NULL;
for (auto entry : options)
{
auto key = entry.first;
auto value = entry.second;
auto config = g_new(struct sr_config, 1);
config->key = key->id();
2014-07-17 17:39:49 +00:00
config->data = value.gobj();
option_list = g_slist_append(option_list, config);
}
/* Run scan. */
GSList *device_list = sr_driver_scan(_structure, option_list);
2014-07-17 17:39:49 +00:00
/* Free option list. */
g_slist_free_full(option_list, g_free);
2014-07-17 17:39:49 +00:00
/* Create device objects. */
vector<shared_ptr<HardwareDevice>> result;
2014-07-17 17:39:49 +00:00
for (GSList *device = device_list; device; device = device->next)
{
auto sdi = (struct sr_dev_inst *) device->data;
result.push_back(shared_ptr<HardwareDevice>(
new HardwareDevice(shared_from_this(), sdi),
HardwareDevice::Deleter()));
2014-07-17 17:39:49 +00:00
}
/* Free GSList returned from scan. */
g_slist_free(device_list);
return result;
}
Configurable::Configurable(
struct sr_dev_driver *driver,
struct sr_dev_inst *sdi,
struct sr_channel_group *cg) :
config_driver(driver),
config_sdi(sdi),
config_channel_group(cg)
{
}
Configurable::~Configurable()
{
}
Glib::VariantBase Configurable::config_get(const ConfigKey *key)
{
GVariant *data;
check(sr_config_get(
config_driver, config_sdi, config_channel_group,
key->id(), &data));
2014-07-17 17:39:49 +00:00
return Glib::VariantBase(data);
}
void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
{
check(sr_config_set(
config_sdi, config_channel_group,
key->id(), value.gobj()));
2014-07-17 17:39:49 +00:00
}
Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
2014-07-17 17:39:49 +00:00
{
GVariant *data;
check(sr_config_list(
config_driver, config_sdi, config_channel_group,
key->id(), &data));
return Glib::VariantContainerBase(data);
2014-07-17 17:39:49 +00:00
}
2014-09-30 10:07:55 +00:00
map<const ConfigKey *, set<Capability>> Configurable::config_keys(const ConfigKey *key)
{
GVariant *gvar_opts;
gsize num_opts;
const uint32_t *opts;
2014-09-30 10:07:55 +00:00
map<const ConfigKey *, set<Capability>> result;
check(sr_config_list(
config_driver, config_sdi, config_channel_group,
key->id(), &gvar_opts));
opts = (const uint32_t *) g_variant_get_fixed_array(
gvar_opts, &num_opts, sizeof(uint32_t));
for (gsize i = 0; i < num_opts; i++)
2014-09-30 10:07:55 +00:00
{
auto key = ConfigKey::get(opts[i] & SR_CONF_MASK);
set<Capability> capabilities;
if (opts[i] & SR_CONF_GET)
capabilities.insert(GET);
if (opts[i] & SR_CONF_SET)
capabilities.insert(SET);
if (opts[i] & SR_CONF_LIST)
capabilities.insert(LIST);
result[key] = capabilities;
}
g_variant_unref(gvar_opts);
return result;
}
bool Configurable::config_check(const ConfigKey *key,
const ConfigKey *index_key)
{
GVariant *gvar_opts;
gsize num_opts;
const uint32_t *opts;
if (sr_config_list(config_driver, config_sdi, config_channel_group,
index_key->id(), &gvar_opts) != SR_OK)
return false;
opts = (const uint32_t *) g_variant_get_fixed_array(
gvar_opts, &num_opts, sizeof(uint32_t));
for (gsize i = 0; i < num_opts; i++)
{
if ((opts[i] & SR_CONF_MASK) == key->id())
{
g_variant_unref(gvar_opts);
return true;
}
}
g_variant_unref(gvar_opts);
return false;
}
2014-07-17 17:39:49 +00:00
Device::Device(struct sr_dev_inst *structure) :
Configurable(structure->driver, structure, NULL),
_structure(structure)
2014-07-17 17:39:49 +00:00
{
for (GSList *entry = structure->channels; entry; entry = entry->next)
{
auto channel = (struct sr_channel *) entry->data;
_channels[channel] = new Channel(channel);
2014-07-17 17:39:49 +00:00
}
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);
}
2014-07-17 17:39:49 +00:00
}
Device::~Device()
{
for (auto entry : _channels)
delete entry.second;
for (auto entry : _channel_groups)
delete entry.second;
2014-07-17 17:39:49 +00:00
}
string Device::vendor()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->vendor);
2014-07-17 17:39:49 +00:00
}
string Device::model()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->model);
2014-07-17 17:39:49 +00:00
}
string Device::version()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->version);
2014-07-17 17:39:49 +00:00
}
string Device::serial_number()
{
return valid_string(_structure->serial_num);
}
string Device::connection_id()
{
return valid_string(_structure->connection_id);
}
vector<shared_ptr<Channel>> Device::channels()
2014-07-17 17:39:49 +00:00
{
vector<shared_ptr<Channel>> result;
for (auto channel = _structure->channels; channel; channel = channel->next)
result.push_back(
_channels[(struct sr_channel *) channel->data]->get_shared_pointer(
get_shared_from_this()));
2014-07-17 17:39:49 +00:00
return result;
}
shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
{
return _channels[ptr]->get_shared_pointer(get_shared_from_this());
}
map<string, shared_ptr<ChannelGroup>>
Device::channel_groups()
{
map<string, shared_ptr<ChannelGroup>> result;
for (auto entry: _channel_groups)
{
auto name = entry.first;
auto channel_group = entry.second;
result[name] = channel_group->get_shared_pointer(get_shared_from_this());
}
return result;
}
2014-07-17 17:39:49 +00:00
void Device::open()
{
check(sr_dev_open(_structure));
2014-07-17 17:39:49 +00:00
}
void Device::close()
{
check(sr_dev_close(_structure));
2014-07-17 17:39:49 +00:00
}
HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
struct sr_dev_inst *structure) :
UserOwned(structure),
2014-07-17 17:39:49 +00:00
Device(structure),
_driver(driver)
2014-07-17 17:39:49 +00:00
{
}
HardwareDevice::~HardwareDevice()
{
}
shared_ptr<Device> HardwareDevice::get_shared_from_this()
{
return static_pointer_cast<Device>(shared_from_this());
}
shared_ptr<Driver> HardwareDevice::driver()
2014-07-17 17:39:49 +00:00
{
return _driver;
2014-07-17 17:39:49 +00:00
}
Channel::Channel(struct sr_channel *structure) :
ParentOwned(structure),
_type(ChannelType::get(_structure->type))
2014-07-17 17:39:49 +00:00
{
}
Channel::~Channel()
{
}
string Channel::name()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->name);
2014-07-17 17:39:49 +00:00
}
void Channel::set_name(string name)
{
check(sr_dev_channel_name_set(_parent->_structure,
_structure->index, name.c_str()));
2014-07-17 17:39:49 +00:00
}
const ChannelType *Channel::type()
2014-07-17 17:39:49 +00:00
{
return ChannelType::get(_structure->type);
2014-07-17 17:39:49 +00:00
}
bool Channel::enabled()
2014-07-17 17:39:49 +00:00
{
return _structure->enabled;
2014-07-17 17:39:49 +00:00
}
void Channel::set_enabled(bool value)
{
check(sr_dev_channel_enable(_parent->_structure, _structure->index, value));
2014-07-17 17:39:49 +00:00
}
unsigned int Channel::index()
{
return _structure->index;
}
ChannelGroup::ChannelGroup(Device *device,
2014-07-17 17:39:49 +00:00
struct sr_channel_group *structure) :
ParentOwned(structure),
Configurable(device->_structure->driver, device->_structure, structure)
2014-07-17 17:39:49 +00:00
{
for (GSList *entry = structure->channels; entry; entry = entry->next)
_channels.push_back(device->_channels[(struct sr_channel *)entry->data]);
2014-07-17 17:39:49 +00:00
}
ChannelGroup::~ChannelGroup()
{
}
string ChannelGroup::name()
2014-07-17 17:39:49 +00:00
{
return valid_string(_structure->name);
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<Channel>> ChannelGroup::channels()
2014-07-17 17:39:49 +00:00
{
vector<shared_ptr<Channel>> result;
for (auto channel : _channels)
result.push_back(channel->get_shared_pointer(_parent));
2014-07-17 17:39:49 +00:00
return result;
}
Trigger::Trigger(shared_ptr<Context> context, string name) :
UserOwned(sr_trigger_new(name.c_str())),
_context(context)
2014-07-17 17:39:49 +00:00
{
for (auto stage = _structure->stages; stage; stage = stage->next)
_stages.push_back(
new TriggerStage((struct sr_trigger_stage *) stage->data));
2014-07-17 17:39:49 +00:00
}
Trigger::~Trigger()
{
for (auto stage: _stages)
2014-07-17 17:39:49 +00:00
delete stage;
sr_trigger_free(_structure);
2014-07-17 17:39:49 +00:00
}
string Trigger::name()
2014-07-17 17:39:49 +00:00
{
return _structure->name;
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<TriggerStage>> Trigger::stages()
2014-07-17 17:39:49 +00:00
{
vector<shared_ptr<TriggerStage>> result;
for (auto stage : _stages)
result.push_back(stage->get_shared_pointer(this));
2014-07-17 17:39:49 +00:00
return result;
}
shared_ptr<TriggerStage> Trigger::add_stage()
{
auto stage = new TriggerStage(sr_trigger_stage_add(_structure));
_stages.push_back(stage);
return stage->get_shared_pointer(this);
2014-07-17 17:39:49 +00:00
}
TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
ParentOwned(structure)
2014-07-17 17:39:49 +00:00
{
}
TriggerStage::~TriggerStage()
{
for (auto match : _matches)
2014-07-17 17:39:49 +00:00
delete match;
}
int TriggerStage::number()
2014-07-17 17:39:49 +00:00
{
return _structure->stage;
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<TriggerMatch>> TriggerStage::matches()
2014-07-17 17:39:49 +00:00
{
vector<shared_ptr<TriggerMatch>> result;
for (auto match : _matches)
result.push_back(match->get_shared_pointer(this));
2014-07-17 17:39:49 +00:00
return result;
}
void TriggerStage::add_match(shared_ptr<Channel> channel,
const TriggerMatchType *type, float value)
2014-07-17 17:39:49 +00:00
{
check(sr_trigger_match_add(_structure,
channel->_structure, type->id(), value));
_matches.push_back(new TriggerMatch(
(struct sr_trigger_match *) g_slist_last(
_structure->matches)->data, channel));
2014-07-17 17:39:49 +00:00
}
void TriggerStage::add_match(shared_ptr<Channel> channel,
const TriggerMatchType *type)
2014-07-17 17:39:49 +00:00
{
add_match(channel, type, NAN);
}
TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
shared_ptr<Channel> channel) :
ParentOwned(structure),
_channel(channel)
2014-07-17 17:39:49 +00:00
{
}
TriggerMatch::~TriggerMatch()
{
}
shared_ptr<Channel> TriggerMatch::channel()
2014-07-17 17:39:49 +00:00
{
return _channel;
2014-07-17 17:39:49 +00:00
}
const TriggerMatchType *TriggerMatch::type()
2014-07-17 17:39:49 +00:00
{
return TriggerMatchType::get(_structure->match);
2014-07-17 17:39:49 +00:00
}
float TriggerMatch::value()
2014-07-17 17:39:49 +00:00
{
return _structure->value;
2014-07-17 17:39:49 +00:00
}
DatafeedCallbackData::DatafeedCallbackData(Session *session,
DatafeedCallbackFunction callback) :
_callback(callback),
_session(session)
2014-07-17 17:39:49 +00:00
{
}
void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *pkt)
{
auto device = _session->get_device(sdi);
auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
_callback(device, packet);
2014-07-17 17:39:49 +00:00
}
SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) :
_source(source)
2014-07-17 17:39:49 +00:00
{
}
bool SourceCallbackData::run(int revents)
{
return _source->_callback((Glib::IOCondition) revents);
2014-07-17 17:39:49 +00:00
}
shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events,
int timeout, SourceCallbackFunction callback)
{
auto result = new EventSource(timeout, callback);
result->_type = EventSource::SOURCE_FD;
result->_fd = fd;
result->_events = events;
2014-07-17 17:39:49 +00:00
return shared_ptr<EventSource>(result, EventSource::Deleter());
}
shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout,
SourceCallbackFunction callback)
{
auto result = new EventSource(timeout, callback);
result->_type = EventSource::SOURCE_POLLFD;
result->_pollfd = pollfd;
2014-07-17 17:39:49 +00:00
return shared_ptr<EventSource>(result, EventSource::Deleter());
}
shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel,
Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
{
auto result = new EventSource(timeout, callback);
result->_type = EventSource::SOURCE_IOCHANNEL;
result->_channel = channel;
result->_events = events;
2014-07-17 17:39:49 +00:00
return shared_ptr<EventSource>(result, EventSource::Deleter());
}
EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
_timeout(timeout),
_callback(callback)
2014-07-17 17:39:49 +00:00
{
}
EventSource::~EventSource()
{
}
SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
ParentOwned(structure),
Device(structure)
{
}
SessionDevice::~SessionDevice()
{
}
shared_ptr<Device> SessionDevice::get_shared_from_this()
{
return static_pointer_cast<Device>(shared_from_this());
}
2014-07-17 17:39:49 +00:00
Session::Session(shared_ptr<Context> context) :
UserOwned(_structure),
_context(context),
_saving(false)
2014-07-17 17:39:49 +00:00
{
check(sr_session_new(&_structure));
_context->_session = this;
2014-07-17 17:39:49 +00:00
}
Session::Session(shared_ptr<Context> context, string filename) :
UserOwned(_structure),
_context(context),
_filename(filename),
_saving(false)
2014-07-17 17:39:49 +00:00
{
check(sr_session_load(filename.c_str(), &_structure));
GSList *dev_list;
check(sr_session_dev_list(_structure, &dev_list));
for (GSList *dev = dev_list; dev; dev = dev->next)
{
auto sdi = (struct sr_dev_inst *) dev->data;
_owned_devices[sdi] = new SessionDevice(sdi);
}
_context->_session = this;
2014-07-17 17:39:49 +00:00
}
Session::~Session()
{
check(sr_session_destroy(_structure));
2014-07-17 17:39:49 +00:00
for (auto callback : _datafeed_callbacks)
2014-07-17 17:39:49 +00:00
delete callback;
for (auto entry : _source_callbacks)
2014-07-17 17:39:49 +00:00
delete entry.second;
for (auto entry : _owned_devices)
delete entry.second;
}
shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
{
if (_owned_devices.count(sdi))
return static_pointer_cast<Device>(
_owned_devices[sdi]->get_shared_pointer(this));
else if (_other_devices.count(sdi))
return _other_devices[sdi];
else
throw Error(SR_ERR_BUG);
2014-07-17 17:39:49 +00:00
}
void Session::add_device(shared_ptr<Device> device)
{
check(sr_session_dev_add(_structure, device->_structure));
_other_devices[device->_structure] = device;
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<Device>> Session::devices()
2014-07-17 17:39:49 +00:00
{
GSList *dev_list;
check(sr_session_dev_list(_structure, &dev_list));
2014-07-17 17:39:49 +00:00
vector<shared_ptr<Device>> result;
for (GSList *dev = dev_list; dev; dev = dev->next)
{
auto sdi = (struct sr_dev_inst *) dev->data;
result.push_back(get_device(sdi));
2014-07-17 17:39:49 +00:00
}
return result;
}
void Session::remove_devices()
{
_other_devices.clear();
check(sr_session_dev_remove_all(_structure));
2014-07-17 17:39:49 +00:00
}
void Session::start()
{
check(sr_session_start(_structure));
2014-07-17 17:39:49 +00:00
}
void Session::run()
{
check(sr_session_run(_structure));
2014-07-17 17:39:49 +00:00
}
void Session::stop()
{
check(sr_session_stop(_structure));
2014-07-17 17:39:49 +00:00
}
void Session::begin_save(string filename)
{
_saving = true;
_save_initialized = false;
_save_filename = filename;
_save_samplerate = 0;
2014-07-17 17:39:49 +00:00
}
void Session::append(shared_ptr<Packet> packet)
2014-07-17 17:39:49 +00:00
{
if (!_saving)
2014-07-17 17:39:49 +00:00
throw Error(SR_ERR);
switch (packet->_structure->type)
2014-07-17 17:39:49 +00:00
{
case SR_DF_META:
{
auto meta = (const struct sr_datafeed_meta *)
packet->_structure->payload;
2014-07-17 17:39:49 +00:00
for (auto l = meta->config; l; l = l->next)
{
auto config = (struct sr_config *) l->data;
if (config->key == SR_CONF_SAMPLERATE)
_save_samplerate = g_variant_get_uint64(config->data);
2014-07-17 17:39:49 +00:00
}
break;
}
case SR_DF_LOGIC:
{
if (_save_samplerate == 0)
2014-07-17 17:39:49 +00:00
{
GVariant *samplerate;
check(sr_config_get(packet->_device->_structure->driver,
packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
&samplerate));
2014-07-17 17:39:49 +00:00
_save_samplerate = g_variant_get_uint64(samplerate);
2014-07-17 17:39:49 +00:00
g_variant_unref(samplerate);
}
if (!_save_initialized)
2014-07-17 17:39:49 +00:00
{
vector<shared_ptr<Channel>> save_channels;
for (auto channel : packet->_device->channels())
if (channel->_structure->enabled &&
channel->_structure->type == SR_CHANNEL_LOGIC)
2014-07-17 17:39:49 +00:00
save_channels.push_back(channel);
auto channels = g_new(char *, save_channels.size());
int i = 0;
for (auto channel : save_channels)
channels[i++] = channel->_structure->name;
2014-07-17 17:39:49 +00:00
channels[i] = NULL;
int ret = sr_session_save_init(_structure, _save_filename.c_str(),
_save_samplerate, channels);
2014-07-17 17:39:49 +00:00
g_free(channels);
if (ret != SR_OK)
throw Error(ret);
_save_initialized = true;
2014-07-17 17:39:49 +00:00
}
auto logic = (const struct sr_datafeed_logic *)
packet->_structure->payload;
2014-07-17 17:39:49 +00:00
check(sr_session_append(_structure, _save_filename.c_str(),
2014-07-17 17:39:49 +00:00
(uint8_t *) logic->data, logic->unitsize,
logic->length / logic->unitsize));
}
}
}
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));
}
2014-07-17 17:39:49 +00:00
static void datafeed_callback(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *pkt, void *cb_data)
{
auto callback = static_cast<DatafeedCallbackData *>(cb_data);
callback->run(sdi, pkt);
}
void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
{
auto cb_data = new DatafeedCallbackData(this, callback);
check(sr_session_datafeed_callback_add(_structure,
datafeed_callback, cb_data));
_datafeed_callbacks.push_back(cb_data);
2014-07-17 17:39:49 +00:00
}
void Session::remove_datafeed_callbacks(void)
{
check(sr_session_datafeed_callback_remove_all(_structure));
for (auto callback : _datafeed_callbacks)
2014-07-17 17:39:49 +00:00
delete callback;
_datafeed_callbacks.clear();
2014-07-17 17:39:49 +00:00
}
static int source_callback(int fd, int revents, void *cb_data)
{
(void) fd;
auto callback = (SourceCallbackData *) cb_data;
return callback->run(revents);
}
void Session::add_source(shared_ptr<EventSource> source)
{
if (_source_callbacks.count(source) == 1)
2014-07-17 17:39:49 +00:00
throw Error(SR_ERR_ARG);
auto cb_data = new SourceCallbackData(source);
switch (source->_type)
2014-07-17 17:39:49 +00:00
{
case EventSource::SOURCE_FD:
check(sr_session_source_add(_structure, source->_fd, source->_events,
source->_timeout, source_callback, cb_data));
2014-07-17 17:39:49 +00:00
break;
case EventSource::SOURCE_POLLFD:
check(sr_session_source_add_pollfd(_structure,
source->_pollfd.gobj(), source->_timeout, source_callback,
2014-07-17 17:39:49 +00:00
cb_data));
break;
case EventSource::SOURCE_IOCHANNEL:
check(sr_session_source_add_channel(_structure,
source->_channel->gobj(), source->_events, source->_timeout,
2014-07-17 17:39:49 +00:00
source_callback, cb_data));
break;
}
_source_callbacks[source] = cb_data;
2014-07-17 17:39:49 +00:00
}
void Session::remove_source(shared_ptr<EventSource> source)
{
if (_source_callbacks.count(source) == 0)
2014-07-17 17:39:49 +00:00
throw Error(SR_ERR_ARG);
switch (source->_type)
2014-07-17 17:39:49 +00:00
{
case EventSource::SOURCE_FD:
check(sr_session_source_remove(_structure, source->_fd));
2014-07-17 17:39:49 +00:00
break;
case EventSource::SOURCE_POLLFD:
check(sr_session_source_remove_pollfd(_structure,
source->_pollfd.gobj()));
2014-07-17 17:39:49 +00:00
break;
case EventSource::SOURCE_IOCHANNEL:
check(sr_session_source_remove_channel(_structure,
source->_channel->gobj()));
2014-07-17 17:39:49 +00:00
break;
}
delete _source_callbacks[source];
2014-07-17 17:39:49 +00:00
_source_callbacks.erase(source);
2014-07-17 17:39:49 +00:00
}
shared_ptr<Trigger> Session::trigger()
{
return _trigger;
}
void Session::set_trigger(shared_ptr<Trigger> trigger)
{
check(sr_session_trigger_set(_structure, trigger->_structure));
_trigger = trigger;
}
string Session::filename()
{
return _filename;
}
Packet::Packet(shared_ptr<Device> device,
const struct sr_datafeed_packet *structure) :
UserOwned(structure),
_device(device)
2014-07-17 17:39:49 +00:00
{
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;
2014-07-17 17:39:49 +00:00
case SR_DF_LOGIC:
_payload = new Logic(
2014-07-17 17:39:49 +00:00
static_cast<const struct sr_datafeed_logic *>(
structure->payload));
break;
case SR_DF_ANALOG:
_payload = new Analog(
2014-07-17 17:39:49 +00:00
static_cast<const struct sr_datafeed_analog *>(
structure->payload));
break;
default:
_payload = nullptr;
break;
2014-07-17 17:39:49 +00:00
}
}
Packet::~Packet()
{
if (_payload)
delete _payload;
2014-07-17 17:39:49 +00:00
}
const PacketType *Packet::type()
{
return PacketType::get(_structure->type);
}
shared_ptr<PacketPayload> Packet::payload()
2014-07-17 17:39:49 +00:00
{
if (_payload)
return _payload->get_shared_pointer(this);
else
throw Error(SR_ERR_NA);
2014-07-17 17:39:49 +00:00
}
PacketPayload::PacketPayload()
{
}
PacketPayload::~PacketPayload()
{
}
Header::Header(const struct sr_datafeed_header *structure) :
ParentOwned(structure),
PacketPayload()
{
}
Header::~Header()
{
}
shared_ptr<PacketPayload> Header::get_shared_pointer(Packet *_parent)
{
return static_pointer_cast<PacketPayload>(
ParentOwned::get_shared_pointer(_parent));
}
int Header::feed_version()
{
return _structure->feed_version;
}
Glib::TimeVal Header::start_time()
{
return Glib::TimeVal(
_structure->starttime.tv_sec,
_structure->starttime.tv_usec);
}
Meta::Meta(const struct sr_datafeed_meta *structure) :
ParentOwned(structure),
PacketPayload()
{
}
Meta::~Meta()
{
}
shared_ptr<PacketPayload> Meta::get_shared_pointer(Packet *_parent)
{
return static_pointer_cast<PacketPayload>(
ParentOwned::get_shared_pointer(_parent));
}
map<const ConfigKey *, Glib::VariantBase> Meta::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) :
ParentOwned(structure),
PacketPayload()
{
}
2014-07-17 17:39:49 +00:00
Logic::~Logic()
{
}
shared_ptr<PacketPayload> Logic::get_shared_pointer(Packet *_parent)
{
return static_pointer_cast<PacketPayload>(
ParentOwned::get_shared_pointer(_parent));
}
void *Logic::data_pointer()
2014-07-17 17:39:49 +00:00
{
return _structure->data;
2014-07-17 17:39:49 +00:00
}
size_t Logic::data_length()
2014-07-17 17:39:49 +00:00
{
return _structure->length;
2014-07-17 17:39:49 +00:00
}
unsigned int Logic::unit_size()
{
return _structure->unitsize;
}
2014-07-17 17:39:49 +00:00
Analog::Analog(const struct sr_datafeed_analog *structure) :
ParentOwned(structure),
PacketPayload()
2014-07-17 17:39:49 +00:00
{
}
Analog::~Analog()
{
}
shared_ptr<PacketPayload> Analog::get_shared_pointer(Packet *_parent)
{
return static_pointer_cast<PacketPayload>(
ParentOwned::get_shared_pointer(_parent));
}
float *Analog::data_pointer()
2014-07-17 17:39:49 +00:00
{
return _structure->data;
2014-07-17 17:39:49 +00:00
}
unsigned int Analog::num_samples()
2014-07-17 17:39:49 +00:00
{
return _structure->num_samples;
2014-07-17 17:39:49 +00:00
}
vector<shared_ptr<Channel>> Analog::channels()
2014-07-17 17:39:49 +00:00
{
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;
2014-07-17 17:39:49 +00:00
}
const Quantity *Analog::mq()
2014-07-17 17:39:49 +00:00
{
return Quantity::get(_structure->mq);
2014-07-17 17:39:49 +00:00
}
const Unit *Analog::unit()
2014-07-17 17:39:49 +00:00
{
return Unit::get(_structure->unit);
2014-07-17 17:39:49 +00:00
}
vector<const QuantityFlag *> Analog::mq_flags()
2014-07-17 17:39:49 +00:00
{
return QuantityFlag::flags_from_mask(_structure->mqflags);
2014-07-17 17:39:49 +00:00
}
2014-08-24 00:40:19 +00:00
InputFormat::InputFormat(const struct sr_input_module *structure) :
ParentOwned(structure)
2014-07-17 17:39:49 +00:00
{
}
InputFormat::~InputFormat()
{
}
string InputFormat::name()
2014-07-17 17:39:49 +00:00
{
return valid_string(sr_input_id_get(_structure));
2014-07-17 17:39:49 +00:00
}
string InputFormat::description()
2014-07-17 17:39:49 +00:00
{
return valid_string(sr_input_description_get(_structure));
2014-08-24 00:40:19 +00:00
}
map<string, shared_ptr<Option>> InputFormat::options()
{
const struct sr_option **options = sr_input_options_get(_structure);
auto option_array = shared_ptr<const struct sr_option *>(
options, sr_input_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;
}
2014-08-24 00:40:19 +00:00
shared_ptr<Input> InputFormat::create_input(
map<string, Glib::VariantBase> options)
{
auto input = sr_input_new(_structure, map_to_hash_variant(options));
2014-08-24 00:40:19 +00:00
if (!input)
throw Error(SR_ERR_ARG);
return shared_ptr<Input>(
new Input(_parent->shared_from_this(), input), Input::Deleter());
2014-07-17 17:39:49 +00:00
}
2014-08-24 00:40:19 +00:00
Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
UserOwned(structure),
_context(context),
_device(nullptr)
2014-07-17 17:39:49 +00:00
{
}
shared_ptr<InputDevice> Input::device()
2014-07-17 17:39:49 +00:00
{
if (!_device)
2014-08-24 00:40:19 +00:00
{
auto sdi = sr_input_dev_inst_get(_structure);
2014-08-24 00:40:19 +00:00
if (!sdi)
throw Error(SR_ERR_NA);
_device = new InputDevice(shared_from_this(), sdi);
2014-08-24 00:40:19 +00:00
}
2014-07-17 17:39:49 +00:00
return _device->get_shared_pointer(shared_from_this());
2014-08-24 00:40:19 +00:00
}
2014-07-17 17:39:49 +00:00
2014-08-24 00:40:19 +00:00
void Input::send(string data)
{
auto gstr = g_string_new(data.c_str());
auto ret = sr_input_send(_structure, gstr);
2014-08-24 00:40:19 +00:00
g_string_free(gstr, false);
check(ret);
2014-07-17 17:39:49 +00:00
}
void Input::end()
{
check(sr_input_end(_structure));
}
2014-08-24 00:40:19 +00:00
Input::~Input()
2014-07-17 17:39:49 +00:00
{
if (_device)
delete _device;
sr_input_free(_structure);
2014-07-17 17:39:49 +00:00
}
InputDevice::InputDevice(shared_ptr<Input> input,
struct sr_dev_inst *structure) :
ParentOwned(structure),
Device(structure),
_input(input)
2014-07-17 17:39:49 +00:00
{
}
2014-08-24 00:40:19 +00:00
InputDevice::~InputDevice()
2014-07-17 17:39:49 +00:00
{
}
shared_ptr<Device> InputDevice::get_shared_from_this()
{
return static_pointer_cast<Device>(shared_from_this());
}
2014-07-26 15:03:25 +00:00
Option::Option(const struct sr_option *structure,
shared_ptr<const struct sr_option *> structure_array) :
UserOwned(structure),
_structure_array(structure_array)
2014-07-26 15:03:25 +00:00
{
}
Option::~Option()
{
}
string Option::id()
2014-07-26 15:03:25 +00:00
{
return valid_string(_structure->id);
2014-07-26 15:03:25 +00:00
}
string Option::name()
2014-07-26 15:03:25 +00:00
{
return valid_string(_structure->name);
2014-07-26 15:03:25 +00:00
}
string Option::description()
2014-07-26 15:03:25 +00:00
{
return valid_string(_structure->desc);
2014-07-26 15:03:25 +00:00
}
Glib::VariantBase Option::default_value()
2014-07-26 15:03:25 +00:00
{
return Glib::VariantBase(_structure->def, true);
2014-07-26 15:03:25 +00:00
}
vector<Glib::VariantBase> Option::values()
2014-07-26 15:03:25 +00:00
{
vector<Glib::VariantBase> result;
for (auto l = _structure->values; l; l = l->next)
2014-07-26 15:03:25 +00:00
result.push_back(Glib::VariantBase((GVariant *) l->data, true));
return result;
}
OutputFormat::OutputFormat(const struct sr_output_module *structure) :
ParentOwned(structure)
2014-07-17 17:39:49 +00:00
{
}
OutputFormat::~OutputFormat()
{
}
string OutputFormat::name()
2014-07-17 17:39:49 +00:00
{
return valid_string(sr_output_id_get(_structure));
2014-07-17 17:39:49 +00:00
}
string OutputFormat::description()
2014-07-17 17:39:49 +00:00
{
return valid_string(sr_output_description_get(_structure));
2014-07-26 15:03:25 +00:00
}
map<string, shared_ptr<Option>> OutputFormat::options()
2014-07-26 15:03:25 +00:00
{
const struct sr_option **options = sr_output_options_get(_structure);
auto option_array = shared_ptr<const struct sr_option *>(
options, sr_output_options_free);
2014-07-26 15:03:25 +00:00
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());
2014-07-26 15:03:25 +00:00
return result;
2014-07-17 17:39:49 +00:00
}
shared_ptr<Output> OutputFormat::create_output(
2014-07-26 15:03:25 +00:00
shared_ptr<Device> device, map<string, Glib::VariantBase> options)
2014-07-17 17:39:49 +00:00
{
return shared_ptr<Output>(
new Output(shared_from_this(), device, options),
2014-07-17 17:39:49 +00:00
Output::Deleter());
}
Output::Output(shared_ptr<OutputFormat> format,
2014-07-26 15:03:25 +00:00
shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
UserOwned(sr_output_new(format->_structure,
map_to_hash_variant(options), device->_structure)),
_format(format),
_device(device),
_options(options)
2014-07-17 17:39:49 +00:00
{
}
Output::~Output()
{
check(sr_output_free(_structure));
2014-07-17 17:39:49 +00:00
}
string Output::receive(shared_ptr<Packet> packet)
{
GString *out;
check(sr_output_send(_structure, packet->_structure, &out));
2014-07-17 17:39:49 +00:00
if (out)
{
auto result = string(out->str, out->str + out->len);
g_string_free(out, true);
return result;
}
else
{
return string();
}
}
#include "enums.cpp"
}