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