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