Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
UartHandler Class Referenceabstract

Class that implements the binary UART protocol. More...

#include <cs_UartHandler.h>

Inheritance diagram for UartHandler:
Collaboration diagram for UartHandler:

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 UartHandlergetInstance ()
 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...
 
- Private Member Functions inherited from EventListener
 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...
 

Detailed Description

Class that implements the binary UART protocol.

  • Wraps messages.
  • Parses incoming UART messages.
  • Handles internal events, to write UART messages.
  • Encrypts outgoing UART messages.
  • Decrypts incoming UART messages.

Constructor & Destructor Documentation

◆ UartHandler() [1/2]

UartHandler::UartHandler ( )
privatedefault

Constructor.

◆ UartHandler() [2/2]

UartHandler::UartHandler ( UartHandler const &  )
privatedelete

This class is singleton, deny implementation.

Member Function Documentation

◆ getEncryptedBufferSize()

cs_buffer_size_t UartHandler::getEncryptedBufferSize ( cs_buffer_size_t  uartMsgSize)
private

Get required buffer size, given the size of a uart msg.

Parameters
[in]uartMsgSizeSize of the msg to encrypt.
Returns
Size of the buffer, includes size of encryption headers.

◆ getInstance()

static UartHandler & UartHandler::getInstance ( )
inlinestatic

Gets a static singleton (no dynamic memory allocation) of this class.

◆ handleEncryptedUartMsg()

void UartHandler::handleEncryptedUartMsg ( uint8_t *  data,
uint16_t  size 
)
private

Handles encrypted UART msg.

Data includes encryption header.

◆ handleEvent()

void UartHandler::handleEvent ( event_t event)
privatevirtual

Handle events as EventListener.

Implements EventListener.

◆ handleMsg() [1/2]

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.

◆ handleMsg() [2/2]

void UartHandler::handleMsg ( uint8_t *  data,
uint16_t  size 
)
private

Handles read msgs.

Data starts after size header, and includes wrapper header and tail (CRC).

◆ handleUartMsg()

void UartHandler::handleUartMsg ( uint8_t *  data,
uint16_t  size,
EncryptionAccessLevel  accessLevel 
)
private

Handles unencrypted UART msg.

Data includes uart msg header.

◆ init()

void UartHandler::init ( serial_enable_t  serialEnabled)

Initialize the class.

  • Allocates memory.
  • Reads settings from State.
  • Starts listening for events

◆ mustBeEncrypted()

bool UartHandler::mustBeEncrypted ( UartOpcodeRx  opCode)
private

Whether an incoming msg must've been encrypted.

◆ mustEncrypt()

bool UartHandler::mustEncrypt ( UartProtocol::Encrypt  encrypt,
UartOpcodeTx  opCode 
)
private

Whether to encrypt an outgoing msg.

◆ onRead()

void UartHandler::onRead ( uint8_t  val)

To be called when a byte was read.

Can be called from interrupt.

Parameters
[in]valValue that was read.

◆ operator=()

void UartHandler::operator= ( UartHandler const &  )
privatedelete

This class is singleton, deny implementation.

◆ resetReadBuf()

void UartHandler::resetReadBuf ( )
private

Reset the read buffer and status.

◆ TYPIFY()

UartHandler::TYPIFY ( CONFIG_CROWNSTONE_ID  )
privatepure virtual

Stone ID, part of the msg header.

◆ writeBytes()

cs_ret_code_t UartHandler::writeBytes ( cs_data_t  data,
bool  updateCrc 
)
private

Write bytes to UART.

Values get escaped when necessary.

Parameters
[in]dataData to write to UART.
[in]updateCrcWhether to update the CRC with thise data.
Returns
Result code.

◆ writeEncryptedBlock()

cs_ret_code_t UartHandler::writeEncryptedBlock ( cs_data_t  key)
private

Encrypt 1 block of data from encryption buffer, update CRC, and write to uart.

Parameters
[in]keyKey to use for encryption.
Returns
Return code.

◆ writeEncryptedEnd()

cs_ret_code_t UartHandler::writeEncryptedEnd ( )
private

Write last encryption block, and update CRC.

◆ writeEncryptedPart()

cs_ret_code_t UartHandler::writeEncryptedPart ( cs_data_t  data)
private

Encrypt given data and write it to UART.

Also updates the CRC.

Parameters
[in]dataData to encrypt and write.
Returns
Return code.

◆ writeEncryptedStart()

cs_ret_code_t UartHandler::writeEncryptedStart ( cs_buffer_size_t  uartMsgSize)
private

Write encrypted header, and update CRC.

Parameters
[in]uartMsgSizeSize of the msg to encrypt.
Returns
Result code.

◆ writeErrorReplyStatus()

void UartHandler::writeErrorReplyStatus ( )
private

Write an error reply: status.

◆ writeMsg() [1/2]

ret_code_t UartHandler::writeMsg ( UartOpcodeTx  opCode)

Convenience method to write a msg over UART without payload data.

◆ writeMsg() [2/2]

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.

Parameters
[in]opCodeOpCode of the msg.
[in]dataPointer to the msg to be sent.
[in]sizeSize of the msg.
[in]encryptHow to encrypt the msg.

◆ writeMsgEnd()

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.

Parameters
[in]opCodeOpCode of the msg.
[in]encryptHow to encrypt the msg.

◆ writeMsgPart()

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.

Parameters
[in]opCodeOpCode of the msg.
[in]dataPointer to the data part to be sent.
[in]sizeSize of this data part.
[in]encryptHow to encrypt the msg.

◆ writeMsgStart()

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().

Parameters
[in]opCodeOpCode of the msg.
[in]sizeSize of the msg.
[in]encryptHow to encrypt the msg.

◆ writeStartByte()

cs_ret_code_t UartHandler::writeStartByte ( )
private

Write the start byte.

◆ writeWrapperStart()

cs_ret_code_t UartHandler::writeWrapperStart ( UartMsgType  msgType,
uint16_t  payloadSize 
)
private

Writes wrapper header (including start and size), and initializes CRC.

Member Data Documentation

◆ _commandHandler

UartCommandHandler UartHandler::_commandHandler
private

◆ _crc

uint16_t UartHandler::_crc
private

Keeps up the crc so far.

◆ _encryptionBlocksWritten

uint8_t UartHandler::_encryptionBlocksWritten = 0
private

Number of blocks encrypted so far.

◆ _encryptionBuffer

uint8_t* UartHandler::_encryptionBuffer = nullptr
private

Encryption buffer.

Used to encrypt outgoing msgs.

Only has to be 1 block size large, as we can stream the writes.

◆ _encryptionBufferWritten

uint8_t UartHandler::_encryptionBufferWritten = 0
private

Number of bytes written to the encryption buffer currently.

◆ _escapeNextByte

bool UartHandler::_escapeNextByte = false
private

Keeps up whether to escape the next read byte.

◆ _initialized

bool UartHandler::_initialized = false
private

◆ _readBuffer

uint8_t* UartHandler::_readBuffer = nullptr
private

Pointer to the read buffer.

◆ _readBufferIdx

uint16_t UartHandler::_readBufferIdx = 0
private

Where to read the next byte into the read buffer.

◆ _readBusy

bool UartHandler::_readBusy = false
private

Whether reading is busy (if true, can't read anything, until the read buffer was processed)

◆ _sizeToRead

uint16_t UartHandler::_sizeToRead = 0
private

Size of the msg to read, including header and tail, excluding start byte.

Once set, the read buffer index is set to 0.

◆ _startedReading

bool UartHandler::_startedReading = false
private

Keeps up whether we started reading into the read buffer.

◆ _writeBuffer

uint8_t* UartHandler::_writeBuffer = nullptr
private

Write buffer. Currently only used as result buffer for control commands.

◆ _writeNonce

encryption_nonce_t UartHandler::_writeNonce
private

Packet nonce to use for writing current msg.


The documentation for this class was generated from the following file: