70 #ifndef LIBSIGROKCXX_HPP 71 #define LIBSIGROKCXX_HPP 73 #include <libsigrok/libsigrok.h> 77 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
79 G_GNUC_END_IGNORE_DEPRECATIONS
96 class SR_API HardwareDevice;
99 class SR_API ConfigKey;
100 class SR_API Capability;
101 class SR_API InputFormat;
102 class SR_API OutputFormat;
103 class SR_API OutputFlag;
104 class SR_API LogLevel;
105 class SR_API ChannelGroup;
106 class SR_API Trigger;
107 class SR_API TriggerStage;
108 class SR_API TriggerMatch;
109 class SR_API TriggerMatchType;
110 class SR_API ChannelType;
112 class SR_API PacketPayload;
113 class SR_API PacketType;
114 class SR_API Quantity;
116 class SR_API QuantityFlag;
117 class SR_API Rational;
119 class SR_API InputDevice;
121 class SR_API DataType;
123 class SR_API UserDevice;
126 class SR_API
Error:
public std::exception
129 explicit Error(
int result);
132 const char *what()
const noexcept;
136 template <
class Class,
class Parent>
137 class SR_API ParentOwned
141 std::weak_ptr<Class> _weak_this;
143 static void reset_parent(Class *
object)
145 if (!object->_parent)
146 throw Error(SR_ERR_BUG);
147 object->_parent.reset();
164 std::shared_ptr<Parent> _parent;
169 std::shared_ptr<Class> shared_from_this()
171 std::shared_ptr<Class> shared = _weak_this.lock();
175 shared.reset(static_cast<Class *>(
this), &reset_parent);
182 std::shared_ptr<Class> share_owned_by(std::shared_ptr<Parent> parent)
185 throw Error(SR_ERR_BUG);
186 this->_parent = parent;
187 return shared_from_this();
192 std::shared_ptr<Parent> parent()
199 template <
class Class>
200 class SR_API UserOwned :
public std::enable_shared_from_this<Class>
205 std::shared_ptr<Class> shared_from_this()
207 auto shared = std::enable_shared_from_this<Class>::shared_from_this();
209 throw Error(SR_ERR_BUG);
215 typedef std::function<void(const LogLevel *, std::string message)> LogCallbackFunction;
225 virtual void open(
struct sr_resource *res, std::string name) = 0;
227 virtual void close(
struct sr_resource *res) = 0;
229 virtual size_t read(
const struct sr_resource *res,
void *buf,
size_t count) = 0;
231 static SR_PRIV
int open_callback(
struct sr_resource *res,
232 const char *name,
void *cb_data) noexcept;
233 static SR_PRIV
int close_callback(
struct sr_resource *res,
234 void *cb_data) noexcept;
235 static SR_PRIV gssize read_callback(
const struct sr_resource *res,
236 void *buf,
size_t count,
void *cb_data) noexcept;
241 class SR_API
Context :
public UserOwned<Context>
245 static std::shared_ptr<Context> create();
247 static std::string package_version();
249 static std::string lib_version();
251 std::map<std::string, std::shared_ptr<Driver> > drivers();
253 std::map<std::string, std::shared_ptr<InputFormat> > input_formats();
255 std::shared_ptr<InputFormat> input_format_match(std::string filename);
257 std::map<std::string, std::shared_ptr<OutputFormat> > output_formats();
262 void set_log_level(
const LogLevel *level);
265 void set_log_callback(LogCallbackFunction callback);
267 void set_log_callback_default();
272 std::shared_ptr<Session> create_session();
274 std::shared_ptr<UserDevice> create_user_device(
275 std::string vendor, std::string model, std::string version);
277 std::shared_ptr<Packet> create_header_packet(Glib::DateTime start_time);
279 std::shared_ptr<Packet> create_meta_packet(
280 std::map<const ConfigKey *, Glib::VariantBase> config);
282 std::shared_ptr<Packet> create_logic_packet(
283 void *data_pointer,
size_t data_length,
unsigned int unit_size);
285 std::shared_ptr<Packet> create_analog_packet(
286 std::vector<std::shared_ptr<Channel> > channels,
287 const float *data_pointer,
unsigned int num_samples,
const Quantity *mq,
288 const Unit *unit, std::vector<const QuantityFlag *> mqflags);
290 std::shared_ptr<Packet> create_end_packet();
293 std::shared_ptr<Session> load_session(std::string filename);
296 std::shared_ptr<Trigger> create_trigger(std::string name);
299 std::shared_ptr<Input> open_file(std::string filename);
302 std::shared_ptr<Input> open_stream(std::string header);
303 std::map<std::string, std::string> serials(std::shared_ptr<Driver> driver)
const;
305 struct sr_context *_structure;
306 std::map<std::string, std::unique_ptr<Driver> > _drivers;
307 std::map<std::string, std::unique_ptr<InputFormat> > _input_formats;
308 std::map<std::string, std::unique_ptr<OutputFormat> > _output_formats;
310 LogCallbackFunction _log_callback;
315 friend struct std::default_delete<Context>;
323 std::set<const ConfigKey *> config_keys()
const;
326 Glib::VariantBase config_get(
const ConfigKey *key)
const;
330 void config_set(
const ConfigKey *key,
const Glib::VariantBase &value);
333 Glib::VariantContainerBase config_list(
const ConfigKey *key)
const;
336 std::set<const Capability *> config_capabilities(
const ConfigKey *key)
const;
343 struct sr_dev_driver *driver,
344 struct sr_dev_inst *sdi,
345 struct sr_channel_group *channel_group);
347 struct sr_dev_driver *config_driver;
348 struct sr_dev_inst *config_sdi;
349 struct sr_channel_group *config_channel_group;
357 std::string name()
const;
359 std::string long_name()
const;
361 std::set<const ConfigKey *> scan_options()
const;
364 std::vector<std::shared_ptr<HardwareDevice> > scan(std::map<const ConfigKey *, Glib::VariantBase>
365 options = std::map<const ConfigKey *, Glib::VariantBase>());
367 struct sr_dev_driver *_structure;
369 std::vector<HardwareDevice *> _devices;
370 explicit Driver(
struct sr_dev_driver *structure);
372 friend class Context;
375 friend struct std::default_delete<Driver>;
383 std::string vendor()
const;
385 std::string model()
const;
387 std::string version()
const;
389 std::string serial_number()
const;
391 std::string connection_id()
const;
393 std::vector<std::shared_ptr<Channel> > channels();
395 std::map<std::string, std::shared_ptr<ChannelGroup> > channel_groups();
401 explicit Device(
struct sr_dev_inst *structure);
403 virtual std::shared_ptr<Device> get_shared_from_this() = 0;
404 std::shared_ptr<Channel> get_channel(
struct sr_channel *ptr);
406 struct sr_dev_inst *_structure;
407 std::map<struct sr_channel *, std::unique_ptr<Channel> > _channels;
409 std::map<std::string, std::unique_ptr<ChannelGroup> > _channel_groups;
416 friend struct std::default_delete<Device>;
421 public UserOwned<HardwareDevice>,
426 std::shared_ptr<Driver> driver();
428 HardwareDevice(std::shared_ptr<Driver> driver,
struct sr_dev_inst *structure);
430 std::shared_ptr<Device> get_shared_from_this();
431 std::shared_ptr<Driver> _driver;
435 friend struct std::default_delete<HardwareDevice>;
440 public UserOwned<UserDevice>,
445 std::shared_ptr<Channel> add_channel(
unsigned int index,
const ChannelType *type, std::string name);
447 UserDevice(std::string vendor, std::string model, std::string version);
449 std::shared_ptr<Device> get_shared_from_this();
451 friend class Context;
452 friend struct std::default_delete<UserDevice>;
457 public ParentOwned<Channel, Device>
461 std::string name()
const;
464 void set_name(std::string name);
468 bool enabled()
const;
471 void set_enabled(
bool value);
473 unsigned int index()
const;
475 explicit Channel(
struct sr_channel *structure);
477 struct sr_channel *_structure;
480 friend class UserDevice;
484 friend class Context;
485 friend struct std::default_delete<Channel>;
490 public ParentOwned<ChannelGroup, Device>,
495 std::string name()
const;
497 std::vector<std::shared_ptr<Channel> > channels();
499 ChannelGroup(
const Device *device,
struct sr_channel_group *structure);
501 std::vector<Channel *> _channels;
503 friend struct std::default_delete<ChannelGroup>;
507 class SR_API
Trigger :
public UserOwned<Trigger>
511 std::string name()
const;
513 std::vector<std::shared_ptr<TriggerStage> > stages();
515 std::shared_ptr<TriggerStage> add_stage();
517 Trigger(std::shared_ptr<Context> context, std::string name);
519 struct sr_trigger *_structure;
520 std::shared_ptr<Context> _context;
521 std::vector<std::unique_ptr<TriggerStage> > _stages;
522 friend class Context;
524 friend struct std::default_delete<Trigger>;
529 public ParentOwned<TriggerStage, Trigger>
535 std::vector<std::shared_ptr<TriggerMatch> > matches();
539 void add_match(std::shared_ptr<Channel> channel,
const TriggerMatchType *type);
544 void add_match(std::shared_ptr<Channel> channel,
const TriggerMatchType *type,
float value);
546 struct sr_trigger_stage *_structure;
547 std::vector<std::unique_ptr<TriggerMatch> > _matches;
548 explicit TriggerStage(
struct sr_trigger_stage *structure);
550 friend class Trigger;
551 friend struct std::default_delete<TriggerStage>;
556 public ParentOwned<TriggerMatch, TriggerStage>
560 std::shared_ptr<Channel> channel();
566 TriggerMatch(
struct sr_trigger_match *structure, std::shared_ptr<Channel> channel);
568 struct sr_trigger_match *_structure;
569 std::shared_ptr<Channel> _channel;
570 friend class TriggerStage;
571 friend struct std::default_delete<TriggerMatch>;
575 typedef std::function<void()> SessionStoppedCallback;
578 typedef std::function<void(std::shared_ptr<Device>, std::shared_ptr<Packet>)>
579 DatafeedCallbackFunction;
582 class SR_PRIV DatafeedCallbackData
585 void run(
const struct sr_dev_inst *sdi,
586 const struct sr_datafeed_packet *pkt);
588 DatafeedCallbackFunction _callback;
589 DatafeedCallbackData(
Session *session,
590 DatafeedCallbackFunction callback);
597 public ParentOwned<SessionDevice, Session>,
603 std::shared_ptr<Device> get_shared_from_this();
606 friend struct std::default_delete<SessionDevice>;
610 class SR_API
Session :
public UserOwned<Session>
615 void add_device(std::shared_ptr<Device> device);
617 std::vector<std::shared_ptr<Device> > devices();
619 void remove_devices();
622 void add_datafeed_callback(DatafeedCallbackFunction callback);
624 void remove_datafeed_callbacks();
632 bool is_running()
const;
634 void set_stopped_callback(SessionStoppedCallback callback);
636 std::shared_ptr<Trigger> trigger();
638 std::shared_ptr<Context> context();
641 void set_trigger(std::shared_ptr<Trigger> trigger);
643 std::string filename()
const;
645 explicit Session(std::shared_ptr<Context> context);
646 Session(std::shared_ptr<Context> context, std::string filename);
648 std::shared_ptr<Device> get_device(
const struct sr_dev_inst *sdi);
649 struct sr_session *_structure;
650 const std::shared_ptr<Context> _context;
651 std::map<const struct sr_dev_inst *, std::unique_ptr<SessionDevice> > _owned_devices;
652 std::map<const struct sr_dev_inst *, std::shared_ptr<Device> > _other_devices;
653 std::vector<std::unique_ptr<DatafeedCallbackData> > _datafeed_callbacks;
654 SessionStoppedCallback _stopped_callback;
655 std::string _filename;
656 std::shared_ptr<Trigger> _trigger;
658 friend class Context;
659 friend class DatafeedCallbackData;
660 friend class SessionDevice;
661 friend struct std::default_delete<Session>;
665 class SR_API
Packet :
public UserOwned<Packet>
671 std::shared_ptr<PacketPayload> payload();
673 Packet(std::shared_ptr<Device> device,
674 const struct sr_datafeed_packet *structure);
676 const struct sr_datafeed_packet *_structure;
677 std::shared_ptr<Device> _device;
678 std::unique_ptr<PacketPayload> _payload;
680 friend class Session;
682 friend class DatafeedCallbackData;
687 friend class Context;
688 friend struct std::default_delete<Packet>;
698 virtual std::shared_ptr<PacketPayload> share_owned_by(std::shared_ptr<Packet> parent) = 0;
702 friend struct std::default_delete<PacketPayload>;
707 public ParentOwned<Header, Packet>,
712 int feed_version()
const;
714 Glib::DateTime start_time()
const;
716 explicit Header(
const struct sr_datafeed_header *structure);
718 std::shared_ptr<PacketPayload> share_owned_by(std::shared_ptr<Packet> parent);
720 const struct sr_datafeed_header *_structure;
727 public ParentOwned<Meta, Packet>,
732 std::map<const ConfigKey *, Glib::VariantBase> config()
const;
734 explicit Meta(
const struct sr_datafeed_meta *structure);
736 std::shared_ptr<PacketPayload> share_owned_by(std::shared_ptr<Packet> parent);
738 const struct sr_datafeed_meta *_structure;
739 std::map<const ConfigKey *, Glib::VariantBase> _config;
746 public ParentOwned<Logic, Packet>,
751 void *data_pointer();
753 size_t data_length()
const;
755 unsigned int unit_size()
const;
757 explicit Logic(
const struct sr_datafeed_logic *structure);
759 std::shared_ptr<PacketPayload> share_owned_by(std::shared_ptr<Packet> parent);
761 const struct sr_datafeed_logic *_structure;
765 friend struct std::default_delete<Logic>;
770 public ParentOwned<Analog, Packet>,
775 void *data_pointer();
780 void get_data_as_float(
float *dest);
782 unsigned int num_samples()
const;
784 std::vector<std::shared_ptr<Channel> > channels();
786 unsigned int unitsize()
const;
788 bool is_signed()
const;
790 bool is_float()
const;
792 bool is_bigendian()
const;
813 bool is_digits_decimal()
const;
815 std::shared_ptr<Rational> scale();
817 std::shared_ptr<Rational> offset();
821 const Unit *unit()
const;
823 std::vector<const QuantityFlag *> mq_flags()
const;
834 std::shared_ptr<Logic> get_logic_via_threshold(
float threshold,
835 uint8_t *data_ptr=
nullptr)
const;
850 std::shared_ptr<Logic> get_logic_via_schmitt_trigger(
float lo_thr,
851 float hi_thr, uint8_t *state, uint8_t *data_ptr=
nullptr)
const;
853 explicit Analog(
const struct sr_datafeed_analog *structure);
855 std::shared_ptr<PacketPayload> share_owned_by(std::shared_ptr<Packet> parent);
857 const struct sr_datafeed_analog *_structure;
864 public ParentOwned<Rational, Analog>
868 int64_t numerator()
const;
870 uint64_t denominator()
const;
874 explicit Rational(
const struct sr_rational *structure);
876 std::shared_ptr<Rational> share_owned_by(std::shared_ptr<Analog> parent);
878 const struct sr_rational *_structure;
881 friend struct std::default_delete<Rational>;
886 public ParentOwned<InputFormat, Context>
890 std::string name()
const;
892 std::string description()
const;
895 std::vector<std::string> extensions()
const;
897 std::map<std::string, std::shared_ptr<Option> > options();
900 std::shared_ptr<Input> create_input(std::map<std::string, Glib::VariantBase>
901 options = std::map<std::string, Glib::VariantBase>());
903 explicit InputFormat(
const struct sr_input_module *structure);
906 const struct sr_input_module *_structure;
908 friend class Context;
910 friend struct std::default_delete<InputFormat>;
914 class SR_API
Input :
public UserOwned<Input>
918 std::shared_ptr<InputDevice> device();
922 void send(
void *data,
size_t length);
927 Input(std::shared_ptr<Context> context,
const struct sr_input *structure);
929 const struct sr_input *_structure;
930 std::shared_ptr<Context> _context;
931 std::unique_ptr<InputDevice> _device;
933 friend class Context;
934 friend class InputFormat;
935 friend struct std::default_delete<Input>;
940 public ParentOwned<InputDevice, Input>,
944 InputDevice(std::shared_ptr<Input> input,
struct sr_dev_inst *sdi);
946 std::shared_ptr<Device> get_shared_from_this();
947 std::shared_ptr<Input> _input;
949 friend struct std::default_delete<InputDevice>;
953 class SR_API
Option :
public UserOwned<Option>
957 std::string id()
const;
959 std::string name()
const;
961 std::string description()
const;
963 Glib::VariantBase default_value()
const;
965 std::vector<Glib::VariantBase> values()
const;
967 Glib::VariantBase parse_string(std::string value);
969 Option(
const struct sr_option *structure,
970 std::shared_ptr<const struct sr_option *> structure_array);
972 const struct sr_option *_structure;
973 std::shared_ptr<const struct sr_option *> _structure_array;
975 friend class InputFormat;
977 friend struct std::default_delete<Option>;
982 public ParentOwned<OutputFormat, Context>
986 std::string name()
const;
988 std::string description()
const;
991 std::vector<std::string> extensions()
const;
993 std::map<std::string, std::shared_ptr<Option> > options();
997 std::shared_ptr<Output> create_output(std::shared_ptr<Device> device,
998 std::map<std::string, Glib::VariantBase> options = std::map<std::string, Glib::VariantBase>());
1003 std::shared_ptr<Output> create_output(std::string filename,
1004 std::shared_ptr<Device> device,
1005 std::map<std::string, Glib::VariantBase> options = std::map<std::string, Glib::VariantBase>());
1012 bool test_flag(
const OutputFlag *flag)
const;
1014 explicit OutputFormat(
const struct sr_output_module *structure);
1017 const struct sr_output_module *_structure;
1019 friend class Context;
1021 friend struct std::default_delete<OutputFormat>;
1025 class SR_API
Output :
public UserOwned<Output>
1030 std::string receive(std::shared_ptr<Packet> packet);
1032 std::shared_ptr<OutputFormat> format();
1034 Output(std::shared_ptr<OutputFormat> format, std::shared_ptr<Device> device);
1035 Output(std::shared_ptr<OutputFormat> format,
1036 std::shared_ptr<Device> device, std::map<std::string, Glib::VariantBase> options);
1037 Output(std::string filename, std::shared_ptr<OutputFormat> format,
1038 std::shared_ptr<Device> device, std::map<std::string, Glib::VariantBase> options);
1041 const struct sr_output *_structure;
1042 const std::shared_ptr<OutputFormat> _format;
1043 const std::shared_ptr<Device> _device;
1044 const std::map<std::string, Glib::VariantBase> _options;
1046 friend class OutputFormat;
1047 friend struct std::default_delete<Output>;
1051 template <
class Class,
typename Enum>
class SR_API
EnumValue 1057 return static_cast<int>(_id);
1065 static const Class *
get(
int id)
1067 const auto pos = _values.find(static_cast<Enum>(
id));
1068 if (pos == _values.end())
1069 throw Error(SR_ERR_ARG);
1075 std::vector<const Class *> result;
1076 for (
auto entry : _values)
1077 result.push_back(entry.second);
1081 EnumValue(Enum
id,
const char name[]) : _id(
id), _name(name)
1088 static const std::map<const Enum, const Class * const> _values;
1090 const std::string _name;
1095 #include <libsigrokcxx/enums.hpp> An output instance (an output format applied to a device)
Payload of a datafeed metadata packet.
A stage in a trigger configuration.
A packet on the session datafeed.
A real hardware device, connected via a driver.
static std::vector< const Class * > values()
Get possible values.
Configuration capability.
A virtual device associated with a stored session.
An option used by an output format.
A virtual device associated with an input.
A virtual device, created by the user.
The global libsigrok context.
An output format supported by the library.
Exception thrown when an error code is returned by any libsigrok call.
A hardware driver provided by the library.
int id() const
The integer constant associated with this value.
An input instance (an input format applied to a file or stream)
A group of channels on a device, which share some configuration.
Number represented by a numerator/denominator integer pair.
Payload of a datafeed packet with logic data.
A match condition in a trigger configuration.
Flag applied to output modules.
Base class for objects which wrap an enumeration value from libsigrok.
A generic device, either hardware or virtual.
Resource reader delegate.
An object that can be configured.
std::string name() const
The name associated with this value.
Abstract base class for datafeed packet payloads.
Payload of a datafeed header packet.
Payload of a datafeed packet with analog data.
An input format supported by the library.