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