You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

402 lines
14 KiB

#include "protocolInternalLRF3D.h"
#define LOG( X ) m_pLog->Log( "protocolLRF3D", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, _tBfrErrMSG234 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, X );
#endif
protocolInternalLRF3D::protocolInternalLRF3D(): protocolParser(){
m_pLog = basicLogCollection::getInstance();
m_nStatus = 0;
};
protocolInternalLRF3D::~protocolInternalLRF3D(){};
int protocolInternalLRF3D::packetChecker( U8 *Buffer, size_t BufferSize ){
#ifdef DUBUG_COMMUNICATION
char tmp[32];
int i;
for( i=0;i< BufferSize; i++ ) {
sprintf( tmp, " 0x%02X", Buffer[i] );
LOG(tmp);
};
#endif
if( Buffer[0] == 0xAA ){
if( BufferSize > 5 ) {
#ifdef PRU_SLAVE
if ((Buffer[1] & 0x80) == 0x80 ){
#else
if ((Buffer[1] & 0x80) == 0x00 ){
#endif
size_t pLength;
pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
if( BufferSize >= ( pLength + 4 ) ) {
U8 pCheckSum;
pCheckSum = checksumLGS( Buffer, pLength+3 );
if( Buffer[pLength+3] == pCheckSum ) {
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketChecker: Packet Is Recognized (0)");
#endif
return 0; // Ok
} else {
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketChecker: Packet Not Recognized CHK SUM(-1)");
#endif
return -1; // To shift
};
};
} else {
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketChecker: Packet Not Recognized TAG (-1)");
#endif
return -1; // To shiftSeverity Code Description Project File Line Suppression State
};
};
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketChecker: Packet Not Recognized SMALL SIZE(1)");
#endif
return 1; // To grow
};
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketChecker: Packet Not Recognized START TAG(-1)");
#endif
return -1; // To shift
};
protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t BufferSize ){
protocolPackage *RetVal;
#ifdef DUBUG_COMMUNICATION
char tmp[32];
int i;
for( i=0;i< BufferSize; i++ ) {
sprintf( tmp, " 0x%02X", Buffer[i] );
LOG(tmp);
};
#endif
if( Buffer[0] == 0xAA ){
if( BufferSize > 5 ){
#ifdef PRU_SLAVE
if ((Buffer[1] & 0x80) == 0x80 ) {
#else
if ((Buffer[1] & 0x80) == 0x00 ) {
#endif
size_t pLength;
pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
if( BufferSize >= ( pLength+4 ) ) {
U8 pCheckSum;
pCheckSum = checksumLGS(Buffer, pLength+3 );
if( Buffer[pLength+3] == pCheckSum ) {
RetVal = (protocolPackage *) malloc(sizeof(protocolPackage));
if( RetVal == NULL ) return NULL;
RetVal->m_pData =(U8*)malloc( pLength+4 );
if( RetVal->m_pData == NULL ) {
free( RetVal );
return NULL;
};
RetVal->m_nSize = pLength+4;
RetVal->m_nCode = Buffer[3];
memcpy( RetVal->m_pData, Buffer, RetVal->m_nSize );
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketExtractor: Packet Is Extracted (0)");
#endif
return RetVal;
};
};
} else {
LOG_ERROR("TAG mismatch!");
};
};
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketExtractor: Packet Not Extracted(1)!!!");
#endif
return NULL;
};
#ifdef DUBUG_COMMUNICATION
LOG((CSTR)"PacketExtractor: Packet Not Extracted(-1)");
#endif
return NULL;
};
uint32_t protocolInternalLRF3D::getStatus(){
uint32_t RetVal;
m_mStatusProtector.lock();
RetVal = m_nStatus;
m_mStatusProtector.unlock();
return RetVal;
};
#undef LOG
#undef LOG_ERROR
#ifdef PRU_SLAVE
#define LOG( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, _tBfrErrMSG234 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, X );
#endif
protocolInternalLRF3D_SLAVE::protocolInternalLRF3D_SLAVE() : protocolInternalLRF3D() {
m_pLog = basicLogCollection::getInstance();
m_nTargetCount = 0;
memset( m_pTargets, 0, sizeof(targetRecord) * TWI_TARGET_QUEUE_SIZE );
for( uint8_t i = 0; i < 16; i++ ) {
m_pThresholds[ i ] = (thresholdNode *) malloc( TH_SIZE_BYTES );
if( m_pThresholds[ i ] != NULL ) {
memset( m_pThresholds[ i ], 0, TH_SIZE_BYTES );
};
};
};
protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){
for( uint8_t i = 0; i < 16; i++ ) {
if( m_pThresholds[ i ] != NULL ) free( m_pThresholds[ i ] );
};
};
bool protocolInternalLRF3D_SLAVE::create( void ){
return protocolInternalLRF3D::create();
};
bool protocolInternalLRF3D_SLAVE::setTrasholdData( uint8_t lrf_id, uint32_t *ptr ){
if( ptr == NULL ) return false;
if( m_pThresholds[ lrf_id ] == NULL ) return false;
m_mData.lock();
memcpy( m_pThresholds[ lrf_id ], ptr, TH_SIZE_BYTES );
m_nStatus |= (( 1 << lrf_id ) << 8 );
m_mData.unlock();
return false;
};
bool protocolInternalLRF3D_SLAVE::enqueueTarget( targetRecord *ptr ){
if( ptr == NULL ) return false;
m_mTargetProtector.lock();
if( m_nTargetCount < TWI_TARGET_QUEUE_SIZE ) {
memcpy( &m_pTargets[m_nTargetCount], ptr, sizeof(targetRecord) );
m_nTargetCount++;
m_nStatus = ( m_nStatus & 0x00fffffe ) | ( m_nTargetCount << 24 );
} else {
memcpy( &m_pTargets[0], &m_pTargets[1], (TWI_TARGET_QUEUE_SIZE-1)*sizeof(targetRecord) );
memcpy( &m_pTargets[TWI_TARGET_QUEUE_SIZE-1], ptr, sizeof(targetRecord) );
m_nStatus |= 1; //Target Overflow flag
};
m_mTargetProtector.unlock();
return true;
};
bool protocolInternalLRF3D_SLAVE::sendTrasholdData( uint8_t lrf_id, uint32_t Idx ){
if( m_pThresholds[ lrf_id ] == NULL ) return false;
uint32_t c_mask = (( 1 << lrf_id ) << 8 );
if( ( m_nStatus & c_mask ) == 0 ) return false;
uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE];
uint32_t node_index = Idx;
uint32_t index = 8;
Buffer[3] = TWIN_TH_DATA;
Buffer[4] = ( node_index >> 24 ) & 0xFF;
Buffer[5] = ( node_index >> 16 ) & 0xFF;
Buffer[6] = ( node_index >> 8 ) & 0xFF;
Buffer[7] = ( node_index & 0xFF );
while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 5 ) ) && ( node_index < TH_SIZE_NODES ) ){
Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 24 ) & 0xFF;//0xFE; // to clear Sunbit data
Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 16 ) & 0xFF;
Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 8 ) & 0xFF;
Buffer[index++] = m_pThresholds[ lrf_id ][ node_index ].data & 0xFF;
node_index++;
};
#ifdef DEBUG
LOG("TH Data Segment");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) );
};
bool protocolInternalLRF3D_SLAVE::sendTargets( void ){
uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE];
if( m_nTargetCount > 0 ) {
Buffer[3] = TWIN_TARGETS_DATA;
uint32_t index = 5;
uint8_t target_index = 0;
m_mTargetProtector.lock();
while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 21 ) ) && ( target_index < m_nTargetCount ) ){
Buffer[index++] = (m_pTargets[ target_index ].width >> 24 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].width >> 16 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].width >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].width & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 24 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 16 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].begin_id & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].end_id >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].end_id & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].end_id >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].end_id & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].range_min >> 24 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].range_min >> 16 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].range_max >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].range_max & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].status >> 24 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].status >> 16 ) & 0xFF;
Buffer[index++] = (m_pTargets[ target_index ].status >> 8 ) & 0xFF;
Buffer[index++] = m_pTargets[ target_index ].status & 0xFF;
target_index++;
};
m_nTargetCount -= target_index;
m_nStatus = ( m_nStatus & 0x00fffffe ) | ( m_nTargetCount << 24 );
m_mTargetProtector.unlock();
Buffer[4] = target_index;
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) );
} else {
Buffer[3] = TWIN_TARGETS_DATA;
Buffer[4] = 0;
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 5 ) );
};
};
bool protocolInternalLRF3D_SLAVE::sendStatus( void ){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_STATUS_ACK;
Buffer[4] = ( m_nStatus >> 24 ) & 0xFF;
Buffer[5] = ( m_nStatus >> 16 ) & 0xFF;
Buffer[6] = ( m_nStatus >> 8 ) & 0xFF;
Buffer[7] = ( m_nStatus & 0xFF );
#ifdef DEBUG
LOG("Status ACK");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
};
bool protocolInternalLRF3D_SLAVE::packetHandler( protocolPackage *Package ){
if( Package == NULL ) return false;
uint8_t cCMD = Package->m_pData[3];
switch( cCMD ) {
case TWIN_STATUS_REQ:{
sendStatus();
break;
};
};
return true;
};
#else // PRU_SLAVE
#define LOG( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, _tBfrErrMSG234 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, X );
#endif
protocolInternalLRF3D_MASTER::protocolInternalLRF3D_MASTER(){
m_pLog = basicLogCollection::getInstance();
};
protocolInternalLRF3D_MASTER::~protocolInternalLRF3D_MASTER(){};
bool protocolInternalLRF3D_MASTER::create( void ){
return protocolInternalLRF3D::create();
};
bool protocolInternalLRF3D_MASTER::ackDataSegment( uint32_t dataIdx ){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_TH_DATA;
Buffer[4] = ( dataIdx >> 8 ) & 0xFF;
Buffer[5] = ( dataIdx & 0xFF );
#ifdef DEBUG
LOG("Treshold Data Seg");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 6 ) );
};
bool protocolInternalLRF3D_MASTER::ackTargets( uint32_t pkgId ){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_TARGETS_REQ;
Buffer[4] = ( pkgId >> 24 ) & 0xFF;
Buffer[5] = ( pkgId >> 16 ) & 0xFF;
Buffer[6] = ( pkgId >> 8 ) & 0xFF;
Buffer[7] = ( pkgId & 0xFF );
#ifdef DEBUG
LOG("Target Ack");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
};
bool protocolInternalLRF3D_MASTER::requestTargets(void){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_TARGETS_REQ;
#ifdef DEBUG
LOG("Target Req");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 4 ) );
};
bool protocolInternalLRF3D_MASTER::requestTrasholdData( uint8_t lrf_id ){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_TH_GETDATA;
Buffer[4] = lrf_id;
#ifdef DEBUG
LOG("Treshold Req");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 5 ) );
};
bool protocolInternalLRF3D_MASTER::requestStatus( void ){
if( m_oDataTransport == NULL ) return false;
uint8_t Buffer[16];
Buffer[3] = TWIN_STATUS_REQ;
#ifdef DEBUG
LOG("Status Req");
#endif
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 4 ) );
};
bool protocolInternalLRF3D_MASTER::packetHandler( protocolPackage *Package ){
if( Package == NULL ) return false;
uint8_t cCMD = Package->m_pData[3];
switch( cCMD ) {
case TWIN_STATUS_ACK:{
uint32_t tStatus;
#ifdef DEBUG
LOG("Status ACK reseived");
#endif
tStatus = ( Package->m_pData[4] << 24 ) | ( Package->m_pData[5] << 16 ) | ( Package->m_pData[6] << 8 ) | Package->m_pData[7];
m_mStatusProtector.lock();
m_nStatus = tStatus;
m_mStatusProtector.unlock();
break;
};
case TWIN_TH_DATA:{
};
};
m_mLastTypeProtector.lock();
m_bLastPackageType = (TWIN_PCMD) cCMD;
m_mLastTypeProtector.unlock();
return true;
};
TWIN_PCMD protocolInternalLRF3D_MASTER::getLastPackageType( void ){
TWIN_PCMD RetVal;
m_mLastTypeProtector.lock();
RetVal = m_bLastPackageType;
m_mLastTypeProtector.unlock();
return RetVal;
};
#endif
#undef LOG
#undef LOG_ERROR