]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/classes.cpp
Free list returned by sr_session_dev_list()
[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;
3940abcb 939 g_slist_free(dev_list);
c23c8659
ML
940}
941
942Session::~Session()
943{
3b161085 944 check(sr_session_destroy(_structure));
ca4e307a
ML
945}
946
947shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
948{
949 if (_owned_devices.count(sdi))
950 return static_pointer_cast<Device>(
b6ab954d 951 _owned_devices[sdi]->share_owned_by(shared_from_this()));
ca4e307a
ML
952 else if (_other_devices.count(sdi))
953 return _other_devices[sdi];
954 else
955 throw Error(SR_ERR_BUG);
c23c8659
ML
956}
957
958void Session::add_device(shared_ptr<Device> device)
959{
d370545d
DE
960 const auto dev_struct = device->_structure;
961 check(sr_session_dev_add(_structure, dev_struct));
962 _other_devices[dev_struct] = move(device);
c23c8659
ML
963}
964
3b161085 965vector<shared_ptr<Device>> Session::devices()
c23c8659
ML
966{
967 GSList *dev_list;
3b161085 968 check(sr_session_dev_list(_structure, &dev_list));
c23c8659 969 vector<shared_ptr<Device>> result;
ce3e1e61
DE
970 for (GSList *dev = dev_list; dev; dev = dev->next) {
971 auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
ca4e307a 972 result.push_back(get_device(sdi));
c23c8659 973 }
3940abcb 974 g_slist_free(dev_list);
c23c8659
ML
975 return result;
976}
977
978void Session::remove_devices()
979{
ca4e307a 980 _other_devices.clear();
3b161085 981 check(sr_session_dev_remove_all(_structure));
c23c8659
ML
982}
983
984void Session::start()
985{
3b161085 986 check(sr_session_start(_structure));
c23c8659
ML
987}
988
989void Session::run()
990{
3b161085 991 check(sr_session_run(_structure));
c23c8659
ML
992}
993
994void Session::stop()
995{
3b161085 996 check(sr_session_stop(_structure));
c23c8659
ML
997}
998
f91cf612
DE
999bool Session::is_running() const
1000{
1001 const int ret = sr_session_is_running(_structure);
1002 if (ret < 0)
1003 throw Error{ret};
1004 return (ret != 0);
1005}
1006
0ab8e5d2 1007static void session_stopped_callback(void *data) noexcept
f91cf612
DE
1008{
1009 auto *const callback = static_cast<SessionStoppedCallback*>(data);
1010 (*callback)();
1011}
1012
1013void Session::set_stopped_callback(SessionStoppedCallback callback)
1014{
1015 _stopped_callback = move(callback);
1016 if (_stopped_callback)
1017 check(sr_session_stopped_callback_set(_structure,
1018 &session_stopped_callback, &_stopped_callback));
1019 else
1020 check(sr_session_stopped_callback_set(_structure,
1021 nullptr, nullptr));
1022}
1023
c23c8659 1024static void datafeed_callback(const struct sr_dev_inst *sdi,
0ab8e5d2 1025 const struct sr_datafeed_packet *pkt, void *cb_data) noexcept
c23c8659
ML
1026{
1027 auto callback = static_cast<DatafeedCallbackData *>(cb_data);
1028 callback->run(sdi, pkt);
1029}
ce3e1e61 1030
c23c8659
ML
1031void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
1032{
f17b4546
DE
1033 unique_ptr<DatafeedCallbackData> cb_data
1034 {new DatafeedCallbackData{this, move(callback)}};
3b161085 1035 check(sr_session_datafeed_callback_add(_structure,
f17b4546
DE
1036 &datafeed_callback, cb_data.get()));
1037 _datafeed_callbacks.push_back(move(cb_data));
c23c8659
ML
1038}
1039
d370545d 1040void Session::remove_datafeed_callbacks()
c23c8659 1041{
3b161085 1042 check(sr_session_datafeed_callback_remove_all(_structure));
3b161085 1043 _datafeed_callbacks.clear();
c23c8659
ML
1044}
1045
3b161085 1046shared_ptr<Trigger> Session::trigger()
6fa0eb86 1047{
3b161085 1048 return _trigger;
6fa0eb86
ML
1049}
1050
1051void Session::set_trigger(shared_ptr<Trigger> trigger)
1052{
e835e808
UH
1053 if (!trigger)
1054 // Set NULL trigger, i.e. remove any trigger from the session.
58e21229 1055 check(sr_session_trigger_set(_structure, nullptr));
e835e808
UH
1056 else
1057 check(sr_session_trigger_set(_structure, trigger->_structure));
d370545d 1058 _trigger = move(trigger);
6fa0eb86
ML
1059}
1060
15bebf57 1061string Session::filename() const
1411f7d8
ML
1062{
1063 return _filename;
1064}
1065
624d1610
UH
1066shared_ptr<Context> Session::context()
1067{
1068 return _context;
1069}
1070
2928f47d
ML
1071Packet::Packet(shared_ptr<Device> device,
1072 const struct sr_datafeed_packet *structure) :
d5d7b09e 1073 _structure(structure),
d370545d 1074 _device(move(device))
c23c8659
ML
1075{
1076 switch (structure->type)
1077 {
2928f47d 1078 case SR_DF_HEADER:
f17b4546 1079 _payload.reset(new Header{
2928f47d 1080 static_cast<const struct sr_datafeed_header *>(
f17b4546 1081 structure->payload)});
2928f47d
ML
1082 break;
1083 case SR_DF_META:
f17b4546 1084 _payload.reset(new Meta{
2928f47d 1085 static_cast<const struct sr_datafeed_meta *>(
f17b4546 1086 structure->payload)});
2928f47d 1087 break;
c23c8659 1088 case SR_DF_LOGIC:
f17b4546 1089 _payload.reset(new Logic{
c23c8659 1090 static_cast<const struct sr_datafeed_logic *>(
f17b4546 1091 structure->payload)});
c23c8659 1092 break;
dd13d47a 1093 case SR_DF_ANALOG:
f17b4546 1094 _payload.reset(new Analog{
dd13d47a 1095 static_cast<const struct sr_datafeed_analog *>(
f17b4546 1096 structure->payload)});
4cd883a7 1097 break;
c23c8659
ML
1098 }
1099}
1100
1101Packet::~Packet()
1102{
c23c8659
ML
1103}
1104
15bebf57 1105const PacketType *Packet::type() const
90ba83f2 1106{
3b161085 1107 return PacketType::get(_structure->type);
90ba83f2
ML
1108}
1109
3b161085 1110shared_ptr<PacketPayload> Packet::payload()
c23c8659 1111{
3b161085 1112 if (_payload)
b6ab954d 1113 return _payload->share_owned_by(shared_from_this());
4cd883a7
ML
1114 else
1115 throw Error(SR_ERR_NA);
c23c8659
ML
1116}
1117
1118PacketPayload::PacketPayload()
1119{
1120}
1121
1122PacketPayload::~PacketPayload()
1123{
1124}
1125
2928f47d 1126Header::Header(const struct sr_datafeed_header *structure) :
d5d7b09e
DE
1127 PacketPayload(),
1128 _structure(structure)
2928f47d
ML
1129{
1130}
1131
1132Header::~Header()
1133{
1134}
1135
b6ab954d 1136shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1137{
4f7bcf0e 1138 return static_pointer_cast<PacketPayload>(
b6ab954d 1139 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1140}
1141
15bebf57 1142int Header::feed_version() const
2928f47d 1143{
3b161085 1144 return _structure->feed_version;
2928f47d
ML
1145}
1146
15bebf57 1147Glib::TimeVal Header::start_time() const
2928f47d
ML
1148{
1149 return Glib::TimeVal(
3b161085
ML
1150 _structure->starttime.tv_sec,
1151 _structure->starttime.tv_usec);
2928f47d
ML
1152}
1153
1154Meta::Meta(const struct sr_datafeed_meta *structure) :
d5d7b09e
DE
1155 PacketPayload(),
1156 _structure(structure)
2928f47d
ML
1157{
1158}
1159
1160Meta::~Meta()
1161{
1162}
1163
b6ab954d 1164shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1165{
4f7bcf0e 1166 return static_pointer_cast<PacketPayload>(
b6ab954d 1167 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1168}
1169
15bebf57 1170map<const ConfigKey *, Glib::VariantBase> Meta::config() const
2928f47d
ML
1171{
1172 map<const ConfigKey *, Glib::VariantBase> result;
ce3e1e61
DE
1173 for (auto l = _structure->config; l; l = l->next) {
1174 auto *const config = static_cast<struct sr_config *>(l->data);
7cccc915 1175 result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true);
2928f47d
ML
1176 }
1177 return result;
1178}
1179
1180Logic::Logic(const struct sr_datafeed_logic *structure) :
d5d7b09e
DE
1181 PacketPayload(),
1182 _structure(structure)
2928f47d
ML
1183{
1184}
c23c8659
ML
1185
1186Logic::~Logic()
1187{
1188}
1189
b6ab954d 1190shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1191{
4f7bcf0e 1192 return static_pointer_cast<PacketPayload>(
b6ab954d 1193 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1194}
1195
3b161085 1196void *Logic::data_pointer()
c23c8659 1197{
3b161085 1198 return _structure->data;
c23c8659
ML
1199}
1200
15bebf57 1201size_t Logic::data_length() const
c23c8659 1202{
3b161085 1203 return _structure->length;
c23c8659
ML
1204}
1205
15bebf57 1206unsigned int Logic::unit_size() const
2928f47d 1207{
3b161085 1208 return _structure->unitsize;
2928f47d
ML
1209}
1210
dd13d47a 1211Analog::Analog(const struct sr_datafeed_analog *structure) :
d5d7b09e
DE
1212 PacketPayload(),
1213 _structure(structure)
c23c8659
ML
1214{
1215}
1216
dd13d47a 1217Analog::~Analog()
c23c8659
ML
1218{
1219}
1220
b6ab954d 1221shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent)
4cd883a7 1222{
4f7bcf0e 1223 return static_pointer_cast<PacketPayload>(
b6ab954d 1224 ParentOwned::share_owned_by(_parent));
4cd883a7
ML
1225}
1226
dd13d47a 1227void *Analog::data_pointer()
c23c8659 1228{
3b161085 1229 return _structure->data;
c23c8659
ML
1230}
1231
c5d081f7
SA
1232void Analog::get_data_as_float(float *dest)
1233{
1234 check(sr_analog_to_float(_structure, dest));
1235}
1236
15bebf57 1237unsigned int Analog::num_samples() const
c23c8659 1238{
3b161085 1239 return _structure->num_samples;
c23c8659
ML
1240}
1241
dd13d47a 1242vector<shared_ptr<Channel>> Analog::channels()
c23c8659 1243{
2928f47d 1244 vector<shared_ptr<Channel>> result;
ce3e1e61
DE
1245 for (auto l = _structure->meaning->channels; l; l = l->next) {
1246 auto *const ch = static_cast<struct sr_channel *>(l->data);
1247 result.push_back(_parent->_device->get_channel(ch));
1248 }
2928f47d 1249 return result;
c23c8659
ML
1250}
1251
0cee3a3e
SA
1252unsigned int Analog::unitsize() const
1253{
1254 return _structure->encoding->unitsize;
1255}
1256
1257bool Analog::is_signed() const
1258{
1259 return _structure->encoding->is_signed;
1260}
1261
1262bool Analog::is_float() const
1263{
1264 return _structure->encoding->is_float;
1265}
1266
1267bool Analog::is_bigendian() const
1268{
1269 return _structure->encoding->is_bigendian;
1270}
1271
1272int Analog::digits() const
1273{
1274 return _structure->encoding->digits;
1275}
1276
1277bool Analog::is_digits_decimal() const
1278{
1279 return _structure->encoding->is_digits_decimal;
1280}
1281
1282shared_ptr<Rational> Analog::scale()
1283{
1284 unique_ptr<Rational> scale;
1285 scale.reset(new Rational(&(_structure->encoding->scale)));
1286
1287 if (scale)
1288 return scale->share_owned_by(shared_from_this());
1289 else
1290 throw Error(SR_ERR_NA);
1291}
1292
1293shared_ptr<Rational> Analog::offset()
1294{
1295 unique_ptr<Rational> offset;
1296 offset.reset(new Rational(&(_structure->encoding->offset)));
1297
1298 if (offset)
1299 return offset->share_owned_by(shared_from_this());
1300 else
1301 throw Error(SR_ERR_NA);
1302}
1303
15bebf57 1304const Quantity *Analog::mq() const
c23c8659 1305{
dd13d47a 1306 return Quantity::get(_structure->meaning->mq);
c23c8659
ML
1307}
1308
15bebf57 1309const Unit *Analog::unit() const
c23c8659 1310{
dd13d47a 1311 return Unit::get(_structure->meaning->unit);
c23c8659
ML
1312}
1313
15bebf57 1314vector<const QuantityFlag *> Analog::mq_flags() const
c23c8659 1315{
dd13d47a 1316 return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
c23c8659
ML
1317}
1318
6ad2fbaa
SA
1319shared_ptr<Logic> Analog::get_logic_via_threshold(float threshold,
1320 uint8_t *data_ptr) const
1321{
1322 auto datafeed = g_new(struct sr_datafeed_logic, 1);
1323 datafeed->length = num_samples();
1324 datafeed->unitsize = 1;
1325
1326 if (data_ptr)
1327 datafeed->data = data_ptr;
1328 else
1329 datafeed->data = g_malloc(datafeed->length);
1330
1331 shared_ptr<Logic> logic =
1332 shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
1333
1334 check(sr_a2l_threshold(_structure, threshold,
1335 (uint8_t*)datafeed->data, datafeed->length));
1336
1337 return logic;
1338}
1339
1340shared_ptr<Logic> Analog::get_logic_via_schmitt_trigger(float lo_thr,
1341 float hi_thr, uint8_t *state, uint8_t *data_ptr) const
1342{
1343 auto datafeed = g_new(struct sr_datafeed_logic, 1);
1344 datafeed->length = num_samples();
1345 datafeed->unitsize = 1;
1346
1347 if (data_ptr)
1348 datafeed->data = data_ptr;
1349 else
1350 datafeed->data = g_malloc(datafeed->length);
1351
1352 shared_ptr<Logic> logic =
1353 shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
1354
1355 check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state,
1356 (uint8_t*)datafeed->data, datafeed->length));
1357
1358 return logic;
1359}
1360
0cee3a3e
SA
1361Rational::Rational(const struct sr_rational *structure) :
1362 _structure(structure)
1363{
1364}
1365
1366Rational::~Rational()
1367{
1368}
1369
1370shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent)
1371{
1372 return static_pointer_cast<Rational>(
1373 ParentOwned::share_owned_by(_parent));
1374}
1375
1376int64_t Rational::numerator() const
1377{
1378 return _structure->p;
1379}
1380
1381uint64_t Rational::denominator() const
1382{
1383 return _structure->q;
1384}
1385
1386float Rational::value() const
1387{
1388 return (float)(_structure->p) / (float)(_structure->q);
1389}
1390
ca3291e3 1391InputFormat::InputFormat(const struct sr_input_module *structure) :
d5d7b09e 1392 _structure(structure)
c23c8659
ML
1393{
1394}
1395
1396InputFormat::~InputFormat()
1397{
1398}
1399
15bebf57 1400string InputFormat::name() const
c23c8659 1401{
3b161085 1402 return valid_string(sr_input_id_get(_structure));
c23c8659
ML
1403}
1404
15bebf57 1405string InputFormat::description() const
c23c8659 1406{
3b161085 1407 return valid_string(sr_input_description_get(_structure));
ca3291e3
ML
1408}
1409
15bebf57 1410vector<string> InputFormat::extensions() const
c7bc82ff
JH
1411{
1412 vector<string> exts;
1413 for (const char *const *e = sr_input_extensions_get(_structure);
1414 e && *e; e++)
1415 exts.push_back(*e);
1416 return exts;
1417}
1418
3b161085 1419map<string, shared_ptr<Option>> InputFormat::options()
43942280 1420{
43942280 1421 map<string, shared_ptr<Option>> result;
a98729a7 1422
e7eb2968 1423 if (const struct sr_option **options = sr_input_options_get(_structure)) {
a98729a7
DE
1424 shared_ptr<const struct sr_option *> option_array
1425 {options, &sr_input_options_free};
1426 for (int i = 0; options[i]; i++) {
1427 shared_ptr<Option> opt {
1428 new Option{options[i], option_array},
1429 default_delete<Option>{}};
b605edf3 1430 result.emplace(opt->id(), move(opt));
a98729a7 1431 }
48d92e2c 1432 }
43942280
ML
1433 return result;
1434}
1435
ca3291e3 1436shared_ptr<Input> InputFormat::create_input(
9e7176bd 1437 map<string, Glib::VariantBase> options)
ca3291e3 1438{
3b161085 1439 auto input = sr_input_new(_structure, map_to_hash_variant(options));
ca3291e3
ML
1440 if (!input)
1441 throw Error(SR_ERR_ARG);
a98729a7 1442 return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}};
c23c8659
ML
1443}
1444
ca3291e3 1445Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
d5d7b09e 1446 _structure(structure),
f17b4546 1447 _context(move(context))
c23c8659 1448{
c23c8659
ML
1449}
1450
3b161085 1451shared_ptr<InputDevice> Input::device()
c23c8659 1452{
e7eb2968 1453 if (!_device) {
3b161085 1454 auto sdi = sr_input_dev_inst_get(_structure);
ca3291e3
ML
1455 if (!sdi)
1456 throw Error(SR_ERR_NA);
f17b4546 1457 _device.reset(new InputDevice{shared_from_this(), sdi});
ca3291e3 1458 }
c23c8659 1459
b6ab954d 1460 return _device->share_owned_by(shared_from_this());
ca3291e3 1461}
c23c8659 1462
2b51d48b 1463void Input::send(void *data, size_t length)
ca3291e3 1464{
ce3e1e61 1465 auto gstr = g_string_new_len(static_cast<char *>(data), length);
3b161085 1466 auto ret = sr_input_send(_structure, gstr);
e3e1f20c 1467 g_string_free(gstr, true);
ca3291e3 1468 check(ret);
c23c8659
ML
1469}
1470
9c51e8ec
ML
1471void Input::end()
1472{
1473 check(sr_input_end(_structure));
1474}
1475
b6b4f03e
SA
1476void Input::reset()
1477{
1478 check(sr_input_reset(_structure));
1479}
1480
ca3291e3 1481Input::~Input()
c23c8659 1482{
9c51e8ec 1483 sr_input_free(_structure);
c23c8659
ML
1484}
1485
6e5240f4
ML
1486InputDevice::InputDevice(shared_ptr<Input> input,
1487 struct sr_dev_inst *structure) :
6e5240f4 1488 Device(structure),
d370545d 1489 _input(move(input))
c23c8659 1490{
c23c8659
ML
1491}
1492
ca3291e3 1493InputDevice::~InputDevice()
c23c8659 1494{
c23c8659
ML
1495}
1496
d01d2314
ML
1497shared_ptr<Device> InputDevice::get_shared_from_this()
1498{
bf52cc8c 1499 return static_pointer_cast<Device>(shared_from_this());
d01d2314
ML
1500}
1501
58aa1f83 1502Option::Option(const struct sr_option *structure,
70d3b20b 1503 shared_ptr<const struct sr_option *> structure_array) :
d5d7b09e 1504 _structure(structure),
d370545d 1505 _structure_array(move(structure_array))
58aa1f83
ML
1506{
1507}
1508
1509Option::~Option()
1510{
1511}
1512
15bebf57 1513string Option::id() const
58aa1f83 1514{
3b161085 1515 return valid_string(_structure->id);
58aa1f83
ML
1516}
1517
15bebf57 1518string Option::name() const
58aa1f83 1519{
3b161085 1520 return valid_string(_structure->name);
58aa1f83
ML
1521}
1522
15bebf57 1523string Option::description() const
58aa1f83 1524{
3b161085 1525 return valid_string(_structure->desc);
58aa1f83
ML
1526}
1527
15bebf57 1528Glib::VariantBase Option::default_value() const
58aa1f83 1529{
3b161085 1530 return Glib::VariantBase(_structure->def, true);
58aa1f83
ML
1531}
1532
15bebf57 1533vector<Glib::VariantBase> Option::values() const
58aa1f83
ML
1534{
1535 vector<Glib::VariantBase> result;
ce3e1e61
DE
1536 for (auto l = _structure->values; l; l = l->next) {
1537 auto *const var = static_cast<GVariant *>(l->data);
1538 result.push_back(Glib::VariantBase(var, true));
1539 }
58aa1f83
ML
1540 return result;
1541}
1542
61a6d983
GS
1543Glib::VariantBase Option::parse_string(string value)
1544{
1545 enum sr_datatype dt;
1546 Glib::VariantBase dflt = default_value();
1547 GVariant *tmpl = dflt.gobj();
1548
1549 if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) {
1550 dt = SR_T_UINT64;
1551 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) {
1552 dt = SR_T_STRING;
1553 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) {
1554 dt = SR_T_BOOL;
1555 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) {
1556 dt = SR_T_FLOAT;
1557 } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) {
1558 dt = SR_T_INT32;
1559 } else {
1560 throw Error(SR_ERR_BUG);
1561 }
1562 return ConfigKey::parse_string(value, dt);
1563}
1564
58aa1f83 1565OutputFormat::OutputFormat(const struct sr_output_module *structure) :
d5d7b09e 1566 _structure(structure)
c23c8659
ML
1567{
1568}
1569
1570OutputFormat::~OutputFormat()
1571{
1572}
1573
15bebf57 1574string OutputFormat::name() const
c23c8659 1575{
3b161085 1576 return valid_string(sr_output_id_get(_structure));
c23c8659
ML
1577}
1578
15bebf57 1579string OutputFormat::description() const
c23c8659 1580{
3b161085 1581 return valid_string(sr_output_description_get(_structure));
58aa1f83
ML
1582}
1583
15bebf57 1584vector<string> OutputFormat::extensions() const
8a174d23
JH
1585{
1586 vector<string> exts;
1587 for (const char *const *e = sr_output_extensions_get(_structure);
1588 e && *e; e++)
1589 exts.push_back(*e);
1590 return exts;
1591}
1592
3b161085 1593map<string, shared_ptr<Option>> OutputFormat::options()
58aa1f83 1594{
58aa1f83 1595 map<string, shared_ptr<Option>> result;
a98729a7 1596
e7eb2968 1597 if (const struct sr_option **options = sr_output_options_get(_structure)) {
a98729a7
DE
1598 shared_ptr<const struct sr_option *> option_array
1599 {options, &sr_output_options_free};
1600 for (int i = 0; options[i]; i++) {
1601 shared_ptr<Option> opt {
1602 new Option{options[i], option_array},
1603 default_delete<Option>{}};
b605edf3 1604 result.emplace(opt->id(), move(opt));
a98729a7 1605 }
48d92e2c 1606 }
58aa1f83 1607 return result;
c23c8659
ML
1608}
1609
1610shared_ptr<Output> OutputFormat::create_output(
9e7176bd 1611 shared_ptr<Device> device, map<string, Glib::VariantBase> options)
c23c8659 1612{
a98729a7 1613 return shared_ptr<Output>{
9e7176bd 1614 new Output{shared_from_this(), move(device), move(options)},
a98729a7 1615 default_delete<Output>{}};
c23c8659
ML
1616}
1617
81b3ce37 1618shared_ptr<Output> OutputFormat::create_output(string filename,
9e7176bd 1619 shared_ptr<Device> device, map<string, Glib::VariantBase> options)
81b3ce37 1620{
a98729a7 1621 return shared_ptr<Output>{
9e7176bd 1622 new Output{move(filename), shared_from_this(), move(device), move(options)},
a98729a7 1623 default_delete<Output>{}};
81b3ce37
SA
1624}
1625
15bebf57 1626bool OutputFormat::test_flag(const OutputFlag *flag) const
3cd4b381
SA
1627{
1628 return sr_output_test_flag(_structure, flag->id());
1629}
1630
c23c8659 1631Output::Output(shared_ptr<OutputFormat> format,
9e7176bd 1632 shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
d5d7b09e 1633 _structure(sr_output_new(format->_structure,
58e21229 1634 map_to_hash_variant(options), device->_structure, nullptr)),
d370545d
DE
1635 _format(move(format)),
1636 _device(move(device)),
9e7176bd 1637 _options(move(options))
81b3ce37
SA
1638{
1639}
1640
1641Output::Output(string filename, shared_ptr<OutputFormat> format,
9e7176bd 1642 shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
d5d7b09e 1643 _structure(sr_output_new(format->_structure,
81b3ce37 1644 map_to_hash_variant(options), device->_structure, filename.c_str())),
d370545d
DE
1645 _format(move(format)),
1646 _device(move(device)),
9e7176bd 1647 _options(move(options))
c23c8659
ML
1648{
1649}
1650
1651Output::~Output()
1652{
3b161085 1653 check(sr_output_free(_structure));
c23c8659
ML
1654}
1655
1656string Output::receive(shared_ptr<Packet> packet)
1657{
1658 GString *out;
3b161085 1659 check(sr_output_send(_structure, packet->_structure, &out));
e7eb2968 1660 if (out) {
c23c8659
ML
1661 auto result = string(out->str, out->str + out->len);
1662 g_string_free(out, true);
1663 return result;
e7eb2968 1664 } else {
c23c8659
ML
1665 return string();
1666 }
1667}
1668
b5f07319 1669#include <enums.cpp>
c23c8659
ML
1670
1671}