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

@ -51,37 +51,40 @@
#define SUCCESS 0x80 #define SUCCESS 0x80
// control services, device specific // control services, device specific
#define U_RESET_IND 0x03 #define U_RESET_IND 0x03
#define U_STATE_IND 0x07 #define U_STATE_IND 0x07
#define SLAVE_COLLISION 0x80 #define SLAVE_COLLISION 0x80
#define RECEIVE_ERROR 0x40 #define RECEIVE_ERROR 0x40
#define TRANSMIT_ERROR 0x20 #define TRANSMIT_ERROR 0x20
#define PROTOCOL_ERROR 0x10 #define PROTOCOL_ERROR 0x10
#define TEMPERATURE_WARNING 0x08 #define TEMPERATURE_WARNING 0x08
#define U_FRAME_STATE_IND 0x13 #define U_FRAME_STATE_IND 0x13
#define U_FRAME_STATE_MASK 0x17 #define U_FRAME_STATE_MASK 0x17
#define PARITY_BIT_ERROR 0x80 #define PARITY_BIT_ERROR 0x80
#define CHECKSUM_LENGTH_ERROR 0x40 #define CHECKSUM_LENGTH_ERROR 0x40
#define TIMING_ERROR 0x20 #define TIMING_ERROR 0x20
#define U_CONFIGURE_IND 0x01 #define U_CONFIGURE_IND 0x01
#define U_CONFIGURE_MASK 0x83 #define U_CONFIGURE_MASK 0x83
#define AUTO_ACKNOWLEDGE 0x20 #define AUTO_ACKNOWLEDGE 0x20
#define AUTO_POLLING 0x10 #define AUTO_POLLING 0x10
#define CRC_CCITT 0x80 #define CRC_CCITT 0x80
#define FRAME_END_WITH_MARKER 0x40 #define FRAME_END_WITH_MARKER 0x40
#define U_FRAME_END_IND 0xCB #define U_FRAME_END_IND 0xCB
#define U_STOP_MODE_IND 0x2B #define U_STOP_MODE_IND 0x2B
#define U_SYSTEM_STAT_IND 0x4B #define U_SYSTEM_STAT_IND 0x4B
//loop states //loop states
#define IDLE 0 #define IDLE 0
#define RX_FIRST_BYTE 1 #define RX_FIRST_BYTE 1
#define RX_L_DATA 2 #define RX_L_DATA 2
#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,224 +94,272 @@ void TpUartDataLinkLayer::loop(){
if (!_enabled) if (!_enabled)
return; return;
switch(_loopState){ switch (_loopState)
case IDLE: {
if(_platform.uartAvailable()){ case IDLE:
_loopState = RX_FIRST_BYTE; if (_platform.uartAvailable())
} {
else{ _loopState = RX_FIRST_BYTE;
if(!_waitConfirm && !isTxQueueEmpty()){ }
loadNextTxFrame(); else
_waitConfirm = true; {
_waitConfirmStartTime = _platform.millis(); if (!_waitConfirm && !isTxQueueEmpty())
_loopState = TX_FRAME; {
loadNextTxFrame();
_waitConfirm = true;
_waitConfirmStartTime = _platform.millis();
_loopState = TX_FRAME;
}
} }
}
break;
case TX_FRAME:
if(sendSingleFrameByte() == false){
_loopState = IDLE;
}
break;
case RX_FIRST_BYTE:
rxByte =_platform.readUart();
_lastByteRxTime = _platform.millis();
_RxByteCnt = 0;
_xorSum = 0;
if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND){
buffer[_RxByteCnt++]=rxByte;
_xorSum ^= rxByte;
_RxByteCnt++; //convert to L_DATA_EXTENDED
_convert = true;
_loopState=RX_L_DATA;
break; break;
} case TX_FRAME:
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND){ if (sendSingleFrameByte() == false)
buffer[_RxByteCnt++]=rxByte; {
_xorSum ^= rxByte; _loopState = IDLE;
_convert = false; }
_loopState=RX_L_DATA;
break; break;
} case RX_FIRST_BYTE:
else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){ rxByte = _platform.readUart();
println("got unexpected L_DATA_CON"); _lastByteRxTime = _platform.millis();
} _RxByteCnt = 0;
else if (rxByte == L_POLL_DATA_IND){ _xorSum = 0;
// not sure if this can happen if ((rxByte & L_DATA_MASK) == L_DATA_STANDARD_IND)
println("got L_POLL_DATA_IND"); {
} buffer[_RxByteCnt++] = rxByte;
else if ((rxByte & L_ACKN_MASK) == L_ACKN_IND){ _xorSum ^= rxByte;
_RxByteCnt++; //convert to L_DATA_EXTENDED
// this can only happen in bus monitor mode _convert = true;
println("got L_ACKN_IND"); _loopState = RX_L_DATA;
}
else if (rxByte == U_RESET_IND){
println("got U_RESET_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){
print("got U_FRAME_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
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){
println("got U_FRAME_END_IND");
}
else if(rxByte == U_STOP_MODE_IND){
println("got U_STOP_MODE_IND");
}
else if(rxByte == U_SYSTEM_STAT_IND){
print("got U_SYSTEM_STAT_IND: 0x");
while (true){
int tmp = _platform.readUart();
if (tmp < 0)
continue;
print(tmp, HEX);
break; break;
} }
println(); else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND)
} {
else{ buffer[_RxByteCnt++] = rxByte;
print("got UNEXPECTED: 0x"); _xorSum ^= rxByte;
print(rxByte, HEX); _convert = false;
println(); _loopState = RX_L_DATA;
} break;
_loopState = IDLE; }
break; else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
case RX_L_DATA: {
if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT){ println("got unexpected L_DATA_CON");
_RxByteCnt = 0; }
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)
{
// this can only happen in bus monitor mode
println("got L_ACKN_IND");
}
else if (rxByte == U_RESET_IND)
{
println("got U_RESET_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)
{
print("got U_FRAME_STATE_IND: 0x");
print(rxByte, HEX);
println();
}
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)
{
println("got U_FRAME_END_IND");
}
else if (rxByte == U_STOP_MODE_IND)
{
println("got U_STOP_MODE_IND");
}
else if (rxByte == U_SYSTEM_STAT_IND)
{
print("got U_SYSTEM_STAT_IND: 0x");
while (true)
{
int tmp = _platform.readUart();
if (tmp < 0)
continue;
print(tmp, HEX);
break;
}
println();
}
else
{
print("got UNEXPECTED: 0x");
print(rxByte, HEX);
println();
}
_loopState = IDLE; _loopState = IDLE;
println("Timeout during RX_L_DATA");
break; break;
} case RX_L_DATA:
if(!_platform.uartAvailable()) break; if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT)
_lastByteRxTime = _platform.millis(); {
rxByte =_platform.readUart(); _RxByteCnt = 0;
_loopState = IDLE;
if(_RxByteCnt == MAX_KNX_TELEGRAM_SIZE){ println("Timeout during RX_L_DATA");
_loopState = IDLE; break;
println("invalid telegram size");
}
else{
buffer[_RxByteCnt++]=rxByte;
}
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
_isEcho = true;
} }
else{ if (!_platform.uartAvailable())
_isEcho = false; break;
_lastByteRxTime = _platform.millis();
rxByte = _platform.readUart();
if (_RxByteCnt == MAX_KNX_TELEGRAM_SIZE)
{
_loopState = IDLE;
println("invalid telegram size");
}
else
{
buffer[_RxByteCnt++] = rxByte;
} }
//convert into Extended.ind if (_RxByteCnt == 7)
if(_convert){ {
uint8_t payloadLength = buffer[6] & 0x0F; //Destination Address + payload available
buffer[1] = buffer[6] & 0xF0; _xorSum ^= rxByte;
buffer[6] = payloadLength; //check if echo
} if (!((buffer[0] ^ _sendBuffer[0]) & ~0x20) && !memcmp(buffer + 2, _sendBuffer + 1, 5))
{ //ignore repeated bit of control byte
if(!_isEcho){ _isEcho = true;
uint8_t c = 0x10;
//ceck if individual or group address
if ((buffer[6] & 0x80) == 0){ //individual
if(_deviceObject.induvidualAddress() == getWord(buffer + 4)){
c |= 0x01;
}
} }
else{ //group else
if(_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0){ {
c |= 0x01; _isEcho = false;
}
} }
_platform.writeUart(c);
} //convert into Extended.ind
} if (_convert)
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 uint8_t payloadLength = buffer[6] & 0x0F;
if(_isEcho && _sendBuffer != NULL){ //check if it is realy an echo, rx_crc = tx_crc buffer[1] = buffer[6] & 0xF0;
if(rxByte == _sendBuffer[_sendBufferLength-1]) buffer[6] = payloadLength;
_isEcho = true; }
if (!_isEcho)
{
uint8_t c = 0x10;
//ceck if individual or group address
if ((buffer[6] & 0x80) == 0)
{
//individual
if (_deviceObject.induvidualAddress() == getWord(buffer + 4))
{
c |= 0x01;
}
}
else else
_isEcho = false; {
} //group
if(_isEcho){ if (_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0)
_loopState = RX_WAIT_DATA_CON; {
} c |= 0x01;
else{ }
frameBytesReceived(_receiveBuffer, _RxByteCnt+2); }
_loopState=IDLE; _platform.writeUart(c);
} }
} }
else{ else if (_RxByteCnt == buffer[6] + 7 + 2)
println("frame with invalid crc ignored"); {
_loopState=IDLE; //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)
{
_loopState = RX_WAIT_DATA_CON;
}
else
{
frameBytesReceived(_receiveBuffer, _RxByteCnt + 2);
_loopState = IDLE;
}
}
else
{
println("frame with invalid crc ignored");
_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())
rxByte =_platform.readUart(); break;
_lastByteRxTime = _platform.millis(); rxByte = _platform.readUart();
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){ _lastByteRxTime = _platform.millis();
//println("L_DATA_CON received"); if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
dataConBytesReceived(_receiveBuffer, _RxByteCnt+2, ((rxByte & SUCCESS) > 0)); {
_waitConfirm = false; //println("L_DATA_CON received");
delete[] _sendBuffer; dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, ((rxByte & SUCCESS) > 0));
_sendBuffer = 0; _waitConfirm = false;
_sendBufferLength = 0; delete[] _sendBuffer;
_loopState=IDLE; _sendBuffer = 0;
} _sendBufferLength = 0;
else{ _loopState = IDLE;
//should not happen }
println("expected L_DATA_CON not received"); else
dataConBytesReceived(_receiveBuffer, _RxByteCnt+2, false); {
_waitConfirm = false; //should not happen
delete[] _sendBuffer; println("expected L_DATA_CON not received");
_sendBuffer = 0; dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, false);
_sendBufferLength = 0; _waitConfirm = false;
_loopState=IDLE; delete[] _sendBuffer;
} _sendBuffer = 0;
break; _sendBufferLength = 0;
default: _loopState = IDLE;
break; }
break;
default:
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;
cemiBuffer[1] = 0; cemiBuffer[1] = 0;
memcpy((cemiBuffer+2),_sendBuffer,_sendBufferLength); memcpy((cemiBuffer + 2), _sendBuffer, _sendBufferLength);
dataConBytesReceived(cemiBuffer, _sendBufferLength+2, false); dataConBytesReceived(cemiBuffer, _sendBufferLength + 2, false);
_waitConfirm = false; _waitConfirm = false;
delete[] _sendBuffer; delete[] _sendBuffer;
_sendBuffer = 0; _sendBuffer = 0;
_sendBufferLength = 0; _sendBufferLength = 0;
if(_loopState == RX_WAIT_DATA_CON) if (_loopState == RX_WAIT_DATA_CON)
_loopState=IDLE; _loopState = IDLE;
} }
} }
} }
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);
@ -364,8 +413,7 @@ 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,35 +486,40 @@ 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){ {
return true; if (_tx_queue.front == NULL)
{
return true;
} }
return false; return false;
} }
void TpUartDataLinkLayer::loadNextTxFrame()
{
if (_tx_queue.front == NULL)
{
return;
}
_tx_queue_frame_t* tx_frame = _tx_queue.front;
_sendBuffer = tx_frame->data;
_sendBufferLength = tx_frame->length;
_tx_queue.front = tx_frame->next;
void TpUartDataLinkLayer::loadNextTxFrame(){ if (_tx_queue.front == NULL)
if (_tx_queue.front == NULL) { {
return; _tx_queue.back = NULL;
} }
_tx_queue_frame_t* tx_frame = _tx_queue.front; delete tx_frame;
_sendBuffer = tx_frame->data;
_sendBufferLength = tx_frame->length;
_tx_queue.front = tx_frame->next;
if (_tx_queue.front == NULL){
_tx_queue.back = NULL;
}
delete tx_frame;
} }

View File

@ -3,24 +3,23 @@
#include <stdint.h> #include <stdint.h>
#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;
public: using DataLinkLayer::_platform;
public:
TpUartDataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab, NetworkLayer& layer, TpUartDataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab, NetworkLayer& layer,
Platform& platform); Platform& platform);
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;
uint8_t* _sendBuffer = 0; uint8_t* _sendBuffer = 0;
@ -33,20 +32,22 @@ private:
bool _isEcho = false; bool _isEcho = false;
bool _isAddressed = false; bool _isAddressed = false;
bool _convert = false; bool _convert = false;
uint8_t _xorSum=0; uint8_t _xorSum = 0;
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 *back=NULL; _tx_queue_frame_t* front = NULL;
}_tx_queue; _tx_queue_frame_t* back = NULL;
} _tx_queue;
void addFrameTxQueue(CemiFrame& frame); void addFrameTxQueue(CemiFrame& frame);
bool isTxQueueEmpty(); bool isTxQueueEmpty();
@ -58,4 +59,3 @@ private:
void resetChip(); void resetChip();
void stopChip(); void stopChip();
}; };