]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/classes.cpp
bindings: Add Device::get_description() method.
[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, string> and GHashTable */
44
45 static GHashTable *map_to_hash(map<string, string> input)
46 {
47         auto output = g_hash_table_new_full(
48                 g_str_hash, g_str_equal, g_free, g_free);
49         for (auto entry : input)
50                 g_hash_table_insert(output,
51                         g_strdup(entry.first.c_str()),
52                         g_strdup(entry.second.c_str()));
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         struct sr_input_format **input_list = sr_input_list();
84         if (input_list)
85                 for (int i = 0; input_list[i]; i++)
86                         input_formats[input_list[i]->id] =
87                                 new InputFormat(input_list[i]);
88         struct sr_output_format **output_list = sr_output_list();
89         if (output_list)
90                 for (int i = 0; output_list[i]; i++)
91                         output_formats[output_list[i]->id] =
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 Driver::Driver(struct sr_dev_driver *structure) :
231         StructureWrapper<Context, struct sr_dev_driver>(structure),
232         initialized(false)
233 {
234 }
235
236 Driver::~Driver()
237 {
238         for (auto device : devices)
239                 delete device;
240 }
241
242 string Driver::get_name()
243 {
244         return valid_string(structure->name);
245 }
246
247 string Driver::get_long_name()
248 {
249         return valid_string(structure->longname);
250 }
251
252 vector<shared_ptr<HardwareDevice>> Driver::scan(
253         map<const ConfigKey *, Glib::VariantBase> options)
254 {
255         /* Initialise the driver if not yet done. */
256         if (!initialized)
257         {
258                 check(sr_driver_init(parent->structure, structure));
259                 initialized = true;
260         }
261
262         /* Clear all existing instances. */
263         for (auto device : devices)
264                 delete device;
265         devices.clear();
266
267         /* Translate scan options to GSList of struct sr_config pointers. */
268         GSList *option_list = NULL;
269         for (auto entry : options)
270         {
271                 auto key = entry.first;
272                 auto value = entry.second;
273                 auto config = g_new(struct sr_config, 1);
274                 config->key = key->get_id();
275                 config->data = value.gobj();
276                 option_list = g_slist_append(option_list, config);
277         }
278
279         /* Run scan. */
280         GSList *device_list = sr_driver_scan(structure, option_list);
281
282         /* Free option list. */
283         g_slist_free_full(option_list, g_free);
284
285         /* Create device objects. */
286         for (GSList *device = device_list; device; device = device->next)
287         {
288                 auto sdi = (struct sr_dev_inst *) device->data;
289                 devices.push_back(new HardwareDevice(this, sdi));
290         }
291
292         /* Free GSList returned from scan. */
293         g_slist_free(device_list);
294
295         /* Create list of shared pointers to device instances for return. */
296         vector<shared_ptr<HardwareDevice>> result;
297         for (auto device : devices)
298                 result.push_back(static_pointer_cast<HardwareDevice>(
299                         device->get_shared_pointer(parent)));
300         return result;
301 }
302
303 Configurable::Configurable(
304                 struct sr_dev_driver *driver,
305                 struct sr_dev_inst *sdi,
306                 struct sr_channel_group *cg) :
307         config_driver(driver),
308         config_sdi(sdi),
309         config_channel_group(cg)
310 {
311 }
312
313 Configurable::~Configurable()
314 {
315 }
316
317 Glib::VariantBase Configurable::config_get(const ConfigKey *key)
318 {
319         GVariant *data;
320         check(sr_config_get(
321                 config_driver, config_sdi, config_channel_group,
322                 key->get_id(), &data));
323         return Glib::VariantBase(data);
324 }
325
326 void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
327 {
328         check(sr_config_set(
329                 config_sdi, config_channel_group,
330                 key->get_id(), value.gobj()));
331 }
332
333 Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
334 {
335         GVariant *data;
336         check(sr_config_list(
337                 config_driver, config_sdi, config_channel_group,
338                 key->get_id(), &data));
339         return Glib::VariantContainerBase(data);
340 }
341
342 Device::Device(struct sr_dev_inst *structure) :
343         Configurable(structure->driver, structure, NULL),
344         StructureWrapper<Context, struct sr_dev_inst>(structure)
345 {
346         for (GSList *entry = structure->channels; entry; entry = entry->next)
347         {
348                 auto channel = (struct sr_channel *) entry->data;
349                 channels[channel] = new Channel(channel);
350         }
351
352         for (GSList *entry = structure->channel_groups; entry; entry = entry->next)
353         {
354                 auto group = (struct sr_channel_group *) entry->data;
355                 channel_groups[group->name] = new ChannelGroup(this, group);
356         }
357 }
358
359 Device::~Device()
360 {
361         for (auto entry : channels)
362                 delete entry.second;
363         for (auto entry : channel_groups)
364                 delete entry.second;
365 }
366
367 string Device::get_description()
368 {
369         ostringstream s;
370
371         vector<string> parts =
372                 {get_vendor(), get_model(), get_version()};
373
374         for (string part : parts)
375                 if (part.length() > 0)
376                         s << part;
377
378         return s.str();
379 }
380
381 string Device::get_vendor()
382 {
383         return valid_string(structure->vendor);
384 }
385
386 string Device::get_model()
387 {
388         return valid_string(structure->model);
389 }
390
391 string Device::get_version()
392 {
393         return valid_string(structure->version);
394 }
395
396 vector<shared_ptr<Channel>> Device::get_channels()
397 {
398         vector<shared_ptr<Channel>> result;
399         for (auto entry : channels)
400                 result.push_back(static_pointer_cast<Channel>(
401                         entry.second->get_shared_pointer(this)));
402         return result;
403 }
404
405 shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
406 {
407         return static_pointer_cast<Channel>(
408                 channels[ptr]->get_shared_pointer(this));
409 }
410
411 map<string, shared_ptr<ChannelGroup>>
412 Device::get_channel_groups()
413 {
414         map<string, shared_ptr<ChannelGroup>> result;
415         for (auto entry: channel_groups)
416         {
417                 auto name = entry.first;
418                 auto channel_group = entry.second;
419                 result[name] = static_pointer_cast<ChannelGroup>(
420                         channel_group->get_shared_pointer(this));
421         }
422         return result;
423 }
424
425 void Device::open()
426 {
427         check(sr_dev_open(structure));
428 }
429
430 void Device::close()
431 {
432         check(sr_dev_close(structure));
433 }
434
435 HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
436         Device(structure),
437         driver(driver)
438 {
439 }
440
441 HardwareDevice::~HardwareDevice()
442 {
443 }
444
445 shared_ptr<Driver> HardwareDevice::get_driver()
446 {
447         return static_pointer_cast<Driver>(driver->get_shared_pointer(parent));
448 }
449
450 Channel::Channel(struct sr_channel *structure) :
451         StructureWrapper<Device, struct sr_channel>(structure),
452         type(ChannelType::get(structure->type))
453 {
454 }
455
456 Channel::~Channel()
457 {
458 }
459
460 string Channel::get_name()
461 {
462         return valid_string(structure->name);
463 }
464
465 void Channel::set_name(string name)
466 {
467         check(sr_dev_channel_name_set(parent->structure, structure->index, name.c_str()));
468 }
469
470 const ChannelType *Channel::get_type()
471 {
472         return ChannelType::get(structure->type);
473 }
474
475 bool Channel::get_enabled()
476 {
477         return structure->enabled;
478 }
479
480 void Channel::set_enabled(bool value)
481 {
482         check(sr_dev_channel_enable(parent->structure, structure->index, value));
483 }
484
485 ChannelGroup::ChannelGroup(Device *device,
486                 struct sr_channel_group *structure) :
487         StructureWrapper<Device, struct sr_channel_group>(structure),
488         Configurable(device->structure->driver, device->structure, structure)
489 {
490         for (GSList *entry = structure->channels; entry; entry = entry->next)
491                 channels.push_back(device->channels[(struct sr_channel *)entry->data]);
492 }
493
494 ChannelGroup::~ChannelGroup()
495 {
496 }
497
498 string ChannelGroup::get_name()
499 {
500         return valid_string(structure->name);
501 }
502
503 vector<shared_ptr<Channel>> ChannelGroup::get_channels()
504 {
505         vector<shared_ptr<Channel>> result;
506         for (auto channel : channels)
507                 result.push_back(static_pointer_cast<Channel>(
508                         channel->get_shared_pointer(parent)));
509         return result;
510 }
511
512 Trigger::Trigger(shared_ptr<Context> context, string name) : 
513         structure(sr_trigger_new(name.c_str())), context(context)
514 {
515         for (auto stage = structure->stages; stage; stage = stage->next)
516                 stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data));
517 }
518
519 Trigger::~Trigger()
520 {
521         for (auto stage: stages)
522                 delete stage;
523
524         sr_trigger_free(structure);
525 }
526
527 string Trigger::get_name()
528 {
529         return structure->name;
530 }
531
532 vector<shared_ptr<TriggerStage>> Trigger::get_stages()
533 {
534         vector<shared_ptr<TriggerStage>> result;
535         for (auto stage : stages)
536                 result.push_back(static_pointer_cast<TriggerStage>(
537                         stage->get_shared_pointer(this)));
538         return result;
539 }
540
541 shared_ptr<TriggerStage> Trigger::add_stage()
542 {
543         auto stage = new TriggerStage(sr_trigger_stage_add(structure));
544         stages.push_back(stage);
545         return static_pointer_cast<TriggerStage>(
546                 stage->get_shared_pointer(this));
547 }
548
549 TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : 
550         StructureWrapper<Trigger, struct sr_trigger_stage>(structure)
551 {
552 }
553
554 TriggerStage::~TriggerStage()
555 {
556         for (auto match : matches)
557                 delete match;
558 }
559         
560 int TriggerStage::get_number()
561 {
562         return structure->stage;
563 }
564
565 vector<shared_ptr<TriggerMatch>> TriggerStage::get_matches()
566 {
567         vector<shared_ptr<TriggerMatch>> result;
568         for (auto match : matches)
569                 result.push_back(static_pointer_cast<TriggerMatch>(
570                         match->get_shared_pointer(this)));
571         return result;
572 }
573
574 void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value)
575 {
576         check(sr_trigger_match_add(structure, channel->structure, type->get_id(), value));
577         matches.push_back(new TriggerMatch(
578                 (struct sr_trigger_match *) g_slist_last(structure->matches)->data, channel));
579 }
580
581 void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type)
582 {
583         add_match(channel, type, NAN);
584 }
585
586 TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel) :
587         StructureWrapper<TriggerStage, struct sr_trigger_match>(structure), channel(channel)
588 {
589 }
590
591 TriggerMatch::~TriggerMatch()
592 {
593 }
594
595 shared_ptr<Channel> TriggerMatch::get_channel()
596 {
597         return channel;
598 }
599
600 const TriggerMatchType *TriggerMatch::get_type()
601 {
602         return TriggerMatchType::get(structure->match);
603 }
604
605 float TriggerMatch::get_value()
606 {
607         return structure->value;
608 }
609
610 DatafeedCallbackData::DatafeedCallbackData(Session *session,
611                 DatafeedCallbackFunction callback) :
612         callback(callback), session(session)
613 {
614 }
615
616 void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
617         const struct sr_datafeed_packet *pkt)
618 {
619         auto device = session->devices[sdi];
620         auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
621         callback(device, packet);
622 }
623
624 SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) :
625         source(source)
626 {
627 }
628
629 bool SourceCallbackData::run(int revents)
630 {
631         return source->callback((Glib::IOCondition) revents);
632 }
633
634 shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events,
635         int timeout, SourceCallbackFunction callback)
636 {
637         auto result = new EventSource(timeout, callback);
638         result->type = EventSource::SOURCE_FD;
639         result->fd = fd;
640         result->events = events;
641         return shared_ptr<EventSource>(result, EventSource::Deleter());
642 }
643
644 shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout,
645         SourceCallbackFunction callback)
646 {
647         auto result = new EventSource(timeout, callback);
648         result->type = EventSource::SOURCE_POLLFD;
649         result->pollfd = pollfd;
650         return shared_ptr<EventSource>(result, EventSource::Deleter());
651 }
652
653 shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel,
654         Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
655 {
656         auto result = new EventSource(timeout, callback);
657         result->type = EventSource::SOURCE_IOCHANNEL;
658         result->channel = channel;
659         result->events = events;
660         return shared_ptr<EventSource>(result, EventSource::Deleter());
661 }
662
663 EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
664         timeout(timeout), callback(callback)
665 {
666 }
667
668 EventSource::~EventSource()
669 {
670 }
671
672 Session::Session(shared_ptr<Context> context) :
673         context(context), saving(false)
674 {
675         check(sr_session_new(&structure));
676         context->session = this;
677 }
678
679 Session::Session(shared_ptr<Context> context, string filename) :
680         context(context), saving(false)
681 {
682         check(sr_session_load(filename.c_str(), &structure));
683         context->session = this;
684 }
685
686 Session::~Session()
687 {
688         check(sr_session_destroy(structure));
689
690         for (auto callback : datafeed_callbacks)
691                 delete callback;
692
693         for (auto entry : source_callbacks)
694                 delete entry.second;
695 }
696
697 void Session::add_device(shared_ptr<Device> device)
698 {
699         check(sr_session_dev_add(structure, device->structure));
700         devices[device->structure] = device;
701 }
702
703 vector<shared_ptr<Device>> Session::get_devices()
704 {
705         GSList *dev_list;
706         check(sr_session_dev_list(structure, &dev_list));
707         vector<shared_ptr<Device>> result;
708         for (GSList *dev = dev_list; dev; dev = dev->next)
709         {
710                 auto sdi = (struct sr_dev_inst *) dev->data;
711                 if (devices.count(sdi) == 0)
712                         devices[sdi] = shared_ptr<Device>(
713                                 new Device(sdi), Device::Deleter());
714                 result.push_back(devices[sdi]);
715         }
716         return result;
717 }
718
719 void Session::remove_devices()
720 {
721         devices.clear();
722         check(sr_session_dev_remove_all(structure));
723 }
724
725 void Session::start()
726 {
727         check(sr_session_start(structure));
728 }
729
730 void Session::run()
731 {
732         check(sr_session_run(structure));
733 }
734
735 void Session::stop()
736 {
737         check(sr_session_stop(structure));
738 }
739
740 void Session::begin_save(string filename)
741 {
742         saving = true;
743         save_initialized = false;
744         save_filename = filename;
745         save_samplerate = 0;
746 }
747
748 void Session::append(shared_ptr<Packet> packet)
749 {
750         if (!saving)
751                 throw Error(SR_ERR);
752
753         switch (packet->structure->type)
754         {
755                 case SR_DF_META:
756                 {
757                         auto meta = (const struct sr_datafeed_meta *)
758                                 packet->structure->payload;
759
760                         for (auto l = meta->config; l; l = l->next)
761                         {
762                                 auto config = (struct sr_config *) l->data;
763                                 if (config->key == SR_CONF_SAMPLERATE)
764                                         save_samplerate = g_variant_get_uint64(config->data);
765                         }
766
767                         break;
768                 }
769                 case SR_DF_LOGIC:
770                 {
771                         if (save_samplerate == 0)
772                         {
773                                 GVariant *samplerate;
774
775                                 check(sr_config_get(packet->device->structure->driver,
776                                         packet->device->structure, NULL, SR_CONF_SAMPLERATE,
777                                         &samplerate));
778
779                                 save_samplerate = g_variant_get_uint64(samplerate);
780
781                                 g_variant_unref(samplerate);
782                         }
783
784                         if (!save_initialized)
785                         {
786                                 vector<shared_ptr<Channel>> save_channels;
787
788                                 for (auto channel : packet->device->get_channels())
789                                         if (channel->structure->enabled &&
790                                                         channel->structure->type == SR_CHANNEL_LOGIC)
791                                                 save_channels.push_back(channel);
792
793                                 auto channels = g_new(char *, save_channels.size());
794
795                                 int i = 0;
796                                 for (auto channel : save_channels)
797                                                 channels[i++] = channel->structure->name;
798                                 channels[i] = NULL;
799
800                                 int ret = sr_session_save_init(structure, save_filename.c_str(),
801                                                 save_samplerate, channels);
802
803                                 g_free(channels);
804
805                                 if (ret != SR_OK)
806                                         throw Error(ret);
807
808                                 save_initialized = true;
809                         }
810
811                         auto logic = (const struct sr_datafeed_logic *)
812                                 packet->structure->payload;
813
814                         check(sr_session_append(structure, save_filename.c_str(),
815                                 (uint8_t *) logic->data, logic->unitsize,
816                                 logic->length / logic->unitsize));
817                 }
818         }
819 }
820
821 void Session::append(void *data, size_t length, unsigned int unit_size)
822 {
823         check(sr_session_append(structure, save_filename.c_str(),
824                 (uint8_t *) data, unit_size, length));
825 }
826
827 static void datafeed_callback(const struct sr_dev_inst *sdi,
828         const struct sr_datafeed_packet *pkt, void *cb_data)
829 {
830         auto callback = static_cast<DatafeedCallbackData *>(cb_data);
831         callback->run(sdi, pkt);
832 }
833         
834 void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
835 {
836         auto cb_data = new DatafeedCallbackData(this, callback);
837         check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data));
838         datafeed_callbacks.push_back(cb_data);
839 }
840
841 void Session::remove_datafeed_callbacks(void)
842 {
843         check(sr_session_datafeed_callback_remove_all(structure));
844         for (auto callback : datafeed_callbacks)
845                 delete callback;
846         datafeed_callbacks.clear();
847 }
848
849 static int source_callback(int fd, int revents, void *cb_data)
850 {
851         (void) fd;
852         auto callback = (SourceCallbackData *) cb_data;
853         return callback->run(revents);
854 }
855
856 void Session::add_source(shared_ptr<EventSource> source)
857 {
858         if (source_callbacks.count(source) == 1)
859                 throw Error(SR_ERR_ARG);
860
861         auto cb_data = new SourceCallbackData(source);
862
863         switch (source->type)
864         {
865                 case EventSource::SOURCE_FD:
866                         check(sr_session_source_add(structure, source->fd, source->events,
867                                 source->timeout, source_callback, cb_data));
868                         break;
869                 case EventSource::SOURCE_POLLFD:
870                         check(sr_session_source_add_pollfd(structure,
871                                 source->pollfd.gobj(), source->timeout, source_callback,
872                                 cb_data));
873                         break;
874                 case EventSource::SOURCE_IOCHANNEL:
875                         check(sr_session_source_add_channel(structure,
876                                 source->channel->gobj(), source->events, source->timeout,
877                                 source_callback, cb_data));
878                         break;
879         }
880
881         source_callbacks[source] = cb_data;
882 }
883
884 void Session::remove_source(shared_ptr<EventSource> source)
885 {
886         if (source_callbacks.count(source) == 0)
887                 throw Error(SR_ERR_ARG);
888
889         switch (source->type)
890         {
891                 case EventSource::SOURCE_FD:
892                         check(sr_session_source_remove(structure, source->fd));
893                         break;
894                 case EventSource::SOURCE_POLLFD:
895                         check(sr_session_source_remove_pollfd(structure,
896                                 source->pollfd.gobj()));
897                         break;
898                 case EventSource::SOURCE_IOCHANNEL:
899                         check(sr_session_source_remove_channel(structure,
900                                 source->channel->gobj()));
901                         break;
902         }
903
904         delete source_callbacks[source];
905
906         source_callbacks.erase(source);
907 }
908
909 shared_ptr<Trigger> Session::get_trigger()
910 {
911         return trigger;
912 }
913
914 void Session::set_trigger(shared_ptr<Trigger> trigger)
915 {
916         check(sr_session_trigger_set(structure, trigger->structure));
917         this->trigger = trigger;
918 }
919
920 Packet::Packet(shared_ptr<Device> device,
921         const struct sr_datafeed_packet *structure) :
922         structure(structure),
923         device(device)
924 {
925         switch (structure->type)
926         {
927                 case SR_DF_HEADER:
928                         payload = new Header(
929                                 static_cast<const struct sr_datafeed_header *>(
930                                         structure->payload));
931                         break;
932                 case SR_DF_META:
933                         payload = new Meta(
934                                 static_cast<const struct sr_datafeed_meta *>(
935                                         structure->payload));
936                         break;
937                 case SR_DF_LOGIC:
938                         payload = new Logic(
939                                 static_cast<const struct sr_datafeed_logic *>(
940                                         structure->payload));
941                         break;
942                 case SR_DF_ANALOG:
943                         payload = new Analog(
944                                 static_cast<const struct sr_datafeed_analog *>(
945                                         structure->payload));
946                         break;
947         }
948 }
949
950 Packet::~Packet()
951 {
952         if (payload)
953                 delete payload;
954 }
955
956 const PacketType *Packet::get_type()
957 {
958         return PacketType::get(structure->type);
959 }
960
961 shared_ptr<PacketPayload> Packet::get_payload()
962 {
963         return payload->get_shared_pointer(this);
964 }
965
966 PacketPayload::PacketPayload()
967 {
968 }
969
970 PacketPayload::~PacketPayload()
971 {
972 }
973
974 Header::Header(const struct sr_datafeed_header *structure) :
975         PacketPayload(),
976         StructureWrapper<Packet, const struct sr_datafeed_header>(structure)
977 {
978 }
979
980 Header::~Header()
981 {
982 }
983
984 int Header::get_feed_version()
985 {
986         return structure->feed_version;
987 }
988
989 Glib::TimeVal Header::get_start_time()
990 {
991         return Glib::TimeVal(
992                 structure->starttime.tv_sec,
993                 structure->starttime.tv_usec);
994 }
995
996 Meta::Meta(const struct sr_datafeed_meta *structure) :
997         PacketPayload(),
998         StructureWrapper<Packet, const struct sr_datafeed_meta>(structure)
999 {
1000 }
1001
1002 Meta::~Meta()
1003 {
1004 }
1005
1006 map<const ConfigKey *, Glib::VariantBase> Meta::get_config()
1007 {
1008         map<const ConfigKey *, Glib::VariantBase> result;
1009         for (auto l = structure->config; l; l = l->next)
1010         {
1011                 auto config = (struct sr_config *) l->data;
1012                 result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data);
1013         }
1014         return result;
1015 }
1016
1017 Logic::Logic(const struct sr_datafeed_logic *structure) :
1018         PacketPayload(),
1019         StructureWrapper<Packet, const struct sr_datafeed_logic>(structure)
1020 {
1021 }
1022
1023 Logic::~Logic()
1024 {
1025 }
1026
1027 void *Logic::get_data_pointer()
1028 {
1029         return structure->data;
1030 }
1031
1032 size_t Logic::get_data_length()
1033 {
1034         return structure->length;
1035 }
1036
1037 unsigned int Logic::get_unit_size()
1038 {
1039         return structure->unitsize;
1040 }
1041
1042 Analog::Analog(const struct sr_datafeed_analog *structure) :
1043         PacketPayload(),
1044         StructureWrapper<Packet, const struct sr_datafeed_analog>(structure)
1045 {
1046 }
1047
1048 Analog::~Analog()
1049 {
1050 }
1051
1052 float *Analog::get_data_pointer()
1053 {
1054         return structure->data;
1055 }
1056
1057 unsigned int Analog::get_num_samples()
1058 {
1059         return structure->num_samples;
1060 }
1061
1062 vector<shared_ptr<Channel>> Analog::get_channels()
1063 {
1064         vector<shared_ptr<Channel>> result;
1065         for (auto l = structure->channels; l; l = l->next)
1066                 result.push_back(parent->device->get_channel(
1067                         (struct sr_channel *)l->data));
1068         return result;
1069 }
1070
1071 const Quantity *Analog::get_mq()
1072 {
1073         return Quantity::get(structure->mq);
1074 }
1075
1076 const Unit *Analog::get_unit()
1077 {
1078         return Unit::get(structure->unit);
1079 }
1080
1081 vector<const QuantityFlag *> Analog::get_mq_flags()
1082 {
1083         return QuantityFlag::flags_from_mask(structure->mqflags);
1084 }
1085
1086 InputFormat::InputFormat(struct sr_input_format *structure) :
1087         StructureWrapper<Context, struct sr_input_format>(structure)
1088 {
1089 }
1090
1091 InputFormat::~InputFormat()
1092 {
1093 }
1094
1095 string InputFormat::get_name()
1096 {
1097         return valid_string(structure->id);
1098 }
1099
1100 string InputFormat::get_description()
1101 {
1102         return valid_string(structure->description);
1103 }
1104
1105 bool InputFormat::format_match(string filename)
1106 {
1107         return structure->format_match(filename.c_str());
1108 }
1109
1110 shared_ptr<InputFileDevice> InputFormat::open_file(string filename,
1111                 map<string, string> options)
1112 {
1113         auto input = g_new(struct sr_input, 1);
1114         input->param = map_to_hash(options);
1115
1116         /** Run initialisation. */
1117         check(structure->init(input, filename.c_str()));
1118
1119         /** Create virtual device. */
1120         return shared_ptr<InputFileDevice>(new InputFileDevice(
1121                 static_pointer_cast<InputFormat>(shared_from_this()), input, filename),
1122                 InputFileDevice::Deleter());
1123 }
1124
1125 InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format,
1126                 struct sr_input *input, string filename) :
1127         Device(input->sdi),
1128         input(input),
1129         format(format),
1130         filename(filename)
1131 {
1132 }
1133
1134 InputFileDevice::~InputFileDevice()
1135 {
1136         g_hash_table_unref(input->param);
1137         g_free(input);
1138 }
1139
1140 void InputFileDevice::load()
1141 {
1142         check(format->structure->loadfile(input, filename.c_str()));
1143 }
1144
1145 OutputFormat::OutputFormat(struct sr_output_format *structure) :
1146         StructureWrapper<Context, struct sr_output_format>(structure)
1147 {
1148 }
1149
1150 OutputFormat::~OutputFormat()
1151 {
1152 }
1153
1154 string OutputFormat::get_name()
1155 {
1156         return valid_string(structure->id);
1157 }
1158
1159 string OutputFormat::get_description()
1160 {
1161         return valid_string(structure->description);
1162 }
1163
1164 shared_ptr<Output> OutputFormat::create_output(
1165         shared_ptr<Device> device, map<string, string> options)
1166 {
1167         return shared_ptr<Output>(
1168                 new Output(
1169                         static_pointer_cast<OutputFormat>(shared_from_this()),
1170                                 device, options),
1171                 Output::Deleter());
1172 }
1173
1174 Output::Output(shared_ptr<OutputFormat> format,
1175                 shared_ptr<Device> device, map<string, string> options) :
1176         structure(sr_output_new(format->structure,
1177                 map_to_hash(options), device->structure)),
1178         format(format), device(device), options(options)
1179 {
1180 }
1181
1182 Output::~Output()
1183 {
1184         g_hash_table_unref(structure->params);
1185         check(sr_output_free(structure));
1186 }
1187
1188 string Output::receive(shared_ptr<Packet> packet)
1189 {
1190         GString *out;
1191         check(sr_output_send(structure, packet->structure, &out));
1192         if (out)
1193         {
1194                 auto result = string(out->str, out->str + out->len);
1195                 g_string_free(out, true);
1196                 return result;
1197         }
1198         else
1199         {
1200                 return string();
1201         }
1202 }
1203
1204 #include "enums.cpp"
1205
1206 }