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