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