PER Firmware
Loading...
Searching...
No Matches
ubx_common.h
Go to the documentation of this file.
1
10#ifndef UBX_COMMON_H
11#define UBX_COMMON_H
12
13#include <stdint.h>
14#include <stddef.h>
15
16static constexpr size_t UBX_HEADER_LENGTH = 6;
17static constexpr uint8_t UBX_SYNC_B0 = 0xB5;
18static constexpr uint8_t UBX_SYNC_B1 = 0x62;
19typedef struct {
20 uint8_t sync_word[2];
22 uint8_t message_id;
25static_assert(sizeof(ubx_header_t) == UBX_HEADER_LENGTH, "ubx_header_t size must be 6 bytes");
26
27static constexpr size_t UBX_CHECKSUM_LENGTH = 2;
29
30static inline bool UBX_is_matching_header(
31 const ubx_header_t *header,
32 const ubx_header_t *expected
33) {
34 return (header->sync_word[0] == expected->sync_word[0])
35 && (header->sync_word[1] == expected->sync_word[1])
36 && (header->message_class == expected->message_class)
37 && (header->message_id == expected->message_id)
38 && (header->payload_length == expected->payload_length);
39}
40
41static inline bool UBX_is_valid_checksum(
42 const uint8_t *rx_buffer,
43 const size_t payload_length
44) {
45 const size_t checksum_offset = UBX_HEADER_LENGTH + payload_length;
46
47 // the checksum does not include the sync word
48 // algorithm defined in ZED-F9P-Protocols-1.32.pdf @ 3.4
49 uint8_t ck_a = 0;
50 uint8_t ck_b = 0;
51 for (size_t i = 2; i < checksum_offset; i++) {
52 ck_a = ck_a + rx_buffer[i];
53 ck_b = ck_b + ck_a;
54 }
55
56 uint8_t checksum[2] = {
57 rx_buffer[checksum_offset + 0],
58 rx_buffer[checksum_offset + 1]
59 };
60
61 return (ck_a == checksum[0]) && (ck_b == checksum[1]);
62}
63
64#endif // UBX_COMMON_H
Definition ubx_common.h:19
uint8_t sync_word[2]
Definition ubx_common.h:20
uint8_t message_id
Definition ubx_common.h:22
uint16_t payload_length
Definition ubx_common.h:23
uint8_t message_class
Definition ubx_common.h:21
static constexpr size_t UBX_OVERHEAD
Definition ubx_common.h:28
static constexpr uint8_t UBX_SYNC_B0
Definition ubx_common.h:17
static constexpr uint8_t UBX_SYNC_B1
Definition ubx_common.h:18
static bool UBX_is_matching_header(const ubx_header_t *header, const ubx_header_t *expected)
Definition ubx_common.h:30
static constexpr size_t UBX_CHECKSUM_LENGTH
Definition ubx_common.h:27
static constexpr size_t UBX_HEADER_LENGTH
Definition ubx_common.h:16
static bool UBX_is_valid_checksum(const uint8_t *rx_buffer, const size_t payload_length)
Definition ubx_common.h:41