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;
147 StructureWrapper<Parent, Struct>(Struct *structure) :
153 /** Type of log callback */
154 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
156 /** The global libsigrok context */
157 class SR_API Context : public enable_shared_from_this<Context>
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);
191 struct sr_context *structure;
192 map<string, Driver *> drivers;
193 map<string, InputFormat *> input_formats;
194 map<string, OutputFormat *> output_formats;
196 LogCallbackFunction log_callback;
199 /** Deleter needed to allow shared_ptr use with protected destructor. */
203 void operator()(Context *context) { delete context; }
205 friend class Deleter;
206 friend class Session;
210 /** A hardware driver provided by the library */
211 class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
214 /** Name of this driver. */
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 = {});
223 vector<HardwareDevice *> devices;
224 Driver(struct sr_dev_driver *structure);
226 friend class Context;
227 friend class HardwareDevice;
228 friend class ChannelGroup;
231 /** An object that can be configured. */
232 class SR_API Configurable
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);
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;
252 /** A generic device, either hardware or virtual */
253 class SR_API Device :
255 public StructureWrapper<Context, struct sr_dev_inst>
258 /** Vendor name for this device. */
260 /** Model name for this device. */
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();
271 Device(struct sr_dev_inst *structure);
273 vector<Channel *> channels;
274 /** Deleter needed to allow shared_ptr use with protected destructor. */
278 void operator()(Device *device) { delete device; }
280 friend class Deleter;
281 friend class Session;
282 friend class Channel;
283 friend class ChannelGroup;
287 /** A real hardware device, connected via a driver */
288 class SR_API HardwareDevice : public Device
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();
296 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
299 map<string, ChannelGroup *> channel_groups;
301 friend class ChannelGroup;
304 /** A channel on a device */
305 class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
308 /** Current name of this channel. */
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. */
316 /** Set the enabled status of this channel. */
317 void set_enabled(bool value);
319 Channel(struct sr_channel *structure);
321 const ChannelType * const type;
323 friend class ChannelGroup;
324 friend class Session;
325 friend class TriggerStage;
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>,
334 /** Name of this channel group. */
336 /** List of the channels in this group. */
337 vector<shared_ptr<Channel> > get_channels();
339 ChannelGroup(HardwareDevice *device, struct sr_channel_group *structure);
341 vector<Channel *> channels;
342 friend class HardwareDevice;
345 /** A trigger configuration */
346 class SR_API Trigger : public enable_shared_from_this<Trigger>
350 vector<shared_ptr<TriggerStage> > get_stages();
351 shared_ptr<TriggerStage> add_stage();
353 Trigger(shared_ptr<Context> context, string name);
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. */
362 void operator()(Trigger *trigger) { delete trigger; }
364 friend class Context;
365 friend class Session;
368 /** A stage in a trigger configuration */
369 class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
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);
377 vector<TriggerMatch *> matches;
378 TriggerStage(struct sr_trigger_stage *structure);
380 friend class Trigger;
383 /** A match condition in a trigger configuration */
384 class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
387 shared_ptr<Channel> get_channel();
388 const TriggerMatchType *get_type();
391 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
393 shared_ptr<Channel> channel;
394 friend class TriggerStage;
397 /** Type of datafeed callback */
398 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
399 DatafeedCallbackFunction;
401 /* Data required for C callback function to call a C++ datafeed callback */
402 class SR_PRIV DatafeedCallbackData
405 void run(const struct sr_dev_inst *sdi,
406 const struct sr_datafeed_packet *pkt);
408 DatafeedCallbackFunction callback;
409 DatafeedCallbackData(Session *session,
410 DatafeedCallbackFunction callback);
412 friend class Session;
415 /** Type of source callback */
416 typedef function<bool(Glib::IOCondition)>
417 SourceCallbackFunction;
419 /* Data required for C callback function to call a C++ source callback */
420 class SR_PRIV SourceCallbackData
423 bool run(int revents);
425 SourceCallbackData(shared_ptr<EventSource> source);
426 shared_ptr<EventSource> source;
427 friend class Session;
430 /** An I/O event source */
431 class SR_API EventSource
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);
445 EventSource(int timeout, SourceCallbackFunction callback);
454 Glib::RefPtr<Glib::IOChannel> channel;
455 Glib::IOCondition events;
457 SourceCallbackFunction callback;
458 /** Deleter needed to allow shared_ptr use with protected destructor. */
462 void operator()(EventSource *source) { delete source; }
464 friend class Deleter;
465 friend class Session;
466 friend class SourceCallbackData;
469 /** A sigrok session */
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. */
489 /** Run the session event loop. */
491 /** Stop the session. */
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);
502 Session(shared_ptr<Context> context);
503 Session(shared_ptr<Context> context, string filename);
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;
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. */
519 void operator()(Session *session) { delete session; }
521 friend class Deleter;
522 friend class Context;
523 friend class DatafeedCallbackData;
526 /** A packet on the session datafeed */
530 /** Type of this packet. */
531 const PacketType *get_type();
532 /** Payload of this packet. */
533 PacketPayload *get_payload();
535 Packet(const struct sr_datafeed_packet *structure);
537 const struct sr_datafeed_packet *structure;
538 PacketPayload *payload;
539 /** Deleter needed to allow shared_ptr use with protected destructor. */
543 void operator()(Packet *packet) { delete packet; }
545 friend class Deleter;
546 friend class Session;
548 friend class DatafeedCallbackData;
551 /** Abstract base class for datafeed packet payloads */
552 class SR_API PacketPayload
556 virtual ~PacketPayload() = 0;
557 virtual void *get_data() = 0;
558 virtual size_t get_data_size() = 0;
563 /** Payload of a datafeed packet with logic data */
564 class SR_API Logic : public PacketPayload
567 Logic(const struct sr_datafeed_logic *structure);
569 const struct sr_datafeed_logic *structure;
570 vector<uint8_t> data;
572 size_t get_data_size();
576 /** Payload of a datafeed packet with analog data */
577 class SR_API Analog : public PacketPayload
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();
589 Analog(const struct sr_datafeed_analog *structure);
591 const struct sr_datafeed_analog *structure;
593 size_t get_data_size();
597 /** An input format supported by the library */
598 class SR_API InputFormat :
599 public StructureWrapper<Context, struct sr_input_format>
602 /** Name of this input format. */
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 = {});
612 InputFormat(struct sr_input_format *structure);
614 friend class Context;
615 friend class InputFileDevice;
618 /** A virtual device associated with an input file */
619 class SR_API InputFileDevice : public Device
622 /** Load data from file. */
625 InputFileDevice(shared_ptr<InputFormat> format,
626 struct sr_input *input, string filename);
628 struct sr_input *input;
629 shared_ptr<InputFormat> format;
631 /** Deleter needed to allow shared_ptr use with protected destructor. */
635 void operator()(InputFileDevice *device) { delete device; }
637 friend class Deleter;
638 friend class InputFormat;
641 /** An output format supported by the library */
642 class SR_API OutputFormat :
643 public StructureWrapper<Context, struct sr_output_format>
646 /** Name of this output format. */
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 = {});
653 OutputFormat(struct sr_output_format *structure);
655 friend class Context;
659 /** An output instance (an output format applied to a device) */
663 /** Update output with data from the given packet. */
664 string receive(shared_ptr<Packet> packet);
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);
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. */
678 void operator()(Output *output) { delete output; }
680 friend class Deleter;
681 friend class OutputFormat;
684 /** Base class for objects which wrap an enumeration value from libsigrok */
685 template <typename T> class SR_API EnumValue
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; }
693 EnumValue(T id, const char name[]) : id(id), name(name) {}
703 #endif // LIBSIGROK_HPP