2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
22 @mainpage API Reference
27 The sigrok++ API provides an object-oriented C++ interface to the functionality
28 in libsigrok, including automatic memory and resource management.
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.
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.
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.
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.
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.
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.
73 #include "libsigrok/libsigrok.h"
74 #include <glibmm-2.4/glibmm.h>
86 /* Forward declarations */
91 class SR_API HardwareDevice;
93 class SR_API EventSource;
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;
106 class SR_API PacketPayload;
107 class SR_API PacketType;
108 class SR_API Quantity;
110 class SR_API QuantityFlag;
111 class SR_API InputFileDevice;
113 class SR_API DataType;
115 /** Exception thrown when an error code is returned by any libsigrok call. */
116 class SR_API Error: public exception
122 const char *what() const throw();
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> >
130 /* Parent object which owns this child object's underlying structure.
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.
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
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;
146 shared_ptr<StructureWrapper<Parent, Struct> >
147 get_shared_pointer(Parent *parent)
149 this->parent = static_pointer_cast<Parent>(parent->shared_from_this());
150 return shared_ptr<StructureWrapper<Parent, Struct> >(
153 shared_ptr<StructureWrapper<Parent, Struct> >
154 get_shared_pointer(shared_ptr<Parent> parent)
156 this->parent = parent;
157 return shared_ptr<StructureWrapper<Parent, Struct> >(
161 static void reset_parent(StructureWrapper<Parent, Struct> *object)
163 object->parent.reset();
168 StructureWrapper<Parent, Struct>(Struct *structure) :
174 /** Type of log callback */
175 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
177 /** The global libsigrok context */
178 class SR_API Context : public enable_shared_from_this<Context>
181 /** Create new context */
182 static shared_ptr<Context> create();
183 /** libsigrok package version. */
184 string get_package_version();
185 /** libsigrok library version. */
186 string get_lib_version();
187 /** Available hardware drivers, indexed by name. */
188 map<string, shared_ptr<Driver> > get_drivers();
189 /** Available input formats, indexed by name. */
190 map<string, shared_ptr<InputFormat> > get_input_formats();
191 /** Available output formats, indexed by name. */
192 map<string, shared_ptr<OutputFormat> > get_output_formats();
193 /** Current log level. */
194 const LogLevel *get_log_level();
195 /** Set the log level. */
196 void set_log_level(const LogLevel *level);
197 /** Current log domain. */
198 string get_log_domain();
199 /** Set the log domain. */
200 void set_log_domain(string value);
201 /** Set the log callback. */
202 void set_log_callback(LogCallbackFunction callback);
203 /** Set the log callback to the default handler. */
204 void set_log_callback_default();
205 /** Create a new session. */
206 shared_ptr<Session> create_session();
207 /** Load a saved session. */
208 shared_ptr<Session> load_session(string filename);
209 /** Create a new trigger. */
210 shared_ptr<Trigger> create_trigger(string name);
212 struct sr_context *structure;
213 map<string, Driver *> drivers;
214 map<string, InputFormat *> input_formats;
215 map<string, OutputFormat *> output_formats;
217 LogCallbackFunction log_callback;
220 /** Deleter needed to allow shared_ptr use with protected destructor. */
224 void operator()(Context *context) { delete context; }
226 friend class Deleter;
227 friend class Session;
231 /** A hardware driver provided by the library */
232 class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
235 /** Name of this driver. */
237 /** Long name for this driver. */
238 string get_long_name();
239 /** Scan for devices and return a list of devices found. */
240 vector<shared_ptr<HardwareDevice> > scan(
241 map<const ConfigKey *, Glib::VariantBase> options = {});
244 vector<HardwareDevice *> devices;
245 Driver(struct sr_dev_driver *structure);
247 friend class Context;
248 friend class HardwareDevice;
249 friend class ChannelGroup;
252 /** An object that can be configured. */
253 class SR_API Configurable
256 /** Read configuration for the given key. */
257 Glib::VariantBase config_get(const ConfigKey *key);
258 /** Set configuration for the given key to a specified value. */
259 void config_set(const ConfigKey *key, Glib::VariantBase value);
260 /** Enumerate available values for the given configuration key. */
261 Glib::VariantContainerBase config_list(const ConfigKey *key);
264 struct sr_dev_driver *driver,
265 struct sr_dev_inst *sdi,
266 struct sr_channel_group *channel_group);
267 virtual ~Configurable();
268 struct sr_dev_driver *config_driver;
269 struct sr_dev_inst *config_sdi;
270 struct sr_channel_group *config_channel_group;
273 /** A generic device, either hardware or virtual */
274 class SR_API Device :
276 public StructureWrapper<Context, struct sr_dev_inst>
279 /** Description identifying this device. */
280 string get_description();
281 /** Vendor name for this device. */
283 /** Model name for this device. */
285 /** Version string for this device. */
286 string get_version();
287 /** List of the channels available on this device. */
288 vector<shared_ptr<Channel> > get_channels();
289 /** Channel groups available on this device, indexed by name. */
290 map<string, shared_ptr<ChannelGroup> > get_channel_groups();
296 Device(struct sr_dev_inst *structure);
298 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
299 map<struct sr_channel *, Channel *> channels;
300 map<string, ChannelGroup *> channel_groups;
301 /** Deleter needed to allow shared_ptr use with protected destructor. */
305 void operator()(Device *device) { delete device; }
307 friend class Deleter;
308 friend class Session;
309 friend class Channel;
310 friend class ChannelGroup;
315 /** A real hardware device, connected via a driver */
316 class SR_API HardwareDevice : public Device
319 /** Driver providing this device. */
320 shared_ptr<Driver> get_driver();
322 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
326 friend class ChannelGroup;
329 /** A channel on a device */
330 class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
333 /** Current name of this channel. */
335 /** Set the name of this channel. */
336 void set_name(string name);
337 /** Type of this channel. */
338 const ChannelType *get_type();
339 /** Enabled status of this channel. */
341 /** Set the enabled status of this channel. */
342 void set_enabled(bool value);
344 Channel(struct sr_channel *structure);
346 const ChannelType * const type;
348 friend class ChannelGroup;
349 friend class Session;
350 friend class TriggerStage;
353 /** A group of channels on a device, which share some configuration */
354 class SR_API ChannelGroup :
355 public StructureWrapper<Device, struct sr_channel_group>,
359 /** Name of this channel group. */
361 /** List of the channels in this group. */
362 vector<shared_ptr<Channel> > get_channels();
364 ChannelGroup(Device *device, struct sr_channel_group *structure);
366 vector<Channel *> channels;
370 /** A trigger configuration */
371 class SR_API Trigger : public enable_shared_from_this<Trigger>
375 vector<shared_ptr<TriggerStage> > get_stages();
376 shared_ptr<TriggerStage> add_stage();
378 Trigger(shared_ptr<Context> context, string name);
380 struct sr_trigger *structure;
381 shared_ptr<Context> context;
382 vector<TriggerStage *> stages;
383 /** Deleter needed to allow shared_ptr use with protected destructor. */
387 void operator()(Trigger *trigger) { delete trigger; }
389 friend class Context;
390 friend class Session;
393 /** A stage in a trigger configuration */
394 class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
398 vector<shared_ptr<TriggerMatch> > get_matches();
399 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
400 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
402 vector<TriggerMatch *> matches;
403 TriggerStage(struct sr_trigger_stage *structure);
405 friend class Trigger;
408 /** A match condition in a trigger configuration */
409 class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
412 shared_ptr<Channel> get_channel();
413 const TriggerMatchType *get_type();
416 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
418 shared_ptr<Channel> channel;
419 friend class TriggerStage;
422 /** Type of datafeed callback */
423 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
424 DatafeedCallbackFunction;
426 /* Data required for C callback function to call a C++ datafeed callback */
427 class SR_PRIV DatafeedCallbackData
430 void run(const struct sr_dev_inst *sdi,
431 const struct sr_datafeed_packet *pkt);
433 DatafeedCallbackFunction callback;
434 DatafeedCallbackData(Session *session,
435 DatafeedCallbackFunction callback);
437 friend class Session;
440 /** Type of source callback */
441 typedef function<bool(Glib::IOCondition)>
442 SourceCallbackFunction;
444 /* Data required for C callback function to call a C++ source callback */
445 class SR_PRIV SourceCallbackData
448 bool run(int revents);
450 SourceCallbackData(shared_ptr<EventSource> source);
451 shared_ptr<EventSource> source;
452 friend class Session;
455 /** An I/O event source */
456 class SR_API EventSource
459 /** Create an event source from a file descriptor. */
460 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
461 int timeout, SourceCallbackFunction callback);
462 /** Create an event source from a Glib::PollFD */
463 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
464 SourceCallbackFunction callback);
465 /** Create an event source from a Glib::IOChannel */
466 static shared_ptr<EventSource> create(
467 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
468 int timeout, SourceCallbackFunction callback);
470 EventSource(int timeout, SourceCallbackFunction callback);
479 Glib::RefPtr<Glib::IOChannel> channel;
480 Glib::IOCondition events;
482 SourceCallbackFunction callback;
483 /** Deleter needed to allow shared_ptr use with protected destructor. */
487 void operator()(EventSource *source) { delete source; }
489 friend class Deleter;
490 friend class Session;
491 friend class SourceCallbackData;
494 /** A sigrok session */
498 /** Add a device to this session. */
499 void add_device(shared_ptr<Device> device);
500 /** List devices attached to this session. */
501 vector<shared_ptr<Device> > get_devices();
502 /** Remove all devices from this session. */
503 void remove_devices();
504 /** Add a datafeed callback to this session. */
505 void add_datafeed_callback(DatafeedCallbackFunction callback);
506 /** Remove all datafeed callbacks from this session. */
507 void remove_datafeed_callbacks();
508 /** Add an event source. */
509 void add_source(shared_ptr<EventSource> source);
510 /** Remove an event source. */
511 void remove_source(shared_ptr<EventSource> source);
512 /** Start the session. */
514 /** Run the session event loop. */
516 /** Stop the session. */
518 /** Begin saving session to a file. */
519 void begin_save(string filename);
520 /** Append a packet to the session file being saved. */
521 void append(shared_ptr<Packet> packet);
522 /** Append raw logic data to the session file being saved. */
523 void append(void *data, size_t length, unsigned int unit_size);
524 /** Get current trigger setting. */
525 shared_ptr<Trigger> get_trigger();
526 /** Set trigger setting. */
527 void set_trigger(shared_ptr<Trigger> trigger);
529 Session(shared_ptr<Context> context);
530 Session(shared_ptr<Context> context, string filename);
532 struct sr_session *structure;
533 const shared_ptr<Context> context;
534 map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
535 vector<DatafeedCallbackData *> datafeed_callbacks;
536 map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
538 bool save_initialized;
539 string save_filename;
540 uint64_t save_samplerate;
541 shared_ptr<Trigger> trigger;
542 /** Deleter needed to allow shared_ptr use with protected destructor. */
546 void operator()(Session *session) { delete session; }
548 friend class Deleter;
549 friend class Context;
550 friend class DatafeedCallbackData;
553 /** A packet on the session datafeed */
554 class SR_API Packet : public enable_shared_from_this<Packet>
557 /** Type of this packet. */
558 const PacketType *get_type();
559 /** Payload of this packet. */
560 shared_ptr<PacketPayload> get_payload();
562 Packet(shared_ptr<Device> device,
563 const struct sr_datafeed_packet *structure);
565 const struct sr_datafeed_packet *structure;
566 shared_ptr<Device> device;
567 PacketPayload *payload;
568 /** Deleter needed to allow shared_ptr use with protected destructor. */
572 void operator()(Packet *packet) { delete packet; }
574 friend class Deleter;
575 friend class Session;
577 friend class DatafeedCallbackData;
584 /** Abstract base class for datafeed packet payloads */
585 class SR_API PacketPayload
589 virtual ~PacketPayload() = 0;
590 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) {
591 return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
593 /** Deleter needed to allow shared_ptr use with protected destructor. */
597 void operator()(PacketPayload *payload) { delete payload; }
599 friend class Deleter;
604 /** Payload of a datafeed header packet */
605 class SR_API Header : public PacketPayload,
606 public StructureWrapper<Packet, const struct sr_datafeed_header>
609 int get_feed_version();
610 Glib::TimeVal get_start_time();
612 Header(const struct sr_datafeed_header *structure);
614 const struct sr_datafeed_header *structure;
618 /** Payload of a datafeed metadata packet */
619 class SR_API Meta : public PacketPayload,
620 public StructureWrapper<Packet, const struct sr_datafeed_meta>
623 map<const ConfigKey *, Glib::VariantBase> get_config();
625 Meta(const struct sr_datafeed_meta *structure);
627 const struct sr_datafeed_meta *structure;
628 map<const ConfigKey *, Glib::VariantBase> config;
632 /** Payload of a datafeed packet with logic data */
633 class SR_API Logic : public PacketPayload,
634 public StructureWrapper<Packet, const struct sr_datafeed_logic>
637 /* Pointer to data. */
638 void *get_data_pointer();
639 /* Data length in bytes. */
640 size_t get_data_length();
641 /* Size of each sample in bytes. */
642 unsigned int get_unit_size();
644 Logic(const struct sr_datafeed_logic *structure);
646 const struct sr_datafeed_logic *structure;
650 /** Payload of a datafeed packet with analog data */
651 class SR_API Analog : public PacketPayload,
652 public StructureWrapper<Packet, const struct sr_datafeed_analog>
655 /** Pointer to data. */
656 float *get_data_pointer();
657 /** Number of samples in this packet. */
658 unsigned int get_num_samples();
659 /** Channels for which this packet contains data. */
660 vector<shared_ptr<Channel> > get_channels();
661 /** Measured quantity of the samples in this packet. */
662 const Quantity *get_mq();
663 /** Unit of the samples in this packet. */
664 const Unit *get_unit();
665 /** Measurement flags associated with the samples in this packet. */
666 vector<const QuantityFlag *> get_mq_flags();
668 Analog(const struct sr_datafeed_analog *structure);
670 const struct sr_datafeed_analog *structure;
674 /** An input format supported by the library */
675 class SR_API InputFormat :
676 public StructureWrapper<Context, struct sr_input_format>
679 /** Name of this input format. */
681 /** Description of this input format. */
682 string get_description();
683 /** Check whether a given file matches this input format. */
684 bool format_match(string filename);
685 /** Open a file using this input format. */
686 shared_ptr<InputFileDevice> open_file(string filename,
687 map<string, string> options = {});
689 InputFormat(struct sr_input_format *structure);
691 friend class Context;
692 friend class InputFileDevice;
695 /** A virtual device associated with an input file */
696 class SR_API InputFileDevice : public Device
699 /** Load data from file. */
702 InputFileDevice(shared_ptr<InputFormat> format,
703 struct sr_input *input, string filename);
705 struct sr_input *input;
706 shared_ptr<InputFormat> format;
708 /** Deleter needed to allow shared_ptr use with protected destructor. */
712 void operator()(InputFileDevice *device) { delete device; }
714 friend class Deleter;
715 friend class InputFormat;
718 /** An output format supported by the library */
719 class SR_API OutputFormat :
720 public StructureWrapper<Context, struct sr_output_format>
723 /** Name of this output format. */
725 /** Description of this output format. */
726 string get_description();
727 /** Create an output using this format. */
728 shared_ptr<Output> create_output(shared_ptr<Device> device, map<string, string> options = {});
730 OutputFormat(struct sr_output_format *structure);
732 friend class Context;
736 /** An output instance (an output format applied to a device) */
740 /** Update output with data from the given packet. */
741 string receive(shared_ptr<Packet> packet);
743 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
744 Output(shared_ptr<OutputFormat> format,
745 shared_ptr<Device> device, map<string, string> options);
747 struct sr_output *structure;
748 const shared_ptr<OutputFormat> format;
749 const shared_ptr<Device> device;
750 const map<string, string> options;
751 /** Deleter needed to allow shared_ptr use with protected destructor. */
755 void operator()(Output *output) { delete output; }
757 friend class Deleter;
758 friend class OutputFormat;
761 /** Base class for objects which wrap an enumeration value from libsigrok */
762 template <typename T> class SR_API EnumValue
765 /** The enum constant associated with this value. */
766 T get_id() const { return id; }
767 /** The name associated with this value. */
768 string get_name() const { return name; }
770 EnumValue(T id, const char name[]) : id(id), name(name) {}
780 #endif // LIBSIGROK_HPP