Remove old hipchat token from Travis CI config.
[apps/agl-service-can-low-level.git] / src / bitfield / 8byte.h
1 #ifndef __8BYTE_H__
2 #define __8BYTE_H__
3
4 #include <stdint.h>
5 #include <stdbool.h>
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 /* Public: Reads a subset of bits into a uint64_t.
12  *
13  * source - the bytes in question.
14  * offset - the starting index of the bit field (beginning from 0).
15  * bit_count - the width of the bit field to extract.
16  * data_is_big_endian - if the data passed in is little endian, set this to false and it
17  *      will be flipped before grabbing the bit field.
18  *
19  * Bit fields are positioned according to big-endian bit layout.
20  *
21  * For example, the bit layout of the value "42" (i.e. 00101010 set at position
22  * 14 with length 6 is:
23  *
24  *     000000000000001010100000000000000000000000000000000000000000000
25  *
26  * and the same value and position but with length 8 is:
27  *
28  *     000000000000000010101000000000000000000000000000000000000000000
29  *
30  * If the architecture where is code is running is little-endian, the input data
31  * will be swapped before grabbing the bit field.
32  *
33  * Examples
34  *
35  *  uint64_t value = get_bitfield(data, 2, 4);
36  *
37  * Returns the value of the requested bit field, right aligned in a uint64_t.
38  */
39 uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset,
40         const uint16_t bit_count, const bool data_is_big_endian);
41
42 /* Public: Return a single nibble from the payload, with range checking.
43  *
44  * source - the source payload.
45  * nibble_index - the index of the nibble to retreive. The leftmost nibble is
46  *      index 0.
47  * data_is_big_endian - if the data passed in is little endian, set this to false and it
48  *      will be flipped before grabbing the bit field.
49  *
50  * Returns the retreived nibble, right aligned in a uint8_t.
51  */
52 uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index,
53         const bool data_is_big_endian);
54
55 /* Public: Return a single byte from the payload, with range checking.
56  *
57  * source - the source byte array.
58  * byte_index - the index of the byte to retreive. The leftmost byte is index 0.
59  * data_is_big_endian - if the data passed in is little endian, set this to false and it
60  *      will be flipped before grabbing the bit field.
61  *
62  * Returns the retreived byte.
63  */
64 uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index,
65         const bool data_is_big_endian);
66
67 /* Public: Set the bit field in the given data array to the new value.
68  *
69  * destination - a byte array with size at least offset + bit_count.
70  * value - the value to set in the bit field.
71  * offset - the starting index of the bit field (beginning from 0).
72  * bit_count - the number of bits to set in the data.
73  *
74  * Returns true if the bit_count is enough to fully represent the value, and
75  *      false if it will not fit.
76  */
77 bool eightbyte_set_bitfield(uint64_t value,
78         const uint16_t offset, const uint16_t bit_count, uint64_t* destination);
79
80 /* Private: Determine the index of the last bit used.
81  */
82 uint8_t find_end_bit(const uint16_t num_bits);
83
84 #ifdef __cplusplus
85 }
86 #endif
87
88 #endif // __8BYTE_H__