]> sigrok.org Git - libsigrok.git/blame - 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
CommitLineData
c23c8659
ML
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
07443fd2
ML
20/**
21
22@mainpage API Reference
23
24Introduction
25------------
26
27The sigrok++ API provides an object-oriented C++ interface to the functionality
28in libsigrok, including automatic memory and resource management.
29
30It is built on top of the public libsigrok C API, and is designed to be used as
31a standalone alternative API. Programs should not mix usage of the C and C++
32APIs; the C++ interface code needs to have full control of all C API calls for
33resources to be managed correctly.
34
35Memory management
36-----------------
37
38All runtime objects created through the C++ API are passed and accessed via
39shared pointers, using the C++11 std::shared_ptr implementation. This means
40that a reference count is kept for each object.
41
42Shared pointers can be copied and assigned in a user's program, automatically
43updating their reference count and deleting objects when they are no longer in
44use. The C++ interface code also keeps track of internal dependencies between
45libsigrok resources, and ensures that objects are not prematurely deleted when
46their resources are in use by other objects.
47
48This means that management of sigrok++ objects and their underlying libsigrok
49resources can be treated as fully automatic. As long as all shared pointers to
50objects are deleted or reassigned when no longer in use, all underlying
51resources will be released at the right time.
52
53Getting started
54---------------
55
56Usage of the C++ API needs to begin with a call to sigrok::Context::create().
57This will create the global libsigrok context and returns a shared pointer to
58the sigrok::Context object. Methods on this object provide access to the
59hardware drivers, input and output formats supported by the library, as well
60as means of creating other objects such as sessions and triggers.
61
62Error handling
63--------------
64
65When any libsigrok C API call returns an error, a sigrok::Error exception is
66raised, which provides access to the error code and description.
67
68*/
69
c23c8659
ML
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
81namespace sigrok
82{
83
84using namespace std;
85
86/* Forward declarations */
87class SR_API Error;
88class SR_API Context;
89class SR_API Driver;
90class SR_API Device;
91class SR_API HardwareDevice;
92class SR_API Channel;
93class SR_API EventSource;
94class SR_API Session;
95class SR_API ConfigKey;
96class SR_API InputFormat;
97class SR_API OutputFormat;
98class SR_API LogLevel;
99class SR_API ChannelGroup;
100class SR_API Trigger;
101class SR_API TriggerStage;
102class SR_API TriggerMatch;
103class SR_API TriggerMatchType;
104class SR_API ChannelType;
105class SR_API Packet;
106class SR_API PacketPayload;
107class SR_API PacketType;
108class SR_API Quantity;
109class SR_API Unit;
110class SR_API QuantityFlag;
111class SR_API InputFileDevice;
112class SR_API Output;
113class SR_API DataType;
114
115/** Exception thrown when an error code is returned by any libsigrok call. */
116class SR_API Error: public exception
117{
118public:
119 Error(int result);
120 ~Error() throw();
121 const int result;
122 const char *what() const throw();
123};
124
07443fd2 125/* Base template for most classes which wrap a struct type from libsigrok. */
c23c8659
ML
126template <class Parent, typename Struct> class SR_API StructureWrapper :
127 public enable_shared_from_this<StructureWrapper<Parent, Struct> >
128{
129public:
07443fd2 130 /* Parent object which owns this child object's underlying structure.
c23c8659
ML
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;
144protected:
145 Struct *structure;
146
147 StructureWrapper<Parent, Struct>(Struct *structure) :
148 structure(structure)
149 {
150 }
151};
152
153/** Type of log callback */
154typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
155
07443fd2 156/** The global libsigrok context */
c23c8659
ML
157class SR_API Context : public enable_shared_from_this<Context>
158{
159public:
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);
190protected:
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
07443fd2 210/** A hardware driver provided by the library */
c23c8659
ML
211class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
212{
213public:
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 = {});
221protected:
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. */
232class SR_API Configurable
233{
234public:
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);
241protected:
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
07443fd2 252/** A generic device, either hardware or virtual */
c23c8659
ML
253class SR_API Device :
254 public Configurable,
255 public StructureWrapper<Context, struct sr_dev_inst>
256{
257public:
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();
270protected:
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
07443fd2 287/** A real hardware device, connected via a driver */
c23c8659
ML
288class SR_API HardwareDevice : public Device
289{
290public:
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();
295protected:
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
07443fd2 304/** A channel on a device */
c23c8659
ML
305class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
306{
307public:
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);
318protected:
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
07443fd2 328/** A group of channels on a device, which share some configuration */
c23c8659
ML
329class SR_API ChannelGroup :
330 public StructureWrapper<HardwareDevice, struct sr_channel_group>,
331 public Configurable
332{
333public:
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();
338protected:
339 ChannelGroup(HardwareDevice *device, struct sr_channel_group *structure);
340 ~ChannelGroup();
341 vector<Channel *> channels;
342 friend class HardwareDevice;
343};
344
07443fd2 345/** A trigger configuration */
c23c8659
ML
346class SR_API Trigger : public enable_shared_from_this<Trigger>
347{
348public:
349 string get_name();
350 vector<shared_ptr<TriggerStage> > get_stages();
351 shared_ptr<TriggerStage> add_stage();
352protected:
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;
6fa0eb86 365 friend class Session;
c23c8659
ML
366};
367
07443fd2 368/** A stage in a trigger configuration */
c23c8659
ML
369class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
370{
371public:
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);
376protected:
377 vector<TriggerMatch *> matches;
378 TriggerStage(struct sr_trigger_stage *structure);
379 ~TriggerStage();
380 friend class Trigger;
381};
382
07443fd2 383/** A match condition in a trigger configuration */
c23c8659
ML
384class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
385{
386public:
387 shared_ptr<Channel> get_channel();
388 const TriggerMatchType *get_type();
389 float get_value();
390protected:
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 */
398typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
399 DatafeedCallbackFunction;
400
07443fd2 401/* Data required for C callback function to call a C++ datafeed callback */
c23c8659
ML
402class SR_PRIV DatafeedCallbackData
403{
404public:
405 void run(const struct sr_dev_inst *sdi,
406 const struct sr_datafeed_packet *pkt);
407protected:
408 DatafeedCallbackFunction callback;
409 DatafeedCallbackData(Session *session,
410 DatafeedCallbackFunction callback);
411 Session *session;
412 friend class Session;
413};
414
415/** Type of source callback */
416typedef function<bool(Glib::IOCondition)>
417 SourceCallbackFunction;
418
07443fd2 419/* Data required for C callback function to call a C++ source callback */
c23c8659
ML
420class SR_PRIV SourceCallbackData
421{
422public:
423 bool run(int revents);
424protected:
425 SourceCallbackData(shared_ptr<EventSource> source);
426 shared_ptr<EventSource> source;
427 friend class Session;
428};
429
07443fd2 430/** An I/O event source */
c23c8659
ML
431class SR_API EventSource
432{
433public:
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);
444protected:
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
07443fd2 469/** A sigrok session */
c23c8659
ML
470class SR_API Session
471{
472public:
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);
6fa0eb86
ML
497 /** Get current trigger setting. */
498 shared_ptr<Trigger> get_trigger();
499 /** Set trigger setting. */
500 void set_trigger(shared_ptr<Trigger> trigger);
c23c8659
ML
501protected:
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;
6fa0eb86 514 shared_ptr<Trigger> trigger;
c23c8659
ML
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
07443fd2 526/** A packet on the session datafeed */
c23c8659
ML
527class SR_API Packet
528{
529public:
90ba83f2
ML
530 /** Type of this packet. */
531 const PacketType *get_type();
c23c8659
ML
532 /** Payload of this packet. */
533 PacketPayload *get_payload();
534protected:
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
07443fd2 551/** Abstract base class for datafeed packet payloads */
c23c8659
ML
552class SR_API PacketPayload
553{
554protected:
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
07443fd2 563/** Payload of a datafeed packet with logic data */
c23c8659
ML
564class SR_API Logic : public PacketPayload
565{
566protected:
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
07443fd2 576/** Payload of a datafeed packet with analog data */
c23c8659
ML
577class SR_API Analog : public PacketPayload
578{
579public:
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();
588protected:
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
07443fd2 597/** An input format supported by the library */
c23c8659
ML
598class SR_API InputFormat :
599 public StructureWrapper<Context, struct sr_input_format>
600{
601public:
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 = {});
611protected:
612 InputFormat(struct sr_input_format *structure);
613 ~InputFormat();
614 friend class Context;
615 friend class InputFileDevice;
616};
617
07443fd2 618/** A virtual device associated with an input file */
c23c8659
ML
619class SR_API InputFileDevice : public Device
620{
621public:
622 /** Load data from file. */
623 void load();
624protected:
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
07443fd2 641/** An output format supported by the library */
c23c8659
ML
642class SR_API OutputFormat :
643 public StructureWrapper<Context, struct sr_output_format>
644{
645public:
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 = {});
652protected:
653 OutputFormat(struct sr_output_format *structure);
654 ~OutputFormat();
655 friend class Context;
656 friend class Output;
657};
658
07443fd2 659/** An output instance (an output format applied to a device) */
c23c8659
ML
660class SR_API Output
661{
662public:
663 /** Update output with data from the given packet. */
664 string receive(shared_ptr<Packet> packet);
665protected:
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 */
685template <typename T> class SR_API EnumValue
686{
687public:
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; }
692protected:
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