Draft work making the bitfield functions more generic.
[apps/agl-service-can-low-level.git] / src / bitfield / bitfield.h
1 #ifndef __BITFIELD_H__
2 #define __BITFIELD_H__
3
4 #include <stdint.h>
5 #include <stdbool.h>
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 typedef enum {
12     ENDIANNESS_LITTLE_ENDIAN,
13     ENDIANNESS_BIG_ENDIAN
14 } Endianness;
15
16 uint8_t getNibble(const uint8_t nibble_index, const uint8_t data[],
17         const uint8_t length, Endianness endianness);
18
19 uint8_t getByte(const uint8_t byte_index, const uint8_t data[],
20         const uint8_t length, Endianness endianness);
21
22 void getBits(const uint16_t start_index, const uint16_t field_size,
23         const uint8_t data[], const uint8_t length, Endianness endianness,
24         uint8_t* result);
25
26 /* Public: Reads a subset of bits from a byte array.
27  *
28  * data - the bytes in question.
29  * startPos - the starting index of the bit field (beginning from 0).
30  * numBits - the width of the bit field to extract.
31  * bigEndian - if the data passed in is little endian, set this to false and it
32  *      will be flipped before grabbing the bit field.
33  *
34  * Bit fields are positioned according to big-endian bit layout, but inside the
35  * bit field, values are represented as little-endian. Therefore, to get the bit
36  * field, we swap the overall byte order if bigEndian == false and
37  * use the value we find in the field (assuming the embedded platform is little
38  * endian).
39  *
40  * For example, the bit layout of the value "42" (i.e. 00101010 set at position
41  * 14 with length 6 is:
42  *
43  *     000000000000001010100000000000000000000000000000000000000000000
44  *
45  * and the same value and position but with length 8 is:
46  *
47  *     000000000000000010101000000000000000000000000000000000000000000
48  *
49  * If the architecture where is code is running is little-endian, the input data
50  * will be swapped before grabbing the bit field.
51  *
52  * Examples
53  *
54  *  uint64_t value = getBitField(data, 2, 4);
55  *
56  * Returns the value of the requested bit field.
57  */
58 uint64_t getBitField(uint64_t data, const uint16_t startPos, const uint16_t numBits, bool bigEndian);
59
60 /* Public: Set the bit field in the given data array to the new value.
61  *
62  * data - a byte array with size at least startPos + numBits.
63  * value - the value to set in the bit field.
64  * startPos - the starting index of the bit field (beginning from 0).
65  */
66 void setBitField(uint64_t* data, uint64_t value, int startPos, int numBits);
67
68 /* Public: Retreive the nth byte out of 8 bytes in a uint64_t.
69  *
70  * source - the source data to retreive the byte from.
71  * byteNum - the index of the byte, starting at 0 and assuming big-endian order.
72  *
73  * Returns the requested byte from the source bytes.
74  */
75 uint8_t nthByte(uint64_t source, int byteNum);
76
77 #ifdef __cplusplus
78 }
79 #endif
80
81 #endif // __BITFIELD_H__