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