]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/include/libsigrok/libsigrok.hpp
C++: Add Configurable::config_keys() method.
[libsigrok.git] / bindings / cxx / include / libsigrok / libsigrok.hpp
CommitLineData
c23c8659
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
07443fd2
ML
20/**
21
22@mainpage API Reference
23
24Introduction
25------------
26
27The sigrok++ API provides an object-oriented C++ interface to the functionality
28in libsigrok, including automatic memory and resource management.
29
30It is built on top of the public libsigrok C API, and is designed to be used as
31a standalone alternative API. Programs should not mix usage of the C and C++
32APIs; the C++ interface code needs to have full control of all C API calls for
33resources to be managed correctly.
34
35Memory management
36-----------------
37
38All runtime objects created through the C++ API are passed and accessed via
39shared pointers, using the C++11 std::shared_ptr implementation. This means
40that a reference count is kept for each object.
41
42Shared pointers can be copied and assigned in a user's program, automatically
43updating their reference count and deleting objects when they are no longer in
44use. The C++ interface code also keeps track of internal dependencies between
45libsigrok resources, and ensures that objects are not prematurely deleted when
46their resources are in use by other objects.
47
48This means that management of sigrok++ objects and their underlying libsigrok
49resources can be treated as fully automatic. As long as all shared pointers to
50objects are deleted or reassigned when no longer in use, all underlying
51resources will be released at the right time.
52
53Getting started
54---------------
55
56Usage of the C++ API needs to begin with a call to sigrok::Context::create().
57This will create the global libsigrok context and returns a shared pointer to
58the sigrok::Context object. Methods on this object provide access to the
59hardware drivers, input and output formats supported by the library, as well
60as means of creating other objects such as sessions and triggers.
61
62Error handling
63--------------
64
65When any libsigrok C API call returns an error, a sigrok::Error exception is
66raised, which provides access to the error code and description.
67
68*/
69
c23c8659
ML
70#ifndef LIBSIGROK_HPP
71#define LIBSIGROK_HPP
72
73#include "libsigrok/libsigrok.h"
74#include <glibmm-2.4/glibmm.h>
75
76#include <stdexcept>
77#include <memory>
78#include <vector>
79#include <map>
80
81namespace sigrok
82{
83
84using namespace std;
85
86/* Forward declarations */
87class SR_API Error;
88class SR_API Context;
89class SR_API Driver;
90class SR_API Device;
91class SR_API HardwareDevice;
92class SR_API Channel;
93class SR_API EventSource;
94class SR_API Session;
95class SR_API ConfigKey;
96class SR_API InputFormat;
97class SR_API OutputFormat;
98class SR_API LogLevel;
99class SR_API ChannelGroup;
100class SR_API Trigger;
101class SR_API TriggerStage;
102class SR_API TriggerMatch;
103class SR_API TriggerMatchType;
104class SR_API ChannelType;
105class SR_API Packet;
106class SR_API PacketPayload;
107class SR_API PacketType;
108class SR_API Quantity;
109class SR_API Unit;
110class SR_API QuantityFlag;
ca3291e3
ML
111class SR_API Input;
112class SR_API InputDevice;
c23c8659
ML
113class SR_API Output;
114class SR_API DataType;
58aa1f83 115class SR_API Option;
c23c8659
ML
116
117/** Exception thrown when an error code is returned by any libsigrok call. */
118class SR_API Error: public exception
119{
120public:
121 Error(int result);
122 ~Error() throw();
123 const int result;
124 const char *what() const throw();
125};
126
541c855e 127/* Base template for classes whose resources are owned by a parent object. */
bf52cc8c 128template <class Class, class Parent, typename Struct>
541c855e 129class SR_API ParentOwned
c23c8659 130{
7649683c 131protected:
07443fd2 132 /* Parent object which owns this child object's underlying structure.
c23c8659
ML
133
134 This shared pointer will be null when this child is unused, but
135 will be assigned to point to the parent before any shared pointer
136 to this child is handed out to the user.
137
138 When the reference count of this child falls to zero, this shared
139 pointer to its parent is reset by a custom deleter on the child's
140 shared pointer.
141
142 This strategy ensures that the destructors for both the child and
143 the parent are called at the correct time, i.e. only when all
144 references to both the parent and all its children are gone. */
145 shared_ptr<Parent> parent;
7649683c 146
0d0170ae 147 /* Weak pointer for shared_from_this() implementation. */
bf52cc8c 148 weak_ptr<Class> weak_this;
0d0170ae 149
7649683c 150public:
0d0170ae 151 /* Note, this implementation will create a new smart_ptr if none exists. */
bf52cc8c 152 shared_ptr<Class> shared_from_this()
0d0170ae 153 {
bf52cc8c 154 shared_ptr<Class> shared;
0d0170ae
ML
155
156 if (!(shared = weak_this.lock()))
157 {
bf52cc8c 158 shared = shared_ptr<Class>((Class *) this, reset_parent);
0d0170ae
ML
159 weak_this = shared;
160 }
161
162 return shared;
163 }
164
bf52cc8c 165 shared_ptr<Class> get_shared_pointer(shared_ptr<Parent> parent)
7649683c 166 {
78132e2a
ML
167 if (!parent)
168 throw Error(SR_ERR_BUG);
0d0170ae
ML
169 this->parent = parent;
170 return shared_from_this();
7649683c 171 }
0d0170ae 172
bf52cc8c 173 shared_ptr<Class> get_shared_pointer(Parent *parent)
7649683c 174 {
78132e2a
ML
175 if (!parent)
176 throw Error(SR_ERR_BUG);
bf52cc8c 177 return get_shared_pointer(parent->shared_from_this());
7649683c 178 }
c23c8659 179protected:
bf52cc8c 180 static void reset_parent(Class *object)
7649683c 181 {
78132e2a
ML
182 if (!object->parent)
183 throw Error(SR_ERR_BUG);
7649683c
ML
184 object->parent.reset();
185 }
186
c23c8659
ML
187 Struct *structure;
188
541c855e 189 ParentOwned<Class, Parent, Struct>(Struct *structure) :
c23c8659
ML
190 structure(structure)
191 {
192 }
193};
194
90e89c2a
ML
195/* Base template for classes whose resources are owned by the user. */
196template <class Class, typename Struct>
197class SR_API UserOwned : public enable_shared_from_this<Class>
198{
199public:
200 shared_ptr<Class> shared_from_this()
201 {
202 auto shared = enable_shared_from_this<Class>::shared_from_this();
203 if (!shared)
204 throw Error(SR_ERR_BUG);
205 return shared;
206 }
207protected:
208 Struct *structure;
209
210 UserOwned<Class, Struct>(Struct *structure) :
211 structure(structure)
212 {
213 }
b4ed33a7
ML
214
215 /* Deleter needed to allow shared_ptr use with protected destructor. */
216 class Deleter
217 {
218 public:
219 void operator()(Class *object) { delete object; }
220 };
90e89c2a
ML
221};
222
c23c8659
ML
223/** Type of log callback */
224typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
225
07443fd2 226/** The global libsigrok context */
90e89c2a 227class SR_API Context : public UserOwned<Context, struct sr_context>
c23c8659
ML
228{
229public:
230 /** Create new context */
231 static shared_ptr<Context> create();
232 /** libsigrok package version. */
233 string get_package_version();
234 /** libsigrok library version. */
235 string get_lib_version();
236 /** Available hardware drivers, indexed by name. */
237 map<string, shared_ptr<Driver> > get_drivers();
238 /** Available input formats, indexed by name. */
239 map<string, shared_ptr<InputFormat> > get_input_formats();
240 /** Available output formats, indexed by name. */
241 map<string, shared_ptr<OutputFormat> > get_output_formats();
242 /** Current log level. */
243 const LogLevel *get_log_level();
b6f411ac
ML
244 /** Set the log level.
245 * @param level LogLevel to use. */
c23c8659
ML
246 void set_log_level(const LogLevel *level);
247 /** Current log domain. */
248 string get_log_domain();
b6f411ac
ML
249 /** Set the log domain.
250 * @param value Log domain prefix string. */
c23c8659 251 void set_log_domain(string value);
b6f411ac
ML
252 /** Set the log callback.
253 * @param callback Callback of the form callback(LogLevel, string). */
c23c8659
ML
254 void set_log_callback(LogCallbackFunction callback);
255 /** Set the log callback to the default handler. */
256 void set_log_callback_default();
257 /** Create a new session. */
258 shared_ptr<Session> create_session();
b6f411ac
ML
259 /** Load a saved session.
260 * @param filename File name string. */
c23c8659 261 shared_ptr<Session> load_session(string filename);
b6f411ac
ML
262 /** Create a new trigger.
263 * @param name Name string for new trigger. */
c23c8659 264 shared_ptr<Trigger> create_trigger(string name);
ca3291e3
ML
265 /** Open an input file.
266 * @param filename File name string. */
267 shared_ptr<Input> open_file(string filename);
268 /** Open an input stream based on header data.
269 * @param header Initial data from stream. */
270 shared_ptr<Input> open_stream(string header);
c23c8659 271protected:
c23c8659
ML
272 map<string, Driver *> drivers;
273 map<string, InputFormat *> input_formats;
274 map<string, OutputFormat *> output_formats;
275 Session *session;
276 LogCallbackFunction log_callback;
277 Context();
278 ~Context();
c23c8659
ML
279 friend class Deleter;
280 friend class Session;
281 friend class Driver;
282};
283
c23c8659
ML
284/** An object that can be configured. */
285class SR_API Configurable
286{
287public:
b6f411ac
ML
288 /** Read configuration for the given key.
289 * @param key ConfigKey to read. */
c23c8659 290 Glib::VariantBase config_get(const ConfigKey *key);
b6f411ac
ML
291 /** Set configuration for the given key to a specified value.
292 * @param key ConfigKey to set.
293 * @param value Value to set. */
c23c8659 294 void config_set(const ConfigKey *key, Glib::VariantBase value);
b6f411ac
ML
295 /** Enumerate available values for the given configuration key.
296 * @param key ConfigKey to enumerate values for. */
e194c011 297 Glib::VariantContainerBase config_list(const ConfigKey *key);
d54190a3
ML
298 /** Enumerate available keys, according to a given index key. */
299 vector<const ConfigKey *> config_keys(const ConfigKey *key);
c23c8659
ML
300protected:
301 Configurable(
302 struct sr_dev_driver *driver,
303 struct sr_dev_inst *sdi,
304 struct sr_channel_group *channel_group);
305 virtual ~Configurable();
306 struct sr_dev_driver *config_driver;
307 struct sr_dev_inst *config_sdi;
308 struct sr_channel_group *config_channel_group;
309};
310
59b74d28
ML
311/** A hardware driver provided by the library */
312class SR_API Driver :
313 public ParentOwned<Driver, Context, struct sr_dev_driver>,
314 public Configurable
315{
316public:
317 /** Name of this driver. */
318 string get_name();
319 /** Long name for this driver. */
320 string get_long_name();
321 /** Scan for devices and return a list of devices found.
322 * @param options Mapping of (ConfigKey, value) pairs. */
323 vector<shared_ptr<HardwareDevice> > scan(
324 map<const ConfigKey *, Glib::VariantBase> options = {});
325protected:
326 bool initialized;
327 vector<HardwareDevice *> devices;
328 Driver(struct sr_dev_driver *structure);
329 ~Driver();
330 friend class Context;
331 friend class HardwareDevice;
332 friend class ChannelGroup;
333};
334
07443fd2 335/** A generic device, either hardware or virtual */
d01d2314 336class SR_API Device : public Configurable
c23c8659
ML
337{
338public:
f36ca889
ML
339 /** Description identifying this device. */
340 string get_description();
c23c8659
ML
341 /** Vendor name for this device. */
342 string get_vendor();
343 /** Model name for this device. */
344 string get_model();
345 /** Version string for this device. */
346 string get_version();
347 /** List of the channels available on this device. */
348 vector<shared_ptr<Channel> > get_channels();
6be7a7f2
ML
349 /** Channel groups available on this device, indexed by name. */
350 map<string, shared_ptr<ChannelGroup> > get_channel_groups();
c23c8659
ML
351 /** Open device. */
352 void open();
353 /** Close device. */
354 void close();
355protected:
356 Device(struct sr_dev_inst *structure);
357 ~Device();
d01d2314 358 virtual shared_ptr<Device> get_shared_from_this() = 0;
4178d971 359 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
6e5240f4 360 struct sr_dev_inst *structure;
4178d971 361 map<struct sr_channel *, Channel *> channels;
6be7a7f2 362 map<string, ChannelGroup *> channel_groups;
c23c8659
ML
363 /** Deleter needed to allow shared_ptr use with protected destructor. */
364 class Deleter
365 {
366 public:
367 void operator()(Device *device) { delete device; }
368 };
369 friend class Deleter;
370 friend class Session;
371 friend class Channel;
372 friend class ChannelGroup;
373 friend class Output;
2928f47d 374 friend class Analog;
c23c8659
ML
375};
376
07443fd2 377/** A real hardware device, connected via a driver */
6e5240f4 378class SR_API HardwareDevice :
541c855e 379 public ParentOwned<HardwareDevice, Context, struct sr_dev_inst>,
6e5240f4 380 public Device
c23c8659
ML
381{
382public:
383 /** Driver providing this device. */
384 shared_ptr<Driver> get_driver();
c23c8659
ML
385protected:
386 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
387 ~HardwareDevice();
d01d2314 388 shared_ptr<Device> get_shared_from_this();
c23c8659 389 Driver *driver;
c23c8659
ML
390 friend class Driver;
391 friend class ChannelGroup;
392};
393
07443fd2 394/** A channel on a device */
bf52cc8c 395class SR_API Channel :
541c855e 396 public ParentOwned<Channel, Device, struct sr_channel>
c23c8659
ML
397{
398public:
399 /** Current name of this channel. */
400 string get_name();
b6f411ac
ML
401 /** Set the name of this channel. *
402 * @param name Name string to set. */
c23c8659
ML
403 void set_name(string name);
404 /** Type of this channel. */
405 const ChannelType *get_type();
406 /** Enabled status of this channel. */
407 bool get_enabled();
b6f411ac
ML
408 /** Set the enabled status of this channel.
409 * @param value Boolean value to set. */
c23c8659 410 void set_enabled(bool value);
06bd935e
ML
411 /** Get the index number of this channel. */
412 unsigned int get_index();
c23c8659
ML
413protected:
414 Channel(struct sr_channel *structure);
415 ~Channel();
416 const ChannelType * const type;
417 friend class Device;
418 friend class ChannelGroup;
419 friend class Session;
420 friend class TriggerStage;
421};
422
07443fd2 423/** A group of channels on a device, which share some configuration */
c23c8659 424class SR_API ChannelGroup :
541c855e 425 public ParentOwned<ChannelGroup, Device, struct sr_channel_group>,
c23c8659
ML
426 public Configurable
427{
428public:
429 /** Name of this channel group. */
430 string get_name();
431 /** List of the channels in this group. */
432 vector<shared_ptr<Channel> > get_channels();
433protected:
6be7a7f2 434 ChannelGroup(Device *device, struct sr_channel_group *structure);
c23c8659
ML
435 ~ChannelGroup();
436 vector<Channel *> channels;
6be7a7f2 437 friend class Device;
c23c8659
ML
438};
439
07443fd2 440/** A trigger configuration */
90e89c2a 441class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
c23c8659
ML
442{
443public:
b6f411ac 444 /** Name of this trigger configuration. */
c23c8659 445 string get_name();
b6f411ac 446 /** List of the stages in this trigger. */
c23c8659 447 vector<shared_ptr<TriggerStage> > get_stages();
b6f411ac 448 /** Add a new stage to this trigger. */
c23c8659
ML
449 shared_ptr<TriggerStage> add_stage();
450protected:
451 Trigger(shared_ptr<Context> context, string name);
452 ~Trigger();
c23c8659
ML
453 shared_ptr<Context> context;
454 vector<TriggerStage *> stages;
b4ed33a7 455 friend class Deleter;
c23c8659 456 friend class Context;
6fa0eb86 457 friend class Session;
c23c8659
ML
458};
459
07443fd2 460/** A stage in a trigger configuration */
bf52cc8c 461class SR_API TriggerStage :
541c855e 462 public ParentOwned<TriggerStage, Trigger, struct sr_trigger_stage>
c23c8659
ML
463{
464public:
b6f411ac 465 /** Index number of this stage. */
c23c8659 466 int get_number();
b6f411ac 467 /** List of match conditions on this stage. */
c23c8659 468 vector<shared_ptr<TriggerMatch> > get_matches();
b6f411ac
ML
469 /** Add a new match condition to this stage.
470 * @param channel Channel to match on.
471 * @param type TriggerMatchType to apply. */
c23c8659 472 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
b6f411ac
ML
473 /** Add a new match condition to this stage.
474 * @param channel Channel to match on.
475 * @param type TriggerMatchType to apply.
476 * @param value Threshold value. */
c23c8659
ML
477 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
478protected:
479 vector<TriggerMatch *> matches;
480 TriggerStage(struct sr_trigger_stage *structure);
481 ~TriggerStage();
482 friend class Trigger;
483};
484
07443fd2 485/** A match condition in a trigger configuration */
bf52cc8c 486class SR_API TriggerMatch :
541c855e 487 public ParentOwned<TriggerMatch, TriggerStage, struct sr_trigger_match>
c23c8659
ML
488{
489public:
b6f411ac 490 /** Channel this condition matches on. */
c23c8659 491 shared_ptr<Channel> get_channel();
b6f411ac 492 /** Type of match. */
c23c8659 493 const TriggerMatchType *get_type();
b6f411ac 494 /** Threshold value. */
c23c8659
ML
495 float get_value();
496protected:
497 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
498 ~TriggerMatch();
499 shared_ptr<Channel> channel;
500 friend class TriggerStage;
501};
502
503/** Type of datafeed callback */
504typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
505 DatafeedCallbackFunction;
506
07443fd2 507/* Data required for C callback function to call a C++ datafeed callback */
c23c8659
ML
508class SR_PRIV DatafeedCallbackData
509{
510public:
511 void run(const struct sr_dev_inst *sdi,
512 const struct sr_datafeed_packet *pkt);
513protected:
514 DatafeedCallbackFunction callback;
515 DatafeedCallbackData(Session *session,
516 DatafeedCallbackFunction callback);
517 Session *session;
518 friend class Session;
519};
520
521/** Type of source callback */
522typedef function<bool(Glib::IOCondition)>
523 SourceCallbackFunction;
524
07443fd2 525/* Data required for C callback function to call a C++ source callback */
c23c8659
ML
526class SR_PRIV SourceCallbackData
527{
528public:
529 bool run(int revents);
530protected:
531 SourceCallbackData(shared_ptr<EventSource> source);
532 shared_ptr<EventSource> source;
533 friend class Session;
534};
535
07443fd2 536/** An I/O event source */
c23c8659
ML
537class SR_API EventSource
538{
539public:
b6f411ac
ML
540 /** Create an event source from a file descriptor.
541 * @param fd File descriptor.
542 * @param events GLib IOCondition event mask.
543 * @param timeout Timeout in milliseconds.
544 * @param callback Callback of the form callback(events) */
c23c8659
ML
545 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
546 int timeout, SourceCallbackFunction callback);
b6f411ac
ML
547 /** Create an event source from a GLib PollFD
548 * @param pollfd GLib PollFD
549 * @param timeout Timeout in milliseconds.
550 * @param callback Callback of the form callback(events) */
c23c8659
ML
551 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
552 SourceCallbackFunction callback);
b6f411ac
ML
553 /** Create an event source from a GLib IOChannel
554 * @param channel GLib IOChannel.
555 * @param events GLib IOCondition event mask.
556 * @param timeout Timeout in milliseconds.
557 * @param callback Callback of the form callback(events) */
c23c8659
ML
558 static shared_ptr<EventSource> create(
559 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
560 int timeout, SourceCallbackFunction callback);
561protected:
562 EventSource(int timeout, SourceCallbackFunction callback);
563 ~EventSource();
564 enum source_type {
565 SOURCE_FD,
566 SOURCE_POLLFD,
567 SOURCE_IOCHANNEL
568 } type;
569 int fd;
570 Glib::PollFD pollfd;
571 Glib::RefPtr<Glib::IOChannel> channel;
572 Glib::IOCondition events;
573 int timeout;
574 SourceCallbackFunction callback;
575 /** Deleter needed to allow shared_ptr use with protected destructor. */
576 class Deleter
577 {
578 public:
579 void operator()(EventSource *source) { delete source; }
580 };
581 friend class Deleter;
582 friend class Session;
583 friend class SourceCallbackData;
584};
585
07443fd2 586/** A sigrok session */
90e89c2a 587class SR_API Session : public UserOwned<Session, struct sr_session>
c23c8659
ML
588{
589public:
b6f411ac
ML
590 /** Add a device to this session.
591 * @param device Device to add. */
c23c8659
ML
592 void add_device(shared_ptr<Device> device);
593 /** List devices attached to this session. */
594 vector<shared_ptr<Device> > get_devices();
595 /** Remove all devices from this session. */
596 void remove_devices();
b6f411ac
ML
597 /** Add a datafeed callback to this session.
598 * @param callback Callback of the form callback(Device, Packet). */
c23c8659
ML
599 void add_datafeed_callback(DatafeedCallbackFunction callback);
600 /** Remove all datafeed callbacks from this session. */
601 void remove_datafeed_callbacks();
b6f411ac
ML
602 /** Add an I/O event source.
603 * @param source EventSource to add. */
c23c8659 604 void add_source(shared_ptr<EventSource> source);
b6f411ac
ML
605 /** Remove an event source.
606 * @param source EventSource to remove. */
c23c8659
ML
607 void remove_source(shared_ptr<EventSource> source);
608 /** Start the session. */
609 void start();
610 /** Run the session event loop. */
611 void run();
612 /** Stop the session. */
613 void stop();
b6f411ac
ML
614 /** Begin saving session to a file.
615 * @param filename File name string. */
c23c8659 616 void begin_save(string filename);
b6f411ac
ML
617 /** Append a packet to the session file being saved.
618 * @param packet Packet to append. */
1d67cfb4
ML
619 void append(shared_ptr<Packet> packet);
620 /** Append raw logic data to the session file being saved. */
621 void append(void *data, size_t length, unsigned int unit_size);
6fa0eb86
ML
622 /** Get current trigger setting. */
623 shared_ptr<Trigger> get_trigger();
b6f411ac
ML
624 /** Set trigger setting.
625 * @param trigger Trigger object to use. */
6fa0eb86 626 void set_trigger(shared_ptr<Trigger> trigger);
c23c8659
ML
627protected:
628 Session(shared_ptr<Context> context);
629 Session(shared_ptr<Context> context, string filename);
630 ~Session();
c23c8659
ML
631 const shared_ptr<Context> context;
632 map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
633 vector<DatafeedCallbackData *> datafeed_callbacks;
634 map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
635 bool saving;
636 bool save_initialized;
637 string save_filename;
638 uint64_t save_samplerate;
6fa0eb86 639 shared_ptr<Trigger> trigger;
c23c8659
ML
640 friend class Deleter;
641 friend class Context;
642 friend class DatafeedCallbackData;
643};
644
07443fd2 645/** A packet on the session datafeed */
90e89c2a 646class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
c23c8659
ML
647{
648public:
90ba83f2
ML
649 /** Type of this packet. */
650 const PacketType *get_type();
c23c8659 651 /** Payload of this packet. */
2928f47d 652 shared_ptr<PacketPayload> get_payload();
c23c8659 653protected:
2928f47d
ML
654 Packet(shared_ptr<Device> device,
655 const struct sr_datafeed_packet *structure);
c23c8659 656 ~Packet();
2928f47d 657 shared_ptr<Device> device;
c23c8659 658 PacketPayload *payload;
c23c8659
ML
659 friend class Deleter;
660 friend class Session;
661 friend class Output;
662 friend class DatafeedCallbackData;
2928f47d
ML
663 friend class Header;
664 friend class Meta;
665 friend class Logic;
666 friend class Analog;
c23c8659
ML
667};
668
07443fd2 669/** Abstract base class for datafeed packet payloads */
c23c8659
ML
670class SR_API PacketPayload
671{
672protected:
673 PacketPayload();
674 virtual ~PacketPayload() = 0;
4cd883a7 675 virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
2928f47d
ML
676 /** Deleter needed to allow shared_ptr use with protected destructor. */
677 class Deleter
678 {
679 public:
680 void operator()(PacketPayload *payload) { delete payload; }
681 };
682 friend class Deleter;
c23c8659
ML
683 friend class Packet;
684 friend class Output;
685};
686
2928f47d 687/** Payload of a datafeed header packet */
4cd883a7 688class SR_API Header :
541c855e 689 public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
4cd883a7 690 public PacketPayload
2928f47d
ML
691{
692public:
b6f411ac 693 /* Feed version number. */
2928f47d 694 int get_feed_version();
b6f411ac 695 /* Start time of this session. */
2928f47d
ML
696 Glib::TimeVal get_start_time();
697protected:
698 Header(const struct sr_datafeed_header *structure);
699 ~Header();
4cd883a7 700 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
2928f47d
ML
701 friend class Packet;
702};
703
704/** Payload of a datafeed metadata packet */
4cd883a7 705class SR_API Meta :
541c855e 706 public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
4cd883a7 707 public PacketPayload
2928f47d
ML
708{
709public:
b6f411ac 710 /* Mapping of (ConfigKey, value) pairs. */
2928f47d
ML
711 map<const ConfigKey *, Glib::VariantBase> get_config();
712protected:
713 Meta(const struct sr_datafeed_meta *structure);
714 ~Meta();
4cd883a7 715 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
2928f47d
ML
716 map<const ConfigKey *, Glib::VariantBase> config;
717 friend class Packet;
718};
719
07443fd2 720/** Payload of a datafeed packet with logic data */
4cd883a7 721class SR_API Logic :
541c855e 722 public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
4cd883a7 723 public PacketPayload
c23c8659 724{
2928f47d
ML
725public:
726 /* Pointer to data. */
727 void *get_data_pointer();
728 /* Data length in bytes. */
729 size_t get_data_length();
730 /* Size of each sample in bytes. */
731 unsigned int get_unit_size();
c23c8659
ML
732protected:
733 Logic(const struct sr_datafeed_logic *structure);
734 ~Logic();
4cd883a7 735 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
c23c8659
ML
736 friend class Packet;
737};
738
07443fd2 739/** Payload of a datafeed packet with analog data */
4cd883a7 740class SR_API Analog :
541c855e 741 public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
4cd883a7 742 public PacketPayload
c23c8659
ML
743{
744public:
2928f47d
ML
745 /** Pointer to data. */
746 float *get_data_pointer();
c23c8659
ML
747 /** Number of samples in this packet. */
748 unsigned int get_num_samples();
2928f47d
ML
749 /** Channels for which this packet contains data. */
750 vector<shared_ptr<Channel> > get_channels();
c23c8659
ML
751 /** Measured quantity of the samples in this packet. */
752 const Quantity *get_mq();
753 /** Unit of the samples in this packet. */
754 const Unit *get_unit();
755 /** Measurement flags associated with the samples in this packet. */
756 vector<const QuantityFlag *> get_mq_flags();
757protected:
758 Analog(const struct sr_datafeed_analog *structure);
759 ~Analog();
4cd883a7 760 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
c23c8659
ML
761 friend class Packet;
762};
763
07443fd2 764/** An input format supported by the library */
c23c8659 765class SR_API InputFormat :
541c855e 766 public ParentOwned<InputFormat, Context, const struct sr_input_module>
c23c8659
ML
767{
768public:
769 /** Name of this input format. */
770 string get_name();
771 /** Description of this input format. */
772 string get_description();
ca3291e3
ML
773 /** Options supported by this input format. */
774 map<string, shared_ptr<Option> > get_options();
775 /** Create an input using this input format.
776 * @param options Mapping of (option name, value) pairs. */
777 shared_ptr<Input> create_input(map<string, Glib::VariantBase> options = {});
c23c8659 778protected:
ca3291e3 779 InputFormat(const struct sr_input_module *structure);
c23c8659
ML
780 ~InputFormat();
781 friend class Context;
ca3291e3 782 friend class InputDevice;
c23c8659
ML
783};
784
ca3291e3 785/** An input instance (an input format applied to a file or stream) */
90e89c2a 786class SR_API Input : public UserOwned<Input, const struct sr_input>
c23c8659
ML
787{
788public:
ca3291e3
ML
789 /** Virtual device associated with this input. */
790 shared_ptr<InputDevice> get_device();
791 /** Send next stream data.
792 * @param data Next stream data. */
793 void send(string data);
c23c8659 794protected:
ca3291e3
ML
795 Input(shared_ptr<Context> context, const struct sr_input *structure);
796 ~Input();
ca3291e3
ML
797 shared_ptr<Context> context;
798 InputDevice *device;
c23c8659 799 friend class Deleter;
ca3291e3 800 friend class Context;
c23c8659
ML
801 friend class InputFormat;
802};
803
ca3291e3 804/** A virtual device associated with an input */
6e5240f4 805class SR_API InputDevice :
541c855e 806 public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
6e5240f4 807 public Device
ca3291e3
ML
808{
809protected:
810 InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
811 ~InputDevice();
d01d2314 812 shared_ptr<Device> get_shared_from_this();
ca3291e3 813 shared_ptr<Input> input;
ca3291e3
ML
814 friend class Input;
815};
816
58aa1f83 817/** An option used by an output format */
90e89c2a 818class SR_API Option : public UserOwned<Option, const struct sr_option>
58aa1f83
ML
819{
820public:
821 /** Short name of this option suitable for command line usage. */
822 string get_id();
823 /** Short name of this option suitable for GUI usage. */
824 string get_name();
825 /** Description of this option in a sentence. */
826 string get_description();
827 /** Default value for this option. */
828 Glib::VariantBase get_default_value();
829 /** Possible values for this option, if a limited set. */
830 vector<Glib::VariantBase> get_values();
831protected:
832 Option(const struct sr_option *structure,
70d3b20b 833 shared_ptr<const struct sr_option *> structure_array);
58aa1f83 834 ~Option();
70d3b20b 835 shared_ptr<const struct sr_option *> structure_array;
58aa1f83 836 friend class Deleter;
43942280 837 friend class InputFormat;
58aa1f83
ML
838 friend class OutputFormat;
839};
840
07443fd2 841/** An output format supported by the library */
c23c8659 842class SR_API OutputFormat :
541c855e 843 public ParentOwned<OutputFormat, Context, const struct sr_output_module>
c23c8659
ML
844{
845public:
846 /** Name of this output format. */
847 string get_name();
848 /** Description of this output format. */
849 string get_description();
58aa1f83
ML
850 /** Options supported by this output format. */
851 map<string, shared_ptr<Option> > get_options();
b6f411ac
ML
852 /** Create an output using this format.
853 * @param device Device to output for.
854 * @param options Mapping of (option name, value) pairs. */
58aa1f83
ML
855 shared_ptr<Output> create_output(shared_ptr<Device> device,
856 map<string, Glib::VariantBase> options = {});
c23c8659 857protected:
58aa1f83 858 OutputFormat(const struct sr_output_module *structure);
c23c8659
ML
859 ~OutputFormat();
860 friend class Context;
861 friend class Output;
862};
863
07443fd2 864/** An output instance (an output format applied to a device) */
90e89c2a 865class SR_API Output : public UserOwned<Output, const struct sr_output>
c23c8659
ML
866{
867public:
b6f411ac
ML
868 /** Update output with data from the given packet.
869 * @param packet Packet to handle. */
c23c8659
ML
870 string receive(shared_ptr<Packet> packet);
871protected:
872 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
873 Output(shared_ptr<OutputFormat> format,
58aa1f83 874 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
c23c8659 875 ~Output();
c23c8659
ML
876 const shared_ptr<OutputFormat> format;
877 const shared_ptr<Device> device;
58aa1f83 878 const map<string, Glib::VariantBase> options;
c23c8659
ML
879 friend class Deleter;
880 friend class OutputFormat;
881};
882
883/** Base class for objects which wrap an enumeration value from libsigrok */
884template <typename T> class SR_API EnumValue
885{
886public:
887 /** The enum constant associated with this value. */
888 T get_id() const { return id; }
889 /** The name associated with this value. */
890 string get_name() const { return name; }
891protected:
892 EnumValue(T id, const char name[]) : id(id), name(name) {}
893 ~EnumValue() {}
894 const T id;
895 const string name;
896};
897
898#include "enums.hpp"
899
900}
901
902#endif // LIBSIGROK_HPP