]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/include/libsigrok/libsigrok.hpp
Mention the Building#FAQ wiki page for common issues.
[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>
4c7c4194 80#include <set>
c23c8659
ML
81
82namespace sigrok
83{
84
85using namespace std;
86
87/* Forward declarations */
88class SR_API Error;
89class SR_API Context;
90class SR_API Driver;
91class SR_API Device;
92class SR_API HardwareDevice;
93class SR_API Channel;
94class SR_API EventSource;
95class SR_API Session;
96class SR_API ConfigKey;
97class SR_API InputFormat;
98class SR_API OutputFormat;
99class SR_API LogLevel;
100class SR_API ChannelGroup;
101class SR_API Trigger;
102class SR_API TriggerStage;
103class SR_API TriggerMatch;
104class SR_API TriggerMatchType;
105class SR_API ChannelType;
106class SR_API Packet;
107class SR_API PacketPayload;
108class SR_API PacketType;
109class SR_API Quantity;
110class SR_API Unit;
111class SR_API QuantityFlag;
ca3291e3
ML
112class SR_API Input;
113class SR_API InputDevice;
c23c8659
ML
114class SR_API Output;
115class SR_API DataType;
58aa1f83 116class SR_API Option;
9fa5b426 117class SR_API UserDevice;
c23c8659
ML
118
119/** Exception thrown when an error code is returned by any libsigrok call. */
120class SR_API Error: public exception
121{
122public:
123 Error(int result);
124 ~Error() throw();
125 const int result;
126 const char *what() const throw();
127};
128
541c855e 129/* Base template for classes whose resources are owned by a parent object. */
bf52cc8c 130template <class Class, class Parent, typename Struct>
541c855e 131class SR_API ParentOwned
c23c8659 132{
7649683c 133protected:
07443fd2 134 /* Parent object which owns this child object's underlying structure.
c23c8659
ML
135
136 This shared pointer will be null when this child is unused, but
137 will be assigned to point to the parent before any shared pointer
138 to this child is handed out to the user.
139
140 When the reference count of this child falls to zero, this shared
141 pointer to its parent is reset by a custom deleter on the child's
142 shared pointer.
143
144 This strategy ensures that the destructors for both the child and
145 the parent are called at the correct time, i.e. only when all
146 references to both the parent and all its children are gone. */
3b161085 147 shared_ptr<Parent> _parent;
7649683c 148
0d0170ae 149 /* Weak pointer for shared_from_this() implementation. */
3b161085 150 weak_ptr<Class> _weak_this;
0d0170ae 151
7649683c 152public:
73a1eb01
ML
153 /* Get parent object that owns this object. */
154 shared_ptr<Parent> parent()
155 {
156 return _parent;
157 }
158
0d0170ae 159 /* Note, this implementation will create a new smart_ptr if none exists. */
bf52cc8c 160 shared_ptr<Class> shared_from_this()
0d0170ae 161 {
bf52cc8c 162 shared_ptr<Class> shared;
0d0170ae 163
3b161085 164 if (!(shared = _weak_this.lock()))
0d0170ae 165 {
bf52cc8c 166 shared = shared_ptr<Class>((Class *) this, reset_parent);
3b161085 167 _weak_this = shared;
0d0170ae
ML
168 }
169
170 return shared;
171 }
172
bf52cc8c 173 shared_ptr<Class> get_shared_pointer(shared_ptr<Parent> parent)
7649683c 174 {
78132e2a
ML
175 if (!parent)
176 throw Error(SR_ERR_BUG);
3b161085 177 this->_parent = parent;
0d0170ae 178 return shared_from_this();
7649683c 179 }
0d0170ae 180
bf52cc8c 181 shared_ptr<Class> get_shared_pointer(Parent *parent)
7649683c 182 {
78132e2a
ML
183 if (!parent)
184 throw Error(SR_ERR_BUG);
bf52cc8c 185 return get_shared_pointer(parent->shared_from_this());
7649683c 186 }
c23c8659 187protected:
bf52cc8c 188 static void reset_parent(Class *object)
7649683c 189 {
3b161085 190 if (!object->_parent)
78132e2a 191 throw Error(SR_ERR_BUG);
3b161085 192 object->_parent.reset();
7649683c
ML
193 }
194
3b161085 195 Struct *_structure;
c23c8659 196
541c855e 197 ParentOwned<Class, Parent, Struct>(Struct *structure) :
3b161085 198 _structure(structure)
c23c8659
ML
199 {
200 }
201};
202
90e89c2a
ML
203/* Base template for classes whose resources are owned by the user. */
204template <class Class, typename Struct>
205class SR_API UserOwned : public enable_shared_from_this<Class>
206{
207public:
208 shared_ptr<Class> shared_from_this()
209 {
210 auto shared = enable_shared_from_this<Class>::shared_from_this();
211 if (!shared)
212 throw Error(SR_ERR_BUG);
213 return shared;
214 }
215protected:
3b161085 216 Struct *_structure;
90e89c2a
ML
217
218 UserOwned<Class, Struct>(Struct *structure) :
3b161085 219 _structure(structure)
90e89c2a
ML
220 {
221 }
b4ed33a7
ML
222
223 /* Deleter needed to allow shared_ptr use with protected destructor. */
224 class Deleter
225 {
226 public:
227 void operator()(Class *object) { delete object; }
228 };
90e89c2a
ML
229};
230
c23c8659
ML
231/** Type of log callback */
232typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
233
07443fd2 234/** The global libsigrok context */
90e89c2a 235class SR_API Context : public UserOwned<Context, struct sr_context>
c23c8659
ML
236{
237public:
238 /** Create new context */
239 static shared_ptr<Context> create();
240 /** libsigrok package version. */
3b161085 241 string package_version();
c23c8659 242 /** libsigrok library version. */
3b161085 243 string lib_version();
c23c8659 244 /** Available hardware drivers, indexed by name. */
3b161085 245 map<string, shared_ptr<Driver> > drivers();
c23c8659 246 /** Available input formats, indexed by name. */
3b161085 247 map<string, shared_ptr<InputFormat> > input_formats();
c23c8659 248 /** Available output formats, indexed by name. */
3b161085 249 map<string, shared_ptr<OutputFormat> > output_formats();
c23c8659 250 /** Current log level. */
3b161085 251 const LogLevel *log_level();
b6f411ac
ML
252 /** Set the log level.
253 * @param level LogLevel to use. */
c23c8659
ML
254 void set_log_level(const LogLevel *level);
255 /** Current log domain. */
3b161085 256 string log_domain();
b6f411ac
ML
257 /** Set the log domain.
258 * @param value Log domain prefix string. */
c23c8659 259 void set_log_domain(string value);
b6f411ac
ML
260 /** Set the log callback.
261 * @param callback Callback of the form callback(LogLevel, string). */
c23c8659
ML
262 void set_log_callback(LogCallbackFunction callback);
263 /** Set the log callback to the default handler. */
264 void set_log_callback_default();
265 /** Create a new session. */
266 shared_ptr<Session> create_session();
9fa5b426
ML
267 /** Create a new user device. */
268 shared_ptr<UserDevice> create_user_device(
269 string vendor, string model, string version);
304be4a7
ML
270 /** Create a header packet. */
271 shared_ptr<Packet> create_header_packet(Glib::TimeVal start_time);
272 /** Create a meta packet. */
273 shared_ptr<Packet> create_meta_packet(
274 map<const ConfigKey *, Glib::VariantBase> config);
275 /** Create a logic packet. */
276 shared_ptr<Packet> create_logic_packet(
277 void *data_pointer, size_t data_length, unsigned int unit_size);
278 /** Create an analog packet. */
279 shared_ptr<Packet> create_analog_packet(
280 vector<shared_ptr<Channel> > channels,
281 float *data_pointer, unsigned int num_samples, const Quantity *mq,
282 const Unit *unit, vector<const QuantityFlag *> mqflags);
b6f411ac
ML
283 /** Load a saved session.
284 * @param filename File name string. */
c23c8659 285 shared_ptr<Session> load_session(string filename);
b6f411ac
ML
286 /** Create a new trigger.
287 * @param name Name string for new trigger. */
c23c8659 288 shared_ptr<Trigger> create_trigger(string name);
ca3291e3
ML
289 /** Open an input file.
290 * @param filename File name string. */
291 shared_ptr<Input> open_file(string filename);
292 /** Open an input stream based on header data.
293 * @param header Initial data from stream. */
294 shared_ptr<Input> open_stream(string header);
c23c8659 295protected:
3b161085
ML
296 map<string, Driver *> _drivers;
297 map<string, InputFormat *> _input_formats;
298 map<string, OutputFormat *> _output_formats;
299 Session *_session;
300 LogCallbackFunction _log_callback;
c23c8659
ML
301 Context();
302 ~Context();
c23c8659
ML
303 friend class Deleter;
304 friend class Session;
305 friend class Driver;
306};
307
4c7c4194
ML
308enum Capability {
309 GET = SR_CONF_GET,
310 SET = SR_CONF_SET,
311 LIST = SR_CONF_LIST
312};
313
c23c8659
ML
314/** An object that can be configured. */
315class SR_API Configurable
316{
317public:
b6f411ac
ML
318 /** Read configuration for the given key.
319 * @param key ConfigKey to read. */
c23c8659 320 Glib::VariantBase config_get(const ConfigKey *key);
b6f411ac
ML
321 /** Set configuration for the given key to a specified value.
322 * @param key ConfigKey to set.
323 * @param value Value to set. */
c23c8659 324 void config_set(const ConfigKey *key, Glib::VariantBase value);
b6f411ac
ML
325 /** Enumerate available values for the given configuration key.
326 * @param key ConfigKey to enumerate values for. */
e194c011 327 Glib::VariantContainerBase config_list(const ConfigKey *key);
d54190a3 328 /** Enumerate available keys, according to a given index key. */
4c7c4194 329 map<const ConfigKey *, set<Capability> > config_keys(const ConfigKey *key);
d9eed47d
ML
330 /** Check for a key in the list from a given index key. */
331 bool config_check(const ConfigKey *key, const ConfigKey *index_key);
c23c8659
ML
332protected:
333 Configurable(
334 struct sr_dev_driver *driver,
335 struct sr_dev_inst *sdi,
336 struct sr_channel_group *channel_group);
337 virtual ~Configurable();
338 struct sr_dev_driver *config_driver;
339 struct sr_dev_inst *config_sdi;
340 struct sr_channel_group *config_channel_group;
341};
342
59b74d28
ML
343/** A hardware driver provided by the library */
344class SR_API Driver :
345 public ParentOwned<Driver, Context, struct sr_dev_driver>,
346 public Configurable
347{
348public:
349 /** Name of this driver. */
3b161085 350 string name();
59b74d28 351 /** Long name for this driver. */
3b161085 352 string long_name();
59b74d28
ML
353 /** Scan for devices and return a list of devices found.
354 * @param options Mapping of (ConfigKey, value) pairs. */
355 vector<shared_ptr<HardwareDevice> > scan(
db560903
ML
356 map<const ConfigKey *, Glib::VariantBase> options =
357 map<const ConfigKey *, Glib::VariantBase>());
59b74d28 358protected:
3b161085
ML
359 bool _initialized;
360 vector<HardwareDevice *> _devices;
59b74d28
ML
361 Driver(struct sr_dev_driver *structure);
362 ~Driver();
363 friend class Context;
364 friend class HardwareDevice;
365 friend class ChannelGroup;
366};
367
07443fd2 368/** A generic device, either hardware or virtual */
d01d2314 369class SR_API Device : public Configurable
c23c8659
ML
370{
371public:
372 /** Vendor name for this device. */
3b161085 373 string vendor();
c23c8659 374 /** Model name for this device. */
3b161085 375 string model();
c23c8659 376 /** Version string for this device. */
3b161085 377 string version();
d1075e5a
ML
378 /** Serial number for this device. */
379 string serial_number();
380 /** Connection ID for this device. */
381 string connection_id();
c23c8659 382 /** List of the channels available on this device. */
3b161085 383 vector<shared_ptr<Channel> > channels();
6be7a7f2 384 /** Channel groups available on this device, indexed by name. */
3b161085 385 map<string, shared_ptr<ChannelGroup> > channel_groups();
c23c8659
ML
386 /** Open device. */
387 void open();
388 /** Close device. */
389 void close();
390protected:
391 Device(struct sr_dev_inst *structure);
392 ~Device();
d01d2314 393 virtual shared_ptr<Device> get_shared_from_this() = 0;
4178d971 394 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
3b161085
ML
395 struct sr_dev_inst *_structure;
396 map<struct sr_channel *, Channel *> _channels;
397 map<string, ChannelGroup *> _channel_groups;
c23c8659
ML
398 /** Deleter needed to allow shared_ptr use with protected destructor. */
399 class Deleter
400 {
401 public:
402 void operator()(Device *device) { delete device; }
403 };
404 friend class Deleter;
405 friend class Session;
406 friend class Channel;
407 friend class ChannelGroup;
408 friend class Output;
2928f47d 409 friend class Analog;
c23c8659
ML
410};
411
07443fd2 412/** A real hardware device, connected via a driver */
6e5240f4 413class SR_API HardwareDevice :
a4e47454 414 public UserOwned<HardwareDevice, struct sr_dev_inst>,
6e5240f4 415 public Device
c23c8659
ML
416{
417public:
418 /** Driver providing this device. */
3b161085 419 shared_ptr<Driver> driver();
c23c8659 420protected:
a4e47454 421 HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
c23c8659 422 ~HardwareDevice();
d01d2314 423 shared_ptr<Device> get_shared_from_this();
a4e47454
ML
424 shared_ptr<Driver> _driver;
425 /** Deleter needed to allow shared_ptr use with protected destructor. */
426 class Deleter
427 {
428 public:
429 void operator()(HardwareDevice *device) { delete device; }
430 };
431 friend class Deleter;
c23c8659
ML
432 friend class Driver;
433 friend class ChannelGroup;
434};
435
9fa5b426
ML
436/** A virtual device, created by the user */
437class SR_API UserDevice :
438 public UserOwned<UserDevice, struct sr_dev_inst>,
439 public Device
440{
441public:
442 /** Add a new channel to this device. */
443 shared_ptr<Channel> add_channel(unsigned int index, const ChannelType *type, string name);
444protected:
445 UserDevice(string vendor, string model, string version);
446 ~UserDevice();
447 shared_ptr<Device> get_shared_from_this();
448 /** Deleter needed to allow shared_ptr use with protected destructor. */
449 class Deleter
450 {
451 public:
452 void operator()(UserDevice *device) { delete device; }
453 };
454 friend class Context;
455 friend class Deleter;
456};
457
07443fd2 458/** A channel on a device */
bf52cc8c 459class SR_API Channel :
541c855e 460 public ParentOwned<Channel, Device, struct sr_channel>
c23c8659
ML
461{
462public:
463 /** Current name of this channel. */
3b161085 464 string name();
b6f411ac
ML
465 /** Set the name of this channel. *
466 * @param name Name string to set. */
c23c8659
ML
467 void set_name(string name);
468 /** Type of this channel. */
3b161085 469 const ChannelType *type();
c23c8659 470 /** Enabled status of this channel. */
3b161085 471 bool enabled();
b6f411ac
ML
472 /** Set the enabled status of this channel.
473 * @param value Boolean value to set. */
c23c8659 474 void set_enabled(bool value);
06bd935e 475 /** Get the index number of this channel. */
3b161085 476 unsigned int index();
c23c8659
ML
477protected:
478 Channel(struct sr_channel *structure);
479 ~Channel();
3b161085 480 const ChannelType * const _type;
c23c8659 481 friend class Device;
9fa5b426 482 friend class UserDevice;
c23c8659
ML
483 friend class ChannelGroup;
484 friend class Session;
485 friend class TriggerStage;
304be4a7 486 friend class Context;
c23c8659
ML
487};
488
07443fd2 489/** A group of channels on a device, which share some configuration */
c23c8659 490class SR_API ChannelGroup :
541c855e 491 public ParentOwned<ChannelGroup, Device, struct sr_channel_group>,
c23c8659
ML
492 public Configurable
493{
494public:
495 /** Name of this channel group. */
3b161085 496 string name();
c23c8659 497 /** List of the channels in this group. */
3b161085 498 vector<shared_ptr<Channel> > channels();
c23c8659 499protected:
6be7a7f2 500 ChannelGroup(Device *device, struct sr_channel_group *structure);
c23c8659 501 ~ChannelGroup();
3b161085 502 vector<Channel *> _channels;
6be7a7f2 503 friend class Device;
c23c8659
ML
504};
505
07443fd2 506/** A trigger configuration */
90e89c2a 507class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
c23c8659
ML
508{
509public:
b6f411ac 510 /** Name of this trigger configuration. */
3b161085 511 string name();
b6f411ac 512 /** List of the stages in this trigger. */
3b161085 513 vector<shared_ptr<TriggerStage> > stages();
b6f411ac 514 /** Add a new stage to this trigger. */
c23c8659
ML
515 shared_ptr<TriggerStage> add_stage();
516protected:
517 Trigger(shared_ptr<Context> context, string name);
518 ~Trigger();
3b161085
ML
519 shared_ptr<Context> _context;
520 vector<TriggerStage *> _stages;
b4ed33a7 521 friend class Deleter;
c23c8659 522 friend class Context;
6fa0eb86 523 friend class Session;
c23c8659
ML
524};
525
07443fd2 526/** A stage in a trigger configuration */
bf52cc8c 527class SR_API TriggerStage :
541c855e 528 public ParentOwned<TriggerStage, Trigger, struct sr_trigger_stage>
c23c8659
ML
529{
530public:
b6f411ac 531 /** Index number of this stage. */
3b161085 532 int number();
b6f411ac 533 /** List of match conditions on this stage. */
3b161085 534 vector<shared_ptr<TriggerMatch> > matches();
b6f411ac
ML
535 /** Add a new match condition to this stage.
536 * @param channel Channel to match on.
537 * @param type TriggerMatchType to apply. */
c23c8659 538 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
b6f411ac
ML
539 /** Add a new match condition to this stage.
540 * @param channel Channel to match on.
541 * @param type TriggerMatchType to apply.
542 * @param value Threshold value. */
c23c8659
ML
543 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
544protected:
3b161085 545 vector<TriggerMatch *> _matches;
c23c8659
ML
546 TriggerStage(struct sr_trigger_stage *structure);
547 ~TriggerStage();
548 friend class Trigger;
549};
550
07443fd2 551/** A match condition in a trigger configuration */
bf52cc8c 552class SR_API TriggerMatch :
541c855e 553 public ParentOwned<TriggerMatch, TriggerStage, struct sr_trigger_match>
c23c8659
ML
554{
555public:
b6f411ac 556 /** Channel this condition matches on. */
3b161085 557 shared_ptr<Channel> channel();
b6f411ac 558 /** Type of match. */
3b161085 559 const TriggerMatchType *type();
b6f411ac 560 /** Threshold value. */
3b161085 561 float value();
c23c8659
ML
562protected:
563 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
564 ~TriggerMatch();
3b161085 565 shared_ptr<Channel> _channel;
c23c8659
ML
566 friend class TriggerStage;
567};
568
569/** Type of datafeed callback */
570typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
571 DatafeedCallbackFunction;
572
07443fd2 573/* Data required for C callback function to call a C++ datafeed callback */
c23c8659
ML
574class SR_PRIV DatafeedCallbackData
575{
576public:
577 void run(const struct sr_dev_inst *sdi,
578 const struct sr_datafeed_packet *pkt);
579protected:
3b161085 580 DatafeedCallbackFunction _callback;
c23c8659
ML
581 DatafeedCallbackData(Session *session,
582 DatafeedCallbackFunction callback);
3b161085 583 Session *_session;
c23c8659
ML
584 friend class Session;
585};
586
587/** Type of source callback */
588typedef function<bool(Glib::IOCondition)>
589 SourceCallbackFunction;
590
07443fd2 591/* Data required for C callback function to call a C++ source callback */
c23c8659
ML
592class SR_PRIV SourceCallbackData
593{
594public:
595 bool run(int revents);
596protected:
597 SourceCallbackData(shared_ptr<EventSource> source);
3b161085 598 shared_ptr<EventSource> _source;
c23c8659
ML
599 friend class Session;
600};
601
07443fd2 602/** An I/O event source */
c23c8659
ML
603class SR_API EventSource
604{
605public:
b6f411ac
ML
606 /** Create an event source from a file descriptor.
607 * @param fd File descriptor.
608 * @param events GLib IOCondition event mask.
609 * @param timeout Timeout in milliseconds.
610 * @param callback Callback of the form callback(events) */
c23c8659
ML
611 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
612 int timeout, SourceCallbackFunction callback);
b6f411ac
ML
613 /** Create an event source from a GLib PollFD
614 * @param pollfd GLib PollFD
615 * @param timeout Timeout in milliseconds.
616 * @param callback Callback of the form callback(events) */
c23c8659
ML
617 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
618 SourceCallbackFunction callback);
b6f411ac
ML
619 /** Create an event source from a GLib IOChannel
620 * @param channel GLib IOChannel.
621 * @param events GLib IOCondition event mask.
622 * @param timeout Timeout in milliseconds.
623 * @param callback Callback of the form callback(events) */
c23c8659
ML
624 static shared_ptr<EventSource> create(
625 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
626 int timeout, SourceCallbackFunction callback);
627protected:
628 EventSource(int timeout, SourceCallbackFunction callback);
629 ~EventSource();
630 enum source_type {
631 SOURCE_FD,
632 SOURCE_POLLFD,
633 SOURCE_IOCHANNEL
3b161085
ML
634 } _type;
635 int _fd;
636 Glib::PollFD _pollfd;
637 Glib::RefPtr<Glib::IOChannel> _channel;
638 Glib::IOCondition _events;
639 int _timeout;
640 SourceCallbackFunction _callback;
c23c8659
ML
641 /** Deleter needed to allow shared_ptr use with protected destructor. */
642 class Deleter
643 {
644 public:
645 void operator()(EventSource *source) { delete source; }
646 };
647 friend class Deleter;
648 friend class Session;
649 friend class SourceCallbackData;
650};
651
cac58676
ML
652/** A virtual device associated with a stored session */
653class SR_API SessionDevice :
654 public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
655 public Device
656{
657protected:
658 SessionDevice(struct sr_dev_inst *sdi);
659 ~SessionDevice();
660 shared_ptr<Device> get_shared_from_this();
661 /** Deleter needed to allow shared_ptr use with protected destructor. */
662 class Deleter
663 {
664 public:
665 void operator()(SessionDevice *device) { delete device; }
666 };
667 friend class Deleter;
668 friend class Session;
669};
670
07443fd2 671/** A sigrok session */
90e89c2a 672class SR_API Session : public UserOwned<Session, struct sr_session>
c23c8659
ML
673{
674public:
b6f411ac
ML
675 /** Add a device to this session.
676 * @param device Device to add. */
c23c8659
ML
677 void add_device(shared_ptr<Device> device);
678 /** List devices attached to this session. */
3b161085 679 vector<shared_ptr<Device> > devices();
c23c8659
ML
680 /** Remove all devices from this session. */
681 void remove_devices();
b6f411ac
ML
682 /** Add a datafeed callback to this session.
683 * @param callback Callback of the form callback(Device, Packet). */
c23c8659
ML
684 void add_datafeed_callback(DatafeedCallbackFunction callback);
685 /** Remove all datafeed callbacks from this session. */
686 void remove_datafeed_callbacks();
b6f411ac
ML
687 /** Add an I/O event source.
688 * @param source EventSource to add. */
c23c8659 689 void add_source(shared_ptr<EventSource> source);
b6f411ac
ML
690 /** Remove an event source.
691 * @param source EventSource to remove. */
c23c8659
ML
692 void remove_source(shared_ptr<EventSource> source);
693 /** Start the session. */
694 void start();
695 /** Run the session event loop. */
696 void run();
697 /** Stop the session. */
698 void stop();
b6f411ac
ML
699 /** Begin saving session to a file.
700 * @param filename File name string. */
c23c8659 701 void begin_save(string filename);
b6f411ac
ML
702 /** Append a packet to the session file being saved.
703 * @param packet Packet to append. */
1d67cfb4
ML
704 void append(shared_ptr<Packet> packet);
705 /** Append raw logic data to the session file being saved. */
706 void append(void *data, size_t length, unsigned int unit_size);
6fa0eb86 707 /** Get current trigger setting. */
3b161085 708 shared_ptr<Trigger> trigger();
624d1610
UH
709 /** Get the context. */
710 shared_ptr<Context> context();
b6f411ac
ML
711 /** Set trigger setting.
712 * @param trigger Trigger object to use. */
6fa0eb86 713 void set_trigger(shared_ptr<Trigger> trigger);
1411f7d8
ML
714 /** Get filename this session was loaded from. */
715 string filename();
c23c8659
ML
716protected:
717 Session(shared_ptr<Context> context);
718 Session(shared_ptr<Context> context, string filename);
719 ~Session();
ca4e307a 720 shared_ptr<Device> get_device(const struct sr_dev_inst *sdi);
3b161085 721 const shared_ptr<Context> _context;
ca4e307a
ML
722 map<const struct sr_dev_inst *, SessionDevice *> _owned_devices;
723 map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
3b161085
ML
724 vector<DatafeedCallbackData *> _datafeed_callbacks;
725 map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
98d39b91 726 string _filename;
3b161085
ML
727 bool _saving;
728 bool _save_initialized;
729 string _save_filename;
730 uint64_t _save_samplerate;
731 shared_ptr<Trigger> _trigger;
c23c8659
ML
732 friend class Deleter;
733 friend class Context;
734 friend class DatafeedCallbackData;
98d39b91 735 friend class SessionDevice;
c23c8659
ML
736};
737
07443fd2 738/** A packet on the session datafeed */
90e89c2a 739class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
c23c8659
ML
740{
741public:
90ba83f2 742 /** Type of this packet. */
3b161085 743 const PacketType *type();
c23c8659 744 /** Payload of this packet. */
3b161085 745 shared_ptr<PacketPayload> payload();
c23c8659 746protected:
2928f47d
ML
747 Packet(shared_ptr<Device> device,
748 const struct sr_datafeed_packet *structure);
c23c8659 749 ~Packet();
3b161085
ML
750 shared_ptr<Device> _device;
751 PacketPayload *_payload;
c23c8659
ML
752 friend class Deleter;
753 friend class Session;
754 friend class Output;
755 friend class DatafeedCallbackData;
2928f47d
ML
756 friend class Header;
757 friend class Meta;
758 friend class Logic;
759 friend class Analog;
304be4a7 760 friend class Context;
c23c8659
ML
761};
762
07443fd2 763/** Abstract base class for datafeed packet payloads */
c23c8659
ML
764class SR_API PacketPayload
765{
766protected:
767 PacketPayload();
768 virtual ~PacketPayload() = 0;
4cd883a7 769 virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
2928f47d
ML
770 /** Deleter needed to allow shared_ptr use with protected destructor. */
771 class Deleter
772 {
773 public:
774 void operator()(PacketPayload *payload) { delete payload; }
775 };
776 friend class Deleter;
c23c8659
ML
777 friend class Packet;
778 friend class Output;
779};
780
2928f47d 781/** Payload of a datafeed header packet */
4cd883a7 782class SR_API Header :
541c855e 783 public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
4cd883a7 784 public PacketPayload
2928f47d
ML
785{
786public:
b6f411ac 787 /* Feed version number. */
3b161085 788 int feed_version();
b6f411ac 789 /* Start time of this session. */
3b161085 790 Glib::TimeVal start_time();
2928f47d
ML
791protected:
792 Header(const struct sr_datafeed_header *structure);
793 ~Header();
4cd883a7 794 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
2928f47d
ML
795 friend class Packet;
796};
797
798/** Payload of a datafeed metadata packet */
4cd883a7 799class SR_API Meta :
541c855e 800 public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
4cd883a7 801 public PacketPayload
2928f47d
ML
802{
803public:
b6f411ac 804 /* Mapping of (ConfigKey, value) pairs. */
3b161085 805 map<const ConfigKey *, Glib::VariantBase> config();
2928f47d
ML
806protected:
807 Meta(const struct sr_datafeed_meta *structure);
808 ~Meta();
4cd883a7 809 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
3b161085 810 map<const ConfigKey *, Glib::VariantBase> _config;
2928f47d
ML
811 friend class Packet;
812};
813
07443fd2 814/** Payload of a datafeed packet with logic data */
4cd883a7 815class SR_API Logic :
541c855e 816 public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
4cd883a7 817 public PacketPayload
c23c8659 818{
2928f47d
ML
819public:
820 /* Pointer to data. */
3b161085 821 void *data_pointer();
2928f47d 822 /* Data length in bytes. */
3b161085 823 size_t data_length();
2928f47d 824 /* Size of each sample in bytes. */
3b161085 825 unsigned int unit_size();
c23c8659
ML
826protected:
827 Logic(const struct sr_datafeed_logic *structure);
828 ~Logic();
4cd883a7 829 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
c23c8659
ML
830 friend class Packet;
831};
832
07443fd2 833/** Payload of a datafeed packet with analog data */
4cd883a7 834class SR_API Analog :
541c855e 835 public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
4cd883a7 836 public PacketPayload
c23c8659
ML
837{
838public:
2928f47d 839 /** Pointer to data. */
3b161085 840 float *data_pointer();
c23c8659 841 /** Number of samples in this packet. */
3b161085 842 unsigned int num_samples();
2928f47d 843 /** Channels for which this packet contains data. */
3b161085 844 vector<shared_ptr<Channel> > channels();
c23c8659 845 /** Measured quantity of the samples in this packet. */
3b161085 846 const Quantity *mq();
c23c8659 847 /** Unit of the samples in this packet. */
3b161085 848 const Unit *unit();
c23c8659 849 /** Measurement flags associated with the samples in this packet. */
3b161085 850 vector<const QuantityFlag *> mq_flags();
c23c8659
ML
851protected:
852 Analog(const struct sr_datafeed_analog *structure);
853 ~Analog();
4cd883a7 854 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
c23c8659
ML
855 friend class Packet;
856};
857
07443fd2 858/** An input format supported by the library */
c23c8659 859class SR_API InputFormat :
541c855e 860 public ParentOwned<InputFormat, Context, const struct sr_input_module>
c23c8659
ML
861{
862public:
863 /** Name of this input format. */
3b161085 864 string name();
c23c8659 865 /** Description of this input format. */
3b161085 866 string description();
ca3291e3 867 /** Options supported by this input format. */
3b161085 868 map<string, shared_ptr<Option> > options();
ca3291e3
ML
869 /** Create an input using this input format.
870 * @param options Mapping of (option name, value) pairs. */
db560903
ML
871 shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
872 map<string, Glib::VariantBase>());
c23c8659 873protected:
ca3291e3 874 InputFormat(const struct sr_input_module *structure);
c23c8659
ML
875 ~InputFormat();
876 friend class Context;
ca3291e3 877 friend class InputDevice;
c23c8659
ML
878};
879
ca3291e3 880/** An input instance (an input format applied to a file or stream) */
90e89c2a 881class SR_API Input : public UserOwned<Input, const struct sr_input>
c23c8659
ML
882{
883public:
ca3291e3 884 /** Virtual device associated with this input. */
3b161085 885 shared_ptr<InputDevice> device();
ca3291e3
ML
886 /** Send next stream data.
887 * @param data Next stream data. */
888 void send(string data);
9c51e8ec
ML
889 /** Signal end of input data. */
890 void end();
c23c8659 891protected:
ca3291e3
ML
892 Input(shared_ptr<Context> context, const struct sr_input *structure);
893 ~Input();
3b161085
ML
894 shared_ptr<Context> _context;
895 InputDevice *_device;
c23c8659 896 friend class Deleter;
ca3291e3 897 friend class Context;
c23c8659
ML
898 friend class InputFormat;
899};
900
ca3291e3 901/** A virtual device associated with an input */
6e5240f4 902class SR_API InputDevice :
541c855e 903 public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
6e5240f4 904 public Device
ca3291e3
ML
905{
906protected:
907 InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
908 ~InputDevice();
d01d2314 909 shared_ptr<Device> get_shared_from_this();
3b161085 910 shared_ptr<Input> _input;
ca3291e3
ML
911 friend class Input;
912};
913
58aa1f83 914/** An option used by an output format */
90e89c2a 915class SR_API Option : public UserOwned<Option, const struct sr_option>
58aa1f83
ML
916{
917public:
918 /** Short name of this option suitable for command line usage. */
3b161085 919 string id();
58aa1f83 920 /** Short name of this option suitable for GUI usage. */
3b161085 921 string name();
58aa1f83 922 /** Description of this option in a sentence. */
3b161085 923 string description();
58aa1f83 924 /** Default value for this option. */
3b161085 925 Glib::VariantBase default_value();
58aa1f83 926 /** Possible values for this option, if a limited set. */
3b161085 927 vector<Glib::VariantBase> values();
58aa1f83
ML
928protected:
929 Option(const struct sr_option *structure,
70d3b20b 930 shared_ptr<const struct sr_option *> structure_array);
58aa1f83 931 ~Option();
3b161085 932 shared_ptr<const struct sr_option *> _structure_array;
58aa1f83 933 friend class Deleter;
43942280 934 friend class InputFormat;
58aa1f83
ML
935 friend class OutputFormat;
936};
937
07443fd2 938/** An output format supported by the library */
c23c8659 939class SR_API OutputFormat :
541c855e 940 public ParentOwned<OutputFormat, Context, const struct sr_output_module>
c23c8659
ML
941{
942public:
943 /** Name of this output format. */
3b161085 944 string name();
c23c8659 945 /** Description of this output format. */
3b161085 946 string description();
58aa1f83 947 /** Options supported by this output format. */
3b161085 948 map<string, shared_ptr<Option> > options();
b6f411ac
ML
949 /** Create an output using this format.
950 * @param device Device to output for.
951 * @param options Mapping of (option name, value) pairs. */
58aa1f83 952 shared_ptr<Output> create_output(shared_ptr<Device> device,
db560903
ML
953 map<string, Glib::VariantBase> options =
954 map<string, Glib::VariantBase>());
c23c8659 955protected:
58aa1f83 956 OutputFormat(const struct sr_output_module *structure);
c23c8659
ML
957 ~OutputFormat();
958 friend class Context;
959 friend class Output;
960};
961
07443fd2 962/** An output instance (an output format applied to a device) */
90e89c2a 963class SR_API Output : public UserOwned<Output, const struct sr_output>
c23c8659
ML
964{
965public:
b6f411ac
ML
966 /** Update output with data from the given packet.
967 * @param packet Packet to handle. */
c23c8659
ML
968 string receive(shared_ptr<Packet> packet);
969protected:
970 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
971 Output(shared_ptr<OutputFormat> format,
58aa1f83 972 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
c23c8659 973 ~Output();
3b161085
ML
974 const shared_ptr<OutputFormat> _format;
975 const shared_ptr<Device> _device;
976 const map<string, Glib::VariantBase> _options;
c23c8659
ML
977 friend class Deleter;
978 friend class OutputFormat;
979};
980
981/** Base class for objects which wrap an enumeration value from libsigrok */
9d229ecb 982template <class Class, typename Enum> class SR_API EnumValue
c23c8659
ML
983{
984public:
9d229ecb
ML
985 /** The integer constant associated with this value. */
986 int id() const
987 {
988 return static_cast<int>(_id);
989 }
c23c8659 990 /** The name associated with this value. */
9d229ecb
ML
991 string name() const
992 {
993 return _name;
994 }
995 /** Get value associated with a given integer constant. */
996 static const Class *get(int id)
997 {
998 auto key = static_cast<Enum>(id);
999 if (_values.find(key) == _values.end())
1000 throw Error(SR_ERR_ARG);
1001 return _values.at(key);
1002 }
1003 /** Get possible values. */
1004 static std::vector<const Class *> values()
1005 {
1006 std::vector<const Class *> result;
1007 for (auto entry : _values)
1008 result.push_back(entry.second);
1009 return result;
1010 }
c23c8659 1011protected:
9d229ecb
ML
1012 EnumValue(Enum id, const char name[]) : _id(id), _name(name)
1013 {
1014 }
1015 ~EnumValue()
1016 {
1017 }
1018 static const std::map<const Enum, const Class * const> _values;
1019 const Enum _id;
3b161085 1020 const string _name;
c23c8659
ML
1021};
1022
1023#include "enums.hpp"
1024
1025}
1026
1027#endif // LIBSIGROK_HPP