]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/include/libsigrok/libsigrok.hpp
e30fd4d396a360dbca2df87de75eda0ff17c8b57
[libsigrok.git] / bindings / cxx / include / libsigrok / libsigrok.hpp
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 /**
21
22 @mainpage API Reference
23
24 Introduction
25 ------------
26
27 The sigrok++ API provides an object-oriented C++ interface to the functionality
28 in libsigrok, including automatic memory and resource management.
29
30 It is built on top of the public libsigrok C API, and is designed to be used as
31 a standalone alternative API. Programs should not mix usage of the C and C++
32 APIs; the C++ interface code needs to have full control of all C API calls for
33 resources to be managed correctly.
34
35 Memory management
36 -----------------
37
38 All runtime objects created through the C++ API are passed and accessed via
39 shared pointers, using the C++11 std::shared_ptr implementation. This means
40 that a reference count is kept for each object.
41
42 Shared pointers can be copied and assigned in a user's program, automatically
43 updating their reference count and deleting objects when they are no longer in
44 use. The C++ interface code also keeps track of internal dependencies between
45 libsigrok resources, and ensures that objects are not prematurely deleted when
46 their resources are in use by other objects.
47
48 This means that management of sigrok++ objects and their underlying libsigrok
49 resources can be treated as fully automatic. As long as all shared pointers to
50 objects are deleted or reassigned when no longer in use, all underlying
51 resources will be released at the right time.
52
53 Getting started
54 ---------------
55
56 Usage of the C++ API needs to begin with a call to sigrok::Context::create().
57 This will create the global libsigrok context and returns a shared pointer to
58 the sigrok::Context object. Methods on this object provide access to the
59 hardware drivers, input and output formats supported by the library, as well
60 as means of creating other objects such as sessions and triggers.
61
62 Error handling
63 --------------
64
65 When any libsigrok C API call returns an error, a sigrok::Error exception is
66 raised, which provides access to the error code and description.
67
68 */
69
70 #ifndef LIBSIGROK_HPP
71 #define LIBSIGROK_HPP
72
73 #include "libsigrok/libsigrok.h"
74 #include <glibmm-2.4/glibmm.h>
75
76 #include <stdexcept>
77 #include <memory>
78 #include <vector>
79 #include <map>
80 #include <set>
81
82 namespace sigrok
83 {
84
85 using namespace std;
86
87 /* Forward declarations */
88 class SR_API Error;
89 class SR_API Context;
90 class SR_API Driver;
91 class SR_API Device;
92 class SR_API HardwareDevice;
93 class SR_API Channel;
94 class SR_API EventSource;
95 class SR_API Session;
96 class SR_API ConfigKey;
97 class SR_API InputFormat;
98 class SR_API OutputFormat;
99 class SR_API LogLevel;
100 class SR_API ChannelGroup;
101 class SR_API Trigger;
102 class SR_API TriggerStage;
103 class SR_API TriggerMatch;
104 class SR_API TriggerMatchType;
105 class SR_API ChannelType;
106 class SR_API Packet;
107 class SR_API PacketPayload;
108 class SR_API PacketType;
109 class SR_API Quantity;
110 class SR_API Unit;
111 class SR_API QuantityFlag;
112 class SR_API Input;
113 class SR_API InputDevice;
114 class SR_API Output;
115 class SR_API DataType;
116 class SR_API Option;
117
118 /** Exception thrown when an error code is returned by any libsigrok call. */
119 class SR_API Error: public exception
120 {
121 public:
122         Error(int result);
123         ~Error() throw();
124         const int result;
125         const char *what() const throw();
126 };
127
128 /* Base template for classes whose resources are owned by a parent object. */
129 template <class Class, class Parent, typename Struct>
130 class SR_API ParentOwned
131 {
132 protected:
133         /*  Parent object which owns this child object's underlying structure.
134
135                 This shared pointer will be null when this child is unused, but
136                 will be assigned to point to the parent before any shared pointer
137                 to this child is handed out to the user.
138
139                 When the reference count of this child falls to zero, this shared
140                 pointer to its parent is reset by a custom deleter on the child's
141                 shared pointer.
142
143                 This strategy ensures that the destructors for both the child and
144                 the parent are called at the correct time, i.e. only when all
145                 references to both the parent and all its children are gone. */
146         shared_ptr<Parent> _parent;
147
148         /* Weak pointer for shared_from_this() implementation. */
149         weak_ptr<Class> _weak_this;
150
151 public:
152         /* Note, this implementation will create a new smart_ptr if none exists. */
153         shared_ptr<Class> shared_from_this()
154         {
155                 shared_ptr<Class> shared;
156
157                 if (!(shared = _weak_this.lock()))
158                 {
159                         shared = shared_ptr<Class>((Class *) this, reset_parent);
160                         _weak_this = shared;
161                 }
162
163                 return shared;
164         }
165
166         shared_ptr<Class> get_shared_pointer(shared_ptr<Parent> parent)
167         {
168                 if (!parent)
169                         throw Error(SR_ERR_BUG);
170                 this->_parent = parent;
171                 return shared_from_this();
172         }
173
174         shared_ptr<Class> get_shared_pointer(Parent *parent)
175         {
176                 if (!parent)
177                         throw Error(SR_ERR_BUG);
178                 return get_shared_pointer(parent->shared_from_this());
179         }
180 protected:
181         static void reset_parent(Class *object)
182         {
183                 if (!object->_parent)
184                         throw Error(SR_ERR_BUG);
185                 object->_parent.reset();
186         }
187
188         Struct *_structure;
189
190         ParentOwned<Class, Parent, Struct>(Struct *structure) :
191                 _structure(structure)
192         {
193         }
194 };
195
196 /* Base template for classes whose resources are owned by the user. */
197 template <class Class, typename Struct>
198 class SR_API UserOwned : public enable_shared_from_this<Class>
199 {
200 public:
201         shared_ptr<Class> shared_from_this()
202         {
203                 auto shared = enable_shared_from_this<Class>::shared_from_this();
204                 if (!shared)
205                         throw Error(SR_ERR_BUG);
206                 return shared;
207         }
208 protected:
209         Struct *_structure;
210
211         UserOwned<Class, Struct>(Struct *structure) :
212                 _structure(structure)
213         {
214         }
215
216         /* Deleter needed to allow shared_ptr use with protected destructor. */
217         class Deleter
218         {
219         public:
220                 void operator()(Class *object) { delete object; }
221         };
222 };
223
224 /** Type of log callback */
225 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
226
227 /** The global libsigrok context */
228 class SR_API Context : public UserOwned<Context, struct sr_context>
229 {
230 public:
231         /** Create new context */
232         static shared_ptr<Context> create();
233         /** libsigrok package version. */
234         string package_version();
235         /** libsigrok library version. */
236         string lib_version();
237         /** Available hardware drivers, indexed by name. */
238         map<string, shared_ptr<Driver> > drivers();
239         /** Available input formats, indexed by name. */
240         map<string, shared_ptr<InputFormat> > input_formats();
241         /** Available output formats, indexed by name. */
242         map<string, shared_ptr<OutputFormat> > output_formats();
243         /** Current log level. */
244         const LogLevel *log_level();
245         /** Set the log level.
246          * @param level LogLevel to use. */
247         void set_log_level(const LogLevel *level);
248         /** Current log domain. */
249         string log_domain();
250         /** Set the log domain.
251          * @param value Log domain prefix string. */
252         void set_log_domain(string value);
253         /** Set the log callback.
254          * @param callback Callback of the form callback(LogLevel, string). */
255         void set_log_callback(LogCallbackFunction callback);
256         /** Set the log callback to the default handler. */
257         void set_log_callback_default();
258         /** Create a new session. */
259         shared_ptr<Session> create_session();
260         /** Load a saved session.
261          * @param filename File name string. */
262         shared_ptr<Session> load_session(string filename);
263         /** Create a new trigger.
264          * @param name Name string for new trigger. */
265         shared_ptr<Trigger> create_trigger(string name);
266         /** Open an input file.
267          * @param filename File name string. */
268         shared_ptr<Input> open_file(string filename);
269         /** Open an input stream based on header data.
270          * @param header Initial data from stream. */
271         shared_ptr<Input> open_stream(string header);
272 protected:
273         map<string, Driver *> _drivers;
274         map<string, InputFormat *> _input_formats;
275         map<string, OutputFormat *> _output_formats;
276         Session *_session;
277         LogCallbackFunction _log_callback;
278         Context();
279         ~Context();
280         friend class Deleter;
281         friend class Session;
282         friend class Driver;
283 };
284
285 enum Capability {
286         GET = SR_CONF_GET,
287         SET = SR_CONF_SET,
288         LIST = SR_CONF_LIST
289 };
290
291 /** An object that can be configured. */
292 class SR_API Configurable
293 {
294 public:
295         /** Read configuration for the given key.
296          * @param key ConfigKey to read. */
297         Glib::VariantBase config_get(const ConfigKey *key);
298         /** Set configuration for the given key to a specified value.
299          * @param key ConfigKey to set.
300          * @param value Value to set. */
301         void config_set(const ConfigKey *key, Glib::VariantBase value);
302         /** Enumerate available values for the given configuration key.
303          * @param key ConfigKey to enumerate values for. */
304         Glib::VariantContainerBase config_list(const ConfigKey *key);
305         /** Enumerate available keys, according to a given index key. */
306         map<const ConfigKey *, set<Capability> > config_keys(const ConfigKey *key);
307         /** Check for a key in the list from a given index key. */
308         bool config_check(const ConfigKey *key, const ConfigKey *index_key);
309 protected:
310         Configurable(
311                 struct sr_dev_driver *driver,
312                 struct sr_dev_inst *sdi,
313                 struct sr_channel_group *channel_group);
314         virtual ~Configurable();
315         struct sr_dev_driver *config_driver;
316         struct sr_dev_inst *config_sdi;
317         struct sr_channel_group *config_channel_group;
318 };
319
320 /** A hardware driver provided by the library */
321 class SR_API Driver :
322         public ParentOwned<Driver, Context, struct sr_dev_driver>,
323         public Configurable
324 {
325 public:
326         /** Name of this driver. */
327         string name();
328         /** Long name for this driver. */
329         string long_name();
330         /** Scan for devices and return a list of devices found.
331          * @param options Mapping of (ConfigKey, value) pairs. */
332         vector<shared_ptr<HardwareDevice> > scan(
333                 map<const ConfigKey *, Glib::VariantBase> options =
334                         map<const ConfigKey *, Glib::VariantBase>());
335 protected:
336         bool _initialized;
337         vector<HardwareDevice *> _devices;
338         Driver(struct sr_dev_driver *structure);
339         ~Driver();
340         friend class Context;
341         friend class HardwareDevice;
342         friend class ChannelGroup;
343 };
344
345 /** A generic device, either hardware or virtual */
346 class SR_API Device : public Configurable
347 {
348 public:
349         /** Description identifying this device. */
350         string description();
351         /** Vendor name for this device. */
352         string vendor();
353         /** Model name for this device. */
354         string model();
355         /** Version string for this device. */
356         string version();
357         /** Serial number for this device. */
358         string serial_number();
359         /** Connection ID for this device. */
360         string connection_id();
361         /** List of the channels available on this device. */
362         vector<shared_ptr<Channel> > channels();
363         /** Channel groups available on this device, indexed by name. */
364         map<string, shared_ptr<ChannelGroup> > channel_groups();
365         /** Open device. */
366         void open();
367         /** Close device. */
368         void close();
369 protected:
370         Device(struct sr_dev_inst *structure);
371         ~Device();
372         virtual shared_ptr<Device> get_shared_from_this() = 0;
373         shared_ptr<Channel> get_channel(struct sr_channel *ptr);
374         struct sr_dev_inst *_structure;
375         map<struct sr_channel *, Channel *> _channels;
376         map<string, ChannelGroup *> _channel_groups;
377         /** Deleter needed to allow shared_ptr use with protected destructor. */
378         class Deleter
379         {
380         public:
381                 void operator()(Device *device) { delete device; }
382         };
383         friend class Deleter;
384         friend class Session;
385         friend class Channel;
386         friend class ChannelGroup;
387         friend class Output;
388         friend class Analog;
389 };
390
391 /** A real hardware device, connected via a driver */
392 class SR_API HardwareDevice :
393         public UserOwned<HardwareDevice, struct sr_dev_inst>,
394         public Device
395 {
396 public:
397         /** Driver providing this device. */
398         shared_ptr<Driver> driver();
399 protected:
400         HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
401         ~HardwareDevice();
402         shared_ptr<Device> get_shared_from_this();
403         shared_ptr<Driver> _driver;
404         /** Deleter needed to allow shared_ptr use with protected destructor. */
405         class Deleter
406         {
407         public:
408                 void operator()(HardwareDevice *device) { delete device; }
409         };
410         friend class Deleter;
411         friend class Driver;
412         friend class ChannelGroup;
413 };
414
415 /** A channel on a device */
416 class SR_API Channel :
417         public ParentOwned<Channel, Device, struct sr_channel>
418 {
419 public:
420         /** Current name of this channel. */
421         string name();
422         /** Set the name of this channel. *
423          * @param name Name string to set. */
424         void set_name(string name);
425         /** Type of this channel. */
426         const ChannelType *type();
427         /** Enabled status of this channel. */
428         bool enabled();
429         /** Set the enabled status of this channel.
430          * @param value Boolean value to set. */
431         void set_enabled(bool value);
432         /** Get the index number of this channel. */
433         unsigned int index();
434 protected:
435         Channel(struct sr_channel *structure);
436         ~Channel();
437         const ChannelType * const _type;
438         friend class Device;
439         friend class ChannelGroup;
440         friend class Session;
441         friend class TriggerStage;
442 };
443
444 /** A group of channels on a device, which share some configuration */
445 class SR_API ChannelGroup :
446         public ParentOwned<ChannelGroup, Device, struct sr_channel_group>,
447         public Configurable
448 {
449 public:
450         /** Name of this channel group. */
451         string name();
452         /** List of the channels in this group. */
453         vector<shared_ptr<Channel> > channels();
454 protected:
455         ChannelGroup(Device *device, struct sr_channel_group *structure);
456         ~ChannelGroup();
457         vector<Channel *> _channels;
458         friend class Device;
459 };
460
461 /** A trigger configuration */
462 class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
463 {
464 public:
465         /** Name of this trigger configuration. */
466         string name();
467         /** List of the stages in this trigger. */
468         vector<shared_ptr<TriggerStage> > stages();
469         /** Add a new stage to this trigger. */
470         shared_ptr<TriggerStage> add_stage();
471 protected:
472         Trigger(shared_ptr<Context> context, string name);
473         ~Trigger();
474         shared_ptr<Context> _context;
475         vector<TriggerStage *> _stages;
476         friend class Deleter;
477         friend class Context;
478         friend class Session;
479 };
480
481 /** A stage in a trigger configuration */
482 class SR_API TriggerStage :
483         public ParentOwned<TriggerStage, Trigger, struct sr_trigger_stage>
484 {
485 public:
486         /** Index number of this stage. */
487         int number();
488         /** List of match conditions on this stage. */
489         vector<shared_ptr<TriggerMatch> > matches();
490         /** Add a new match condition to this stage.
491          * @param channel Channel to match on.
492          * @param type TriggerMatchType to apply. */
493         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
494         /** Add a new match condition to this stage.
495          * @param channel Channel to match on.
496          * @param type TriggerMatchType to apply.
497          * @param value Threshold value. */
498         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
499 protected:
500         vector<TriggerMatch *> _matches;
501         TriggerStage(struct sr_trigger_stage *structure);
502         ~TriggerStage();
503         friend class Trigger;
504 };
505
506 /** A match condition in a trigger configuration  */
507 class SR_API TriggerMatch :
508         public ParentOwned<TriggerMatch, TriggerStage, struct sr_trigger_match>
509 {
510 public:
511         /** Channel this condition matches on. */
512         shared_ptr<Channel> channel();
513         /** Type of match. */
514         const TriggerMatchType *type();
515         /** Threshold value. */
516         float value();
517 protected:
518         TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
519         ~TriggerMatch();
520         shared_ptr<Channel> _channel;
521         friend class TriggerStage;
522 };
523
524 /** Type of datafeed callback */
525 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
526         DatafeedCallbackFunction;
527
528 /* Data required for C callback function to call a C++ datafeed callback */
529 class SR_PRIV DatafeedCallbackData
530 {
531 public:
532         void run(const struct sr_dev_inst *sdi,
533                 const struct sr_datafeed_packet *pkt);
534 protected:
535         DatafeedCallbackFunction _callback;
536         DatafeedCallbackData(Session *session,
537                 DatafeedCallbackFunction callback);
538         Session *_session;
539         friend class Session;
540 };
541
542 /** Type of source callback */
543 typedef function<bool(Glib::IOCondition)>
544         SourceCallbackFunction;
545
546 /* Data required for C callback function to call a C++ source callback */
547 class SR_PRIV SourceCallbackData
548 {
549 public:
550         bool run(int revents);
551 protected:
552         SourceCallbackData(shared_ptr<EventSource> source);
553         shared_ptr<EventSource> _source;
554         friend class Session;
555 };
556
557 /** An I/O event source */
558 class SR_API EventSource
559 {
560 public:
561         /** Create an event source from a file descriptor.
562          * @param fd File descriptor.
563          * @param events GLib IOCondition event mask.
564          * @param timeout Timeout in milliseconds.
565          * @param callback Callback of the form callback(events) */
566         static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
567                 int timeout, SourceCallbackFunction callback);
568         /** Create an event source from a GLib PollFD
569          * @param pollfd GLib PollFD
570          * @param timeout Timeout in milliseconds.
571          * @param callback Callback of the form callback(events) */
572         static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
573                 SourceCallbackFunction callback);
574         /** Create an event source from a GLib IOChannel
575          * @param channel GLib IOChannel.
576          * @param events GLib IOCondition event mask.
577          * @param timeout Timeout in milliseconds.
578          * @param callback Callback of the form callback(events) */
579         static shared_ptr<EventSource> create(
580                 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
581                 int timeout, SourceCallbackFunction callback);
582 protected:
583         EventSource(int timeout, SourceCallbackFunction callback);
584         ~EventSource();
585         enum source_type {
586                 SOURCE_FD,
587                 SOURCE_POLLFD,
588                 SOURCE_IOCHANNEL
589         } _type;
590         int _fd;
591         Glib::PollFD _pollfd;
592         Glib::RefPtr<Glib::IOChannel> _channel;
593         Glib::IOCondition _events;
594         int _timeout;
595         SourceCallbackFunction _callback;
596         /** Deleter needed to allow shared_ptr use with protected destructor. */
597         class Deleter
598         {
599         public:
600                 void operator()(EventSource *source) { delete source; }
601         };
602         friend class Deleter;
603         friend class Session;
604         friend class SourceCallbackData;
605 };
606
607 /** A virtual device associated with a stored session */
608 class SR_API SessionDevice :
609         public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
610         public Device
611 {
612 protected:
613         SessionDevice(struct sr_dev_inst *sdi);
614         ~SessionDevice();
615         shared_ptr<Device> get_shared_from_this();
616         /** Deleter needed to allow shared_ptr use with protected destructor. */
617         class Deleter
618         {
619         public:
620                 void operator()(SessionDevice *device) { delete device; }
621         };
622         friend class Deleter;
623         friend class Session;
624 };
625
626 /** A sigrok session */
627 class SR_API Session : public UserOwned<Session, struct sr_session>
628 {
629 public:
630         /** Add a device to this session.
631          * @param device Device to add. */
632         void add_device(shared_ptr<Device> device);
633         /** List devices attached to this session. */
634         vector<shared_ptr<Device> > devices();
635         /** Remove all devices from this session. */
636         void remove_devices();
637         /** Add a datafeed callback to this session.
638          * @param callback Callback of the form callback(Device, Packet). */
639         void add_datafeed_callback(DatafeedCallbackFunction callback);
640         /** Remove all datafeed callbacks from this session. */
641         void remove_datafeed_callbacks();
642         /** Add an I/O event source.
643          * @param source EventSource to add. */
644         void add_source(shared_ptr<EventSource> source);
645         /** Remove an event source.
646          * @param source EventSource to remove. */
647         void remove_source(shared_ptr<EventSource> source);
648         /** Start the session. */
649         void start();
650         /** Run the session event loop. */
651         void run();
652         /** Stop the session. */
653         void stop();
654         /** Begin saving session to a file.
655          * @param filename File name string. */
656         void begin_save(string filename);
657         /** Append a packet to the session file being saved.
658          * @param packet Packet to append. */
659         void append(shared_ptr<Packet> packet);
660         /** Append raw logic data to the session file being saved. */
661         void append(void *data, size_t length, unsigned int unit_size);
662         /** Get current trigger setting. */
663         shared_ptr<Trigger> trigger();
664         /** Set trigger setting.
665          * @param trigger Trigger object to use. */
666         void set_trigger(shared_ptr<Trigger> trigger);
667 protected:
668         Session(shared_ptr<Context> context);
669         Session(shared_ptr<Context> context, string filename);
670         ~Session();
671         const shared_ptr<Context> _context;
672         map<const struct sr_dev_inst *, shared_ptr<Device> > _devices;
673         vector<DatafeedCallbackData *> _datafeed_callbacks;
674         map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
675         bool _saving;
676         bool _save_initialized;
677         string _save_filename;
678         uint64_t _save_samplerate;
679         shared_ptr<Trigger> _trigger;
680         friend class Deleter;
681         friend class Context;
682         friend class DatafeedCallbackData;
683 };
684
685 /** A packet on the session datafeed */
686 class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
687 {
688 public:
689         /** Type of this packet. */
690         const PacketType *type();
691         /** Payload of this packet. */
692         shared_ptr<PacketPayload> payload();
693 protected:
694         Packet(shared_ptr<Device> device,
695                 const struct sr_datafeed_packet *structure);
696         ~Packet();
697         shared_ptr<Device> _device;
698         PacketPayload *_payload;
699         friend class Deleter;
700         friend class Session;
701         friend class Output;
702         friend class DatafeedCallbackData;
703         friend class Header;
704         friend class Meta;
705         friend class Logic;
706         friend class Analog;
707 };
708
709 /** Abstract base class for datafeed packet payloads */
710 class SR_API PacketPayload
711 {
712 protected:
713         PacketPayload();
714         virtual ~PacketPayload() = 0;
715         virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
716         /** Deleter needed to allow shared_ptr use with protected destructor. */
717         class Deleter
718         {
719         public:
720                 void operator()(PacketPayload *payload) { delete payload; }
721         };
722         friend class Deleter;
723         friend class Packet;
724         friend class Output;
725 };
726
727 /** Payload of a datafeed header packet */
728 class SR_API Header :
729         public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
730         public PacketPayload
731 {
732 public:
733         /* Feed version number. */
734         int feed_version();
735         /* Start time of this session. */
736         Glib::TimeVal start_time();
737 protected:
738         Header(const struct sr_datafeed_header *structure);
739         ~Header();
740         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
741         friend class Packet;
742 };
743
744 /** Payload of a datafeed metadata packet */
745 class SR_API Meta :
746         public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
747         public PacketPayload
748 {
749 public:
750         /* Mapping of (ConfigKey, value) pairs. */
751         map<const ConfigKey *, Glib::VariantBase> config();
752 protected:
753         Meta(const struct sr_datafeed_meta *structure);
754         ~Meta();
755         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
756         map<const ConfigKey *, Glib::VariantBase> _config;
757         friend class Packet;
758 };
759
760 /** Payload of a datafeed packet with logic data */
761 class SR_API Logic :
762         public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
763         public PacketPayload
764 {
765 public:
766         /* Pointer to data. */
767         void *data_pointer();
768         /* Data length in bytes. */
769         size_t data_length();
770         /* Size of each sample in bytes. */
771         unsigned int unit_size();
772 protected:
773         Logic(const struct sr_datafeed_logic *structure);
774         ~Logic();
775         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
776         friend class Packet;
777 };
778
779 /** Payload of a datafeed packet with analog data */
780 class SR_API Analog :
781         public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
782         public PacketPayload
783 {
784 public:
785         /** Pointer to data. */
786         float *data_pointer();
787         /** Number of samples in this packet. */
788         unsigned int num_samples();
789         /** Channels for which this packet contains data. */
790         vector<shared_ptr<Channel> > channels();
791         /** Measured quantity of the samples in this packet. */
792         const Quantity *mq();
793         /** Unit of the samples in this packet. */
794         const Unit *unit();
795         /** Measurement flags associated with the samples in this packet. */
796         vector<const QuantityFlag *> mq_flags();
797 protected:
798         Analog(const struct sr_datafeed_analog *structure);
799         ~Analog();
800         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
801         friend class Packet;
802 };
803
804 /** An input format supported by the library */
805 class SR_API InputFormat :
806         public ParentOwned<InputFormat, Context, const struct sr_input_module>
807 {
808 public:
809         /** Name of this input format. */
810         string name();
811         /** Description of this input format. */
812         string description();
813         /** Options supported by this input format. */
814         map<string, shared_ptr<Option> > options();
815         /** Create an input using this input format.
816          * @param options Mapping of (option name, value) pairs. */
817         shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
818                 map<string, Glib::VariantBase>());
819 protected:
820         InputFormat(const struct sr_input_module *structure);
821         ~InputFormat();
822         friend class Context;
823         friend class InputDevice;
824 };
825
826 /** An input instance (an input format applied to a file or stream) */
827 class SR_API Input : public UserOwned<Input, const struct sr_input>
828 {
829 public:
830         /** Virtual device associated with this input. */
831         shared_ptr<InputDevice> device();
832         /** Send next stream data.
833          * @param data Next stream data. */
834         void send(string data);
835         /** Signal end of input data. */
836         void end();
837 protected:
838         Input(shared_ptr<Context> context, const struct sr_input *structure);
839         ~Input();
840         shared_ptr<Context> _context;
841         InputDevice *_device;
842         friend class Deleter;
843         friend class Context;
844         friend class InputFormat;
845 };
846
847 /** A virtual device associated with an input */
848 class SR_API InputDevice :
849         public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
850         public Device
851 {
852 protected:
853         InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
854         ~InputDevice();
855         shared_ptr<Device> get_shared_from_this();
856         shared_ptr<Input> _input;
857         friend class Input;
858 };
859
860 /** An option used by an output format */
861 class SR_API Option : public UserOwned<Option, const struct sr_option>
862 {
863 public:
864         /** Short name of this option suitable for command line usage. */
865         string id();
866         /** Short name of this option suitable for GUI usage. */
867         string name();
868         /** Description of this option in a sentence. */
869         string description();
870         /** Default value for this option. */
871         Glib::VariantBase default_value();
872         /** Possible values for this option, if a limited set. */
873         vector<Glib::VariantBase> values();
874 protected:
875         Option(const struct sr_option *structure,
876                 shared_ptr<const struct sr_option *> structure_array);
877         ~Option();
878         shared_ptr<const struct sr_option *> _structure_array;
879         friend class Deleter;
880         friend class InputFormat;
881         friend class OutputFormat;
882 };
883
884 /** An output format supported by the library */
885 class SR_API OutputFormat :
886         public ParentOwned<OutputFormat, Context, const struct sr_output_module>
887 {
888 public:
889         /** Name of this output format. */
890         string name();
891         /** Description of this output format. */
892         string description();
893         /** Options supported by this output format. */
894         map<string, shared_ptr<Option> > options();
895         /** Create an output using this format.
896          * @param device Device to output for.
897          * @param options Mapping of (option name, value) pairs. */
898         shared_ptr<Output> create_output(shared_ptr<Device> device,
899                 map<string, Glib::VariantBase> options =
900                         map<string, Glib::VariantBase>());
901 protected:
902         OutputFormat(const struct sr_output_module *structure);
903         ~OutputFormat();
904         friend class Context;
905         friend class Output;
906 };
907
908 /** An output instance (an output format applied to a device) */
909 class SR_API Output : public UserOwned<Output, const struct sr_output>
910 {
911 public:
912         /** Update output with data from the given packet.
913          * @param packet Packet to handle. */
914         string receive(shared_ptr<Packet> packet);
915 protected:
916         Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
917         Output(shared_ptr<OutputFormat> format,
918                 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
919         ~Output();
920         const shared_ptr<OutputFormat> _format;
921         const shared_ptr<Device> _device;
922         const map<string, Glib::VariantBase> _options;
923         friend class Deleter;
924         friend class OutputFormat;
925 };
926
927 /** Base class for objects which wrap an enumeration value from libsigrok */
928 template <typename T> class SR_API EnumValue
929 {
930 public:
931         /** The enum constant associated with this value. */
932         T id() const { return _id; }
933         /** The name associated with this value. */
934         string name() const { return _name; }
935 protected:
936         EnumValue(T id, const char name[]) : _id(id), _name(name) {}
937         ~EnumValue() {}
938         const T _id;
939         const string _name;
940 };
941
942 #include "enums.hpp"
943
944 }
945
946 #endif // LIBSIGROK_HPP