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