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