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.
369 lines
14 KiB
369 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();
|
|
};
|
|
|
|
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[CMD_IDX];
|
|
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;
|
|
};
|
|
|
|
#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( config_setting_t *Settings) : protocolInternalLRF3D() {
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"MASTER_LRF_CONNECTION" );
|
|
m_bFailureReason = 0;
|
|
m_bSettingsIsLoadded = false;
|
|
m_pMasterAddress = (CSTR) malloc( 16 );
|
|
m_pPort = NULL;
|
|
};
|
|
|
|
protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){
|
|
if( m_pMasterAddress != NULL ) free( m_pMasterAddress );
|
|
};
|
|
|
|
bool protocolInternalLRF3D_SLAVE::create( void ){
|
|
config_setting_t *pSetting;
|
|
int tVal;
|
|
char tBuffer[256];
|
|
|
|
m_bFailureReason = 1;
|
|
|
|
if( m_pSettings == NULL ) return false;
|
|
if ( m_pMasterAddress == NULL ) return false;
|
|
|
|
if ( !m_bSettingsIsLoadded ){
|
|
if (!config_setting_lookup_string(m_pSettings, "MASTER_ADDRESS", (const char**)&m_pMasterAddress)) {
|
|
strncpy( m_pMasterAddress, "10.0.0.1", 15 );
|
|
pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
|
|
config_setting_set_string(pSetting, m_pMasterAddress);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
} else {
|
|
sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_pMasterAddress );
|
|
LOG( tBuffer );
|
|
};
|
|
if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
|
|
tVal = m_nMasterPort = 6564;
|
|
pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
|
|
config_setting_set_bool( pSetting, tVal );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
} else {
|
|
m_nMasterPort = tVal;
|
|
sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
//basicSettingsProvider::getInstance()->validate();
|
|
m_bSettingsIsLoadded = true;
|
|
};
|
|
m_bFailureReason = 2;
|
|
if( m_pPort == NULL ){
|
|
m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
|
|
if( m_pPort == NULL ) return false;
|
|
m_pPort->addRecipient( this );
|
|
m_bFailureReason = 3;
|
|
if( m_pPort->getConnectionStatus() != CS_CONNECTED ) return false;
|
|
} else {
|
|
m_bFailureReason = 3;
|
|
if( !m_pPort->connectTo( m_pMasterAddress, m_nMasterPort ) ) return false;
|
|
};
|
|
m_bFailureReason = 0;
|
|
return true;
|
|
};
|
|
bool protocolInternalLRF3D_SLAVE::sendTrasholdData( uint8_t lrf_id, uint32_t dataIdx ){
|
|
};
|
|
bool protocolInternalLRF3D_SLAVE::sendTargets( void ){
|
|
};
|
|
|
|
bool protocolInternalLRF3D_SLAVE::sendStatus( void ){
|
|
if( m_oDataTransport == NULL ) return false;
|
|
uint8_t Buffer[16];
|
|
Buffer[3] = 0xDC;
|
|
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("setStatus");
|
|
#endif
|
|
return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
|
|
};
|
|
#else
|
|
#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
|
|
bool protocolInternalLRF3D_MASTER::create( void ){
|
|
config_setting_t *pSetting;
|
|
int tVal;
|
|
char tBuffer[256];
|
|
|
|
m_bFailureReason = 1;
|
|
|
|
if( m_pSettings == NULL ) return false;
|
|
if ( m_pBindAddress == NULL ) return false;
|
|
|
|
if ( !m_bSettingsIsLoadded ){
|
|
if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&m_pBindAddress)) {
|
|
strncpy( m_pBindAddress, "10.0.0.1", 15 );
|
|
pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
|
|
config_setting_set_string(pSetting, m_pBindAddress);
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
} else {
|
|
sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_pBindAddress );
|
|
LOG( tBuffer );
|
|
};
|
|
if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
|
|
tVal = m_nBindPort = 6564;
|
|
pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
|
|
config_setting_set_bool( pSetting, tVal );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
} else {
|
|
m_nBindPort = tVal;
|
|
sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
|
|
LOG( tBuffer );
|
|
};
|
|
//basicSettingsProvider::getInstance()->validate();
|
|
m_bSettingsIsLoadded = true;
|
|
};
|
|
m_bFailureReason = 2;
|
|
if( m_pPort == NULL ){
|
|
|
|
|
|
struct sockaddr ClientAddress;
|
|
socklen_t size;
|
|
int ClientSocket;
|
|
|
|
size = (socklen_t) sizeof( struct sockaddr_in );
|
|
|
|
ClientSocket = accept ( MainSocket, &ClientAddress, &size );
|
|
if ( ClientSocket == -1 ) {
|
|
switch( errno ){
|
|
case EAGAIN:{
|
|
// Checkup client disconnection
|
|
if( m_vHandlersPool.size()) {
|
|
for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
|
|
LRF_ClientHandler *tClient = (LRF_ClientHandler *)(*index);
|
|
if( tClient->getConnectionStatus() == CS_DISCONNECTED ){
|
|
// Remove Client Handler
|
|
LOG("Remove Client");
|
|
delete( tClient );
|
|
m_vHandlersPool.erase( index );
|
|
LOG("Client Stoped");
|
|
LOG("Remove Client Done");
|
|
};
|
|
};
|
|
};
|
|
SLEEP(10);
|
|
continue;
|
|
};
|
|
//case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
|
|
case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
|
|
case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
|
|
case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
|
|
case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
|
|
case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
|
|
case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
|
|
case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
|
|
case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
|
|
case EFAULT: LOG_ERROR( "The addr argument is not in a writeable part of the user address space."); break;
|
|
case ENOBUFS:
|
|
case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
|
|
case EPROTO: LOG_ERROR( "Protocol error."); break;
|
|
case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
|
|
case ENOSR:
|
|
case ESOCKTNOSUPPORT:
|
|
case EPROTONOSUPPORT:
|
|
case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
|
|
//case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
|
|
default: LOG_ERROR( " Unhanded error in Kernel"); break;
|
|
};
|
|
m_bThreadStatus = TH_ENDED;
|
|
} else {
|
|
if ( ClientSocket >= 0 ) {
|
|
if( m_bConnectionType == 1 ){
|
|
char tstr[CQUERY];
|
|
inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
|
|
};
|
|
LOG( "Client Connection Accepted" );
|
|
m_pPort = new serverPort( ClientSocket );
|
|
} else {
|
|
LOG_ERROR( "Client Socket Error" );
|
|
switch( errno ){
|
|
//case EAGAIN: continue;
|
|
case EWOULDBLOCK: LOG_ERROR( " The socket is marked non-blocking and no connections are present to be accepted." ); break;
|
|
case EBADF: LOG_ERROR( " The descriptor is invalid." ); break;
|
|
case ECONNABORTED: LOG_ERROR( " A connection has been aborted." ); break;
|
|
case EINTR:{// If Interrupted by Signal - No Harm was done
|
|
LOG_ERROR( " The system call was interrupted by a signal that was caught before a valid connection arrived." );
|
|
continue;
|
|
};
|
|
case EINVAL: LOG_ERROR( " Socket is not listening for connections, or addrlen is invalid (e.g., is negative)." ); break;
|
|
case EMFILE: LOG_ERROR( " The per-process limit of open file descriptors has been reached."); break;
|
|
case ENFILE: LOG_ERROR( " The system limit on the total number of open files has been reached."); break;
|
|
case ENOTSOCK: LOG_ERROR( " The descriptor references a file, not a socket."); break;
|
|
case EOPNOTSUPP: LOG_ERROR( " The referenced socket is not of type SOCK_STREAM." ); break;
|
|
case EFAULT: LOG_ERROR( " The addr argument is not in a writable part of the user address space." ); break;
|
|
case ENOBUFS:
|
|
case ENOMEM: LOG_ERROR( " Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory." ); break;
|
|
case EPROTO: LOG_ERROR( " Protocol error." ); break;
|
|
case EPERM: LOG_ERROR( " Firewall rules forbid connection." ); break;
|
|
case ENOSR:
|
|
case ESOCKTNOSUPPORT:
|
|
case EPROTONOSUPPORT:
|
|
case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)" ); break;
|
|
//case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
|
|
default: LOG_ERROR( " Unhandled error in Kernel" ); break;
|
|
};
|
|
return false;
|
|
};
|
|
};
|
|
if( m_pPort == NULL ) return false;
|
|
m_pPort->addRecipient( this );
|
|
m_bFailureReason = 3;
|
|
if( m_pPort->getConnectionStatus() != CS_CONNECTED ) return false;
|
|
} else {
|
|
m_bFailureReason = 3;
|
|
if( !m_pPort->connectTo( m_pMasterAddress, m_nMasterPort ) ) return false;
|
|
};
|
|
m_bFailureReason = 0;
|
|
return true;
|
|
};
|
|
#endif
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|