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