#include "protocolInternalLRF3D.h" #define LOG( X ) m_pLog->Log( "PTWINC", 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( "PTWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "PTWINC", this, _LOG_ERROR, X ); #endif protocolInternalLRF3D::protocolInternalLRF3D(): protocolParser(){ m_pLog = basicLogCollection::getInstance(); }; 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 > 3 ) { #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 > 3 ){ #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; }; bool protocolInternalLRF3D::sendACK( TWIN_COMMAND ACK_CMD ){ uint8_t Package[10]; if( m_oDataTransport == NULL ) return false; Package[TWI_CMD_INDEX] = TWC_ACK_CMD; Package[TWI_CMD_INDEX+1] = ACK_CMD; return m_oDataTransport->sendData( Package, FinishUpPackage( Package, TWI_CMD_INDEX+2 ) ); }; bool protocolInternalLRF3D::sendNACK( TWIN_COMMAND ACK_CMD ){ uint8_t Package[10]; if( m_oDataTransport == NULL ) return false; Package[TWI_CMD_INDEX] = TWC_NACK_CMD; Package[TWI_CMD_INDEX+1] = ACK_CMD; return m_oDataTransport->sendData( Package, FinishUpPackage( Package, TWI_CMD_INDEX+2 ) ); }; bool protocolInternalLRF3D::sendDetectionParameters( PLGS_3D_PARAMETERS ptr ){ uint8_t Buffer[1024]; if( ptr == NULL ) return false; Buffer[TWI_CMD_INDEX] = TWC_SET_CONFIG; for( uint8_t idx = 0; idx < sizeof(LGS_3D_PARAMETERS); idx++ ) { Buffer[TWI_PLS_INDEX+idx] = ((uint8_t *)ptr)[idx]; }; return m_oDataTransport->sendData(Buffer, FinishUpPackage(Buffer, TWI_PLS_INDEX+sizeof(LGS_3D_PARAMETERS))); }; bool protocolInternalLRF3D::sendPolygone( uint8_t polyID, simplePolygone *SP ){ uint16_t counter = TWI_PLS_INDEX+5; uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE]; Buffer[TWI_CMD_INDEX] = TWC_SET_SHADOW_POLY; Buffer[TWI_PLS_INDEX] = polyID; Buffer[TWI_PLS_INDEX+1] = SP->count; Buffer[TWI_PLS_INDEX+2] = SP->color[0]; Buffer[TWI_PLS_INDEX+3] = SP->color[1]; Buffer[TWI_PLS_INDEX+4] = SP->color[2]; for( uint8_t i = 0; i < SP->count; i++ ){ uint16_t x,y; x = SP->points[i].x(); y = SP->points[i].y(); Buffer[counter++] = ( x >> 8 ) & 0xFF; Buffer[counter++] = x & 0xFF; Buffer[counter++] = ( y >> 8 ) & 0xFF; Buffer[counter++] = y & 0xFF; }; #ifdef DEBUG LOG("sendPolygone"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, counter ) ); }; bool protocolInternalLRF3D::sendShadowTrace( vectorZoneMask *VM ){ uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE]; uint16_t counter = TWI_PLS_INDEX+2; uint16_t tVal = VM->size(); Buffer[TWI_CMD_INDEX] = TWC_SET_SHADOW_TRACE; Buffer[TWI_PLS_INDEX] = ( tVal >> 8 ) & 0xFF; Buffer[TWI_PLS_INDEX+1] = tVal & 0xFF; for( uint16_t i = 0; i < tVal; i++ ){ uint16_t x, y, start, stop; x = VM->at( i )->column; y = VM->at( i )->count; Buffer[counter++] = ( x >> 8 ) & 0xFF; Buffer[counter++] = x & 0xFF; Buffer[counter++] = ( y >> 8 ) & 0xFF; Buffer[counter++] = y & 0xFF; for( uint8_t j = 0; j < y; j++ ){ start = VM->at( i )->zones[j].start; stop = VM->at( i )->zones[j].stop; Buffer[counter++] = ( start >> 8 ) & 0xFF; Buffer[counter++] = start & 0xFF; Buffer[counter++] = ( stop >> 8 ) & 0xFF; Buffer[counter++] = stop & 0xFF; }; }; #ifdef DEBUG LOG("sendShadowTrace"); #endif return m_oDataTransport->sendData(Buffer, FinishUpPackage( Buffer, counter ) ); }; bool protocolInternalLRF3D::sendPlainCMD( TWIN_COMMAND CMD ){ uint8_t Package[10]; #ifdef DEBUG LOG("Plain CMD <->"); #endif if( m_oDataTransport == NULL ) return false; Package[TWI_CMD_INDEX] = CMD; return m_oDataTransport->sendData( Package, FinishUpPackage( Package, TWI_CMD_INDEX+1 ) ); }; //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(); }; protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){ }; bool protocolInternalLRF3D_SLAVE::create( void ){ return protocolInternalLRF3D::create(); }; bool protocolInternalLRF3D_SLAVE::sendTrasholdData( thresholdNode *data, uint32_t dataIdx ){ if( data == NULL ) return false; uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE]; uint32_t node_index = dataIdx; uint32_t index = TWI_PLS_INDEX + 4; Buffer[TWI_CMD_INDEX] = TWC_TH_DATA; Buffer[TWI_PLS_INDEX] = ( node_index >> 24 ) & 0xFF; Buffer[TWI_PLS_INDEX+1] = ( node_index >> 16 ) & 0xFF; Buffer[TWI_PLS_INDEX+2] = ( node_index >> 8 ) & 0xFF; Buffer[TWI_PLS_INDEX+3] = ( node_index & 0xFF ); while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 5 ) ) && ( node_index < TH_SIZE_NODES ) ){ Buffer[index++] = (data[ node_index ].data >> 24 ) & 0xFF;//0xFE; // to clear Sunbit data Buffer[index++] = (data[ node_index ].data >> 16 ) & 0xFF; Buffer[index++] = (data[ node_index ].data >> 8 ) & 0xFF; Buffer[index++] = data[ node_index ].data & 0xFF; node_index++; }; #ifdef DEBUG LOG("TH Data->"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) ); }; bool protocolInternalLRF3D_SLAVE::sendTarget( targetRecord *ptr ){ if( ptr == NULL ) return false; uint8_t Buffer[32]; Buffer[TWI_CMD_INDEX] = TWC_PUSH_TARGET; for( uint8_t idx = 0; idx < sizeof(targetRecord); idx++ ) { Buffer[TWI_PLS_INDEX+idx] = ((uint8_t *)ptr)[idx]; }; #ifdef DEBUG LOG("Target->"); #endif return m_oDataTransport->sendData(Buffer, FinishUpPackage(Buffer, TWI_PLS_INDEX+sizeof(targetRecord))); }; bool protocolInternalLRF3D_SLAVE::sendStatus( TWI_PEER_STATUS *ptr ){ if( m_oDataTransport == NULL ) return false; if( ptr == NULL ) return false; uint8_t Buffer[64]; Buffer[TWI_CMD_INDEX] = TWC_CURRENT_STATUS; for( uint8_t idx = 0; idx < sizeof(TWI_PEER_STATUS); idx++ ) { Buffer[TWI_PLS_INDEX+idx] = ((uint8_t *)ptr)[idx]; }; #ifdef DEBUG LOG("Status->"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage(Buffer, TWI_PLS_INDEX+sizeof(TWI_PEER_STATUS))); }; bool protocolInternalLRF3D_SLAVE::sendShadowInfo( TWI_MASK_INFO *ptr ){ if( m_oDataTransport == NULL ) return false; if( ptr == NULL ) return false; uint8_t Buffer[64]; Buffer[TWI_CMD_INDEX] = TWC_SHADOW_INFO; for( uint8_t idx = 0; idx < sizeof(TWI_MASK_INFO); idx++ ) { Buffer[TWI_PLS_INDEX+idx] = ((uint8_t *)ptr)[idx]; }; #ifdef DEBUG LOG("ShadowInfo->"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX+sizeof(TWI_MASK_INFO) ) ); }; bool protocolInternalLRF3D_SLAVE::sendMode( uint8_t mode ){ if( m_oDataTransport == NULL ) return false; uint8_t Buffer[16]; Buffer[TWI_CMD_INDEX] = TWC_CURRENT_MODE; Buffer[TWI_PLS_INDEX] = mode; #ifdef DEBUG LOG("Mode->"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX+1 ) ); }; bool protocolInternalLRF3D_SLAVE::sendVersion( SOFTWARE_INFO *sv ){ uint8_t Buffer[64]; if( sv ){ Buffer[TWI_CMD_INDEX] = TWC_CURRENT_VERSION; for( int i=0; i < sizeof(SOFTWARE_INFO); i++ ){ Buffer[TWI_PLS_INDEX + i] = ((uint8_t *)sv)[i]; }; #ifdef DEBUG LOG("Version->"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX + sizeof(SOFTWARE_INFO) ) ); }; }; #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::ackThresholdSegment( uint32_t dataIdx ){ if( m_oDataTransport == NULL ) return false; uint8_t Buffer[16]; Buffer[TWI_CMD_INDEX] = TWC_ACK_CMD; Buffer[TWI_PLS_INDEX] = TWC_TH_DATA; Buffer[TWI_PLS_INDEX+1] = ( dataIdx >> 8 ) & 0xFF; Buffer[TWI_PLS_INDEX+2] = ( dataIdx & 0xFF ); #ifdef DEBUG LOG("Treshold Data Seg"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX+3 ) ); }; bool protocolInternalLRF3D_MASTER::getShadowPoly( uint8_t polyID ){ if( m_oDataTransport == NULL ) return false; uint8_t Buffer[16]; Buffer[TWI_CMD_INDEX] = TWC_GET_SHADOW_POLY; Buffer[TWI_PLS_INDEX] = polyID; #ifdef DEBUG LOG("ShadowPoly Req"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX+1 ) ); }; bool protocolInternalLRF3D_MASTER::getTHData( uint8_t lrf_id ){ if( m_oDataTransport == NULL ) return false; uint8_t Buffer[16]; Buffer[TWI_CMD_INDEX] = TWC_GET_THRESHOLD; Buffer[TWI_PLS_INDEX] = lrf_id; #ifdef DEBUG LOG("Treshold Req"); #endif return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, TWI_PLS_INDEX+1 ) ); }; #endif #undef LOG #undef LOG_ERROR