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 libsigrokcxx API provides an object-oriented C++ interface to the
28 functionality 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 libsigrokcxx objects and their underlying
49 libsigrok resources can be treated as fully automatic. As long as all shared
50 pointers to objects are deleted or reassigned when no longer in use, all
51 underlying 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.
70 #ifndef LIBSIGROKCXX_HPP
71 #define LIBSIGROKCXX_HPP
73 #include <libsigrok/libsigrok.h>
75 /* Suppress warnings due to glibmm's use of std::auto_ptr<> in a public
76 * header file. To be removed once glibmm is fixed. */
77 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
79 G_GNUC_END_IGNORE_DEPRECATIONS
92 /* Forward declarations */
97 class SR_API HardwareDevice;
100 class SR_API ConfigKey;
101 class SR_API Capability;
102 class SR_API InputFormat;
103 class SR_API OutputFormat;
104 class SR_API OutputFlag;
105 class SR_API LogLevel;
106 class SR_API ChannelGroup;
107 class SR_API Trigger;
108 class SR_API TriggerStage;
109 class SR_API TriggerMatch;
110 class SR_API TriggerMatchType;
111 class SR_API ChannelType;
113 class SR_API PacketPayload;
114 class SR_API PacketType;
115 class SR_API Quantity;
117 class SR_API QuantityFlag;
118 class SR_API Rational;
120 class SR_API InputDevice;
122 class SR_API DataType;
124 class SR_API UserDevice;
126 /** Exception thrown when an error code is returned by any libsigrok call. */
127 class SR_API Error: public exception
130 explicit Error(int result);
133 const char *what() const noexcept;
136 /* Base template for classes whose resources are owned by a parent object. */
137 template <class Class, class Parent>
138 class SR_API ParentOwned
141 /* Weak pointer for shared_from_this() implementation. */
142 weak_ptr<Class> _weak_this;
144 static void reset_parent(Class *object)
146 if (!object->_parent)
147 throw Error(SR_ERR_BUG);
148 object->_parent.reset();
152 /* Parent object which owns this child object's underlying structure.
154 This shared pointer will be null when this child is unused, but
155 will be assigned to point to the parent before any shared pointer
156 to this child is handed out to the user.
158 When the reference count of this child falls to zero, this shared
159 pointer to its parent is reset by a custom deleter on the child's
162 This strategy ensures that the destructors for both the child and
163 the parent are called at the correct time, i.e. only when all
164 references to both the parent and all its children are gone. */
165 shared_ptr<Parent> _parent;
169 /* Note, this implementation will create a new smart_ptr if none exists. */
170 shared_ptr<Class> shared_from_this()
172 shared_ptr<Class> shared = _weak_this.lock();
176 shared.reset(static_cast<Class *>(this), &reset_parent);
183 shared_ptr<Class> share_owned_by(shared_ptr<Parent> parent)
186 throw Error(SR_ERR_BUG);
187 this->_parent = parent;
188 return shared_from_this();
192 /* Get parent object that owns this object. */
193 shared_ptr<Parent> parent()
199 /* Base template for classes whose resources are owned by the user. */
200 template <class Class>
201 class SR_API UserOwned : public enable_shared_from_this<Class>
206 shared_ptr<Class> shared_from_this()
208 auto shared = enable_shared_from_this<Class>::shared_from_this();
210 throw Error(SR_ERR_BUG);
215 /** Type of log callback */
216 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
218 /** Resource reader delegate. */
219 class SR_API ResourceReader
223 virtual ~ResourceReader();
225 /** Resource open hook. */
226 virtual void open(struct sr_resource *res, string name) = 0;
227 /** Resource close hook. */
228 virtual void close(struct sr_resource *res) = 0;
229 /** Resource read hook. */
230 virtual size_t read(const struct sr_resource *res, void *buf, size_t count) = 0;
232 static SR_PRIV int open_callback(struct sr_resource *res,
233 const char *name, void *cb_data) noexcept;
234 static SR_PRIV int close_callback(struct sr_resource *res,
235 void *cb_data) noexcept;
236 static SR_PRIV gssize read_callback(const struct sr_resource *res,
237 void *buf, size_t count, void *cb_data) noexcept;
238 friend class Context;
241 /** The global libsigrok context */
242 class SR_API Context : public UserOwned<Context>
245 /** Create new context */
246 static shared_ptr<Context> create();
247 /** libsigrok package version. */
248 static string package_version();
249 /** libsigrok library version. */
250 static string lib_version();
251 /** Available hardware drivers, indexed by name. */
252 map<string, shared_ptr<Driver> > drivers();
253 /** Available input formats, indexed by name. */
254 map<string, shared_ptr<InputFormat> > input_formats();
255 /** Lookup the responsible input module for an input file. */
256 shared_ptr<InputFormat> input_format_match(string filename);
257 /** Available output formats, indexed by name. */
258 map<string, shared_ptr<OutputFormat> > output_formats();
259 /** Current log level. */
260 const LogLevel *log_level() const;
261 /** Set the log level.
262 * @param level LogLevel to use. */
263 void set_log_level(const LogLevel *level);
264 /** Set the log callback.
265 * @param callback Callback of the form callback(LogLevel, string). */
266 void set_log_callback(LogCallbackFunction callback);
267 /** Set the log callback to the default handler. */
268 void set_log_callback_default();
269 /** Install a delegate for reading resource files.
270 * @param reader The resource reader delegate, or nullptr to unset. */
271 void set_resource_reader(ResourceReader *reader);
272 /** Create a new session. */
273 shared_ptr<Session> create_session();
274 /** Create a new user device. */
275 shared_ptr<UserDevice> create_user_device(
276 string vendor, string model, string version);
277 /** Create a header packet. */
278 shared_ptr<Packet> create_header_packet(Glib::TimeVal start_time);
279 /** Create a meta packet. */
280 shared_ptr<Packet> create_meta_packet(
281 map<const ConfigKey *, Glib::VariantBase> config);
282 /** Create a logic packet. */
283 shared_ptr<Packet> create_logic_packet(
284 void *data_pointer, size_t data_length, unsigned int unit_size);
285 /** Create an analog packet. */
286 shared_ptr<Packet> create_analog_packet(
287 vector<shared_ptr<Channel> > channels,
288 const float *data_pointer, unsigned int num_samples, const Quantity *mq,
289 const Unit *unit, vector<const QuantityFlag *> mqflags);
290 /** Create an end packet. */
291 shared_ptr<Packet> create_end_packet();
292 /** Load a saved session.
293 * @param filename File name string. */
294 shared_ptr<Session> load_session(string filename);
295 /** Create a new trigger.
296 * @param name Name string for new trigger. */
297 shared_ptr<Trigger> create_trigger(string name);
298 /** Open an input file.
299 * @param filename File name string. */
300 shared_ptr<Input> open_file(string filename);
301 /** Open an input stream based on header data.
302 * @param header Initial data from stream. */
303 shared_ptr<Input> open_stream(string header);
304 map<string, string> serials(shared_ptr<Driver> driver) const;
306 struct sr_context *_structure;
307 map<string, unique_ptr<Driver> > _drivers;
308 map<string, unique_ptr<InputFormat> > _input_formats;
309 map<string, unique_ptr<OutputFormat> > _output_formats;
311 LogCallbackFunction _log_callback;
314 friend class Session;
316 friend struct std::default_delete<Context>;
319 /** An object that can be configured. */
320 class SR_API Configurable
323 /** Supported configuration keys. */
324 set<const ConfigKey *> config_keys() const;
325 /** Read configuration for the given key.
326 * @param key ConfigKey to read. */
327 Glib::VariantBase config_get(const ConfigKey *key) const;
328 /** Set configuration for the given key to a specified value.
329 * @param key ConfigKey to set.
330 * @param value Value to set. */
331 void config_set(const ConfigKey *key, const Glib::VariantBase &value);
332 /** Enumerate available values for the given configuration key.
333 * @param key ConfigKey to enumerate values for. */
334 Glib::VariantContainerBase config_list(const ConfigKey *key) const;
335 /** Enumerate configuration capabilities for the given configuration key.
336 * @param key ConfigKey to enumerate capabilities for. */
337 set<const Capability *> config_capabilities(const ConfigKey *key) const;
338 /** Check whether a configuration capability is supported for a given key.
339 * @param key ConfigKey to check.
340 * @param capability Capability to check for. */
341 bool config_check(const ConfigKey *key, const Capability *capability) const;
344 struct sr_dev_driver *driver,
345 struct sr_dev_inst *sdi,
346 struct sr_channel_group *channel_group);
347 virtual ~Configurable();
348 struct sr_dev_driver *config_driver;
349 struct sr_dev_inst *config_sdi;
350 struct sr_channel_group *config_channel_group;
353 /** A hardware driver provided by the library */
354 class SR_API Driver : public ParentOwned<Driver, Context>, public Configurable
357 /** Name of this driver. */
359 /** Long name for this driver. */
360 string long_name() const;
361 /** Scan options supported by this driver. */
362 set<const ConfigKey *> scan_options() const;
363 /** Scan for devices and return a list of devices found.
364 * @param options Mapping of (ConfigKey, value) pairs. */
365 vector<shared_ptr<HardwareDevice> > scan(map<const ConfigKey *, Glib::VariantBase>
366 options = map<const ConfigKey *, Glib::VariantBase>());
368 struct sr_dev_driver *_structure;
370 vector<HardwareDevice *> _devices;
371 explicit Driver(struct sr_dev_driver *structure);
373 friend class Context;
374 friend class HardwareDevice;
375 friend class ChannelGroup;
376 friend struct std::default_delete<Driver>;
379 /** A generic device, either hardware or virtual */
380 class SR_API Device : public Configurable
383 /** Vendor name for this device. */
384 string vendor() const;
385 /** Model name for this device. */
386 string model() const;
387 /** Version string for this device. */
388 string version() const;
389 /** Serial number for this device. */
390 string serial_number() const;
391 /** Connection ID for this device. */
392 string connection_id() const;
393 /** List of the channels available on this device. */
394 vector<shared_ptr<Channel> > channels();
395 /** Channel groups available on this device, indexed by name. */
396 map<string, shared_ptr<ChannelGroup> > channel_groups();
402 explicit Device(struct sr_dev_inst *structure);
404 virtual shared_ptr<Device> get_shared_from_this() = 0;
405 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
407 struct sr_dev_inst *_structure;
408 map<struct sr_channel *, unique_ptr<Channel> > _channels;
410 map<string, unique_ptr<ChannelGroup> > _channel_groups;
412 friend class Session;
413 friend class Channel;
414 friend class ChannelGroup;
417 friend struct std::default_delete<Device>;
420 /** A real hardware device, connected via a driver */
421 class SR_API HardwareDevice :
422 public UserOwned<HardwareDevice>,
426 /** Driver providing this device. */
427 shared_ptr<Driver> driver();
429 HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
431 shared_ptr<Device> get_shared_from_this();
432 shared_ptr<Driver> _driver;
435 friend class ChannelGroup;
436 friend struct std::default_delete<HardwareDevice>;
439 /** A virtual device, created by the user */
440 class SR_API UserDevice :
441 public UserOwned<UserDevice>,
445 /** Add a new channel to this device. */
446 shared_ptr<Channel> add_channel(unsigned int index, const ChannelType *type, string name);
448 UserDevice(string vendor, string model, string version);
450 shared_ptr<Device> get_shared_from_this();
452 friend class Context;
453 friend struct std::default_delete<UserDevice>;
456 /** A channel on a device */
457 class SR_API Channel :
458 public ParentOwned<Channel, Device>
461 /** Current name of this channel. */
463 /** Set the name of this channel. *
464 * @param name Name string to set. */
465 void set_name(string name);
466 /** Type of this channel. */
467 const ChannelType *type() const;
468 /** Enabled status of this channel. */
469 bool enabled() const;
470 /** Set the enabled status of this channel.
471 * @param value Boolean value to set. */
472 void set_enabled(bool value);
473 /** Get the index number of this channel. */
474 unsigned int index() const;
476 explicit Channel(struct sr_channel *structure);
478 struct sr_channel *_structure;
479 const ChannelType * const _type;
481 friend class UserDevice;
482 friend class ChannelGroup;
483 friend class Session;
484 friend class TriggerStage;
485 friend class Context;
486 friend struct std::default_delete<Channel>;
489 /** A group of channels on a device, which share some configuration */
490 class SR_API ChannelGroup :
491 public ParentOwned<ChannelGroup, Device>,
495 /** Name of this channel group. */
497 /** List of the channels in this group. */
498 vector<shared_ptr<Channel> > channels();
500 ChannelGroup(const Device *device, struct sr_channel_group *structure);
502 vector<Channel *> _channels;
504 friend struct std::default_delete<ChannelGroup>;
507 /** A trigger configuration */
508 class SR_API Trigger : public UserOwned<Trigger>
511 /** Name of this trigger configuration. */
513 /** List of the stages in this trigger. */
514 vector<shared_ptr<TriggerStage> > stages();
515 /** Add a new stage to this trigger. */
516 shared_ptr<TriggerStage> add_stage();
518 Trigger(shared_ptr<Context> context, string name);
520 struct sr_trigger *_structure;
521 shared_ptr<Context> _context;
522 vector<unique_ptr<TriggerStage> > _stages;
523 friend class Context;
524 friend class Session;
525 friend struct std::default_delete<Trigger>;
528 /** A stage in a trigger configuration */
529 class SR_API TriggerStage :
530 public ParentOwned<TriggerStage, Trigger>
533 /** Index number of this stage. */
535 /** List of match conditions on this stage. */
536 vector<shared_ptr<TriggerMatch> > matches();
537 /** Add a new match condition to this stage.
538 * @param channel Channel to match on.
539 * @param type TriggerMatchType to apply. */
540 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
541 /** Add a new match condition to this stage.
542 * @param channel Channel to match on.
543 * @param type TriggerMatchType to apply.
544 * @param value Threshold value. */
545 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
547 struct sr_trigger_stage *_structure;
548 vector<unique_ptr<TriggerMatch> > _matches;
549 explicit TriggerStage(struct sr_trigger_stage *structure);
551 friend class Trigger;
552 friend struct std::default_delete<TriggerStage>;
555 /** A match condition in a trigger configuration */
556 class SR_API TriggerMatch :
557 public ParentOwned<TriggerMatch, TriggerStage>
560 /** Channel this condition matches on. */
561 shared_ptr<Channel> channel();
562 /** Type of match. */
563 const TriggerMatchType *type() const;
564 /** Threshold value. */
567 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
569 struct sr_trigger_match *_structure;
570 shared_ptr<Channel> _channel;
571 friend class TriggerStage;
572 friend struct std::default_delete<TriggerMatch>;
575 /** Type of session stopped callback */
576 typedef function<void()> SessionStoppedCallback;
578 /** Type of datafeed callback */
579 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
580 DatafeedCallbackFunction;
582 /* Data required for C callback function to call a C++ datafeed callback */
583 class SR_PRIV DatafeedCallbackData
586 void run(const struct sr_dev_inst *sdi,
587 const struct sr_datafeed_packet *pkt);
589 DatafeedCallbackFunction _callback;
590 DatafeedCallbackData(Session *session,
591 DatafeedCallbackFunction callback);
593 friend class Session;
596 /** A virtual device associated with a stored session */
597 class SR_API SessionDevice :
598 public ParentOwned<SessionDevice, Session>,
602 explicit SessionDevice(struct sr_dev_inst *sdi);
604 shared_ptr<Device> get_shared_from_this();
606 friend class Session;
607 friend struct std::default_delete<SessionDevice>;
610 /** A sigrok session */
611 class SR_API Session : public UserOwned<Session>
614 /** Add a device to this session.
615 * @param device Device to add. */
616 void add_device(shared_ptr<Device> device);
617 /** List devices attached to this session. */
618 vector<shared_ptr<Device> > devices();
619 /** Remove all devices from this session. */
620 void remove_devices();
621 /** Add a datafeed callback to this session.
622 * @param callback Callback of the form callback(Device, Packet). */
623 void add_datafeed_callback(DatafeedCallbackFunction callback);
624 /** Remove all datafeed callbacks from this session. */
625 void remove_datafeed_callbacks();
626 /** Start the session. */
628 /** Run the session event loop. */
630 /** Stop the session. */
632 /** Return whether the session is running. */
633 bool is_running() const;
634 /** Set callback to be invoked on session stop. */
635 void set_stopped_callback(SessionStoppedCallback callback);
636 /** Get current trigger setting. */
637 shared_ptr<Trigger> trigger();
638 /** Get the context. */
639 shared_ptr<Context> context();
640 /** Set trigger setting.
641 * @param trigger Trigger object to use. */
642 void set_trigger(shared_ptr<Trigger> trigger);
643 /** Get filename this session was loaded from. */
644 string filename() const;
646 explicit Session(shared_ptr<Context> context);
647 Session(shared_ptr<Context> context, string filename);
649 shared_ptr<Device> get_device(const struct sr_dev_inst *sdi);
650 struct sr_session *_structure;
651 const shared_ptr<Context> _context;
652 map<const struct sr_dev_inst *, unique_ptr<SessionDevice> > _owned_devices;
653 map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
654 vector<unique_ptr<DatafeedCallbackData> > _datafeed_callbacks;
655 SessionStoppedCallback _stopped_callback;
657 shared_ptr<Trigger> _trigger;
659 friend class Context;
660 friend class DatafeedCallbackData;
661 friend class SessionDevice;
662 friend struct std::default_delete<Session>;
665 /** A packet on the session datafeed */
666 class SR_API Packet : public UserOwned<Packet>
669 /** Type of this packet. */
670 const PacketType *type() const;
671 /** Payload of this packet. */
672 shared_ptr<PacketPayload> payload();
674 Packet(shared_ptr<Device> device,
675 const struct sr_datafeed_packet *structure);
677 const struct sr_datafeed_packet *_structure;
678 shared_ptr<Device> _device;
679 unique_ptr<PacketPayload> _payload;
681 friend class Session;
683 friend class DatafeedCallbackData;
688 friend class Context;
689 friend struct std::default_delete<Packet>;
692 /** Abstract base class for datafeed packet payloads */
693 class SR_API PacketPayload
697 virtual ~PacketPayload() = 0;
699 virtual shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent) = 0;
703 friend struct std::default_delete<PacketPayload>;
706 /** Payload of a datafeed header packet */
707 class SR_API Header :
708 public ParentOwned<Header, Packet>,
712 /* Feed version number. */
713 int feed_version() const;
714 /* Start time of this session. */
715 Glib::TimeVal start_time() const;
717 explicit Header(const struct sr_datafeed_header *structure);
719 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
721 const struct sr_datafeed_header *_structure;
726 /** Payload of a datafeed metadata packet */
728 public ParentOwned<Meta, Packet>,
732 /* Mapping of (ConfigKey, value) pairs. */
733 map<const ConfigKey *, Glib::VariantBase> config() const;
735 explicit Meta(const struct sr_datafeed_meta *structure);
737 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
739 const struct sr_datafeed_meta *_structure;
740 map<const ConfigKey *, Glib::VariantBase> _config;
745 /** Payload of a datafeed packet with logic data */
747 public ParentOwned<Logic, Packet>,
751 /* Pointer to data. */
752 void *data_pointer();
753 /* Data length in bytes. */
754 size_t data_length() const;
755 /* Size of each sample in bytes. */
756 unsigned int unit_size() const;
758 explicit Logic(const struct sr_datafeed_logic *structure);
760 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
762 const struct sr_datafeed_logic *_structure;
766 friend struct std::default_delete<Logic>;
769 /** Payload of a datafeed packet with analog data */
770 class SR_API Analog :
771 public ParentOwned<Analog, Packet>,
775 /** Pointer to data. */
776 void *data_pointer();
778 * Fills dest pointer with the analog data converted to float.
779 * The pointer must have space for num_samples() floats.
781 void get_data_as_float(float *dest);
782 /** Number of samples in this packet. */
783 unsigned int num_samples() const;
784 /** Channels for which this packet contains data. */
785 vector<shared_ptr<Channel> > channels();
786 /** Size of a single sample in bytes. */
787 unsigned int unitsize() const;
788 /** Samples use a signed data type. */
789 bool is_signed() const;
790 /** Samples use float. */
791 bool is_float() const;
792 /** Samples are stored in big-endian order. */
793 bool is_bigendian() const;
795 * Number of significant digits after the decimal point if positive,
796 * or number of non-significant digits before the decimal point if negative
797 * (refers to the value we actually read on the wire).
801 bool is_digits_decimal() const;
803 shared_ptr<Rational> scale();
805 shared_ptr<Rational> offset();
806 /** Measured quantity of the samples in this packet. */
807 const Quantity *mq() const;
808 /** Unit of the samples in this packet. */
809 const Unit *unit() const;
810 /** Measurement flags associated with the samples in this packet. */
811 vector<const QuantityFlag *> mq_flags() const;
813 * Provides a Logic packet that contains a conversion of the analog
814 * data using a simple threshold.
816 * @param threshold Threshold to use.
817 * @param data_ptr Pointer to num_samples() bytes where the logic
818 * samples are stored. When nullptr, memory for
819 * logic->data_pointer() will be allocated and must
820 * be freed by the caller.
822 shared_ptr<Logic> get_logic_via_threshold(float threshold,
823 uint8_t *data_ptr=nullptr) const;
825 * Provides a Logic packet that contains a conversion of the analog
826 * data using a Schmitt-Trigger.
828 * @param lo_thr Low threshold to use (anything below this is low).
829 * @param hi_thr High threshold to use (anything above this is high).
830 * @param state Points to a byte that contains the current state of the
831 * converter. For best results, set to value of logic
833 * @param data_ptr Pointer to num_samples() bytes where the logic
834 * samples are stored. When nullptr, memory for
835 * logic->data_pointer() will be allocated and must be
836 * freed by the caller.
838 shared_ptr<Logic> get_logic_via_schmitt_trigger(float lo_thr,
839 float hi_thr, uint8_t *state, uint8_t *data_ptr=nullptr) const;
841 explicit Analog(const struct sr_datafeed_analog *structure);
843 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
845 const struct sr_datafeed_analog *_structure;
850 /** Number represented by a numerator/denominator integer pair */
851 class SR_API Rational :
852 public ParentOwned<Rational, Analog>
855 /** Numerator, i.e. the dividend. */
856 int64_t numerator() const;
857 /** Denominator, i.e. the divider. */
858 uint64_t denominator() const;
859 /** Actual (lossy) value. */
862 explicit Rational(const struct sr_rational *structure);
864 shared_ptr<Rational> share_owned_by(shared_ptr<Analog> parent);
866 const struct sr_rational *_structure;
869 friend struct std::default_delete<Rational>;
872 /** An input format supported by the library */
873 class SR_API InputFormat :
874 public ParentOwned<InputFormat, Context>
877 /** Name of this input format. */
879 /** Description of this input format. */
880 string description() const;
881 /** A list of preferred file name extensions for this file format.
882 * @note This list is a recommendation only. */
883 vector<string> extensions() const;
884 /** Options supported by this input format. */
885 map<string, shared_ptr<Option> > options();
886 /** Create an input using this input format.
887 * @param options Mapping of (option name, value) pairs. */
888 shared_ptr<Input> create_input(map<string, Glib::VariantBase>
889 options = map<string, Glib::VariantBase>());
891 explicit InputFormat(const struct sr_input_module *structure);
894 const struct sr_input_module *_structure;
896 friend class Context;
897 friend class InputDevice;
898 friend struct std::default_delete<InputFormat>;
901 /** An input instance (an input format applied to a file or stream) */
902 class SR_API Input : public UserOwned<Input>
905 /** Virtual device associated with this input. */
906 shared_ptr<InputDevice> device();
907 /** Send next stream data.
908 * @param data Next stream data.
909 * @param length Length of data. */
910 void send(void *data, size_t length);
911 /** Signal end of input data. */
915 Input(shared_ptr<Context> context, const struct sr_input *structure);
917 const struct sr_input *_structure;
918 shared_ptr<Context> _context;
919 unique_ptr<InputDevice> _device;
921 friend class Context;
922 friend class InputFormat;
923 friend struct std::default_delete<Input>;
926 /** A virtual device associated with an input */
927 class SR_API InputDevice :
928 public ParentOwned<InputDevice, Input>,
932 InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
934 shared_ptr<Device> get_shared_from_this();
935 shared_ptr<Input> _input;
937 friend struct std::default_delete<InputDevice>;
940 /** An option used by an output format */
941 class SR_API Option : public UserOwned<Option>
944 /** Short name of this option suitable for command line usage. */
946 /** Short name of this option suitable for GUI usage. */
948 /** Description of this option in a sentence. */
949 string description() const;
950 /** Default value for this option. */
951 Glib::VariantBase default_value() const;
952 /** Possible values for this option, if a limited set. */
953 vector<Glib::VariantBase> values() const;
954 /** Parse a string argument into the appropriate type for this option. */
955 Glib::VariantBase parse_string(string value);
957 Option(const struct sr_option *structure,
958 shared_ptr<const struct sr_option *> structure_array);
960 const struct sr_option *_structure;
961 shared_ptr<const struct sr_option *> _structure_array;
963 friend class InputFormat;
964 friend class OutputFormat;
965 friend struct std::default_delete<Option>;
968 /** An output format supported by the library */
969 class SR_API OutputFormat :
970 public ParentOwned<OutputFormat, Context>
973 /** Name of this output format. */
975 /** Description of this output format. */
976 string description() const;
977 /** A list of preferred file name extensions for this file format.
978 * @note This list is a recommendation only. */
979 vector<string> extensions() const;
980 /** Options supported by this output format. */
981 map<string, shared_ptr<Option> > options();
982 /** Create an output using this format.
983 * @param device Device to output for.
984 * @param options Mapping of (option name, value) pairs. */
985 shared_ptr<Output> create_output(shared_ptr<Device> device,
986 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
987 /** Create an output using this format.
988 * @param filename Name of destination file.
989 * @param device Device to output for.
990 * @param options Mapping of (option name, value) pairs. */
991 shared_ptr<Output> create_output(string filename,
992 shared_ptr<Device> device,
993 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
995 * Checks whether a given flag is set.
996 * @param flag Flag to check
997 * @return true if flag is set for this module
998 * @see sr_output_flags
1000 bool test_flag(const OutputFlag *flag) const;
1002 explicit OutputFormat(const struct sr_output_module *structure);
1005 const struct sr_output_module *_structure;
1007 friend class Context;
1008 friend class Output;
1009 friend struct std::default_delete<OutputFormat>;
1012 /** An output instance (an output format applied to a device) */
1013 class SR_API Output : public UserOwned<Output>
1016 /** Update output with data from the given packet.
1017 * @param packet Packet to handle. */
1018 string receive(shared_ptr<Packet> packet);
1019 /** Output format in use for this output */
1020 shared_ptr<OutputFormat> format();
1022 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
1023 Output(shared_ptr<OutputFormat> format,
1024 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
1025 Output(string filename, shared_ptr<OutputFormat> format,
1026 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
1029 const struct sr_output *_structure;
1030 const shared_ptr<OutputFormat> _format;
1031 const shared_ptr<Device> _device;
1032 const map<string, Glib::VariantBase> _options;
1034 friend class OutputFormat;
1035 friend struct std::default_delete<Output>;
1038 /** Base class for objects which wrap an enumeration value from libsigrok */
1039 template <class Class, typename Enum> class SR_API EnumValue
1042 /** The integer constant associated with this value. */
1045 return static_cast<int>(_id);
1047 /** The name associated with this value. */
1052 /** Get value associated with a given integer constant. */
1053 static const Class *get(int id)
1055 const auto pos = _values.find(static_cast<Enum>(id));
1056 if (pos == _values.end())
1057 throw Error(SR_ERR_ARG);
1060 /** Get possible values. */
1061 static std::vector<const Class *> values()
1063 std::vector<const Class *> result;
1064 for (auto entry : _values)
1065 result.push_back(entry.second);
1069 EnumValue(Enum id, const char name[]) : _id(id), _name(name)
1076 static const std::map<const Enum, const Class * const> _values;
1083 #include <libsigrokcxx/enums.hpp>