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
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;
119 class SR_API InputDevice;
121 class SR_API DataType;
123 class SR_API UserDevice;
126 class SR_API
Error:
public exception
129 explicit Error(
int result);
132 const char *what()
const noexcept;
136 template <
class Class,
class Parent>
137 class SR_API ParentOwned
141 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 shared_ptr<Parent> _parent;
169 shared_ptr<Class> shared_from_this()
171 shared_ptr<Class> shared = _weak_this.lock();
175 shared.reset(static_cast<Class *>(
this), &reset_parent);
182 shared_ptr<Class> share_owned_by(shared_ptr<Parent> parent)
185 throw Error(SR_ERR_BUG);
186 this->_parent = parent;
187 return shared_from_this();
192 shared_ptr<Parent> parent()
199 template <
class Class>
200 class SR_API UserOwned :
public enable_shared_from_this<Class>
205 shared_ptr<Class> shared_from_this()
207 auto shared = enable_shared_from_this<Class>::shared_from_this();
209 throw Error(SR_ERR_BUG);
215 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
225 virtual void open(
struct sr_resource *res,
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 shared_ptr<Context> create();
247 static string package_version();
249 static string lib_version();
251 map<string, shared_ptr<Driver> > drivers();
253 map<string, shared_ptr<InputFormat> > input_formats();
255 map<string, shared_ptr<OutputFormat> > output_formats();
260 void set_log_level(
const LogLevel *level);
263 void set_log_callback(LogCallbackFunction callback);
265 void set_log_callback_default();
270 shared_ptr<Session> create_session();
272 shared_ptr<UserDevice> create_user_device(
273 string vendor,
string model,
string version);
275 shared_ptr<Packet> create_header_packet(Glib::TimeVal start_time);
277 shared_ptr<Packet> create_meta_packet(
278 map<const ConfigKey *, Glib::VariantBase> config);
280 shared_ptr<Packet> create_logic_packet(
281 void *data_pointer,
size_t data_length,
unsigned int unit_size);
283 shared_ptr<Packet> create_analog_packet(
284 vector<shared_ptr<Channel> > channels,
285 float *data_pointer,
unsigned int num_samples,
const Quantity *mq,
286 const Unit *unit, vector<const QuantityFlag *> mqflags);
289 shared_ptr<Session> load_session(
string filename);
292 shared_ptr<Trigger> create_trigger(
string name);
295 shared_ptr<Input> open_file(
string filename);
298 shared_ptr<Input> open_stream(
string header);
299 map<string, string> serials(shared_ptr<Driver> driver)
const;
301 struct sr_context *_structure;
302 map<string, unique_ptr<Driver> > _drivers;
303 map<string, unique_ptr<InputFormat> > _input_formats;
304 map<string, unique_ptr<OutputFormat> > _output_formats;
306 LogCallbackFunction _log_callback;
311 friend struct std::default_delete<Context>;
319 set<const ConfigKey *> config_keys()
const;
322 Glib::VariantBase config_get(
const ConfigKey *key)
const;
326 void config_set(
const ConfigKey *key,
const Glib::VariantBase &value);
329 Glib::VariantContainerBase config_list(
const ConfigKey *key)
const;
332 set<const Capability *> config_capabilities(
const ConfigKey *key)
const;
339 struct sr_dev_driver *driver,
340 struct sr_dev_inst *sdi,
341 struct sr_channel_group *channel_group);
343 struct sr_dev_driver *config_driver;
344 struct sr_dev_inst *config_sdi;
345 struct sr_channel_group *config_channel_group;
355 string long_name()
const;
357 set<const ConfigKey *> scan_options()
const;
360 vector<shared_ptr<HardwareDevice> > scan(map<const ConfigKey *, Glib::VariantBase>
361 options = map<const ConfigKey *, Glib::VariantBase>());
363 struct sr_dev_driver *_structure;
365 vector<HardwareDevice *> _devices;
366 explicit Driver(
struct sr_dev_driver *structure);
371 friend struct std::default_delete<Driver>;
379 string vendor()
const;
381 string model()
const;
383 string version()
const;
385 string serial_number()
const;
387 string connection_id()
const;
389 vector<shared_ptr<Channel> > channels();
391 map<string, shared_ptr<ChannelGroup> > channel_groups();
397 explicit Device(
struct sr_dev_inst *structure);
399 virtual shared_ptr<Device> get_shared_from_this() = 0;
400 shared_ptr<Channel> get_channel(
struct sr_channel *ptr);
402 struct sr_dev_inst *_structure;
403 map<struct sr_channel *, unique_ptr<Channel> > _channels;
405 map<string, unique_ptr<ChannelGroup> > _channel_groups;
412 friend struct std::default_delete<Device>;
417 public UserOwned<HardwareDevice>,
422 shared_ptr<Driver> driver();
424 HardwareDevice(shared_ptr<Driver> driver,
struct sr_dev_inst *structure);
426 shared_ptr<Device> get_shared_from_this();
427 shared_ptr<Driver> _driver;
431 friend struct std::default_delete<HardwareDevice>;
436 public UserOwned<UserDevice>,
441 shared_ptr<Channel> add_channel(
unsigned int index,
const ChannelType *type,
string name);
443 UserDevice(
string vendor,
string model,
string version);
445 shared_ptr<Device> get_shared_from_this();
448 friend struct std::default_delete<UserDevice>;
453 public ParentOwned<Channel, Device>
460 void set_name(
string name);
464 bool enabled()
const;
467 void set_enabled(
bool value);
469 unsigned int index()
const;
471 explicit Channel(
struct sr_channel *structure);
473 struct sr_channel *_structure;
481 friend struct std::default_delete<Channel>;
486 public ParentOwned<ChannelGroup, Device>,
493 vector<shared_ptr<Channel> > channels();
497 vector<Channel *> _channels;
499 friend struct std::default_delete<ChannelGroup>;
503 class SR_API
Trigger :
public UserOwned<Trigger>
509 vector<shared_ptr<TriggerStage> > stages();
511 shared_ptr<TriggerStage> add_stage();
513 Trigger(shared_ptr<Context> context,
string name);
515 struct sr_trigger *_structure;
516 shared_ptr<Context> _context;
517 vector<unique_ptr<TriggerStage> > _stages;
520 friend struct std::default_delete<Trigger>;
525 public ParentOwned<TriggerStage, Trigger>
531 vector<shared_ptr<TriggerMatch> > matches();
540 void add_match(shared_ptr<Channel> channel,
const TriggerMatchType *type,
float value);
542 struct sr_trigger_stage *_structure;
543 vector<unique_ptr<TriggerMatch> > _matches;
544 explicit TriggerStage(
struct sr_trigger_stage *structure);
547 friend struct std::default_delete<TriggerStage>;
552 public ParentOwned<TriggerMatch, TriggerStage>
556 shared_ptr<Channel> channel();
562 TriggerMatch(
struct sr_trigger_match *structure, shared_ptr<Channel> channel);
564 struct sr_trigger_match *_structure;
565 shared_ptr<Channel> _channel;
567 friend struct std::default_delete<TriggerMatch>;
571 typedef function<void()> SessionStoppedCallback;
574 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
575 DatafeedCallbackFunction;
578 class SR_PRIV DatafeedCallbackData
581 void run(
const struct sr_dev_inst *sdi,
582 const struct sr_datafeed_packet *pkt);
584 DatafeedCallbackFunction _callback;
585 DatafeedCallbackData(
Session *session,
586 DatafeedCallbackFunction callback);
593 public ParentOwned<SessionDevice, Session>,
599 shared_ptr<Device> get_shared_from_this();
602 friend struct std::default_delete<SessionDevice>;
606 class SR_API
Session :
public UserOwned<Session>
611 void add_device(shared_ptr<Device> device);
613 vector<shared_ptr<Device> > devices();
615 void remove_devices();
618 void add_datafeed_callback(DatafeedCallbackFunction callback);
620 void remove_datafeed_callbacks();
628 bool is_running()
const;
630 void set_stopped_callback(SessionStoppedCallback callback);
632 shared_ptr<Trigger> trigger();
634 shared_ptr<Context> context();
637 void set_trigger(shared_ptr<Trigger> trigger);
639 string filename()
const;
641 explicit Session(shared_ptr<Context> context);
642 Session(shared_ptr<Context> context,
string filename);
644 shared_ptr<Device> get_device(
const struct sr_dev_inst *sdi);
645 struct sr_session *_structure;
646 const shared_ptr<Context> _context;
647 map<const struct sr_dev_inst *, unique_ptr<SessionDevice> > _owned_devices;
648 map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
649 vector<unique_ptr<DatafeedCallbackData> > _datafeed_callbacks;
650 SessionStoppedCallback _stopped_callback;
652 shared_ptr<Trigger> _trigger;
655 friend class DatafeedCallbackData;
657 friend struct std::default_delete<Session>;
661 class SR_API
Packet :
public UserOwned<Packet>
667 shared_ptr<PacketPayload> payload();
669 Packet(shared_ptr<Device> device,
670 const struct sr_datafeed_packet *structure);
672 const struct sr_datafeed_packet *_structure;
673 shared_ptr<Device> _device;
674 unique_ptr<PacketPayload> _payload;
678 friend class DatafeedCallbackData;
684 friend struct std::default_delete<Packet>;
694 virtual shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent) = 0;
698 friend struct std::default_delete<PacketPayload>;
703 public ParentOwned<Header, Packet>,
708 int feed_version()
const;
710 Glib::TimeVal start_time()
const;
712 explicit Header(
const struct sr_datafeed_header *structure);
714 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
716 const struct sr_datafeed_header *_structure;
723 public ParentOwned<Meta, Packet>,
728 map<const ConfigKey *, Glib::VariantBase> config()
const;
730 explicit Meta(
const struct sr_datafeed_meta *structure);
732 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
734 const struct sr_datafeed_meta *_structure;
735 map<const ConfigKey *, Glib::VariantBase> _config;
742 public ParentOwned<Logic, Packet>,
747 void *data_pointer();
749 size_t data_length()
const;
751 unsigned int unit_size()
const;
753 explicit Logic(
const struct sr_datafeed_logic *structure);
755 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
757 const struct sr_datafeed_logic *_structure;
764 public ParentOwned<Analog, Packet>,
769 void *data_pointer();
771 unsigned int num_samples()
const;
773 vector<shared_ptr<Channel> > channels();
777 const Unit *unit()
const;
779 vector<const QuantityFlag *> mq_flags()
const;
781 explicit Analog(
const struct sr_datafeed_analog *structure);
783 shared_ptr<PacketPayload> share_owned_by(shared_ptr<Packet> parent);
785 const struct sr_datafeed_analog *_structure;
792 public ParentOwned<InputFormat, Context>
798 string description()
const;
801 vector<string> extensions()
const;
803 map<string, shared_ptr<Option> > options();
806 shared_ptr<Input> create_input(map<string, Glib::VariantBase>
807 options = map<string, Glib::VariantBase>());
809 explicit InputFormat(
const struct sr_input_module *structure);
812 const struct sr_input_module *_structure;
816 friend struct std::default_delete<InputFormat>;
820 class SR_API
Input :
public UserOwned<Input>
824 shared_ptr<InputDevice> device();
828 void send(
void *data,
size_t length);
832 Input(shared_ptr<Context> context,
const struct sr_input *structure);
834 const struct sr_input *_structure;
835 shared_ptr<Context> _context;
836 unique_ptr<InputDevice> _device;
840 friend struct std::default_delete<Input>;
845 public ParentOwned<InputDevice, Input>,
849 InputDevice(shared_ptr<Input> input,
struct sr_dev_inst *sdi);
851 shared_ptr<Device> get_shared_from_this();
852 shared_ptr<Input> _input;
854 friend struct std::default_delete<InputDevice>;
858 class SR_API
Option :
public UserOwned<Option>
866 string description()
const;
868 Glib::VariantBase default_value()
const;
870 vector<Glib::VariantBase> values()
const;
872 Option(
const struct sr_option *structure,
873 shared_ptr<const struct sr_option *> structure_array);
875 const struct sr_option *_structure;
876 shared_ptr<const struct sr_option *> _structure_array;
880 friend struct std::default_delete<Option>;
885 public ParentOwned<OutputFormat, Context>
891 string description()
const;
894 vector<string> extensions()
const;
896 map<string, shared_ptr<Option> > options();
900 shared_ptr<Output> create_output(shared_ptr<Device> device,
901 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
906 shared_ptr<Output> create_output(
string filename,
907 shared_ptr<Device> device,
908 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
917 explicit OutputFormat(
const struct sr_output_module *structure);
920 const struct sr_output_module *_structure;
924 friend struct std::default_delete<OutputFormat>;
928 class SR_API
Output :
public UserOwned<Output>
933 string receive(shared_ptr<Packet> packet);
935 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
936 Output(shared_ptr<OutputFormat> format,
937 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
938 Output(
string filename, shared_ptr<OutputFormat> format,
939 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
942 const struct sr_output *_structure;
943 const shared_ptr<OutputFormat> _format;
944 const shared_ptr<Device> _device;
945 const map<string, Glib::VariantBase> _options;
948 friend struct std::default_delete<Output>;
952 template <
class Class,
typename Enum>
class SR_API
EnumValue
958 return static_cast<int>(_id);
966 static const Class *
get(
int id)
968 const auto pos = _values.find(static_cast<Enum>(
id));
969 if (pos == _values.end())
970 throw Error(SR_ERR_ARG);
974 static std::vector<const Class *>
values()
976 std::vector<const Class *> result;
977 for (
auto entry : _values)
978 result.push_back(entry.second);
982 EnumValue(Enum
id,
const char name[]) : _id(id), _name(name)
989 static const std::map<const Enum, const Class * const> _values;
996 #include <libsigrokcxx/enums.hpp>
Flag applied to output modules.
An option used by an output format.
A generic device, either hardware or virtual.
A virtual device, created by the user.
Abstract base class for datafeed packet payloads.
Payload of a datafeed header packet.
Payload of a datafeed packet with analog data.
A match condition in a trigger configuration.
An output instance (an output format applied to a device)
Payload of a datafeed metadata packet.
An input format supported by the library.
A packet on the session datafeed.
static std::vector< const Class * > values()
Get possible values.
A real hardware device, connected via a driver.
Base class for objects which wrap an enumeration value from libsigrok.
A virtual device associated with an input.
int id() const
The integer constant associated with this value.
A stage in a trigger configuration.
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.
A virtual device associated with a stored session.
The global libsigrok context.
Resource reader delegate.
string name() const
The name 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.
Configuration capability.
Payload of a datafeed packet with logic data.
An object that can be configured.