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