]> sigrok.org Git - libsigrok.git/blame - src/binary_helpers.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / binary_helpers.c
CommitLineData
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 25SR_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 45 VALUE_TYPE(BVT_BE_UINT32, read_u32be, sizeof(uint32_t));
18698b40 46
e7fe5bb4 47 VALUE_TYPE(BVT_LE_UINT16, read_u16le, sizeof(uint16_t));
d6b236ef 48 VALUE_TYPE(BVT_LE_UINT24, read_u24le, 3 * sizeof(uint8_t));
e7fe5bb4 49 VALUE_TYPE(BVT_LE_UINT32, read_u32le, sizeof(uint32_t));
18698b40
AS
50
51 default:
52 return SR_ERR_ARG;
53 }
54
55#undef VALUE_TYPE
56
e7fe5bb4
GS
57 if (out)
58 *out = value;
18698b40
AS
59 return SR_OK;
60}
9697b4a5
MP
61
62SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec,
63 const uint8_t *data)
64{
65 float value;
66 const uint8_t *ptr;
67
68 ptr = &data[spec->offset];
69
70 switch (spec->type) {
71 case BVT_UINT8:
72 value = read_u8(ptr);
73 break;
74 case BVT_BE_UINT16:
75 value = read_u16be(ptr);
76 break;
77 case BVT_BE_UINT24:
78 value = read_u24be(ptr);
79 break;
80 case BVT_BE_UINT32:
81 value = read_u32be(ptr);
82 break;
9697b4a5
MP
83 case BVT_LE_UINT16:
84 value = read_u16le(ptr);
85 break;
86 case BVT_LE_UINT24:
87 value = read_u24le(ptr);
88 break;
89 case BVT_LE_UINT32:
90 value = read_u32le(ptr);
91 break;
9697b4a5
MP
92 default:
93 return SR_ERR_ARG;
94 }
95
96 if (out)
97 *out = value;
98 return SR_OK;
99}