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