]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/include/libsigrok/libsigrok.hpp
C++: Expose config key capabilities.
[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         /** List of the channels available on this device. */
358         vector<shared_ptr<Channel> > channels();
359         /** Channel groups available on this device, indexed by name. */
360         map<string, shared_ptr<ChannelGroup> > channel_groups();
361         /** Open device. */
362         void open();
363         /** Close device. */
364         void close();
365 protected:
366         Device(struct sr_dev_inst *structure);
367         ~Device();
368         virtual shared_ptr<Device> get_shared_from_this() = 0;
369         shared_ptr<Channel> get_channel(struct sr_channel *ptr);
370         struct sr_dev_inst *_structure;
371         map<struct sr_channel *, Channel *> _channels;
372         map<string, ChannelGroup *> _channel_groups;
373         /** Deleter needed to allow shared_ptr use with protected destructor. */
374         class Deleter
375         {
376         public:
377                 void operator()(Device *device) { delete device; }
378         };
379         friend class Deleter;
380         friend class Session;
381         friend class Channel;
382         friend class ChannelGroup;
383         friend class Output;
384         friend class Analog;
385 };
386
387 /** A real hardware device, connected via a driver */
388 class SR_API HardwareDevice :
389         public UserOwned<HardwareDevice, struct sr_dev_inst>,
390         public Device
391 {
392 public:
393         /** Driver providing this device. */
394         shared_ptr<Driver> driver();
395 protected:
396         HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
397         ~HardwareDevice();
398         shared_ptr<Device> get_shared_from_this();
399         shared_ptr<Driver> _driver;
400         /** Deleter needed to allow shared_ptr use with protected destructor. */
401         class Deleter
402         {
403         public:
404                 void operator()(HardwareDevice *device) { delete device; }
405         };
406         friend class Deleter;
407         friend class Driver;
408         friend class ChannelGroup;
409 };
410
411 /** A channel on a device */
412 class SR_API Channel :
413         public ParentOwned<Channel, Device, struct sr_channel>
414 {
415 public:
416         /** Current name of this channel. */
417         string name();
418         /** Set the name of this channel. *
419          * @param name Name string to set. */
420         void set_name(string name);
421         /** Type of this channel. */
422         const ChannelType *type();
423         /** Enabled status of this channel. */
424         bool enabled();
425         /** Set the enabled status of this channel.
426          * @param value Boolean value to set. */
427         void set_enabled(bool value);
428         /** Get the index number of this channel. */
429         unsigned int index();
430 protected:
431         Channel(struct sr_channel *structure);
432         ~Channel();
433         const ChannelType * const _type;
434         friend class Device;
435         friend class ChannelGroup;
436         friend class Session;
437         friend class TriggerStage;
438 };
439
440 /** A group of channels on a device, which share some configuration */
441 class SR_API ChannelGroup :
442         public ParentOwned<ChannelGroup, Device, struct sr_channel_group>,
443         public Configurable
444 {
445 public:
446         /** Name of this channel group. */
447         string name();
448         /** List of the channels in this group. */
449         vector<shared_ptr<Channel> > channels();
450 protected:
451         ChannelGroup(Device *device, struct sr_channel_group *structure);
452         ~ChannelGroup();
453         vector<Channel *> _channels;
454         friend class Device;
455 };
456
457 /** A trigger configuration */
458 class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
459 {
460 public:
461         /** Name of this trigger configuration. */
462         string name();
463         /** List of the stages in this trigger. */
464         vector<shared_ptr<TriggerStage> > stages();
465         /** Add a new stage to this trigger. */
466         shared_ptr<TriggerStage> add_stage();
467 protected:
468         Trigger(shared_ptr<Context> context, string name);
469         ~Trigger();
470         shared_ptr<Context> _context;
471         vector<TriggerStage *> _stages;
472         friend class Deleter;
473         friend class Context;
474         friend class Session;
475 };
476
477 /** A stage in a trigger configuration */
478 class SR_API TriggerStage :
479         public ParentOwned<TriggerStage, Trigger, struct sr_trigger_stage>
480 {
481 public:
482         /** Index number of this stage. */
483         int number();
484         /** List of match conditions on this stage. */
485         vector<shared_ptr<TriggerMatch> > matches();
486         /** Add a new match condition to this stage.
487          * @param channel Channel to match on.
488          * @param type TriggerMatchType to apply. */
489         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
490         /** Add a new match condition to this stage.
491          * @param channel Channel to match on.
492          * @param type TriggerMatchType to apply.
493          * @param value Threshold value. */
494         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
495 protected:
496         vector<TriggerMatch *> _matches;
497         TriggerStage(struct sr_trigger_stage *structure);
498         ~TriggerStage();
499         friend class Trigger;
500 };
501
502 /** A match condition in a trigger configuration  */
503 class SR_API TriggerMatch :
504         public ParentOwned<TriggerMatch, TriggerStage, struct sr_trigger_match>
505 {
506 public:
507         /** Channel this condition matches on. */
508         shared_ptr<Channel> channel();
509         /** Type of match. */
510         const TriggerMatchType *type();
511         /** Threshold value. */
512         float value();
513 protected:
514         TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
515         ~TriggerMatch();
516         shared_ptr<Channel> _channel;
517         friend class TriggerStage;
518 };
519
520 /** Type of datafeed callback */
521 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
522         DatafeedCallbackFunction;
523
524 /* Data required for C callback function to call a C++ datafeed callback */
525 class SR_PRIV DatafeedCallbackData
526 {
527 public:
528         void run(const struct sr_dev_inst *sdi,
529                 const struct sr_datafeed_packet *pkt);
530 protected:
531         DatafeedCallbackFunction _callback;
532         DatafeedCallbackData(Session *session,
533                 DatafeedCallbackFunction callback);
534         Session *_session;
535         friend class Session;
536 };
537
538 /** Type of source callback */
539 typedef function<bool(Glib::IOCondition)>
540         SourceCallbackFunction;
541
542 /* Data required for C callback function to call a C++ source callback */
543 class SR_PRIV SourceCallbackData
544 {
545 public:
546         bool run(int revents);
547 protected:
548         SourceCallbackData(shared_ptr<EventSource> source);
549         shared_ptr<EventSource> _source;
550         friend class Session;
551 };
552
553 /** An I/O event source */
554 class SR_API EventSource
555 {
556 public:
557         /** Create an event source from a file descriptor.
558          * @param fd File descriptor.
559          * @param events GLib IOCondition event mask.
560          * @param timeout Timeout in milliseconds.
561          * @param callback Callback of the form callback(events) */
562         static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
563                 int timeout, SourceCallbackFunction callback);
564         /** Create an event source from a GLib PollFD
565          * @param pollfd GLib PollFD
566          * @param timeout Timeout in milliseconds.
567          * @param callback Callback of the form callback(events) */
568         static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
569                 SourceCallbackFunction callback);
570         /** Create an event source from a GLib IOChannel
571          * @param channel GLib IOChannel.
572          * @param events GLib IOCondition event mask.
573          * @param timeout Timeout in milliseconds.
574          * @param callback Callback of the form callback(events) */
575         static shared_ptr<EventSource> create(
576                 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
577                 int timeout, SourceCallbackFunction callback);
578 protected:
579         EventSource(int timeout, SourceCallbackFunction callback);
580         ~EventSource();
581         enum source_type {
582                 SOURCE_FD,
583                 SOURCE_POLLFD,
584                 SOURCE_IOCHANNEL
585         } _type;
586         int _fd;
587         Glib::PollFD _pollfd;
588         Glib::RefPtr<Glib::IOChannel> _channel;
589         Glib::IOCondition _events;
590         int _timeout;
591         SourceCallbackFunction _callback;
592         /** Deleter needed to allow shared_ptr use with protected destructor. */
593         class Deleter
594         {
595         public:
596                 void operator()(EventSource *source) { delete source; }
597         };
598         friend class Deleter;
599         friend class Session;
600         friend class SourceCallbackData;
601 };
602
603 /** A virtual device associated with a stored session */
604 class SR_API SessionDevice :
605         public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
606         public Device
607 {
608 protected:
609         SessionDevice(struct sr_dev_inst *sdi);
610         ~SessionDevice();
611         shared_ptr<Device> get_shared_from_this();
612         /** Deleter needed to allow shared_ptr use with protected destructor. */
613         class Deleter
614         {
615         public:
616                 void operator()(SessionDevice *device) { delete device; }
617         };
618         friend class Deleter;
619         friend class Session;
620 };
621
622 /** A sigrok session */
623 class SR_API Session : public UserOwned<Session, struct sr_session>
624 {
625 public:
626         /** Add a device to this session.
627          * @param device Device to add. */
628         void add_device(shared_ptr<Device> device);
629         /** List devices attached to this session. */
630         vector<shared_ptr<Device> > devices();
631         /** Remove all devices from this session. */
632         void remove_devices();
633         /** Add a datafeed callback to this session.
634          * @param callback Callback of the form callback(Device, Packet). */
635         void add_datafeed_callback(DatafeedCallbackFunction callback);
636         /** Remove all datafeed callbacks from this session. */
637         void remove_datafeed_callbacks();
638         /** Add an I/O event source.
639          * @param source EventSource to add. */
640         void add_source(shared_ptr<EventSource> source);
641         /** Remove an event source.
642          * @param source EventSource to remove. */
643         void remove_source(shared_ptr<EventSource> source);
644         /** Start the session. */
645         void start();
646         /** Run the session event loop. */
647         void run();
648         /** Stop the session. */
649         void stop();
650         /** Begin saving session to a file.
651          * @param filename File name string. */
652         void begin_save(string filename);
653         /** Append a packet to the session file being saved.
654          * @param packet Packet to append. */
655         void append(shared_ptr<Packet> packet);
656         /** Append raw logic data to the session file being saved. */
657         void append(void *data, size_t length, unsigned int unit_size);
658         /** Get current trigger setting. */
659         shared_ptr<Trigger> trigger();
660         /** Set trigger setting.
661          * @param trigger Trigger object to use. */
662         void set_trigger(shared_ptr<Trigger> trigger);
663 protected:
664         Session(shared_ptr<Context> context);
665         Session(shared_ptr<Context> context, string filename);
666         ~Session();
667         const shared_ptr<Context> _context;
668         map<const struct sr_dev_inst *, shared_ptr<Device> > _devices;
669         vector<DatafeedCallbackData *> _datafeed_callbacks;
670         map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
671         bool _saving;
672         bool _save_initialized;
673         string _save_filename;
674         uint64_t _save_samplerate;
675         shared_ptr<Trigger> _trigger;
676         friend class Deleter;
677         friend class Context;
678         friend class DatafeedCallbackData;
679 };
680
681 /** A packet on the session datafeed */
682 class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
683 {
684 public:
685         /** Type of this packet. */
686         const PacketType *type();
687         /** Payload of this packet. */
688         shared_ptr<PacketPayload> payload();
689 protected:
690         Packet(shared_ptr<Device> device,
691                 const struct sr_datafeed_packet *structure);
692         ~Packet();
693         shared_ptr<Device> _device;
694         PacketPayload *_payload;
695         friend class Deleter;
696         friend class Session;
697         friend class Output;
698         friend class DatafeedCallbackData;
699         friend class Header;
700         friend class Meta;
701         friend class Logic;
702         friend class Analog;
703 };
704
705 /** Abstract base class for datafeed packet payloads */
706 class SR_API PacketPayload
707 {
708 protected:
709         PacketPayload();
710         virtual ~PacketPayload() = 0;
711         virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
712         /** Deleter needed to allow shared_ptr use with protected destructor. */
713         class Deleter
714         {
715         public:
716                 void operator()(PacketPayload *payload) { delete payload; }
717         };
718         friend class Deleter;
719         friend class Packet;
720         friend class Output;
721 };
722
723 /** Payload of a datafeed header packet */
724 class SR_API Header :
725         public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
726         public PacketPayload
727 {
728 public:
729         /* Feed version number. */
730         int feed_version();
731         /* Start time of this session. */
732         Glib::TimeVal start_time();
733 protected:
734         Header(const struct sr_datafeed_header *structure);
735         ~Header();
736         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
737         friend class Packet;
738 };
739
740 /** Payload of a datafeed metadata packet */
741 class SR_API Meta :
742         public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
743         public PacketPayload
744 {
745 public:
746         /* Mapping of (ConfigKey, value) pairs. */
747         map<const ConfigKey *, Glib::VariantBase> config();
748 protected:
749         Meta(const struct sr_datafeed_meta *structure);
750         ~Meta();
751         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
752         map<const ConfigKey *, Glib::VariantBase> _config;
753         friend class Packet;
754 };
755
756 /** Payload of a datafeed packet with logic data */
757 class SR_API Logic :
758         public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
759         public PacketPayload
760 {
761 public:
762         /* Pointer to data. */
763         void *data_pointer();
764         /* Data length in bytes. */
765         size_t data_length();
766         /* Size of each sample in bytes. */
767         unsigned int unit_size();
768 protected:
769         Logic(const struct sr_datafeed_logic *structure);
770         ~Logic();
771         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
772         friend class Packet;
773 };
774
775 /** Payload of a datafeed packet with analog data */
776 class SR_API Analog :
777         public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
778         public PacketPayload
779 {
780 public:
781         /** Pointer to data. */
782         float *data_pointer();
783         /** Number of samples in this packet. */
784         unsigned int num_samples();
785         /** Channels for which this packet contains data. */
786         vector<shared_ptr<Channel> > channels();
787         /** Measured quantity of the samples in this packet. */
788         const Quantity *mq();
789         /** Unit of the samples in this packet. */
790         const Unit *unit();
791         /** Measurement flags associated with the samples in this packet. */
792         vector<const QuantityFlag *> mq_flags();
793 protected:
794         Analog(const struct sr_datafeed_analog *structure);
795         ~Analog();
796         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
797         friend class Packet;
798 };
799
800 /** An input format supported by the library */
801 class SR_API InputFormat :
802         public ParentOwned<InputFormat, Context, const struct sr_input_module>
803 {
804 public:
805         /** Name of this input format. */
806         string name();
807         /** Description of this input format. */
808         string description();
809         /** Options supported by this input format. */
810         map<string, shared_ptr<Option> > options();
811         /** Create an input using this input format.
812          * @param options Mapping of (option name, value) pairs. */
813         shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
814                 map<string, Glib::VariantBase>());
815 protected:
816         InputFormat(const struct sr_input_module *structure);
817         ~InputFormat();
818         friend class Context;
819         friend class InputDevice;
820 };
821
822 /** An input instance (an input format applied to a file or stream) */
823 class SR_API Input : public UserOwned<Input, const struct sr_input>
824 {
825 public:
826         /** Virtual device associated with this input. */
827         shared_ptr<InputDevice> device();
828         /** Send next stream data.
829          * @param data Next stream data. */
830         void send(string data);
831         /** Signal end of input data. */
832         void end();
833 protected:
834         Input(shared_ptr<Context> context, const struct sr_input *structure);
835         ~Input();
836         shared_ptr<Context> _context;
837         InputDevice *_device;
838         friend class Deleter;
839         friend class Context;
840         friend class InputFormat;
841 };
842
843 /** A virtual device associated with an input */
844 class SR_API InputDevice :
845         public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
846         public Device
847 {
848 protected:
849         InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
850         ~InputDevice();
851         shared_ptr<Device> get_shared_from_this();
852         shared_ptr<Input> _input;
853         friend class Input;
854 };
855
856 /** An option used by an output format */
857 class SR_API Option : public UserOwned<Option, const struct sr_option>
858 {
859 public:
860         /** Short name of this option suitable for command line usage. */
861         string id();
862         /** Short name of this option suitable for GUI usage. */
863         string name();
864         /** Description of this option in a sentence. */
865         string description();
866         /** Default value for this option. */
867         Glib::VariantBase default_value();
868         /** Possible values for this option, if a limited set. */
869         vector<Glib::VariantBase> values();
870 protected:
871         Option(const struct sr_option *structure,
872                 shared_ptr<const struct sr_option *> structure_array);
873         ~Option();
874         shared_ptr<const struct sr_option *> _structure_array;
875         friend class Deleter;
876         friend class InputFormat;
877         friend class OutputFormat;
878 };
879
880 /** An output format supported by the library */
881 class SR_API OutputFormat :
882         public ParentOwned<OutputFormat, Context, const struct sr_output_module>
883 {
884 public:
885         /** Name of this output format. */
886         string name();
887         /** Description of this output format. */
888         string description();
889         /** Options supported by this output format. */
890         map<string, shared_ptr<Option> > options();
891         /** Create an output using this format.
892          * @param device Device to output for.
893          * @param options Mapping of (option name, value) pairs. */
894         shared_ptr<Output> create_output(shared_ptr<Device> device,
895                 map<string, Glib::VariantBase> options =
896                         map<string, Glib::VariantBase>());
897 protected:
898         OutputFormat(const struct sr_output_module *structure);
899         ~OutputFormat();
900         friend class Context;
901         friend class Output;
902 };
903
904 /** An output instance (an output format applied to a device) */
905 class SR_API Output : public UserOwned<Output, const struct sr_output>
906 {
907 public:
908         /** Update output with data from the given packet.
909          * @param packet Packet to handle. */
910         string receive(shared_ptr<Packet> packet);
911 protected:
912         Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
913         Output(shared_ptr<OutputFormat> format,
914                 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
915         ~Output();
916         const shared_ptr<OutputFormat> _format;
917         const shared_ptr<Device> _device;
918         const map<string, Glib::VariantBase> _options;
919         friend class Deleter;
920         friend class OutputFormat;
921 };
922
923 /** Base class for objects which wrap an enumeration value from libsigrok */
924 template <typename T> class SR_API EnumValue
925 {
926 public:
927         /** The enum constant associated with this value. */
928         T id() const { return _id; }
929         /** The name associated with this value. */
930         string name() const { return _name; }
931 protected:
932         EnumValue(T id, const char name[]) : _id(id), _name(name) {}
933         ~EnumValue() {}
934         const T _id;
935         const string _name;
936 };
937
938 #include "enums.hpp"
939
940 }
941
942 #endif // LIBSIGROK_HPP