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