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