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