]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/classes.cpp
Update bindings for new input API.
[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 #include "libsigrok/libsigrok.hpp"
21
22 #include <sstream>
23
24 namespace sigrok
25 {
26
27 /** Helper function to translate C errors to C++ exceptions. */
28 static void check(int result)
29 {
30         if (result != SR_OK)
31                 throw Error(result);
32 }
33
34 /** Helper function to obtain valid strings from possibly null input. */
35 static const char *valid_string(const char *input)
36 {
37         if (input != NULL)
38                 return input;
39         else
40                 return "";
41 }
42
43 /** Helper function to convert between map<string, VariantBase> and GHashTable */
44 static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input)
45 {
46         auto output = g_hash_table_new_full(
47                 g_variant_hash, g_variant_equal, g_free,
48                 (void (*)(void *))g_variant_unref);
49         for (auto entry : input)
50                 g_hash_table_insert(output,
51                         g_strdup(entry.first.c_str()),
52                         entry.second.gobj_copy());
53     return output;
54 }
55
56 Error::Error(int result) : result(result)
57 {
58 }
59
60 const char *Error::what() const throw()
61 {
62         return sr_strerror(result);
63 }
64
65 Error::~Error() throw()
66 {
67 }
68
69 shared_ptr<Context> Context::create()
70 {
71         return shared_ptr<Context>(new Context(), Context::Deleter());
72 }
73
74 Context::Context() :
75         session(NULL)
76 {
77         check(sr_init(&structure));
78         struct sr_dev_driver **driver_list = sr_driver_list();
79         if (driver_list)
80                 for (int i = 0; driver_list[i]; i++)
81                         drivers[driver_list[i]->name] =
82                                 new Driver(driver_list[i]);
83         const struct sr_input_module **input_list = sr_input_list();
84         if (input_list)
85                 for (int i = 0; input_list[i]; i++)
86                         input_formats[sr_input_id_get(input_list[i])] =
87                                 new InputFormat(input_list[i]);
88         const struct sr_output_module **output_list = sr_output_list();
89         if (output_list)
90                 for (int i = 0; output_list[i]; i++)
91                         output_formats[sr_output_id_get(output_list[i])] =
92                                 new OutputFormat(output_list[i]);
93 }
94
95 string Context::get_package_version()
96 {
97         return sr_package_version_string_get();
98 }
99
100 string Context::get_lib_version()
101 {
102         return sr_lib_version_string_get();
103 }
104
105 map<string, shared_ptr<Driver>> Context::get_drivers()
106 {
107         map<string, shared_ptr<Driver>> result;
108         for (auto entry: drivers)
109         {
110                 auto name = entry.first;
111                 auto driver = entry.second;
112                 result[name] = static_pointer_cast<Driver>(
113                         driver->get_shared_pointer(this));
114         }
115         return result;
116 }
117
118 map<string, shared_ptr<InputFormat>> Context::get_input_formats()
119 {
120         map<string, shared_ptr<InputFormat>> result;
121         for (auto entry: input_formats)
122         {
123                 auto name = entry.first;
124                 auto input_format = entry.second;
125                 result[name] = static_pointer_cast<InputFormat>(
126                         input_format->get_shared_pointer(this));
127         }
128         return result;
129 }
130
131 map<string, shared_ptr<OutputFormat>> Context::get_output_formats()
132 {
133         map<string, shared_ptr<OutputFormat>> result;
134         for (auto entry: output_formats)
135         {
136                 auto name = entry.first;
137                 auto output_format = entry.second;
138                 result[name] = static_pointer_cast<OutputFormat>(
139                         output_format->get_shared_pointer(this));
140         }
141         return result;
142 }
143
144 Context::~Context()
145 {
146         for (auto entry : drivers)
147                 delete entry.second;
148         for (auto entry : input_formats)
149                 delete entry.second;
150         for (auto entry : output_formats)
151                 delete entry.second;
152         check(sr_exit(structure));
153 }
154
155 const LogLevel *Context::get_log_level()
156 {
157         return LogLevel::get(sr_log_loglevel_get());
158 }
159
160 void Context::set_log_level(const LogLevel *level)
161 {
162         check(sr_log_loglevel_set(level->get_id()));
163 }
164
165 string Context::get_log_domain()
166 {
167         return valid_string(sr_log_logdomain_get());
168 }
169
170 void Context::set_log_domain(string value)
171 {
172         check(sr_log_logdomain_set(value.c_str()));
173 }
174
175 static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args)
176 {
177         va_list args_copy;
178         va_copy(args_copy, args);
179         int length = vsnprintf(NULL, 0, format, args_copy);
180         va_end(args_copy);
181         char *buf = (char *) g_malloc(length + 1);
182         vsprintf(buf, format, args);
183         string message(buf, length);
184         g_free(buf);
185
186         LogCallbackFunction callback = *((LogCallbackFunction *) cb_data);
187
188         try
189         {
190                 callback(LogLevel::get(loglevel), message);
191         }
192         catch (Error e)
193         {
194                 return e.result;
195         }
196
197         return SR_OK;
198 }
199
200 void Context::set_log_callback(LogCallbackFunction callback)
201 {
202         log_callback = callback;
203         check(sr_log_callback_set(call_log_callback, &log_callback));
204
205
206 void Context::set_log_callback_default()
207 {
208         check(sr_log_callback_set_default());
209         log_callback = nullptr;
210
211
212 shared_ptr<Session> Context::create_session()
213 {
214         return shared_ptr<Session>(
215                 new Session(shared_from_this()), Session::Deleter());
216 }
217
218 shared_ptr<Session> Context::load_session(string filename)
219 {
220         return shared_ptr<Session>(
221                 new Session(shared_from_this(), filename), Session::Deleter());
222 }
223
224 shared_ptr<Trigger> Context::create_trigger(string name)
225 {
226         return shared_ptr<Trigger>(
227                 new Trigger(shared_from_this(), name), Trigger::Deleter());
228 }
229
230 shared_ptr<Input> Context::open_file(string filename)
231 {
232         auto input = sr_input_scan_file(filename.c_str());
233         if (!input)
234                 throw Error(SR_ERR_NA);
235         return shared_ptr<Input>(
236                 new Input(shared_from_this(), input), Input::Deleter());
237 }
238
239 shared_ptr<Input> Context::open_stream(string header)
240 {
241         auto gstr = g_string_new(header.c_str());
242         auto input = sr_input_scan_buffer(gstr);
243         g_string_free(gstr, false);
244         if (!input)
245                 throw Error(SR_ERR_NA);
246         return shared_ptr<Input>(
247                 new Input(shared_from_this(), input), Input::Deleter());
248 }
249
250 Driver::Driver(struct sr_dev_driver *structure) :
251         StructureWrapper<Context, struct sr_dev_driver>(structure),
252         initialized(false)
253 {
254 }
255
256 Driver::~Driver()
257 {
258         for (auto device : devices)
259                 delete device;
260 }
261
262 string Driver::get_name()
263 {
264         return valid_string(structure->name);
265 }
266
267 string Driver::get_long_name()
268 {
269         return valid_string(structure->longname);
270 }
271
272 vector<shared_ptr<HardwareDevice>> Driver::scan(
273         map<const ConfigKey *, Glib::VariantBase> options)
274 {
275         /* Initialise the driver if not yet done. */
276         if (!initialized)
277         {
278                 check(sr_driver_init(parent->structure, structure));
279                 initialized = true;
280         }
281
282         /* Clear all existing instances. */
283         for (auto device : devices)
284                 delete device;
285         devices.clear();
286
287         /* Translate scan options to GSList of struct sr_config pointers. */
288         GSList *option_list = NULL;
289         for (auto entry : options)
290         {
291                 auto key = entry.first;
292                 auto value = entry.second;
293                 auto config = g_new(struct sr_config, 1);
294                 config->key = key->get_id();
295                 config->data = value.gobj();
296                 option_list = g_slist_append(option_list, config);
297         }
298
299         /* Run scan. */
300         GSList *device_list = sr_driver_scan(structure, option_list);
301
302         /* Free option list. */
303         g_slist_free_full(option_list, g_free);
304
305         /* Create device objects. */
306         for (GSList *device = device_list; device; device = device->next)
307         {
308                 auto sdi = (struct sr_dev_inst *) device->data;
309                 devices.push_back(new HardwareDevice(this, sdi));
310         }
311
312         /* Free GSList returned from scan. */
313         g_slist_free(device_list);
314
315         /* Create list of shared pointers to device instances for return. */
316         vector<shared_ptr<HardwareDevice>> result;
317         for (auto device : devices)
318                 result.push_back(static_pointer_cast<HardwareDevice>(
319                         device->get_shared_pointer(parent)));
320         return result;
321 }
322
323 Configurable::Configurable(
324                 struct sr_dev_driver *driver,
325                 struct sr_dev_inst *sdi,
326                 struct sr_channel_group *cg) :
327         config_driver(driver),
328         config_sdi(sdi),
329         config_channel_group(cg)
330 {
331 }
332
333 Configurable::~Configurable()
334 {
335 }
336
337 Glib::VariantBase Configurable::config_get(const ConfigKey *key)
338 {
339         GVariant *data;
340         check(sr_config_get(
341                 config_driver, config_sdi, config_channel_group,
342                 key->get_id(), &data));
343         return Glib::VariantBase(data);
344 }
345
346 void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
347 {
348         check(sr_config_set(
349                 config_sdi, config_channel_group,
350                 key->get_id(), value.gobj()));
351 }
352
353 Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
354 {
355         GVariant *data;
356         check(sr_config_list(
357                 config_driver, config_sdi, config_channel_group,
358                 key->get_id(), &data));
359         return Glib::VariantContainerBase(data);
360 }
361
362 Device::Device(struct sr_dev_inst *structure) :
363         Configurable(structure->driver, structure, NULL),
364         StructureWrapper<Context, struct sr_dev_inst>(structure)
365 {
366         for (GSList *entry = structure->channels; entry; entry = entry->next)
367         {
368                 auto channel = (struct sr_channel *) entry->data;
369                 channels[channel] = new Channel(channel);
370         }
371
372         for (GSList *entry = structure->channel_groups; entry; entry = entry->next)
373         {
374                 auto group = (struct sr_channel_group *) entry->data;
375                 channel_groups[group->name] = new ChannelGroup(this, group);
376         }
377 }
378
379 Device::~Device()
380 {
381         for (auto entry : channels)
382                 delete entry.second;
383         for (auto entry : channel_groups)
384                 delete entry.second;
385 }
386
387 string Device::get_description()
388 {
389         ostringstream s;
390
391         vector<string> parts =
392                 {get_vendor(), get_model(), get_version()};
393
394         for (string part : parts)
395                 if (part.length() > 0)
396                         s << part;
397
398         return s.str();
399 }
400
401 string Device::get_vendor()
402 {
403         return valid_string(structure->vendor);
404 }
405
406 string Device::get_model()
407 {
408         return valid_string(structure->model);
409 }
410
411 string Device::get_version()
412 {
413         return valid_string(structure->version);
414 }
415
416 vector<shared_ptr<Channel>> Device::get_channels()
417 {
418         vector<shared_ptr<Channel>> result;
419         for (auto entry : channels)
420                 result.push_back(static_pointer_cast<Channel>(
421                         entry.second->get_shared_pointer(this)));
422         return result;
423 }
424
425 shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
426 {
427         return static_pointer_cast<Channel>(
428                 channels[ptr]->get_shared_pointer(this));
429 }
430
431 map<string, shared_ptr<ChannelGroup>>
432 Device::get_channel_groups()
433 {
434         map<string, shared_ptr<ChannelGroup>> result;
435         for (auto entry: channel_groups)
436         {
437                 auto name = entry.first;
438                 auto channel_group = entry.second;
439                 result[name] = static_pointer_cast<ChannelGroup>(
440                         channel_group->get_shared_pointer(this));
441         }
442         return result;
443 }
444
445 void Device::open()
446 {
447         check(sr_dev_open(structure));
448 }
449
450 void Device::close()
451 {
452         check(sr_dev_close(structure));
453 }
454
455 HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
456         Device(structure),
457         driver(driver)
458 {
459 }
460
461 HardwareDevice::~HardwareDevice()
462 {
463 }
464
465 shared_ptr<Driver> HardwareDevice::get_driver()
466 {
467         return static_pointer_cast<Driver>(driver->get_shared_pointer(parent));
468 }
469
470 Channel::Channel(struct sr_channel *structure) :
471         StructureWrapper<Device, struct sr_channel>(structure),
472         type(ChannelType::get(structure->type))
473 {
474 }
475
476 Channel::~Channel()
477 {
478 }
479
480 string Channel::get_name()
481 {
482         return valid_string(structure->name);
483 }
484
485 void Channel::set_name(string name)
486 {
487         check(sr_dev_channel_name_set(parent->structure, structure->index, name.c_str()));
488 }
489
490 const ChannelType *Channel::get_type()
491 {
492         return ChannelType::get(structure->type);
493 }
494
495 bool Channel::get_enabled()
496 {
497         return structure->enabled;
498 }
499
500 void Channel::set_enabled(bool value)
501 {
502         check(sr_dev_channel_enable(parent->structure, structure->index, value));
503 }
504
505 unsigned int Channel::get_index()
506 {
507         return structure->index;
508 }
509
510 ChannelGroup::ChannelGroup(Device *device,
511                 struct sr_channel_group *structure) :
512         StructureWrapper<Device, struct sr_channel_group>(structure),
513         Configurable(device->structure->driver, device->structure, structure)
514 {
515         for (GSList *entry = structure->channels; entry; entry = entry->next)
516                 channels.push_back(device->channels[(struct sr_channel *)entry->data]);
517 }
518
519 ChannelGroup::~ChannelGroup()
520 {
521 }
522
523 string ChannelGroup::get_name()
524 {
525         return valid_string(structure->name);
526 }
527
528 vector<shared_ptr<Channel>> ChannelGroup::get_channels()
529 {
530         vector<shared_ptr<Channel>> result;
531         for (auto channel : channels)
532                 result.push_back(static_pointer_cast<Channel>(
533                         channel->get_shared_pointer(parent)));
534         return result;
535 }
536
537 Trigger::Trigger(shared_ptr<Context> context, string name) : 
538         structure(sr_trigger_new(name.c_str())), context(context)
539 {
540         for (auto stage = structure->stages; stage; stage = stage->next)
541                 stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data));
542 }
543
544 Trigger::~Trigger()
545 {
546         for (auto stage: stages)
547                 delete stage;
548
549         sr_trigger_free(structure);
550 }
551
552 string Trigger::get_name()
553 {
554         return structure->name;
555 }
556
557 vector<shared_ptr<TriggerStage>> Trigger::get_stages()
558 {
559         vector<shared_ptr<TriggerStage>> result;
560         for (auto stage : stages)
561                 result.push_back(static_pointer_cast<TriggerStage>(
562                         stage->get_shared_pointer(this)));
563         return result;
564 }
565
566 shared_ptr<TriggerStage> Trigger::add_stage()
567 {
568         auto stage = new TriggerStage(sr_trigger_stage_add(structure));
569         stages.push_back(stage);
570         return static_pointer_cast<TriggerStage>(
571                 stage->get_shared_pointer(this));
572 }
573
574 TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : 
575         StructureWrapper<Trigger, struct sr_trigger_stage>(structure)
576 {
577 }
578
579 TriggerStage::~TriggerStage()
580 {
581         for (auto match : matches)
582                 delete match;
583 }
584         
585 int TriggerStage::get_number()
586 {
587         return structure->stage;
588 }
589
590 vector<shared_ptr<TriggerMatch>> TriggerStage::get_matches()
591 {
592         vector<shared_ptr<TriggerMatch>> result;
593         for (auto match : matches)
594                 result.push_back(static_pointer_cast<TriggerMatch>(
595                         match->get_shared_pointer(this)));
596         return result;
597 }
598
599 void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value)
600 {
601         check(sr_trigger_match_add(structure, channel->structure, type->get_id(), value));
602         matches.push_back(new TriggerMatch(
603                 (struct sr_trigger_match *) g_slist_last(structure->matches)->data, channel));
604 }
605
606 void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type)
607 {
608         add_match(channel, type, NAN);
609 }
610
611 TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel) :
612         StructureWrapper<TriggerStage, struct sr_trigger_match>(structure), channel(channel)
613 {
614 }
615
616 TriggerMatch::~TriggerMatch()
617 {
618 }
619
620 shared_ptr<Channel> TriggerMatch::get_channel()
621 {
622         return channel;
623 }
624
625 const TriggerMatchType *TriggerMatch::get_type()
626 {
627         return TriggerMatchType::get(structure->match);
628 }
629
630 float TriggerMatch::get_value()
631 {
632         return structure->value;
633 }
634
635 DatafeedCallbackData::DatafeedCallbackData(Session *session,
636                 DatafeedCallbackFunction callback) :
637         callback(callback), session(session)
638 {
639 }
640
641 void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
642         const struct sr_datafeed_packet *pkt)
643 {
644         auto device = session->devices[sdi];
645         auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
646         callback(device, packet);
647 }
648
649 SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) :
650         source(source)
651 {
652 }
653
654 bool SourceCallbackData::run(int revents)
655 {
656         return source->callback((Glib::IOCondition) revents);
657 }
658
659 shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events,
660         int timeout, SourceCallbackFunction callback)
661 {
662         auto result = new EventSource(timeout, callback);
663         result->type = EventSource::SOURCE_FD;
664         result->fd = fd;
665         result->events = events;
666         return shared_ptr<EventSource>(result, EventSource::Deleter());
667 }
668
669 shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout,
670         SourceCallbackFunction callback)
671 {
672         auto result = new EventSource(timeout, callback);
673         result->type = EventSource::SOURCE_POLLFD;
674         result->pollfd = pollfd;
675         return shared_ptr<EventSource>(result, EventSource::Deleter());
676 }
677
678 shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel,
679         Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
680 {
681         auto result = new EventSource(timeout, callback);
682         result->type = EventSource::SOURCE_IOCHANNEL;
683         result->channel = channel;
684         result->events = events;
685         return shared_ptr<EventSource>(result, EventSource::Deleter());
686 }
687
688 EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
689         timeout(timeout), callback(callback)
690 {
691 }
692
693 EventSource::~EventSource()
694 {
695 }
696
697 Session::Session(shared_ptr<Context> context) :
698         context(context), saving(false)
699 {
700         check(sr_session_new(&structure));
701         context->session = this;
702 }
703
704 Session::Session(shared_ptr<Context> context, string filename) :
705         context(context), saving(false)
706 {
707         check(sr_session_load(filename.c_str(), &structure));
708         context->session = this;
709 }
710
711 Session::~Session()
712 {
713         check(sr_session_destroy(structure));
714
715         for (auto callback : datafeed_callbacks)
716                 delete callback;
717
718         for (auto entry : source_callbacks)
719                 delete entry.second;
720 }
721
722 void Session::add_device(shared_ptr<Device> device)
723 {
724         check(sr_session_dev_add(structure, device->structure));
725         devices[device->structure] = device;
726 }
727
728 vector<shared_ptr<Device>> Session::get_devices()
729 {
730         GSList *dev_list;
731         check(sr_session_dev_list(structure, &dev_list));
732         vector<shared_ptr<Device>> result;
733         for (GSList *dev = dev_list; dev; dev = dev->next)
734         {
735                 auto sdi = (struct sr_dev_inst *) dev->data;
736                 if (devices.count(sdi) == 0)
737                         devices[sdi] = shared_ptr<Device>(
738                                 new Device(sdi), Device::Deleter());
739                 result.push_back(devices[sdi]);
740         }
741         return result;
742 }
743
744 void Session::remove_devices()
745 {
746         devices.clear();
747         check(sr_session_dev_remove_all(structure));
748 }
749
750 void Session::start()
751 {
752         check(sr_session_start(structure));
753 }
754
755 void Session::run()
756 {
757         check(sr_session_run(structure));
758 }
759
760 void Session::stop()
761 {
762         check(sr_session_stop(structure));
763 }
764
765 void Session::begin_save(string filename)
766 {
767         saving = true;
768         save_initialized = false;
769         save_filename = filename;
770         save_samplerate = 0;
771 }
772
773 void Session::append(shared_ptr<Packet> packet)
774 {
775         if (!saving)
776                 throw Error(SR_ERR);
777
778         switch (packet->structure->type)
779         {
780                 case SR_DF_META:
781                 {
782                         auto meta = (const struct sr_datafeed_meta *)
783                                 packet->structure->payload;
784
785                         for (auto l = meta->config; l; l = l->next)
786                         {
787                                 auto config = (struct sr_config *) l->data;
788                                 if (config->key == SR_CONF_SAMPLERATE)
789                                         save_samplerate = g_variant_get_uint64(config->data);
790                         }
791
792                         break;
793                 }
794                 case SR_DF_LOGIC:
795                 {
796                         if (save_samplerate == 0)
797                         {
798                                 GVariant *samplerate;
799
800                                 check(sr_config_get(packet->device->structure->driver,
801                                         packet->device->structure, NULL, SR_CONF_SAMPLERATE,
802                                         &samplerate));
803
804                                 save_samplerate = g_variant_get_uint64(samplerate);
805
806                                 g_variant_unref(samplerate);
807                         }
808
809                         if (!save_initialized)
810                         {
811                                 vector<shared_ptr<Channel>> save_channels;
812
813                                 for (auto channel : packet->device->get_channels())
814                                         if (channel->structure->enabled &&
815                                                         channel->structure->type == SR_CHANNEL_LOGIC)
816                                                 save_channels.push_back(channel);
817
818                                 auto channels = g_new(char *, save_channels.size());
819
820                                 int i = 0;
821                                 for (auto channel : save_channels)
822                                                 channels[i++] = channel->structure->name;
823                                 channels[i] = NULL;
824
825                                 int ret = sr_session_save_init(structure, save_filename.c_str(),
826                                                 save_samplerate, channels);
827
828                                 g_free(channels);
829
830                                 if (ret != SR_OK)
831                                         throw Error(ret);
832
833                                 save_initialized = true;
834                         }
835
836                         auto logic = (const struct sr_datafeed_logic *)
837                                 packet->structure->payload;
838
839                         check(sr_session_append(structure, save_filename.c_str(),
840                                 (uint8_t *) logic->data, logic->unitsize,
841                                 logic->length / logic->unitsize));
842                 }
843         }
844 }
845
846 void Session::append(void *data, size_t length, unsigned int unit_size)
847 {
848         check(sr_session_append(structure, save_filename.c_str(),
849                 (uint8_t *) data, unit_size, length));
850 }
851
852 static void datafeed_callback(const struct sr_dev_inst *sdi,
853         const struct sr_datafeed_packet *pkt, void *cb_data)
854 {
855         auto callback = static_cast<DatafeedCallbackData *>(cb_data);
856         callback->run(sdi, pkt);
857 }
858         
859 void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
860 {
861         auto cb_data = new DatafeedCallbackData(this, callback);
862         check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data));
863         datafeed_callbacks.push_back(cb_data);
864 }
865
866 void Session::remove_datafeed_callbacks(void)
867 {
868         check(sr_session_datafeed_callback_remove_all(structure));
869         for (auto callback : datafeed_callbacks)
870                 delete callback;
871         datafeed_callbacks.clear();
872 }
873
874 static int source_callback(int fd, int revents, void *cb_data)
875 {
876         (void) fd;
877         auto callback = (SourceCallbackData *) cb_data;
878         return callback->run(revents);
879 }
880
881 void Session::add_source(shared_ptr<EventSource> source)
882 {
883         if (source_callbacks.count(source) == 1)
884                 throw Error(SR_ERR_ARG);
885
886         auto cb_data = new SourceCallbackData(source);
887
888         switch (source->type)
889         {
890                 case EventSource::SOURCE_FD:
891                         check(sr_session_source_add(structure, source->fd, source->events,
892                                 source->timeout, source_callback, cb_data));
893                         break;
894                 case EventSource::SOURCE_POLLFD:
895                         check(sr_session_source_add_pollfd(structure,
896                                 source->pollfd.gobj(), source->timeout, source_callback,
897                                 cb_data));
898                         break;
899                 case EventSource::SOURCE_IOCHANNEL:
900                         check(sr_session_source_add_channel(structure,
901                                 source->channel->gobj(), source->events, source->timeout,
902                                 source_callback, cb_data));
903                         break;
904         }
905
906         source_callbacks[source] = cb_data;
907 }
908
909 void Session::remove_source(shared_ptr<EventSource> source)
910 {
911         if (source_callbacks.count(source) == 0)
912                 throw Error(SR_ERR_ARG);
913
914         switch (source->type)
915         {
916                 case EventSource::SOURCE_FD:
917                         check(sr_session_source_remove(structure, source->fd));
918                         break;
919                 case EventSource::SOURCE_POLLFD:
920                         check(sr_session_source_remove_pollfd(structure,
921                                 source->pollfd.gobj()));
922                         break;
923                 case EventSource::SOURCE_IOCHANNEL:
924                         check(sr_session_source_remove_channel(structure,
925                                 source->channel->gobj()));
926                         break;
927         }
928
929         delete source_callbacks[source];
930
931         source_callbacks.erase(source);
932 }
933
934 shared_ptr<Trigger> Session::get_trigger()
935 {
936         return trigger;
937 }
938
939 void Session::set_trigger(shared_ptr<Trigger> trigger)
940 {
941         check(sr_session_trigger_set(structure, trigger->structure));
942         this->trigger = trigger;
943 }
944
945 Packet::Packet(shared_ptr<Device> device,
946         const struct sr_datafeed_packet *structure) :
947         structure(structure),
948         device(device)
949 {
950         switch (structure->type)
951         {
952                 case SR_DF_HEADER:
953                         payload = new Header(
954                                 static_cast<const struct sr_datafeed_header *>(
955                                         structure->payload));
956                         break;
957                 case SR_DF_META:
958                         payload = new Meta(
959                                 static_cast<const struct sr_datafeed_meta *>(
960                                         structure->payload));
961                         break;
962                 case SR_DF_LOGIC:
963                         payload = new Logic(
964                                 static_cast<const struct sr_datafeed_logic *>(
965                                         structure->payload));
966                         break;
967                 case SR_DF_ANALOG:
968                         payload = new Analog(
969                                 static_cast<const struct sr_datafeed_analog *>(
970                                         structure->payload));
971                         break;
972         }
973 }
974
975 Packet::~Packet()
976 {
977         if (payload)
978                 delete payload;
979 }
980
981 const PacketType *Packet::get_type()
982 {
983         return PacketType::get(structure->type);
984 }
985
986 shared_ptr<PacketPayload> Packet::get_payload()
987 {
988         return payload->get_shared_pointer(this);
989 }
990
991 PacketPayload::PacketPayload()
992 {
993 }
994
995 PacketPayload::~PacketPayload()
996 {
997 }
998
999 Header::Header(const struct sr_datafeed_header *structure) :
1000         PacketPayload(),
1001         StructureWrapper<Packet, const struct sr_datafeed_header>(structure)
1002 {
1003 }
1004
1005 Header::~Header()
1006 {
1007 }
1008
1009 int Header::get_feed_version()
1010 {
1011         return structure->feed_version;
1012 }
1013
1014 Glib::TimeVal Header::get_start_time()
1015 {
1016         return Glib::TimeVal(
1017                 structure->starttime.tv_sec,
1018                 structure->starttime.tv_usec);
1019 }
1020
1021 Meta::Meta(const struct sr_datafeed_meta *structure) :
1022         PacketPayload(),
1023         StructureWrapper<Packet, const struct sr_datafeed_meta>(structure)
1024 {
1025 }
1026
1027 Meta::~Meta()
1028 {
1029 }
1030
1031 map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
1032 {
1033         map<const ConfigKey *, Glib::VariantBase> result;
1034         for (auto l = structure->config; l; l = l->next)
1035         {
1036                 auto config = (struct sr_config *) l->data;
1037                 result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data);
1038         }
1039         return result;
1040 }
1041
1042 Logic::Logic(const struct sr_datafeed_logic *structure) :
1043         PacketPayload(),
1044         StructureWrapper<Packet, const struct sr_datafeed_logic>(structure)
1045 {
1046 }
1047
1048 Logic::~Logic()
1049 {
1050 }
1051
1052 void *Logic::get_data_pointer()
1053 {
1054         return structure->data;
1055 }
1056
1057 size_t Logic::get_data_length()
1058 {
1059         return structure->length;
1060 }
1061
1062 unsigned int Logic::get_unit_size()
1063 {
1064         return structure->unitsize;
1065 }
1066
1067 Analog::Analog(const struct sr_datafeed_analog *structure) :
1068         PacketPayload(),
1069         StructureWrapper<Packet, const struct sr_datafeed_analog>(structure)
1070 {
1071 }
1072
1073 Analog::~Analog()
1074 {
1075 }
1076
1077 float *Analog::get_data_pointer()
1078 {
1079         return structure->data;
1080 }
1081
1082 unsigned int Analog::get_num_samples()
1083 {
1084         return structure->num_samples;
1085 }
1086
1087 vector<shared_ptr<Channel>> Analog::get_channels()
1088 {
1089         vector<shared_ptr<Channel>> result;
1090         for (auto l = structure->channels; l; l = l->next)
1091                 result.push_back(parent->device->get_channel(
1092                         (struct sr_channel *)l->data));
1093         return result;
1094 }
1095
1096 const Quantity *Analog::get_mq()
1097 {
1098         return Quantity::get(structure->mq);
1099 }
1100
1101 const Unit *Analog::get_unit()
1102 {
1103         return Unit::get(structure->unit);
1104 }
1105
1106 vector<const QuantityFlag *> Analog::get_mq_flags()
1107 {
1108         return QuantityFlag::flags_from_mask(structure->mqflags);
1109 }
1110
1111 InputFormat::InputFormat(const struct sr_input_module *structure) :
1112         StructureWrapper<Context, const struct sr_input_module>(structure)
1113 {
1114 }
1115
1116 InputFormat::~InputFormat()
1117 {
1118 }
1119
1120 string InputFormat::get_name()
1121 {
1122         return valid_string(sr_input_id_get(structure));
1123 }
1124
1125 string InputFormat::get_description()
1126 {
1127         return valid_string(sr_input_description_get(structure));
1128 }
1129
1130 shared_ptr<Input> InputFormat::create_input(
1131         map<string, Glib::VariantBase> options)
1132 {
1133         auto input = sr_input_new(structure, map_to_hash_variant(options));
1134         if (!input)
1135                 throw Error(SR_ERR_ARG);
1136         return shared_ptr<Input>(
1137                 new Input(parent->shared_from_this(), input), Input::Deleter());
1138 }
1139
1140 Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
1141         structure(structure),
1142         context(context),
1143         device(nullptr)
1144 {
1145 }
1146
1147 shared_ptr<InputDevice> Input::get_device()
1148 {
1149         if (!device)
1150         {
1151                 auto sdi = sr_input_dev_inst_get(structure);
1152                 if (!sdi)
1153                         throw Error(SR_ERR_NA);
1154                 device = new InputDevice(shared_from_this(), sdi);
1155         }
1156
1157         return static_pointer_cast<InputDevice>(
1158                 device->get_shared_pointer(context->shared_from_this()));
1159 }
1160
1161 void Input::send(string data)
1162 {
1163         auto gstr = g_string_new(data.c_str());
1164         auto ret = sr_input_send(structure, gstr);
1165         g_string_free(gstr, false);
1166         check(ret);
1167 }
1168
1169 Input::~Input()
1170 {
1171         if (device)
1172                 delete device;
1173         check(sr_input_free(structure));
1174 }
1175
1176 InputDevice::InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi) :
1177         Device(sdi),
1178         input(input)
1179 {
1180 }
1181
1182 InputDevice::~InputDevice()
1183 {
1184 }
1185
1186 Option::Option(const struct sr_option *structure,
1187                 shared_ptr<const struct sr_option *> structure_array) :
1188         structure(structure),
1189         structure_array(structure_array)
1190 {
1191 }
1192
1193 Option::~Option()
1194 {
1195 }
1196
1197 string Option::get_id()
1198 {
1199         return valid_string(structure->id);
1200 }
1201
1202 string Option::get_name()
1203 {
1204         return valid_string(structure->name);
1205 }
1206
1207 string Option::get_description()
1208 {
1209         return valid_string(structure->desc);
1210 }
1211
1212 Glib::VariantBase Option::get_default_value()
1213 {
1214         return Glib::VariantBase(structure->def, true);
1215 }
1216
1217 vector<Glib::VariantBase> Option::get_values()
1218 {
1219         vector<Glib::VariantBase> result;
1220         for (auto l = structure->values; l; l = l->next)
1221                 result.push_back(Glib::VariantBase((GVariant *) l->data, true));
1222         return result;
1223 }
1224
1225 OutputFormat::OutputFormat(const struct sr_output_module *structure) :
1226         StructureWrapper<Context, const struct sr_output_module>(structure)
1227 {
1228 }
1229
1230 OutputFormat::~OutputFormat()
1231 {
1232 }
1233
1234 string OutputFormat::get_name()
1235 {
1236         return valid_string(sr_output_id_get(structure));
1237 }
1238
1239 string OutputFormat::get_description()
1240 {
1241         return valid_string(sr_output_description_get(structure));
1242 }
1243
1244 map<string, shared_ptr<Option>> OutputFormat::get_options()
1245 {
1246         const struct sr_option **options = sr_output_options_get(structure);
1247         auto option_array = shared_ptr<const struct sr_option *>(
1248                 options, sr_output_options_free);
1249         map<string, shared_ptr<Option>> result;
1250         for (int i = 0; options[i]; i++)
1251                 result[options[i]->id] = shared_ptr<Option>(
1252                         new Option(options[i], option_array), Option::Deleter());
1253         return result;
1254 }
1255
1256 shared_ptr<Output> OutputFormat::create_output(
1257         shared_ptr<Device> device, map<string, Glib::VariantBase> options)
1258 {
1259         return shared_ptr<Output>(
1260                 new Output(
1261                         static_pointer_cast<OutputFormat>(shared_from_this()),
1262                                 device, options),
1263                 Output::Deleter());
1264 }
1265
1266 Output::Output(shared_ptr<OutputFormat> format,
1267                 shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
1268         structure(sr_output_new(format->structure,
1269                 map_to_hash_variant(options), device->structure)),
1270         format(format), device(device), options(options)
1271 {
1272 }
1273
1274 Output::~Output()
1275 {
1276         check(sr_output_free(structure));
1277 }
1278
1279 string Output::receive(shared_ptr<Packet> packet)
1280 {
1281         GString *out;
1282         check(sr_output_send(structure, packet->structure, &out));
1283         if (out)
1284         {
1285                 auto result = string(out->str, out->str + out->len);
1286                 g_string_free(out, true);
1287                 return result;
1288         }
1289         else
1290         {
1291                 return string();
1292         }
1293 }
1294
1295 #include "enums.cpp"
1296
1297 }