]> sigrok.org Git - libsigrok.git/blame - src/libsigrok-internal.h
scpi-dmm: add support to get/set range on Agilent protocol using meters
[libsigrok.git] / src / libsigrok-internal.h
CommitLineData
1483577e 1/*
50985c20 2 * This file is part of the libsigrok project.
1483577e 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
1483577e
UH
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
a5827886
UH
20#ifndef LIBSIGROK_LIBSIGROK_INTERNAL_H
21#define LIBSIGROK_LIBSIGROK_INTERNAL_H
1483577e 22
1df81f4b
GS
23#include "config.h"
24
cc8a7d25 25#include <glib.h>
4417074c
GS
26#ifdef HAVE_LIBHIDAPI
27#include <hidapi.h>
28#endif
c4f2dfd0 29#ifdef HAVE_LIBSERIALPORT
dc991353 30#include <libserialport.h>
c4f2dfd0 31#endif
fdcdfe53
GS
32#ifdef HAVE_LIBUSB_1_0
33#include <libusb.h>
34#endif
35#include <stdarg.h>
ad5aa993 36#include <stdint.h>
fdcdfe53 37#include <stdio.h>
ae4c1fb6 38#include <stdlib.h>
b08024a8 39
98654c99
DE
40struct zip;
41struct zip_stat;
42
393fb9cb
UH
43/**
44 * @file
45 *
46 * libsigrok private header file, only to be used internally.
47 */
48
4cea9eb2
UH
49/*--- Macros ----------------------------------------------------------------*/
50
51#ifndef ARRAY_SIZE
52#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
53#endif
54
55#ifndef ARRAY_AND_SIZE
56#define ARRAY_AND_SIZE(a) (a), ARRAY_SIZE(a)
57#endif
58
6cfc6c5c
UH
59#ifndef G_SOURCE_FUNC
60#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) /* Since 2.58. */
61#endif
62
63#define SR_RECEIVE_DATA_CALLBACK(f) \
64 ((sr_receive_data_callback) (void (*)(void)) (f))
65
a4f9c35b 66/**
5bf0dd6a 67 * Read a 8 bits unsigned integer out of memory.
a4f9c35b 68 * @param x a pointer to the input memory
5bf0dd6a 69 * @return the corresponding unsigned integer
a4f9c35b 70 */
f1833600
GS
71static inline uint8_t read_u8(const uint8_t *p)
72{
73 return p[0];
74}
75#define R8(x) read_u8((const uint8_t *)(x))
a4f9c35b 76
e4bcc63d
GS
77/**
78 * Read an 8 bits signed integer out of memory.
79 * @param x a pointer to the input memory
80 * @return the corresponding signed integer
81 */
82static inline int8_t read_i8(const uint8_t *p)
83{
84 return (int8_t)p[0];
85}
86
e28ef28a 87/**
5bf0dd6a 88 * Read a 16 bits big endian unsigned integer out of memory.
e28ef28a 89 * @param x a pointer to the input memory
5bf0dd6a 90 * @return the corresponding unsigned integer
e28ef28a 91 */
f1833600
GS
92static inline uint16_t read_u16be(const uint8_t *p)
93{
94 uint16_t u;
95
96 u = 0;
97 u <<= 8; u |= p[0];
98 u <<= 8; u |= p[1];
99
100 return u;
101}
102#define RB16(x) read_u16be((const uint8_t *)(x))
e28ef28a
AJ
103
104/**
5bf0dd6a 105 * Read a 16 bits little endian unsigned integer out of memory.
e28ef28a 106 * @param x a pointer to the input memory
5bf0dd6a 107 * @return the corresponding unsigned integer
e28ef28a 108 */
f1833600
GS
109static inline uint16_t read_u16le(const uint8_t *p)
110{
111 uint16_t u;
112
113 u = 0;
114 u <<= 8; u |= p[1];
115 u <<= 8; u |= p[0];
116
117 return u;
118}
119#define RL16(x) read_u16le((const uint8_t *)(x))
e28ef28a 120
4d376e08
SB
121/**
122 * Read a 16 bits big endian signed integer out of memory.
123 * @param x a pointer to the input memory
124 * @return the corresponding signed integer
125 */
f1833600
GS
126static inline int16_t read_i16be(const uint8_t *p)
127{
128 uint16_t u;
129 int16_t i;
130
131 u = read_u16be(p);
132 i = (int16_t)u;
133
134 return i;
135}
136#define RB16S(x) read_i16be((const uint8_t *)(x))
4d376e08 137
e28ef28a 138/**
5bf0dd6a 139 * Read a 16 bits little endian signed integer out of memory.
e28ef28a 140 * @param x a pointer to the input memory
5bf0dd6a
BV
141 * @return the corresponding signed integer
142 */
f1833600
GS
143static inline int16_t read_i16le(const uint8_t *p)
144{
145 uint16_t u;
146 int16_t i;
147
148 u = read_u16le(p);
149 i = (int16_t)u;
150
151 return i;
152}
153#define RL16S(x) read_i16le((const uint8_t *)(x))
5bf0dd6a 154
080b6bcf
GS
155/**
156 * Read a 24 bits little endian unsigned integer out of memory.
157 * @param x a pointer to the input memory
158 * @return the corresponding unsigned integer
159 */
160static inline uint32_t read_u24le(const uint8_t *p)
161{
162 uint32_t u;
163
164 u = 0;
165 u <<= 8; u |= p[2];
166 u <<= 8; u |= p[1];
167 u <<= 8; u |= p[0];
168
169 return u;
170}
171
5bf0dd6a
BV
172/**
173 * Read a 32 bits big endian unsigned integer out of memory.
174 * @param x a pointer to the input memory
175 * @return the corresponding unsigned integer
e28ef28a 176 */
f1833600
GS
177static inline uint32_t read_u32be(const uint8_t *p)
178{
179 uint32_t u;
180
181 u = 0;
182 u <<= 8; u |= p[0];
183 u <<= 8; u |= p[1];
184 u <<= 8; u |= p[2];
185 u <<= 8; u |= p[3];
186
187 return u;
188}
189#define RB32(x) read_u32be((const uint8_t *)(x))
e28ef28a
AJ
190
191/**
5bf0dd6a 192 * Read a 32 bits little endian unsigned integer out of memory.
e28ef28a 193 * @param x a pointer to the input memory
5bf0dd6a 194 * @return the corresponding unsigned integer
e28ef28a 195 */
f1833600
GS
196static inline uint32_t read_u32le(const uint8_t *p)
197{
198 uint32_t u;
199
200 u = 0;
201 u <<= 8; u |= p[3];
202 u <<= 8; u |= p[2];
203 u <<= 8; u |= p[1];
204 u <<= 8; u |= p[0];
205
206 return u;
207}
208#define RL32(x) read_u32le((const uint8_t *)(x))
e28ef28a 209
4d376e08
SB
210/**
211 * Read a 32 bits big endian signed integer out of memory.
212 * @param x a pointer to the input memory
213 * @return the corresponding signed integer
214 */
f1833600
GS
215static inline int32_t read_i32be(const uint8_t *p)
216{
217 uint32_t u;
218 int32_t i;
219
220 u = read_u32be(p);
221 i = (int32_t)u;
222
223 return i;
224}
225#define RB32S(x) read_i32be((const uint8_t *)(x))
4d376e08 226
ea2d6d99 227/**
5bf0dd6a
BV
228 * Read a 32 bits little endian signed integer out of memory.
229 * @param x a pointer to the input memory
230 * @return the corresponding signed integer
231 */
f1833600
GS
232static inline int32_t read_i32le(const uint8_t *p)
233{
234 uint32_t u;
235 int32_t i;
236
237 u = read_u32le(p);
238 i = (int32_t)u;
239
240 return i;
241}
242#define RL32S(x) read_i32le((const uint8_t *)(x))
5bf0dd6a 243
17b93fd7
TS
244/**
245 * Read a 64 bits big endian unsigned integer out of memory.
246 * @param x a pointer to the input memory
247 * @return the corresponding unsigned integer
248 */
f1833600
GS
249static inline uint64_t read_u64be(const uint8_t *p)
250{
251 uint64_t u;
252
253 u = 0;
254 u <<= 8; u |= p[0];
255 u <<= 8; u |= p[1];
256 u <<= 8; u |= p[2];
257 u <<= 8; u |= p[3];
258 u <<= 8; u |= p[4];
259 u <<= 8; u |= p[5];
260 u <<= 8; u |= p[6];
261 u <<= 8; u |= p[7];
262
263 return u;
264}
265#define RB64(x) read_u64be((const uint8_t *)(x))
17b93fd7 266
8a66b077
SA
267/**
268 * Read a 64 bits little endian unsigned integer out of memory.
269 * @param x a pointer to the input memory
270 * @return the corresponding unsigned integer
271 */
f1833600
GS
272static inline uint64_t read_u64le(const uint8_t *p)
273{
274 uint64_t u;
275
276 u = 0;
277 u <<= 8; u |= p[7];
278 u <<= 8; u |= p[6];
279 u <<= 8; u |= p[5];
280 u <<= 8; u |= p[4];
281 u <<= 8; u |= p[3];
282 u <<= 8; u |= p[2];
283 u <<= 8; u |= p[1];
284 u <<= 8; u |= p[0];
285
286 return u;
287}
288#define RL64(x) read_u64le((const uint8_t *)(x))
289
290/**
291 * Read a 64 bits big endian signed integer out of memory.
292 * @param x a pointer to the input memory
293 * @return the corresponding unsigned integer
294 */
295static inline int64_t read_i64be(const uint8_t *p)
296{
297 uint64_t u;
298 int64_t i;
299
300 u = read_u64be(p);
301 i = (int64_t)u;
302
303 return i;
304}
305#define RB64S(x) read_i64be((const uint8_t *)(x))
8a66b077
SA
306
307/**
308 * Read a 64 bits little endian signed integer out of memory.
309 * @param x a pointer to the input memory
310 * @return the corresponding unsigned integer
311 */
f1833600
GS
312static inline int64_t read_i64le(const uint8_t *p)
313{
314 uint64_t u;
315 int64_t i;
316
317 u = read_u64le(p);
318 i = (int64_t)u;
319
320 return i;
321}
322#define RL64S(x) read_i64le((const uint8_t *)(x))
8a66b077 323
a2632bca 324/**
daa895cb 325 * Read a 32 bits big endian float out of memory (single precision).
a2632bca
AJ
326 * @param x a pointer to the input memory
327 * @return the corresponding float
328 */
f1833600
GS
329static inline float read_fltbe(const uint8_t *p)
330{
331 /*
332 * Implementor's note: Strictly speaking the "union" trick
333 * is not portable. But this phrase was found to work on the
334 * project's supported platforms, and serve well until a more
335 * appropriate phrase is found.
336 */
337 union { uint32_t u32; float flt; } u;
338 float f;
339
340 u.u32 = read_u32be(p);
341 f = u.flt;
342
343 return f;
344}
345#define RBFL(x) read_fltbe((const uint8_t *)(x))
a2632bca
AJ
346
347/**
daa895cb 348 * Read a 32 bits little endian float out of memory (single precision).
a2632bca
AJ
349 * @param x a pointer to the input memory
350 * @return the corresponding float
351 */
f1833600
GS
352static inline float read_fltle(const uint8_t *p)
353{
354 /*
355 * Implementor's note: Strictly speaking the "union" trick
356 * is not portable. But this phrase was found to work on the
357 * project's supported platforms, and serve well until a more
358 * appropriate phrase is found.
359 */
360 union { uint32_t u32; float flt; } u;
361 float f;
362
363 u.u32 = read_u32le(p);
364 f = u.flt;
365
366 return f;
367}
368#define RLFL(x) read_fltle((const uint8_t *)(x))
a2632bca 369
e4bcc63d
GS
370/**
371 * Read a 64 bits big endian float out of memory (double precision).
372 * @param x a pointer to the input memory
373 * @return the corresponding floating point value
374 */
375static inline double read_dblbe(const uint8_t *p)
376{
377 /*
378 * Implementor's note: Strictly speaking the "union" trick
379 * is not portable. But this phrase was found to work on the
380 * project's supported platforms, and serve well until a more
381 * appropriate phrase is found.
382 */
383 union { uint64_t u64; double flt; } u;
384 double f;
385
386 u.u64 = read_u64be(p);
387 f = u.flt;
388
389 return f;
390}
391
daa895cb
GS
392/**
393 * Read a 64 bits little endian float out of memory (double precision).
394 * @param x a pointer to the input memory
395 * @return the corresponding floating point value
396 */
397static inline double read_dblle(const uint8_t *p)
398{
399 /*
400 * Implementor's note: Strictly speaking the "union" trick
401 * is not portable. But this phrase was found to work on the
402 * project's supported platforms, and serve well until a more
403 * appropriate phrase is found.
404 */
405 union { uint64_t u64; double flt; } u;
406 double f;
407
408 u.u64 = read_u64le(p);
409 f = u.flt;
410
411 return f;
412}
413#define RLDB(x) read_dblle((const uint8_t *)(x))
414
5bf0dd6a
BV
415/**
416 * Write a 8 bits unsigned integer to memory.
ea2d6d99 417 * @param p a pointer to the output memory
5bf0dd6a 418 * @param x the input unsigned integer
ea2d6d99 419 */
f1833600
GS
420static inline void write_u8(uint8_t *p, uint8_t x)
421{
422 p[0] = x;
423}
424#define W8(p, x) write_u8((uint8_t *)(p), (uint8_t)(x))
ea2d6d99
AJ
425
426/**
5bf0dd6a 427 * Write a 16 bits unsigned integer to memory stored as big endian.
ea2d6d99 428 * @param p a pointer to the output memory
5bf0dd6a 429 * @param x the input unsigned integer
ea2d6d99 430 */
f1833600
GS
431static inline void write_u16be(uint8_t *p, uint16_t x)
432{
433 p[1] = x & 0xff; x >>= 8;
434 p[0] = x & 0xff; x >>= 8;
435}
436#define WB16(p, x) write_u16be((uint8_t *)(p), (uint16_t)(x))
ea2d6d99
AJ
437
438/**
5bf0dd6a 439 * Write a 16 bits unsigned integer to memory stored as little endian.
ea2d6d99 440 * @param p a pointer to the output memory
5bf0dd6a 441 * @param x the input unsigned integer
ea2d6d99 442 */
f1833600
GS
443static inline void write_u16le(uint8_t *p, uint16_t x)
444{
445 p[0] = x & 0xff; x >>= 8;
446 p[1] = x & 0xff; x >>= 8;
447}
448#define WL16(p, x) write_u16le((uint8_t *)(p), (uint16_t)(x))
ea2d6d99
AJ
449
450/**
5bf0dd6a 451 * Write a 32 bits unsigned integer to memory stored as big endian.
ea2d6d99 452 * @param p a pointer to the output memory
5bf0dd6a 453 * @param x the input unsigned integer
ea2d6d99 454 */
f1833600
GS
455static inline void write_u32be(uint8_t *p, uint32_t x)
456{
457 p[3] = x & 0xff; x >>= 8;
458 p[2] = x & 0xff; x >>= 8;
459 p[1] = x & 0xff; x >>= 8;
460 p[0] = x & 0xff; x >>= 8;
461}
462#define WB32(p, x) write_u32be((uint8_t *)(p), (uint32_t)(x))
ea2d6d99
AJ
463
464/**
5bf0dd6a 465 * Write a 32 bits unsigned integer to memory stored as little endian.
ea2d6d99 466 * @param p a pointer to the output memory
5bf0dd6a 467 * @param x the input unsigned integer
ea2d6d99 468 */
f1833600
GS
469static inline void write_u32le(uint8_t *p, uint32_t x)
470{
471 p[0] = x & 0xff; x >>= 8;
472 p[1] = x & 0xff; x >>= 8;
473 p[2] = x & 0xff; x >>= 8;
474 p[3] = x & 0xff; x >>= 8;
475}
476#define WL32(p, x) write_u32le((uint8_t *)(p), (uint32_t)(x))
ea2d6d99 477
797c8d90
GS
478/**
479 * Write a 48 bits unsigned integer to memory stored as little endian.
480 * @param p a pointer to the output memory
481 * @param x the input unsigned integer
482 */
483static inline void write_u48le(uint8_t *p, uint64_t x)
484{
485 p[0] = x & 0xff; x >>= 8;
486 p[1] = x & 0xff; x >>= 8;
487 p[2] = x & 0xff; x >>= 8;
488 p[3] = x & 0xff; x >>= 8;
489 p[4] = x & 0xff; x >>= 8;
490 p[5] = x & 0xff; x >>= 8;
491}
492#define WL48(p, x) write_u48le((uint8_t *)(p), (uint64_t)(x))
493
efce57da
GS
494/**
495 * Write a 64 bits unsigned integer to memory stored as big endian.
496 * @param p a pointer to the output memory
497 * @param x the input unsigned integer
498 */
499static inline void write_u64be(uint8_t *p, uint64_t x)
500{
501 p[7] = x & 0xff; x >>= 8;
502 p[6] = x & 0xff; x >>= 8;
503 p[5] = x & 0xff; x >>= 8;
504 p[4] = x & 0xff; x >>= 8;
505 p[3] = x & 0xff; x >>= 8;
506 p[2] = x & 0xff; x >>= 8;
507 p[1] = x & 0xff; x >>= 8;
508 p[0] = x & 0xff; x >>= 8;
509}
510
daa895cb
GS
511/**
512 * Write a 64 bits unsigned integer to memory stored as little endian.
513 * @param p a pointer to the output memory
514 * @param x the input unsigned integer
515 */
516static inline void write_u64le(uint8_t *p, uint64_t x)
517{
518 p[0] = x & 0xff; x >>= 8;
519 p[1] = x & 0xff; x >>= 8;
520 p[2] = x & 0xff; x >>= 8;
521 p[3] = x & 0xff; x >>= 8;
522 p[4] = x & 0xff; x >>= 8;
523 p[5] = x & 0xff; x >>= 8;
524 p[6] = x & 0xff; x >>= 8;
525 p[7] = x & 0xff; x >>= 8;
526}
527#define WL64(p, x) write_u64le((uint8_t *)(p), (uint64_t)(x))
528
a2632bca
AJ
529/**
530 * Write a 32 bits float to memory stored as big endian.
531 * @param p a pointer to the output memory
532 * @param x the input float
533 */
f1833600
GS
534static inline void write_fltbe(uint8_t *p, float x)
535{
536 union { uint32_t u; float f; } u;
537 u.f = x;
538 write_u32be(p, u.u);
539}
540#define WBFL(p, x) write_fltbe((uint8_t *)(p), (x))
a2632bca
AJ
541
542/**
543 * Write a 32 bits float to memory stored as little endian.
544 * @param p a pointer to the output memory
545 * @param x the input float
546 */
f1833600
GS
547static inline void write_fltle(uint8_t *p, float x)
548{
549 union { uint32_t u; float f; } u;
550 u.f = x;
551 write_u32le(p, u.u);
552}
553#define WLFL(p, x) write_fltle((uint8_t *)(p), float (x))
554
daa895cb
GS
555/**
556 * Write a 64 bits float to memory stored as little endian.
557 * @param p a pointer to the output memory
558 * @param x the input floating point value
559 */
560static inline void write_dblle(uint8_t *p, double x)
561{
562 union { uint64_t u; double f; } u;
563 u.f = x;
564 write_u64le(p, u.u);
565}
566#define WLDB(p, x) write_dblle((uint8_t *)(p), float (x))
567
f1833600
GS
568/* Endianess conversion helpers with read/write position increment. */
569
570/**
571 * Read unsigned 8bit integer from raw memory, increment read position.
572 * @param[in, out] p Pointer into byte stream.
573 * @return Retrieved integer value, unsigned.
574 */
575static inline uint8_t read_u8_inc(const uint8_t **p)
576{
577 uint8_t v;
578
579 if (!p || !*p)
580 return 0;
581 v = read_u8(*p);
582 *p += sizeof(v);
583
584 return v;
585}
586
e4bcc63d
GS
587/**
588 * Read signed 8bit integer from raw memory, increment read position.
589 * @param[in, out] p Pointer into byte stream.
590 * @return Retrieved integer value, signed.
591 */
592static inline int8_t read_i8_inc(const uint8_t **p)
593{
594 int8_t v;
595
596 if (!p || !*p)
597 return 0;
598 v = read_i8(*p);
599 *p += sizeof(v);
600
601 return v;
602}
603
f1833600
GS
604/**
605 * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
606 * @param[in, out] p Pointer into byte stream.
607 * @return Retrieved integer value, unsigned.
608 */
609static inline uint16_t read_u16be_inc(const uint8_t **p)
610{
611 uint16_t v;
612
613 if (!p || !*p)
614 return 0;
615 v = read_u16be(*p);
616 *p += sizeof(v);
617
618 return v;
619}
620
621/**
622 * Read unsigned 16bit integer from raw memory (little endian format), increment read position.
623 * @param[in, out] p Pointer into byte stream.
624 * @return Retrieved integer value, unsigned.
625 */
626static inline uint16_t read_u16le_inc(const uint8_t **p)
627{
628 uint16_t v;
629
630 if (!p || !*p)
631 return 0;
632 v = read_u16le(*p);
633 *p += sizeof(v);
634
635 return v;
636}
637
638/**
e4bcc63d 639 * Read signed 16bit integer from raw memory (big endian format), increment read position.
f1833600 640 * @param[in, out] p Pointer into byte stream.
e4bcc63d 641 * @return Retrieved integer value, signed.
f1833600 642 */
e4bcc63d 643static inline int16_t read_i16be_inc(const uint8_t **p)
f1833600 644{
e4bcc63d 645 int16_t v;
f1833600
GS
646
647 if (!p || !*p)
648 return 0;
e4bcc63d
GS
649 v = read_i16be(*p);
650 *p += sizeof(v);
651
652 return v;
653}
654
655/**
656 * Read signed 16bit integer from raw memory (little endian format), increment read position.
657 * @param[in, out] p Pointer into byte stream.
658 * @return Retrieved integer value, signed.
659 */
660static inline int16_t read_i16le_inc(const uint8_t **p)
661{
662 int16_t v;
663
664 if (!p || !*p)
665 return 0;
666 v = read_i16le(*p);
f1833600
GS
667 *p += sizeof(v);
668
669 return v;
670}
671
080b6bcf
GS
672/**
673 * Read unsigned 24bit integer from raw memory (little endian format), increment read position.
674 * @param[in, out] p Pointer into byte stream.
675 * @return Retrieved integer value, unsigned.
676 */
677static inline uint32_t read_u24le_inc(const uint8_t **p)
678{
679 uint32_t v;
680
681 if (!p || !*p)
682 return 0;
683 v = read_u24le(*p);
684 *p += 3 * sizeof(uint8_t);
685
686 return v;
687}
688
e4bcc63d
GS
689/**
690 * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
691 * @param[in, out] p Pointer into byte stream.
692 * @return Retrieved integer value, unsigned.
693 */
694static inline uint32_t read_u32be_inc(const uint8_t **p)
695{
696 uint32_t v;
697
698 if (!p || !*p)
699 return 0;
700 v = read_u32be(*p);
701 *p += sizeof(v);
702
703 return v;
704}
705
f1833600
GS
706/**
707 * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
708 * @param[in, out] p Pointer into byte stream.
709 * @return Retrieved integer value, unsigned.
710 */
711static inline uint32_t read_u32le_inc(const uint8_t **p)
712{
713 uint32_t v;
714
715 if (!p || !*p)
716 return 0;
717 v = read_u32le(*p);
718 *p += sizeof(v);
719
720 return v;
721}
722
e4bcc63d
GS
723/**
724 * Read signed 32bit integer from raw memory (big endian format), increment read position.
725 * @param[in, out] p Pointer into byte stream.
726 * @return Retrieved integer value, signed.
727 */
728static inline int32_t read_i32be_inc(const uint8_t **p)
729{
730 int32_t v;
731
732 if (!p || !*p)
733 return 0;
734 v = read_i32be(*p);
735 *p += sizeof(v);
736
737 return v;
738}
739
740/**
741 * Read signed 32bit integer from raw memory (little endian format), increment read position.
742 * @param[in, out] p Pointer into byte stream.
743 * @return Retrieved integer value, signed.
744 */
745static inline int32_t read_i32le_inc(const uint8_t **p)
746{
747 int32_t v;
748
749 if (!p || !*p)
750 return 0;
751 v = read_i32le(*p);
752 *p += sizeof(v);
753
754 return v;
755}
756
f1833600
GS
757/**
758 * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
759 * @param[in, out] p Pointer into byte stream.
760 * @return Retrieved integer value, unsigned.
761 */
762static inline uint64_t read_u64be_inc(const uint8_t **p)
763{
764 uint64_t v;
765
766 if (!p || !*p)
767 return 0;
768 v = read_u64be(*p);
769 *p += sizeof(v);
770
771 return v;
772}
773
774/**
775 * Read unsigned 64bit integer from raw memory (little endian format), increment read position.
776 * @param[in, out] p Pointer into byte stream.
777 * @return Retrieved integer value, unsigned.
778 */
779static inline uint64_t read_u64le_inc(const uint8_t **p)
780{
781 uint64_t v;
782
783 if (!p || !*p)
784 return 0;
785 v = read_u64le(*p);
786 *p += sizeof(v);
787
788 return v;
789}
790
e4bcc63d
GS
791/**
792 * Read 32bit float from raw memory (big endian format), increment read position.
793 * @param[in, out] p Pointer into byte stream.
794 * @return Retrieved float value.
795 */
796static inline float read_fltbe_inc(const uint8_t **p)
797{
798 float v;
799
800 if (!p || !*p)
801 return 0;
802 v = read_fltbe(*p);
803 *p += sizeof(v);
804
805 return v;
806}
807
daa895cb
GS
808/**
809 * Read 32bit float from raw memory (little endian format), increment read position.
810 * @param[in, out] p Pointer into byte stream.
811 * @return Retrieved float value.
812 */
813static inline float read_fltle_inc(const uint8_t **p)
814{
815 float v;
816
817 if (!p || !*p)
818 return 0;
819 v = read_fltle(*p);
820 *p += sizeof(v);
821
822 return v;
823}
824
e4bcc63d
GS
825/**
826 * Read 64bit float from raw memory (big endian format), increment read position.
827 * @param[in, out] p Pointer into byte stream.
828 * @return Retrieved float value.
829 */
830static inline double read_dblbe_inc(const uint8_t **p)
831{
832 double v;
833
834 if (!p || !*p)
835 return 0;
836 v = read_dblbe(*p);
837 *p += sizeof(v);
838
839 return v;
840}
841
daa895cb
GS
842/**
843 * Read 64bit float from raw memory (little endian format), increment read position.
844 * @param[in, out] p Pointer into byte stream.
845 * @return Retrieved float value.
846 */
847static inline double read_dblle_inc(const uint8_t **p)
848{
849 double v;
850
851 if (!p || !*p)
852 return 0;
853 v = read_dblle(*p);
854 *p += sizeof(v);
855
856 return v;
857}
858
f1833600
GS
859/**
860 * Write unsigned 8bit integer to raw memory, increment write position.
861 * @param[in, out] p Pointer into byte stream.
862 * @param[in] x Value to write.
863 */
864static inline void write_u8_inc(uint8_t **p, uint8_t x)
865{
866 if (!p || !*p)
867 return;
868 write_u8(*p, x);
869 *p += sizeof(x);
870}
871
872/**
873 * Write unsigned 16bit big endian integer to raw memory, increment write position.
874 * @param[in, out] p Pointer into byte stream.
875 * @param[in] x Value to write.
876 */
877static inline void write_u16be_inc(uint8_t **p, uint16_t x)
878{
879 if (!p || !*p)
880 return;
881 write_u16be(*p, x);
882 *p += sizeof(x);
883}
884
885/**
886 * Write unsigned 16bit little endian integer to raw memory, increment write position.
887 * @param[in, out] p Pointer into byte stream.
888 * @param[in] x Value to write.
889 */
890static inline void write_u16le_inc(uint8_t **p, uint16_t x)
891{
892 if (!p || !*p)
893 return;
894 write_u16le(*p, x);
895 *p += sizeof(x);
896}
897
898/**
899 * Write unsigned 32bit big endian integer to raw memory, increment write position.
900 * @param[in, out] p Pointer into byte stream.
901 * @param[in] x Value to write.
902 */
903static inline void write_u32be_inc(uint8_t **p, uint32_t x)
904{
905 if (!p || !*p)
906 return;
907 write_u32be(*p, x);
908 *p += sizeof(x);
909}
910
911/**
912 * Write unsigned 32bit little endian integer to raw memory, increment write position.
913 * @param[in, out] p Pointer into byte stream.
914 * @param[in] x Value to write.
915 */
916static inline void write_u32le_inc(uint8_t **p, uint32_t x)
917{
918 if (!p || !*p)
919 return;
920 write_u32le(*p, x);
921 *p += sizeof(x);
922}
a2632bca 923
797c8d90
GS
924/**
925 * Write unsigned 48bit little endian integer to raw memory, increment write position.
926 * @param[in, out] p Pointer into byte stream.
927 * @param[in] x Value to write.
928 */
929static inline void write_u48le_inc(uint8_t **p, uint64_t x)
930{
931 if (!p || !*p)
932 return;
933 write_u48le(*p, x);
934 *p += 48 / 8 * sizeof(uint8_t);
935}
936
daa895cb
GS
937/**
938 * Write unsigned 64bit little endian integer to raw memory, increment write position.
939 * @param[in, out] p Pointer into byte stream.
940 * @param[in] x Value to write.
941 */
942static inline void write_u64le_inc(uint8_t **p, uint64_t x)
943{
944 if (!p || !*p)
945 return;
946 write_u64le(*p, x);
947 *p += sizeof(x);
948}
949
950/**
951 * Write single precision little endian float to raw memory, increment write position.
952 * @param[in, out] p Pointer into byte stream.
953 * @param[in] x Value to write.
954 */
955static inline void write_fltle_inc(uint8_t **p, float x)
956{
957 if (!p || !*p)
958 return;
959 write_fltle(*p, x);
960 *p += sizeof(x);
961}
962
963/**
964 * Write double precision little endian float to raw memory, increment write position.
965 * @param[in, out] p Pointer into byte stream.
966 * @param[in] x Value to write.
967 */
968static inline void write_dblle_inc(uint8_t **p, double x)
969{
970 if (!p || !*p)
971 return;
972 write_dblle(*p, x);
973 *p += sizeof(x);
974}
975
6bf4273e
UH
976/* Portability fixes for FreeBSD. */
977#ifdef __FreeBSD__
978#define LIBUSB_CLASS_APPLICATION 0xfe
15eea61a 979#define libusb_has_capability(x) 0
6bf4273e
UH
980#define libusb_handle_events_timeout_completed(ctx, tv, c) \
981 libusb_handle_events_timeout(ctx, tv)
982#endif
983
1685c276
BV
984/* Static definitions of structs ending with an all-zero entry are a
985 * problem when compiling with -Wmissing-field-initializers: GCC
986 * suppresses the warning only with { 0 }, clang wants { } */
987#ifdef __clang__
988#define ALL_ZERO { }
989#else
990#define ALL_ZERO { 0 }
991#endif
992
dd5c48a6
LPC
993#ifdef __APPLE__
994#define SR_DRIVER_LIST_SECTION "__DATA,__sr_driver_list"
995#else
02a8c07d 996#define SR_DRIVER_LIST_SECTION "__sr_driver_list"
dd5c48a6
LPC
997#endif
998
3decd3b1
GS
999#if !defined SR_DRIVER_LIST_NOREORDER && defined __has_attribute
1000#if __has_attribute(no_reorder)
1001#define SR_DRIVER_LIST_NOREORDER __attribute__((no_reorder))
1002#endif
1003#endif
1004#if !defined SR_DRIVER_LIST_NOREORDER
1005#define SR_DRIVER_LIST_NOREORDER /* EMPTY */
1006#endif
1007
dd5c48a6
LPC
1008/**
1009 * Register a list of hardware drivers.
1010 *
1011 * This macro can be used to register multiple hardware drivers to the library.
1012 * This is useful when a driver supports multiple similar but slightly
1013 * different devices that require different sr_dev_driver struct definitions.
1014 *
1015 * For registering only a single driver see SR_REGISTER_DEV_DRIVER().
1016 *
1017 * Example:
1018 * @code{c}
1019 * #define MY_DRIVER(_name) \
1020 * &(struct sr_dev_driver){ \
1021 * .name = _name, \
1022 * ...
1023 * };
1024 *
1025 * SR_REGISTER_DEV_DRIVER_LIST(my_driver_infos,
1026 * MY_DRIVER("driver 1"),
1027 * MY_DRIVER("driver 2"),
1028 * ...
1029 * );
1030 * @endcode
1031 *
1032 * @param name Name to use for the driver list identifier.
1033 * @param ... Comma separated list of pointers to sr_dev_driver structs.
1034 */
1035#define SR_REGISTER_DEV_DRIVER_LIST(name, ...) \
1036 static const struct sr_dev_driver *name[] \
3decd3b1 1037 SR_DRIVER_LIST_NOREORDER \
dd5c48a6
LPC
1038 __attribute__((section (SR_DRIVER_LIST_SECTION), used, \
1039 aligned(sizeof(struct sr_dev_driver *)))) \
1040 = { \
1041 __VA_ARGS__ \
1042 };
1043
1044/**
1045 * Register a hardware driver.
1046 *
1047 * This macro is used to register a hardware driver with the library. It has
1048 * to be used in order to make the driver accessible to applications using the
1049 * library.
1050 *
1051 * The macro invocation should be placed directly under the struct
1052 * sr_dev_driver definition.
1053 *
1054 * Example:
1055 * @code{c}
1056 * static struct sr_dev_driver driver_info = {
1057 * .name = "driver",
1058 * ....
1059 * };
1060 * SR_REGISTER_DEV_DRIVER(driver_info);
1061 * @endcode
1062 *
1063 * @param name Identifier name of sr_dev_driver struct to register.
1064 */
1065#define SR_REGISTER_DEV_DRIVER(name) \
1066 SR_REGISTER_DEV_DRIVER_LIST(name##_list, &name);
1067
ced48274 1068SR_API void sr_drivers_init(struct sr_context *context);
5d8b3913 1069
b8072700 1070struct sr_context {
032da34b 1071 struct sr_dev_driver **driver_list;
785b9ff2
PS
1072#ifdef HAVE_LIBUSB_1_0
1073 libusb_context *libusb_ctx;
1074#endif
bee24666
DE
1075 sr_resource_open_callback resource_open_cb;
1076 sr_resource_close_callback resource_close_cb;
1077 sr_resource_read_callback resource_read_cb;
1078 void *resource_cb_data;
b8072700
PS
1079};
1080
20e88821
BV
1081/** Input module metadata keys. */
1082enum sr_input_meta_keys {
1083 /** The input filename, if there is one. */
1084 SR_INPUT_META_FILENAME = 0x01,
1085 /** The input file's size in bytes. */
1086 SR_INPUT_META_FILESIZE = 0x02,
1087 /** The first 128 bytes of the file, provided as a GString. */
1088 SR_INPUT_META_HEADER = 0x04,
20e88821
BV
1089
1090 /** The module cannot identify a file without this metadata. */
1091 SR_INPUT_META_REQUIRED = 0x80,
1092};
1093
d514d35d
BV
1094/** Input (file) module struct. */
1095struct sr_input {
1096 /**
1097 * A pointer to this input module's 'struct sr_input_module'.
d514d35d 1098 */
17bfaca6
BV
1099 const struct sr_input_module *module;
1100 GString *buf;
d514d35d 1101 struct sr_dev_inst *sdi;
d0181813 1102 gboolean sdi_ready;
17bfaca6 1103 void *priv;
d514d35d
BV
1104};
1105
1106/** Input (file) module driver. */
1107struct sr_input_module {
17bfaca6 1108 /**
d65fcbcd 1109 * A unique ID for this input module, suitable for use in command-line
17bfaca6
BV
1110 * clients, [a-z0-9-]. Must not be NULL.
1111 */
1112 const char *id;
d514d35d
BV
1113
1114 /**
d65fcbcd 1115 * A unique name for this input module, suitable for use in GUI
17bfaca6 1116 * clients, can contain UTF-8. Must not be NULL.
d514d35d 1117 */
17bfaca6 1118 const char *name;
d514d35d
BV
1119
1120 /**
d65fcbcd 1121 * A short description of the input module. Must not be NULL.
d514d35d 1122 *
d65fcbcd 1123 * This can be displayed by frontends, e.g. when selecting the input
17bfaca6
BV
1124 * module for saving a file.
1125 */
1126 const char *desc;
1127
c7bc82ff
JH
1128 /**
1129 * A NULL terminated array of strings containing a list of file name
1130 * extensions typical for the input file format, or NULL if there is
1131 * no typical extension for this file format.
1132 */
1133 const char *const *exts;
1134
17bfaca6
BV
1135 /**
1136 * Zero-terminated list of metadata items the module needs to be able
1137 * to identify an input stream. Can be all-zero, if the module cannot
1138 * identify streams at all, i.e. has to be forced into use.
1139 *
1140 * Each item is one of:
1141 * SR_INPUT_META_FILENAME
1142 * SR_INPUT_META_FILESIZE
1143 * SR_INPUT_META_HEADER
17bfaca6
BV
1144 *
1145 * If the high bit (SR_INPUT META_REQUIRED) is set, the module cannot
1146 * identify a stream without the given metadata.
1147 */
1148 const uint8_t metadata[8];
1149
1150 /**
1151 * Returns a NULL-terminated list of options this module can take.
1152 * Can be NULL, if the module has no options.
1153 */
2c240774 1154 const struct sr_option *(*options) (void);
17bfaca6
BV
1155
1156 /**
1157 * Check if this input module can load and parse the specified stream.
1158 *
1159 * @param[in] metadata Metadata the module can use to identify the stream.
54ee427d
GS
1160 * @param[out] confidence "Strength" of the detection.
1161 * Specialized handlers can take precedence over generic/basic support.
d514d35d 1162 *
4f979115 1163 * @retval SR_OK This module knows the format.
753793ef 1164 * @retval SR_ERR_NA There wasn't enough data for this module to
4f979115 1165 * positively identify the format.
d9251a2c
UH
1166 * @retval SR_ERR_DATA This module knows the format, but cannot handle
1167 * it. This means the stream is either corrupt, or indicates a
1168 * feature that the module does not support.
4f979115 1169 * @retval SR_ERR This module does not know the format.
54ee427d
GS
1170 *
1171 * Lower numeric values of 'confidence' mean that the input module
1172 * stronger believes in its capability to handle this specific format.
1173 * This way, multiple input modules can claim support for a format,
1174 * and the application can pick the best match, or try fallbacks
1175 * in case of errors. This approach also copes with formats that
1176 * are unreliable to detect in the absence of magic signatures.
d514d35d 1177 */
54ee427d 1178 int (*format_match) (GHashTable *metadata, unsigned int *confidence);
d514d35d
BV
1179
1180 /**
1181 * Initialize the input module.
1182 *
d514d35d
BV
1183 * @retval SR_OK Success
1184 * @retval other Negative error code.
1185 */
17bfaca6 1186 int (*init) (struct sr_input *in, GHashTable *options);
d514d35d
BV
1187
1188 /**
753793ef 1189 * Send data to the specified input instance.
d514d35d 1190 *
753793ef
BV
1191 * When an input module instance is created with sr_input_new(), this
1192 * function is used to feed data to the instance.
d514d35d 1193 *
753793ef
BV
1194 * As enough data gets fed into this function to completely populate
1195 * the device instance associated with this input instance, this is
1196 * guaranteed to return the moment it's ready. This gives the caller
1197 * the chance to examine the device instance, attach session callbacks
1198 * and so on.
17bfaca6
BV
1199 *
1200 * @retval SR_OK Success
1201 * @retval other Negative error code.
1202 */
d0181813 1203 int (*receive) (struct sr_input *in, GString *buf);
17bfaca6 1204
753793ef
BV
1205 /**
1206 * Signal the input module no more data will come.
1207 *
1208 * This will cause the module to process any data it may have buffered.
1209 * The SR_DF_END packet will also typically be sent at this time.
1210 */
7066fd46
BV
1211 int (*end) (struct sr_input *in);
1212
d9251a2c
UH
1213 /**
1214 * Reset the input module's input handling structures.
1215 *
1216 * Causes the input module to reset its internal state so that we can
1217 * re-send the input data from the beginning without having to
1218 * re-create the entire input module.
1219 *
1220 * @retval SR_OK Success.
1221 * @retval other Negative error code.
1222 */
b6b4f03e
SA
1223 int (*reset) (struct sr_input *in);
1224
17bfaca6
BV
1225 /**
1226 * This function is called after the caller is finished using
1227 * the input module, and can be used to free any internal
1228 * resources the module may keep.
d514d35d 1229 *
753793ef
BV
1230 * This function is optional.
1231 *
d514d35d
BV
1232 * @retval SR_OK Success
1233 * @retval other Negative error code.
1234 */
d5cc282f 1235 void (*cleanup) (struct sr_input *in);
d514d35d
BV
1236};
1237
a755b0e1
BV
1238/** Output module instance. */
1239struct sr_output {
d9251a2c 1240 /** A pointer to this output's module. */
a755b0e1
BV
1241 const struct sr_output_module *module;
1242
1243 /**
1244 * The device for which this output module is creating output. This
1245 * can be used by the module to find out channel names and numbers.
1246 */
1247 const struct sr_dev_inst *sdi;
1248
81b3ce37
SA
1249 /**
1250 * The name of the file that the data should be written to.
1251 */
1252 const char *filename;
1253
a755b0e1
BV
1254 /**
1255 * A generic pointer which can be used by the module to keep internal
1256 * state between calls into its callback functions.
1257 *
1258 * For example, the module might store a pointer to a chunk of output
1259 * there, and only flush it when it reaches a certain size.
1260 */
d686c5ec 1261 void *priv;
a755b0e1
BV
1262};
1263
1264/** Output module driver. */
1265struct sr_output_module {
1266 /**
1267 * A unique ID for this output module, suitable for use in command-line
1268 * clients, [a-z0-9-]. Must not be NULL.
1269 */
2c240774 1270 const char *id;
a755b0e1
BV
1271
1272 /**
1273 * A unique name for this output module, suitable for use in GUI
1274 * clients, can contain UTF-8. Must not be NULL.
1275 */
1276 const char *name;
1277
1278 /**
1279 * A short description of the output module. Must not be NULL.
1280 *
1281 * This can be displayed by frontends, e.g. when selecting the output
1282 * module for saving a file.
1283 */
2c240774 1284 const char *desc;
a755b0e1 1285
8a174d23
JH
1286 /**
1287 * A NULL terminated array of strings containing a list of file name
1288 * extensions typical for the input file format, or NULL if there is
1289 * no typical extension for this file format.
1290 */
1291 const char *const *exts;
1292
3cd4b381
SA
1293 /**
1294 * Bitfield containing flags that describe certain properties
1295 * this output module may or may not have.
1296 * @see sr_output_flags
1297 */
1298 const uint64_t flags;
1299
a755b0e1
BV
1300 /**
1301 * Returns a NULL-terminated list of options this module can take.
1302 * Can be NULL, if the module has no options.
a755b0e1 1303 */
af7d656d 1304 const struct sr_option *(*options) (void);
a755b0e1
BV
1305
1306 /**
1307 * This function is called once, at the beginning of an output stream.
1308 *
1309 * The device struct will be available in the output struct passed in,
1310 * as well as the param field -- which may be NULL or an empty string,
1311 * if no parameter was passed.
1312 *
1313 * The module can use this to initialize itself, create a struct for
1314 * keeping state and storing it in the <code>internal</code> field.
1315 *
1316 * @param o Pointer to the respective 'struct sr_output'.
1317 *
1318 * @retval SR_OK Success
1319 * @retval other Negative error code.
1320 */
1321 int (*init) (struct sr_output *o, GHashTable *options);
1322
1323 /**
f3f19d11 1324 * This function is passed a copy of every packet in the data feed.
a755b0e1
BV
1325 * Any output generated by the output module in response to the
1326 * packet should be returned in a newly allocated GString
1327 * <code>out</code>, which will be freed by the caller.
1328 *
1329 * Packets not of interest to the output module can just be ignored,
1330 * and the <code>out</code> parameter set to NULL.
1331 *
1332 * @param o Pointer to the respective 'struct sr_output'.
1333 * @param sdi The device instance that generated the packet.
1334 * @param packet The complete packet.
1335 * @param out A pointer where a GString * should be stored if
1336 * the module generates output, or NULL if not.
1337 *
1338 * @retval SR_OK Success
1339 * @retval other Negative error code.
1340 */
1341 int (*receive) (const struct sr_output *o,
1342 const struct sr_datafeed_packet *packet, GString **out);
1343
1344 /**
1345 * This function is called after the caller is finished using
1346 * the output module, and can be used to free any internal
1347 * resources the module may keep.
1348 *
1349 * @retval SR_OK Success
1350 * @retval other Negative error code.
1351 */
1352 int (*cleanup) (struct sr_output *o);
1353};
1354
790320f6
UH
1355/** Transform module instance. */
1356struct sr_transform {
d9251a2c 1357 /** A pointer to this transform's module. */
790320f6
UH
1358 const struct sr_transform_module *module;
1359
1360 /**
1361 * The device for which this transform module is used. This
1362 * can be used by the module to find out channel names and numbers.
1363 */
1364 const struct sr_dev_inst *sdi;
1365
1366 /**
1367 * A generic pointer which can be used by the module to keep internal
1368 * state between calls into its callback functions.
1369 */
1370 void *priv;
1371};
1372
1373struct sr_transform_module {
1374 /**
1375 * A unique ID for this transform module, suitable for use in
1376 * command-line clients, [a-z0-9-]. Must not be NULL.
1377 */
2c240774 1378 const char *id;
790320f6
UH
1379
1380 /**
1381 * A unique name for this transform module, suitable for use in GUI
1382 * clients, can contain UTF-8. Must not be NULL.
1383 */
1384 const char *name;
1385
1386 /**
1387 * A short description of the transform module. Must not be NULL.
1388 *
1389 * This can be displayed by frontends, e.g. when selecting
1390 * which transform module(s) to add.
1391 */
2c240774 1392 const char *desc;
790320f6
UH
1393
1394 /**
1395 * Returns a NULL-terminated list of options this transform module
1396 * can take. Can be NULL, if the transform module has no options.
1397 */
1398 const struct sr_option *(*options) (void);
1399
1400 /**
1401 * This function is called once, at the beginning of a stream.
1402 *
1403 * @param t Pointer to the respective 'struct sr_transform'.
1404 * @param options Hash table of options for this transform module.
1405 * Can be NULL if no options are to be used.
1406 *
1407 * @retval SR_OK Success
1408 * @retval other Negative error code.
1409 */
1410 int (*init) (struct sr_transform *t, GHashTable *options);
1411
1412 /**
1413 * This function is passed a pointer to every packet in the data feed.
1414 *
1415 * It can either return (in packet_out) a pointer to another packet
1416 * (possibly the exact same packet it got as input), or NULL.
1417 *
1418 * @param t Pointer to the respective 'struct sr_transform'.
1419 * @param packet_in Pointer to a datafeed packet.
1420 * @param packet_out Pointer to the resulting datafeed packet after
1421 * this function was run. If NULL, the transform
1422 * module intentionally didn't output a new packet.
1423 *
1424 * @retval SR_OK Success
1425 * @retval other Negative error code.
1426 */
1427 int (*receive) (const struct sr_transform *t,
1428 struct sr_datafeed_packet *packet_in,
1429 struct sr_datafeed_packet **packet_out);
1430
1431 /**
1432 * This function is called after the caller is finished using
1433 * the transform module, and can be used to free any internal
1434 * resources the module may keep.
1435 *
1436 * @retval SR_OK Success
1437 * @retval other Negative error code.
1438 */
1439 int (*cleanup) (struct sr_transform *t);
1440};
1441
69890f73 1442#ifdef HAVE_LIBUSB_1_0
04cb9157 1443/** USB device instance */
d68e2d1a 1444struct sr_usb_dev_inst {
98582bf5
BV
1445 /** USB bus */
1446 uint8_t bus;
1447 /** Device address on USB bus */
1448 uint8_t address;
1449 /** libusb device handle */
1450 struct libusb_device_handle *devhdl;
69890f73
UH
1451};
1452#endif
1453
1ac8c218 1454struct sr_serial_dev_inst;
1df81f4b 1455#ifdef HAVE_SERIAL_COMM
a7b8692e 1456struct ser_lib_functions;
edec0436 1457struct ser_hid_chip_functions;
b79c3422 1458struct sr_bt_desc;
d4787248
GS
1459typedef void (*serial_rx_chunk_callback)(struct sr_serial_dev_inst *serial,
1460 void *cb_data, const void *buf, size_t count);
d68e2d1a 1461struct sr_serial_dev_inst {
98582bf5
BV
1462 /** Port name, e.g. '/dev/tty42'. */
1463 char *port;
1464 /** Comm params for serial_set_paramstr(). */
1465 char *serialcomm;
a7b8692e 1466 struct ser_lib_functions *lib_funcs;
639c6f61
GS
1467 struct {
1468 int bit_rate;
1469 int data_bits;
1470 int parity_bits;
1471 int stop_bits;
1472 } comm_params;
ad5aa993 1473 GString *rcv_buffer;
d4787248
GS
1474 serial_rx_chunk_callback rx_chunk_cb_func;
1475 void *rx_chunk_cb_data;
a7b8692e 1476#ifdef HAVE_LIBSERIALPORT
98582bf5 1477 /** libserialport port handle */
271392d9 1478 struct sp_port *sp_data;
a7b8692e 1479#endif
4417074c 1480#ifdef HAVE_LIBHIDAPI
edec0436
GS
1481 enum ser_hid_chip_t {
1482 SER_HID_CHIP_UNKNOWN, /**!< place holder */
a10284cd 1483 SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */
616bc3a1 1484 SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */
0cdb72c8 1485 SER_HID_CHIP_VICTOR_DMM, /**!< Victor 70/86 DMM cable */
828eeea2 1486 SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */
edec0436
GS
1487 SER_HID_CHIP_LAST, /**!< sentinel */
1488 } hid_chip;
1489 struct ser_hid_chip_functions *hid_chip_funcs;
1490 char *usb_path;
1491 char *usb_serno;
1492 const char *hid_path;
4417074c 1493 hid_device *hid_dev;
edec0436 1494 GSList *hid_source_args;
4417074c 1495#endif
b79c3422
GS
1496#ifdef HAVE_BLUETOOTH
1497 enum ser_bt_conn_t {
1498 SER_BT_CONN_UNKNOWN, /**!< place holder */
1499 SER_BT_CONN_RFCOMM, /**!< BT classic, RFCOMM channel */
1500 SER_BT_CONN_BLE122, /**!< BLE, BLE122 module, indications */
1501 SER_BT_CONN_NRF51, /**!< BLE, Nordic nRF51, notifications */
1502 SER_BT_CONN_CC254x, /**!< BLE, TI CC254x, notifications */
1503 SER_BT_CONN_MAX, /**!< sentinel */
1504 } bt_conn_type;
1505 char *bt_addr_local;
1506 char *bt_addr_remote;
1507 size_t bt_rfcomm_channel;
1508 uint16_t bt_notify_handle_read;
1509 uint16_t bt_notify_handle_write;
1510 uint16_t bt_notify_handle_cccd;
1511 uint16_t bt_notify_value_cccd;
1512 struct sr_bt_desc *bt_desc;
1513 GSList *bt_source_args;
1514#endif
69890f73 1515};
c4f2dfd0 1516#endif
69890f73 1517
ae67644f
ML
1518struct sr_usbtmc_dev_inst {
1519 char *device;
1520 int fd;
1521};
1522
026c822d
UH
1523/* Private driver context. */
1524struct drv_context {
98582bf5
BV
1525 /** sigrok context */
1526 struct sr_context *sr_ctx;
026c822d
UH
1527 GSList *instances;
1528};
1529
48a486cd
BV
1530/*--- log.c -----------------------------------------------------------------*/
1531
00f0016c 1532#if defined(_WIN32) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
7419638d
DE
1533/*
1534 * On MinGW, we need to specify the gnu_printf format flavor or GCC
1535 * will assume non-standard Microsoft printf syntax.
1536 */
1537SR_PRIV int sr_log(int loglevel, const char *format, ...)
1538 __attribute__((__format__ (__gnu_printf__, 2, 3)));
1539#else
be92d5b4 1540SR_PRIV int sr_log(int loglevel, const char *format, ...) G_GNUC_PRINTF(2, 3);
7419638d 1541#endif
48a486cd 1542
3544f848 1543/* Message logging helpers with subsystem-specific prefix string. */
be92d5b4
DE
1544#define sr_spew(...) sr_log(SR_LOG_SPEW, LOG_PREFIX ": " __VA_ARGS__)
1545#define sr_dbg(...) sr_log(SR_LOG_DBG, LOG_PREFIX ": " __VA_ARGS__)
1546#define sr_info(...) sr_log(SR_LOG_INFO, LOG_PREFIX ": " __VA_ARGS__)
1547#define sr_warn(...) sr_log(SR_LOG_WARN, LOG_PREFIX ": " __VA_ARGS__)
1548#define sr_err(...) sr_log(SR_LOG_ERR, LOG_PREFIX ": " __VA_ARGS__)
3544f848 1549
48a486cd
BV
1550/*--- device.c --------------------------------------------------------------*/
1551
cffdc3e6
ML
1552/** Scan options supported by a driver. */
1553#define SR_CONF_SCAN_OPTIONS 0x7FFF0000
1554
1555/** Device options for a particular device. */
1556#define SR_CONF_DEVICE_OPTIONS 0x7FFF0001
1557
e318f01b
ML
1558/** Mask for separating config keys from capabilities. */
1559#define SR_CONF_MASK 0x1fffffff
1560
f3ca73ed 1561/** Values for the changes argument of sr_dev_driver.config_channel_set. */
2a854d71 1562enum {
fca75cbb 1563 /** The enabled state of the channel has been changed. */
3f239f08 1564 SR_CHANNEL_SET_ENABLED = 1 << 0,
2a854d71
DE
1565};
1566
5e23fcab
ML
1567SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
1568 int index, int type, gboolean enabled, const char *name);
fe71c7e4
GS
1569SR_PRIV void sr_channel_free(struct sr_channel *ch);
1570SR_PRIV void sr_channel_free_cb(void *p);
9c24d16a
BV
1571SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi,
1572 struct sr_channel *cur_channel);
712f981d
GS
1573SR_PRIV gboolean sr_channels_differ(struct sr_channel *ch1, struct sr_channel *ch2);
1574SR_PRIV gboolean sr_channel_lists_differ(GSList *l1, GSList *l2);
48a486cd 1575
96727ef0
UH
1576/** Device instance data */
1577struct sr_dev_inst {
1578 /** Device driver. */
1579 struct sr_dev_driver *driver;
1580 /** Device instance status. SR_ST_NOT_FOUND, etc. */
1581 int status;
1582 /** Device instance type. SR_INST_USB, etc. */
1583 int inst_type;
1584 /** Device vendor. */
1585 char *vendor;
1586 /** Device model. */
1587 char *model;
1588 /** Device version. */
1589 char *version;
1590 /** Serial number. */
1591 char *serial_num;
1592 /** Connection string to uniquely identify devices. */
1593 char *connection_id;
1594 /** List of channels. */
1595 GSList *channels;
1596 /** List of sr_channel_group structs */
1597 GSList *channel_groups;
1598 /** Device instance connection data (used?) */
1599 void *conn;
1600 /** Device instance private data (used?) */
1601 void *priv;
1602 /** Session to which this device is currently assigned. */
1603 struct sr_session *session;
1604};
1605
48a486cd 1606/* Generic device instances */
48a486cd 1607SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi);
48a486cd 1608
545f9786 1609#ifdef HAVE_LIBUSB_1_0
69890f73 1610/* USB-specific instances */
d68e2d1a 1611SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
69890f73 1612 uint8_t address, struct libusb_device_handle *hdl);
d68e2d1a 1613SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb);
69890f73
UH
1614#endif
1615
1df81f4b 1616#ifdef HAVE_SERIAL_COMM
1ac8c218
GS
1617#ifndef HAVE_LIBSERIALPORT
1618/*
1619 * Some identifiers which initially got provided by libserialport are
1620 * used internally within the libsigrok serial layer's implementation,
1621 * while libserialport no longer is the exclusive provider of serial
1622 * communication support. Declare the identifiers here so they remain
1623 * available across all build configurations.
1624 */
1625enum libsp_parity {
1626 SP_PARITY_NONE = 0,
1627 SP_PARITY_ODD = 1,
1628 SP_PARITY_EVEN = 2,
1629 SP_PARITY_MARK = 3,
1630 SP_PARITY_SPACE = 4,
1631};
1632
1633enum libsp_flowcontrol {
1634 SP_FLOWCONTROL_NONE = 0,
1635 SP_FLOWCONTROL_XONXOFF = 1,
1636 SP_FLOWCONTROL_RTSCTS = 2,
1637 SP_FLOWCONTROL_DTRDSR = 3,
1638};
1639#endif
1640
69890f73 1641/* Serial-specific instances */
299bdb24
BV
1642SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
1643 const char *serialcomm);
d68e2d1a 1644SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial);
c4f2dfd0 1645#endif
69890f73 1646
ae67644f
ML
1647/* USBTMC-specific instances */
1648SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device);
1649SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
b08024a8 1650
c09f0b57 1651/*--- hwdriver.c ------------------------------------------------------------*/
996b0c72 1652
13fef1ed 1653SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
584560f1 1654SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
032da34b 1655SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
584560f1 1656SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
722db131 1657SR_PRIV void sr_config_free(struct sr_config *src);
f670835f 1658SR_PRIV int sr_dev_acquisition_start(struct sr_dev_inst *sdi);
d2f7c417 1659SR_PRIV int sr_dev_acquisition_stop(struct sr_dev_inst *sdi);
996b0c72 1660
a1645fcd
BV
1661/*--- session.c -------------------------------------------------------------*/
1662
e2b23821 1663struct sr_session {
4ed5d21d
ML
1664 /** Context this session exists in. */
1665 struct sr_context *ctx;
c0e3ba4b 1666 /** List of struct sr_dev_inst pointers. */
e2b23821 1667 GSList *devs;
1de3cced
ML
1668 /** List of struct sr_dev_inst pointers owned by this session. */
1669 GSList *owned_devs;
e2b23821
UH
1670 /** List of struct datafeed_callback pointers. */
1671 GSList *datafeed_callbacks;
c0a1e532 1672 GSList *transforms;
7b5e6d29 1673 struct sr_trigger *trigger;
62d7945f 1674
5de0fc55
DE
1675 /** Callback to invoke on session stop. */
1676 sr_session_stopped_callback stopped_callback;
1677 /** User data to be passed to the session stop callback. */
1678 void *stopped_cb_data;
1679
2e5e3df4 1680 /** Mutex protecting the main context pointer. */
c2bf5506
DE
1681 GMutex main_mutex;
1682 /** Context of the session main loop. */
1683 GMainContext *main_context;
e2b23821 1684
c2bf5506
DE
1685 /** Registered event sources for this session. */
1686 GHashTable *event_sources;
1687 /** Session main loop. */
1688 GMainLoop *main_loop;
5de0fc55
DE
1689 /** ID of idle source for dispatching the session stop notification. */
1690 unsigned int stop_check_id;
2e5e3df4
DE
1691 /** Whether the session has been started. */
1692 gboolean running;
e2b23821
UH
1693};
1694
62d7945f 1695SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
c2bf5506 1696 void *key, GSource *source);
92248e78 1697SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
c2bf5506
DE
1698 void *key);
1699SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
1700 void *key, GSource *source);
cbc1413f
DE
1701SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
1702 void *key, gintptr fd, int events, int timeout,
1703 sr_receive_data_callback cb, void *cb_data);
ee9953ef
DE
1704
1705SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
1706 int events, int timeout, sr_receive_data_callback cb, void *cb_data);
1707SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
1708 GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
1709 void *cb_data);
1710SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
1711 GIOChannel *channel, int events, int timeout,
1712 sr_receive_data_callback cb, void *cb_data);
1713SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
1714SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
1715 GPollFD *pollfd);
1716SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
1717 GIOChannel *channel);
1718
7d1a4a52
FS
1719SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi,
1720 uint32_t key, GVariant *var);
de4d3f99 1721SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
ae5859ff 1722 const struct sr_datafeed_packet *packet);
f438e0c9 1723SR_PRIV int sr_sessionfile_check(const char *filename);
7c69b528
SA
1724SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(const char *filename,
1725 struct sr_session **session);
a1645fcd 1726
98654c99
DE
1727/*--- session_file.c --------------------------------------------------------*/
1728
a6dc3dac
DE
1729#if !HAVE_ZIP_DISCARD
1730/* Replace zip_discard() if not available. */
1731#define zip_discard(zip) sr_zip_discard(zip)
1732SR_PRIV void sr_zip_discard(struct zip *archive);
1733#endif
1734
98654c99
DE
1735SR_PRIV GKeyFile *sr_sessionfile_read_metadata(struct zip *archive,
1736 const struct zip_stat *entry);
1737
41caa319
AJ
1738/*--- analog.c --------------------------------------------------------------*/
1739
edb691fc 1740SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
41caa319
AJ
1741 struct sr_analog_encoding *encoding,
1742 struct sr_analog_meaning *meaning,
1743 struct sr_analog_spec *spec,
1744 int digits);
1745
063e7aef
UH
1746/*--- std.c -----------------------------------------------------------------*/
1747
144f6660
UH
1748typedef int (*dev_close_callback)(struct sr_dev_inst *sdi);
1749typedef void (*std_dev_clear_callback)(void *priv);
cd2f0fe2 1750
c45c32ce 1751SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx);
700d6b64 1752SR_PRIV int std_cleanup(const struct sr_dev_driver *di);
4d67b9d9
UH
1753SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi);
1754SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi);
1755SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi);
1756SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi);
1df81f4b 1757#ifdef HAVE_SERIAL_COMM
23dc6661 1758SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi);
1b38775b 1759SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi);
c4f2dfd0 1760#endif
bee2b016
LPC
1761SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi);
1762SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi);
10cf8113 1763SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi);
7f7702b8
UH
1764SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi);
1765SR_PRIV int std_session_send_df_frame_end(const struct sr_dev_inst *sdi);
6e43c3d5 1766SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
144f6660 1767 std_dev_clear_callback clear_private);
f778bf02 1768SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver);
c01bf34c 1769SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di);
043e899a 1770SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi);
15a5bfe4 1771SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices);
4afdfd46 1772
e66d1892
UH
1773SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
1774 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
1775 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
1776 size_t drvsize, const uint32_t devopts[], size_t devsize);
1777
23772462
UH
1778extern SR_PRIV const uint32_t NO_OPTS[1];
1779
e66d1892
UH
1780#define STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts) \
1781 std_opts_config_list(key, data, sdi, cg, ARRAY_AND_SIZE(scanopts), \
1782 ARRAY_AND_SIZE(drvopts), ARRAY_AND_SIZE(devopts))
1783
58ffcf97 1784SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n);
db944f16 1785SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n);
463160cb
UH
1786SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n);
1787SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n);
54d471f4
UH
1788SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step);
1789SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]);
7bc3cfe6 1790SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double dmin, const double dmax, const double dstep);
db944f16 1791
a162eeb2 1792SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high);
43995cda 1793SR_PRIV GVariant *std_gvar_tuple_double(double low, double high);
a162eeb2 1794
769561cb
UH
1795SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n);
1796SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n);
1797SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n);
70635036 1798SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n);
db944f16 1799
94e64a0b 1800SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n);
9fb9afb5 1801
697fb6dd
UH
1802SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n);
1803SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n);
1804SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n);
1805
1806SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n);
1807SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n);
1808
1809SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n);
1810SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
1811SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
1812
fcd6a8bd
UH
1813SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
1814
c0aa074e
UH
1815SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
1816 int baudrate, int bits, int parity, int stopbits,
1817 int flowcontrol, int rts, int dtr);
3ad30b4e
GS
1818SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
1819 int rts, int dtr);
c0aa074e 1820
bee24666
DE
1821/*--- resource.c ------------------------------------------------------------*/
1822
7d89fd60
DE
1823SR_PRIV int64_t sr_file_get_size(FILE *file);
1824
bee24666
DE
1825SR_PRIV int sr_resource_open(struct sr_context *ctx,
1826 struct sr_resource *res, int type, const char *name)
1827 G_GNUC_WARN_UNUSED_RESULT;
1828SR_PRIV int sr_resource_close(struct sr_context *ctx,
1829 struct sr_resource *res);
32ba0d80 1830SR_PRIV gssize sr_resource_read(struct sr_context *ctx,
bee24666
DE
1831 const struct sr_resource *res, void *buf, size_t count)
1832 G_GNUC_WARN_UNUSED_RESULT;
1833SR_PRIV void *sr_resource_load(struct sr_context *ctx, int type,
1834 const char *name, size_t *size, size_t max_size)
1835 G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
1836
8d558c7a
UH
1837/*--- strutil.c -------------------------------------------------------------*/
1838
1839SR_PRIV int sr_atol(const char *str, long *ret);
97aa41e9 1840SR_PRIV int sr_atol_base(const char *str, long *ret, char **end, int base);
30903c40 1841SR_PRIV int sr_atoul_base(const char *str, unsigned long *ret, char **end, int base);
8d558c7a
UH
1842SR_PRIV int sr_atoi(const char *str, int *ret);
1843SR_PRIV int sr_atod(const char *str, double *ret);
1844SR_PRIV int sr_atof(const char *str, float *ret);
4f0463a0 1845SR_PRIV int sr_atod_ascii(const char *str, double *ret);
91ab2f64 1846SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
9806c2d5 1847SR_PRIV int sr_atof_ascii(const char *str, float *ret);
8d558c7a 1848
d8bc7ca3
GS
1849SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
1850SR_PRIV void sr_hexdump_free(GString *s);
1851
ac136b57
BV
1852/*--- soft-trigger.c --------------------------------------------------------*/
1853
1854struct soft_trigger_logic {
1855 const struct sr_dev_inst *sdi;
1856 const struct sr_trigger *trigger;
1857 int count;
1858 int unitsize;
1859 int cur_stage;
1860 uint8_t *prev_sample;
fe5a7355
AJ
1861 uint8_t *pre_trigger_buffer;
1862 uint8_t *pre_trigger_head;
1863 int pre_trigger_size;
1864 int pre_trigger_fill;
ac136b57
BV
1865};
1866
d1078180 1867SR_PRIV int logic_channel_unitsize(GSList *channels);
ac136b57 1868SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
fe5a7355
AJ
1869 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
1870 int pre_trigger_samples);
ac136b57
BV
1871SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *st);
1872SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *st, uint8_t *buf,
fe5a7355 1873 int len, int *pre_trigger_samples);
ac136b57 1874
fcfa36fd 1875/*--- serial.c --------------------------------------------------------------*/
058b7035 1876
1df81f4b 1877#ifdef HAVE_SERIAL_COMM
a54dd31e
UH
1878enum {
1879 SERIAL_RDWR = 1,
1880 SERIAL_RDONLY = 2,
a54dd31e
UH
1881};
1882
144f6660 1883typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
1a7adeac
GS
1884typedef int (*packet_valid_len_callback)(void *st,
1885 const uint8_t *p, size_t l, size_t *pl);
766456be 1886
ae4c1fb6
GS
1887typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
1888 const char *desc);
1889typedef GSList *(*sr_ser_find_append_t)(GSList *devs, const char *name);
1890
299bdb24
BV
1891SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);
1892SR_PRIV int serial_close(struct sr_serial_dev_inst *serial);
1893SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial);
bce75f94 1894SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial);
8c3df6e5 1895SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial);
9a474211 1896SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
eead2782 1897 const void *buf, size_t count, unsigned int timeout_ms);
9a474211
ML
1898SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
1899 const void *buf, size_t count);
9a474211 1900SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
eead2782 1901 size_t count, unsigned int timeout_ms);
9a474211
ML
1902SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
1903 size_t count);
d4787248
GS
1904SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
1905 serial_rx_chunk_callback cb, void *cb_data);
299bdb24 1906SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
71caaad4 1907 int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
3ad30b4e
GS
1908SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
1909 int rts, int dtr);
299bdb24
BV
1910SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
1911 const char *paramstr);
1912SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
1913 int *buflen, gint64 timeout_ms);
766456be 1914SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
1a7adeac
GS
1915 uint8_t *buf, size_t *buflen,
1916 size_t packet_size, packet_valid_callback is_valid,
1917 packet_valid_len_callback is_valid_len, size_t *return_size,
1918 uint64_t timeout_ms);
1bd9e678
DJ
1919SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
1920 const char **serial_options);
102f1239
BV
1921SR_PRIV int serial_source_add(struct sr_session *session,
1922 struct sr_serial_dev_inst *serial, int events, int timeout,
1923 sr_receive_data_callback cb, void *cb_data);
1924SR_PRIV int serial_source_remove(struct sr_session *session,
1925 struct sr_serial_dev_inst *serial);
b541f837 1926SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id);
c5cfc735 1927SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes);
ae4c1fb6 1928
ad5aa993
GS
1929SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial);
1930SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial);
1931SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
1932 const uint8_t *data, size_t len);
1933SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
1934 uint8_t *data, size_t len);
1935
a7b8692e
GS
1936struct ser_lib_functions {
1937 int (*open)(struct sr_serial_dev_inst *serial, int flags);
1938 int (*close)(struct sr_serial_dev_inst *serial);
1939 int (*flush)(struct sr_serial_dev_inst *serial);
1940 int (*drain)(struct sr_serial_dev_inst *serial);
1941 int (*write)(struct sr_serial_dev_inst *serial,
1942 const void *buf, size_t count,
1943 int nonblocking, unsigned int timeout_ms);
1944 int (*read)(struct sr_serial_dev_inst *serial,
1945 void *buf, size_t count,
1946 int nonblocking, unsigned int timeout_ms);
1947 int (*set_params)(struct sr_serial_dev_inst *serial,
1948 int baudrate, int bits, int parity, int stopbits,
1949 int flowcontrol, int rts, int dtr);
3ad30b4e
GS
1950 int (*set_handshake)(struct sr_serial_dev_inst *serial,
1951 int rts, int dtr);
a7b8692e
GS
1952 int (*setup_source_add)(struct sr_session *session,
1953 struct sr_serial_dev_inst *serial,
1954 int events, int timeout,
1955 sr_receive_data_callback cb, void *cb_data);
1956 int (*setup_source_remove)(struct sr_session *session,
1957 struct sr_serial_dev_inst *serial);
1958 GSList *(*list)(GSList *list, sr_ser_list_append_t append);
1959 GSList *(*find_usb)(GSList *list, sr_ser_find_append_t append,
1960 uint16_t vendor_id, uint16_t product_id);
1961 int (*get_frame_format)(struct sr_serial_dev_inst *serial,
1962 int *baud, int *bits);
1963 size_t (*get_rx_avail)(struct sr_serial_dev_inst *serial);
1964};
1965extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_libsp;
4417074c
GS
1966SR_PRIV int ser_name_is_hid(struct sr_serial_dev_inst *serial);
1967extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_hid;
b79c3422
GS
1968SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial);
1969extern SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt;
edec0436
GS
1970
1971#ifdef HAVE_LIBHIDAPI
1972struct vid_pid_item {
1973 uint16_t vid, pid;
1974};
edec0436
GS
1975
1976struct ser_hid_chip_functions {
1977 const char *chipname;
1978 const char *chipdesc;
1979 const struct vid_pid_item *vid_pid_items;
1980 const int max_bytes_per_request;
1981 int (*set_params)(struct sr_serial_dev_inst *serial,
1982 int baudrate, int bits, int parity, int stopbits,
1983 int flowcontrol, int rts, int dtr);
1984 int (*read_bytes)(struct sr_serial_dev_inst *serial,
1985 uint8_t *data, int space, unsigned int timeout);
1986 int (*write_bytes)(struct sr_serial_dev_inst *serial,
1987 const uint8_t *data, int space);
1988 int (*flush)(struct sr_serial_dev_inst *serial);
1989 int (*drain)(struct sr_serial_dev_inst *serial);
1990};
a10284cd 1991extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x;
828eeea2 1992extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325;
616bc3a1 1993extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110;
0cdb72c8 1994extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor;
edec0436
GS
1995SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid);
1996#endif
c4f2dfd0 1997#endif
1483577e 1998
7c8ae47d
GS
1999/*--- bt/ API ---------------------------------------------------------------*/
2000
2001#ifdef HAVE_BLUETOOTH
2002SR_PRIV const char *sr_bt_adapter_get_address(size_t idx);
2003
2004struct sr_bt_desc;
2005typedef void (*sr_bt_scan_cb)(void *cb_data, const char *addr, const char *name);
2006typedef int (*sr_bt_data_cb)(void *cb_data, uint8_t *data, size_t dlen);
2007
2008SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void);
2009SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc);
2010
2011SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
2012 sr_bt_scan_cb cb, void *cb_data);
2013SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
2014 sr_bt_data_cb cb, void *cb_data);
2015SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr);
2016SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr);
2017SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel);
2018SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
2019 uint16_t read_handle, uint16_t write_handle,
2020 uint16_t cccd_handle, uint16_t cccd_value);
2021
2022SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration);
2023SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration);
2024
2025SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc);
2026SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc);
2027SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc);
2028
2029SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc,
2030 void *data, size_t len);
2031SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
2032 const void *data, size_t len);
2033
2034SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc);
2035SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
2036#endif
2037
fcfa36fd 2038/*--- ezusb.c ---------------------------------------------------------------*/
058b7035 2039
22b02383 2040#ifdef HAVE_LIBUSB_1_0
1a081ca6 2041SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
8e2d6c9d
DE
2042SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2043 const char *name);
2044SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2045 int configuration, const char *name);
22b02383 2046#endif
058b7035 2047
fcfa36fd 2048/*--- usb.c -----------------------------------------------------------------*/
0c632d36
UH
2049
2050#ifdef HAVE_LIBUSB_1_0
7ae6a758 2051SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn);
0c632d36 2052SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
67e95ed3 2053SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb);
102f1239
BV
2054SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
2055 int timeout, sr_receive_data_callback cb, void *cb_data);
2056SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx);
76bc5f63 2057SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len);
69f7d9b4
JH
2058SR_PRIV gboolean usb_match_manuf_prod(libusb_device *dev,
2059 const char *manufacturer, const char *product);
0c632d36
UH
2060#endif
2061
18698b40
AS
2062/*--- binary_helpers.c ------------------------------------------------------*/
2063
2064/** Binary value type */
2065enum binary_value_type {
2066 BVT_UINT8 = 0,
2067 BVT_BE_UINT8 = BVT_UINT8,
2068 BVT_LE_UINT8 = BVT_UINT8,
2069
2070 BVT_BE_UINT16,
2071 BVT_BE_UINT32,
2072 BVT_BE_UINT64,
2073 BVT_BE_FLOAT,
2074
2075 BVT_LE_UINT16,
2076 BVT_LE_UINT32,
2077 BVT_LE_UINT64,
2078 BVT_LE_FLOAT,
2079};
2080
2081/** Binary value specification */
2082struct binary_value_spec {
2083 /** Offset into binary blob */
2084 size_t offset;
2085 /** Data type to decode */
2086 enum binary_value_type type;
2087 /** Scale factor to get native units */
2088 float scale;
2089};
2090
2091/** Binary channel definition */
2092struct binary_analog_channel {
2093 /** Channel name */
2094 const char *name;
2095 /** Binary value in data stream */
2096 struct binary_value_spec spec;
2097 /** Significant digits */
2098 int digits;
2099 /** Measured quantity */
2100 enum sr_mq mq;
2101 /** Measured unit */
2102 enum sr_unit unit;
2103};
2104
2105/**
2106 * Read extract a value from a binary blob.
2107 *
2108 * @param out Pointer to output buffer.
2109 * @param spec Binary value specification
2110 * @param data Pointer to binary blob
2111 * @param length Size of binary blob
2112 * @return SR_OK on success, SR_ERR_* error code on failure.
2113 */
2114SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const void *data, size_t length);
2115
2116/**
2117 * Send an analog channel packet based on a binary analog channel
2118 * specification.
2119 *
2120 * @param sdi Device instance
2121 * @param ch Sigrok channel
2122 * @param spec Channel specification
2123 * @param data Pointer to binary blob
2124 * @param length Size of binary blob
2125 * @return SR_OK on success, SR_ERR_* error code on failure.
2126 */
2127SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch,
2128 const struct binary_analog_channel *spec, const void *data, size_t length);
7b9d7320 2129
4ea012bd
AS
2130/*--- crc.c -----------------------------------------------------------------*/
2131
2132#define SR_CRC16_DEFAULT_INIT 0xffffU
2133
2134/**
2135 * Calculate a CRC16 checksum using the 0x8005 polynomial.
2136 *
2137 * This CRC16 flavor is also known as CRC16-ANSI or CRC16-MODBUS.
2138 *
2139 * @param crc Initial value (typically 0xffff)
2140 * @param buffer Input buffer
2141 * @param len Buffer length
2142 * @return Checksum
2143 */
2144SR_PRIV uint16_t sr_crc16(uint16_t crc, const uint8_t *buffer, int len);
2145
daa39012
AJ
2146/*--- modbus/modbus.c -------------------------------------------------------*/
2147
2148struct sr_modbus_dev_inst {
2149 const char *name;
2150 const char *prefix;
2151 int priv_size;
2152 GSList *(*scan)(int modbusaddr);
2153 int (*dev_inst_new)(void *priv, const char *resource,
2154 char **params, const char *serialcomm, int modbusaddr);
2155 int (*open)(void *priv);
2156 int (*source_add)(struct sr_session *session, void *priv, int events,
2157 int timeout, sr_receive_data_callback cb, void *cb_data);
2158 int (*source_remove)(struct sr_session *session, void *priv);
2159 int (*send)(void *priv, const uint8_t *buffer, int buffer_size);
2160 int (*read_begin)(void *priv, uint8_t *function_code);
2161 int (*read_data)(void *priv, uint8_t *buf, int maxlen);
2162 int (*read_end)(void *priv);
2163 int (*close)(void *priv);
2164 void (*free)(void *priv);
2165 unsigned int read_timeout_ms;
2166 void *priv;
2167};
2168
2169SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
2170 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus));
2171SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
2172 const char *serialcomm, int modbusaddr);
2173SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus);
2174SR_PRIV int sr_modbus_source_add(struct sr_session *session,
2175 struct sr_modbus_dev_inst *modbus, int events, int timeout,
2176 sr_receive_data_callback cb, void *cb_data);
2177SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
2178 struct sr_modbus_dev_inst *modbus);
2179SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
2180 uint8_t *request, int request_size);
2181SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
2182 uint8_t *reply, int reply_size);
2183SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
2184 uint8_t *request, int request_size,
2185 uint8_t *reply, int reply_size);
2186SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
2187 int address, int nb_coils, uint8_t *coils);
2188SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
2189 int address, int nb_registers,
2190 uint16_t *registers);
2191SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
2192 int address, int value);
2193SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
2194 int address, int nb_registers,
2195 uint16_t *registers);
2196SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus);
2197SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus);
2198
fcfa36fd 2199/*--- dmm/es519xx.c ---------------------------------------------------------*/
c01bdebc 2200
bfb926c1
AJ
2201/**
2202 * All 11-byte es519xx chips repeat each block twice for each conversion cycle
2203 * so always read 2 blocks at a time.
2204 */
2205#define ES519XX_11B_PACKET_SIZE (11 * 2)
72e1672f 2206#define ES519XX_14B_PACKET_SIZE 14
c01bdebc
AJ
2207
2208struct es519xx_info {
2209 gboolean is_judge, is_voltage, is_auto, is_micro, is_current;
2210 gboolean is_milli, is_resistance, is_continuity, is_diode;
2211 gboolean is_frequency, is_rpm, is_capacitance, is_duty_cycle;
2212 gboolean is_temperature, is_celsius, is_fahrenheit;
2213 gboolean is_adp0, is_adp1, is_adp2, is_adp3;
2214 gboolean is_sign, is_batt, is_ol, is_pmax, is_pmin, is_apo;
2215 gboolean is_dc, is_ac, is_vahz, is_min, is_max, is_rel, is_hold;
2216 gboolean is_digit4, is_ul, is_vasel, is_vbar, is_lpf1, is_lpf0, is_rmr;
2217 uint32_t baudrate;
2218 int packet_size;
2219 gboolean alt_functions, fivedigits, clampmeter, selectable_lpf;
8cbf5627 2220 int digits;
c01bdebc
AJ
2221};
2222
72e1672f
UH
2223SR_PRIV gboolean sr_es519xx_2400_11b_packet_valid(const uint8_t *buf);
2224SR_PRIV int sr_es519xx_2400_11b_parse(const uint8_t *buf, float *floatval,
8c677240 2225 struct sr_datafeed_analog *analog, void *info);
2588e50c
AJ
2226SR_PRIV gboolean sr_es519xx_2400_11b_altfn_packet_valid(const uint8_t *buf);
2227SR_PRIV int sr_es519xx_2400_11b_altfn_parse(const uint8_t *buf,
8c677240 2228 float *floatval, struct sr_datafeed_analog *analog, void *info);
72e1672f 2229SR_PRIV gboolean sr_es519xx_19200_11b_5digits_packet_valid(const uint8_t *buf);
93d719cd 2230SR_PRIV int sr_es519xx_19200_11b_5digits_parse(const uint8_t *buf,
8c677240 2231 float *floatval, struct sr_datafeed_analog *analog, void *info);
72e1672f
UH
2232SR_PRIV gboolean sr_es519xx_19200_11b_clamp_packet_valid(const uint8_t *buf);
2233SR_PRIV int sr_es519xx_19200_11b_clamp_parse(const uint8_t *buf,
8c677240 2234 float *floatval, struct sr_datafeed_analog *analog, void *info);
72e1672f
UH
2235SR_PRIV gboolean sr_es519xx_19200_11b_packet_valid(const uint8_t *buf);
2236SR_PRIV int sr_es519xx_19200_11b_parse(const uint8_t *buf, float *floatval,
8c677240 2237 struct sr_datafeed_analog *analog, void *info);
72e1672f
UH
2238SR_PRIV gboolean sr_es519xx_19200_14b_packet_valid(const uint8_t *buf);
2239SR_PRIV int sr_es519xx_19200_14b_parse(const uint8_t *buf, float *floatval,
8c677240 2240 struct sr_datafeed_analog *analog, void *info);
72e1672f
UH
2241SR_PRIV gboolean sr_es519xx_19200_14b_sel_lpf_packet_valid(const uint8_t *buf);
2242SR_PRIV int sr_es519xx_19200_14b_sel_lpf_parse(const uint8_t *buf,
8c677240 2243 float *floatval, struct sr_datafeed_analog *analog, void *info);
c01bdebc 2244
fcfa36fd 2245/*--- dmm/fs9922.c ----------------------------------------------------------*/
79081ec8 2246
913abe83
UH
2247#define FS9922_PACKET_SIZE 14
2248
2249struct fs9922_info {
2250 gboolean is_auto, is_dc, is_ac, is_rel, is_hold, is_bpn, is_z1, is_z2;
2251 gboolean is_max, is_min, is_apo, is_bat, is_nano, is_z3, is_micro;
2252 gboolean is_milli, is_kilo, is_mega, is_beep, is_diode, is_percent;
2253 gboolean is_z4, is_volt, is_ampere, is_ohm, is_hfe, is_hertz, is_farad;
2254 gboolean is_celsius, is_fahrenheit;
2255 int bargraph_sign, bargraph_value;
2256};
2257
913abe83
UH
2258SR_PRIV gboolean sr_fs9922_packet_valid(const uint8_t *buf);
2259SR_PRIV int sr_fs9922_parse(const uint8_t *buf, float *floatval,
8c677240
UH
2260 struct sr_datafeed_analog *analog, void *info);
2261SR_PRIV void sr_fs9922_z1_diode(struct sr_datafeed_analog *analog, void *info);
79081ec8 2262
fcfa36fd 2263/*--- dmm/fs9721.c ----------------------------------------------------------*/
6c701476 2264
8c1adf37
UH
2265#define FS9721_PACKET_SIZE 14
2266
2267struct fs9721_info {
2268 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2269 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2270 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2271 gboolean is_c2c1_11, is_c2c1_10, is_c2c1_01, is_c2c1_00, is_sign;
2272};
2273
8c1adf37
UH
2274SR_PRIV gboolean sr_fs9721_packet_valid(const uint8_t *buf);
2275SR_PRIV int sr_fs9721_parse(const uint8_t *buf, float *floatval,
8c677240
UH
2276 struct sr_datafeed_analog *analog, void *info);
2277SR_PRIV void sr_fs9721_00_temp_c(struct sr_datafeed_analog *analog, void *info);
2278SR_PRIV void sr_fs9721_01_temp_c(struct sr_datafeed_analog *analog, void *info);
2279SR_PRIV void sr_fs9721_10_temp_c(struct sr_datafeed_analog *analog, void *info);
2280SR_PRIV void sr_fs9721_01_10_temp_f_c(struct sr_datafeed_analog *analog, void *info);
2281SR_PRIV void sr_fs9721_max_c_min(struct sr_datafeed_analog *analog, void *info);
dcd212f7 2282
4c29bba1
PS
2283/*--- dmm/mm38xr.c ---------------------------------------------------------*/
2284
2285#define METERMAN_38XR_PACKET_SIZE 15
2286
2287struct meterman_38xr_info { int dummy; };
2288
2289SR_PRIV gboolean meterman_38xr_packet_valid(const uint8_t *buf);
2290SR_PRIV int meterman_38xr_parse(const uint8_t *buf, float *floatval,
2291 struct sr_datafeed_analog *analog, void *info);
2292
dcd212f7
VV
2293/*--- dmm/ms2115b.c ---------------------------------------------------------*/
2294
2295#define MS2115B_PACKET_SIZE 9
2296
2297enum ms2115b_display {
2298 MS2115B_DISPLAY_MAIN,
2299 MS2115B_DISPLAY_SUB,
2300 MS2115B_DISPLAY_COUNT,
2301};
2302
2303struct ms2115b_info {
2304 /* Selected channel. */
2305 size_t ch_idx;
2306 gboolean is_ac, is_dc, is_auto;
2307 gboolean is_diode, is_beep, is_farad;
2308 gboolean is_ohm, is_ampere, is_volt, is_hz;
2309 gboolean is_duty_cycle, is_percent;
2310};
2311
2312extern SR_PRIV const char *ms2115b_channel_formats[];
2313SR_PRIV gboolean sr_ms2115b_packet_valid(const uint8_t *buf);
2314SR_PRIV int sr_ms2115b_parse(const uint8_t *buf, float *floatval,
2315 struct sr_datafeed_analog *analog, void *info);
6c701476 2316
fcfa36fd 2317/*--- dmm/ms8250d.c ---------------------------------------------------------*/
67070942
M
2318
2319#define MS8250D_PACKET_SIZE 18
2320
2321struct ms8250d_info {
2322 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2323 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2324 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2325 gboolean is_ncv, is_min, is_max, is_sign, is_autotimer;
2326};
2327
2328SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf);
2329SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
2330 struct sr_datafeed_analog *analog, void *info);
2331
fcfa36fd 2332/*--- dmm/dtm0660.c ---------------------------------------------------------*/
eed3dec8
MG
2333
2334#define DTM0660_PACKET_SIZE 15
2335
2336struct dtm0660_info {
2337 gboolean is_ac, is_dc, is_auto, is_rs232, is_micro, is_nano, is_kilo;
2338 gboolean is_diode, is_milli, is_percent, is_mega, is_beep, is_farad;
2339 gboolean is_ohm, is_rel, is_hold, is_ampere, is_volt, is_hz, is_bat;
2340 gboolean is_degf, is_degc, is_c2c1_01, is_c2c1_00, is_apo, is_min;
2341 gboolean is_minmax, is_max, is_sign;
2342};
2343
2344SR_PRIV gboolean sr_dtm0660_packet_valid(const uint8_t *buf);
2345SR_PRIV int sr_dtm0660_parse(const uint8_t *buf, float *floatval,
8c677240 2346 struct sr_datafeed_analog *analog, void *info);
eed3dec8 2347
fcfa36fd 2348/*--- dmm/m2110.c -----------------------------------------------------------*/
825da8b2
MH
2349
2350#define BBCGM_M2110_PACKET_SIZE 9
2351
9d12555f
UH
2352/* Dummy info struct. The parser does not use it. */
2353struct m2110_info { int dummy; };
2354
825da8b2
MH
2355SR_PRIV gboolean sr_m2110_packet_valid(const uint8_t *buf);
2356SR_PRIV int sr_m2110_parse(const uint8_t *buf, float *floatval,
8c677240 2357 struct sr_datafeed_analog *analog, void *info);
825da8b2 2358
fcfa36fd 2359/*--- dmm/metex14.c ---------------------------------------------------------*/
ac913e5c
UH
2360
2361#define METEX14_PACKET_SIZE 14
2362
2363struct metex14_info {
556a926d 2364 size_t ch_idx;
ac913e5c
UH
2365 gboolean is_ac, is_dc, is_resistance, is_capacity, is_temperature;
2366 gboolean is_diode, is_frequency, is_ampere, is_volt, is_farad;
7fb4ff02
FS
2367 gboolean is_hertz, is_ohm, is_celsius, is_fahrenheit, is_watt;
2368 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
fd8dc1db 2369 gboolean is_gain, is_decibel, is_power, is_decibel_mw, is_power_factor;
7fb4ff02 2370 gboolean is_hfe, is_unitless, is_logic, is_min, is_max, is_avg;
ac913e5c
UH
2371};
2372
1df81f4b 2373#ifdef HAVE_SERIAL_COMM
ce3777ad 2374SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial);
c4f2dfd0 2375#endif
ac913e5c
UH
2376SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf);
2377SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
8c677240 2378 struct sr_datafeed_analog *analog, void *info);
556a926d
GS
2379SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf);
2380SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
2381 struct sr_datafeed_analog *analog, void *info);
ac913e5c 2382
fcfa36fd 2383/*--- dmm/rs9lcd.c ----------------------------------------------------------*/
05f134ab 2384
21829e67 2385#define RS9LCD_PACKET_SIZE 9
05f134ab 2386
e7ed87a4 2387/* Dummy info struct. The parser does not use it. */
bf6f8399 2388struct rs9lcd_info { int dummy; };
e7ed87a4 2389
05f134ab
AG
2390SR_PRIV gboolean sr_rs9lcd_packet_valid(const uint8_t *buf);
2391SR_PRIV int sr_rs9lcd_parse(const uint8_t *buf, float *floatval,
8c677240 2392 struct sr_datafeed_analog *analog, void *info);
05f134ab 2393
fcfa36fd 2394/*--- dmm/bm25x.c -----------------------------------------------------------*/
a24c3f4a
JH
2395
2396#define BRYMEN_BM25X_PACKET_SIZE 15
2397
2398/* Dummy info struct. The parser does not use it. */
2399struct bm25x_info { int dummy; };
2400
2401SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
2402SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
8c677240 2403 struct sr_datafeed_analog *analog, void *info);
a24c3f4a 2404
400bc4ff
GS
2405/*--- dmm/bm52x.c -----------------------------------------------------------*/
2406
2407#define BRYMEN_BM52X_PACKET_SIZE 24
2408#define BRYMEN_BM52X_DISPLAY_COUNT 2
2409
2410struct brymen_bm52x_info { size_t ch_idx; };
2411
2412#ifdef HAVE_SERIAL_COMM
2413SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
6bee394d 2414SR_PRIV int sr_brymen_bm82x_packet_request(struct sr_serial_dev_inst *serial);
400bc4ff
GS
2415#endif
2416SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
6bee394d
GS
2417SR_PRIV gboolean sr_brymen_bm82x_packet_valid(const uint8_t *buf);
2418/* BM520s and BM820s protocols are similar, the parse routine is shared. */
400bc4ff
GS
2419SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
2420 struct sr_datafeed_analog *analog, void *info);
2421
0931639a
GS
2422struct brymen_bm52x_state;
2423
2424SR_PRIV void *brymen_bm52x_state_init(void);
2425SR_PRIV void brymen_bm52x_state_free(void *state);
2426SR_PRIV int brymen_bm52x_config_get(void *state, uint32_t key, GVariant **data,
2427 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2428SR_PRIV int brymen_bm52x_config_set(void *state, uint32_t key, GVariant *data,
2429 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2430SR_PRIV int brymen_bm52x_config_list(void *state, uint32_t key, GVariant **data,
2431 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2432SR_PRIV int brymen_bm52x_acquire_start(void *state,
2433 const struct sr_dev_inst *sdi,
2434 sr_receive_data_callback *cb, void **cb_data);
2435
27186eda
GS
2436/*--- dmm/bm85x.c -----------------------------------------------------------*/
2437
2438#define BRYMEN_BM85x_PACKET_SIZE_MIN 4
2439
2440struct brymen_bm85x_info { int dummy; };
2441
2442#ifdef HAVE_SERIAL_COMM
2443SR_PRIV int brymen_bm85x_after_open(struct sr_serial_dev_inst *serial);
2444SR_PRIV int brymen_bm85x_packet_request(struct sr_serial_dev_inst *serial);
2445#endif
2446SR_PRIV gboolean brymen_bm85x_packet_valid(void *state,
2447 const uint8_t *buf, size_t len, size_t *pkt_len);
2448SR_PRIV int brymen_bm85x_parse(void *state, const uint8_t *buf, size_t len,
2449 double *floatval, struct sr_datafeed_analog *analog, void *info);
2450
0759e2f5
GS
2451/*--- dmm/bm86x.c -----------------------------------------------------------*/
2452
2453#define BRYMEN_BM86X_PACKET_SIZE 24
2454#define BRYMEN_BM86X_DISPLAY_COUNT 2
2455
2456struct brymen_bm86x_info { size_t ch_idx; };
2457
2458#ifdef HAVE_SERIAL_COMM
2459SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial);
2460#endif
2461SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf);
2462SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *floatval,
2463 struct sr_datafeed_analog *analog, void *info);
2464
fcfa36fd 2465/*--- dmm/ut71x.c -----------------------------------------------------------*/
626027df
UH
2466
2467#define UT71X_PACKET_SIZE 11
2468
2469struct ut71x_info {
2470 gboolean is_voltage, is_resistance, is_capacitance, is_temperature;
2471 gboolean is_celsius, is_fahrenheit, is_current, is_continuity;
2472 gboolean is_diode, is_frequency, is_duty_cycle, is_dc, is_ac;
2473 gboolean is_auto, is_manual, is_sign, is_power, is_loop_current;
2474};
2475
2476SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf);
2477SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval,
8c677240 2478 struct sr_datafeed_analog *analog, void *info);
626027df 2479
fcfa36fd 2480/*--- dmm/vc870.c -----------------------------------------------------------*/
c36f78f7
UH
2481
2482#define VC870_PACKET_SIZE 23
2483
2484struct vc870_info {
2485 gboolean is_voltage, is_dc, is_ac, is_temperature, is_resistance;
2486 gboolean is_continuity, is_capacitance, is_diode, is_loop_current;
2487 gboolean is_current, is_micro, is_milli, is_power;
ee2e9be2 2488 gboolean is_power_factor_freq, is_power_apparent_power, is_v_a_rms_value;
c36f78f7
UH
2489 gboolean is_sign2, is_sign1, is_batt, is_ol1, is_max, is_min;
2490 gboolean is_maxmin, is_rel, is_ol2, is_open, is_manu, is_hold;
2491 gboolean is_light, is_usb, is_warning, is_auto_power, is_misplug_warn;
2492 gboolean is_lo, is_hi, is_open2;
2493
3591481e 2494 gboolean is_frequency, is_dual_display, is_auto;
c36f78f7
UH
2495};
2496
2497SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf);
2498SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
8c677240 2499 struct sr_datafeed_analog *analog, void *info);
c36f78f7 2500
fcfa36fd 2501/*--- dmm/vc96.c ------------------------------------------------------------*/
9456d636
MS
2502
2503#define VC96_PACKET_SIZE 13
2504
2505struct vc96_info {
2506 size_t ch_idx;
2507 gboolean is_ac, is_dc, is_resistance, is_diode, is_ampere, is_volt;
2508 gboolean is_ohm, is_micro, is_milli, is_kilo, is_mega, is_hfe;
2509 gboolean is_unitless;
2510};
2511
2512SR_PRIV gboolean sr_vc96_packet_valid(const uint8_t *buf);
2513SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval,
2514 struct sr_datafeed_analog *analog, void *info);
2515
fcfa36fd 2516/*--- lcr/es51919.c ---------------------------------------------------------*/
6bcb3ee8 2517
bf5c4d46
GS
2518/* Acquisition details which apply to all supported serial-lcr devices. */
2519struct lcr_parse_info {
2520 size_t ch_idx;
2521 uint64_t output_freq;
2522 const char *circuit_model;
2523};
2524
2525#define ES51919_PACKET_SIZE 17
2526#define ES51919_CHANNEL_COUNT 2
2527#define ES51919_COMM_PARAM "9600/8n1/rts=1/dtr=1"
2528
2529SR_PRIV int es51919_config_get(uint32_t key, GVariant **data,
2530 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2531SR_PRIV int es51919_config_set(uint32_t key, GVariant *data,
2532 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2533SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
2534 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2535SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt);
2536SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *floatval,
b50970a5
GS
2537 struct sr_datafeed_analog *analog, void *info);
2538
2539/*--- lcr/vc4080.c ----------------------------------------------------------*/
2540
2541/* Note: Also uses 'struct lcr_parse_info' from es51919 above. */
2542
2543#define VC4080_PACKET_SIZE 39
2544#define VC4080_COMM_PARAM "1200/8n1"
2545#define VC4080_WITH_DQ_CHANS 0 /* Enable separate D/Q channels? */
2546
2547enum vc4080_display {
2548 VC4080_DISPLAY_PRIMARY,
2549 VC4080_DISPLAY_SECONDARY,
2550#if VC4080_WITH_DQ_CHANS
2551 VC4080_DISPLAY_D_VALUE,
2552 VC4080_DISPLAY_Q_VALUE,
2553#endif
2554 VC4080_CHANNEL_COUNT,
2555};
2556
2557extern SR_PRIV const char *vc4080_channel_formats[VC4080_CHANNEL_COUNT];
2558
2559SR_PRIV int vc4080_config_list(uint32_t key, GVariant **data,
2560 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg);
2561SR_PRIV int vc4080_packet_request(struct sr_serial_dev_inst *serial);
2562SR_PRIV gboolean vc4080_packet_valid(const uint8_t *pkt);
2563SR_PRIV int vc4080_packet_parse(const uint8_t *pkt, float *floatval,
bf5c4d46 2564 struct sr_datafeed_analog *analog, void *info);
6bcb3ee8 2565
fcfa36fd 2566/*--- dmm/ut372.c -----------------------------------------------------------*/
f3cde309
ML
2567
2568#define UT372_PACKET_SIZE 27
2569
2570struct ut372_info {
2571 int dummy;
2572};
2573
2574SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf);
2575SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
8c677240 2576 struct sr_datafeed_analog *analog, void *info);
f3cde309 2577
fcfa36fd 2578/*--- dmm/asycii.c ----------------------------------------------------------*/
4ba4d52a
GS
2579
2580#define ASYCII_PACKET_SIZE 16
2581
2582struct asycii_info {
2583 gboolean is_ac, is_dc, is_ac_and_dc;
2584 gboolean is_resistance, is_capacitance, is_diode, is_gain;
2585 gboolean is_frequency, is_duty_cycle, is_duty_pos, is_duty_neg;
2586 gboolean is_pulse_width, is_period_pos, is_period_neg;
2587 gboolean is_pulse_count, is_count_pos, is_count_neg;
2588 gboolean is_ampere, is_volt, is_volt_ampere, is_farad, is_ohm;
2589 gboolean is_hertz, is_percent, is_seconds, is_decibel;
2590 gboolean is_pico, is_nano, is_micro, is_milli, is_kilo, is_mega;
2591 gboolean is_unitless;
2592 gboolean is_peak_min, is_peak_max;
2593 gboolean is_invalid;
2594};
2595
1df81f4b 2596#ifdef HAVE_SERIAL_COMM
4ba4d52a
GS
2597SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial);
2598#endif
2599SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf);
2600SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
2601 struct sr_datafeed_analog *analog, void *info);
2602
fcfa36fd 2603/*--- dmm/eev121gw.c --------------------------------------------------------*/
1c3098aa
GS
2604
2605#define EEV121GW_PACKET_SIZE 19
2606
2607enum eev121gw_display {
2608 EEV121GW_DISPLAY_MAIN,
2609 EEV121GW_DISPLAY_SUB,
2610 EEV121GW_DISPLAY_BAR,
2611 EEV121GW_DISPLAY_COUNT,
2612};
2613
2614struct eev121gw_info {
2615 /* Selected channel. */
2616 size_t ch_idx;
2617 /*
2618 * Measured value, number and sign/overflow flags, scale factor
2619 * and significant digits.
2620 */
2621 uint32_t uint_value;
2622 gboolean is_ofl, is_neg;
2623 int factor, digits;
2624 /* Currently active mode (meter's function). */
2625 gboolean is_ac, is_dc, is_voltage, is_current, is_power, is_gain;
2626 gboolean is_resistance, is_capacitance, is_diode, is_temperature;
2627 gboolean is_continuity, is_frequency, is_period, is_duty_cycle;
2628 /* Quantities associated with mode/function. */
2629 gboolean is_ampere, is_volt, is_volt_ampere, is_dbm;
2630 gboolean is_ohm, is_farad, is_celsius, is_fahrenheit;
2631 gboolean is_hertz, is_seconds, is_percent, is_loop_current;
2632 gboolean is_unitless, is_logic;
2633 /* Other indicators. */
2634 gboolean is_min, is_max, is_avg, is_1ms_peak, is_rel, is_hold;
2635 gboolean is_low_pass, is_mem, is_bt, is_auto_range, is_test;
2636 gboolean is_auto_poweroff, is_low_batt;
2637};
2638
015df4ae 2639extern SR_PRIV const char *eev121gw_channel_formats[];
1c3098aa 2640SR_PRIV gboolean sr_eev121gw_packet_valid(const uint8_t *buf);
1c3098aa 2641SR_PRIV int sr_eev121gw_3displays_parse(const uint8_t *buf, float *floatval,
8556703a 2642 struct sr_datafeed_analog *analog, void *info);
1c3098aa 2643
fcfa36fd 2644/*--- scale/kern.c ----------------------------------------------------------*/
e5d953b5
UH
2645
2646struct kern_info {
2647 gboolean is_gram, is_carat, is_ounce, is_pound, is_troy_ounce;
2648 gboolean is_pennyweight, is_grain, is_tael, is_momme, is_tola;
2649 gboolean is_percentage, is_piece, is_unstable, is_stable, is_error;
2650 int buflen;
2651};
2652
2653SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
2654SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
563ba4a5 2655 struct sr_datafeed_analog *analog, void *info);
e5d953b5 2656
aea4e458
LPC
2657/*--- sw_limits.c -----------------------------------------------------------*/
2658
2659struct sr_sw_limits {
2660 uint64_t limit_samples;
67785f25 2661 uint64_t limit_frames;
aea4e458
LPC
2662 uint64_t limit_msec;
2663 uint64_t samples_read;
67785f25 2664 uint64_t frames_read;
aea4e458
LPC
2665 uint64_t start_time;
2666};
2667
d579755a 2668SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
aea4e458
LPC
2669 GVariant **data);
2670SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
2671 GVariant *data);
2672SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
2673SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
2674SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
2675 uint64_t samples_read);
67785f25
GS
2676SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
2677 uint64_t frames_read);
aea4e458
LPC
2678SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
2679
47a102f9
GS
2680/*--- feed_queue.h ----------------------------------------------------------*/
2681
2682struct feed_queue_logic;
2683struct feed_queue_analog;
2684
2685SR_API struct feed_queue_logic *feed_queue_logic_alloc(
2686 struct sr_dev_inst *sdi,
2687 size_t sample_count, size_t unit_size);
2688SR_API int feed_queue_logic_submit(struct feed_queue_logic *q,
2689 const uint8_t *data, size_t count);
2690SR_API int feed_queue_logic_flush(struct feed_queue_logic *q);
2691SR_API void feed_queue_logic_free(struct feed_queue_logic *q);
2692
2693SR_API struct feed_queue_analog *feed_queue_analog_alloc(
2694 struct sr_dev_inst *sdi,
2695 size_t sample_count, int digits, struct sr_channel *ch);
2696SR_API int feed_queue_analog_submit(struct feed_queue_analog *q,
2697 float data, size_t count);
2698SR_API int feed_queue_analog_flush(struct feed_queue_analog *q);
2699SR_API void feed_queue_analog_free(struct feed_queue_analog *q);
2700
1483577e 2701#endif