Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Class that implements the binary UART protocol. More...
#include <cs_UartHandler.h>
Public Member Functions | |
void | init (serial_enable_t serialEnabled) |
Initialize the class. More... | |
ret_code_t | writeMsg (UartOpcodeTx opCode, uint8_t *data, uint16_t size, UartProtocol::Encrypt encrypt=UartProtocol::ENCRYPT_ACCORDING_TO_TYPE) |
Write a msg over UART. More... | |
ret_code_t | writeMsg (UartOpcodeTx opCode) |
Convenience method to write a msg over UART without payload data. More... | |
ret_code_t | writeMsgStart (UartOpcodeTx opCode, uint16_t size, UartProtocol::Encrypt encrypt=UartProtocol::ENCRYPT_ACCORDING_TO_TYPE) |
Write a msg over UART in a streaming manner. More... | |
ret_code_t | writeMsgPart (UartOpcodeTx opCode, const uint8_t *const data, uint16_t size, UartProtocol::Encrypt encrypt=UartProtocol::ENCRYPT_ACCORDING_TO_TYPE) |
Write a msg over UART in a streaming manner. More... | |
ret_code_t | writeMsgEnd (UartOpcodeTx opCode, UartProtocol::Encrypt encrypt=UartProtocol::ENCRYPT_ACCORDING_TO_TYPE) |
Write a msg over UART in a streaming manner. More... | |
void | onRead (uint8_t val) |
To be called when a byte was read. More... | |
void | handleMsg (cs_data_t *msgData) |
Handles read msgs (private function) More... | |
Static Public Member Functions | |
static UartHandler & | getInstance () |
Gets a static singleton (no dynamic memory allocation) of this class. More... | |
Private Member Functions | |
UartHandler ()=default | |
Constructor. More... | |
UartHandler (UartHandler const &)=delete | |
This class is singleton, deny implementation. More... | |
void | operator= (UartHandler const &)=delete |
This class is singleton, deny implementation. More... | |
TYPIFY (CONFIG_CROWNSTONE_ID) _stoneId=0 | |
Stone ID, part of the msg header. More... | |
cs_ret_code_t | writeStartByte () |
Write the start byte. More... | |
cs_ret_code_t | writeBytes (cs_data_t data, bool updateCrc) |
Write bytes to UART. More... | |
cs_ret_code_t | writeWrapperStart (UartMsgType msgType, uint16_t payloadSize) |
Writes wrapper header (including start and size), and initializes CRC. More... | |
bool | mustEncrypt (UartProtocol::Encrypt encrypt, UartOpcodeTx opCode) |
Whether to encrypt an outgoing msg. More... | |
bool | mustBeEncrypted (UartOpcodeRx opCode) |
Whether an incoming msg must've been encrypted. More... | |
cs_buffer_size_t | getEncryptedBufferSize (cs_buffer_size_t uartMsgSize) |
Get required buffer size, given the size of a uart msg. More... | |
cs_ret_code_t | writeEncryptedStart (cs_buffer_size_t uartMsgSize) |
Write encrypted header, and update CRC. More... | |
cs_ret_code_t | writeEncryptedPart (cs_data_t data) |
Encrypt given data and write it to UART. More... | |
cs_ret_code_t | writeEncryptedEnd () |
Write last encryption block, and update CRC. More... | |
cs_ret_code_t | writeEncryptedBlock (cs_data_t key) |
Encrypt 1 block of data from encryption buffer, update CRC, and write to uart. More... | |
void | writeErrorReplyStatus () |
Write an error reply: status. More... | |
void | handleMsg (uint8_t *data, uint16_t size) |
Handles read msgs. More... | |
void | handleEncryptedUartMsg (uint8_t *data, uint16_t size) |
Handles encrypted UART msg. More... | |
void | handleUartMsg (uint8_t *data, uint16_t size, EncryptionAccessLevel accessLevel) |
Handles unencrypted UART msg. More... | |
void | resetReadBuf () |
Reset the read buffer and status. More... | |
void | handleEvent (event_t &event) |
Handle events as EventListener. More... | |
![]() | |
EventListener () | |
virtual | ~EventListener () |
unregisters the listener. More... | |
virtual void | handleEvent (event_t &event)=0 |
Handle events. More... | |
void | listen () |
Registers this with the EventDispatcher. More... | |
Private Attributes | |
bool | _initialized = false |
UartCommandHandler | _commandHandler |
uint8_t * | _readBuffer = nullptr |
Pointer to the read buffer. More... | |
uint16_t | _readBufferIdx = 0 |
Where to read the next byte into the read buffer. More... | |
bool | _startedReading = false |
Keeps up whether we started reading into the read buffer. More... | |
bool | _escapeNextByte = false |
Keeps up whether to escape the next read byte. More... | |
uint16_t | _sizeToRead = 0 |
Size of the msg to read, including header and tail, excluding start byte. More... | |
bool | _readBusy = false |
Whether reading is busy (if true, can't read anything, until the read buffer was processed) More... | |
uint8_t * | _writeBuffer = nullptr |
Write buffer. Currently only used as result buffer for control commands. More... | |
uint8_t * | _encryptionBuffer = nullptr |
Encryption buffer. More... | |
uint8_t | _encryptionBufferWritten = 0 |
Number of bytes written to the encryption buffer currently. More... | |
uint8_t | _encryptionBlocksWritten = 0 |
Number of blocks encrypted so far. More... | |
encryption_nonce_t | _writeNonce |
Packet nonce to use for writing current msg. More... | |
uint16_t | _crc |
Keeps up the crc so far. More... | |
Class that implements the binary UART protocol.
|
privatedefault |
Constructor.
|
privatedelete |
This class is singleton, deny implementation.
|
private |
Get required buffer size, given the size of a uart msg.
[in] | uartMsgSize | Size of the msg to encrypt. |
|
inlinestatic |
Gets a static singleton (no dynamic memory allocation) of this class.
|
private |
Handles encrypted UART msg.
Data includes encryption header.
|
privatevirtual |
Handle events as EventListener.
Implements EventListener.
void UartHandler::handleMsg | ( | cs_data_t * | msgData | ) |
Handles read msgs (private function)
Message data starts after START byte, and includes the tail (CRC). Wrapper header size has been checked already.
|
private |
Handles read msgs.
Data starts after size header, and includes wrapper header and tail (CRC).
|
private |
Handles unencrypted UART msg.
Data includes uart msg header.
void UartHandler::init | ( | serial_enable_t | serialEnabled | ) |
Initialize the class.
|
private |
Whether an incoming msg must've been encrypted.
|
private |
Whether to encrypt an outgoing msg.
void UartHandler::onRead | ( | uint8_t | val | ) |
To be called when a byte was read.
Can be called from interrupt.
[in] | val | Value that was read. |
|
privatedelete |
This class is singleton, deny implementation.
|
private |
Reset the read buffer and status.
|
privatepure virtual |
Stone ID, part of the msg header.
|
private |
Write bytes to UART.
Values get escaped when necessary.
[in] | data | Data to write to UART. |
[in] | updateCrc | Whether to update the CRC with thise data. |
|
private |
Encrypt 1 block of data from encryption buffer, update CRC, and write to uart.
[in] | key | Key to use for encryption. |
|
private |
Write last encryption block, and update CRC.
|
private |
Encrypt given data and write it to UART.
Also updates the CRC.
[in] | data | Data to encrypt and write. |
|
private |
Write encrypted header, and update CRC.
[in] | uartMsgSize | Size of the msg to encrypt. |
|
private |
Write an error reply: status.
ret_code_t UartHandler::writeMsg | ( | UartOpcodeTx | opCode | ) |
Convenience method to write a msg over UART without payload data.
ret_code_t UartHandler::writeMsg | ( | UartOpcodeTx | opCode, |
uint8_t * | data, | ||
uint16_t | size, | ||
UartProtocol::Encrypt | encrypt = UartProtocol::ENCRYPT_ACCORDING_TO_TYPE |
||
) |
Write a msg over UART.
[in] | opCode | OpCode of the msg. |
[in] | data | Pointer to the msg to be sent. |
[in] | size | Size of the msg. |
[in] | encrypt | How to encrypt the msg. |
ret_code_t UartHandler::writeMsgEnd | ( | UartOpcodeTx | opCode, |
UartProtocol::Encrypt | encrypt = UartProtocol::ENCRYPT_ACCORDING_TO_TYPE |
||
) |
Write a msg over UART in a streaming manner.
This finalizes the stream.
[in] | opCode | OpCode of the msg. |
[in] | encrypt | How to encrypt the msg. |
ret_code_t UartHandler::writeMsgPart | ( | UartOpcodeTx | opCode, |
const uint8_t *const | data, | ||
uint16_t | size, | ||
UartProtocol::Encrypt | encrypt = UartProtocol::ENCRYPT_ACCORDING_TO_TYPE |
||
) |
Write a msg over UART in a streaming manner.
After writing the start of the msg, stream the data with calls to this functions.
[in] | opCode | OpCode of the msg. |
[in] | data | Pointer to the data part to be sent. |
[in] | size | Size of this data part. |
[in] | encrypt | How to encrypt the msg. |
ret_code_t UartHandler::writeMsgStart | ( | UartOpcodeTx | opCode, |
uint16_t | size, | ||
UartProtocol::Encrypt | encrypt = UartProtocol::ENCRYPT_ACCORDING_TO_TYPE |
||
) |
Write a msg over UART in a streaming manner.
Must be followed by 1 or more writeMsgPart(), followed by 1 writeMsgEnd().
[in] | opCode | OpCode of the msg. |
[in] | size | Size of the msg. |
[in] | encrypt | How to encrypt the msg. |
|
private |
Write the start byte.
|
private |
Writes wrapper header (including start and size), and initializes CRC.
|
private |
|
private |
Keeps up the crc so far.
|
private |
Number of blocks encrypted so far.
|
private |
Encryption buffer.
Used to encrypt outgoing msgs.
Only has to be 1 block size large, as we can stream the writes.
|
private |
Number of bytes written to the encryption buffer currently.
|
private |
Keeps up whether to escape the next read byte.
|
private |
|
private |
Pointer to the read buffer.
|
private |
Where to read the next byte into the read buffer.
|
private |
Whether reading is busy (if true, can't read anything, until the read buffer was processed)
|
private |
Size of the msg to read, including header and tail, excluding start byte.
Once set, the read buffer index is set to 0.
|
private |
Keeps up whether we started reading into the read buffer.
|
private |
Write buffer. Currently only used as result buffer for control commands.
|
private |
Packet nonce to use for writing current msg.