]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/include/libsigrok/libsigrok.hpp
Return sensible Device::description() for session and input devices.
[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         virtual 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         /** Description identifying this device. */
613         string description();
614 protected:
615         SessionDevice(struct sr_dev_inst *sdi);
616         ~SessionDevice();
617         shared_ptr<Device> get_shared_from_this();
618         /** Deleter needed to allow shared_ptr use with protected destructor. */
619         class Deleter
620         {
621         public:
622                 void operator()(SessionDevice *device) { delete device; }
623         };
624         friend class Deleter;
625         friend class Session;
626 };
627
628 /** A sigrok session */
629 class SR_API Session : public UserOwned<Session, struct sr_session>
630 {
631 public:
632         /** Add a device to this session.
633          * @param device Device to add. */
634         void add_device(shared_ptr<Device> device);
635         /** List devices attached to this session. */
636         vector<shared_ptr<Device> > devices();
637         /** Remove all devices from this session. */
638         void remove_devices();
639         /** Add a datafeed callback to this session.
640          * @param callback Callback of the form callback(Device, Packet). */
641         void add_datafeed_callback(DatafeedCallbackFunction callback);
642         /** Remove all datafeed callbacks from this session. */
643         void remove_datafeed_callbacks();
644         /** Add an I/O event source.
645          * @param source EventSource to add. */
646         void add_source(shared_ptr<EventSource> source);
647         /** Remove an event source.
648          * @param source EventSource to remove. */
649         void remove_source(shared_ptr<EventSource> source);
650         /** Start the session. */
651         void start();
652         /** Run the session event loop. */
653         void run();
654         /** Stop the session. */
655         void stop();
656         /** Begin saving session to a file.
657          * @param filename File name string. */
658         void begin_save(string filename);
659         /** Append a packet to the session file being saved.
660          * @param packet Packet to append. */
661         void append(shared_ptr<Packet> packet);
662         /** Append raw logic data to the session file being saved. */
663         void append(void *data, size_t length, unsigned int unit_size);
664         /** Get current trigger setting. */
665         shared_ptr<Trigger> trigger();
666         /** Set trigger setting.
667          * @param trigger Trigger object to use. */
668         void set_trigger(shared_ptr<Trigger> trigger);
669 protected:
670         Session(shared_ptr<Context> context);
671         Session(shared_ptr<Context> context, string filename);
672         ~Session();
673         shared_ptr<Device> get_device(const struct sr_dev_inst *sdi);
674         const shared_ptr<Context> _context;
675         map<const struct sr_dev_inst *, SessionDevice *> _owned_devices;
676         map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
677         vector<DatafeedCallbackData *> _datafeed_callbacks;
678         map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
679         string _filename;
680         bool _saving;
681         bool _save_initialized;
682         string _save_filename;
683         uint64_t _save_samplerate;
684         shared_ptr<Trigger> _trigger;
685         friend class Deleter;
686         friend class Context;
687         friend class DatafeedCallbackData;
688         friend class SessionDevice;
689 };
690
691 /** A packet on the session datafeed */
692 class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
693 {
694 public:
695         /** Type of this packet. */
696         const PacketType *type();
697         /** Payload of this packet. */
698         shared_ptr<PacketPayload> payload();
699 protected:
700         Packet(shared_ptr<Device> device,
701                 const struct sr_datafeed_packet *structure);
702         ~Packet();
703         shared_ptr<Device> _device;
704         PacketPayload *_payload;
705         friend class Deleter;
706         friend class Session;
707         friend class Output;
708         friend class DatafeedCallbackData;
709         friend class Header;
710         friend class Meta;
711         friend class Logic;
712         friend class Analog;
713 };
714
715 /** Abstract base class for datafeed packet payloads */
716 class SR_API PacketPayload
717 {
718 protected:
719         PacketPayload();
720         virtual ~PacketPayload() = 0;
721         virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
722         /** Deleter needed to allow shared_ptr use with protected destructor. */
723         class Deleter
724         {
725         public:
726                 void operator()(PacketPayload *payload) { delete payload; }
727         };
728         friend class Deleter;
729         friend class Packet;
730         friend class Output;
731 };
732
733 /** Payload of a datafeed header packet */
734 class SR_API Header :
735         public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
736         public PacketPayload
737 {
738 public:
739         /* Feed version number. */
740         int feed_version();
741         /* Start time of this session. */
742         Glib::TimeVal start_time();
743 protected:
744         Header(const struct sr_datafeed_header *structure);
745         ~Header();
746         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
747         friend class Packet;
748 };
749
750 /** Payload of a datafeed metadata packet */
751 class SR_API Meta :
752         public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
753         public PacketPayload
754 {
755 public:
756         /* Mapping of (ConfigKey, value) pairs. */
757         map<const ConfigKey *, Glib::VariantBase> config();
758 protected:
759         Meta(const struct sr_datafeed_meta *structure);
760         ~Meta();
761         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
762         map<const ConfigKey *, Glib::VariantBase> _config;
763         friend class Packet;
764 };
765
766 /** Payload of a datafeed packet with logic data */
767 class SR_API Logic :
768         public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
769         public PacketPayload
770 {
771 public:
772         /* Pointer to data. */
773         void *data_pointer();
774         /* Data length in bytes. */
775         size_t data_length();
776         /* Size of each sample in bytes. */
777         unsigned int unit_size();
778 protected:
779         Logic(const struct sr_datafeed_logic *structure);
780         ~Logic();
781         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
782         friend class Packet;
783 };
784
785 /** Payload of a datafeed packet with analog data */
786 class SR_API Analog :
787         public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
788         public PacketPayload
789 {
790 public:
791         /** Pointer to data. */
792         float *data_pointer();
793         /** Number of samples in this packet. */
794         unsigned int num_samples();
795         /** Channels for which this packet contains data. */
796         vector<shared_ptr<Channel> > channels();
797         /** Measured quantity of the samples in this packet. */
798         const Quantity *mq();
799         /** Unit of the samples in this packet. */
800         const Unit *unit();
801         /** Measurement flags associated with the samples in this packet. */
802         vector<const QuantityFlag *> mq_flags();
803 protected:
804         Analog(const struct sr_datafeed_analog *structure);
805         ~Analog();
806         shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
807         friend class Packet;
808 };
809
810 /** An input format supported by the library */
811 class SR_API InputFormat :
812         public ParentOwned<InputFormat, Context, const struct sr_input_module>
813 {
814 public:
815         /** Name of this input format. */
816         string name();
817         /** Description of this input format. */
818         string description();
819         /** Options supported by this input format. */
820         map<string, shared_ptr<Option> > options();
821         /** Create an input using this input format.
822          * @param options Mapping of (option name, value) pairs. */
823         shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
824                 map<string, Glib::VariantBase>());
825 protected:
826         InputFormat(const struct sr_input_module *structure);
827         ~InputFormat();
828         friend class Context;
829         friend class InputDevice;
830 };
831
832 /** An input instance (an input format applied to a file or stream) */
833 class SR_API Input : public UserOwned<Input, const struct sr_input>
834 {
835 public:
836         /** Virtual device associated with this input. */
837         shared_ptr<InputDevice> device();
838         /** Send next stream data.
839          * @param data Next stream data. */
840         void send(string data);
841         /** Signal end of input data. */
842         void end();
843 protected:
844         Input(shared_ptr<Context> context, const struct sr_input *structure);
845         ~Input();
846         shared_ptr<Context> _context;
847         InputDevice *_device;
848         friend class Deleter;
849         friend class Context;
850         friend class InputFormat;
851 };
852
853 /** A virtual device associated with an input */
854 class SR_API InputDevice :
855         public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
856         public Device
857 {
858 public:
859         /** Description identifying this device. */
860         string description();
861 protected:
862         InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
863         ~InputDevice();
864         shared_ptr<Device> get_shared_from_this();
865         shared_ptr<Input> _input;
866         friend class Input;
867 };
868
869 /** An option used by an output format */
870 class SR_API Option : public UserOwned<Option, const struct sr_option>
871 {
872 public:
873         /** Short name of this option suitable for command line usage. */
874         string id();
875         /** Short name of this option suitable for GUI usage. */
876         string name();
877         /** Description of this option in a sentence. */
878         string description();
879         /** Default value for this option. */
880         Glib::VariantBase default_value();
881         /** Possible values for this option, if a limited set. */
882         vector<Glib::VariantBase> values();
883 protected:
884         Option(const struct sr_option *structure,
885                 shared_ptr<const struct sr_option *> structure_array);
886         ~Option();
887         shared_ptr<const struct sr_option *> _structure_array;
888         friend class Deleter;
889         friend class InputFormat;
890         friend class OutputFormat;
891 };
892
893 /** An output format supported by the library */
894 class SR_API OutputFormat :
895         public ParentOwned<OutputFormat, Context, const struct sr_output_module>
896 {
897 public:
898         /** Name of this output format. */
899         string name();
900         /** Description of this output format. */
901         string description();
902         /** Options supported by this output format. */
903         map<string, shared_ptr<Option> > options();
904         /** Create an output using this format.
905          * @param device Device to output for.
906          * @param options Mapping of (option name, value) pairs. */
907         shared_ptr<Output> create_output(shared_ptr<Device> device,
908                 map<string, Glib::VariantBase> options =
909                         map<string, Glib::VariantBase>());
910 protected:
911         OutputFormat(const struct sr_output_module *structure);
912         ~OutputFormat();
913         friend class Context;
914         friend class Output;
915 };
916
917 /** An output instance (an output format applied to a device) */
918 class SR_API Output : public UserOwned<Output, const struct sr_output>
919 {
920 public:
921         /** Update output with data from the given packet.
922          * @param packet Packet to handle. */
923         string receive(shared_ptr<Packet> packet);
924 protected:
925         Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
926         Output(shared_ptr<OutputFormat> format,
927                 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
928         ~Output();
929         const shared_ptr<OutputFormat> _format;
930         const shared_ptr<Device> _device;
931         const map<string, Glib::VariantBase> _options;
932         friend class Deleter;
933         friend class OutputFormat;
934 };
935
936 /** Base class for objects which wrap an enumeration value from libsigrok */
937 template <typename T> class SR_API EnumValue
938 {
939 public:
940         /** The enum constant associated with this value. */
941         T id() const { return _id; }
942         /** The name associated with this value. */
943         string name() const { return _name; }
944 protected:
945         EnumValue(T id, const char name[]) : _id(id), _name(name) {}
946         ~EnumValue() {}
947         const T _id;
948         const string _name;
949 };
950
951 #include "enums.hpp"
952
953 }
954
955 #endif // LIBSIGROK_HPP