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