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/>.
|
|
|
|
*/
|
|
|
|
|
2015-09-13 18:12:12 +00:00
|
|
|
/* Needed for isascii(), as used in the GNU libstdc++ headers */
|
2018-01-26 12:53:45 +00:00
|
|
|
/* Needed in strutil.c for POSIX.1-2008 locale functions */
|
2015-09-13 18:12:12 +00:00
|
|
|
#ifndef _XOPEN_SOURCE
|
2018-01-26 12:53:45 +00:00
|
|
|
#define _XOPEN_SOURCE 700
|
2015-09-13 18:12:12 +00:00
|
|
|
#endif
|
|
|
|
|
2015-09-13 16:22:17 +00:00
|
|
|
#include <config.h>
|
2015-08-15 15:54:14 +00:00
|
|
|
#include <libsigrokcxx/libsigrokcxx.hpp>
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2014-07-24 13:45:58 +00:00
|
|
|
#include <sstream>
|
2014-09-13 19:23:23 +00:00
|
|
|
#include <cmath>
|
2014-07-24 13:45:58 +00:00
|
|
|
|
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. */
|
2015-10-11 01:38:19 +00:00
|
|
|
static inline const char *valid_string(const char *input)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 01:38:19 +00:00
|
|
|
return (input) ? input : "";
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 15:03:25 +00:00
|
|
|
/** Helper function to convert between map<string, VariantBase> and GHashTable */
|
2015-10-11 01:12:02 +00:00
|
|
|
static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input)
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
2015-10-15 16:32:44 +00:00
|
|
|
auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
|
2015-10-11 08:48:12 +00:00
|
|
|
reinterpret_cast<GDestroyNotify>(&g_variant_unref));
|
2015-10-15 16:32:44 +00:00
|
|
|
for (const auto &entry : input)
|
2014-07-26 15:03:25 +00:00
|
|
|
g_hash_table_insert(output,
|
|
|
|
g_strdup(entry.first.c_str()),
|
|
|
|
entry.second.gobj_copy());
|
2015-10-11 01:12:02 +00:00
|
|
|
return output;
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
Error::Error(int result) : result(result)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 10:19:27 +00:00
|
|
|
const char *Error::what() const noexcept
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
return sr_strerror(result);
|
|
|
|
}
|
|
|
|
|
2015-10-11 10:19:27 +00:00
|
|
|
Error::~Error() noexcept
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-26 22:03:21 +00:00
|
|
|
ResourceReader::~ResourceReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV int ResourceReader::open_callback(struct sr_resource *res,
|
2015-10-14 18:56:51 +00:00
|
|
|
const char *name, void *cb_data) noexcept
|
2015-09-26 22:03:21 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto *const reader = static_cast<ResourceReader*>(cb_data);
|
|
|
|
reader->open(res, name);
|
|
|
|
} catch (const Error &err) {
|
|
|
|
return err.result;
|
|
|
|
} catch (...) {
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-14 18:56:51 +00:00
|
|
|
SR_PRIV int ResourceReader::close_callback(struct sr_resource *res,
|
|
|
|
void *cb_data) noexcept
|
2015-09-26 22:03:21 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto *const reader = static_cast<ResourceReader*>(cb_data);
|
|
|
|
reader->close(res);
|
|
|
|
} catch (const Error &err) {
|
|
|
|
return err.result;
|
|
|
|
} catch (...) {
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-04 17:53:30 +00:00
|
|
|
SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res,
|
2015-10-14 18:56:51 +00:00
|
|
|
void *buf, size_t count, void *cb_data) noexcept
|
2015-09-26 22:03:21 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto *const reader = static_cast<ResourceReader*>(cb_data);
|
|
|
|
return reader->read(res, buf, count);
|
|
|
|
} catch (const Error &err) {
|
|
|
|
return err.result;
|
|
|
|
} catch (...) {
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
shared_ptr<Context> Context::create()
|
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Context>{new Context{}, default_delete<Context>{}};
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Context::Context() :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(nullptr),
|
2015-10-11 01:38:19 +00:00
|
|
|
_session(nullptr)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_init(&_structure));
|
2014-09-02 20:05:42 +00:00
|
|
|
|
2015-10-15 16:32:44 +00:00
|
|
|
if (struct sr_dev_driver **driver_list = sr_driver_list(_structure))
|
|
|
|
for (int i = 0; driver_list[i]; i++) {
|
|
|
|
unique_ptr<Driver> driver {new Driver{driver_list[i]}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_drivers.emplace(driver->name(), move(driver));
|
2015-10-15 16:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const struct sr_input_module **input_list = sr_input_list())
|
|
|
|
for (int i = 0; input_list[i]; i++) {
|
|
|
|
unique_ptr<InputFormat> input {new InputFormat{input_list[i]}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_input_formats.emplace(input->name(), move(input));
|
2015-10-15 16:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const struct sr_output_module **output_list = sr_output_list())
|
|
|
|
for (int i = 0; output_list[i]; i++) {
|
|
|
|
unique_ptr<OutputFormat> output {new OutputFormat{output_list[i]}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_output_formats.emplace(output->name(), move(output));
|
2015-10-15 16:32:44 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
string Context::package_version()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
return sr_package_version_string_get();
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
string Context::lib_version()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
return sr_lib_version_string_get();
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
map<string, shared_ptr<Driver>> Context::drivers()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<Driver>> result;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &entry: _drivers) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &name = entry.first;
|
|
|
|
const auto &driver = entry.second;
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(name, driver->share_owned_by(shared_from_this()));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
map<string, shared_ptr<InputFormat>> Context::input_formats()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<InputFormat>> result;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &entry: _input_formats) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &name = entry.first;
|
|
|
|
const auto &input_format = entry.second;
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(name, input_format->share_owned_by(shared_from_this()));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-13 06:56:09 +00:00
|
|
|
shared_ptr<InputFormat> Context::input_format_match(string filename)
|
|
|
|
{
|
|
|
|
const struct sr_input *input;
|
|
|
|
const struct sr_input_module *imod;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Have the input module looked up for the specified file.
|
|
|
|
* Failed lookup (or "successful lookup" with an empty result)
|
|
|
|
* are non-fatal. Free the sr_input that was created by the
|
|
|
|
* lookup routine, but grab the input module kind and return an
|
|
|
|
* InputFormat instance to the application. This works because
|
|
|
|
* the application passes a filename, no input data got buffered
|
|
|
|
* in the sr_input that we release.
|
|
|
|
*/
|
|
|
|
input = NULL;
|
|
|
|
rc = sr_input_scan_file(filename.c_str(), &input);
|
|
|
|
if (rc != SR_OK)
|
|
|
|
return nullptr;
|
|
|
|
if (!input)
|
|
|
|
return nullptr;
|
|
|
|
imod = sr_input_module_get(input);
|
|
|
|
sr_input_free(input);
|
|
|
|
return shared_ptr<InputFormat>{new InputFormat{imod}, default_delete<InputFormat>{}};
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
map<string, shared_ptr<OutputFormat>> Context::output_formats()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<OutputFormat>> result;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &entry: _output_formats) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &name = entry.first;
|
|
|
|
const auto &output_format = entry.second;
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(name, output_format->share_owned_by(shared_from_this()));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_exit(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const LogLevel *Context::log_level() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
return LogLevel::get(sr_log_loglevel_get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Context::set_log_level(const LogLevel *level)
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_log_loglevel_set(level->id()));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 18:56:51 +00:00
|
|
|
static int call_log_callback(void *cb_data, int loglevel,
|
|
|
|
const char *format, va_list args) noexcept
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 01:38:19 +00:00
|
|
|
const unique_ptr<char, decltype(&g_free)>
|
|
|
|
message {g_strdup_vprintf(format, args), &g_free};
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-10-11 08:48:12 +00:00
|
|
|
auto *const callback = static_cast<LogCallbackFunction *>(cb_data);
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2017-06-11 07:51:59 +00:00
|
|
|
try {
|
2015-10-11 08:48:12 +00:00
|
|
|
(*callback)(LogLevel::get(loglevel), message.get());
|
2018-07-22 15:07:26 +00:00
|
|
|
} catch (Error &e) {
|
2014-07-17 17:39:49 +00:00
|
|
|
return e.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Context::set_log_callback(LogCallbackFunction callback)
|
|
|
|
{
|
2015-10-11 01:12:02 +00:00
|
|
|
_log_callback = move(callback);
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_log_callback_set(call_log_callback, &_log_callback));
|
2015-09-26 22:03:21 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
void Context::set_log_callback_default()
|
|
|
|
{
|
|
|
|
check(sr_log_callback_set_default());
|
2014-09-11 20:25:52 +00:00
|
|
|
_log_callback = nullptr;
|
2015-09-26 22:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Context::set_resource_reader(ResourceReader *reader)
|
|
|
|
{
|
|
|
|
if (reader) {
|
|
|
|
check(sr_resource_set_hooks(_structure,
|
|
|
|
&ResourceReader::open_callback,
|
|
|
|
&ResourceReader::close_callback,
|
|
|
|
&ResourceReader::read_callback, reader));
|
|
|
|
} else {
|
|
|
|
check(sr_resource_set_hooks(_structure,
|
|
|
|
nullptr, nullptr, nullptr, nullptr));
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
shared_ptr<Session> Context::create_session()
|
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Session>{new Session{shared_from_this()},
|
|
|
|
default_delete<Session>{}};
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 02:23:02 +00:00
|
|
|
shared_ptr<UserDevice> Context::create_user_device(
|
|
|
|
string vendor, string model, string version)
|
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<UserDevice>{
|
|
|
|
new UserDevice{move(vendor), move(model), move(version)},
|
|
|
|
default_delete<UserDevice>{}};
|
2014-11-12 02:23:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 12:37:22 +00:00
|
|
|
shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time)
|
|
|
|
{
|
|
|
|
auto header = g_new(struct sr_datafeed_header, 1);
|
|
|
|
header->feed_version = 1;
|
|
|
|
header->starttime.tv_sec = start_time.tv_sec;
|
|
|
|
header->starttime.tv_usec = start_time.tv_usec;
|
|
|
|
auto packet = g_new(struct sr_datafeed_packet, 1);
|
|
|
|
packet->type = SR_DF_HEADER;
|
|
|
|
packet->payload = header;
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Packet>{new Packet{nullptr, packet},
|
|
|
|
default_delete<Packet>{}};
|
2014-11-12 12:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Packet> Context::create_meta_packet(
|
2015-10-26 06:04:10 +00:00
|
|
|
map<const ConfigKey *, Glib::VariantBase> config)
|
2014-11-12 12:37:22 +00:00
|
|
|
{
|
|
|
|
auto meta = g_new0(struct sr_datafeed_meta, 1);
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &input : config) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &key = input.first;
|
|
|
|
const auto &value = input.second;
|
|
|
|
auto *const output = g_new(struct sr_config, 1);
|
2014-11-12 12:37:22 +00:00
|
|
|
output->key = key->id();
|
2015-10-15 16:32:44 +00:00
|
|
|
output->data = value.gobj_copy();
|
2014-11-12 12:37:22 +00:00
|
|
|
meta->config = g_slist_append(meta->config, output);
|
|
|
|
}
|
|
|
|
auto packet = g_new(struct sr_datafeed_packet, 1);
|
|
|
|
packet->type = SR_DF_META;
|
|
|
|
packet->payload = meta;
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Packet>{new Packet{nullptr, packet},
|
|
|
|
default_delete<Packet>{}};
|
2014-11-12 12:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Packet> Context::create_logic_packet(
|
|
|
|
void *data_pointer, size_t data_length, unsigned int unit_size)
|
|
|
|
{
|
|
|
|
auto logic = g_new(struct sr_datafeed_logic, 1);
|
|
|
|
logic->length = data_length;
|
|
|
|
logic->unitsize = unit_size;
|
|
|
|
logic->data = data_pointer;
|
|
|
|
auto packet = g_new(struct sr_datafeed_packet, 1);
|
|
|
|
packet->type = SR_DF_LOGIC;
|
|
|
|
packet->payload = logic;
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
|
2014-11-12 12:37:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 14:18:35 +00:00
|
|
|
shared_ptr<Packet> Context::create_analog_packet(
|
2015-10-26 06:04:10 +00:00
|
|
|
vector<shared_ptr<Channel> > channels,
|
2017-07-01 21:57:34 +00:00
|
|
|
const float *data_pointer, unsigned int num_samples, const Quantity *mq,
|
2015-10-26 06:04:10 +00:00
|
|
|
const Unit *unit, vector<const QuantityFlag *> mqflags)
|
2014-11-12 12:37:22 +00:00
|
|
|
{
|
2015-10-02 14:18:35 +00:00
|
|
|
auto analog = g_new0(struct sr_datafeed_analog, 1);
|
|
|
|
auto meaning = g_new0(struct sr_analog_meaning, 1);
|
2016-03-22 07:25:08 +00:00
|
|
|
auto encoding = g_new0(struct sr_analog_encoding, 1);
|
|
|
|
auto spec = g_new0(struct sr_analog_spec, 1);
|
2015-10-02 14:18:35 +00:00
|
|
|
|
|
|
|
analog->meaning = meaning;
|
|
|
|
|
2015-10-15 16:32:44 +00:00
|
|
|
for (const auto &channel : channels)
|
2015-10-02 14:18:35 +00:00
|
|
|
meaning->channels = g_slist_append(meaning->channels, channel->_structure);
|
2015-10-11 08:48:12 +00:00
|
|
|
meaning->mq = static_cast<sr_mq>(mq->id());
|
|
|
|
meaning->unit = static_cast<sr_unit>(unit->id());
|
2015-10-26 06:04:10 +00:00
|
|
|
meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(move(mqflags)));
|
2016-03-22 07:25:08 +00:00
|
|
|
|
|
|
|
analog->encoding = encoding;
|
|
|
|
|
|
|
|
encoding->unitsize = sizeof(float);
|
|
|
|
encoding->is_signed = TRUE;
|
|
|
|
encoding->is_float = TRUE;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
encoding->is_bigendian = TRUE;
|
|
|
|
#else
|
|
|
|
encoding->is_bigendian = FALSE;
|
|
|
|
#endif
|
|
|
|
encoding->digits = 0;
|
|
|
|
encoding->is_digits_decimal = FALSE;
|
|
|
|
encoding->scale.p = 1;
|
|
|
|
encoding->scale.q = 1;
|
|
|
|
encoding->offset.p = 0;
|
|
|
|
encoding->offset.q = 1;
|
|
|
|
|
|
|
|
analog->spec = spec;
|
|
|
|
|
|
|
|
spec->spec_digits = 0;
|
|
|
|
|
|
|
|
analog->num_samples = num_samples;
|
2017-07-01 21:57:34 +00:00
|
|
|
analog->data = (float*)data_pointer;
|
2014-11-12 12:37:22 +00:00
|
|
|
auto packet = g_new(struct sr_datafeed_packet, 1);
|
2015-10-02 14:18:35 +00:00
|
|
|
packet->type = SR_DF_ANALOG;
|
2014-11-12 12:37:22 +00:00
|
|
|
packet->payload = analog;
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
|
2014-11-12 12:37:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 19:38:30 +00:00
|
|
|
shared_ptr<Packet> Context::create_end_packet()
|
|
|
|
{
|
|
|
|
auto packet = g_new(struct sr_datafeed_packet, 1);
|
|
|
|
packet->type = SR_DF_END;
|
|
|
|
return shared_ptr<Packet>{new Packet{nullptr, packet},
|
|
|
|
default_delete<Packet>{}};
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
shared_ptr<Session> Context::load_session(string filename)
|
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Session>{
|
|
|
|
new Session{shared_from_this(), move(filename)},
|
|
|
|
default_delete<Session>{}};
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Trigger> Context::create_trigger(string name)
|
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Trigger>{
|
|
|
|
new Trigger{shared_from_this(), move(name)},
|
|
|
|
default_delete<Trigger>{}};
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-24 00:40:19 +00:00
|
|
|
shared_ptr<Input> Context::open_file(string filename)
|
|
|
|
{
|
2014-08-27 23:49:48 +00:00
|
|
|
const struct sr_input *input;
|
|
|
|
|
2014-09-06 16:28:27 +00:00
|
|
|
check(sr_input_scan_file(filename.c_str(), &input));
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Input>{
|
|
|
|
new Input{shared_from_this(), input},
|
|
|
|
default_delete<Input>{}};
|
2014-08-24 00:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Input> Context::open_stream(string header)
|
|
|
|
{
|
2014-08-27 23:49:48 +00:00
|
|
|
const struct sr_input *input;
|
|
|
|
|
2014-08-24 00:40:19 +00:00
|
|
|
auto gstr = g_string_new(header.c_str());
|
2014-08-27 23:49:48 +00:00
|
|
|
auto ret = sr_input_scan_buffer(gstr, &input);
|
|
|
|
g_string_free(gstr, true);
|
|
|
|
check(ret);
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Input>{
|
|
|
|
new Input{shared_from_this(), input},
|
|
|
|
default_delete<Input>{}};
|
2014-08-24 00:40:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
map<string, string> Context::serials(shared_ptr<Driver> driver) const
|
2014-12-06 23:56:22 +00:00
|
|
|
{
|
2015-10-11 01:38:19 +00:00
|
|
|
GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr);
|
2014-12-06 23:56:22 +00:00
|
|
|
map<string, string> serials;
|
|
|
|
|
|
|
|
for (GSList *serial = serial_list; serial; serial = serial->next) {
|
2015-10-11 08:48:12 +00:00
|
|
|
auto *const port = static_cast<sr_serial_port *>(serial->data);
|
2014-12-06 23:56:22 +00:00
|
|
|
serials[string(port->name)] = string(port->description);
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:48:12 +00:00
|
|
|
g_slist_free_full(serial_list,
|
|
|
|
reinterpret_cast<GDestroyNotify>(&sr_serial_free));
|
2014-12-06 23:56:22 +00:00
|
|
|
return serials;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
Driver::Driver(struct sr_dev_driver *structure) :
|
2015-10-11 01:38:19 +00:00
|
|
|
Configurable(structure, nullptr, nullptr),
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2014-09-11 20:25:52 +00:00
|
|
|
_initialized(false)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Driver::~Driver()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Driver::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->name);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Driver::long_name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->longname);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
set<const ConfigKey *> Driver::scan_options() const
|
|
|
|
{
|
2015-12-31 17:47:55 +00:00
|
|
|
GArray *opts = sr_driver_scan_options_list(_structure);
|
2015-11-02 22:05:02 +00:00
|
|
|
set<const ConfigKey *> result;
|
2016-04-17 07:30:54 +00:00
|
|
|
if (opts) {
|
|
|
|
for (guint i = 0; i < opts->len; i++)
|
|
|
|
result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
|
|
|
|
g_array_free(opts, TRUE);
|
|
|
|
}
|
2015-11-02 22:05:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
vector<shared_ptr<HardwareDevice>> Driver::scan(
|
2015-10-26 06:04:10 +00:00
|
|
|
map<const ConfigKey *, Glib::VariantBase> options)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
/* Initialise the driver if not yet done. */
|
2017-06-11 07:51:59 +00:00
|
|
|
if (!_initialized) {
|
2014-09-11 20:25:52 +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. */
|
2015-10-11 01:38:19 +00:00
|
|
|
GSList *option_list = nullptr;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &entry : options) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &key = entry.first;
|
|
|
|
const auto &value = entry.second;
|
|
|
|
auto *const config = g_new(struct sr_config, 1);
|
2014-09-11 20:25:52 +00:00
|
|
|
config->key = key->id();
|
2015-10-15 16:32:44 +00:00
|
|
|
config->data = const_cast<GVariant*>(value.gobj());
|
2014-07-17 17:39:49 +00:00
|
|
|
option_list = g_slist_append(option_list, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run scan. */
|
2014-09-11 20:25:52 +00:00
|
|
|
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-09-14 04:33:24 +00:00
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
/* Create device objects. */
|
2014-09-14 04:33:24 +00:00
|
|
|
vector<shared_ptr<HardwareDevice>> result;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (GSList *device = device_list; device; device = device->next) {
|
2015-10-11 08:48:12 +00:00
|
|
|
auto *const sdi = static_cast<struct sr_dev_inst *>(device->data);
|
2015-10-15 17:21:28 +00:00
|
|
|
shared_ptr<HardwareDevice> hwdev {
|
|
|
|
new HardwareDevice{shared_from_this(), sdi},
|
|
|
|
default_delete<HardwareDevice>{}};
|
|
|
|
result.push_back(move(hwdev));
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
set<const ConfigKey *> Configurable::config_keys() const
|
|
|
|
{
|
|
|
|
GArray *opts;
|
|
|
|
set<const ConfigKey *> result;
|
|
|
|
|
|
|
|
opts = sr_dev_options(config_driver, config_sdi, config_channel_group);
|
|
|
|
|
2016-04-26 16:07:03 +00:00
|
|
|
if (opts) {
|
|
|
|
for (guint i = 0; i < opts->len; i++)
|
|
|
|
result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
|
|
|
|
g_array_free(opts, TRUE);
|
|
|
|
}
|
2015-11-02 22:05:02 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
Glib::VariantBase Configurable::config_get(const ConfigKey *key) const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
GVariant *data;
|
|
|
|
check(sr_config_get(
|
|
|
|
config_driver, config_sdi, config_channel_group,
|
2014-09-11 20:25:52 +00:00
|
|
|
key->id(), &data));
|
2014-07-17 17:39:49 +00:00
|
|
|
return Glib::VariantBase(data);
|
|
|
|
}
|
|
|
|
|
2015-10-11 01:12:02 +00:00
|
|
|
void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
check(sr_config_set(
|
|
|
|
config_sdi, config_channel_group,
|
2015-10-11 01:12:02 +00:00
|
|
|
key->id(), const_cast<GVariant*>(value.gobj())));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
set<const Capability *> Configurable::config_capabilities(const ConfigKey *key) const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-12-31 17:45:17 +00:00
|
|
|
int caps = sr_dev_config_capabilities_list(config_sdi,
|
|
|
|
config_channel_group, key->id());
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
set<const Capability *> result;
|
2014-09-06 12:08:51 +00:00
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
for (auto cap: Capability::values())
|
|
|
|
if (caps & cap->id())
|
|
|
|
result.insert(cap);
|
2014-09-06 12:08:51 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-06 12:53:09 +00:00
|
|
|
bool Configurable::config_check(const ConfigKey *key,
|
2015-11-02 22:05:02 +00:00
|
|
|
const Capability *capability) const
|
2014-09-06 12:53:09 +00:00
|
|
|
{
|
2015-12-31 17:45:17 +00:00
|
|
|
int caps = sr_dev_config_capabilities_list(config_sdi,
|
|
|
|
config_channel_group, key->id());
|
2014-09-06 12:53:09 +00:00
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
return (caps & capability->id());
|
|
|
|
}
|
2014-09-06 12:53:09 +00:00
|
|
|
|
2015-11-02 22:05:02 +00:00
|
|
|
Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const
|
|
|
|
{
|
|
|
|
GVariant *data;
|
|
|
|
check(sr_config_list(
|
|
|
|
config_driver, config_sdi, config_channel_group,
|
|
|
|
key->id(), &data));
|
|
|
|
return Glib::VariantContainerBase(data);
|
2014-09-06 12:53:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
Device::Device(struct sr_dev_inst *structure) :
|
2015-10-11 01:38:19 +00:00
|
|
|
Configurable(sr_dev_inst_driver_get(structure), structure, nullptr),
|
2014-09-11 20:25:52 +00:00
|
|
|
_structure(structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2017-06-11 07:51:59 +00:00
|
|
|
for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) {
|
2015-10-15 16:32:44 +00:00
|
|
|
auto *const ch = static_cast<struct sr_channel *>(entry->data);
|
|
|
|
unique_ptr<Channel> channel {new Channel{ch}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_channels.emplace(ch, move(channel));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
2014-07-24 11:41:49 +00:00
|
|
|
|
2017-06-11 07:51:59 +00:00
|
|
|
for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) {
|
2015-10-15 16:32:44 +00:00
|
|
|
auto *const cg = static_cast<struct sr_channel_group *>(entry->data);
|
|
|
|
unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_channel_groups.emplace(group->name(), move(group));
|
2014-07-24 11:41:49 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Device::~Device()
|
2017-06-11 07:51:59 +00:00
|
|
|
{
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Device::vendor() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-11-11 15:55:37 +00:00
|
|
|
return valid_string(sr_dev_inst_vendor_get(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Device::model() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-11-11 15:55:37 +00:00
|
|
|
return valid_string(sr_dev_inst_model_get(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Device::version() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-11-11 15:55:37 +00:00
|
|
|
return valid_string(sr_dev_inst_version_get(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Device::serial_number() const
|
2014-09-30 10:50:07 +00:00
|
|
|
{
|
2014-11-11 15:55:37 +00:00
|
|
|
return valid_string(sr_dev_inst_sernum_get(_structure));
|
2014-09-30 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Device::connection_id() const
|
2014-09-30 10:50:07 +00:00
|
|
|
{
|
2014-11-11 15:55:37 +00:00
|
|
|
return valid_string(sr_dev_inst_connid_get(_structure));
|
2014-09-30 10:50:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
vector<shared_ptr<Channel>> Device::channels()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
vector<shared_ptr<Channel>> result;
|
2015-10-11 08:48:12 +00:00
|
|
|
for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) {
|
|
|
|
auto *const ch = static_cast<struct sr_channel *>(channel->data);
|
2015-10-11 15:11:37 +00:00
|
|
|
result.push_back(_channels[ch]->share_owned_by(get_shared_from_this()));
|
2015-10-11 08:48:12 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-24 02:22:44 +00:00
|
|
|
shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
|
|
|
|
{
|
2015-10-11 15:11:37 +00:00
|
|
|
return _channels[ptr]->share_owned_by(get_shared_from_this());
|
2014-07-24 02:22:44 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 11:41:49 +00:00
|
|
|
map<string, shared_ptr<ChannelGroup>>
|
2014-09-11 20:25:52 +00:00
|
|
|
Device::channel_groups()
|
2014-07-24 11:41:49 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<ChannelGroup>> result;
|
2017-06-11 07:51:59 +00:00
|
|
|
for (const auto &entry: _channel_groups) {
|
2015-10-15 16:32:44 +00:00
|
|
|
const auto &name = entry.first;
|
|
|
|
const auto &channel_group = entry.second;
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(name, channel_group->share_owned_by(get_shared_from_this()));
|
2014-07-24 11:41:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
void Device::open()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_dev_open(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::close()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_dev_close(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 04:33:24 +00:00
|
|
|
HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
|
|
|
|
struct sr_dev_inst *structure) :
|
2014-07-17 17:39:49 +00:00
|
|
|
Device(structure),
|
2015-10-11 01:12:02 +00:00
|
|
|
_driver(move(driver))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HardwareDevice::~HardwareDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:51 +00:00
|
|
|
shared_ptr<Device> HardwareDevice::get_shared_from_this()
|
|
|
|
{
|
2014-09-02 19:30:50 +00:00
|
|
|
return static_pointer_cast<Device>(shared_from_this());
|
2014-08-29 13:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
shared_ptr<Driver> HardwareDevice::driver()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-14 04:33:24 +00:00
|
|
|
return _driver;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 02:23:02 +00:00
|
|
|
UserDevice::UserDevice(string vendor, string model, string version) :
|
2015-10-11 15:57:30 +00:00
|
|
|
Device(sr_dev_inst_user_new(
|
|
|
|
vendor.c_str(), model.c_str(), version.c_str()))
|
2014-11-12 02:23:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDevice::~UserDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Device> UserDevice::get_shared_from_this()
|
|
|
|
{
|
|
|
|
return static_pointer_cast<Device>(shared_from_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
|
|
|
|
const ChannelType *type, string name)
|
|
|
|
{
|
|
|
|
check(sr_dev_inst_channel_add(Device::_structure,
|
|
|
|
index, type->id(), name.c_str()));
|
2015-10-11 08:48:12 +00:00
|
|
|
GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure));
|
|
|
|
auto *const ch = static_cast<struct sr_channel *>(last->data);
|
2015-10-15 16:32:44 +00:00
|
|
|
unique_ptr<Channel> channel {new Channel{ch}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_channels.emplace(ch, move(channel));
|
2015-10-11 08:48:12 +00:00
|
|
|
return get_channel(ch);
|
2014-11-12 02:23:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
Channel::Channel(struct sr_channel *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2014-09-11 20:25:52 +00:00
|
|
|
_type(ChannelType::get(_structure->type))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::~Channel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Channel::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->name);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::set_name(string name)
|
|
|
|
{
|
2015-03-19 21:55:48 +00:00
|
|
|
check(sr_dev_channel_name_set(_structure, name.c_str()));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const ChannelType *Channel::type() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return ChannelType::get(_structure->type);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
bool Channel::enabled() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->enabled;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::set_enabled(bool value)
|
|
|
|
{
|
2015-03-19 21:55:48 +00:00
|
|
|
check(sr_dev_channel_enable(_structure, value));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
unsigned int Channel::index() const
|
2014-07-24 14:01:26 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->index;
|
2014-07-24 14:01:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 16:32:44 +00:00
|
|
|
ChannelGroup::ChannelGroup(const Device *device,
|
2014-07-17 17:39:49 +00:00
|
|
|
struct sr_channel_group *structure) :
|
2014-11-11 15:55:37 +00:00
|
|
|
Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 15:57:30 +00:00
|
|
|
for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) {
|
2015-10-11 08:48:12 +00:00
|
|
|
auto *const ch = static_cast<struct sr_channel *>(entry->data);
|
2015-10-15 16:32:44 +00:00
|
|
|
/* Note: This relies on Device::_channels to keep the Channel
|
|
|
|
* objects around over the lifetime of the ChannelGroup. */
|
|
|
|
_channels.push_back(device->_channels.find(ch)->second.get());
|
2015-10-11 08:48:12 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChannelGroup::~ChannelGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string ChannelGroup::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 15:57:30 +00:00
|
|
|
return valid_string(config_channel_group->name);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
vector<shared_ptr<Channel>> ChannelGroup::channels()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
vector<shared_ptr<Channel>> result;
|
2015-10-15 16:32:44 +00:00
|
|
|
for (const auto &channel : _channels)
|
2015-10-11 15:11:37 +00:00
|
|
|
result.push_back(channel->share_owned_by(_parent));
|
2014-07-17 17:39:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:10:02 +00:00
|
|
|
Trigger::Trigger(shared_ptr<Context> context, string name) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(sr_trigger_new(name.c_str())),
|
2015-10-11 01:12:02 +00:00
|
|
|
_context(move(context))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-15 16:32:44 +00:00
|
|
|
for (auto *stage = _structure->stages; stage; stage = stage->next) {
|
|
|
|
unique_ptr<TriggerStage> ts {new TriggerStage{
|
|
|
|
static_cast<struct sr_trigger_stage *>(stage->data)}};
|
|
|
|
_stages.push_back(move(ts));
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Trigger::~Trigger()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
sr_trigger_free(_structure);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Trigger::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->name;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
vector<shared_ptr<TriggerStage>> Trigger::stages()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
vector<shared_ptr<TriggerStage>> result;
|
2015-10-15 16:32:44 +00:00
|
|
|
for (const auto &stage : _stages)
|
2015-10-11 15:11:37 +00:00
|
|
|
result.push_back(stage->share_owned_by(shared_from_this()));
|
2014-07-17 17:39:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<TriggerStage> Trigger::add_stage()
|
|
|
|
{
|
2015-10-15 16:32:44 +00:00
|
|
|
unique_ptr<TriggerStage> stage {new TriggerStage{sr_trigger_stage_add(_structure)}};
|
|
|
|
_stages.push_back(move(stage));
|
|
|
|
return _stages.back()->share_owned_by(shared_from_this());
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 15:57:30 +00:00
|
|
|
TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
|
|
|
|
_structure(structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerStage::~TriggerStage()
|
|
|
|
{
|
|
|
|
}
|
2017-06-06 12:10:02 +00:00
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
int TriggerStage::number() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->stage;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
vector<shared_ptr<TriggerMatch>> TriggerStage::matches()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
vector<shared_ptr<TriggerMatch>> result;
|
2015-10-15 16:32:44 +00:00
|
|
|
for (const auto &match : _matches)
|
2015-10-11 15:11:37 +00:00
|
|
|
result.push_back(match->share_owned_by(shared_from_this()));
|
2014-07-17 17:39:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
void TriggerStage::add_match(shared_ptr<Channel> channel,
|
|
|
|
const TriggerMatchType *type, float value)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_trigger_match_add(_structure,
|
|
|
|
channel->_structure, type->id(), value));
|
2015-10-11 08:48:12 +00:00
|
|
|
GSList *const last = g_slist_last(_structure->matches);
|
2015-10-15 16:32:44 +00:00
|
|
|
unique_ptr<TriggerMatch> match {new TriggerMatch{
|
|
|
|
static_cast<struct sr_trigger_match *>(last->data),
|
|
|
|
move(channel)}};
|
|
|
|
_matches.push_back(move(match));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
void TriggerStage::add_match(shared_ptr<Channel> channel,
|
|
|
|
const TriggerMatchType *type)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 01:12:02 +00:00
|
|
|
add_match(move(channel), type, NAN);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
|
|
|
|
shared_ptr<Channel> channel) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2015-10-11 01:12:02 +00:00
|
|
|
_channel(move(channel))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerMatch::~TriggerMatch()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
shared_ptr<Channel> TriggerMatch::channel()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _channel;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const TriggerMatchType *TriggerMatch::type() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return TriggerMatchType::get(_structure->match);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
float TriggerMatch::value() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->value;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DatafeedCallbackData::DatafeedCallbackData(Session *session,
|
|
|
|
DatafeedCallbackFunction callback) :
|
2015-10-11 01:12:02 +00:00
|
|
|
_callback(move(callback)),
|
2014-09-11 20:25:52 +00:00
|
|
|
_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)
|
|
|
|
{
|
2014-09-30 15:05:27 +00:00
|
|
|
auto device = _session->get_device(sdi);
|
2015-10-15 17:21:28 +00:00
|
|
|
shared_ptr<Packet> packet {new Packet{device, pkt}, default_delete<Packet>{}};
|
2015-10-11 01:12:02 +00:00
|
|
|
_callback(move(device), move(packet));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 15:06:00 +00:00
|
|
|
SessionDevice::SessionDevice(struct sr_dev_inst *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) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(nullptr),
|
2015-10-11 01:12:02 +00:00
|
|
|
_context(move(context))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 01:12:02 +00:00
|
|
|
check(sr_session_new(_context->_structure, &_structure));
|
2014-09-11 20:25:52 +00:00
|
|
|
_context->_session = this;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::Session(shared_ptr<Context> context, string filename) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(nullptr),
|
2015-10-11 01:12:02 +00:00
|
|
|
_context(move(context)),
|
|
|
|
_filename(move(filename))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 01:12:02 +00:00
|
|
|
check(sr_session_load(_context->_structure, _filename.c_str(), &_structure));
|
2014-09-06 15:06:00 +00:00
|
|
|
GSList *dev_list;
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_dev_list(_structure, &dev_list));
|
2015-10-11 08:48:12 +00:00
|
|
|
for (GSList *dev = dev_list; dev; dev = dev->next) {
|
|
|
|
auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
|
2015-10-15 16:32:44 +00:00
|
|
|
unique_ptr<SessionDevice> device {new SessionDevice{sdi}};
|
2017-06-28 06:54:57 +00:00
|
|
|
_owned_devices.emplace(sdi, move(device));
|
2014-09-06 15:06:00 +00:00
|
|
|
}
|
2014-09-11 20:25:52 +00:00
|
|
|
_context->_session = this;
|
2018-10-14 23:17:09 +00:00
|
|
|
g_slist_free(dev_list);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_destroy(_structure));
|
2014-09-30 15:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
|
|
|
|
{
|
|
|
|
if (_owned_devices.count(sdi))
|
|
|
|
return static_pointer_cast<Device>(
|
2015-10-11 15:11:37 +00:00
|
|
|
_owned_devices[sdi]->share_owned_by(shared_from_this()));
|
2014-09-30 15:05:27 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-10-11 01:12:02 +00:00
|
|
|
const auto dev_struct = device->_structure;
|
|
|
|
check(sr_session_dev_add(_structure, dev_struct));
|
|
|
|
_other_devices[dev_struct] = move(device);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
vector<shared_ptr<Device>> Session::devices()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
GSList *dev_list;
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_dev_list(_structure, &dev_list));
|
2014-07-17 17:39:49 +00:00
|
|
|
vector<shared_ptr<Device>> result;
|
2015-10-11 08:48:12 +00:00
|
|
|
for (GSList *dev = dev_list; dev; dev = dev->next) {
|
|
|
|
auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
|
2014-09-30 15:05:27 +00:00
|
|
|
result.push_back(get_device(sdi));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
2018-10-14 23:17:09 +00:00
|
|
|
g_slist_free(dev_list);
|
2014-07-17 17:39:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::remove_devices()
|
|
|
|
{
|
2014-09-30 15:05:27 +00:00
|
|
|
_other_devices.clear();
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_dev_remove_all(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::start()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_start(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::run()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_run(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::stop()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_stop(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:24:51 +00:00
|
|
|
bool Session::is_running() const
|
|
|
|
{
|
|
|
|
const int ret = sr_session_is_running(_structure);
|
|
|
|
if (ret < 0)
|
|
|
|
throw Error{ret};
|
|
|
|
return (ret != 0);
|
|
|
|
}
|
|
|
|
|
2015-10-14 18:56:51 +00:00
|
|
|
static void session_stopped_callback(void *data) noexcept
|
2015-10-09 17:24:51 +00:00
|
|
|
{
|
|
|
|
auto *const callback = static_cast<SessionStoppedCallback*>(data);
|
|
|
|
(*callback)();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::set_stopped_callback(SessionStoppedCallback callback)
|
|
|
|
{
|
|
|
|
_stopped_callback = move(callback);
|
|
|
|
if (_stopped_callback)
|
|
|
|
check(sr_session_stopped_callback_set(_structure,
|
|
|
|
&session_stopped_callback, &_stopped_callback));
|
|
|
|
else
|
|
|
|
check(sr_session_stopped_callback_set(_structure,
|
|
|
|
nullptr, nullptr));
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
static void datafeed_callback(const struct sr_dev_inst *sdi,
|
2015-10-14 18:56:51 +00:00
|
|
|
const struct sr_datafeed_packet *pkt, void *cb_data) noexcept
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
auto callback = static_cast<DatafeedCallbackData *>(cb_data);
|
|
|
|
callback->run(sdi, pkt);
|
|
|
|
}
|
2015-10-11 08:48:12 +00:00
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
|
|
|
|
{
|
2015-10-15 16:32:44 +00:00
|
|
|
unique_ptr<DatafeedCallbackData> cb_data
|
|
|
|
{new DatafeedCallbackData{this, move(callback)}};
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_datafeed_callback_add(_structure,
|
2015-10-15 16:32:44 +00:00
|
|
|
&datafeed_callback, cb_data.get()));
|
|
|
|
_datafeed_callbacks.push_back(move(cb_data));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 01:12:02 +00:00
|
|
|
void Session::remove_datafeed_callbacks()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_session_datafeed_callback_remove_all(_structure));
|
|
|
|
_datafeed_callbacks.clear();
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
shared_ptr<Trigger> Session::trigger()
|
2014-07-19 21:45:35 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _trigger;
|
2014-07-19 21:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::set_trigger(shared_ptr<Trigger> trigger)
|
|
|
|
{
|
2014-11-24 00:11:17 +00:00
|
|
|
if (!trigger)
|
|
|
|
// Set NULL trigger, i.e. remove any trigger from the session.
|
2015-10-11 01:38:19 +00:00
|
|
|
check(sr_session_trigger_set(_structure, nullptr));
|
2014-11-24 00:11:17 +00:00
|
|
|
else
|
|
|
|
check(sr_session_trigger_set(_structure, trigger->_structure));
|
2015-10-11 01:12:02 +00:00
|
|
|
_trigger = move(trigger);
|
2014-07-19 21:45:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Session::filename() const
|
2014-10-12 16:53:10 +00:00
|
|
|
{
|
|
|
|
return _filename;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:15:37 +00:00
|
|
|
shared_ptr<Context> Session::context()
|
|
|
|
{
|
|
|
|
return _context;
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:20:13 +00:00
|
|
|
Packet::Packet(shared_ptr<Device> device,
|
|
|
|
const struct sr_datafeed_packet *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2015-10-11 01:12:02 +00:00
|
|
|
_device(move(device))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
switch (structure->type)
|
|
|
|
{
|
2014-07-20 01:20:13 +00:00
|
|
|
case SR_DF_HEADER:
|
2015-10-15 16:32:44 +00:00
|
|
|
_payload.reset(new Header{
|
2014-07-20 01:20:13 +00:00
|
|
|
static_cast<const struct sr_datafeed_header *>(
|
2015-10-15 16:32:44 +00:00
|
|
|
structure->payload)});
|
2014-07-20 01:20:13 +00:00
|
|
|
break;
|
|
|
|
case SR_DF_META:
|
2015-10-15 16:32:44 +00:00
|
|
|
_payload.reset(new Meta{
|
2014-07-20 01:20:13 +00:00
|
|
|
static_cast<const struct sr_datafeed_meta *>(
|
2015-10-15 16:32:44 +00:00
|
|
|
structure->payload)});
|
2014-07-20 01:20:13 +00:00
|
|
|
break;
|
2014-07-17 17:39:49 +00:00
|
|
|
case SR_DF_LOGIC:
|
2015-10-15 16:32:44 +00:00
|
|
|
_payload.reset(new Logic{
|
2014-07-17 17:39:49 +00:00
|
|
|
static_cast<const struct sr_datafeed_logic *>(
|
2015-10-15 16:32:44 +00:00
|
|
|
structure->payload)});
|
2014-07-17 17:39:49 +00:00
|
|
|
break;
|
2015-10-02 14:18:35 +00:00
|
|
|
case SR_DF_ANALOG:
|
2015-10-15 16:32:44 +00:00
|
|
|
_payload.reset(new Analog{
|
2015-10-02 14:18:35 +00:00
|
|
|
static_cast<const struct sr_datafeed_analog *>(
|
2015-10-15 16:32:44 +00:00
|
|
|
structure->payload)});
|
2014-08-29 19:06:49 +00:00
|
|
|
break;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet::~Packet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const PacketType *Packet::type() const
|
2014-07-20 00:38:31 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return PacketType::get(_structure->type);
|
2014-07-20 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
shared_ptr<PacketPayload> Packet::payload()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
if (_payload)
|
2015-10-11 15:11:37 +00:00
|
|
|
return _payload->share_owned_by(shared_from_this());
|
2014-08-29 19:06:49 +00:00
|
|
|
else
|
|
|
|
throw Error(SR_ERR_NA);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PacketPayload::PacketPayload()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketPayload::~PacketPayload()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:20:13 +00:00
|
|
|
Header::Header(const struct sr_datafeed_header *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
PacketPayload(),
|
|
|
|
_structure(structure)
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Header::~Header()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:11:37 +00:00
|
|
|
shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent)
|
2014-08-29 19:06:49 +00:00
|
|
|
{
|
2014-09-06 11:29:36 +00:00
|
|
|
return static_pointer_cast<PacketPayload>(
|
2015-10-11 15:11:37 +00:00
|
|
|
ParentOwned::share_owned_by(_parent));
|
2014-08-29 19:06:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
int Header::feed_version() const
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->feed_version;
|
2014-07-20 01:20:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
Glib::TimeVal Header::start_time() const
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
|
|
|
return Glib::TimeVal(
|
2014-09-11 20:25:52 +00:00
|
|
|
_structure->starttime.tv_sec,
|
|
|
|
_structure->starttime.tv_usec);
|
2014-07-20 01:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Meta::Meta(const struct sr_datafeed_meta *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
PacketPayload(),
|
|
|
|
_structure(structure)
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Meta::~Meta()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:11:37 +00:00
|
|
|
shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent)
|
2014-08-29 19:06:49 +00:00
|
|
|
{
|
2014-09-06 11:29:36 +00:00
|
|
|
return static_pointer_cast<PacketPayload>(
|
2015-10-11 15:11:37 +00:00
|
|
|
ParentOwned::share_owned_by(_parent));
|
2014-08-29 19:06:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
map<const ConfigKey *, Glib::VariantBase> Meta::config() const
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
|
|
|
map<const ConfigKey *, Glib::VariantBase> result;
|
2015-10-11 08:48:12 +00:00
|
|
|
for (auto l = _structure->config; l; l = l->next) {
|
|
|
|
auto *const config = static_cast<struct sr_config *>(l->data);
|
2015-11-28 22:18:50 +00:00
|
|
|
result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true);
|
2014-07-20 01:20:13 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logic::Logic(const struct sr_datafeed_logic *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
PacketPayload(),
|
|
|
|
_structure(structure)
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
Logic::~Logic()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:11:37 +00:00
|
|
|
shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent)
|
2014-08-29 19:06:49 +00:00
|
|
|
{
|
2014-09-06 11:29:36 +00:00
|
|
|
return static_pointer_cast<PacketPayload>(
|
2015-10-11 15:11:37 +00:00
|
|
|
ParentOwned::share_owned_by(_parent));
|
2014-08-29 19:06:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
void *Logic::data_pointer()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->data;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
size_t Logic::data_length() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->length;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
unsigned int Logic::unit_size() const
|
2014-07-20 01:20:13 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->unitsize;
|
2014-07-20 01:20:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 14:18:35 +00:00
|
|
|
Analog::Analog(const struct sr_datafeed_analog *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
PacketPayload(),
|
|
|
|
_structure(structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-02 14:18:35 +00:00
|
|
|
Analog::~Analog()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:11:37 +00:00
|
|
|
shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent)
|
2014-08-29 19:06:49 +00:00
|
|
|
{
|
2014-09-06 11:29:36 +00:00
|
|
|
return static_pointer_cast<PacketPayload>(
|
2015-10-11 15:11:37 +00:00
|
|
|
ParentOwned::share_owned_by(_parent));
|
2014-08-29 19:06:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 14:18:35 +00:00
|
|
|
void *Analog::data_pointer()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->data;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 20:37:09 +00:00
|
|
|
void Analog::get_data_as_float(float *dest)
|
|
|
|
{
|
|
|
|
check(sr_analog_to_float(_structure, dest));
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
unsigned int Analog::num_samples() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return _structure->num_samples;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 14:18:35 +00:00
|
|
|
vector<shared_ptr<Channel>> Analog::channels()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-07-20 01:20:13 +00:00
|
|
|
vector<shared_ptr<Channel>> result;
|
2015-10-11 08:48:12 +00:00
|
|
|
for (auto l = _structure->meaning->channels; l; l = l->next) {
|
|
|
|
auto *const ch = static_cast<struct sr_channel *>(l->data);
|
|
|
|
result.push_back(_parent->_device->get_channel(ch));
|
|
|
|
}
|
2014-07-20 01:20:13 +00:00
|
|
|
return result;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 14:54:53 +00:00
|
|
|
unsigned int Analog::unitsize() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->unitsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Analog::is_signed() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->is_signed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Analog::is_float() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->is_float;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Analog::is_bigendian() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->is_bigendian;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Analog::digits() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->digits;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Analog::is_digits_decimal() const
|
|
|
|
{
|
|
|
|
return _structure->encoding->is_digits_decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Rational> Analog::scale()
|
|
|
|
{
|
|
|
|
unique_ptr<Rational> scale;
|
|
|
|
scale.reset(new Rational(&(_structure->encoding->scale)));
|
|
|
|
|
|
|
|
if (scale)
|
|
|
|
return scale->share_owned_by(shared_from_this());
|
|
|
|
else
|
|
|
|
throw Error(SR_ERR_NA);
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Rational> Analog::offset()
|
|
|
|
{
|
|
|
|
unique_ptr<Rational> offset;
|
|
|
|
offset.reset(new Rational(&(_structure->encoding->offset)));
|
|
|
|
|
|
|
|
if (offset)
|
|
|
|
return offset->share_owned_by(shared_from_this());
|
|
|
|
else
|
|
|
|
throw Error(SR_ERR_NA);
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const Quantity *Analog::mq() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-02 14:18:35 +00:00
|
|
|
return Quantity::get(_structure->meaning->mq);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
const Unit *Analog::unit() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-02 14:18:35 +00:00
|
|
|
return Unit::get(_structure->meaning->unit);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
vector<const QuantityFlag *> Analog::mq_flags() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-02 14:18:35 +00:00
|
|
|
return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 22:55:03 +00:00
|
|
|
shared_ptr<Logic> Analog::get_logic_via_threshold(float threshold,
|
|
|
|
uint8_t *data_ptr) const
|
|
|
|
{
|
|
|
|
auto datafeed = g_new(struct sr_datafeed_logic, 1);
|
|
|
|
datafeed->length = num_samples();
|
|
|
|
datafeed->unitsize = 1;
|
|
|
|
|
|
|
|
if (data_ptr)
|
|
|
|
datafeed->data = data_ptr;
|
|
|
|
else
|
|
|
|
datafeed->data = g_malloc(datafeed->length);
|
|
|
|
|
|
|
|
shared_ptr<Logic> logic =
|
|
|
|
shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
|
|
|
|
|
|
|
|
check(sr_a2l_threshold(_structure, threshold,
|
|
|
|
(uint8_t*)datafeed->data, datafeed->length));
|
|
|
|
|
|
|
|
return logic;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Logic> Analog::get_logic_via_schmitt_trigger(float lo_thr,
|
|
|
|
float hi_thr, uint8_t *state, uint8_t *data_ptr) const
|
|
|
|
{
|
|
|
|
auto datafeed = g_new(struct sr_datafeed_logic, 1);
|
|
|
|
datafeed->length = num_samples();
|
|
|
|
datafeed->unitsize = 1;
|
|
|
|
|
|
|
|
if (data_ptr)
|
|
|
|
datafeed->data = data_ptr;
|
|
|
|
else
|
|
|
|
datafeed->data = g_malloc(datafeed->length);
|
|
|
|
|
|
|
|
shared_ptr<Logic> logic =
|
|
|
|
shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
|
|
|
|
|
|
|
|
check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state,
|
|
|
|
(uint8_t*)datafeed->data, datafeed->length));
|
|
|
|
|
|
|
|
return logic;
|
|
|
|
}
|
|
|
|
|
2017-05-27 14:54:53 +00:00
|
|
|
Rational::Rational(const struct sr_rational *structure) :
|
|
|
|
_structure(structure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Rational::~Rational()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent)
|
|
|
|
{
|
|
|
|
return static_pointer_cast<Rational>(
|
|
|
|
ParentOwned::share_owned_by(_parent));
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t Rational::numerator() const
|
|
|
|
{
|
|
|
|
return _structure->p;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Rational::denominator() const
|
|
|
|
{
|
|
|
|
return _structure->q;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Rational::value() const
|
|
|
|
{
|
|
|
|
return (float)(_structure->p) / (float)(_structure->q);
|
|
|
|
}
|
|
|
|
|
2014-08-24 00:40:19 +00:00
|
|
|
InputFormat::InputFormat(const struct sr_input_module *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputFormat::~InputFormat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string InputFormat::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(sr_input_id_get(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string InputFormat::description() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(sr_input_description_get(_structure));
|
2014-08-24 00:40:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
vector<string> InputFormat::extensions() const
|
2015-01-21 05:56:51 +00:00
|
|
|
{
|
|
|
|
vector<string> exts;
|
|
|
|
for (const char *const *e = sr_input_extensions_get(_structure);
|
|
|
|
e && *e; e++)
|
|
|
|
exts.push_back(*e);
|
|
|
|
return exts;
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
map<string, shared_ptr<Option>> InputFormat::options()
|
2014-08-24 13:22:03 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<Option>> result;
|
2015-10-15 17:21:28 +00:00
|
|
|
|
2017-06-11 07:51:59 +00:00
|
|
|
if (const struct sr_option **options = sr_input_options_get(_structure)) {
|
2015-10-15 17:21:28 +00:00
|
|
|
shared_ptr<const struct sr_option *> option_array
|
|
|
|
{options, &sr_input_options_free};
|
|
|
|
for (int i = 0; options[i]; i++) {
|
|
|
|
shared_ptr<Option> opt {
|
|
|
|
new Option{options[i], option_array},
|
|
|
|
default_delete<Option>{}};
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(opt->id(), move(opt));
|
2015-10-15 17:21:28 +00:00
|
|
|
}
|
2015-01-18 22:43:02 +00:00
|
|
|
}
|
2014-08-24 13:22:03 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-24 00:40:19 +00:00
|
|
|
shared_ptr<Input> InputFormat::create_input(
|
2015-10-26 06:04:10 +00:00
|
|
|
map<string, Glib::VariantBase> options)
|
2014-08-24 00:40:19 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
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);
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}};
|
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) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2015-10-15 16:32:44 +00:00
|
|
|
_context(move(context))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
shared_ptr<InputDevice> Input::device()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2017-06-11 07:51:59 +00:00
|
|
|
if (!_device) {
|
2014-09-11 20:25:52 +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);
|
2015-10-15 16:32:44 +00:00
|
|
|
_device.reset(new InputDevice{shared_from_this(), sdi});
|
2014-08-24 00:40:19 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-10-11 15:11:37 +00:00
|
|
|
return _device->share_owned_by(shared_from_this());
|
2014-08-24 00:40:19 +00:00
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
|
2015-04-20 09:02:34 +00:00
|
|
|
void Input::send(void *data, size_t length)
|
2014-08-24 00:40:19 +00:00
|
|
|
{
|
2015-10-11 08:48:12 +00:00
|
|
|
auto gstr = g_string_new_len(static_cast<char *>(data), length);
|
2014-09-11 20:25:52 +00:00
|
|
|
auto ret = sr_input_send(_structure, gstr);
|
2017-06-09 21:13:35 +00:00
|
|
|
g_string_free(gstr, true);
|
2014-08-24 00:40:19 +00:00
|
|
|
check(ret);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 15:29:34 +00:00
|
|
|
void Input::end()
|
|
|
|
{
|
|
|
|
check(sr_input_end(_structure));
|
|
|
|
}
|
|
|
|
|
2016-04-28 06:13:17 +00:00
|
|
|
void Input::reset()
|
|
|
|
{
|
|
|
|
check(sr_input_reset(_structure));
|
|
|
|
}
|
|
|
|
|
2014-08-24 00:40:19 +00:00
|
|
|
Input::~Input()
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-27 15:29:34 +00:00
|
|
|
sr_input_free(_structure);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-24 01:00:14 +00:00
|
|
|
InputDevice::InputDevice(shared_ptr<Input> input,
|
|
|
|
struct sr_dev_inst *structure) :
|
|
|
|
Device(structure),
|
2015-10-11 01:12:02 +00:00
|
|
|
_input(move(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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:51 +00:00
|
|
|
shared_ptr<Device> InputDevice::get_shared_from_this()
|
|
|
|
{
|
2014-09-02 19:30:50 +00:00
|
|
|
return static_pointer_cast<Device>(shared_from_this());
|
2014-08-29 13:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 15:03:25 +00:00
|
|
|
Option::Option(const struct sr_option *structure,
|
2014-08-17 10:44:05 +00:00
|
|
|
shared_ptr<const struct sr_option *> structure_array) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure),
|
2015-10-11 01:12:02 +00:00
|
|
|
_structure_array(move(structure_array))
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Option::~Option()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Option::id() const
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->id);
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Option::name() const
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->name);
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string Option::description() const
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(_structure->desc);
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
Glib::VariantBase Option::default_value() const
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return Glib::VariantBase(_structure->def, true);
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
vector<Glib::VariantBase> Option::values() const
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
|
|
|
vector<Glib::VariantBase> result;
|
2015-10-11 08:48:12 +00:00
|
|
|
for (auto l = _structure->values; l; l = l->next) {
|
|
|
|
auto *const var = static_cast<GVariant *>(l->data);
|
|
|
|
result.push_back(Glib::VariantBase(var, true));
|
|
|
|
}
|
2014-07-26 15:03:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-11 07:54:52 +00:00
|
|
|
Glib::VariantBase Option::parse_string(string value)
|
|
|
|
{
|
|
|
|
enum sr_datatype dt;
|
|
|
|
Glib::VariantBase dflt = default_value();
|
|
|
|
GVariant *tmpl = dflt.gobj();
|
|
|
|
|
|
|
|
if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) {
|
|
|
|
dt = SR_T_UINT64;
|
|
|
|
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) {
|
|
|
|
dt = SR_T_STRING;
|
|
|
|
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) {
|
|
|
|
dt = SR_T_BOOL;
|
|
|
|
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) {
|
|
|
|
dt = SR_T_FLOAT;
|
|
|
|
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) {
|
|
|
|
dt = SR_T_INT32;
|
|
|
|
} else {
|
|
|
|
throw Error(SR_ERR_BUG);
|
|
|
|
}
|
|
|
|
return ConfigKey::parse_string(value, dt);
|
|
|
|
}
|
|
|
|
|
2014-07-26 15:03:25 +00:00
|
|
|
OutputFormat::OutputFormat(const struct sr_output_module *structure) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(structure)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputFormat::~OutputFormat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string OutputFormat::name() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(sr_output_id_get(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
string OutputFormat::description() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
return valid_string(sr_output_description_get(_structure));
|
2014-07-26 15:03:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
vector<string> OutputFormat::extensions() const
|
2015-01-21 06:02:14 +00:00
|
|
|
{
|
|
|
|
vector<string> exts;
|
|
|
|
for (const char *const *e = sr_output_extensions_get(_structure);
|
|
|
|
e && *e; e++)
|
|
|
|
exts.push_back(*e);
|
|
|
|
return exts;
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
map<string, shared_ptr<Option>> OutputFormat::options()
|
2014-07-26 15:03:25 +00:00
|
|
|
{
|
|
|
|
map<string, shared_ptr<Option>> result;
|
2015-10-15 17:21:28 +00:00
|
|
|
|
2017-06-11 07:51:59 +00:00
|
|
|
if (const struct sr_option **options = sr_output_options_get(_structure)) {
|
2015-10-15 17:21:28 +00:00
|
|
|
shared_ptr<const struct sr_option *> option_array
|
|
|
|
{options, &sr_output_options_free};
|
|
|
|
for (int i = 0; options[i]; i++) {
|
|
|
|
shared_ptr<Option> opt {
|
|
|
|
new Option{options[i], option_array},
|
|
|
|
default_delete<Option>{}};
|
2017-06-28 06:54:57 +00:00
|
|
|
result.emplace(opt->id(), move(opt));
|
2015-10-15 17:21:28 +00:00
|
|
|
}
|
2015-01-18 22:43:02 +00:00
|
|
|
}
|
2014-07-26 15:03:25 +00:00
|
|
|
return result;
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Output> OutputFormat::create_output(
|
2015-10-26 06:04:10 +00:00
|
|
|
shared_ptr<Device> device, map<string, Glib::VariantBase> options)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Output>{
|
2015-10-26 06:04:10 +00:00
|
|
|
new Output{shared_from_this(), move(device), move(options)},
|
2015-10-15 17:21:28 +00:00
|
|
|
default_delete<Output>{}};
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:19:49 +00:00
|
|
|
shared_ptr<Output> OutputFormat::create_output(string filename,
|
2015-10-26 06:04:10 +00:00
|
|
|
shared_ptr<Device> device, map<string, Glib::VariantBase> options)
|
2015-07-29 19:19:49 +00:00
|
|
|
{
|
2015-10-15 17:21:28 +00:00
|
|
|
return shared_ptr<Output>{
|
2015-10-26 06:04:10 +00:00
|
|
|
new Output{move(filename), shared_from_this(), move(device), move(options)},
|
2015-10-15 17:21:28 +00:00
|
|
|
default_delete<Output>{}};
|
2015-07-29 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:57:42 +00:00
|
|
|
bool OutputFormat::test_flag(const OutputFlag *flag) const
|
2015-08-15 21:40:29 +00:00
|
|
|
{
|
|
|
|
return sr_output_test_flag(_structure, flag->id());
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
Output::Output(shared_ptr<OutputFormat> format,
|
2015-10-26 06:04:10 +00:00
|
|
|
shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(sr_output_new(format->_structure,
|
2015-10-11 01:38:19 +00:00
|
|
|
map_to_hash_variant(options), device->_structure, nullptr)),
|
2015-10-11 01:12:02 +00:00
|
|
|
_format(move(format)),
|
|
|
|
_device(move(device)),
|
2015-10-26 06:04:10 +00:00
|
|
|
_options(move(options))
|
2015-07-29 19:19:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Output::Output(string filename, shared_ptr<OutputFormat> format,
|
2015-10-26 06:04:10 +00:00
|
|
|
shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
|
2015-10-11 15:57:30 +00:00
|
|
|
_structure(sr_output_new(format->_structure,
|
2015-07-29 19:19:49 +00:00
|
|
|
map_to_hash_variant(options), device->_structure, filename.c_str())),
|
2015-10-11 01:12:02 +00:00
|
|
|
_format(move(format)),
|
|
|
|
_device(move(device)),
|
2015-10-26 06:04:10 +00:00
|
|
|
_options(move(options))
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Output::~Output()
|
|
|
|
{
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_output_free(_structure));
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 03:01:21 +00:00
|
|
|
shared_ptr<OutputFormat> Output::format()
|
|
|
|
{
|
|
|
|
return _format;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:39:49 +00:00
|
|
|
string Output::receive(shared_ptr<Packet> packet)
|
|
|
|
{
|
|
|
|
GString *out;
|
2014-09-11 20:25:52 +00:00
|
|
|
check(sr_output_send(_structure, packet->_structure, &out));
|
2017-06-11 07:51:59 +00:00
|
|
|
if (out) {
|
2014-07-17 17:39:49 +00:00
|
|
|
auto result = string(out->str, out->str + out->len);
|
|
|
|
g_string_free(out, true);
|
|
|
|
return result;
|
2017-06-11 07:51:59 +00:00
|
|
|
} else {
|
2014-07-17 17:39:49 +00:00
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 15:54:14 +00:00
|
|
|
#include <enums.cpp>
|
2014-07-17 17:39:49 +00:00
|
|
|
|
|
|
|
}
|