libsigrok  unreleased development snapshot
sigrok hardware access and backend library
binary_helpers.c
Go to the documentation of this file.
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 
25 SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec,
26  const uint8_t *data, size_t length)
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) {
41  VALUE_TYPE(BVT_UINT8, read_u8, sizeof(uint8_t));
42 
43  VALUE_TYPE(BVT_BE_UINT16, read_u16be, sizeof(uint16_t));
44  VALUE_TYPE(BVT_BE_UINT24, read_u24be, 3 * sizeof(uint8_t));
45  VALUE_TYPE(BVT_BE_UINT32, read_u32be, sizeof(uint32_t));
46 
47  VALUE_TYPE(BVT_LE_UINT16, read_u16le, sizeof(uint16_t));
48  VALUE_TYPE(BVT_LE_UINT24, read_u24le, 3 * sizeof(uint8_t));
49  VALUE_TYPE(BVT_LE_UINT32, read_u32le, sizeof(uint32_t));
50 
51  default:
52  return SR_ERR_ARG;
53  }
54 
55 #undef VALUE_TYPE
56 
57  if (out)
58  *out = value;
59  return SR_OK;
60 }
61 
62 SR_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;
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;
92  default:
93  return SR_ERR_ARG;
94  }
95 
96  if (out)
97  *out = value;
98  return SR_OK;
99 }
No error.
Definition: libsigrok.h:67
SR_PRIV int bv_get_value_len(float *out, const struct binary_value_spec *spec, const uint8_t *data, size_t length)
lzo_voidp ptr
Definition: lzoconf.h:278
Function argument error.
Definition: libsigrok.h:70
The public libsigrok header file to be used by frontends.
#define SR_PRIV
Definition: libsigrok.h:135
#define VALUE_TYPE(T, R, L)
SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const uint8_t *data)