15#define UART_RX_BUFFER_SIZE 192
16#define UART_TX_BUFFER_SIZE 300
17#define UART_TX_ENCRYPTION_BUFFER_SIZE AES_BLOCK_SIZE
88 const uint8_t*
const data,
Event listener.
Definition: cs_EventListener.h:17
Definition: cs_UartCommandHandler.h:12
Class that implements the binary UART protocol.
Definition: cs_UartHandler.h:28
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.
void handleEncryptedUartMsg(uint8_t *data, uint16_t size)
Handles encrypted UART msg.
cs_ret_code_t writeStartByte()
Write the start byte.
uint16_t _sizeToRead
Size of the msg to read, including header and tail, excluding start byte.
Definition: cs_UartHandler.h:150
UartHandler()=default
Constructor.
void resetReadBuf()
Reset the read buffer and status.
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.
uint16_t _readBufferIdx
Where to read the next byte into the read buffer.
Definition: cs_UartHandler.h:138
bool mustEncrypt(UartProtocol::Encrypt encrypt, UartOpcodeTx opCode)
Whether to encrypt an outgoing msg.
void init(serial_enable_t serialEnabled)
Initialize the class.
void operator=(UartHandler const &)=delete
This class is singleton, deny implementation.
ret_code_t writeMsg(UartOpcodeTx opCode)
Convenience method to write a msg over UART without payload data.
void handleUartMsg(uint8_t *data, uint16_t size, EncryptionAccessLevel accessLevel)
Handles unencrypted UART msg.
uint8_t * _readBuffer
Pointer to the read buffer.
Definition: cs_UartHandler.h:135
uint8_t _encryptionBufferWritten
Number of bytes written to the encryption buffer currently.
Definition: cs_UartHandler.h:168
void handleEvent(event_t &event)
Handle events as EventListener.
bool _startedReading
Keeps up whether we started reading into the read buffer.
Definition: cs_UartHandler.h:141
uint16_t _crc
Keeps up the crc so far.
Definition: cs_UartHandler.h:177
void handleMsg(uint8_t *data, uint16_t size)
Handles read msgs.
TYPIFY(CONFIG_CROWNSTONE_ID) _stoneId=0
Stone ID, part of the msg header.
void writeErrorReplyStatus()
Write an error reply: status.
ret_code_t writeMsgEnd(UartOpcodeTx opCode, UartProtocol::Encrypt encrypt=UartProtocol::ENCRYPT_ACCORDING_TO_TYPE)
Write a msg over UART in a streaming manner.
cs_ret_code_t writeBytes(cs_data_t data, bool updateCrc)
Write bytes to UART.
bool _escapeNextByte
Keeps up whether to escape the next read byte.
Definition: cs_UartHandler.h:144
void handleMsg(cs_data_t *msgData)
Handles read msgs (private function)
uint8_t _encryptionBlocksWritten
Number of blocks encrypted so far.
Definition: cs_UartHandler.h:171
bool _initialized
Definition: cs_UartHandler.h:128
UartCommandHandler _commandHandler
Definition: cs_UartHandler.h:130
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.
bool _readBusy
Whether reading is busy (if true, can't read anything, until the read buffer was processed)
Definition: cs_UartHandler.h:153
uint8_t * _encryptionBuffer
Encryption buffer.
Definition: cs_UartHandler.h:165
cs_ret_code_t writeEncryptedBlock(cs_data_t key)
Encrypt 1 block of data from encryption buffer, update CRC, and write to uart.
cs_buffer_size_t getEncryptedBufferSize(cs_buffer_size_t uartMsgSize)
Get required buffer size, given the size of a uart msg.
uint8_t * _writeBuffer
Write buffer. Currently only used as result buffer for control commands.
Definition: cs_UartHandler.h:158
cs_ret_code_t writeEncryptedPart(cs_data_t data)
Encrypt given data and write it to UART.
cs_ret_code_t writeWrapperStart(UartMsgType msgType, uint16_t payloadSize)
Writes wrapper header (including start and size), and initializes CRC.
cs_ret_code_t writeEncryptedStart(cs_buffer_size_t uartMsgSize)
Write encrypted header, and update CRC.
static UartHandler & getInstance()
Gets a static singleton (no dynamic memory allocation) of this class.
Definition: cs_UartHandler.h:31
bool mustBeEncrypted(UartOpcodeRx opCode)
Whether an incoming msg must've been encrypted.
UartHandler(UartHandler const &)=delete
This class is singleton, deny implementation.
cs_ret_code_t writeEncryptedEnd()
Write last encryption block, and update CRC.
encryption_nonce_t _writeNonce
Packet nonce to use for writing current msg.
Definition: cs_UartHandler.h:174
void onRead(uint8_t val)
To be called when a byte was read.
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
uint32_t ret_code_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Apr.
Definition: cs_Nordic.h:14
EncryptionAccessLevel
Packets (structs) that are used over the air, over uart, or stored in flash.
Definition: cs_Packets.h:36
serial_enable_t
Definition: cs_SerialTypes.h:35
uint16_t cs_buffer_size_t
Definition: cs_Typedefs.h:20
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
UartOpcodeTx
Send messages over the UART to someone listening.
Definition: cs_UartOpcodes.h:59
UartOpcodeRx
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Jun 18,...
Definition: cs_UartOpcodes.h:15
UartMsgType
Definition: cs_UartProtocol.h:25
Encrypt
Option whether a UART message should be encrypted.
Definition: cs_UartOpcodes.h:151
@ ENCRYPT_ACCORDING_TO_TYPE
Definition: cs_UartOpcodes.h:155
Packets (structs) that are used internally in the firmware, and can be changed freely.
Definition: cs_PacketsInternal.h:27
Nonce used for encryption.
Definition: cs_Packets.h:51