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