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