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