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/>.
23 #include "libsigrok/libsigrok.h"
24 #include <glibmm-2.4/glibmm.h>
36 /* Forward declarations */
41 class SR_API HardwareDevice;
43 class SR_API EventSource;
45 class SR_API ConfigKey;
46 class SR_API InputFormat;
47 class SR_API OutputFormat;
48 class SR_API LogLevel;
49 class SR_API ChannelGroup;
51 class SR_API TriggerStage;
52 class SR_API TriggerMatch;
53 class SR_API TriggerMatchType;
54 class SR_API ChannelType;
56 class SR_API PacketPayload;
57 class SR_API PacketType;
58 class SR_API Quantity;
60 class SR_API QuantityFlag;
61 class SR_API InputFileDevice;
63 class SR_API DataType;
65 /** Exception thrown when an error code is returned by any libsigrok call. */
66 class SR_API Error: public exception
72 const char *what() const throw();
75 /** Base template for most classes which wrap a struct type from libsigrok. */
76 template <class Parent, typename Struct> class SR_API StructureWrapper :
77 public enable_shared_from_this<StructureWrapper<Parent, Struct> >
80 /** Parent object which owns this child object's underlying structure.
82 This shared pointer will be null when this child is unused, but
83 will be assigned to point to the parent before any shared pointer
84 to this child is handed out to the user.
86 When the reference count of this child falls to zero, this shared
87 pointer to its parent is reset by a custom deleter on the child's
90 This strategy ensures that the destructors for both the child and
91 the parent are called at the correct time, i.e. only when all
92 references to both the parent and all its children are gone. */
93 shared_ptr<Parent> parent;
97 StructureWrapper<Parent, Struct>(Struct *structure) :
103 /** Type of log callback */
104 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
107 class SR_API Context : public enable_shared_from_this<Context>
110 /** Create new context */
111 static shared_ptr<Context> create();
112 /** libsigrok package version. */
113 string get_package_version();
114 /** libsigrok library version. */
115 string get_lib_version();
116 /** Available hardware drivers, indexed by name. */
117 map<string, shared_ptr<Driver> > get_drivers();
118 /** Available input formats, indexed by name. */
119 map<string, shared_ptr<InputFormat> > get_input_formats();
120 /** Available output formats, indexed by name. */
121 map<string, shared_ptr<OutputFormat> > get_output_formats();
122 /** Current log level. */
123 const LogLevel *get_log_level();
124 /** Set the log level. */
125 void set_log_level(const LogLevel *level);
126 /** Current log domain. */
127 string get_log_domain();
128 /** Set the log domain. */
129 void set_log_domain(string value);
130 /** Set the log callback. */
131 void set_log_callback(LogCallbackFunction callback);
132 /** Set the log callback to the default handler. */
133 void set_log_callback_default();
134 /** Create a new session. */
135 shared_ptr<Session> create_session();
136 /** Load a saved session. */
137 shared_ptr<Session> load_session(string filename);
138 /** Create a new trigger. */
139 shared_ptr<Trigger> create_trigger(string name);
141 struct sr_context *structure;
142 map<string, Driver *> drivers;
143 map<string, InputFormat *> input_formats;
144 map<string, OutputFormat *> output_formats;
146 LogCallbackFunction log_callback;
149 /** Deleter needed to allow shared_ptr use with protected destructor. */
153 void operator()(Context *context) { delete context; }
155 friend class Deleter;
156 friend class Session;
160 /** Hardware driver */
161 class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
164 /** Name of this driver. */
166 /** Long name for this driver. */
167 string get_long_name();
168 /** Scan for devices and return a list of devices found. */
169 vector<shared_ptr<HardwareDevice> > scan(
170 map<const ConfigKey *, Glib::VariantBase> options = {});
173 vector<HardwareDevice *> devices;
174 Driver(struct sr_dev_driver *structure);
176 friend class Context;
177 friend class HardwareDevice;
178 friend class ChannelGroup;
181 /** An object that can be configured. */
182 class SR_API Configurable
185 /** Read configuration for the given key. */
186 Glib::VariantBase config_get(const ConfigKey *key);
187 /** Set configuration for the given key to a specified value. */
188 void config_set(const ConfigKey *key, Glib::VariantBase value);
189 /** Enumerate available values for the given configuration key. */
190 Glib::VariantBase config_list(const ConfigKey *key);
193 struct sr_dev_driver *driver,
194 struct sr_dev_inst *sdi,
195 struct sr_channel_group *channel_group);
196 virtual ~Configurable();
197 struct sr_dev_driver *config_driver;
198 struct sr_dev_inst *config_sdi;
199 struct sr_channel_group *config_channel_group;
202 /** Generic device (may be real or from an input file) */
203 class SR_API Device :
205 public StructureWrapper<Context, struct sr_dev_inst>
208 /** Vendor name for this device. */
210 /** Model name for this device. */
212 /** Version string for this device. */
213 string get_version();
214 /** List of the channels available on this device. */
215 vector<shared_ptr<Channel> > get_channels();
221 Device(struct sr_dev_inst *structure);
223 vector<Channel *> channels;
224 /** Deleter needed to allow shared_ptr use with protected destructor. */
228 void operator()(Device *device) { delete device; }
230 friend class Deleter;
231 friend class Session;
232 friend class Channel;
233 friend class ChannelGroup;
237 /** Hardware device (connected via a driver) */
238 class SR_API HardwareDevice : public Device
241 /** Driver providing this device. */
242 shared_ptr<Driver> get_driver();
243 /** Channel groups available on this device, indexed by name. */
244 map<string, shared_ptr<ChannelGroup> > get_channel_groups();
246 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
249 map<string, ChannelGroup *> channel_groups;
251 friend class ChannelGroup;
255 class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
258 /** Current name of this channel. */
260 /** Set the name of this channel. */
261 void set_name(string name);
262 /** Type of this channel. */
263 const ChannelType *get_type();
264 /** Enabled status of this channel. */
266 /** Set the enabled status of this channel. */
267 void set_enabled(bool value);
269 Channel(struct sr_channel *structure);
271 const ChannelType * const type;
273 friend class ChannelGroup;
274 friend class Session;
275 friend class TriggerStage;
279 class SR_API ChannelGroup :
280 public StructureWrapper<HardwareDevice, struct sr_channel_group>,
284 /** Name of this channel group. */
286 /** List of the channels in this group. */
287 vector<shared_ptr<Channel> > get_channels();
289 ChannelGroup(HardwareDevice *device, struct sr_channel_group *structure);
291 vector<Channel *> channels;
292 friend class HardwareDevice;
296 class SR_API Trigger : public enable_shared_from_this<Trigger>
300 vector<shared_ptr<TriggerStage> > get_stages();
301 shared_ptr<TriggerStage> add_stage();
303 Trigger(shared_ptr<Context> context, string name);
305 struct sr_trigger *structure;
306 shared_ptr<Context> context;
307 vector<TriggerStage *> stages;
308 /** Deleter needed to allow shared_ptr use with protected destructor. */
312 void operator()(Trigger *trigger) { delete trigger; }
314 friend class Context;
315 friend class Session;
319 class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
323 vector<shared_ptr<TriggerMatch> > get_matches();
324 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
325 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
327 vector<TriggerMatch *> matches;
328 TriggerStage(struct sr_trigger_stage *structure);
330 friend class Trigger;
334 class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
337 shared_ptr<Channel> get_channel();
338 const TriggerMatchType *get_type();
341 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
343 shared_ptr<Channel> channel;
344 friend class TriggerStage;
347 /** Type of datafeed callback */
348 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
349 DatafeedCallbackFunction;
351 /** Data required for C callback function to call a C++ datafeed callback */
352 class SR_PRIV DatafeedCallbackData
355 void run(const struct sr_dev_inst *sdi,
356 const struct sr_datafeed_packet *pkt);
358 DatafeedCallbackFunction callback;
359 DatafeedCallbackData(Session *session,
360 DatafeedCallbackFunction callback);
362 friend class Session;
365 /** Type of source callback */
366 typedef function<bool(Glib::IOCondition)>
367 SourceCallbackFunction;
369 /** Data required for C callback function to call a C++ source callback */
370 class SR_PRIV SourceCallbackData
373 bool run(int revents);
375 SourceCallbackData(shared_ptr<EventSource> source);
376 shared_ptr<EventSource> source;
377 friend class Session;
381 class SR_API EventSource
384 /** Create an event source from a file descriptor. */
385 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
386 int timeout, SourceCallbackFunction callback);
387 /** Create an event source from a Glib::PollFD */
388 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
389 SourceCallbackFunction callback);
390 /** Create an event source from a Glib::IOChannel */
391 static shared_ptr<EventSource> create(
392 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
393 int timeout, SourceCallbackFunction callback);
395 EventSource(int timeout, SourceCallbackFunction callback);
404 Glib::RefPtr<Glib::IOChannel> channel;
405 Glib::IOCondition events;
407 SourceCallbackFunction callback;
408 /** Deleter needed to allow shared_ptr use with protected destructor. */
412 void operator()(EventSource *source) { delete source; }
414 friend class Deleter;
415 friend class Session;
416 friend class SourceCallbackData;
423 /** Add a device to this session. */
424 void add_device(shared_ptr<Device> device);
425 /** List devices attached to this session. */
426 vector<shared_ptr<Device> > get_devices();
427 /** Remove all devices from this session. */
428 void remove_devices();
429 /** Add a datafeed callback to this session. */
430 void add_datafeed_callback(DatafeedCallbackFunction callback);
431 /** Remove all datafeed callbacks from this session. */
432 void remove_datafeed_callbacks();
433 /** Add an event source. */
434 void add_source(shared_ptr<EventSource> source);
435 /** Remove an event source. */
436 void remove_source(shared_ptr<EventSource> source);
437 /** Start the session. */
439 /** Run the session event loop. */
441 /** Stop the session. */
443 /** Begin saving session to a file. */
444 void begin_save(string filename);
445 /** Append a packet to the session file being saved. */
446 void append(shared_ptr<Device> device, shared_ptr<Packet> packet);
447 /** Get current trigger setting. */
448 shared_ptr<Trigger> get_trigger();
449 /** Set trigger setting. */
450 void set_trigger(shared_ptr<Trigger> trigger);
452 Session(shared_ptr<Context> context);
453 Session(shared_ptr<Context> context, string filename);
455 struct sr_session *structure;
456 const shared_ptr<Context> context;
457 map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
458 vector<DatafeedCallbackData *> datafeed_callbacks;
459 map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
461 bool save_initialized;
462 string save_filename;
463 uint64_t save_samplerate;
464 shared_ptr<Trigger> trigger;
465 /** Deleter needed to allow shared_ptr use with protected destructor. */
469 void operator()(Session *session) { delete session; }
471 friend class Deleter;
472 friend class Context;
473 friend class DatafeedCallbackData;
476 /** Datafeed packet */
480 /** Payload of this packet. */
481 PacketPayload *get_payload();
483 Packet(const struct sr_datafeed_packet *structure);
485 const struct sr_datafeed_packet *structure;
486 PacketPayload *payload;
487 /** Deleter needed to allow shared_ptr use with protected destructor. */
491 void operator()(Packet *packet) { delete packet; }
493 friend class Deleter;
494 friend class Session;
496 friend class DatafeedCallbackData;
499 /** Abstract base class for datafeed packet payloads. */
500 class SR_API PacketPayload
504 virtual ~PacketPayload() = 0;
505 virtual void *get_data() = 0;
506 virtual size_t get_data_size() = 0;
511 /** Logic data payload */
512 class SR_API Logic : public PacketPayload
515 Logic(const struct sr_datafeed_logic *structure);
517 const struct sr_datafeed_logic *structure;
518 vector<uint8_t> data;
520 size_t get_data_size();
524 /** Analog data payload */
525 class SR_API Analog : public PacketPayload
528 /** Number of samples in this packet. */
529 unsigned int get_num_samples();
530 /** Measured quantity of the samples in this packet. */
531 const Quantity *get_mq();
532 /** Unit of the samples in this packet. */
533 const Unit *get_unit();
534 /** Measurement flags associated with the samples in this packet. */
535 vector<const QuantityFlag *> get_mq_flags();
537 Analog(const struct sr_datafeed_analog *structure);
539 const struct sr_datafeed_analog *structure;
541 size_t get_data_size();
546 class SR_API InputFormat :
547 public StructureWrapper<Context, struct sr_input_format>
550 /** Name of this input format. */
552 /** Description of this input format. */
553 string get_description();
554 /** Check whether a given file matches this input format. */
555 bool format_match(string filename);
556 /** Open a file using this input format. */
557 shared_ptr<InputFileDevice> open_file(string filename,
558 map<string, string> options = {});
560 InputFormat(struct sr_input_format *structure);
562 friend class Context;
563 friend class InputFileDevice;
566 /** Virtual device associated with an input file */
567 class SR_API InputFileDevice : public Device
570 /** Load data from file. */
573 InputFileDevice(shared_ptr<InputFormat> format,
574 struct sr_input *input, string filename);
576 struct sr_input *input;
577 shared_ptr<InputFormat> format;
579 /** Deleter needed to allow shared_ptr use with protected destructor. */
583 void operator()(InputFileDevice *device) { delete device; }
585 friend class Deleter;
586 friend class InputFormat;
590 class SR_API OutputFormat :
591 public StructureWrapper<Context, struct sr_output_format>
594 /** Name of this output format. */
596 /** Description of this output format. */
597 string get_description();
598 /** Create an output using this format. */
599 shared_ptr<Output> create_output(shared_ptr<Device> device, map<string, string> options = {});
601 OutputFormat(struct sr_output_format *structure);
603 friend class Context;
607 /** Output instance (an output format applied to a device) */
611 /** Update output with data from the given packet. */
612 string receive(shared_ptr<Packet> packet);
614 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
615 Output(shared_ptr<OutputFormat> format,
616 shared_ptr<Device> device, map<string, string> options);
618 struct sr_output *structure;
619 const shared_ptr<OutputFormat> format;
620 const shared_ptr<Device> device;
621 const map<string, string> options;
622 /** Deleter needed to allow shared_ptr use with protected destructor. */
626 void operator()(Output *output) { delete output; }
628 friend class Deleter;
629 friend class OutputFormat;
632 /** Base class for objects which wrap an enumeration value from libsigrok */
633 template <typename T> class SR_API EnumValue
636 /** The enum constant associated with this value. */
637 T get_id() const { return id; }
638 /** The name associated with this value. */
639 string get_name() const { return name; }
641 EnumValue(T id, const char name[]) : id(id), name(name) {}
651 #endif // LIBSIGROK_HPP