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