]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp
bindings: Wrap enum sr_configcap as Capability class.
[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>
cea8c312
DE
74
75/* Suppress warnings due to glibmm's use of std::auto_ptr<> in a public
76 * header file. To be removed once glibmm is fixed. */
77G_GNUC_BEGIN_IGNORE_DEPRECATIONS
e0e6aecc 78#include <glibmm.h>
cea8c312 79G_GNUC_END_IGNORE_DEPRECATIONS
c23c8659
ML
80
81#include <stdexcept>
82#include <memory>
83#include <vector>
84#include <map>
4c7c4194 85#include <set>
c23c8659
ML
86
87namespace sigrok
88{
89
90using namespace std;
91
92/* Forward declarations */
93class SR_API Error;
94class SR_API Context;
95class SR_API Driver;
96class SR_API Device;
97class SR_API HardwareDevice;
98class SR_API Channel;
c23c8659
ML
99class SR_API Session;
100class SR_API ConfigKey;
12f2f640 101class SR_API Capability;
c23c8659
ML
102class SR_API InputFormat;
103class SR_API OutputFormat;
3cd4b381 104class SR_API OutputFlag;
c23c8659
ML
105class SR_API LogLevel;
106class SR_API ChannelGroup;
107class SR_API Trigger;
108class SR_API TriggerStage;
109class SR_API TriggerMatch;
110class SR_API TriggerMatchType;
111class SR_API ChannelType;
112class SR_API Packet;
113class SR_API PacketPayload;
114class SR_API PacketType;
115class SR_API Quantity;
116class SR_API Unit;
117class SR_API QuantityFlag;
ca3291e3
ML
118class SR_API Input;
119class SR_API InputDevice;
c23c8659
ML
120class SR_API Output;
121class SR_API DataType;
58aa1f83 122class SR_API Option;
9fa5b426 123class SR_API UserDevice;
c23c8659
ML
124
125/** Exception thrown when an error code is returned by any libsigrok call. */
126class SR_API Error: public exception
127{
128public:
6c11b496 129 explicit Error(int result);
15914cdb 130 ~Error() noexcept;
c23c8659 131 const int result;
15914cdb 132 const char *what() const noexcept;
c23c8659
ML
133};
134
541c855e 135/* Base template for classes whose resources are owned by a parent object. */
d5d7b09e 136template <class Class, class Parent>
541c855e 137class SR_API ParentOwned
c23c8659 138{
21d1bec6
DE
139private:
140 /* Weak pointer for shared_from_this() implementation. */
141 weak_ptr<Class> _weak_this;
142
143 static void reset_parent(Class *object)
144 {
145 if (!object->_parent)
146 throw Error(SR_ERR_BUG);
147 object->_parent.reset();
148 }
149
7649683c 150protected:
07443fd2 151 /* Parent object which owns this child object's underlying structure.
c23c8659
ML
152
153 This shared pointer will be null when this child is unused, but
154 will be assigned to point to the parent before any shared pointer
155 to this child is handed out to the user.
156
157 When the reference count of this child falls to zero, this shared
158 pointer to its parent is reset by a custom deleter on the child's
159 shared pointer.
160
161 This strategy ensures that the destructors for both the child and
162 the parent are called at the correct time, i.e. only when all
163 references to both the parent and all its children are gone. */
3b161085 164 shared_ptr<Parent> _parent;
7649683c 165
d5d7b09e 166 ParentOwned() {}
0d0170ae 167
0d0170ae 168 /* Note, this implementation will create a new smart_ptr if none exists. */
bf52cc8c 169 shared_ptr<Class> shared_from_this()
0d0170ae 170 {
67b82fc9 171 shared_ptr<Class> shared = _weak_this.lock();
0d0170ae 172
67b82fc9 173 if (!shared)
0d0170ae 174 {
67b82fc9 175 shared.reset(static_cast<Class *>(this), &reset_parent);
3b161085 176 _weak_this = shared;
0d0170ae
ML
177 }
178
179 return shared;
180 }
181
b6ab954d 182 shared_ptr<Class> share_owned_by(shared_ptr<Parent> parent)
7649683c 183 {
78132e2a
ML
184 if (!parent)
185 throw Error(SR_ERR_BUG);
3b161085 186 this->_parent = parent;
0d0170ae 187 return shared_from_this();
7649683c 188 }
0d0170ae 189
67b82fc9
DE
190public:
191 /* Get parent object that owns this object. */
192 shared_ptr<Parent> parent()
7649683c 193 {
67b82fc9 194 return _parent;
7649683c 195 }
c23c8659
ML
196};
197
90e89c2a 198/* Base template for classes whose resources are owned by the user. */
d5d7b09e 199template <class Class>
90e89c2a
ML
200class SR_API UserOwned : public enable_shared_from_this<Class>
201{
90e89c2a 202protected:
d5d7b09e 203 UserOwned() {}
b4ed33a7 204
67b82fc9
DE
205 shared_ptr<Class> shared_from_this()
206 {
207 auto shared = enable_shared_from_this<Class>::shared_from_this();
208 if (!shared)
209 throw Error(SR_ERR_BUG);
210 return shared;
211 }
90e89c2a
ML
212};
213
c23c8659
ML
214/** Type of log callback */
215typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
216
e2eaf858
DE
217/** Resource reader delegate. */
218class SR_API ResourceReader
219{
220public:
221 ResourceReader() {}
222 virtual ~ResourceReader();
223private:
224 /** Resource open hook. */
225 virtual void open(struct sr_resource *res, string name) = 0;
226 /** Resource close hook. */
227 virtual void close(struct sr_resource *res) = 0;
228 /** Resource read hook. */
229 virtual size_t read(const struct sr_resource *res, void *buf, size_t count) = 0;
230
231 static SR_PRIV int open_callback(struct sr_resource *res,
0ab8e5d2 232 const char *name, void *cb_data) noexcept;
e2eaf858 233 static SR_PRIV int close_callback(struct sr_resource *res,
0ab8e5d2 234 void *cb_data) noexcept;
e2eaf858 235 static SR_PRIV ssize_t read_callback(const struct sr_resource *res,
0ab8e5d2 236 void *buf, size_t count, void *cb_data) noexcept;
e2eaf858
DE
237 friend class Context;
238};
239
07443fd2 240/** The global libsigrok context */
d5d7b09e 241class SR_API Context : public UserOwned<Context>
c23c8659
ML
242{
243public:
244 /** Create new context */
245 static shared_ptr<Context> create();
246 /** libsigrok package version. */
a73d4926 247 static string package_version();
c23c8659 248 /** libsigrok library version. */
a73d4926 249 static string lib_version();
c23c8659 250 /** Available hardware drivers, indexed by name. */
3b161085 251 map<string, shared_ptr<Driver> > drivers();
c23c8659 252 /** Available input formats, indexed by name. */
3b161085 253 map<string, shared_ptr<InputFormat> > input_formats();
c23c8659 254 /** Available output formats, indexed by name. */
3b161085 255 map<string, shared_ptr<OutputFormat> > output_formats();
c23c8659 256 /** Current log level. */
15bebf57 257 const LogLevel *log_level() const;
b6f411ac
ML
258 /** Set the log level.
259 * @param level LogLevel to use. */
c23c8659 260 void set_log_level(const LogLevel *level);
b6f411ac
ML
261 /** Set the log callback.
262 * @param callback Callback of the form callback(LogLevel, string). */
c23c8659
ML
263 void set_log_callback(LogCallbackFunction callback);
264 /** Set the log callback to the default handler. */
265 void set_log_callback_default();
e2eaf858
DE
266 /** Install a delegate for reading resource files.
267 * @param reader The resource reader delegate, or nullptr to unset. */
268 void set_resource_reader(ResourceReader *reader);
c23c8659
ML
269 /** Create a new session. */
270 shared_ptr<Session> create_session();
9fa5b426
ML
271 /** Create a new user device. */
272 shared_ptr<UserDevice> create_user_device(
273 string vendor, string model, string version);
304be4a7
ML
274 /** Create a header packet. */
275 shared_ptr<Packet> create_header_packet(Glib::TimeVal start_time);
276 /** Create a meta packet. */
277 shared_ptr<Packet> create_meta_packet(
9e7176bd 278 map<const ConfigKey *, Glib::VariantBase> config);
304be4a7
ML
279 /** Create a logic packet. */
280 shared_ptr<Packet> create_logic_packet(
281 void *data_pointer, size_t data_length, unsigned int unit_size);
282 /** Create an analog packet. */
dd13d47a 283 shared_ptr<Packet> create_analog_packet(
9e7176bd 284 vector<shared_ptr<Channel> > channels,
304be4a7 285 float *data_pointer, unsigned int num_samples, const Quantity *mq,
9e7176bd 286 const Unit *unit, vector<const QuantityFlag *> mqflags);
b6f411ac
ML
287 /** Load a saved session.
288 * @param filename File name string. */
c23c8659 289 shared_ptr<Session> load_session(string filename);
b6f411ac
ML
290 /** Create a new trigger.
291 * @param name Name string for new trigger. */
c23c8659 292 shared_ptr<Trigger> create_trigger(string name);
ca3291e3
ML
293 /** Open an input file.
294 * @param filename File name string. */
295 shared_ptr<Input> open_file(string filename);
296 /** Open an input stream based on header data.
297 * @param header Initial data from stream. */
298 shared_ptr<Input> open_stream(string header);
15bebf57 299 map<string, string> serials(shared_ptr<Driver> driver) const;
21d1bec6 300private:
d5d7b09e 301 struct sr_context *_structure;
f17b4546
DE
302 map<string, unique_ptr<Driver> > _drivers;
303 map<string, unique_ptr<InputFormat> > _input_formats;
304 map<string, unique_ptr<OutputFormat> > _output_formats;
3b161085
ML
305 Session *_session;
306 LogCallbackFunction _log_callback;
c23c8659
ML
307 Context();
308 ~Context();
c23c8659
ML
309 friend class Session;
310 friend class Driver;
01451225 311 friend struct std::default_delete<Context>;
c23c8659
ML
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. */
12f2f640 329 map<const ConfigKey *, set<const 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. */
9e7176bd
DE
355 vector<shared_ptr<HardwareDevice> > scan(map<const ConfigKey *, Glib::VariantBase>
356 options = map<const ConfigKey *, Glib::VariantBase>());
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;
01451225 366 friend struct 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;
01451225 407 friend struct 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;
01451225 426 friend struct 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;
01451225 443 friend struct 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;
01451225 476 friend struct 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;
01451225 494 friend struct 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;
01451225 515 friend struct 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;
01451225 542 friend struct 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;
01451225 562 friend struct 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;
01451225 597 friend struct 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;
01451225 652 friend struct 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;
01451225 679 friend struct 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;
01451225 693 friend struct 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. */
9e7176bd
DE
801 shared_ptr<Input> create_input(map<string, Glib::VariantBase>
802 options = map<string, Glib::VariantBase>());
21d1bec6 803private:
6c11b496 804 explicit InputFormat(const struct sr_input_module *structure);
c23c8659 805 ~InputFormat();
d5d7b09e
DE
806
807 const struct sr_input_module *_structure;
808
c23c8659 809 friend class Context;
ca3291e3 810 friend class InputDevice;
01451225 811 friend struct std::default_delete<InputFormat>;
c23c8659
ML
812};
813
ca3291e3 814/** An input instance (an input format applied to a file or stream) */
d5d7b09e 815class SR_API Input : public UserOwned<Input>
c23c8659
ML
816{
817public:
ca3291e3 818 /** Virtual device associated with this input. */
3b161085 819 shared_ptr<InputDevice> device();
ca3291e3 820 /** Send next stream data.
2b51d48b
ML
821 * @param data Next stream data.
822 * @param length Length of data. */
823 void send(void *data, size_t length);
9c51e8ec
ML
824 /** Signal end of input data. */
825 void end();
21d1bec6 826private:
ca3291e3
ML
827 Input(shared_ptr<Context> context, const struct sr_input *structure);
828 ~Input();
d5d7b09e 829 const struct sr_input *_structure;
3b161085 830 shared_ptr<Context> _context;
f17b4546 831 unique_ptr<InputDevice> _device;
a98729a7 832
ca3291e3 833 friend class Context;
c23c8659 834 friend class InputFormat;
01451225 835 friend struct std::default_delete<Input>;
c23c8659
ML
836};
837
ca3291e3 838/** A virtual device associated with an input */
6e5240f4 839class SR_API InputDevice :
d5d7b09e 840 public ParentOwned<InputDevice, Input>,
6e5240f4 841 public Device
ca3291e3 842{
21d1bec6 843private:
ca3291e3
ML
844 InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
845 ~InputDevice();
d01d2314 846 shared_ptr<Device> get_shared_from_this();
3b161085 847 shared_ptr<Input> _input;
ca3291e3 848 friend class Input;
01451225 849 friend struct std::default_delete<InputDevice>;
ca3291e3
ML
850};
851
58aa1f83 852/** An option used by an output format */
d5d7b09e 853class SR_API Option : public UserOwned<Option>
58aa1f83
ML
854{
855public:
856 /** Short name of this option suitable for command line usage. */
15bebf57 857 string id() const;
58aa1f83 858 /** Short name of this option suitable for GUI usage. */
15bebf57 859 string name() const;
58aa1f83 860 /** Description of this option in a sentence. */
15bebf57 861 string description() const;
58aa1f83 862 /** Default value for this option. */
15bebf57 863 Glib::VariantBase default_value() const;
58aa1f83 864 /** Possible values for this option, if a limited set. */
15bebf57 865 vector<Glib::VariantBase> values() const;
21d1bec6 866private:
58aa1f83 867 Option(const struct sr_option *structure,
70d3b20b 868 shared_ptr<const struct sr_option *> structure_array);
58aa1f83 869 ~Option();
d5d7b09e 870 const struct sr_option *_structure;
3b161085 871 shared_ptr<const struct sr_option *> _structure_array;
a98729a7 872
43942280 873 friend class InputFormat;
58aa1f83 874 friend class OutputFormat;
01451225 875 friend struct std::default_delete<Option>;
58aa1f83
ML
876};
877
07443fd2 878/** An output format supported by the library */
c23c8659 879class SR_API OutputFormat :
d5d7b09e 880 public ParentOwned<OutputFormat, Context>
c23c8659
ML
881{
882public:
883 /** Name of this output format. */
15bebf57 884 string name() const;
c23c8659 885 /** Description of this output format. */
15bebf57 886 string description() const;
8a174d23
JH
887 /** A list of preferred file name extensions for this file format.
888 * @note This list is a recommendation only. */
15bebf57 889 vector<string> extensions() const;
58aa1f83 890 /** Options supported by this output format. */
3b161085 891 map<string, shared_ptr<Option> > options();
b6f411ac
ML
892 /** Create an output using this format.
893 * @param device Device to output for.
894 * @param options Mapping of (option name, value) pairs. */
bf03d635 895 shared_ptr<Output> create_output(shared_ptr<Device> device,
9e7176bd 896 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
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,
9e7176bd 903 map<string, Glib::VariantBase> options = map<string, Glib::VariantBase>());
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;
01451225 919 friend struct 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,
9e7176bd 932 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
81b3ce37 933 Output(string filename, shared_ptr<OutputFormat> format,
9e7176bd 934 shared_ptr<Device> device, 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;
01451225 943 friend struct 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
c23c8659
ML
989}
990
ace872d5
ML
991#include <libsigrokcxx/enums.hpp>
992
1b40fdb8 993#endif