]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | Introduction | |
25 | ------------ | |
26 | ||
27 | The sigrok++ API provides an object-oriented C++ interface to the functionality | |
28 | in libsigrok, including automatic memory and resource management. | |
29 | ||
30 | It is built on top of the public libsigrok C API, and is designed to be used as | |
31 | a standalone alternative API. Programs should not mix usage of the C and C++ | |
32 | APIs; the C++ interface code needs to have full control of all C API calls for | |
33 | resources to be managed correctly. | |
34 | ||
35 | Memory management | |
36 | ----------------- | |
37 | ||
38 | All runtime objects created through the C++ API are passed and accessed via | |
39 | shared pointers, using the C++11 std::shared_ptr implementation. This means | |
40 | that a reference count is kept for each object. | |
41 | ||
42 | Shared pointers can be copied and assigned in a user's program, automatically | |
43 | updating their reference count and deleting objects when they are no longer in | |
44 | use. The C++ interface code also keeps track of internal dependencies between | |
45 | libsigrok resources, and ensures that objects are not prematurely deleted when | |
46 | their resources are in use by other objects. | |
47 | ||
48 | This means that management of sigrok++ objects and their underlying libsigrok | |
49 | resources can be treated as fully automatic. As long as all shared pointers to | |
50 | objects are deleted or reassigned when no longer in use, all underlying | |
51 | resources will be released at the right time. | |
52 | ||
53 | Getting started | |
54 | --------------- | |
55 | ||
56 | Usage of the C++ API needs to begin with a call to sigrok::Context::create(). | |
57 | This will create the global libsigrok context and returns a shared pointer to | |
58 | the sigrok::Context object. Methods on this object provide access to the | |
59 | hardware drivers, input and output formats supported by the library, as well | |
60 | as means of creating other objects such as sessions and triggers. | |
61 | ||
62 | Error handling | |
63 | -------------- | |
64 | ||
65 | When any libsigrok C API call returns an error, a sigrok::Error exception is | |
66 | raised, 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> | |
80 | ||
81 | namespace sigrok | |
82 | { | |
83 | ||
84 | using namespace std; | |
85 | ||
86 | /* Forward declarations */ | |
87 | class SR_API Error; | |
88 | class SR_API Context; | |
89 | class SR_API Driver; | |
90 | class SR_API Device; | |
91 | class SR_API HardwareDevice; | |
92 | class SR_API Channel; | |
93 | class SR_API EventSource; | |
94 | class SR_API Session; | |
95 | class SR_API ConfigKey; | |
96 | class SR_API InputFormat; | |
97 | class SR_API OutputFormat; | |
98 | class SR_API LogLevel; | |
99 | class SR_API ChannelGroup; | |
100 | class SR_API Trigger; | |
101 | class SR_API TriggerStage; | |
102 | class SR_API TriggerMatch; | |
103 | class SR_API TriggerMatchType; | |
104 | class SR_API ChannelType; | |
105 | class SR_API Packet; | |
106 | class SR_API PacketPayload; | |
107 | class SR_API PacketType; | |
108 | class SR_API Quantity; | |
109 | class SR_API Unit; | |
110 | class SR_API QuantityFlag; | |
111 | class SR_API InputFileDevice; | |
112 | class SR_API Output; | |
113 | class SR_API DataType; | |
114 | ||
115 | /** Exception thrown when an error code is returned by any libsigrok call. */ | |
116 | class SR_API Error: public exception | |
117 | { | |
118 | public: | |
119 | Error(int result); | |
120 | ~Error() throw(); | |
121 | const int result; | |
122 | const char *what() const throw(); | |
123 | }; | |
124 | ||
07443fd2 | 125 | /* Base template for most classes which wrap a struct type from libsigrok. */ |
c23c8659 ML |
126 | template <class Parent, typename Struct> class SR_API StructureWrapper : |
127 | public enable_shared_from_this<StructureWrapper<Parent, Struct> > | |
128 | { | |
7649683c | 129 | protected: |
07443fd2 | 130 | /* Parent object which owns this child object's underlying structure. |
c23c8659 ML |
131 | |
132 | This shared pointer will be null when this child is unused, but | |
133 | will be assigned to point to the parent before any shared pointer | |
134 | to this child is handed out to the user. | |
135 | ||
136 | When the reference count of this child falls to zero, this shared | |
137 | pointer to its parent is reset by a custom deleter on the child's | |
138 | shared pointer. | |
139 | ||
140 | This strategy ensures that the destructors for both the child and | |
141 | the parent are called at the correct time, i.e. only when all | |
142 | references to both the parent and all its children are gone. */ | |
143 | shared_ptr<Parent> parent; | |
7649683c ML |
144 | |
145 | public: | |
146 | shared_ptr<StructureWrapper<Parent, Struct> > | |
147 | get_shared_pointer(Parent *parent) | |
148 | { | |
149 | this->parent = static_pointer_cast<Parent>(parent->shared_from_this()); | |
150 | return shared_ptr<StructureWrapper<Parent, Struct> >( | |
151 | this, reset_parent); | |
152 | } | |
153 | shared_ptr<StructureWrapper<Parent, Struct> > | |
154 | get_shared_pointer(shared_ptr<Parent> parent) | |
155 | { | |
156 | this->parent = parent; | |
157 | return shared_ptr<StructureWrapper<Parent, Struct> >( | |
158 | this, reset_parent); | |
159 | } | |
c23c8659 | 160 | protected: |
7649683c ML |
161 | static void reset_parent(StructureWrapper<Parent, Struct> *object) |
162 | { | |
163 | object->parent.reset(); | |
164 | } | |
165 | ||
c23c8659 ML |
166 | Struct *structure; |
167 | ||
168 | StructureWrapper<Parent, Struct>(Struct *structure) : | |
169 | structure(structure) | |
170 | { | |
171 | } | |
172 | }; | |
173 | ||
174 | /** Type of log callback */ | |
175 | typedef function<void(const LogLevel *, string message)> LogCallbackFunction; | |
176 | ||
07443fd2 | 177 | /** The global libsigrok context */ |
c23c8659 ML |
178 | class SR_API Context : public enable_shared_from_this<Context> |
179 | { | |
180 | public: | |
181 | /** Create new context */ | |
182 | static shared_ptr<Context> create(); | |
183 | /** libsigrok package version. */ | |
184 | string get_package_version(); | |
185 | /** libsigrok library version. */ | |
186 | string get_lib_version(); | |
187 | /** Available hardware drivers, indexed by name. */ | |
188 | map<string, shared_ptr<Driver> > get_drivers(); | |
189 | /** Available input formats, indexed by name. */ | |
190 | map<string, shared_ptr<InputFormat> > get_input_formats(); | |
191 | /** Available output formats, indexed by name. */ | |
192 | map<string, shared_ptr<OutputFormat> > get_output_formats(); | |
193 | /** Current log level. */ | |
194 | const LogLevel *get_log_level(); | |
195 | /** Set the log level. */ | |
196 | void set_log_level(const LogLevel *level); | |
197 | /** Current log domain. */ | |
198 | string get_log_domain(); | |
199 | /** Set the log domain. */ | |
200 | void set_log_domain(string value); | |
201 | /** Set the log callback. */ | |
202 | void set_log_callback(LogCallbackFunction callback); | |
203 | /** Set the log callback to the default handler. */ | |
204 | void set_log_callback_default(); | |
205 | /** Create a new session. */ | |
206 | shared_ptr<Session> create_session(); | |
207 | /** Load a saved session. */ | |
208 | shared_ptr<Session> load_session(string filename); | |
209 | /** Create a new trigger. */ | |
210 | shared_ptr<Trigger> create_trigger(string name); | |
211 | protected: | |
212 | struct sr_context *structure; | |
213 | map<string, Driver *> drivers; | |
214 | map<string, InputFormat *> input_formats; | |
215 | map<string, OutputFormat *> output_formats; | |
216 | Session *session; | |
217 | LogCallbackFunction log_callback; | |
218 | Context(); | |
219 | ~Context(); | |
220 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
221 | class Deleter | |
222 | { | |
223 | public: | |
224 | void operator()(Context *context) { delete context; } | |
225 | }; | |
226 | friend class Deleter; | |
227 | friend class Session; | |
228 | friend class Driver; | |
229 | }; | |
230 | ||
07443fd2 | 231 | /** A hardware driver provided by the library */ |
c23c8659 ML |
232 | class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver> |
233 | { | |
234 | public: | |
235 | /** Name of this driver. */ | |
236 | string get_name(); | |
237 | /** Long name for this driver. */ | |
238 | string get_long_name(); | |
239 | /** Scan for devices and return a list of devices found. */ | |
240 | vector<shared_ptr<HardwareDevice> > scan( | |
241 | map<const ConfigKey *, Glib::VariantBase> options = {}); | |
242 | protected: | |
243 | bool initialized; | |
244 | vector<HardwareDevice *> devices; | |
245 | Driver(struct sr_dev_driver *structure); | |
246 | ~Driver(); | |
247 | friend class Context; | |
248 | friend class HardwareDevice; | |
249 | friend class ChannelGroup; | |
250 | }; | |
251 | ||
252 | /** An object that can be configured. */ | |
253 | class SR_API Configurable | |
254 | { | |
255 | public: | |
256 | /** Read configuration for the given key. */ | |
257 | Glib::VariantBase config_get(const ConfigKey *key); | |
258 | /** Set configuration for the given key to a specified value. */ | |
259 | void config_set(const ConfigKey *key, Glib::VariantBase value); | |
260 | /** Enumerate available values for the given configuration key. */ | |
e194c011 | 261 | Glib::VariantContainerBase config_list(const ConfigKey *key); |
c23c8659 ML |
262 | protected: |
263 | Configurable( | |
264 | struct sr_dev_driver *driver, | |
265 | struct sr_dev_inst *sdi, | |
266 | struct sr_channel_group *channel_group); | |
267 | virtual ~Configurable(); | |
268 | struct sr_dev_driver *config_driver; | |
269 | struct sr_dev_inst *config_sdi; | |
270 | struct sr_channel_group *config_channel_group; | |
271 | }; | |
272 | ||
07443fd2 | 273 | /** A generic device, either hardware or virtual */ |
c23c8659 ML |
274 | class SR_API Device : |
275 | public Configurable, | |
276 | public StructureWrapper<Context, struct sr_dev_inst> | |
277 | { | |
278 | public: | |
f36ca889 ML |
279 | /** Description identifying this device. */ |
280 | string get_description(); | |
c23c8659 ML |
281 | /** Vendor name for this device. */ |
282 | string get_vendor(); | |
283 | /** Model name for this device. */ | |
284 | string get_model(); | |
285 | /** Version string for this device. */ | |
286 | string get_version(); | |
287 | /** List of the channels available on this device. */ | |
288 | vector<shared_ptr<Channel> > get_channels(); | |
6be7a7f2 ML |
289 | /** Channel groups available on this device, indexed by name. */ |
290 | map<string, shared_ptr<ChannelGroup> > get_channel_groups(); | |
c23c8659 ML |
291 | /** Open device. */ |
292 | void open(); | |
293 | /** Close device. */ | |
294 | void close(); | |
295 | protected: | |
296 | Device(struct sr_dev_inst *structure); | |
297 | ~Device(); | |
4178d971 ML |
298 | shared_ptr<Channel> get_channel(struct sr_channel *ptr); |
299 | map<struct sr_channel *, Channel *> channels; | |
6be7a7f2 | 300 | map<string, ChannelGroup *> channel_groups; |
c23c8659 ML |
301 | /** Deleter needed to allow shared_ptr use with protected destructor. */ |
302 | class Deleter | |
303 | { | |
304 | public: | |
305 | void operator()(Device *device) { delete device; } | |
306 | }; | |
307 | friend class Deleter; | |
308 | friend class Session; | |
309 | friend class Channel; | |
310 | friend class ChannelGroup; | |
311 | friend class Output; | |
2928f47d | 312 | friend class Analog; |
c23c8659 ML |
313 | }; |
314 | ||
07443fd2 | 315 | /** A real hardware device, connected via a driver */ |
c23c8659 ML |
316 | class SR_API HardwareDevice : public Device |
317 | { | |
318 | public: | |
319 | /** Driver providing this device. */ | |
320 | shared_ptr<Driver> get_driver(); | |
c23c8659 ML |
321 | protected: |
322 | HardwareDevice(Driver *driver, struct sr_dev_inst *structure); | |
323 | ~HardwareDevice(); | |
324 | Driver *driver; | |
c23c8659 ML |
325 | friend class Driver; |
326 | friend class ChannelGroup; | |
327 | }; | |
328 | ||
07443fd2 | 329 | /** A channel on a device */ |
c23c8659 ML |
330 | class SR_API Channel : public StructureWrapper<Device, struct sr_channel> |
331 | { | |
332 | public: | |
333 | /** Current name of this channel. */ | |
334 | string get_name(); | |
335 | /** Set the name of this channel. */ | |
336 | void set_name(string name); | |
337 | /** Type of this channel. */ | |
338 | const ChannelType *get_type(); | |
339 | /** Enabled status of this channel. */ | |
340 | bool get_enabled(); | |
341 | /** Set the enabled status of this channel. */ | |
342 | void set_enabled(bool value); | |
06bd935e ML |
343 | /** Get the index number of this channel. */ |
344 | unsigned int get_index(); | |
c23c8659 ML |
345 | protected: |
346 | Channel(struct sr_channel *structure); | |
347 | ~Channel(); | |
348 | const ChannelType * const type; | |
349 | friend class Device; | |
350 | friend class ChannelGroup; | |
351 | friend class Session; | |
352 | friend class TriggerStage; | |
353 | }; | |
354 | ||
07443fd2 | 355 | /** A group of channels on a device, which share some configuration */ |
c23c8659 | 356 | class SR_API ChannelGroup : |
6be7a7f2 | 357 | public StructureWrapper<Device, struct sr_channel_group>, |
c23c8659 ML |
358 | public Configurable |
359 | { | |
360 | public: | |
361 | /** Name of this channel group. */ | |
362 | string get_name(); | |
363 | /** List of the channels in this group. */ | |
364 | vector<shared_ptr<Channel> > get_channels(); | |
365 | protected: | |
6be7a7f2 | 366 | ChannelGroup(Device *device, struct sr_channel_group *structure); |
c23c8659 ML |
367 | ~ChannelGroup(); |
368 | vector<Channel *> channels; | |
6be7a7f2 | 369 | friend class Device; |
c23c8659 ML |
370 | }; |
371 | ||
07443fd2 | 372 | /** A trigger configuration */ |
c23c8659 ML |
373 | class SR_API Trigger : public enable_shared_from_this<Trigger> |
374 | { | |
375 | public: | |
376 | string get_name(); | |
377 | vector<shared_ptr<TriggerStage> > get_stages(); | |
378 | shared_ptr<TriggerStage> add_stage(); | |
379 | protected: | |
380 | Trigger(shared_ptr<Context> context, string name); | |
381 | ~Trigger(); | |
382 | struct sr_trigger *structure; | |
383 | shared_ptr<Context> context; | |
384 | vector<TriggerStage *> stages; | |
385 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
386 | class Deleter | |
387 | { | |
388 | public: | |
389 | void operator()(Trigger *trigger) { delete trigger; } | |
390 | }; | |
391 | friend class Context; | |
6fa0eb86 | 392 | friend class Session; |
c23c8659 ML |
393 | }; |
394 | ||
07443fd2 | 395 | /** A stage in a trigger configuration */ |
c23c8659 ML |
396 | class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage> |
397 | { | |
398 | public: | |
399 | int get_number(); | |
400 | vector<shared_ptr<TriggerMatch> > get_matches(); | |
401 | void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type); | |
402 | void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value); | |
403 | protected: | |
404 | vector<TriggerMatch *> matches; | |
405 | TriggerStage(struct sr_trigger_stage *structure); | |
406 | ~TriggerStage(); | |
407 | friend class Trigger; | |
408 | }; | |
409 | ||
07443fd2 | 410 | /** A match condition in a trigger configuration */ |
c23c8659 ML |
411 | class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match> |
412 | { | |
413 | public: | |
414 | shared_ptr<Channel> get_channel(); | |
415 | const TriggerMatchType *get_type(); | |
416 | float get_value(); | |
417 | protected: | |
418 | TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel); | |
419 | ~TriggerMatch(); | |
420 | shared_ptr<Channel> channel; | |
421 | friend class TriggerStage; | |
422 | }; | |
423 | ||
424 | /** Type of datafeed callback */ | |
425 | typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)> | |
426 | DatafeedCallbackFunction; | |
427 | ||
07443fd2 | 428 | /* Data required for C callback function to call a C++ datafeed callback */ |
c23c8659 ML |
429 | class SR_PRIV DatafeedCallbackData |
430 | { | |
431 | public: | |
432 | void run(const struct sr_dev_inst *sdi, | |
433 | const struct sr_datafeed_packet *pkt); | |
434 | protected: | |
435 | DatafeedCallbackFunction callback; | |
436 | DatafeedCallbackData(Session *session, | |
437 | DatafeedCallbackFunction callback); | |
438 | Session *session; | |
439 | friend class Session; | |
440 | }; | |
441 | ||
442 | /** Type of source callback */ | |
443 | typedef function<bool(Glib::IOCondition)> | |
444 | SourceCallbackFunction; | |
445 | ||
07443fd2 | 446 | /* Data required for C callback function to call a C++ source callback */ |
c23c8659 ML |
447 | class SR_PRIV SourceCallbackData |
448 | { | |
449 | public: | |
450 | bool run(int revents); | |
451 | protected: | |
452 | SourceCallbackData(shared_ptr<EventSource> source); | |
453 | shared_ptr<EventSource> source; | |
454 | friend class Session; | |
455 | }; | |
456 | ||
07443fd2 | 457 | /** An I/O event source */ |
c23c8659 ML |
458 | class SR_API EventSource |
459 | { | |
460 | public: | |
461 | /** Create an event source from a file descriptor. */ | |
462 | static shared_ptr<EventSource> create(int fd, Glib::IOCondition events, | |
463 | int timeout, SourceCallbackFunction callback); | |
464 | /** Create an event source from a Glib::PollFD */ | |
465 | static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout, | |
466 | SourceCallbackFunction callback); | |
467 | /** Create an event source from a Glib::IOChannel */ | |
468 | static shared_ptr<EventSource> create( | |
469 | Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events, | |
470 | int timeout, SourceCallbackFunction callback); | |
471 | protected: | |
472 | EventSource(int timeout, SourceCallbackFunction callback); | |
473 | ~EventSource(); | |
474 | enum source_type { | |
475 | SOURCE_FD, | |
476 | SOURCE_POLLFD, | |
477 | SOURCE_IOCHANNEL | |
478 | } type; | |
479 | int fd; | |
480 | Glib::PollFD pollfd; | |
481 | Glib::RefPtr<Glib::IOChannel> channel; | |
482 | Glib::IOCondition events; | |
483 | int timeout; | |
484 | SourceCallbackFunction callback; | |
485 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
486 | class Deleter | |
487 | { | |
488 | public: | |
489 | void operator()(EventSource *source) { delete source; } | |
490 | }; | |
491 | friend class Deleter; | |
492 | friend class Session; | |
493 | friend class SourceCallbackData; | |
494 | }; | |
495 | ||
07443fd2 | 496 | /** A sigrok session */ |
c23c8659 ML |
497 | class SR_API Session |
498 | { | |
499 | public: | |
500 | /** Add a device to this session. */ | |
501 | void add_device(shared_ptr<Device> device); | |
502 | /** List devices attached to this session. */ | |
503 | vector<shared_ptr<Device> > get_devices(); | |
504 | /** Remove all devices from this session. */ | |
505 | void remove_devices(); | |
506 | /** Add a datafeed callback to this session. */ | |
507 | void add_datafeed_callback(DatafeedCallbackFunction callback); | |
508 | /** Remove all datafeed callbacks from this session. */ | |
509 | void remove_datafeed_callbacks(); | |
510 | /** Add an event source. */ | |
511 | void add_source(shared_ptr<EventSource> source); | |
512 | /** Remove an event source. */ | |
513 | void remove_source(shared_ptr<EventSource> source); | |
514 | /** Start the session. */ | |
515 | void start(); | |
516 | /** Run the session event loop. */ | |
517 | void run(); | |
518 | /** Stop the session. */ | |
519 | void stop(); | |
520 | /** Begin saving session to a file. */ | |
521 | void begin_save(string filename); | |
522 | /** Append a packet to the session file being saved. */ | |
1d67cfb4 ML |
523 | void append(shared_ptr<Packet> packet); |
524 | /** Append raw logic data to the session file being saved. */ | |
525 | void append(void *data, size_t length, unsigned int unit_size); | |
6fa0eb86 ML |
526 | /** Get current trigger setting. */ |
527 | shared_ptr<Trigger> get_trigger(); | |
528 | /** Set trigger setting. */ | |
529 | void set_trigger(shared_ptr<Trigger> trigger); | |
c23c8659 ML |
530 | protected: |
531 | Session(shared_ptr<Context> context); | |
532 | Session(shared_ptr<Context> context, string filename); | |
533 | ~Session(); | |
534 | struct sr_session *structure; | |
535 | const shared_ptr<Context> context; | |
536 | map<const struct sr_dev_inst *, shared_ptr<Device> > devices; | |
537 | vector<DatafeedCallbackData *> datafeed_callbacks; | |
538 | map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks; | |
539 | bool saving; | |
540 | bool save_initialized; | |
541 | string save_filename; | |
542 | uint64_t save_samplerate; | |
6fa0eb86 | 543 | shared_ptr<Trigger> trigger; |
c23c8659 ML |
544 | /** Deleter needed to allow shared_ptr use with protected destructor. */ |
545 | class Deleter | |
546 | { | |
547 | public: | |
548 | void operator()(Session *session) { delete session; } | |
549 | }; | |
550 | friend class Deleter; | |
551 | friend class Context; | |
552 | friend class DatafeedCallbackData; | |
553 | }; | |
554 | ||
07443fd2 | 555 | /** A packet on the session datafeed */ |
2928f47d | 556 | class SR_API Packet : public enable_shared_from_this<Packet> |
c23c8659 ML |
557 | { |
558 | public: | |
90ba83f2 ML |
559 | /** Type of this packet. */ |
560 | const PacketType *get_type(); | |
c23c8659 | 561 | /** Payload of this packet. */ |
2928f47d | 562 | shared_ptr<PacketPayload> get_payload(); |
c23c8659 | 563 | protected: |
2928f47d ML |
564 | Packet(shared_ptr<Device> device, |
565 | const struct sr_datafeed_packet *structure); | |
c23c8659 ML |
566 | ~Packet(); |
567 | const struct sr_datafeed_packet *structure; | |
2928f47d | 568 | shared_ptr<Device> device; |
c23c8659 ML |
569 | PacketPayload *payload; |
570 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
571 | class Deleter | |
572 | { | |
573 | public: | |
574 | void operator()(Packet *packet) { delete packet; } | |
575 | }; | |
576 | friend class Deleter; | |
577 | friend class Session; | |
578 | friend class Output; | |
579 | friend class DatafeedCallbackData; | |
2928f47d ML |
580 | friend class Header; |
581 | friend class Meta; | |
582 | friend class Logic; | |
583 | friend class Analog; | |
c23c8659 ML |
584 | }; |
585 | ||
07443fd2 | 586 | /** Abstract base class for datafeed packet payloads */ |
c23c8659 ML |
587 | class SR_API PacketPayload |
588 | { | |
589 | protected: | |
590 | PacketPayload(); | |
591 | virtual ~PacketPayload() = 0; | |
2928f47d ML |
592 | shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) { |
593 | return static_pointer_cast<PacketPayload>(get_shared_pointer(parent)); | |
594 | } | |
595 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
596 | class Deleter | |
597 | { | |
598 | public: | |
599 | void operator()(PacketPayload *payload) { delete payload; } | |
600 | }; | |
601 | friend class Deleter; | |
c23c8659 ML |
602 | friend class Packet; |
603 | friend class Output; | |
604 | }; | |
605 | ||
2928f47d ML |
606 | /** Payload of a datafeed header packet */ |
607 | class SR_API Header : public PacketPayload, | |
608 | public StructureWrapper<Packet, const struct sr_datafeed_header> | |
609 | { | |
610 | public: | |
611 | int get_feed_version(); | |
612 | Glib::TimeVal get_start_time(); | |
613 | protected: | |
614 | Header(const struct sr_datafeed_header *structure); | |
615 | ~Header(); | |
616 | const struct sr_datafeed_header *structure; | |
617 | friend class Packet; | |
618 | }; | |
619 | ||
620 | /** Payload of a datafeed metadata packet */ | |
621 | class SR_API Meta : public PacketPayload, | |
622 | public StructureWrapper<Packet, const struct sr_datafeed_meta> | |
623 | { | |
624 | public: | |
625 | map<const ConfigKey *, Glib::VariantBase> get_config(); | |
626 | protected: | |
627 | Meta(const struct sr_datafeed_meta *structure); | |
628 | ~Meta(); | |
629 | const struct sr_datafeed_meta *structure; | |
630 | map<const ConfigKey *, Glib::VariantBase> config; | |
631 | friend class Packet; | |
632 | }; | |
633 | ||
07443fd2 | 634 | /** Payload of a datafeed packet with logic data */ |
2928f47d ML |
635 | class SR_API Logic : public PacketPayload, |
636 | public StructureWrapper<Packet, const struct sr_datafeed_logic> | |
c23c8659 | 637 | { |
2928f47d ML |
638 | public: |
639 | /* Pointer to data. */ | |
640 | void *get_data_pointer(); | |
641 | /* Data length in bytes. */ | |
642 | size_t get_data_length(); | |
643 | /* Size of each sample in bytes. */ | |
644 | unsigned int get_unit_size(); | |
c23c8659 ML |
645 | protected: |
646 | Logic(const struct sr_datafeed_logic *structure); | |
647 | ~Logic(); | |
648 | const struct sr_datafeed_logic *structure; | |
c23c8659 ML |
649 | friend class Packet; |
650 | }; | |
651 | ||
07443fd2 | 652 | /** Payload of a datafeed packet with analog data */ |
2928f47d ML |
653 | class SR_API Analog : public PacketPayload, |
654 | public StructureWrapper<Packet, const struct sr_datafeed_analog> | |
c23c8659 ML |
655 | { |
656 | public: | |
2928f47d ML |
657 | /** Pointer to data. */ |
658 | float *get_data_pointer(); | |
c23c8659 ML |
659 | /** Number of samples in this packet. */ |
660 | unsigned int get_num_samples(); | |
2928f47d ML |
661 | /** Channels for which this packet contains data. */ |
662 | vector<shared_ptr<Channel> > get_channels(); | |
c23c8659 ML |
663 | /** Measured quantity of the samples in this packet. */ |
664 | const Quantity *get_mq(); | |
665 | /** Unit of the samples in this packet. */ | |
666 | const Unit *get_unit(); | |
667 | /** Measurement flags associated with the samples in this packet. */ | |
668 | vector<const QuantityFlag *> get_mq_flags(); | |
669 | protected: | |
670 | Analog(const struct sr_datafeed_analog *structure); | |
671 | ~Analog(); | |
672 | const struct sr_datafeed_analog *structure; | |
c23c8659 ML |
673 | friend class Packet; |
674 | }; | |
675 | ||
07443fd2 | 676 | /** An input format supported by the library */ |
c23c8659 ML |
677 | class SR_API InputFormat : |
678 | public StructureWrapper<Context, struct sr_input_format> | |
679 | { | |
680 | public: | |
681 | /** Name of this input format. */ | |
682 | string get_name(); | |
683 | /** Description of this input format. */ | |
684 | string get_description(); | |
685 | /** Check whether a given file matches this input format. */ | |
686 | bool format_match(string filename); | |
687 | /** Open a file using this input format. */ | |
688 | shared_ptr<InputFileDevice> open_file(string filename, | |
689 | map<string, string> options = {}); | |
690 | protected: | |
691 | InputFormat(struct sr_input_format *structure); | |
692 | ~InputFormat(); | |
693 | friend class Context; | |
694 | friend class InputFileDevice; | |
695 | }; | |
696 | ||
07443fd2 | 697 | /** A virtual device associated with an input file */ |
c23c8659 ML |
698 | class SR_API InputFileDevice : public Device |
699 | { | |
700 | public: | |
701 | /** Load data from file. */ | |
702 | void load(); | |
703 | protected: | |
704 | InputFileDevice(shared_ptr<InputFormat> format, | |
705 | struct sr_input *input, string filename); | |
706 | ~InputFileDevice(); | |
707 | struct sr_input *input; | |
708 | shared_ptr<InputFormat> format; | |
709 | string filename; | |
710 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
711 | class Deleter | |
712 | { | |
713 | public: | |
714 | void operator()(InputFileDevice *device) { delete device; } | |
715 | }; | |
716 | friend class Deleter; | |
717 | friend class InputFormat; | |
718 | }; | |
719 | ||
07443fd2 | 720 | /** An output format supported by the library */ |
c23c8659 ML |
721 | class SR_API OutputFormat : |
722 | public StructureWrapper<Context, struct sr_output_format> | |
723 | { | |
724 | public: | |
725 | /** Name of this output format. */ | |
726 | string get_name(); | |
727 | /** Description of this output format. */ | |
728 | string get_description(); | |
729 | /** Create an output using this format. */ | |
730 | shared_ptr<Output> create_output(shared_ptr<Device> device, map<string, string> options = {}); | |
731 | protected: | |
732 | OutputFormat(struct sr_output_format *structure); | |
733 | ~OutputFormat(); | |
734 | friend class Context; | |
735 | friend class Output; | |
736 | }; | |
737 | ||
07443fd2 | 738 | /** An output instance (an output format applied to a device) */ |
c23c8659 ML |
739 | class SR_API Output |
740 | { | |
741 | public: | |
742 | /** Update output with data from the given packet. */ | |
743 | string receive(shared_ptr<Packet> packet); | |
744 | protected: | |
745 | Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device); | |
746 | Output(shared_ptr<OutputFormat> format, | |
747 | shared_ptr<Device> device, map<string, string> options); | |
748 | ~Output(); | |
749 | struct sr_output *structure; | |
750 | const shared_ptr<OutputFormat> format; | |
751 | const shared_ptr<Device> device; | |
752 | const map<string, string> options; | |
753 | /** Deleter needed to allow shared_ptr use with protected destructor. */ | |
754 | class Deleter | |
755 | { | |
756 | public: | |
757 | void operator()(Output *output) { delete output; } | |
758 | }; | |
759 | friend class Deleter; | |
760 | friend class OutputFormat; | |
761 | }; | |
762 | ||
763 | /** Base class for objects which wrap an enumeration value from libsigrok */ | |
764 | template <typename T> class SR_API EnumValue | |
765 | { | |
766 | public: | |
767 | /** The enum constant associated with this value. */ | |
768 | T get_id() const { return id; } | |
769 | /** The name associated with this value. */ | |
770 | string get_name() const { return name; } | |
771 | protected: | |
772 | EnumValue(T id, const char name[]) : id(id), name(name) {} | |
773 | ~EnumValue() {} | |
774 | const T id; | |
775 | const string name; | |
776 | }; | |
777 | ||
778 | #include "enums.hpp" | |
779 | ||
780 | } | |
781 | ||
782 | #endif // LIBSIGROK_HPP |