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