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