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.
 
 
 
 
 
 

467 lines
17 KiB

#include "LRF_TwinConnector.h"
#include "LRF_Kernel.h"
#include "LRF_TargetTracker.h"
#define LOG( X ) m_pLog->Log( "TWINC", 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( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
#endif
LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
m_pProtocol = NULL;
m_pPort = NULL;
m_pEngine = NULL;
#ifdef PRU_SLAVE
m_nMasterPort = 0;
m_pMasterAddress = (CSTR) malloc( 16 );
#else
m_nMainSocket = -1;
m_nBindPort = 0;
m_pBindAddress = (CSTR) malloc( 16 );
#endif
};
LRF_TwinConnector::~LRF_TwinConnector(){
if( m_pPort != NULL ) delete( m_pPort );
if( m_pProtocol != NULL ) delete( m_pProtocol );
#ifdef PRU_SLAVE
if( m_pMasterAddress ) free( m_pMasterAddress );
#else
if( m_pBindAddress ) free( m_pBindAddress );
#endif
};
bool LRF_TwinConnector::create(){
if( m_pSettings!= NULL ) {
config_setting_t *pSetting;
int tVal;
char tBuffer[256];
#ifdef PRU_SLAVE
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 );
};
#else
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 );
};
#endif
if( basicSettingsProvider::getInstance()->isCompromised() ) {
LOG("Save Settings");
basicSettingsProvider::getInstance()->validate();
};
} else {
LOG_ERROR("No Settings Provided!");
return false;
};
#ifdef PRU_SLAVE
m_pProtocol = new protocolInternalLRF3D_SLAVE();
if( m_pProtocol == NULL ){
LOG_ERROR("Error creating protocol!");
return false;
};
if( !m_pProtocol->create() ) return false;
#else
int RetCode;
struct hostent * h_info;
struct sockaddr_in * MainSocketAddress = NULL;
m_pProtocol = new protocolInternalLRF3D_MASTER();
if( m_pProtocol == NULL ){
LOG_ERROR("Error creating protocol!");
return false;
};
if( !m_pProtocol->create() ) return false;
m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( m_nMainSocket == -1){
LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
return false;
};
MainSocketAddress = ( struct sockaddr_in * ) malloc( sizeof( struct sockaddr_in ) );
if( MainSocketAddress == NULL ){
LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
return false;
};
bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
h_info = gethostbyname ( m_pBindAddress );
if ( h_info == NULL ){
LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
return false;
};
//*****************************************
//* there we building our network address *
//*****************************************
bcopy( h_info->h_addr, &(MainSocketAddress->sin_addr), h_info->h_length );
MainSocketAddress->sin_family = h_info->h_addrtype;
MainSocketAddress->sin_port = htons( m_nBindPort );
RetCode = bind( m_nMainSocket ,(struct sockaddr *) MainSocketAddress, sizeof( struct sockaddr_in ) );
if ( RetCode != 0 ){
LOG_ERROR( "Error Binding to Port/Address" );
return false;
};
RetCode = listen( m_nMainSocket, 128);
if ( RetCode != 0 ){
LOG_ERROR( "Cannot Listen Port/Address" );
return false;
};
U8 dontblock;
dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
dontblock |= O_NONBLOCK | O_NDELAY;
RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
if ( RetCode == 0 ){
LOG( "Set N-Blocking Mode of Accepting Socket" );
} else {
LOG( "Can't set N-Blocking Mode of Accepting Socket" );
};
#endif // PRU_SLAVE
return init();
};
#ifdef PRU_SLAVE
void LRF_TwinConnector::m_fThreadProc( void ){
TWIN_CMD_REC * tCMD = NULL;
int ClientSocket = -1;
struct sockaddr ClientAddress;
socklen_t ClientAddressSize;
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
// No CMD is EXEC so check new cmds
CMD_PROTEC.lock();
if( m_qCMD.size() > 0 ){
tCMD = m_qCMD.front();
m_qCMD.pop();
};
CMD_PROTEC.unlock();
};
if( m_pPort != NULL ){
if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
if( tCMD!= NULL ){
//reset commands on disconnect
m_bCCMD = TWC_NO_COMMAND;
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
delete( m_pPort );
m_pPort = NULL;
continue;
};
if( tCMD!= NULL ){
if( m_bCCMD == TWC_NO_COMMAND ){
// CMD not issued yet!
switch( tCMD->CMD ){
case TWC_PUSH_TARGET:{
break;
};
};
} else {
// Waiting CMD exe status
switch( m_bCCMD ){
case TWC_SET_THDRAW:{
break;
};
};
if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
// The command is finished - Will fetch next command from queue
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
};
SLEEP( 5 );
} else {
SLEEP( 100 );
};
} else {
LOG("(re)Establish Connection");
m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
if( m_pPort!= NULL ){
m_pProtocol->setTransport( m_pPort );
};
};
};
};
#else //Master
void LRF_TwinConnector::m_fThreadProc( void ){
TWIN_CMD_REC * tCMD = NULL;
int ClientSocket = -1;
struct sockaddr ClientAddress;
socklen_t ClientAddressSize;
long long cMilliseconds;
long long cStatusTO = 0;
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
cMilliseconds = getMilliseconds();
if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
// No CMD is EXEC so check new cmds
CMD_PROTEC.lock();
if( m_qCMD.size() > 0 ){
tCMD = m_qCMD.front();
m_qCMD.pop();
};
CMD_PROTEC.unlock();
};
if( m_pPort != NULL ){
if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
if( tCMD!= NULL ){
//reset commands on disconnect
m_bCCMD = TWC_NO_COMMAND;
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
delete( m_pPort );
m_pPort = NULL;
continue;
};
if( tCMD!= NULL ){
if( m_bCCMD == TWC_NO_COMMAND ){
// CMD not issued yet!
switch( tCMD->CMD ){
case TWC_GET_STATUS:{
if( m_pProtocol->requestStatus() ){
m_bCCMD = tCMD->CMD;
};
break;
};
case TWC_GET_CONFIG:{
break;
};
case TWC_SET_CONFIG:{
break;
};
case TWC_GET_THDRAW:{
break;
};
case TWC_DO_REBOOT:{
break;
};
};
} else {
// Waiting CMD exe status
switch( m_bCCMD ){
case TWC_GET_STATUS:{
if( m_pProtocol->getLastPackageType() == TWIN_STATUS_ACK ){
m_bCCMD = TWC_NO_COMMAND;
#ifdef DEBUG
LOG("Registered Ack Status");
#endif
uint32_t nStatus = m_pProtocol->getStatus();
if( ( nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){
#ifdef DEBUG
LOG("Have new Targets!");
#endif
m_pProtocol->setTData( m_pTargets );
m_pProtocol->requestTargets();
m_bCCMD = TWC_GET_TARGETS;
continue;
};
};
break;
};
case TWC_GET_TARGETS:{
if( m_pProtocol->getLastPackageType() == TWIN_TARGETS_DATA ){
m_bCCMD = TWC_NO_COMMAND;
#ifdef DEBUG
LOG("Registered Ack Targets");
#endif
if( m_pTracker != NULL ) {
uint8_t nCNT = (( m_pProtocol->getStatus() & TWI_STATUS_TARGET_MASK ) >> 8 );
for( uint8_t index = 0; index < nCNT; index++ ){
m_pTracker->validateTarget( &m_pTargets[index], false );
};
};
};
break;
};
};
if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
// The command is finished - Will fetch next command from queue
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
};
SLEEP( TWI_QUICK_LOOP_SLEEP );
} else {
if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ) {
cStatusTO = cMilliseconds;
m_pProtocol->requestStatus();
m_bCCMD = TWC_GET_STATUS;
LOG("Req Status");
} else {
SLEEP( TWI_SLOW_LOOP_SLEEP );
};
};
} else {
// (re)Establish Connection
ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
if ( ClientSocket == -1 ) {
switch( errno ){
case EAGAIN:{
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 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;
};
m_bThreadStatus = TH_ENDED;
} else {
char tstr[CQUERY];
inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, CQUERY );
LOG( "SLAVE LRF Connection Accepted" );
m_pPort = new serverPort( ClientSocket );
};
if( m_pPort!= NULL ){
m_pProtocol->setTransport( m_pPort );
};
};
};
};
#endif //!PRU_SLAVE
bool LRF_TwinConnector::cleanTHData( uint8_t kid ){
char tBuffer[1024];
FILE *FID = NULL;
if( kid > 31 ) kid = 31;
sprintf( tBuffer, "data/th_data_%d.raw", kid );
if( !fileExist( "data" ) ){
int RetVal = mkdir( "data", STORAGE_DIR_MODE );
if( RetVal < 0 ) {
LOG_ERROR( "Error Creating DATA DIR!" );
return false;
};
};
FID = fopen( tBuffer, "wb+" );
if( FID == NULL ){
LOG_ERROR( "Error Creating DATA FILE!" );
return false;
};
uint32_t ZZ = 0;
for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
fwrite( &ZZ, 4, 1, FID );
};
fclose( FID );
return true;
};
bool LRF_TwinConnector::loadTHData( uint8_t kid, thresholdNode *ptr ){
FILE *FID;
char tBuffer[1024];
if( ptr == NULL ) return false;
if( kid > 31 ) kid = 31;
sprintf( tBuffer, "data/th_data_%d.raw", kid );
FID = fopen( tBuffer, "rb" );
if( FID == NULL ) {
LOG_ERROR( "Error Opening DATA FILE!" );
return false;
};
for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
fread( &ptr[i], TH_NODE_SIZE, 1, FID );
};
fclose( FID );
return true;
};
bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){
FILE *FID;
char tBuffer[1024];
if( ptr == NULL ) return false;
if( kid > 31 ) kid = 31;
sprintf( tBuffer, "data/th_data_%d.raw", kid );
FID = fopen( tBuffer, "wb+" );
if( FID == NULL ) {
LOG_ERROR( "Error Creating DATA FILE!" );
return false;
};
for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
fwrite( &ptr[i], TH_NODE_SIZE, 1, FID );
};
fclose( FID );
return true;
};
#ifndef PRU_SLAVE
bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){
if(ptr == NULL) return false;
m_pTracker = ptr;
return false;
};
#endif
#undef LOG
#undef LOG_ERROR