Fix parse_bool tests.
[apps/low-level-can-service.git] / src / canutil / read.c
1 #include <canutil/read.h>
2 #include <bitfield/bitfield.h>
3 #include <bitfield/8byte.h>
4
5 float eightbyte_parse_float(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
6         float factor, float offset) {
7     uint64_t raw = eightbyte_get_bitfield(data, bit_offset, bit_size, true);
8     return raw * factor + offset;
9 }
10
11 bool eightbyte_parse_bool(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
12         float factor, float offset) {
13     float value = eightbyte_parse_float(data, bit_offset, bit_size, factor, offset);
14     return value == 0.0 ? false : true;
15 }
16
17 float bitfield_parse_float(const uint8_t source[], const uint16_t source_length,
18         const uint8_t bit_offset, const uint8_t bit_size, const float factor,
19         const float offset) {
20     uint64_t raw = get_bitfield(source, source_length, bit_offset, bit_size);
21     // TODO seems dumb that this is repeated from eightbyte_parse_float - is it
22     // really worth keeping around these two implementations?
23     return raw * factor + offset;
24 }
25
26 bool bitfield_parse_bool(const uint8_t source[], const uint16_t source_length,
27         const uint8_t bit_offset, const uint8_t bit_size, const float factor,
28         const float offset) {
29     float value = bitfield_parse_float(source, source_length, bit_offset,
30             bit_size, factor, offset);
31     return value == 0.0 ? false : true;
32 }