#include "mojoRemoteClient.h" #include "mojoKernel.h" #define LOG( X ) m_pLog->Log( "RPC", 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( "RPC", this, _LOG_ERROR, _tBfrErrMSG234 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "RPC", this, _LOG_ERROR, X ); #endif mojoRemoteClient::mojoRemoteClient( mojoKernel * ptr ):protocolParser(){ m_pLog = basicLogCollection::getInstance(); m_pPort = NULL; m_pMaster = ptr; m_nOutbondSerial = 0; m_nInbondSerial = 0; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; m_qCMD = new RPC_CMD_QUEUE(); }; mojoRemoteClient::~mojoRemoteClient(){ waitThreadEnd( true ); if( m_bRegisteredWithRemotePeer ) { //Unregister Mojo }; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; if ( m_pPort ) delete( m_pPort ); delete( m_qCMD ); }; bool mojoRemoteClient::create(){ if( m_pMaster == NULL ) return false; if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true; m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT; return init(); }; int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){ //char tB[64]; if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) { if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){ uint32_t SIZE = Buffer[1]; SIZE = ( SIZE << 8 ) | Buffer[2]; SIZE = ( SIZE << 8 ) | Buffer[3]; SIZE = ( SIZE << 8 ) | Buffer[4]; //sprintf( tB, "Pack Size: %08X", SIZE ); //LOG(tB); if ( ( SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) { uint16_t tCRC = checksum16( Buffer, SIZE + ARGUS_HEADER_SIZE ); uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE]; cCRC = ( cCRC << 8 ) | Buffer[SIZE + ARGUS_HEADER_SIZE+1]; //sprintf( tB, "cCRC: %04X tCRC: %04X", tCRC, cCRC ); //LOG(tB); if ( tCRC == cCRC ) return 0; } else return 1; // to grou } else return 1; //to grow }; return -1; // to skip }; protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){ protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage)); if ( tPack ) { tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE]; tPack->m_nSize = Buffer[1]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[2]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[3]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[4]; tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE; tPack->m_pData = (U8 *) malloc( tPack->m_nSize ); if ( tPack->m_pData ) { memcpy( tPack->m_pData, Buffer, tPack->m_nSize ); } else { free( tPack ); tPack = NULL; }; }; return tPack; }; bool mojoRemoteClient::packetHandler( protocolPackage *Package ){ m_nInbondSerial = Package->m_pData[1]; m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[2]; m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[3]; m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[4]; switch( Package->m_nCode ){ case ARGUS_DATA_MSG_REGISTER: m_bRegisteredWithRemotePeer = true; enqueueInfoMsg(); finishTransaction(); break; case ARGUS_DATA_MSG_ACCESS_DENIED: m_bRegisteredWithRemotePeer = false; case ARGUS_DATA_MSG_DISABLED: finishTransaction(); break; case ARGUS_DATA_MSG_REQ_TIME_SYNC:{ RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD)); if ( tCMD ) { tCMD->CMD = ARGUS_DATA_MSG_TIME_SYNC; tCMD->DATA_SIZE = sizeof( ARGUS_DATA_TIME_SYNC_REQ ); tCMD->DATA = malloc(tCMD->DATA_SIZE); if (tCMD->DATA) { ARGUS_DATA_TIME_SYNC_REQ *tDATA = (ARGUS_DATA_TIME_SYNC_REQ*)tCMD->DATA; tDATA->STS = getMilliseconds(); if ( peer_send_rpc_pack( tCMD ) ) { setTO( 3 ); free( tCMD->DATA ); free( tCMD ); break; }; free( tCMD->DATA ); }; free( tCMD ); }; finishTransaction(); break; }; case ARGUS_DATA_MSG_TIME_SYNC:{ struct timespec tS = { 0 }; uint64_t cT = getMilliseconds(); ARGUS_DATA_TIME_SYNC_ACK *tPTR = (ARGUS_DATA_TIME_SYNC_ACK *)(Package->m_pData+ARGUS_HEADER_SIZE+1); uint64_t eTime = tPTR->ATS + ( (cT - tPTR->STS) >> 1 ); tS.tv_sec = eTime / 1000; tS.tv_nsec = ( eTime % 1000 ) * 1000000; if( clock_settime( CLOCK_REALTIME, &tS ) != 0){ LOG_ERROR("Time Correction Failed!"); switch ( errno ) { case EACCES: LOG_ERROR("Not have write permission for the dynamic POSIX clock device indicated!"); break; case EFAULT: LOG_ERROR("tS points outside the accessible address space!"); break; case EINVAL: LOG_ERROR("Parameter specified is invalid!"); break; case ENODEV: LOG_ERROR("The hot-pluggable device (like USB for example) represented by a dynamic clk_id has disappeared after its character device was opened!"); break; case ENOTSUP: LOG_ERROR("The operation is not supported by the dynamic POSIX clock device specified!"); break; case EPERM: LOG_ERROR("clock_settime() does not have permission to set the clock indicated!"); break; }; }; #ifdef DEBUG_EXTRA LOG("Time Sync ACK!"); char tB[256]; sprintf( tB,"RT:%lld ST:%lld CT:%lld", tPTR->STS, tPTR->ATS, cT ); LOG( tB ); sprintf( tB,"tv_s:%lld tv_ns:%lld", tS.tv_sec, tS.tv_nsec ); LOG( tB ); #endif finishTransaction(); break; }; }; return true; }; uint8_t mojoRemoteClient::getAutomationStage( ){ m_mAutomationStageProtector.lock(); uint8_t retVal = m_bAutomationStage; m_mAutomationStageProtector.unlock(); return retVal; }; void mojoRemoteClient::setAutomationStage( uint8_t stg ){ m_mAutomationStageProtector.lock(); m_bAutomationStage = stg; m_mAutomationStageProtector.unlock(); }; void mojoRemoteClient::finishTransaction(){ m_mCurrentCMDProtector.lock(); if ( m_pCurrentCMD ) { if ( m_pCurrentCMD->DATA ) free( m_pCurrentCMD->DATA ); free( m_pCurrentCMD ); m_pCurrentCMD = NULL; }; m_mCurrentCMDProtector.unlock(); }; bool mojoRemoteClient::validateTransaction(){ if ( m_mCurrentCMDProtector.try_lock() ) { if ( m_pCurrentCMD == NULL ) { m_mCurrentCMDProtector.unlock(); return true; }; m_mCurrentCMDProtector.unlock(); }; return false; }; bool mojoRemoteClient::pushNewTransaction( RPC_CMD_RECORD *ptr ){ if ( ptr == NULL ) return false; m_mCurrentCMDProtector.lock(); if ( m_qCMD->push( ptr ) != -1 ){ m_mCurrentCMDProtector.unlock(); return true; }; m_mCurrentCMDProtector.unlock(); return false; }; bool mojoRemoteClient::fetchNewTransaction(){ m_mCurrentCMDProtector.lock(); if ( m_pCurrentCMD ) { m_mCurrentCMDProtector.unlock(); return false; }; m_pCurrentCMD = m_qCMD->pop(); if ( m_pCurrentCMD ) { m_mCurrentCMDProtector.unlock(); return true; }; m_mCurrentCMDProtector.unlock(); return false; }; void mojoRemoteClient::enqueueInfoMsg(){ RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD)); if ( tCMD ) { tCMD->CMD = ARGUS_DATA_MSG_INFO; tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO ); tCMD->DATA = malloc(tCMD->DATA_SIZE); if (tCMD->DATA) { ARGUS_DATA_MOJO_INFO *tINFO = (ARGUS_DATA_MOJO_INFO*)tCMD->DATA; tINFO->MOJO_WORK_MODE = 2; tINFO->UPPER_ID = 2; tINFO->BOTTOM_ID = 3; tINFO->CAM_ID = 4; tINFO->CPU_COUNT = m_pMaster->m_sSysInfo.CPU_CORES; tINFO->TOT_DISK = m_pMaster->m_sSysInfo.T_DISK; tINFO->TOT_MEMO = m_pMaster->m_sSysInfo.T_MEM; if ( pushNewTransaction( tCMD ) ) return; free( tCMD->DATA ); }; free( tCMD ); }; }; void mojoRemoteClient::enqueueStatMsg(){ RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD)); if ( tCMD ) { tCMD->CMD = ARGUS_DATA_MSG_INFO; tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_STATUS ); tCMD->DATA = malloc(tCMD->DATA_SIZE); if (tCMD->DATA) { ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA; DBC_SYS_LOAD_INFO cSYSSTAT; uint8_t tS = m_pMaster->m_pDBC->getLRFDataStatus( 1 ); cSTATUS->STATUSES = 0xff00ff00 | tS; m_pMaster->getSystemUtilization( &cSYSSTAT ); cSTATUS->CPU_LOAD = round( cSYSSTAT.cpu15 ); cSTATUS->CPU_TEMP = round( cSYSSTAT.temperature ); //if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0; //if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0; cSTATUS->DISK_UTI = cSYSSTAT.disk; cSTATUS->MEMO_UTI = cSYSSTAT.mem; if ( pushNewTransaction( tCMD ) ) return; free( tCMD->DATA ); }; free( tCMD ); }; }; void mojoRemoteClient::enqueueTimeReq(){ RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD)); if ( tCMD ) { tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC; tCMD->DATA_SIZE = 0; tCMD->DATA = NULL; if ( pushNewTransaction( tCMD ) ) return; free( tCMD ); }; }; void mojoRemoteClient::setTO( uint8_t seconds ){ m_mTOProtoctor.lock(); m_bTOCounter = seconds; m_mTOProtoctor.unlock(); }; bool mojoRemoteClient::decTO(){ m_mTOProtoctor.lock(); if ( m_bTOCounter ) { m_bTOCounter--; m_mTOProtoctor.unlock(); return true; }; m_mTOProtoctor.unlock(); return false; }; void mojoRemoteClient::m_fThreadProc( void ){ time_t tTimeTSeconds = 0; time_t cTimeTSeconds = 0; uint32_t tSecondsCounter = 0; m_bThreadStatus = TH_WORKING; #ifdef DEBUG LOG("Connector Thread Started"); #endif while ( m_bThreadStatus < TH_ENDED ) { // seconds Counter (for internal purposes) cTimeTSeconds = time( NULL ); //Process each second if ( cTimeTSeconds != tTimeTSeconds ) { tSecondsCounter++; tTimeTSeconds = cTimeTSeconds; switch ( getAutomationStage() ) { case ARGUS_RPC_STAGE_CONNECT:{ #ifdef DEBUG char tB[128]; sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); LOG( tB ); #endif if( m_pMaster->m_sSiteData.remote_port ) { m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); if( m_pPort ) { if( m_pPort->create() ) { #ifdef DEBUG LOG("Connection Sucessfull"); #endif m_bShutdownCommunication = false; m_bCommunicationEstablished = true; m_bRegisteredWithRemotePeer = false; setTransport( m_pPort, false ); setAutomationStage( ARGUS_RPC_STAGE_REGISTER ); } else { delete( m_pPort ); m_pPort = NULL; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; setTO( 30 ); //30 sec to wait setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT ); }; }; }; break; }; case ARGUS_RPC_STAGE_WAIT_RECONNECT:{ if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT ); break; }; case ARGUS_RPC_STAGE_REGISTER:{ RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD*) malloc(sizeof(RPC_CMD_RECORD)); if ( tCMD ) { tCMD->CMD = ARGUS_DATA_MSG_REGISTER; tCMD->DATA_SIZE = strlen( m_pMaster->m_sIdentity ); tCMD->DATA = malloc(tCMD->DATA_SIZE); if ( tCMD->DATA ) { strncpy( (char *)tCMD->DATA, m_pMaster->m_sIdentity, tCMD->DATA_SIZE ); if( pushNewTransaction( tCMD ) ){ setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK ); setTO( 3 ); // Wait for 3 seconds to be registered with server break; }; free( tCMD->DATA ); }; free( tCMD ); }; break; }; case ARGUS_RPC_STAGE_WAIT_ACK:{ LOG("TO On Waiting Responce!"); if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CLOSE ); break; }; case ARGUS_RPC_STAGE_IDLE: if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) enqueueTimeReq(); else enqueueStatMsg(); break; case ARGUS_RPC_STAGE_DISCONNECT:{ // Gracefully Disconnect //if( peer_send_disconnect() ){ // bTO_COUNTER = 1; // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK ); //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE ); break; }; case ARGUS_RPC_STAGE_CLOSE:{ // Just Disconnect delete( m_pPort ); m_pPort = NULL; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; setTO( 30 ); //30 sec to wait setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT ); break; }; }; }; if ( validateTransaction() ) { if ( fetchNewTransaction() ) { if( peer_send_rpc_pack( m_pCurrentCMD ) ) { setTO( 1 ); setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK ); }; } else SLEEP( DEFAULT_THREAD_SLEEP_TIME ); } else SLEEP( DEFAULT_THREAD_SLEEP_TIME ); }; LOG("Connector Thread Ended!"); }; /* Register With Server and make Time Sync With Server */ bool mojoRemoteClient::peer_send_rpc_pack( RPC_CMD_RECORD *ptr){ bool retVal; if ( ptr == NULL ) return false; if( !m_bCommunicationEstablished ) return false; if( !m_bRegisteredWithRemotePeer ) return false; m_mBufferProtector.lock(); m_pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD; uint16_t i = 0; for (; i < ptr->DATA_SIZE; i++) m_pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i]; m_nOutbondSerial++; retVal = m_pPort->sendData( m_pBuffer, argus_rpc_finishupPackage( m_pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i )); m_mBufferProtector.unlock(); return retVal; }; bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){ if( ptr == NULL ) return false; if( !m_bCommunicationEstablished ) return false; if( !m_bRegisteredWithRemotePeer ) return false; uint8_t tBuffer[1024]; tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO; uint16_t i = 0; for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i]; m_nOutbondSerial++; return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i )); }; bool mojoRemoteClient::validate(){ if( m_bCommunicationEstablished ){ if ( getConnectionStatus() == CS_DISCONNECTED ) { m_bCommunicationEstablished = false; if( m_pPort ) { // Cancel all ongoing Queries m_bShutdownCommunication = true; setTransport( NULL, false ); delete( m_pPort ); }; m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT; /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); if( m_pPort ) { if( m_pPort->create() ) { m_bShutdownCommunication = false; m_bCommunicationEstablished = true; m_bRegisteredWithRemotePeer = false; setTransport( m_pPort, false ); } else { delete( m_pPort ); m_pPort = NULL; }; }; */ }; } else { /* if( m_pPort ) { // Cancel all ongoing Queries m_bShutdownCommunication = true; setTransport( NULL, false ); delete( m_pPort ); }; //Try To connect m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); if( m_pPort ) { if( m_pPort->create() ) { m_bShutdownCommunication = false; m_bCommunicationEstablished = true; m_bRegisteredWithRemotePeer = false; setTransport( m_pPort, false ); } else { delete( m_pPort ); m_pPort = NULL; }; }; */ }; return true; }; bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){ #ifdef DEBUG LOG("Add Evt!"); #endif if ( Evt == NULL ) return false; RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD)); if ( tPack ) { tPack->DATA = (void*) argus_rpc_convert_evt( Evt ); if ( tPack->DATA ) { tPack->CMD = ARGUS_DATA_MSG_EVT; tPack->DATA_SIZE = sizeof( ARGUS_DATA_EVENT ); if( m_qCMD->push( tPack ) ) return true; free( tPack->DATA ); }; free( tPack ); }; return false; }; #undef LOG #undef LOG_ERROR