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
// control services, device specific
#define U_RESET_IND 0x03
#define U_STATE_IND 0x07
#define SLAVE_COLLISION 0x80
#define RECEIVE_ERROR 0x40
#define TRANSMIT_ERROR 0x20
#define PROTOCOL_ERROR 0x10
#define TEMPERATURE_WARNING 0x08
#define U_FRAME_STATE_IND 0x13
#define U_FRAME_STATE_MASK 0x17
#define PARITY_BIT_ERROR 0x80
#define U_RESET_IND 0x03
#define U_STATE_IND 0x07
#define SLAVE_COLLISION 0x80
#define RECEIVE_ERROR 0x40
#define TRANSMIT_ERROR 0x20
#define PROTOCOL_ERROR 0x10
#define TEMPERATURE_WARNING 0x08
#define U_FRAME_STATE_IND 0x13
#define U_FRAME_STATE_MASK 0x17
#define PARITY_BIT_ERROR 0x80
#define CHECKSUM_LENGTH_ERROR 0x40
#define TIMING_ERROR 0x20
#define U_CONFIGURE_IND 0x01
#define U_CONFIGURE_MASK 0x83
#define AUTO_ACKNOWLEDGE 0x20
#define AUTO_POLLING 0x10
#define CRC_CCITT 0x80
#define TIMING_ERROR 0x20
#define U_CONFIGURE_IND 0x01
#define U_CONFIGURE_MASK 0x83
#define AUTO_ACKNOWLEDGE 0x20
#define AUTO_POLLING 0x10
#define CRC_CCITT 0x80
#define FRAME_END_WITH_MARKER 0x40
#define U_FRAME_END_IND 0xCB
#define U_STOP_MODE_IND 0x2B
#define U_SYSTEM_STAT_IND 0x4B
#define U_FRAME_END_IND 0xCB
#define U_STOP_MODE_IND 0x2B
#define U_SYSTEM_STAT_IND 0x4B
//loop states
#define IDLE 0
#define RX_FIRST_BYTE 1
#define RX_L_DATA 2
#define RX_WAIT_DATA_CON 3
#define TX_FRAME 4
#define IDLE 0
#define RX_FIRST_BYTE 1
#define RX_L_DATA 2
#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,224 +94,272 @@ void TpUartDataLinkLayer::loop(){
if (!_enabled)
return;
switch(_loopState){
case IDLE:
if(_platform.uartAvailable()){
_loopState = RX_FIRST_BYTE;
}
else{
if(!_waitConfirm && !isTxQueueEmpty()){
loadNextTxFrame();
_waitConfirm = true;
_waitConfirmStartTime = _platform.millis();
_loopState = TX_FRAME;
switch (_loopState)
{
case IDLE:
if (_platform.uartAvailable())
{
_loopState = RX_FIRST_BYTE;
}
else
{
if (!_waitConfirm && !isTxQueueEmpty())
{
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;
}
else if ((rxByte & L_DATA_MASK) == L_DATA_EXTENDED_IND){
buffer[_RxByteCnt++]=rxByte;
_xorSum ^= rxByte;
_convert = false;
_loopState=RX_L_DATA;
case TX_FRAME:
if (sendSingleFrameByte() == false)
{
_loopState = IDLE;
}
break;
}
else if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){
println("got unexpected L_DATA_CON");
}
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);
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;
}
println();
}
else{
print("got UNEXPECTED: 0x");
print(rxByte, HEX);
println();
}
_loopState = IDLE;
break;
case RX_L_DATA:
if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT){
_RxByteCnt = 0;
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)
{
println("got unexpected L_DATA_CON");
}
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;
println("Timeout during RX_L_DATA");
break;
}
if(!_platform.uartAvailable()) break;
_lastByteRxTime = _platform.millis();
rxByte =_platform.readUart();
if(_RxByteCnt == MAX_KNX_TELEGRAM_SIZE){
_loopState = IDLE;
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;
case RX_L_DATA:
if (_platform.millis() - _lastByteRxTime > BYTE_TIMEOUT)
{
_RxByteCnt = 0;
_loopState = IDLE;
println("Timeout during RX_L_DATA");
break;
}
else{
_isEcho = false;
if (!_platform.uartAvailable())
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(_convert){
uint8_t payloadLength = buffer[6] & 0x0F;
buffer[1] = buffer[6] & 0xF0;
buffer[6] = payloadLength;
}
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;
}
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{ //group
if(_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0){
c |= 0x01;
}
else
{
_isEcho = false;
}
_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
if(rxByte == _sendBuffer[_sendBufferLength-1])
_isEcho = true;
//convert into Extended.ind
if (_convert)
{
uint8_t payloadLength = buffer[6] & 0x0F;
buffer[1] = buffer[6] & 0xF0;
buffer[6] = payloadLength;
}
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
_isEcho = false;
}
if(_isEcho){
_loopState = RX_WAIT_DATA_CON;
}
else{
frameBytesReceived(_receiveBuffer, _RxByteCnt+2);
_loopState=IDLE;
{
//group
if (_groupAddressTable.contains(getWord(buffer + 4)) || getWord(buffer + 4) == 0)
{
c |= 0x01;
}
}
_platform.writeUart(c);
}
}
else{
println("frame with invalid crc ignored");
_loopState=IDLE;
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)
{
_loopState = RX_WAIT_DATA_CON;
}
else
{
frameBytesReceived(_receiveBuffer, _RxByteCnt + 2);
_loopState = IDLE;
}
}
else
{
println("frame with invalid crc ignored");
_loopState = IDLE;
}
}
}
else{
_xorSum ^= rxByte;
}
break;
case RX_WAIT_DATA_CON:
if(!_platform.uartAvailable()) break;
rxByte =_platform.readUart();
_lastByteRxTime = _platform.millis();
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON){
//println("L_DATA_CON received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt+2, ((rxByte & SUCCESS) > 0));
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_loopState=IDLE;
}
else{
//should not happen
println("expected L_DATA_CON not received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt+2, false);
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_loopState=IDLE;
}
break;
default:
break;
else
{
_xorSum ^= rxByte;
}
break;
case RX_WAIT_DATA_CON:
if (!_platform.uartAvailable())
break;
rxByte = _platform.readUart();
_lastByteRxTime = _platform.millis();
if ((rxByte & L_DATA_CON_MASK) == L_DATA_CON)
{
//println("L_DATA_CON received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, ((rxByte & SUCCESS) > 0));
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_loopState = IDLE;
}
else
{
//should not happen
println("expected L_DATA_CON not received");
dataConBytesReceived(_receiveBuffer, _RxByteCnt + 2, false);
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
_loopState = IDLE;
}
break;
default:
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;
cemiBuffer[1] = 0;
memcpy((cemiBuffer+2),_sendBuffer,_sendBufferLength);
dataConBytesReceived(cemiBuffer, _sendBufferLength+2, false);
memcpy((cemiBuffer + 2), _sendBuffer, _sendBufferLength);
dataConBytesReceived(cemiBuffer, _sendBufferLength + 2, false);
_waitConfirm = false;
delete[] _sendBuffer;
_sendBuffer = 0;
_sendBufferLength = 0;
if(_loopState == RX_WAIT_DATA_CON)
_loopState=IDLE;
if (_loopState == RX_WAIT_DATA_CON)
_loopState = IDLE;
}
}
}
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);
@ -364,8 +413,7 @@ void TpUartDataLinkLayer::dataConBytesReceived(uint8_t* buffer, uint16_t length,
{
//printHex("=>", buffer, length);
CemiFrame frame(buffer, length);
dataConReceived(frame,success);
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{
_TxByteCnt=0;
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,35 +486,40 @@ 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){
return true;
bool TpUartDataLinkLayer::isTxQueueEmpty()
{
if (_tx_queue.front == NULL)
{
return true;
}
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) {
return;
}
_tx_queue_frame_t* tx_frame = _tx_queue.front;
_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;
if (_tx_queue.front == NULL)
{
_tx_queue.back = NULL;
}
delete tx_frame;
}

View File

@ -3,24 +3,23 @@
#include <stdint.h>
#include "data_link_layer.h"
#define MAX_KNX_TELEGRAM_SIZE 263
#define BYTE_TIMEOUT 3 //milli seconds
#define CONFIRM_TIMEOUT 500 //milli seconds
#define MAX_KNX_TELEGRAM_SIZE 263
class TpUartDataLinkLayer: public DataLinkLayer
class TpUartDataLinkLayer : public DataLinkLayer
{
using DataLinkLayer::_deviceObject;
using DataLinkLayer::_platform;
using DataLinkLayer::_groupAddressTable;
public:
using DataLinkLayer::_platform;
public:
TpUartDataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab, NetworkLayer& layer,
Platform& platform);
Platform& platform);
void loop();
void enabled(bool value);
bool enabled() const;
private:
private:
bool _enabled = false;
bool _waitConfirm = false;
uint8_t* _sendBuffer = 0;
@ -33,20 +32,22 @@ private:
bool _isEcho = false;
bool _isAddressed = false;
bool _convert = false;
uint8_t _xorSum=0;
uint8_t _xorSum = 0;
uint32_t _lastByteRxTime;
uint32_t _waitConfirmStartTime;
struct _tx_queue_frame_t{
uint8_t *data;
struct _tx_queue_frame_t
{
uint8_t* data;
uint16_t length;
_tx_queue_frame_t *next;
_tx_queue_frame_t* next;
};
struct _tx_queue_t {
_tx_queue_frame_t *front=NULL;
_tx_queue_frame_t *back=NULL;
}_tx_queue;
struct _tx_queue_t
{
_tx_queue_frame_t* front = NULL;
_tx_queue_frame_t* back = NULL;
} _tx_queue;
void addFrameTxQueue(CemiFrame& frame);
bool isTxQueueEmpty();
@ -58,4 +59,3 @@ private:
void resetChip();
void stopChip();
};