]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/classes.cpp
All Doxyfile files: Update file template to doxygen 1.8.16.
[libsigrok.git] / bindings / cxx / classes.cpp
CommitLineData
c23c8659
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
000f504f 20/* Needed for isascii(), as used in the GNU libstdc++ headers */
79034d4f 21/* Needed in strutil.c for POSIX.1-2008 locale functions */
000f504f 22#ifndef _XOPEN_SOURCE
79034d4f 23#define _XOPEN_SOURCE 700
000f504f
DE
24#endif
25
6ec6c43b 26#include <config.h>
b5f07319 27#include <libsigrokcxx/libsigrokcxx.hpp>
c23c8659 28
f36ca889 29#include <sstream>
35114c33 30#include <cmath>
f36ca889 31
c23c8659
ML
32namespace sigrok
33{
34
6c6dd732
DA
35using namespace std;
36
c23c8659
ML
37/** Helper function to translate C errors to C++ exceptions. */
38static void check(int result)
39{
40 if (result != SR_OK)
41 throw Error(result);
42}
43
44/** Helper function to obtain valid strings from possibly null input. */
58e21229 45static inline const char *valid_string(const char *input)
c23c8659 46{
58e21229 47 return (input) ? input : "";
c23c8659
ML
48}
49
58aa1f83 50/** Helper function to convert between map<string, VariantBase> and GHashTable */
d370545d 51static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input)
58aa1f83 52{
f17b4546 53 auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
ce3e1e61 54 reinterpret_cast<GDestroyNotify>(&g_variant_unref));
f17b4546 55 for (const auto &entry : input)
58aa1f83
ML
56 g_hash_table_insert(output,
57 g_strdup(entry.first.c_str()),
58 entry.second.gobj_copy());
d370545d 59 return output;
58aa1f83
ML
60}
61
c23c8659
ML
62Error::Error(int result) : result(result)
63{
64}
65
15914cdb 66const char *Error::what() const noexcept
c23c8659
ML
67{
68 return sr_strerror(result);
69}
70
15914cdb 71Error::~Error() noexcept
c23c8659
ML
72{
73}
74
e2eaf858
DE
75ResourceReader::~ResourceReader()
76{
77}
78
79SR_PRIV int ResourceReader::open_callback(struct sr_resource *res,
0ab8e5d2 80 const char *name, void *cb_data) noexcept
e2eaf858
DE
81{
82 try {
83 auto *const reader = static_cast<ResourceReader*>(cb_data);
84 reader->open(res, name);
85 } catch (const Error &err) {
86 return err.result;
87 } catch (...) {
88 return SR_ERR;
89 }
90 return SR_OK;
91}
92
0ab8e5d2
DE
93SR_PRIV int ResourceReader::close_callback(struct sr_resource *res,
94 void *cb_data) noexcept
e2eaf858
DE
95{
96 try {
97 auto *const reader = static_cast<ResourceReader*>(cb_data);
98 reader->close(res);
99 } catch (const Error &err) {
100 return err.result;
101 } catch (...) {
102 return SR_ERR;
103 }
104 return SR_OK;
105}
106
32ba0d80 107SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res,
0ab8e5d2 108 void *buf, size_t count, void *cb_data) noexcept
e2eaf858
DE
109{
110 try {
111 auto *const reader = static_cast<ResourceReader*>(cb_data);
112 return reader->read(res, buf, count);
113 } catch (const Error &err) {
114 return err.result;
115 } catch (...) {
116 return SR_ERR;
117 }
118}
119
c23c8659
ML
120shared_ptr<Context> Context::create()
121{
a98729a7 122 return shared_ptr<Context>{new Context{}, default_delete<Context>{}};
c23c8659
ML
123}
124
125Context::Context() :
d5d7b09e 126 _structure(nullptr),
58e21229 127 _session(nullptr)
c23c8659 128{
3b161085 129 check(sr_init(&_structure));
90e89c2a 130
f17b4546
DE
131 if (struct sr_dev_driver **driver_list = sr_driver_list(_structure))
132 for (int i = 0; driver_list[i]; i++) {
133 unique_ptr<Driver> driver {new Driver{driver_list[i]}};
b605edf3 134 _drivers.emplace(driver->name(), move(driver));
f17b4546
DE
135 }
136
137 if (const struct sr_input_module **input_list = sr_input_list())
138 for (int i = 0; input_list[i]; i++) {
139 unique_ptr<InputFormat> input {new InputFormat{input_list[i]}};
b605edf3 140 _input_formats.emplace(input->name(), move(input));
f17b4546
DE
141 }
142
143 if (const struct sr_output_module **output_list = sr_output_list())
144 for (int i = 0; output_list[i]; i++) {
145 unique_ptr<OutputFormat> output {new OutputFormat{output_list[i]}};
b605edf3 146 _output_formats.emplace(output->name(), move(output));
f17b4546 147 }
c23c8659
ML
148}
149
3b161085 150string Context::package_version()
c23c8659
ML
151{
152 return sr_package_version_string_get();
153}
154
3b161085 155string Context::lib_version()
c23c8659
ML
156{
157 return sr_lib_version_string_get();
158}
159
3b161085 160map<string, shared_ptr<Driver>> Context::drivers()
c23c8659
ML
161{
162 map<string, shared_ptr<Driver>> result;
e7eb2968 163 for (const auto &entry: _drivers) {
f17b4546
DE
164 const auto &name = entry.first;
165 const auto &driver = entry.second;
b605edf3 166 result.emplace(name, driver->share_owned_by(shared_from_this()));
c23c8659
ML
167 }
168 return result;
169}
170
3b161085 171map<string, shared_ptr<InputFormat>> Context::input_formats()
c23c8659
ML
172{
173 map<string, shared_ptr<InputFormat>> result;
e7eb2968 174 for (const auto &entry: _input_formats) {
f17b4546
DE
175 const auto &name = entry.first;
176 const auto &input_format = entry.second;
b605edf3 177 result.emplace(name, input_format->share_owned_by(shared_from_this()));
c23c8659
ML
178 }
179 return result;
180}
181
092843eb
GS
182shared_ptr<InputFormat> Context::input_format_match(string filename)
183{
184 const struct sr_input *input;
185 const struct sr_input_module *imod;
186 int rc;
187
188 /*
189 * Have the input module looked up for the specified file.
190 * Failed lookup (or "successful lookup" with an empty result)
191 * are non-fatal. Free the sr_input that was created by the
192 * lookup routine, but grab the input module kind and return an
193 * InputFormat instance to the application. This works because
194 * the application passes a filename, no input data got buffered
195 * in the sr_input that we release.
196 */
197 input = NULL;
198 rc = sr_input_scan_file(filename.c_str(), &input);
199 if (rc != SR_OK)
200 return nullptr;
201 if (!input)
202 return nullptr;
203 imod = sr_input_module_get(input);
204 sr_input_free(input);
205 return shared_ptr<InputFormat>{new InputFormat{imod}, default_delete<InputFormat>{}};
206}
207
3b161085 208map<string, shared_ptr<OutputFormat>> Context::output_formats()
c23c8659
ML
209{
210 map<string, shared_ptr<OutputFormat>> result;
e7eb2968 211 for (const auto &entry: _output_formats) {
f17b4546
DE
212 const auto &name = entry.first;
213 const auto &output_format = entry.second;
b605edf3 214 result.emplace(name, output_format->share_owned_by(shared_from_this()));
c23c8659
ML
215 }
216 return result;
217}
218
219Context::~Context()
220{
3b161085 221 check(sr_exit(_structure));
c23c8659
ML
222}
223
15bebf57 224const LogLevel *Context::log_level() const
c23c8659
ML
225{
226 return LogLevel::get(sr_log_loglevel_get());
227}
228
229void Context::set_log_level(const LogLevel *level)
230{
3b161085 231 check(sr_log_loglevel_set(level->id()));
c23c8659
ML
232}
233
0ab8e5d2
DE
234static int call_log_callback(void *cb_data, int loglevel,
235 const char *format, va_list args) noexcept
c23c8659 236{
58e21229
DE
237 const unique_ptr<char, decltype(&g_free)>
238 message {g_strdup_vprintf(format, args), &g_free};
c23c8659 239
ce3e1e61 240 auto *const callback = static_cast<LogCallbackFunction *>(cb_data);
c23c8659 241
e7eb2968 242 try {
ce3e1e61 243 (*callback)(LogLevel::get(loglevel), message.get());
0875f11d 244 } catch (Error &e) {
c23c8659
ML
245 return e.result;
246 }
247
248 return SR_OK;
249}
250
251void Context::set_log_callback(LogCallbackFunction callback)
252{
d370545d 253 _log_callback = move(callback);
3b161085 254 check(sr_log_callback_set(call_log_callback, &_log_callback));
e2eaf858 255}
c23c8659
ML
256
257void Context::set_log_callback_default()
258{
259 check(sr_log_callback_set_default());
3b161085 260 _log_callback = nullptr;
e2eaf858
DE
261}
262
263void Context::set_resource_reader(ResourceReader *reader)
264{
265 if (reader) {
266 check(sr_resource_set_hooks(_structure,
267 &ResourceReader::open_callback,
268 &ResourceReader::close_callback,
269 &ResourceReader::read_callback, reader));
270 } else {
271 check(sr_resource_set_hooks(_structure,
272 nullptr, nullptr, nullptr, nullptr));
273 }
274}
c23c8659
ML
275
276shared_ptr<Session> Context::create_session()
277{
a98729a7
DE
278 return shared_ptr<Session>{new Session{shared_from_this()},
279 default_delete<Session>{}};
c23c8659
ML
280}
281
9fa5b426
ML
282shared_ptr<UserDevice> Context::create_user_device(
283 string vendor, string model, string version)
284{
a98729a7
DE
285 return shared_ptr<UserDevice>{
286 new UserDevice{move(vendor), move(model), move(version)},
287 default_delete<UserDevice>{}};
9fa5b426
ML
288}
289
304be4a7
ML
290shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time)
291{
292 auto header = g_new(struct sr_datafeed_header, 1);
293 header->feed_version = 1;
294 header->starttime.tv_sec = start_time.tv_sec;
295 header->starttime.tv_usec = start_time.tv_usec;
296 auto packet = g_new(struct sr_datafeed_packet, 1);
297 packet->type = SR_DF_HEADER;
298 packet->payload = header;
a98729a7
DE
299 return shared_ptr<Packet>{new Packet{nullptr, packet},
300 default_delete<Packet>{}};
304be4a7
ML
301}
302
303shared_ptr<Packet> Context::create_meta_packet(
9e7176bd 304 map<const ConfigKey *, Glib::VariantBase> config)
304be4a7
ML
305{
306 auto meta = g_new0(struct sr_datafeed_meta, 1);
e7eb2968 307 for (const auto &input : config) {
f17b4546
DE
308 const auto &key = input.first;
309 const auto &value = input.second;
310 auto *const output = g_new(struct sr_config, 1);
304be4a7 311 output->key = key->id();
f17b4546 312 output->data = value.gobj_copy();
304be4a7
ML
313 meta->config = g_slist_append(meta->config, output);
314 }
315 auto packet = g_new(struct sr_datafeed_packet, 1);
316 packet->type = SR_DF_META;
317 packet->payload = meta;
a98729a7
DE
318 return shared_ptr<Packet>{new Packet{nullptr, packet},
319 default_delete<Packet>{}};
304be4a7
ML
320}
321
322shared_ptr<Packet> Context::create_logic_packet(
323 void *data_pointer, size_t data_length, unsigned int unit_size)
324{
325 auto logic = g_new(struct sr_datafeed_logic, 1);
326 logic->length = data_length;
327 logic->unitsize = unit_size;
328 logic->data = data_pointer;
329 auto packet = g_new(struct sr_datafeed_packet, 1);
330 packet->type = SR_DF_LOGIC;
331 packet->payload = logic;
a98729a7 332 return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
304be4a7
ML
333}
334
dd13d47a 335shared_ptr<Packet> Context::create_analog_packet(
9e7176bd 336 vector<shared_ptr<Channel> > channels,
895cbcdd 337 const float *data_pointer, unsigned int num_samples, const Quantity *mq,
9e7176bd 338 const Unit *unit, vector<const QuantityFlag *> mqflags)
304be4a7 339{
dd13d47a
UH
340 auto analog = g_new0(struct sr_datafeed_analog, 1);
341 auto meaning = g_new0(struct sr_analog_meaning, 1);
d9d57ceb
SA
342 auto encoding = g_new0(struct sr_analog_encoding, 1);
343 auto spec = g_new0(struct sr_analog_spec, 1);
dd13d47a
UH
344
345 analog->meaning = meaning;
346
f17b4546 347 for (const auto &channel : channels)
dd13d47a 348 meaning->channels = g_slist_append(meaning->channels, channel->_structure);
ce3e1e61
DE
349 meaning->mq = static_cast<sr_mq>(mq->id());
350 meaning->unit = static_cast<sr_unit>(unit->id());
9e7176bd 351 meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(move(mqflags)));
d9d57ceb
SA
352
353 analog->encoding = encoding;
354
355 encoding->unitsize = sizeof(float);
356 encoding->is_signed = TRUE;
357 encoding->is_float = TRUE;
358#ifdef WORDS_BIGENDIAN
359 encoding->is_bigendian = TRUE;
360#else
361 encoding->is_bigendian = FALSE;
362#endif
363 encoding->digits = 0;
364 encoding->is_digits_decimal = FALSE;
365 encoding->scale.p = 1;
366 encoding->scale.q = 1;
367 encoding->offset.p = 0;
368 encoding->offset.q = 1;
369
370 analog->spec = spec;
371
372 spec->spec_digits = 0;
373
374 analog->num_samples = num_samples;
895cbcdd 375 analog->data = (float*)data_pointer;
304be4a7 376 auto packet = g_new(struct sr_datafeed_packet, 1);
dd13d47a 377 packet->type = SR_DF_ANALOG;
304be4a7 378 packet->payload = analog;
a98729a7 379 return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
304be4a7
ML
380}
381
a9ed2eb0
ML
382shared_ptr<Packet> Context::create_end_packet()
383{
384 auto packet = g_new(struct sr_datafeed_packet, 1);
385 packet->type = SR_DF_END;
386 return shared_ptr<Packet>{new Packet{nullptr, packet},
387 default_delete<Packet>{}};
388}
389
c23c8659
ML
390shared_ptr<Session> Context::load_session(string filename)
391{
a98729a7
DE
392 return shared_ptr<Session>{
393 new Session{shared_from_this(), move(filename)},
394 default_delete<Session>{}};
c23c8659
ML
395}
396
397shared_ptr<Trigger> Context::create_trigger(string name)
398{
a98729a7
DE
399 return shared_ptr<Trigger>{
400 new Trigger{shared_from_this(), move(name)},
401 default_delete<Trigger>{}};
c23c8659
ML
402}
403
ca3291e3
ML
404shared_ptr<Input> Context::open_file(string filename)
405{
f88c7373
BV
406 const struct sr_input *input;
407
d4cf45e5 408 check(sr_input_scan_file(filename.c_str(), &input));
a98729a7
DE
409 return shared_ptr<Input>{
410 new Input{shared_from_this(), input},
411 default_delete<Input>{}};
ca3291e3
ML
412}
413
414shared_ptr<Input> Context::open_stream(string header)
415{
f88c7373
BV
416 const struct sr_input *input;
417
ca3291e3 418 auto gstr = g_string_new(header.c_str());
f88c7373
BV
419 auto ret = sr_input_scan_buffer(gstr, &input);
420 g_string_free(gstr, true);
421 check(ret);
a98729a7
DE
422 return shared_ptr<Input>{
423 new Input{shared_from_this(), input},
424 default_delete<Input>{}};
ca3291e3
ML
425}
426
15bebf57 427map<string, string> Context::serials(shared_ptr<Driver> driver) const
24287ea9 428{
58e21229 429 GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr);
24287ea9
AJ
430 map<string, string> serials;
431
432 for (GSList *serial = serial_list; serial; serial = serial->next) {
ce3e1e61 433 auto *const port = static_cast<sr_serial_port *>(serial->data);
24287ea9
AJ
434 serials[string(port->name)] = string(port->description);
435 }
436
ce3e1e61
DE
437 g_slist_free_full(serial_list,
438 reinterpret_cast<GDestroyNotify>(&sr_serial_free));
24287ea9
AJ
439 return serials;
440}
441
c23c8659 442Driver::Driver(struct sr_dev_driver *structure) :
58e21229 443 Configurable(structure, nullptr, nullptr),
d5d7b09e 444 _structure(structure),
3b161085 445 _initialized(false)
c23c8659
ML
446{
447}
448
449Driver::~Driver()
450{
c23c8659
ML
451}
452
15bebf57 453string Driver::name() const
c23c8659 454{
3b161085 455 return valid_string(_structure->name);
c23c8659
ML
456}
457
15bebf57 458string Driver::long_name() const
c23c8659 459{
3b161085 460 return valid_string(_structure->longname);
c23c8659
ML
461}
462
36bb818d
ML
463set<const ConfigKey *> Driver::scan_options() const
464{
8141a032 465 GArray *opts = sr_driver_scan_options_list(_structure);
36bb818d 466 set<const ConfigKey *> result;
116670b1
E
467 if (opts) {
468 for (guint i = 0; i < opts->len; i++)
469 result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
470 g_array_free(opts, TRUE);
471 }
36bb818d
ML
472 return result;
473}
474
c23c8659 475vector<shared_ptr<HardwareDevice>> Driver::scan(
9e7176bd 476 map<const ConfigKey *, Glib::VariantBase> options)
c23c8659
ML
477{
478 /* Initialise the driver if not yet done. */
e7eb2968 479 if (!_initialized) {
3b161085
ML
480 check(sr_driver_init(_parent->_structure, _structure));
481 _initialized = true;
c23c8659
ML
482 }
483
c23c8659 484 /* Translate scan options to GSList of struct sr_config pointers. */
58e21229 485 GSList *option_list = nullptr;
e7eb2968 486 for (const auto &entry : options) {
f17b4546
DE
487 const auto &key = entry.first;
488 const auto &value = entry.second;
489 auto *const config = g_new(struct sr_config, 1);
3b161085 490 config->key = key->id();
f17b4546 491 config->data = const_cast<GVariant*>(value.gobj());
c23c8659
ML
492 option_list = g_slist_append(option_list, config);
493 }
494
495 /* Run scan. */
3b161085 496 GSList *device_list = sr_driver_scan(_structure, option_list);
c23c8659
ML
497
498 /* Free option list. */
499 g_slist_free_full(option_list, g_free);
500
a4e47454 501
c23c8659 502 /* Create device objects. */
a4e47454 503 vector<shared_ptr<HardwareDevice>> result;
e7eb2968 504 for (GSList *device = device_list; device; device = device->next) {
ce3e1e61 505 auto *const sdi = static_cast<struct sr_dev_inst *>(device->data);
a98729a7
DE
506 shared_ptr<HardwareDevice> hwdev {
507 new HardwareDevice{shared_from_this(), sdi},
508 default_delete<HardwareDevice>{}};
509 result.push_back(move(hwdev));
c23c8659
ML
510 }
511
512 /* Free GSList returned from scan. */
513 g_slist_free(device_list);
514
c23c8659
ML
515 return result;
516}
517
518Configurable::Configurable(
519 struct sr_dev_driver *driver,
520 struct sr_dev_inst *sdi,
521 struct sr_channel_group *cg) :
522 config_driver(driver),
523 config_sdi(sdi),
524 config_channel_group(cg)
525{
526}
527
528Configurable::~Configurable()
529{
530}
531
36bb818d
ML
532set<const ConfigKey *> Configurable::config_keys() const
533{
534 GArray *opts;
535 set<const ConfigKey *> result;
536
537 opts = sr_dev_options(config_driver, config_sdi, config_channel_group);
538
0221cbdd
SA
539 if (opts) {
540 for (guint i = 0; i < opts->len; i++)
541 result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i)));
542 g_array_free(opts, TRUE);
543 }
36bb818d
ML
544
545 return result;
546}
547
15bebf57 548Glib::VariantBase Configurable::config_get(const ConfigKey *key) const
c23c8659
ML
549{
550 GVariant *data;
551 check(sr_config_get(
552 config_driver, config_sdi, config_channel_group,
3b161085 553 key->id(), &data));
c23c8659
ML
554 return Glib::VariantBase(data);
555}
556
d370545d 557void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value)
c23c8659
ML
558{
559 check(sr_config_set(
560 config_sdi, config_channel_group,
d370545d 561 key->id(), const_cast<GVariant*>(value.gobj())));
c23c8659
ML
562}
563
36bb818d 564set<const Capability *> Configurable::config_capabilities(const ConfigKey *key) const
c23c8659 565{
0c697a4b
UH
566 int caps = sr_dev_config_capabilities_list(config_sdi,
567 config_channel_group, key->id());
c23c8659 568
36bb818d 569 set<const Capability *> result;
d54190a3 570
36bb818d
ML
571 for (auto cap: Capability::values())
572 if (caps & cap->id())
573 result.insert(cap);
d54190a3
ML
574
575 return result;
576}
577
d9eed47d 578bool Configurable::config_check(const ConfigKey *key,
36bb818d 579 const Capability *capability) const
d9eed47d 580{
0c697a4b
UH
581 int caps = sr_dev_config_capabilities_list(config_sdi,
582 config_channel_group, key->id());
d9eed47d 583
36bb818d
ML
584 return (caps & capability->id());
585}
d9eed47d 586
36bb818d
ML
587Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const
588{
589 GVariant *data;
590 check(sr_config_list(
591 config_driver, config_sdi, config_channel_group,
592 key->id(), &data));
593 return Glib::VariantContainerBase(data);
d9eed47d
ML
594}
595
c23c8659 596Device::Device(struct sr_dev_inst *structure) :
58e21229 597 Configurable(sr_dev_inst_driver_get(structure), structure, nullptr),
3b161085 598 _structure(structure)
c23c8659 599{
e7eb2968 600 for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) {
f17b4546
DE
601 auto *const ch = static_cast<struct sr_channel *>(entry->data);
602 unique_ptr<Channel> channel {new Channel{ch}};
b605edf3 603 _channels.emplace(ch, move(channel));
c23c8659 604 }
6be7a7f2 605
e7eb2968 606 for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) {
f17b4546
DE
607 auto *const cg = static_cast<struct sr_channel_group *>(entry->data);
608 unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}};
b605edf3 609 _channel_groups.emplace(group->name(), move(group));
6be7a7f2 610 }
c23c8659
ML
611}
612
613Device::~Device()
e7eb2968
GS
614{
615}
c23c8659 616
15bebf57 617string Device::vendor() const
c23c8659 618{
80fe5247 619 return valid_string(sr_dev_inst_vendor_get(_structure));
c23c8659
ML
620}
621
15bebf57 622string Device::model() const
c23c8659 623{
80fe5247 624 return valid_string(sr_dev_inst_model_get(_structure));
c23c8659
ML
625}
626
15bebf57 627string Device::version() const
c23c8659 628{
80fe5247 629 return valid_string(sr_dev_inst_version_get(_structure));
c23c8659
ML
630}
631
15bebf57 632string Device::serial_number() const
d1075e5a 633{
80fe5247 634 return valid_string(sr_dev_inst_sernum_get(_structure));
d1075e5a
ML
635}
636
15bebf57 637string Device::connection_id() const
d1075e5a 638{
80fe5247 639 return valid_string(sr_dev_inst_connid_get(_structure));
d1075e5a
ML
640}
641
3b161085 642vector<shared_ptr<Channel>> Device::channels()
c23c8659
ML
643{
644 vector<shared_ptr<Channel>> result;
ce3e1e61
DE
645 for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) {
646 auto *const ch = static_cast<struct sr_channel *>(channel->data);
b6ab954d 647 result.push_back(_channels[ch]->share_owned_by(get_shared_from_this()));
ce3e1e61 648 }
c23c8659
ML
649 return result;
650}
651
4178d971
ML
652shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
653{
b6ab954d 654 return _channels[ptr]->share_owned_by(get_shared_from_this());
4178d971
ML
655}
656
6be7a7f2 657map<string, shared_ptr<ChannelGroup>>
3b161085 658Device::channel_groups()
6be7a7f2
ML
659{
660 map<string, shared_ptr<ChannelGroup>> result;
e7eb2968 661 for (const auto &entry: _channel_groups) {
f17b4546
DE
662 const auto &name = entry.first;
663 const auto &channel_group = entry.second;
b605edf3 664 result.emplace(name, channel_group->share_owned_by(get_shared_from_this()));
6be7a7f2
ML
665 }
666 return result;
667}
668
c23c8659
ML
669void Device::open()
670{
3b161085 671 check(sr_dev_open(_structure));
c23c8659
ML
672}
673
674void Device::close()
675{
3b161085 676 check(sr_dev_close(_structure));
c23c8659
ML
677}
678
a4e47454
ML
679HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
680 struct sr_dev_inst *structure) :
c23c8659 681 Device(structure),
d370545d 682 _driver(move(driver))
c23c8659 683{
c23c8659
ML
684}
685
686HardwareDevice::~HardwareDevice()
687{
c23c8659
ML
688}
689
d01d2314
ML
690shared_ptr<Device> HardwareDevice::get_shared_from_this()
691{
bf52cc8c 692 return static_pointer_cast<Device>(shared_from_this());
d01d2314
ML
693}
694
3b161085 695shared_ptr<Driver> HardwareDevice::driver()
c23c8659 696{
a4e47454 697 return _driver;
c23c8659
ML
698}
699
9fa5b426 700UserDevice::UserDevice(string vendor, string model, string version) :
d5d7b09e
DE
701 Device(sr_dev_inst_user_new(
702 vendor.c_str(), model.c_str(), version.c_str()))
9fa5b426
ML
703{
704}
705
706UserDevice::~UserDevice()
707{
708}
709
710shared_ptr<Device> UserDevice::get_shared_from_this()
711{
712 return static_pointer_cast<Device>(shared_from_this());
713}
714
715shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
716 const ChannelType *type, string name)
717{
718 check(sr_dev_inst_channel_add(Device::_structure,
719 index, type->id(), name.c_str()));
ce3e1e61
DE
720 GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure));
721 auto *const ch = static_cast<struct sr_channel *>(last->data);
f17b4546 722 unique_ptr<Channel> channel {new Channel{ch}};
b605edf3 723 _channels.emplace(ch, move(channel));
ce3e1e61 724 return get_channel(ch);
9fa5b426
ML
725}
726
c23c8659 727Channel::Channel(struct sr_channel *structure) :
d5d7b09e 728 _structure(structure),
3b161085 729 _type(ChannelType::get(_structure->type))
c23c8659
ML
730{
731}
732
733Channel::~Channel()
734{
735}
736
15bebf57 737string Channel::name() const
c23c8659 738{
3b161085 739 return valid_string(_structure->name);
c23c8659
ML
740}
741
742void Channel::set_name(string name)
743{
6f1346fb 744 check(sr_dev_channel_name_set(_structure, name.c_str()));
c23c8659
ML
745}
746
15bebf57 747const ChannelType *Channel::type() const
c23c8659 748{
3b161085 749 return ChannelType::get(_structure->type);
c23c8659
ML
750}
751
15bebf57 752bool Channel::enabled() const
c23c8659 753{
3b161085 754 return _structure->enabled;
c23c8659
ML
755}
756
757void Channel::set_enabled(bool value)
758{
6f1346fb 759 check(sr_dev_channel_enable(_structure, value));
c23c8659
ML
760}
761
15bebf57 762unsigned int Channel::index() const
06bd935e 763{
3b161085 764 return _structure->index;
06bd935e
ML
765}
766
f17b4546 767ChannelGroup::ChannelGroup(const Device *device,
c23c8659 768 struct sr_channel_group *structure) :
80fe5247 769 Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
c23c8659 770{
d5d7b09e 771 for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) {
ce3e1e61 772 auto *const ch = static_cast<struct sr_channel *>(entry->data);
f17b4546
DE
773 /* Note: This relies on Device::_channels to keep the Channel
774 * objects around over the lifetime of the ChannelGroup. */
775 _channels.push_back(device->_channels.find(ch)->second.get());
ce3e1e61 776 }
c23c8659
ML
777}
778
779ChannelGroup::~ChannelGroup()
780{
781}
782
15bebf57 783string ChannelGroup::name() const
c23c8659 784{
d5d7b09e 785 return valid_string(config_channel_group->name);
c23c8659
ML
786}
787
3b161085 788vector<shared_ptr<Channel>> ChannelGroup::channels()
c23c8659
ML
789{
790 vector<shared_ptr<Channel>> result;
f17b4546 791 for (const auto &channel : _channels)
b6ab954d 792 result.push_back(channel->share_owned_by(_parent));
c23c8659
ML
793 return result;
794}
795
176d785d 796Trigger::Trigger(shared_ptr<Context> context, string name) :
d5d7b09e 797 _structure(sr_trigger_new(name.c_str())),
d370545d 798 _context(move(context))
c23c8659 799{
f17b4546
DE
800 for (auto *stage = _structure->stages; stage; stage = stage->next) {
801 unique_ptr<TriggerStage> ts {new TriggerStage{
802 static_cast<struct sr_trigger_stage *>(stage->data)}};
803 _stages.push_back(move(ts));
804 }
c23c8659
ML
805}
806
807Trigger::~Trigger()
808{
3b161085 809 sr_trigger_free(_structure);
c23c8659
ML
810}
811
15bebf57 812string Trigger::name() const
c23c8659 813{
3b161085 814 return _structure->name;
c23c8659
ML
815}
816
3b161085 817vector<shared_ptr<TriggerStage>> Trigger::stages()
c23c8659
ML
818{
819 vector<shared_ptr<TriggerStage>> result;
f17b4546 820 for (const auto &stage : _stages)
b6ab954d 821 result.push_back(stage->share_owned_by(shared_from_this()));
c23c8659
ML
822 return result;
823}
824
825shared_ptr<TriggerStage> Trigger::add_stage()
826{
f17b4546
DE
827 unique_ptr<TriggerStage> stage {new TriggerStage{sr_trigger_stage_add(_structure)}};
828 _stages.push_back(move(stage));
829 return _stages.back()->share_owned_by(shared_from_this());
c23c8659
ML
830}
831
d5d7b09e
DE
832TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
833 _structure(structure)
c23c8659
ML
834{
835}
836
837TriggerStage::~TriggerStage()
838{
c23c8659 839}
176d785d 840
15bebf57 841int TriggerStage::number() const
c23c8659 842{
3b161085 843 return _structure->stage;
c23c8659
ML
844}
845
3b161085 846vector<shared_ptr<TriggerMatch>> TriggerStage::matches()
c23c8659
ML
847{
848 vector<shared_ptr<TriggerMatch>> result;
f17b4546 849 for (const auto &match : _matches)
b6ab954d 850 result.push_back(match->share_owned_by(shared_from_this()));
c23c8659
ML
851 return result;
852}
853
3b161085
ML
854void TriggerStage::add_match(shared_ptr<Channel> channel,
855 const TriggerMatchType *type, float value)
c23c8659 856{
3b161085
ML
857 check(sr_trigger_match_add(_structure,
858 channel->_structure, type->id(), value));
ce3e1e61 859 GSList *const last = g_slist_last(_structure->matches);
f17b4546
DE
860 unique_ptr<TriggerMatch> match {new TriggerMatch{
861 static_cast<struct sr_trigger_match *>(last->data),
862 move(channel)}};
863 _matches.push_back(move(match));
c23c8659
ML
864}
865
3b161085
ML
866void TriggerStage::add_match(shared_ptr<Channel> channel,
867 const TriggerMatchType *type)
c23c8659 868{
d370545d 869 add_match(move(channel), type, NAN);
c23c8659
ML
870}
871
3b161085
ML
872TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
873 shared_ptr<Channel> channel) :
d5d7b09e 874 _structure(structure),
d370545d 875 _channel(move(channel))
c23c8659
ML
876{
877}
878
879TriggerMatch::~TriggerMatch()
880{
881}
882
3b161085 883shared_ptr<Channel> TriggerMatch::channel()
c23c8659 884{
3b161085 885 return _channel;
c23c8659
ML
886}
887
15bebf57 888const TriggerMatchType *TriggerMatch::type() const
c23c8659 889{
3b161085 890 return TriggerMatchType::get(_structure->match);
c23c8659
ML
891}
892
15bebf57 893float TriggerMatch::value() const
c23c8659 894{
3b161085 895 return _structure->value;
c23c8659
ML
896}
897
898DatafeedCallbackData::DatafeedCallbackData(Session *session,
899 DatafeedCallbackFunction callback) :
d370545d 900 _callback(move(callback)),
3b161085 901 _session(session)
c23c8659
ML
902{
903}
904
905void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
906 const struct sr_datafeed_packet *pkt)
907{
ca4e307a 908 auto device = _session->get_device(sdi);
a98729a7 909 shared_ptr<Packet> packet {new Packet{device, pkt}, default_delete<Packet>{}};
d370545d 910 _callback(move(device), move(packet));
c23c8659
ML
911}
912
cac58676 913SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
cac58676
ML
914 Device(structure)
915{
916}
917
918SessionDevice::~SessionDevice()
919{
920}
921
922shared_ptr<Device> SessionDevice::get_shared_from_this()
923{
924 return static_pointer_cast<Device>(shared_from_this());
925}
926
c23c8659 927Session::Session(shared_ptr<Context> context) :
d5d7b09e 928 _structure(nullptr),
d370545d 929 _context(move(context))
c23c8659 930{
d370545d 931 check(sr_session_new(_context->_structure, &_structure));
3b161085 932 _context->_session = this;
c23c8659
ML
933}
934
935Session::Session(shared_ptr<Context> context, string filename) :
d5d7b09e 936 _structure(nullptr),
d370545d
DE
937 _context(move(context)),
938 _filename(move(filename))
c23c8659 939{
d370545d 940 check(sr_session_load(_context->_structure, _filename.c_str(), &_structure));
cac58676 941 GSList *dev_list;
3b161085 942 check(sr_session_dev_list(_structure, &dev_list));
ce3e1e61
DE
943 for (GSList *dev = dev_list; dev; dev = dev->next) {
944 auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
f17b4546 945 unique_ptr<SessionDevice> device {new SessionDevice{sdi}};
b605edf3 946 _owned_devices.emplace(sdi, move(device));
cac58676 947 }
3b161085 948 _context->_session = this;
3940abcb 949 g_slist_free(dev_list);
c23c8659
ML
950}
951
952Session::~Session()
953{
3b161085 954 check(sr_session_destroy(_structure));
ca4e307a
ML
955}
956
957shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
958{
959 if (_owned_devices.count(sdi))
960 return static_pointer_cast<Device>(
b6ab954d 961 _owned_devices[sdi]->share_owned_by(shared_from_this()));
ca4e307a
ML
962 else if (_other_devices.count(sdi))
963 return _other_devices[sdi];
964 else
965 throw Error(SR_ERR_BUG);
c23c8659
ML
966}
967
968void Session::add_device(shared_ptr<Device> device)
969{
d370545d
DE
970 const auto dev_struct = device->_structure;
971 check(sr_session_dev_add(_structure, dev_struct));
972 _other_devices[dev_struct] = move(device);
c23c8659
ML
973}
974
3b161085 975vector<shared_ptr<Device>> Session::devices()
c23c8659
ML
976{
977 GSList *dev_list;
3b161085 978 check(sr_session_dev_list(_structure, &dev_list));
c23c8659 979 vector<shared_ptr<Device>> result;
ce3e1e61
DE
980 for (GSList *dev = dev_list; dev; dev = dev->next) {
981 auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
ca4e307a 982 result.push_back(get_device(sdi));
c23c8659 983 }
3940abcb 984 g_slist_free(dev_list);
c23c8659
ML
985 return result;
986}
987
988void Session::remove_devices()
989{
ca4e307a 990 _other_devices.clear();
3b161085 991 check(sr_session_dev_remove_all(_structure));
c23c8659
ML
992}
993
994void Session::start()
995{
3b161085 996 check(sr_session_start(_structure));
c23c8659
ML
997}
998
999void Session::run()
1000{
3b161085 1001 check(sr_session_run(_structure));
c23c8659
ML
1002}
1003
1004void Session::stop()
1005{
3b161085 1006 check(sr_session_stop(_structure));
c23c8659
ML
1007}
1008
f91cf612
DE
1009bool Session::is_running() const
1010{
1011 const int ret = sr_session_is_running(_structure);
1012 if (ret < 0)
1013 throw Error{ret};
1014 return (ret != 0);
1015}
1016
0ab8e5d2 1017static void session_stopped_callback(void *data) noexcept
f91cf612
DE
1018{
1019 auto *const callback = static_cast<SessionStoppedCallback*>(data);
1020 (*callback)();
1021}
1022
1023void Session::set_stopped_callback(SessionStoppedCallback callback)
1024{
1025 _stopped_callback = move(callback);
1026 if (_stopped_callback)
1027 check(sr_session_stopped_callback_set(_structure,
1028 &session_stopped_callback, &_stopped_callback));
1029 else
1030 check(sr_session_stopped_callback_set(_structure,
1031 nullptr, nullptr));
1032}
1033
c23c8659 1034static void datafeed_callback(const struct sr_dev_inst *sdi,
0ab8e5d2 1035 const struct sr_datafeed_packet *pkt, void *cb_data) noexcept
c23c8659
ML
1036{
1037 auto callback = static_cast<DatafeedCallbackData *>(cb_data);
1038 callback->run(sdi, pkt);
1039}
ce3e1e61 1040
c23c8659
ML
1041void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
1042{
f17b4546
DE
1043 unique_ptr<DatafeedCallbackData> cb_data
1044 {new DatafeedCallbackData{this, move(callback)}};
3b161085 1045 check(sr_session_datafeed_callback_add(_structure,
f17b4546
DE
1046 &datafeed_callback, cb_data.get()));
1047 _datafeed_callbacks.push_back(move(cb_data));
c23c8659
ML
1048}
1049
d370545d 1050void Session::remove_datafeed_callbacks()
c23c8659 1051{
3b161085 1052 check(sr_session_datafeed_callback_remove_all(_structure));
3b161085 1053 _datafeed_callbacks.clear();
c23c8659
ML
1054}
1055
3b161085 1056shared_ptr<Trigger> Session::trigger()
6fa0eb86 1057{
3b161085 1058 return _trigger;
6fa0eb86
ML
1059}
1060
1061void Session::set_trigger(shared_ptr<Trigger> trigger)
1062{
e835e808
UH
1063 if (!trigger)
1064 // Set NULL trigger, i.e. remove any trigger from the session.
58e21229 1065 check(sr_session_trigger_set(_structure, nullptr));
e835e808
UH
1066 else
1067 check(sr_session_trigger_set(_structure, trigger->_structure));
d370545d 1068 _trigger = move(trigger);
6fa0eb86
ML
1069}
1070
15bebf57 1071string Session::filename() const
1411f7d8
ML
1072{
1073 return _filename;
1074}
1075
624d1610
UH
1076shared_ptr<Context> Session::context()
1077{
1078 return _context;
1079}
1080
2928f47d
ML
1081Packet::Packet(shared_ptr<Device> device,
1082 const struct sr_datafeed_packet *structure) :
d5d7b09e 1083 _structure(structure),
d370545d 1084 _device(move(device))
c23c8659
ML
1085{
1086 switch (structure->type)
1087 {
2928f47d 1088 case SR_DF_HEADER:
f17b4546 1089 _payload.reset(new Header{
2928f47d 1090 static_cast<const struct sr_datafeed_header *>(
f17b4546 1091 structure->payload)});
2928f47d
ML
1092 break;
1093 case SR_DF_META:
f17b4546 1094 _payload.reset(new Meta{
2928f47d 1095 static_cast<const struct sr_datafeed_meta *>(
f17b4546 1096 structure->payload)});
2928f47d 1097 break;
c23c8659 1098 case SR_DF_LOGIC:
f17b4546 1099 _payload.reset(new Logic{
c23c8659 1100 static_cast<const struct sr_datafeed_logic *>(
f17b4546 1101 structure->payload)});
c23c8659 1102 break;
dd13d47a 1103 case SR_DF_ANALOG:
f17b4546 1104 _payload.reset(new Analog{
dd13d47a 1105 static_cast<const struct sr_datafeed_analog *>(
f17b4546 1106 structure->payload)});
4cd883a7 1107 break;
c23c8659
ML
1108 }
1109}
1110
1111Packet::~Packet()
1112{
c23c8659
ML
1113}
1114
15bebf57 1115const PacketType *Packet::type() const
90ba83f2 1116{
3b161085 1117 return PacketType::get(_structure->type);
90ba83f2
ML
1118}
1119
3b161085 1120shared_ptr<PacketPayload> Packet::payload()
c23c8659 1121{
3b161085 1122 if (_payload)
b6ab954d 1123 return _payload->share_owned_by(shared_from_this());
4cd883a7
ML
1124 else
1125 throw Error(SR_ERR_NA);
c23c8659
ML
1126}
1127
1128PacketPayload::PacketPayload()
1129{
1130}
1131
1132PacketPayload::~PacketPayload()
1133{
1134}
1135
2928f47d 1136Header::Header(const struct sr_datafeed_header *structure) :
d5d7b09e
DE
1137 PacketPayload(),
1138 _structure(structure)
2928f47d
ML
1139{
1140}
1141
1142Header::~Header()
1143{
1144}
1145
b6ab954d 1146shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1147{
4f7bcf0e 1148 return static_pointer_cast<PacketPayload>(
b6ab954d 1149 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1150}
1151
15bebf57 1152int Header::feed_version() const
2928f47d 1153{
3b161085 1154 return _structure->feed_version;
2928f47d
ML
1155}
1156
15bebf57 1157Glib::TimeVal Header::start_time() const
2928f47d
ML
1158{
1159 return Glib::TimeVal(
3b161085
ML
1160 _structure->starttime.tv_sec,
1161 _structure->starttime.tv_usec);
2928f47d
ML
1162}
1163
1164Meta::Meta(const struct sr_datafeed_meta *structure) :
d5d7b09e
DE
1165 PacketPayload(),
1166 _structure(structure)
2928f47d
ML
1167{
1168}
1169
1170Meta::~Meta()
1171{
1172}
1173
b6ab954d 1174shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1175{
4f7bcf0e 1176 return static_pointer_cast<PacketPayload>(
b6ab954d 1177 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1178}
1179
15bebf57 1180map<const ConfigKey *, Glib::VariantBase> Meta::config() const
2928f47d
ML
1181{
1182 map<const ConfigKey *, Glib::VariantBase> result;
ce3e1e61
DE
1183 for (auto l = _structure->config; l; l = l->next) {
1184 auto *const config = static_cast<struct sr_config *>(l->data);
7cccc915 1185 result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true);
2928f47d
ML
1186 }
1187 return result;
1188}
1189
1190Logic::Logic(const struct sr_datafeed_logic *structure) :
d5d7b09e
DE
1191 PacketPayload(),
1192 _structure(structure)
2928f47d
ML
1193{
1194}
c23c8659
ML
1195
1196Logic::~Logic()
1197{
1198}
1199
b6ab954d 1200shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1201{
4f7bcf0e 1202 return static_pointer_cast<PacketPayload>(
b6ab954d 1203 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1204}
1205
3b161085 1206void *Logic::data_pointer()
c23c8659 1207{
3b161085 1208 return _structure->data;
c23c8659
ML
1209}
1210
15bebf57 1211size_t Logic::data_length() const
c23c8659 1212{
3b161085 1213 return _structure->length;
c23c8659
ML
1214}
1215
15bebf57 1216unsigned int Logic::unit_size() const
2928f47d 1217{
3b161085 1218 return _structure->unitsize;
2928f47d
ML
1219}
1220
dd13d47a 1221Analog::Analog(const struct sr_datafeed_analog *structure) :
d5d7b09e
DE
1222 PacketPayload(),
1223 _structure(structure)
c23c8659
ML
1224{
1225}
1226
dd13d47a 1227Analog::~Analog()
c23c8659
ML
1228{
1229}
1230
b6ab954d 1231shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1232{
4f7bcf0e 1233 return static_pointer_cast<PacketPayload>(
b6ab954d 1234 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1235}
1236
dd13d47a 1237void *Analog::data_pointer()
c23c8659 1238{
3b161085 1239 return _structure->data;
c23c8659
ML
1240}
1241
c5d081f7
SA
1242void Analog::get_data_as_float(float *dest)
1243{
1244 check(sr_analog_to_float(_structure, dest));
1245}
1246
15bebf57 1247unsigned int Analog::num_samples() const
c23c8659 1248{
3b161085 1249 return _structure->num_samples;
c23c8659
ML
1250}
1251
dd13d47a 1252vector<shared_ptr<Channel>> Analog::channels()
c23c8659 1253{
2928f47d 1254 vector<shared_ptr<Channel>> result;
ce3e1e61
DE
1255 for (auto l = _structure->meaning->channels; l; l = l->next) {
1256 auto *const ch = static_cast<struct sr_channel *>(l->data);
1257 result.push_back(_parent->_device->get_channel(ch));
1258 }
2928f47d 1259 return result;
c23c8659
ML
1260}
1261
0cee3a3e
SA
1262unsigned int Analog::unitsize() const
1263{
1264 return _structure->encoding->unitsize;
1265}
1266
1267bool Analog::is_signed() const
1268{
1269 return _structure->encoding->is_signed;
1270}
1271
1272bool Analog::is_float() const
1273{
1274 return _structure->encoding->is_float;
1275}
1276
1277bool Analog::is_bigendian() const
1278{
1279 return _structure->encoding->is_bigendian;
1280}
1281
1282int Analog::digits() const
1283{
1284 return _structure->encoding->digits;
1285}
1286
1287bool Analog::is_digits_decimal() const
1288{
1289 return _structure->encoding->is_digits_decimal;
1290}
1291
1292shared_ptr<Rational> Analog::scale()
1293{
1294 unique_ptr<Rational> scale;
1295 scale.reset(new Rational(&(_structure->encoding->scale)));
1296
1297 if (scale)
1298 return scale->share_owned_by(shared_from_this());
1299 else
1300 throw Error(SR_ERR_NA);
1301}
1302
1303shared_ptr<Rational> Analog::offset()
1304{
1305 unique_ptr<Rational> offset;
1306 offset.reset(new Rational(&(_structure->encoding->offset)));
1307
1308 if (offset)
1309 return offset->share_owned_by(shared_from_this());
1310 else
1311 throw Error(SR_ERR_NA);
1312}
1313
15bebf57 1314const Quantity *Analog::mq() const
c23c8659 1315{
dd13d47a 1316 return Quantity::get(_structure->meaning->mq);
c23c8659
ML
1317}
1318
15bebf57 1319const Unit *Analog::unit() const
c23c8659 1320{
dd13d47a 1321 return Unit::get(_structure->meaning->unit);
c23c8659
ML
1322}
1323
15bebf57 1324vector<const QuantityFlag *> Analog::mq_flags() const
c23c8659 1325{
dd13d47a 1326 return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
c23c8659
ML
1327}
1328
6ad2fbaa
SA
1329shared_ptr<Logic> Analog::get_logic_via_threshold(float threshold,
1330 uint8_t *data_ptr) const
1331{
1332 auto datafeed = g_new(struct sr_datafeed_logic, 1);
1333 datafeed->length = num_samples();
1334 datafeed->unitsize = 1;
1335
1336 if (data_ptr)
1337 datafeed->data = data_ptr;
1338 else
1339 datafeed->data = g_malloc(datafeed->length);
1340
1341 shared_ptr<Logic> logic =
1342 shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
1343
1344 check(sr_a2l_threshold(_structure, threshold,
1345 (uint8_t*)datafeed->data, datafeed->length));
1346
1347 return logic;
1348}
1349
1350shared_ptr<Logic> Analog::get_logic_via_schmitt_trigger(float lo_thr,
1351 float hi_thr, uint8_t *state, uint8_t *data_ptr) const
1352{
1353 auto datafeed = g_new(struct sr_datafeed_logic, 1);
1354 datafeed->length = num_samples();
1355 datafeed->unitsize = 1;
1356
1357 if (data_ptr)
1358 datafeed->data = data_ptr;
1359 else
1360 datafeed->data = g_malloc(datafeed->length);
1361
1362 shared_ptr<Logic> logic =
1363 shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
1364
1365 check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state,
1366 (uint8_t*)datafeed->data, datafeed->length));
1367
1368 return logic;
1369}
1370
0cee3a3e
SA
1371Rational::Rational(const struct sr_rational *structure) :
1372 _structure(structure)
1373{
1374}
1375
1376Rational::~Rational()
1377{
1378}
1379
1380shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent)
1381{
1382 return static_pointer_cast<Rational>(
1383 ParentOwned::share_owned_by(_parent));
1384}
1385
1386int64_t Rational::numerator() const
1387{
1388 return _structure->p;
1389}
1390
1391uint64_t Rational::denominator() const
1392{
1393 return _structure->q;
1394}
1395
1396float Rational::value() const
1397{
1398 return (float)(_structure->p) / (float)(_structure->q);
1399}
1400
ca3291e3 1401InputFormat::InputFormat(const struct sr_input_module *structure) :
d5d7b09e 1402 _structure(structure)
c23c8659
ML
1403{
1404}
1405
1406InputFormat::~InputFormat()
1407{
1408}
1409
15bebf57 1410string InputFormat::name() const
c23c8659 1411{
3b161085 1412 return valid_string(sr_input_id_get(_structure));
c23c8659
ML
1413}
1414
15bebf57 1415string InputFormat::description() const
c23c8659 1416{
3b161085 1417 return valid_string(sr_input_description_get(_structure));
ca3291e3
ML
1418}
1419
15bebf57 1420vector<string> InputFormat::extensions() const
c7bc82ff
JH
1421{
1422 vector<string> exts;
1423 for (const char *const *e = sr_input_extensions_get(_structure);
1424 e && *e; e++)
1425 exts.push_back(*e);
1426 return exts;
1427}
1428
3b161085 1429map<string, shared_ptr<Option>> InputFormat::options()
43942280 1430{
43942280 1431 map<string, shared_ptr<Option>> result;
a98729a7 1432
e7eb2968 1433 if (const struct sr_option **options = sr_input_options_get(_structure)) {
a98729a7
DE
1434 shared_ptr<const struct sr_option *> option_array
1435 {options, &sr_input_options_free};
1436 for (int i = 0; options[i]; i++) {
1437 shared_ptr<Option> opt {
1438 new Option{options[i], option_array},
1439 default_delete<Option>{}};
b605edf3 1440 result.emplace(opt->id(), move(opt));
a98729a7 1441 }
48d92e2c 1442 }
43942280
ML
1443 return result;
1444}
1445
ca3291e3 1446shared_ptr<Input> InputFormat::create_input(
9e7176bd 1447 map<string, Glib::VariantBase> options)
ca3291e3 1448{
3b161085 1449 auto input = sr_input_new(_structure, map_to_hash_variant(options));
ca3291e3
ML
1450 if (!input)
1451 throw Error(SR_ERR_ARG);
a98729a7 1452 return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}};
c23c8659
ML
1453}
1454
ca3291e3 1455Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
d5d7b09e 1456 _structure(structure),
f17b4546 1457 _context(move(context))
c23c8659 1458{
c23c8659
ML
1459}
1460
3b161085 1461shared_ptr<InputDevice> Input::device()
c23c8659 1462{
e7eb2968 1463 if (!_device) {
3b161085 1464 auto sdi = sr_input_dev_inst_get(_structure);
ca3291e3
ML
1465 if (!sdi)
1466 throw Error(SR_ERR_NA);
f17b4546 1467 _device.reset(new InputDevice{shared_from_this(), sdi});
ca3291e3 1468 }
c23c8659 1469
b6ab954d 1470 return _device->share_owned_by(shared_from_this());
ca3291e3 1471}
c23c8659 1472
2b51d48b 1473void Input::send(void *data, size_t length)
ca3291e3 1474{
ce3e1e61 1475 auto gstr = g_string_new_len(static_cast<char *>(data), length);
3b161085 1476 auto ret = sr_input_send(_structure, gstr);
e3e1f20c 1477 g_string_free(gstr, true);
ca3291e3 1478 check(ret);
c23c8659
ML
1479}
1480
9c51e8ec
ML
1481void Input::end()
1482{
1483 check(sr_input_end(_structure));
1484}
1485
b6b4f03e
SA
1486void Input::reset()
1487{
1488 check(sr_input_reset(_structure));
1489}
1490
ca3291e3 1491Input::~Input()
c23c8659 1492{
9c51e8ec 1493 sr_input_free(_structure);
c23c8659
ML
1494}
1495
6e5240f4
ML
1496InputDevice::InputDevice(shared_ptr<Input> input,
1497 struct sr_dev_inst *structure) :
6e5240f4 1498 Device(structure),
d370545d 1499 _input(move(input))
c23c8659 1500{
c23c8659
ML
1501}
1502
ca3291e3 1503InputDevice::~InputDevice()
c23c8659 1504{
c23c8659
ML
1505}
1506
d01d2314
ML
1507shared_ptr<Device> InputDevice::get_shared_from_this()
1508{
bf52cc8c 1509 return static_pointer_cast<Device>(shared_from_this());
d01d2314
ML
1510}
1511
58aa1f83 1512Option::Option(const struct sr_option *structure,
70d3b20b 1513 shared_ptr<const struct sr_option *> structure_array) :
d5d7b09e 1514 _structure(structure),
d370545d 1515 _structure_array(move(structure_array))
58aa1f83
ML
1516{
1517}
1518
1519Option::~Option()
1520{
1521}
1522
15bebf57 1523string Option::id() const
58aa1f83 1524{
3b161085 1525 return valid_string(_structure->id);
58aa1f83
ML
1526}
1527
15bebf57 1528string Option::name() const
58aa1f83 1529{
3b161085 1530 return valid_string(_structure->name);
58aa1f83
ML
1531}
1532
15bebf57 1533string Option::description() const
58aa1f83 1534{
3b161085 1535 return valid_string(_structure->desc);
58aa1f83
ML
1536}
1537
15bebf57 1538Glib::VariantBase Option::default_value() const
58aa1f83 1539{
3b161085 1540 return Glib::VariantBase(_structure->def, true);
58aa1f83
ML
1541}
1542
15bebf57 1543vector<Glib::VariantBase> Option::values() const
58aa1f83
ML
1544{
1545 vector<Glib::VariantBase> result;
ce3e1e61
DE
1546 for (auto l = _structure->values; l; l = l->next) {
1547 auto *const var = static_cast<GVariant *>(l->data);
1548 result.push_back(Glib::VariantBase(var, true));
1549 }
58aa1f83
ML
1550 return result;
1551}
1552
61a6d983
GS
1553Glib::VariantBase Option::parse_string(string value)
1554{
1555 enum sr_datatype dt;
1556 Glib::VariantBase dflt = default_value();
1557 GVariant *tmpl = dflt.gobj();
1558
1559 if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) {
1560 dt = SR_T_UINT64;
1561 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) {
1562 dt = SR_T_STRING;
1563 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) {
1564 dt = SR_T_BOOL;
1565 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) {
1566 dt = SR_T_FLOAT;
1567 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) {
1568 dt = SR_T_INT32;
1569 } else {
1570 throw Error(SR_ERR_BUG);
1571 }
1572 return ConfigKey::parse_string(value, dt);
1573}
1574
58aa1f83 1575OutputFormat::OutputFormat(const struct sr_output_module *structure) :
d5d7b09e 1576 _structure(structure)
c23c8659
ML
1577{
1578}
1579
1580OutputFormat::~OutputFormat()
1581{
1582}
1583
15bebf57 1584string OutputFormat::name() const
c23c8659 1585{
3b161085 1586 return valid_string(sr_output_id_get(_structure));
c23c8659
ML
1587}
1588
15bebf57 1589string OutputFormat::description() const
c23c8659 1590{
3b161085 1591 return valid_string(sr_output_description_get(_structure));
58aa1f83
ML
1592}
1593
15bebf57 1594vector<string> OutputFormat::extensions() const
8a174d23
JH
1595{
1596 vector<string> exts;
1597 for (const char *const *e = sr_output_extensions_get(_structure);
1598 e && *e; e++)
1599 exts.push_back(*e);
1600 return exts;
1601}
1602
3b161085 1603map<string, shared_ptr<Option>> OutputFormat::options()
58aa1f83 1604{
58aa1f83 1605 map<string, shared_ptr<Option>> result;
a98729a7 1606
e7eb2968 1607 if (const struct sr_option **options = sr_output_options_get(_structure)) {
a98729a7
DE
1608 shared_ptr<const struct sr_option *> option_array
1609 {options, &sr_output_options_free};
1610 for (int i = 0; options[i]; i++) {
1611 shared_ptr<Option> opt {
1612 new Option{options[i], option_array},
1613 default_delete<Option>{}};
b605edf3 1614 result.emplace(opt->id(), move(opt));
a98729a7 1615 }
48d92e2c 1616 }
58aa1f83 1617 return result;
c23c8659
ML
1618}
1619
1620shared_ptr<Output> OutputFormat::create_output(
9e7176bd 1621 shared_ptr<Device> device, map<string, Glib::VariantBase> options)
c23c8659 1622{
a98729a7 1623 return shared_ptr<Output>{
9e7176bd 1624 new Output{shared_from_this(), move(device), move(options)},
a98729a7 1625 default_delete<Output>{}};
c23c8659
ML
1626}
1627
81b3ce37 1628shared_ptr<Output> OutputFormat::create_output(string filename,
9e7176bd 1629 shared_ptr<Device> device, map<string, Glib::VariantBase> options)
81b3ce37 1630{
a98729a7 1631 return shared_ptr<Output>{
9e7176bd 1632 new Output{move(filename), shared_from_this(), move(device), move(options)},
a98729a7 1633 default_delete<Output>{}};
81b3ce37
SA
1634}
1635
15bebf57 1636bool OutputFormat::test_flag(const OutputFlag *flag) const
3cd4b381
SA
1637{
1638 return sr_output_test_flag(_structure, flag->id());
1639}
1640
c23c8659 1641Output::Output(shared_ptr<OutputFormat> format,
9e7176bd 1642 shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
d5d7b09e 1643 _structure(sr_output_new(format->_structure,
58e21229 1644 map_to_hash_variant(options), device->_structure, nullptr)),
d370545d
DE
1645 _format(move(format)),
1646 _device(move(device)),
9e7176bd 1647 _options(move(options))
81b3ce37
SA
1648{
1649}
1650
1651Output::Output(string filename, shared_ptr<OutputFormat> format,
9e7176bd 1652 shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
d5d7b09e 1653 _structure(sr_output_new(format->_structure,
81b3ce37 1654 map_to_hash_variant(options), device->_structure, filename.c_str())),
d370545d
DE
1655 _format(move(format)),
1656 _device(move(device)),
9e7176bd 1657 _options(move(options))
c23c8659
ML
1658{
1659}
1660
1661Output::~Output()
1662{
3b161085 1663 check(sr_output_free(_structure));
c23c8659
ML
1664}
1665
ea7a83a4
ML
1666shared_ptr<OutputFormat> Output::format()
1667{
1668 return _format;
1669}
1670
c23c8659
ML
1671string Output::receive(shared_ptr<Packet> packet)
1672{
1673 GString *out;
3b161085 1674 check(sr_output_send(_structure, packet->_structure, &out));
e7eb2968 1675 if (out) {
c23c8659
ML
1676 auto result = string(out->str, out->str + out->len);
1677 g_string_free(out, true);
1678 return result;
e7eb2968 1679 } else {
c23c8659
ML
1680 return string();
1681 }
1682}
1683
b5f07319 1684#include <enums.cpp>
c23c8659
ML
1685
1686}