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