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;
116 /** Exception thrown when an error code is returned by any libsigrok call. */
117 class SR_API Error: public exception
123 const char *what() const throw();
126 /* Base template for most classes which wrap a struct type from libsigrok. */
127 template <class Parent, typename Struct> class SR_API StructureWrapper :
128 public enable_shared_from_this<StructureWrapper<Parent, Struct> >
131 /* Parent object which owns this child object's underlying structure.
133 This shared pointer will be null when this child is unused, but
134 will be assigned to point to the parent before any shared pointer
135 to this child is handed out to the user.
137 When the reference count of this child falls to zero, this shared
138 pointer to its parent is reset by a custom deleter on the child's
141 This strategy ensures that the destructors for both the child and
142 the parent are called at the correct time, i.e. only when all
143 references to both the parent and all its children are gone. */
144 shared_ptr<Parent> parent;
147 shared_ptr<StructureWrapper<Parent, Struct> >
148 get_shared_pointer(Parent *parent)
150 this->parent = static_pointer_cast<Parent>(parent->shared_from_this());
151 return shared_ptr<StructureWrapper<Parent, Struct> >(
154 shared_ptr<StructureWrapper<Parent, Struct> >
155 get_shared_pointer(shared_ptr<Parent> parent)
157 this->parent = parent;
158 return shared_ptr<StructureWrapper<Parent, Struct> >(
162 static void reset_parent(StructureWrapper<Parent, Struct> *object)
164 object->parent.reset();
169 StructureWrapper<Parent, Struct>(Struct *structure) :
175 /** Type of log callback */
176 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
178 /** The global libsigrok context */
179 class SR_API Context : public enable_shared_from_this<Context>
182 /** Create new context */
183 static shared_ptr<Context> create();
184 /** libsigrok package version. */
185 string get_package_version();
186 /** libsigrok library version. */
187 string get_lib_version();
188 /** Available hardware drivers, indexed by name. */
189 map<string, shared_ptr<Driver> > get_drivers();
190 /** Available input formats, indexed by name. */
191 map<string, shared_ptr<InputFormat> > get_input_formats();
192 /** Available output formats, indexed by name. */
193 map<string, shared_ptr<OutputFormat> > get_output_formats();
194 /** Current log level. */
195 const LogLevel *get_log_level();
196 /** Set the log level.
197 * @param level LogLevel to use. */
198 void set_log_level(const LogLevel *level);
199 /** Current log domain. */
200 string get_log_domain();
201 /** Set the log domain.
202 * @param value Log domain prefix string. */
203 void set_log_domain(string value);
204 /** Set the log callback.
205 * @param callback Callback of the form callback(LogLevel, string). */
206 void set_log_callback(LogCallbackFunction callback);
207 /** Set the log callback to the default handler. */
208 void set_log_callback_default();
209 /** Create a new session. */
210 shared_ptr<Session> create_session();
211 /** Load a saved session.
212 * @param filename File name string. */
213 shared_ptr<Session> load_session(string filename);
214 /** Create a new trigger.
215 * @param name Name string for new trigger. */
216 shared_ptr<Trigger> create_trigger(string name);
218 struct sr_context *structure;
219 map<string, Driver *> drivers;
220 map<string, InputFormat *> input_formats;
221 map<string, OutputFormat *> output_formats;
223 LogCallbackFunction log_callback;
226 /** Deleter needed to allow shared_ptr use with protected destructor. */
230 void operator()(Context *context) { delete context; }
232 friend class Deleter;
233 friend class Session;
237 /** A hardware driver provided by the library */
238 class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
241 /** Name of this driver. */
243 /** Long name for this driver. */
244 string get_long_name();
245 /** Scan for devices and return a list of devices found.
246 * @param options Mapping of (ConfigKey, value) pairs. */
247 vector<shared_ptr<HardwareDevice> > scan(
248 map<const ConfigKey *, Glib::VariantBase> options = {});
251 vector<HardwareDevice *> devices;
252 Driver(struct sr_dev_driver *structure);
254 friend class Context;
255 friend class HardwareDevice;
256 friend class ChannelGroup;
259 /** An object that can be configured. */
260 class SR_API Configurable
263 /** Read configuration for the given key.
264 * @param key ConfigKey to read. */
265 Glib::VariantBase config_get(const ConfigKey *key);
266 /** Set configuration for the given key to a specified value.
267 * @param key ConfigKey to set.
268 * @param value Value to set. */
269 void config_set(const ConfigKey *key, Glib::VariantBase value);
270 /** Enumerate available values for the given configuration key.
271 * @param key ConfigKey to enumerate values for. */
272 Glib::VariantContainerBase config_list(const ConfigKey *key);
275 struct sr_dev_driver *driver,
276 struct sr_dev_inst *sdi,
277 struct sr_channel_group *channel_group);
278 virtual ~Configurable();
279 struct sr_dev_driver *config_driver;
280 struct sr_dev_inst *config_sdi;
281 struct sr_channel_group *config_channel_group;
284 /** A generic device, either hardware or virtual */
285 class SR_API Device :
287 public StructureWrapper<Context, struct sr_dev_inst>
290 /** Description identifying this device. */
291 string get_description();
292 /** Vendor name for this device. */
294 /** Model name for this device. */
296 /** Version string for this device. */
297 string get_version();
298 /** List of the channels available on this device. */
299 vector<shared_ptr<Channel> > get_channels();
300 /** Channel groups available on this device, indexed by name. */
301 map<string, shared_ptr<ChannelGroup> > get_channel_groups();
307 Device(struct sr_dev_inst *structure);
309 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
310 map<struct sr_channel *, Channel *> channels;
311 map<string, ChannelGroup *> channel_groups;
312 /** Deleter needed to allow shared_ptr use with protected destructor. */
316 void operator()(Device *device) { delete device; }
318 friend class Deleter;
319 friend class Session;
320 friend class Channel;
321 friend class ChannelGroup;
326 /** A real hardware device, connected via a driver */
327 class SR_API HardwareDevice : public Device
330 /** Driver providing this device. */
331 shared_ptr<Driver> get_driver();
333 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
337 friend class ChannelGroup;
340 /** A channel on a device */
341 class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
344 /** Current name of this channel. */
346 /** Set the name of this channel. *
347 * @param name Name string to set. */
348 void set_name(string name);
349 /** Type of this channel. */
350 const ChannelType *get_type();
351 /** Enabled status of this channel. */
353 /** Set the enabled status of this channel.
354 * @param value Boolean value to set. */
355 void set_enabled(bool value);
356 /** Get the index number of this channel. */
357 unsigned int get_index();
359 Channel(struct sr_channel *structure);
361 const ChannelType * const type;
363 friend class ChannelGroup;
364 friend class Session;
365 friend class TriggerStage;
368 /** A group of channels on a device, which share some configuration */
369 class SR_API ChannelGroup :
370 public StructureWrapper<Device, struct sr_channel_group>,
374 /** Name of this channel group. */
376 /** List of the channels in this group. */
377 vector<shared_ptr<Channel> > get_channels();
379 ChannelGroup(Device *device, struct sr_channel_group *structure);
381 vector<Channel *> channels;
385 /** A trigger configuration */
386 class SR_API Trigger : public enable_shared_from_this<Trigger>
389 /** Name of this trigger configuration. */
391 /** List of the stages in this trigger. */
392 vector<shared_ptr<TriggerStage> > get_stages();
393 /** Add a new stage to this trigger. */
394 shared_ptr<TriggerStage> add_stage();
396 Trigger(shared_ptr<Context> context, string name);
398 struct sr_trigger *structure;
399 shared_ptr<Context> context;
400 vector<TriggerStage *> stages;
401 /** Deleter needed to allow shared_ptr use with protected destructor. */
405 void operator()(Trigger *trigger) { delete trigger; }
407 friend class Context;
408 friend class Session;
411 /** A stage in a trigger configuration */
412 class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
415 /** Index number of this stage. */
417 /** List of match conditions on this stage. */
418 vector<shared_ptr<TriggerMatch> > get_matches();
419 /** Add a new match condition to this stage.
420 * @param channel Channel to match on.
421 * @param type TriggerMatchType to apply. */
422 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
423 /** Add a new match condition to this stage.
424 * @param channel Channel to match on.
425 * @param type TriggerMatchType to apply.
426 * @param value Threshold value. */
427 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
429 vector<TriggerMatch *> matches;
430 TriggerStage(struct sr_trigger_stage *structure);
432 friend class Trigger;
435 /** A match condition in a trigger configuration */
436 class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
439 /** Channel this condition matches on. */
440 shared_ptr<Channel> get_channel();
441 /** Type of match. */
442 const TriggerMatchType *get_type();
443 /** Threshold value. */
446 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
448 shared_ptr<Channel> channel;
449 friend class TriggerStage;
452 /** Type of datafeed callback */
453 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
454 DatafeedCallbackFunction;
456 /* Data required for C callback function to call a C++ datafeed callback */
457 class SR_PRIV DatafeedCallbackData
460 void run(const struct sr_dev_inst *sdi,
461 const struct sr_datafeed_packet *pkt);
463 DatafeedCallbackFunction callback;
464 DatafeedCallbackData(Session *session,
465 DatafeedCallbackFunction callback);
467 friend class Session;
470 /** Type of source callback */
471 typedef function<bool(Glib::IOCondition)>
472 SourceCallbackFunction;
474 /* Data required for C callback function to call a C++ source callback */
475 class SR_PRIV SourceCallbackData
478 bool run(int revents);
480 SourceCallbackData(shared_ptr<EventSource> source);
481 shared_ptr<EventSource> source;
482 friend class Session;
485 /** An I/O event source */
486 class SR_API EventSource
489 /** Create an event source from a file descriptor.
490 * @param fd File descriptor.
491 * @param events GLib IOCondition event mask.
492 * @param timeout Timeout in milliseconds.
493 * @param callback Callback of the form callback(events) */
494 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
495 int timeout, SourceCallbackFunction callback);
496 /** Create an event source from a GLib PollFD
497 * @param pollfd GLib PollFD
498 * @param timeout Timeout in milliseconds.
499 * @param callback Callback of the form callback(events) */
500 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
501 SourceCallbackFunction callback);
502 /** Create an event source from a GLib IOChannel
503 * @param channel GLib IOChannel.
504 * @param events GLib IOCondition event mask.
505 * @param timeout Timeout in milliseconds.
506 * @param callback Callback of the form callback(events) */
507 static shared_ptr<EventSource> create(
508 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
509 int timeout, SourceCallbackFunction callback);
511 EventSource(int timeout, SourceCallbackFunction callback);
520 Glib::RefPtr<Glib::IOChannel> channel;
521 Glib::IOCondition events;
523 SourceCallbackFunction callback;
524 /** Deleter needed to allow shared_ptr use with protected destructor. */
528 void operator()(EventSource *source) { delete source; }
530 friend class Deleter;
531 friend class Session;
532 friend class SourceCallbackData;
535 /** A sigrok session */
539 /** Add a device to this session.
540 * @param device Device to add. */
541 void add_device(shared_ptr<Device> device);
542 /** List devices attached to this session. */
543 vector<shared_ptr<Device> > get_devices();
544 /** Remove all devices from this session. */
545 void remove_devices();
546 /** Add a datafeed callback to this session.
547 * @param callback Callback of the form callback(Device, Packet). */
548 void add_datafeed_callback(DatafeedCallbackFunction callback);
549 /** Remove all datafeed callbacks from this session. */
550 void remove_datafeed_callbacks();
551 /** Add an I/O event source.
552 * @param source EventSource to add. */
553 void add_source(shared_ptr<EventSource> source);
554 /** Remove an event source.
555 * @param source EventSource to remove. */
556 void remove_source(shared_ptr<EventSource> source);
557 /** Start the session. */
559 /** Run the session event loop. */
561 /** Stop the session. */
563 /** Begin saving session to a file.
564 * @param filename File name string. */
565 void begin_save(string filename);
566 /** Append a packet to the session file being saved.
567 * @param packet Packet to append. */
568 void append(shared_ptr<Packet> packet);
569 /** Append raw logic data to the session file being saved. */
570 void append(void *data, size_t length, unsigned int unit_size);
571 /** Get current trigger setting. */
572 shared_ptr<Trigger> get_trigger();
573 /** Set trigger setting.
574 * @param trigger Trigger object to use. */
575 void set_trigger(shared_ptr<Trigger> trigger);
577 Session(shared_ptr<Context> context);
578 Session(shared_ptr<Context> context, string filename);
580 struct sr_session *structure;
581 const shared_ptr<Context> context;
582 map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
583 vector<DatafeedCallbackData *> datafeed_callbacks;
584 map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
586 bool save_initialized;
587 string save_filename;
588 uint64_t save_samplerate;
589 shared_ptr<Trigger> trigger;
590 /** Deleter needed to allow shared_ptr use with protected destructor. */
594 void operator()(Session *session) { delete session; }
596 friend class Deleter;
597 friend class Context;
598 friend class DatafeedCallbackData;
601 /** A packet on the session datafeed */
602 class SR_API Packet : public enable_shared_from_this<Packet>
605 /** Type of this packet. */
606 const PacketType *get_type();
607 /** Payload of this packet. */
608 shared_ptr<PacketPayload> get_payload();
610 Packet(shared_ptr<Device> device,
611 const struct sr_datafeed_packet *structure);
613 const struct sr_datafeed_packet *structure;
614 shared_ptr<Device> device;
615 PacketPayload *payload;
616 /** Deleter needed to allow shared_ptr use with protected destructor. */
620 void operator()(Packet *packet) { delete packet; }
622 friend class Deleter;
623 friend class Session;
625 friend class DatafeedCallbackData;
632 /** Abstract base class for datafeed packet payloads */
633 class SR_API PacketPayload
637 virtual ~PacketPayload() = 0;
638 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) {
639 return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
641 /** Deleter needed to allow shared_ptr use with protected destructor. */
645 void operator()(PacketPayload *payload) { delete payload; }
647 friend class Deleter;
652 /** Payload of a datafeed header packet */
653 class SR_API Header : public PacketPayload,
654 public StructureWrapper<Packet, const struct sr_datafeed_header>
657 /* Feed version number. */
658 int get_feed_version();
659 /* Start time of this session. */
660 Glib::TimeVal get_start_time();
662 Header(const struct sr_datafeed_header *structure);
664 const struct sr_datafeed_header *structure;
668 /** Payload of a datafeed metadata packet */
669 class SR_API Meta : public PacketPayload,
670 public StructureWrapper<Packet, const struct sr_datafeed_meta>
673 /* Mapping of (ConfigKey, value) pairs. */
674 map<const ConfigKey *, Glib::VariantBase> get_config();
676 Meta(const struct sr_datafeed_meta *structure);
678 const struct sr_datafeed_meta *structure;
679 map<const ConfigKey *, Glib::VariantBase> config;
683 /** Payload of a datafeed packet with logic data */
684 class SR_API Logic : public PacketPayload,
685 public StructureWrapper<Packet, const struct sr_datafeed_logic>
688 /* Pointer to data. */
689 void *get_data_pointer();
690 /* Data length in bytes. */
691 size_t get_data_length();
692 /* Size of each sample in bytes. */
693 unsigned int get_unit_size();
695 Logic(const struct sr_datafeed_logic *structure);
697 const struct sr_datafeed_logic *structure;
701 /** Payload of a datafeed packet with analog data */
702 class SR_API Analog : public PacketPayload,
703 public StructureWrapper<Packet, const struct sr_datafeed_analog>
706 /** Pointer to data. */
707 float *get_data_pointer();
708 /** Number of samples in this packet. */
709 unsigned int get_num_samples();
710 /** Channels for which this packet contains data. */
711 vector<shared_ptr<Channel> > get_channels();
712 /** Measured quantity of the samples in this packet. */
713 const Quantity *get_mq();
714 /** Unit of the samples in this packet. */
715 const Unit *get_unit();
716 /** Measurement flags associated with the samples in this packet. */
717 vector<const QuantityFlag *> get_mq_flags();
719 Analog(const struct sr_datafeed_analog *structure);
721 const struct sr_datafeed_analog *structure;
725 /** An input format supported by the library */
726 class SR_API InputFormat :
727 public StructureWrapper<Context, struct sr_input_format>
730 /** Name of this input format. */
732 /** Description of this input format. */
733 string get_description();
734 /** Check whether a given file matches this input format.
735 * @param filename File name string. */
736 bool format_match(string filename);
737 /** Open a file using this input format.
738 * @param filename File name string.
739 * @param options Mapping of (option name, value) strings. */
740 shared_ptr<InputFileDevice> open_file(string filename,
741 map<string, string> options = {});
743 InputFormat(struct sr_input_format *structure);
745 friend class Context;
746 friend class InputFileDevice;
749 /** A virtual device associated with an input file */
750 class SR_API InputFileDevice : public Device
753 /** Load data from file. */
756 InputFileDevice(shared_ptr<InputFormat> format,
757 struct sr_input *input, string filename);
759 struct sr_input *input;
760 shared_ptr<InputFormat> format;
762 /** Deleter needed to allow shared_ptr use with protected destructor. */
766 void operator()(InputFileDevice *device) { delete device; }
768 friend class Deleter;
769 friend class InputFormat;
772 /** An option used by an output format */
776 /** Short name of this option suitable for command line usage. */
778 /** Short name of this option suitable for GUI usage. */
780 /** Description of this option in a sentence. */
781 string get_description();
782 /** Default value for this option. */
783 Glib::VariantBase get_default_value();
784 /** Possible values for this option, if a limited set. */
785 vector<Glib::VariantBase> get_values();
787 Option(const struct sr_option *structure,
788 shared_ptr<const struct sr_option> structure_array);
790 const struct sr_option *structure;
791 shared_ptr<const struct sr_option> structure_array;
792 /** Deleter needed to allow shared_ptr use with protected destructor. */
796 void operator()(Option *option) { delete option; }
798 friend class Deleter;
799 friend class OutputFormat;
802 /** An output format supported by the library */
803 class SR_API OutputFormat :
804 public StructureWrapper<Context, const struct sr_output_module>
807 /** Name of this output format. */
809 /** Description of this output format. */
810 string get_description();
811 /** Options supported by this output format. */
812 map<string, shared_ptr<Option> > get_options();
813 /** Create an output using this format.
814 * @param device Device to output for.
815 * @param options Mapping of (option name, value) pairs. */
816 shared_ptr<Output> create_output(shared_ptr<Device> device,
817 map<string, Glib::VariantBase> options = {});
819 OutputFormat(const struct sr_output_module *structure);
821 friend class Context;
825 /** An output instance (an output format applied to a device) */
829 /** Update output with data from the given packet.
830 * @param packet Packet to handle. */
831 string receive(shared_ptr<Packet> packet);
833 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
834 Output(shared_ptr<OutputFormat> format,
835 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
837 const struct sr_output *structure;
838 const shared_ptr<OutputFormat> format;
839 const shared_ptr<Device> device;
840 const map<string, Glib::VariantBase> options;
841 /** Deleter needed to allow shared_ptr use with protected destructor. */
845 void operator()(Output *output) { delete output; }
847 friend class Deleter;
848 friend class OutputFormat;
851 /** Base class for objects which wrap an enumeration value from libsigrok */
852 template <typename T> class SR_API EnumValue
855 /** The enum constant associated with this value. */
856 T get_id() const { return id; }
857 /** The name associated with this value. */
858 string get_name() const { return name; }
860 EnumValue(T id, const char name[]) : id(id), name(name) {}
870 #endif // LIBSIGROK_HPP