Fix some implicitly defined functions - import proper headers.
[apps/agl-service-can-low-level.git] / src / bitfield / bitfield.c
1 #include <bitfield/bitfield.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stddef.h>
5
6 uint64_t bitmask(const uint8_t bit_count) {
7     return (((uint64_t)0x1) << bit_count) - 1;
8 }
9
10 uint8_t get_nibble(const uint8_t source[], const uint8_t source_length,
11                 const uint8_t nibble_index) {
12     uint8_t byte_index = nibble_index / 2;
13     uint8_t result = get_byte(source, source_length, byte_index);
14     if(nibble_index % 2 == 0) {
15         result >>= NIBBLE_SIZE;
16     }
17     result &= bitmask(NIBBLE_SIZE);
18     return result;
19 }
20
21 uint8_t get_byte(const uint8_t source[], const uint8_t source_length,
22         const uint8_t byte_index) {
23     if(byte_index < source_length) {
24         return source[byte_index];
25     }
26     return 0;
27 }
28
29 bool set_nibble(const uint16_t nibble_index, const uint8_t value,
30         uint8_t* destination, const uint16_t destination_length) {
31     return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination,
32             destination_length, nibble_index * NIBBLE_SIZE);
33 }