Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_UartProtocol.h
Go to the documentation of this file.
1
8#pragma once
9
12
13#include <cstdint>
14
15// bit: 7654 3210
16#define UART_START_BYTE 0x7E // "~" 0111 1110 resets the state
17#define UART_ESCAPE_BYTE 0x5C // "\" 0101 1100 next byte gets bits flipped that are in flip mask
18#define UART_ESCAPE_FLIP_MASK 0x40 // 0100 0000
19
20#define UART_PROTOCOL_MAJOR 1
21#define UART_PROTOCOL_MINOR 0
22
23#define UART_PROTOCOL_VALIDATION 0xCAFEBABE
24
25enum class UartMsgType : uint8_t {
26 UART_MSG = 0, // Payload is: uart_msg_header + data
27 ENCRYPTED_UART_MSG = 128, // Payload is: uart_encrypted_msg_header + encrypted blocks
28};
29
34struct __attribute__((__packed__)) uart_msg_size_header_t {
35 uint16_t size = 0; // Size of all remaining bytes, including tail.
36};
37
41struct __attribute__((__packed__)) uart_msg_wrapper_header_t {
42 uint8_t protocolMajor = UART_PROTOCOL_MAJOR;
43 uint8_t protocolMinor = UART_PROTOCOL_MINOR;
44 uint8_t type = 0;
45 // Followed by payload
46};
47
51struct __attribute__((__packed__)) uart_encrypted_msg_header_t {
52 uint8_t packetNonce[PACKET_NONCE_LENGTH];
53 uint8_t keyId;
54 // Followed by encrypted data
55};
56
60struct __attribute__((__packed__)) uart_encrypted_data_header_t {
61 uint32_t validation;
62 uint16_t size; // Size of uart msg.
63 // Followed by uart msg and padding.
64};
65
69struct __attribute__((__packed__)) uart_msg_header_t {
70 uint16_t type; // Type of message, see ..
71 // Followed by data.
72};
73
77struct __attribute__((__packed__)) uart_msg_tail_t {
78 uint16_t crc; // The CRC16 (CRC-16-CCITT) of everything after the size field.
79};
80
81namespace UartProtocol {
85void escape(uint8_t& val);
86
90void unEscape(uint8_t& val);
91
95uint16_t crc16(const uint8_t* data, uint16_t size);
96
100void crc16(const uint8_t* data, const uint16_t size, uint16_t& crc);
101}; // namespace UartProtocol
#define PACKET_NONCE_LENGTH
Definition: cs_Packets.h:26
#define UART_PROTOCOL_MAJOR
Definition: cs_UartProtocol.h:20
#define UART_PROTOCOL_MINOR
Definition: cs_UartProtocol.h:21
UartMsgType
Definition: cs_UartProtocol.h:25
Definition: cs_UartOpcodes.h:146
void unEscape(uint8_t &val)
Unescape a value: converts an escaped value back to the original value.
void escape(uint8_t &val)
Escape a value: converts a value to the an escaped version.
uint16_t crc16(const uint8_t *data, uint16_t size)
Calculate the CRC of given data.
Encrypted header for encrypted uart msg.
Definition: cs_UartProtocol.h:60
uint32_t validation
Definition: cs_UartProtocol.h:61
uint16_t size
Definition: cs_UartProtocol.h:62
Non-encrypted header for encrypted uart msg.
Definition: cs_UartProtocol.h:51
uint8_t keyId
Definition: cs_UartProtocol.h:53
UART message, either non-encrypted, or after decryption.
Definition: cs_UartProtocol.h:69
uint16_t type
Definition: cs_UartProtocol.h:70
Very first header, right after the start byte.
Definition: cs_UartProtocol.h:34
Final bytes.
Definition: cs_UartProtocol.h:77
uint16_t crc
Definition: cs_UartProtocol.h:78
Wrapper header, after the initial size field.
Definition: cs_UartProtocol.h:41