]> sigrok.org Git - libsigrok.git/blob - bindings/cxx/include/libsigrok/libsigrok.hpp
Add Doxyfile for C++ bindings and adaptations to C++ header file.
[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 InputFileDevice;
112 class SR_API Output;
113 class SR_API DataType;
114
115 /** Exception thrown when an error code is returned by any libsigrok call. */
116 class SR_API Error: public exception
117 {
118 public:
119         Error(int result);
120         ~Error() throw();
121         const int result;
122         const char *what() const throw();
123 };
124
125 /* Base template for most classes which wrap a struct type from libsigrok. */
126 template <class Parent, typename Struct> class SR_API StructureWrapper :
127         public enable_shared_from_this<StructureWrapper<Parent, Struct> >
128 {
129 public:
130         /*  Parent object which owns this child object's underlying structure.
131
132                 This shared pointer will be null when this child is unused, but
133                 will be assigned to point to the parent before any shared pointer
134                 to this child is handed out to the user.
135
136                 When the reference count of this child falls to zero, this shared
137                 pointer to its parent is reset by a custom deleter on the child's
138                 shared pointer.
139
140                 This strategy ensures that the destructors for both the child and
141                 the parent are called at the correct time, i.e. only when all
142                 references to both the parent and all its children are gone. */
143         shared_ptr<Parent> parent;
144 protected:
145         Struct *structure;
146
147         StructureWrapper<Parent, Struct>(Struct *structure) :
148                 structure(structure)
149         {
150         }
151 };
152
153 /** Type of log callback */
154 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
155
156 /** The global libsigrok context */
157 class SR_API Context : public enable_shared_from_this<Context>
158 {
159 public:
160         /** Create new context */
161         static shared_ptr<Context> create();
162         /** libsigrok package version. */
163         string get_package_version();
164         /** libsigrok library version. */
165         string get_lib_version();
166         /** Available hardware drivers, indexed by name. */
167         map<string, shared_ptr<Driver> > get_drivers();
168         /** Available input formats, indexed by name. */
169         map<string, shared_ptr<InputFormat> > get_input_formats();
170         /** Available output formats, indexed by name. */
171         map<string, shared_ptr<OutputFormat> > get_output_formats();
172         /** Current log level. */
173         const LogLevel *get_log_level();
174         /** Set the log level. */
175         void set_log_level(const LogLevel *level);
176         /** Current log domain. */
177         string get_log_domain();
178         /** Set the log domain. */
179         void set_log_domain(string value);
180         /** Set the log callback. */
181         void set_log_callback(LogCallbackFunction callback);
182         /** Set the log callback to the default handler. */
183         void set_log_callback_default();
184         /** Create a new session. */
185         shared_ptr<Session> create_session();
186         /** Load a saved session. */
187         shared_ptr<Session> load_session(string filename);
188         /** Create a new trigger. */
189         shared_ptr<Trigger> create_trigger(string name);
190 protected:
191         struct sr_context *structure;
192         map<string, Driver *> drivers;
193         map<string, InputFormat *> input_formats;
194         map<string, OutputFormat *> output_formats;
195         Session *session;
196         LogCallbackFunction log_callback;
197         Context();
198         ~Context();
199         /** Deleter needed to allow shared_ptr use with protected destructor. */
200         class Deleter
201         {
202         public:
203                 void operator()(Context *context) { delete context; }
204         };
205         friend class Deleter;
206         friend class Session;
207         friend class Driver;
208 };
209
210 /** A hardware driver provided by the library */
211 class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
212 {
213 public:
214         /** Name of this driver. */
215         string get_name();
216         /** Long name for this driver. */
217         string get_long_name();
218         /** Scan for devices and return a list of devices found. */
219         vector<shared_ptr<HardwareDevice> > scan(
220                 map<const ConfigKey *, Glib::VariantBase> options = {});
221 protected:
222         bool initialized;
223         vector<HardwareDevice *> devices;
224         Driver(struct sr_dev_driver *structure);
225         ~Driver();
226         friend class Context;
227         friend class HardwareDevice;
228         friend class ChannelGroup;
229 };
230
231 /** An object that can be configured. */
232 class SR_API Configurable
233 {
234 public:
235         /** Read configuration for the given key. */
236         Glib::VariantBase config_get(const ConfigKey *key);
237         /** Set configuration for the given key to a specified value. */
238         void config_set(const ConfigKey *key, Glib::VariantBase value);
239         /** Enumerate available values for the given configuration key. */
240         Glib::VariantBase config_list(const ConfigKey *key);
241 protected:
242         Configurable(
243                 struct sr_dev_driver *driver,
244                 struct sr_dev_inst *sdi,
245                 struct sr_channel_group *channel_group);
246         virtual ~Configurable();
247         struct sr_dev_driver *config_driver;
248         struct sr_dev_inst *config_sdi;
249         struct sr_channel_group *config_channel_group;
250 };
251
252 /** A generic device, either hardware or virtual */
253 class SR_API Device :
254         public Configurable,
255         public StructureWrapper<Context, struct sr_dev_inst>
256 {
257 public:
258         /** Vendor name for this device. */
259         string get_vendor();
260         /** Model name for this device. */
261         string get_model();
262         /** Version string for this device. */
263         string get_version();
264         /** List of the channels available on this device. */
265         vector<shared_ptr<Channel> > get_channels();
266         /** Open device. */
267         void open();
268         /** Close device. */
269         void close();
270 protected:
271         Device(struct sr_dev_inst *structure);
272         ~Device();
273         vector<Channel *> channels;
274         /** Deleter needed to allow shared_ptr use with protected destructor. */
275         class Deleter
276         {
277         public:
278                 void operator()(Device *device) { delete device; }
279         };
280         friend class Deleter;
281         friend class Session;
282         friend class Channel;
283         friend class ChannelGroup;
284         friend class Output;
285 };
286
287 /** A real hardware device, connected via a driver */
288 class SR_API HardwareDevice : public Device
289 {
290 public:
291         /** Driver providing this device. */
292         shared_ptr<Driver> get_driver();
293         /** Channel groups available on this device, indexed by name. */
294         map<string, shared_ptr<ChannelGroup> > get_channel_groups();
295 protected:
296         HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
297         ~HardwareDevice();
298         Driver *driver;
299         map<string, ChannelGroup *> channel_groups;
300         friend class Driver;
301         friend class ChannelGroup;
302 };
303
304 /** A channel on a device */
305 class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
306 {
307 public:
308         /** Current name of this channel. */
309         string get_name();
310         /** Set the name of this channel. */
311         void set_name(string name);
312         /** Type of this channel. */
313         const ChannelType *get_type();
314         /** Enabled status of this channel. */
315         bool get_enabled();
316         /** Set the enabled status of this channel. */
317         void set_enabled(bool value);
318 protected:
319         Channel(struct sr_channel *structure);
320         ~Channel();
321         const ChannelType * const type;
322         friend class Device;
323         friend class ChannelGroup;
324         friend class Session;
325         friend class TriggerStage;
326 };
327
328 /** A group of channels on a device, which share some configuration */
329 class SR_API ChannelGroup :
330         public StructureWrapper<HardwareDevice, struct sr_channel_group>,
331         public Configurable
332 {
333 public:
334         /** Name of this channel group. */
335         string get_name();
336         /** List of the channels in this group. */
337         vector<shared_ptr<Channel> > get_channels();
338 protected:
339         ChannelGroup(HardwareDevice *device, struct sr_channel_group *structure);
340         ~ChannelGroup();
341         vector<Channel *> channels;
342         friend class HardwareDevice;
343 };
344
345 /** A trigger configuration */
346 class SR_API Trigger : public enable_shared_from_this<Trigger>
347 {
348 public:
349         string get_name();
350         vector<shared_ptr<TriggerStage> > get_stages();
351         shared_ptr<TriggerStage> add_stage();
352 protected:
353         Trigger(shared_ptr<Context> context, string name);
354         ~Trigger();
355         struct sr_trigger *structure;
356         shared_ptr<Context> context;
357         vector<TriggerStage *> stages;
358         /** Deleter needed to allow shared_ptr use with protected destructor. */
359         class Deleter
360         {
361         public:
362                 void operator()(Trigger *trigger) { delete trigger; }
363         };
364         friend class Context;
365         friend class Session;
366 };
367
368 /** A stage in a trigger configuration */
369 class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
370 {
371 public:
372         int get_number();
373         vector<shared_ptr<TriggerMatch> > get_matches();
374         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
375         void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
376 protected:
377         vector<TriggerMatch *> matches;
378         TriggerStage(struct sr_trigger_stage *structure);
379         ~TriggerStage();
380         friend class Trigger;
381 };
382
383 /** A match condition in a trigger configuration  */
384 class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
385 {
386 public:
387         shared_ptr<Channel> get_channel();
388         const TriggerMatchType *get_type();
389         float get_value();
390 protected:
391         TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
392         ~TriggerMatch();
393         shared_ptr<Channel> channel;
394         friend class TriggerStage;
395 };
396
397 /** Type of datafeed callback */
398 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
399         DatafeedCallbackFunction;
400
401 /* Data required for C callback function to call a C++ datafeed callback */
402 class SR_PRIV DatafeedCallbackData
403 {
404 public:
405         void run(const struct sr_dev_inst *sdi,
406                 const struct sr_datafeed_packet *pkt);
407 protected:
408         DatafeedCallbackFunction callback;
409         DatafeedCallbackData(Session *session,
410                 DatafeedCallbackFunction callback);
411         Session *session;
412         friend class Session;
413 };
414
415 /** Type of source callback */
416 typedef function<bool(Glib::IOCondition)>
417         SourceCallbackFunction;
418
419 /* Data required for C callback function to call a C++ source callback */
420 class SR_PRIV SourceCallbackData
421 {
422 public:
423         bool run(int revents);
424 protected:
425         SourceCallbackData(shared_ptr<EventSource> source);
426         shared_ptr<EventSource> source;
427         friend class Session;
428 };
429
430 /** An I/O event source */
431 class SR_API EventSource
432 {
433 public:
434         /** Create an event source from a file descriptor. */
435         static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
436                 int timeout, SourceCallbackFunction callback);
437         /** Create an event source from a Glib::PollFD */
438         static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
439                 SourceCallbackFunction callback);
440         /** Create an event source from a Glib::IOChannel */
441         static shared_ptr<EventSource> create(
442                 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
443                 int timeout, SourceCallbackFunction callback);
444 protected:
445         EventSource(int timeout, SourceCallbackFunction callback);
446         ~EventSource();
447         enum source_type {
448                 SOURCE_FD,
449                 SOURCE_POLLFD,
450                 SOURCE_IOCHANNEL
451         } type;
452         int fd;
453         Glib::PollFD pollfd;
454         Glib::RefPtr<Glib::IOChannel> channel;
455         Glib::IOCondition events;
456         int timeout;
457         SourceCallbackFunction callback;
458         /** Deleter needed to allow shared_ptr use with protected destructor. */
459         class Deleter
460         {
461         public:
462                 void operator()(EventSource *source) { delete source; }
463         };
464         friend class Deleter;
465         friend class Session;
466         friend class SourceCallbackData;
467 };
468
469 /** A sigrok session */
470 class SR_API Session 
471 {
472 public:
473         /** Add a device to this session. */
474         void add_device(shared_ptr<Device> device);
475         /** List devices attached to this session. */
476         vector<shared_ptr<Device> > get_devices();
477         /** Remove all devices from this session. */
478         void remove_devices();
479         /** Add a datafeed callback to this session. */
480         void add_datafeed_callback(DatafeedCallbackFunction callback);
481         /** Remove all datafeed callbacks from this session. */
482         void remove_datafeed_callbacks();
483         /** Add an event source. */
484         void add_source(shared_ptr<EventSource> source);
485         /** Remove an event source. */
486         void remove_source(shared_ptr<EventSource> source);
487         /** Start the session. */
488         void start();
489         /** Run the session event loop. */
490         void run();
491         /** Stop the session. */
492         void stop();
493         /** Begin saving session to a file. */
494         void begin_save(string filename);
495         /** Append a packet to the session file being saved. */
496         void append(shared_ptr<Device> device, shared_ptr<Packet> packet);
497         /** Get current trigger setting. */
498         shared_ptr<Trigger> get_trigger();
499         /** Set trigger setting. */
500         void set_trigger(shared_ptr<Trigger> trigger);
501 protected:
502         Session(shared_ptr<Context> context);
503         Session(shared_ptr<Context> context, string filename);
504         ~Session();
505         struct sr_session *structure;
506         const shared_ptr<Context> context;
507         map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
508         vector<DatafeedCallbackData *> datafeed_callbacks;
509         map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
510         bool saving;
511         bool save_initialized;
512         string save_filename;
513         uint64_t save_samplerate;
514         shared_ptr<Trigger> trigger;
515         /** Deleter needed to allow shared_ptr use with protected destructor. */
516         class Deleter
517         {
518         public:
519                 void operator()(Session *session) { delete session; }
520         };
521         friend class Deleter;
522         friend class Context;
523         friend class DatafeedCallbackData;
524 };
525
526 /** A packet on the session datafeed */
527 class SR_API Packet
528 {
529 public:
530         /** Type of this packet. */
531         const PacketType *get_type();
532         /** Payload of this packet. */
533         PacketPayload *get_payload();
534 protected:
535         Packet(const struct sr_datafeed_packet *structure);
536         ~Packet();
537         const struct sr_datafeed_packet *structure;
538         PacketPayload *payload;
539         /** Deleter needed to allow shared_ptr use with protected destructor. */
540         class Deleter
541         {
542         public:
543                 void operator()(Packet *packet) { delete packet; }
544         };
545         friend class Deleter;
546         friend class Session;
547         friend class Output;
548         friend class DatafeedCallbackData;
549 };
550
551 /** Abstract base class for datafeed packet payloads */
552 class SR_API PacketPayload
553 {
554 protected:
555         PacketPayload();
556         virtual ~PacketPayload() = 0;
557         virtual void *get_data() = 0;
558         virtual size_t get_data_size() = 0;
559         friend class Packet;
560         friend class Output;
561 };
562
563 /** Payload of a datafeed packet with logic data */
564 class SR_API Logic : public PacketPayload
565 {
566 protected:
567         Logic(const struct sr_datafeed_logic *structure);
568         ~Logic();
569         const struct sr_datafeed_logic *structure;
570         vector<uint8_t> data;
571         void *get_data();
572         size_t get_data_size();
573         friend class Packet;
574 };
575
576 /** Payload of a datafeed packet with analog data */
577 class SR_API Analog : public PacketPayload
578 {
579 public:
580         /** Number of samples in this packet. */
581         unsigned int get_num_samples();
582         /** Measured quantity of the samples in this packet. */
583         const Quantity *get_mq();
584         /** Unit of the samples in this packet. */
585         const Unit *get_unit();
586         /** Measurement flags associated with the samples in this packet. */
587         vector<const QuantityFlag *> get_mq_flags();
588 protected:
589         Analog(const struct sr_datafeed_analog *structure);
590         ~Analog();
591         const struct sr_datafeed_analog *structure;
592         void *get_data();
593         size_t get_data_size();
594         friend class Packet;
595 };
596
597 /** An input format supported by the library */
598 class SR_API InputFormat :
599         public StructureWrapper<Context, struct sr_input_format>
600 {
601 public:
602         /** Name of this input format. */
603         string get_name();
604         /** Description of this input format. */
605         string get_description();
606         /** Check whether a given file matches this input format. */
607         bool format_match(string filename);
608         /** Open a file using this input format. */
609         shared_ptr<InputFileDevice> open_file(string filename,
610                 map<string, string> options = {});
611 protected:
612         InputFormat(struct sr_input_format *structure);
613         ~InputFormat();
614         friend class Context;
615         friend class InputFileDevice;
616 };
617
618 /** A virtual device associated with an input file */
619 class SR_API InputFileDevice : public Device
620 {
621 public:
622         /** Load data from file. */
623         void load();
624 protected:
625         InputFileDevice(shared_ptr<InputFormat> format,
626                 struct sr_input *input, string filename);
627         ~InputFileDevice();
628         struct sr_input *input;
629         shared_ptr<InputFormat> format;
630         string filename;
631         /** Deleter needed to allow shared_ptr use with protected destructor. */
632         class Deleter
633         {
634         public:
635                 void operator()(InputFileDevice *device) { delete device; }
636         };
637         friend class Deleter;
638         friend class InputFormat;
639 };
640
641 /** An output format supported by the library */
642 class SR_API OutputFormat :
643         public StructureWrapper<Context, struct sr_output_format>
644 {
645 public:
646         /** Name of this output format. */
647         string get_name();
648         /** Description of this output format. */
649         string get_description();
650         /** Create an output using this format. */
651         shared_ptr<Output> create_output(shared_ptr<Device> device, map<string, string> options = {});
652 protected:
653         OutputFormat(struct sr_output_format *structure);
654         ~OutputFormat();
655         friend class Context;
656         friend class Output;
657 };
658
659 /** An output instance (an output format applied to a device) */
660 class SR_API Output
661 {
662 public:
663         /** Update output with data from the given packet. */
664         string receive(shared_ptr<Packet> packet);
665 protected:
666         Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
667         Output(shared_ptr<OutputFormat> format,
668                 shared_ptr<Device> device, map<string, string> options);
669         ~Output();
670         struct sr_output *structure;
671         const shared_ptr<OutputFormat> format;
672         const shared_ptr<Device> device;
673         const map<string, string> options;
674         /** Deleter needed to allow shared_ptr use with protected destructor. */
675         class Deleter
676         {
677         public:
678                 void operator()(Output *output) { delete output; }
679         };
680         friend class Deleter;
681         friend class OutputFormat;
682 };
683
684 /** Base class for objects which wrap an enumeration value from libsigrok */
685 template <typename T> class SR_API EnumValue
686 {
687 public:
688         /** The enum constant associated with this value. */
689         T get_id() const { return id; }
690         /** The name associated with this value. */
691         string get_name() const { return name; }
692 protected:
693         EnumValue(T id, const char name[]) : id(id), name(name) {}
694         ~EnumValue() {}
695         const T id;
696         const string name;
697 };
698
699 #include "enums.hpp"
700
701 }
702
703 #endif // LIBSIGROK_HPP