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