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