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