]>
Commit | Line | Data |
---|---|---|
18698b40 AS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <libsigrok/libsigrok.h> | |
23 | #include "libsigrok-internal.h" | |
24 | ||
48b7c901 | 25 | SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec, |
1fa04876 | 26 | const uint8_t *data, size_t length) |
18698b40 AS |
27 | { |
28 | float value; | |
29 | ||
30 | if (!out || !spec || !data) | |
31 | return SR_ERR_ARG; | |
32 | ||
33 | #define VALUE_TYPE(T, R, L) \ | |
34 | case T: \ | |
35 | if (spec->offset + (L) > length) \ | |
36 | return SR_ERR_DATA; \ | |
37 | value = R(data + spec->offset); \ | |
38 | break | |
39 | ||
40 | switch (spec->type) { | |
e7fe5bb4 | 41 | VALUE_TYPE(BVT_UINT8, read_u8, sizeof(uint8_t)); |
18698b40 | 42 | |
e7fe5bb4 | 43 | VALUE_TYPE(BVT_BE_UINT16, read_u16be, sizeof(uint16_t)); |
d6b236ef | 44 | VALUE_TYPE(BVT_BE_UINT24, read_u24be, 3 * sizeof(uint8_t)); |
e7fe5bb4 GS |
45 | VALUE_TYPE(BVT_BE_UINT32, read_u32be, sizeof(uint32_t)); |
46 | VALUE_TYPE(BVT_BE_UINT64, read_u64be, sizeof(uint64_t)); | |
47 | VALUE_TYPE(BVT_BE_FLOAT, read_fltbe, sizeof(float)); | |
18698b40 | 48 | |
e7fe5bb4 | 49 | VALUE_TYPE(BVT_LE_UINT16, read_u16le, sizeof(uint16_t)); |
d6b236ef | 50 | VALUE_TYPE(BVT_LE_UINT24, read_u24le, 3 * sizeof(uint8_t)); |
e7fe5bb4 GS |
51 | VALUE_TYPE(BVT_LE_UINT32, read_u32le, sizeof(uint32_t)); |
52 | VALUE_TYPE(BVT_LE_UINT64, read_u64le, sizeof(uint64_t)); | |
53 | VALUE_TYPE(BVT_LE_FLOAT, read_fltle, sizeof(float)); | |
18698b40 AS |
54 | |
55 | default: | |
56 | return SR_ERR_ARG; | |
57 | } | |
58 | ||
59 | #undef VALUE_TYPE | |
60 | ||
e7fe5bb4 GS |
61 | if (out) |
62 | *out = value; | |
18698b40 AS |
63 | return SR_OK; |
64 | } | |
9697b4a5 MP |
65 | |
66 | SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, | |
67 | const uint8_t *data) | |
68 | { | |
69 | float value; | |
70 | const uint8_t *ptr; | |
71 | ||
72 | ptr = &data[spec->offset]; | |
73 | ||
74 | switch (spec->type) { | |
75 | case BVT_UINT8: | |
76 | value = read_u8(ptr); | |
77 | break; | |
78 | case BVT_BE_UINT16: | |
79 | value = read_u16be(ptr); | |
80 | break; | |
81 | case BVT_BE_UINT24: | |
82 | value = read_u24be(ptr); | |
83 | break; | |
84 | case BVT_BE_UINT32: | |
85 | value = read_u32be(ptr); | |
86 | break; | |
87 | case BVT_BE_UINT64: | |
88 | value = read_u64be(ptr); | |
89 | break; | |
90 | case BVT_BE_FLOAT: | |
91 | value = read_fltbe(ptr); | |
92 | break; | |
93 | case BVT_LE_UINT16: | |
94 | value = read_u16le(ptr); | |
95 | break; | |
96 | case BVT_LE_UINT24: | |
97 | value = read_u24le(ptr); | |
98 | break; | |
99 | case BVT_LE_UINT32: | |
100 | value = read_u32le(ptr); | |
101 | break; | |
102 | case BVT_LE_UINT64: | |
103 | value = read_u64le(ptr); | |
104 | break; | |
105 | case BVT_LE_FLOAT: | |
106 | value = read_fltle(ptr); | |
107 | break; | |
108 | default: | |
109 | return SR_ERR_ARG; | |
110 | } | |
111 | ||
112 | if (out) | |
113 | *out = value; | |
114 | return SR_OK; | |
115 | } |