run code through clang-format

This commit is contained in:
Thomas Kunze 2019-07-15 21:23:51 +02:00
parent 141f9ddd81
commit 24834d0f51
2 changed files with 320 additions and 263 deletions

View File

@ -80,8 +80,11 @@
#define RX_WAIT_DATA_CON 3
#define TX_FRAME 4
#define BYTE_TIMEOUT 3 //milli seconds
#define CONFIRM_TIMEOUT 500 //milli seconds
void TpUartDataLinkLayer::loop(){
void TpUartDataLinkLayer::loop()
{
_receiveBuffer[0] = 0x29;
_receiveBuffer[1] = 0;
@ -91,13 +94,17 @@ void TpUartDataLinkLayer::loop(){
if (!_enabled)
return;
switch(_loopState){
switch (_loopState)
{
case IDLE:
if(_platform.uartAvailable()){
if (_platform.uartAvailable())
{
_loopState = RX_FIRST_BYTE;
}
else{
if(!_waitConfirm && !isTxQueueEmpty()){
else
{
if (!_waitConfirm && !isTxQueueEmpty())
{
loadNextTxFrame();
_waitConfirm = true;
_waitConfirmStartTime = _platform.millis();
@ -106,7 +113,8 @@ void TpUartDataLinkLayer::loop(){
}
break;
case TX_FRAME:
if(sendSingleFrameByte() == false){
if (sendSingleFrameByte() == false)
{
_loopState = IDLE;
}
break;
@ -115,7 +123,8 @@ void TpUartDataLinkLayer::loop(){
_lastByteRxTime = _platform.millis();
_RxByteCnt = 0;
_xorSum = 0;
if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND){
if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND)
{
buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_RxByteCnt++; //convert to L_DATA_EXTENDED
@ -123,52 +132,63 @@ void TpUartDataLinkLayer::loop(){
_loopState = RX_L_DATA;
break;
}
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND){
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND)
{
buffer[_RxByteCnt++] = rxByte;
_xorSum ^= rxByte;
_convert = false;
_loopState = RX_L_DATA;
break;
}
else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){
else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
{
println("got unexpected L_DATA_CON");
}
else if (rxByte == L_POLL_DATA_IND){
else if (rxByte == L_POLL_DATA_IND)
{
// not sure if this can happen
println("got L_POLL_DATA_IND");
}
else if ((rxByte & L_ACKN_MASK) == L_ACKN_IND){
else if ((rxByte & L_ACKN_MASK) == L_ACKN_IND)
{
// this can only happen in bus monitor mode
println("got L_ACKN_IND");
}
else if (rxByte == U_RESET_IND){
else if (rxByte == U_RESET_IND)
{
println("got U_RESET_IND");
}
else if((rxByte & U_STATE_IND) == U_STATE_IND){
else if ((rxByte & U_STATE_IND) == U_STATE_IND)
{
print("got U_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
else if((rxByte & U_FRAME_STATE_MASK) == U_FRAME_STATE_IND){
else if ((rxByte & U_FRAME_STATE_MASK) == U_FRAME_STATE_IND)
{
print("got U_FRAME_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
else if((rxByte & U_CONFIGURE_MASK) == U_CONFIGURE_IND){
else if ((rxByte & U_CONFIGURE_MASK) == U_CONFIGURE_IND)
{
print("got U_CONFIGURE_IND: 0x");
print(rxByte, HEX);
println();
}
else if(rxByte == U_FRAME_END_IND){
else if (rxByte == U_FRAME_END_IND)
{
println("got U_FRAME_END_IND");
}
else if(rxByte == U_STOP_MODE_IND){
else if (rxByte == U_STOP_MODE_IND)
{
println("got U_STOP_MODE_IND");
}
else if(rxByte == U_SYSTEM_STAT_IND){
else if (rxByte == U_SYSTEM_STAT_IND)
{
print("got U_SYSTEM_STAT_IND: 0x");
while (true){
while (true)
{
int tmp = _platform.readUart();
if (tmp < 0)
continue;
@ -178,7 +198,8 @@ void TpUartDataLinkLayer::loop(){
}
println();
}
else{
else
{
print("got UNEXPECTED: 0x");
print(rxByte, HEX);
println();
@ -186,87 +207,115 @@ void TpUartDataLinkLayer::loop(){
_loopState = IDLE;
break;
case RX_L_DATA:
if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT){
if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT)
{
_RxByteCnt = 0;
_loopState = IDLE;
println("Timeout during RX_L_DATA");
break;
}
if(!_platform.uartAvailable()) break;
if (!_platform.uartAvailable())
break;
_lastByteRxTime = _platform.millis();
rxByte = _platform.readUart();
if(_RxByteCnt == MAX_KNX_TELEGRAM_SIZE){
if (_RxByteCnt == MAX_KNX_TELEGRAM_SIZE)
{
_loopState = IDLE;
println("invalid telegram size");
}
else{
else
{
buffer[_RxByteCnt++] = rxByte;
}
if(_RxByteCnt==7){ //Destination Address + payload available
if (_RxByteCnt == 7)
{
//Destination Address + payload available
_xorSum ^= rxByte;
//check if echo
if (!((buffer[0]^_sendBuffer[0])&~0x20) && !memcmp(buffer+2,_sendBuffer+1,5)){ //ignore repeated bit of control byte
if (!((buffer[0] ^ _sendBuffer[0]) & ~0x20) && !memcmp(buffer + 2, _sendBuffer + 1, 5))
{ //ignore repeated bit of control byte
_isEcho = true;
}
else{
else
{
_isEcho = false;
}
//convert into Extended.ind
if(_convert){
if (_convert)
{
uint8_t payloadLength = buffer[6] & 0x0F;
buffer[1] = buffer[6] & 0xF0;
buffer[6] = payloadLength;
}
if(!_isEcho){
if (!_isEcho)
{
uint8_t c = 0x10;
//ceck if individual or group address
if ((buffer[6] & 0x80) == 0){ //individual
if(_deviceObject.induvidualAddress() == getWord(buffer + 4)){
if ((buffer[6] & 0x80) == 0)
{
//individual
if (_deviceObject.induvidualAddress() == getWord(buffer + 4))
{
c |= 0x01;
}
}
else{ //group
if(_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0){
else
{
//group
if (_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0)
{
c |= 0x01;
}
}
_platform.writeUart(c);
}
}
else if(_RxByteCnt == buffer[6]+7+2){ //complete Frame received, payloadLength+1 for TCPI +1 for CRC
if(rxByte == (uint8_t)(~_xorSum)){ //check if crc is correct
if(_isEcho && _sendBuffer != NULL){ //check if it is realy an echo, rx_crc = tx_crc
else if (_RxByteCnt == buffer[6] + 7 + 2)
{
//complete Frame received, payloadLength+1 for TCPI +1 for CRC
if (rxByte == (uint8_t)(~_xorSum))
{
//check if crc is correct
if (_isEcho && _sendBuffer != NULL)
{
//check if it is realy an echo, rx_crc = tx_crc
if (rxByte == _sendBuffer[_sendBufferLength - 1])
_isEcho = true;
else
_isEcho = false;
}
if(_isEcho){
if (_isEcho)
{
_loopState = RX_WAIT_DATA_CON;
}
else{
else
{
frameBytesReceived(_receiveBuffer, _RxByteCnt + 2);
_loopState = IDLE;
}
}
else{
else
{
println("frame with invalid crc ignored");
_loopState = IDLE;
}
}
else{
else
{
_xorSum ^= rxByte;
}
break;
case RX_WAIT_DATA_CON:
if(!_platform.uartAvailable()) break;
if (!_platform.uartAvailable())
break;
rxByte = _platform.readUart();
_lastByteRxTime = _platform.millis();
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
{
//println("L_DATA_CON received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, ((rxByte & SUCCESS) > 0));
_waitConfirm = false;
@ -275,7 +324,8 @@ void TpUartDataLinkLayer::loop(){
_sendBufferLength = 0;
_loopState = IDLE;
}
else{
else
{
//should not happen
println("expected L_DATA_CON not received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, false);
@ -290,8 +340,10 @@ void TpUartDataLinkLayer::loop(){
break;
}
if(_waitConfirm){
if (_platform.millis() - _waitConfirmStartTime > CONFIRM_TIMEOUT){
if (_waitConfirm)
{
if (_platform.millis() - _waitConfirmStartTime > CONFIRM_TIMEOUT)
{
println("L_DATA_CON not received within expected time");
uint8_t cemiBuffer[MAX_KNX_TELEGRAM_SIZE];
cemiBuffer[0] = 0x29;
@ -308,7 +360,6 @@ void TpUartDataLinkLayer::loop(){
}
}
bool TpUartDataLinkLayer::sendFrame(CemiFrame& frame)
{
if (!_enabled)
@ -318,7 +369,6 @@ bool TpUartDataLinkLayer::sendFrame(CemiFrame& frame)
return true;
}
void TpUartDataLinkLayer::resetChip()
{
uint8_t cmd = U_RESET_REQ;
@ -345,13 +395,12 @@ void TpUartDataLinkLayer::stopChip()
#endif
}
TpUartDataLinkLayer::TpUartDataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab,
NetworkLayer& layer, Platform& platform) : DataLinkLayer(devObj, addrTab, layer, platform)
NetworkLayer& layer, Platform& platform)
: DataLinkLayer(devObj, addrTab, layer, platform)
{
}
void TpUartDataLinkLayer::frameBytesReceived(uint8_t* buffer, uint16_t length)
{
//printHex("=>", buffer, length);
@ -365,7 +414,6 @@ void TpUartDataLinkLayer::dataConBytesReceived(uint8_t* buffer, uint16_t length,
//printHex("=>", buffer, length);
CemiFrame frame(buffer, length);
dataConReceived(frame, success);
}
void TpUartDataLinkLayer::enabled(bool value)
@ -394,14 +442,16 @@ bool TpUartDataLinkLayer::enabled() const
return _enabled;
}
bool TpUartDataLinkLayer::sendSingleFrameByte(){
bool TpUartDataLinkLayer::sendSingleFrameByte()
{
uint8_t cmd[2];
uint8_t idx = _TxByteCnt / 64;
if(_sendBuffer == NULL) return false;
if (_sendBuffer == NULL)
return false;
if(_TxByteCnt < _sendBufferLength){
if (_TxByteCnt < _sendBufferLength)
{
if (idx != _oldIdx)
{
_oldIdx = idx;
@ -420,13 +470,15 @@ bool TpUartDataLinkLayer::sendSingleFrameByte(){
_TxByteCnt++;
return true;
}
else{
else
{
_TxByteCnt = 0;
return false;
}
}
void TpUartDataLinkLayer::addFrameTxQueue(CemiFrame& frame){
void TpUartDataLinkLayer::addFrameTxQueue(CemiFrame& frame)
{
_tx_queue_frame_t* tx_frame = new _tx_queue_frame_t;
tx_frame->length = frame.telegramLengthtTP();
@ -434,25 +486,30 @@ void TpUartDataLinkLayer::addFrameTxQueue(CemiFrame& frame){
tx_frame->next = NULL;
frame.fillTelegramTP(tx_frame->data);
if (_tx_queue.back == NULL) {
if (_tx_queue.back == NULL)
{
_tx_queue.front = _tx_queue.back = tx_frame;
}
else{
else
{
_tx_queue.back->next = tx_frame;
_tx_queue.back = tx_frame;
}
}
bool TpUartDataLinkLayer::isTxQueueEmpty(){
if (_tx_queue.front == NULL){
bool TpUartDataLinkLayer::isTxQueueEmpty()
{
if (_tx_queue.front == NULL)
{
return true;
}
return false;
}
void TpUartDataLinkLayer::loadNextTxFrame(){
if (_tx_queue.front == NULL) {
void TpUartDataLinkLayer::loadNextTxFrame()
{
if (_tx_queue.front == NULL)
{
return;
}
_tx_queue_frame_t* tx_frame = _tx_queue.front;
@ -460,9 +517,9 @@ void TpUartDataLinkLayer::loadNextTxFrame(){
_sendBufferLength = tx_frame->length;
_tx_queue.front = tx_frame->next;
if (_tx_queue.front == NULL){
if (_tx_queue.front == NULL)
{
_tx_queue.back = NULL;
}
delete tx_frame;
}

View File

@ -4,15 +4,13 @@
#include "data_link_layer.h"
#define MAX_KNX_TELEGRAM_SIZE 263
#define BYTE_TIMEOUT 3 //milli seconds
#define CONFIRM_TIMEOUT 500 //milli seconds
class TpUartDataLinkLayer : public DataLinkLayer
{
using DataLinkLayer::_deviceObject;
using DataLinkLayer::_platform;
using DataLinkLayer::_groupAddressTable;
using DataLinkLayer::_platform;
public:
TpUartDataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab, NetworkLayer& layer,
Platform& platform);
@ -20,6 +18,7 @@ public:
void loop();
void enabled(bool value);
bool enabled() const;
private:
bool _enabled = false;
bool _waitConfirm = false;
@ -37,13 +36,15 @@ private:
uint32_t _lastByteRxTime;
uint32_t _waitConfirmStartTime;
struct _tx_queue_frame_t{
struct _tx_queue_frame_t
{
uint8_t* data;
uint16_t length;
_tx_queue_frame_t* next;
};
struct _tx_queue_t {
struct _tx_queue_t
{
_tx_queue_frame_t* front = NULL;
_tx_queue_frame_t* back = NULL;
} _tx_queue;
@ -58,4 +59,3 @@ private:
void resetChip();
void stopChip();
};