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