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

View File

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