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.
395 lines
16 KiB
395 lines
16 KiB
#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_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
};
|
|
|
|
mojoRemoteClient::~mojoRemoteClient(){
|
|
waitThreadEnd( true );
|
|
if( m_bRegisteredWithRemotePeer ) {
|
|
//Unregister Mojo
|
|
};
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
if ( m_pPort ) delete( m_pPort );
|
|
};
|
|
|
|
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 ){
|
|
uint32_t tQueryMessage;
|
|
m_mQueryProtector.lock();
|
|
tQueryMessage = m_nQueryMessage;
|
|
m_mQueryProtector.unlock();
|
|
switch( Package->m_nCode ){
|
|
case ARGUS_DATA_MSG_REGISTER: {
|
|
if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) {
|
|
m_mQueryProtector.lock();
|
|
m_nQueryMessage = 0;
|
|
m_mQueryProtector.unlock();
|
|
};
|
|
m_bRegisteredWithRemotePeer = true;
|
|
break;
|
|
};
|
|
case ARGUS_DATA_MSG_DISABLED:
|
|
case ARGUS_DATA_MSG_ACCESS_DENIED:{
|
|
if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) {
|
|
m_mQueryProtector.lock();
|
|
m_nQueryMessage = 0;
|
|
m_mQueryProtector.unlock();
|
|
};
|
|
m_bRegisteredWithRemotePeer = false;
|
|
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
|
|
break;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
void mojoRemoteClient::m_fThreadProc( void ){
|
|
time_t tTimeTSeconds = 0;
|
|
time_t cTimeTSeconds = 0;
|
|
uint8_t bTO_COUNTER = 0;
|
|
uint32_t tSecondsCounter = 0;
|
|
|
|
m_bThreadStatus = TH_WORKING;
|
|
LOG("Connector Thread Started");
|
|
while ( m_bThreadStatus < TH_ENDED ) {
|
|
// seconds Counter (for internal purposes)
|
|
cTimeTSeconds = time( NULL );
|
|
if ( cTimeTSeconds!=tTimeTSeconds ) {
|
|
//Process each second
|
|
tSecondsCounter++;
|
|
tTimeTSeconds = cTimeTSeconds;
|
|
switch ( m_bAutomationStage ) {
|
|
case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
|
|
if ( bTO_COUNTER ) {
|
|
bTO_COUNTER--;
|
|
} else {
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
|
|
};
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_IDLE:{
|
|
uint8_t tS = m_pMaster->m_pDBC->getLRFDataStatus( 1 );
|
|
ARGUS_DATA_MOJO_STATUS cSTATUS;
|
|
DBC_SYS_LOAD_INFO cSYSSTAT;
|
|
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;
|
|
peer_send_status( &cSTATUS );
|
|
break;
|
|
};
|
|
};
|
|
if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) {
|
|
LOG("Req Time Sync!");
|
|
ARGUS_DATA_TIME_SYNC_REQ tTR;
|
|
tTR.STS = getMilliseconds();
|
|
peer_send_time_sync_req( &tTR );
|
|
};
|
|
};
|
|
switch ( m_bAutomationStage ) {
|
|
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() ) {
|
|
LOG("Connection Sucessfull");
|
|
m_bShutdownCommunication = false;
|
|
m_bCommunicationEstablished = true;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
setTransport( m_pPort, false );
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_REGISTER;
|
|
} else {
|
|
delete( m_pPort );
|
|
m_pPort = NULL;
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
bTO_COUNTER = 30; //30 sec to wait
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_WAIT_RECONNECT;
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_WAIT_RECONNECT: break;
|
|
case ARGUS_RPC_STAGE_REGISTER:{
|
|
if( peer_send_reg_req( m_pMaster->m_sIdentity )){
|
|
//m_nQueryMessage = ARGUS_RPC_STAGE_REGISTER;
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_SEND_HELLO;
|
|
bTO_COUNTER = 1;
|
|
};
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_SEND_HELLO:{
|
|
ARGUS_DATA_MOJO_INFO tINFO;
|
|
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( peer_send_hello( &tINFO ) ) {
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
|
|
};
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_IDLE:{
|
|
|
|
break;
|
|
};
|
|
};
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
|
|
};
|
|
LOG("Connector Thread Ended!");
|
|
};
|
|
|
|
/*
|
|
Register With Server and make Time Sync With Server
|
|
*/
|
|
bool mojoRemoteClient::peer_send_reg_req( CSTR identity ){
|
|
if( identity == NULL ) return false;
|
|
if( !m_bCommunicationEstablished ) return false;
|
|
if( m_bRegisteredWithRemotePeer ) return true;
|
|
uint8_t tBuffer[256];
|
|
uint32_t tPackSize = ARGUS_HEADER_SIZE+33;
|
|
tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER;
|
|
memcpy( tBuffer+1+ARGUS_HEADER_SIZE, identity, 32 );
|
|
if( m_pPort->sendData( tBuffer, argus_rpc_finishupPackage( tBuffer, tPackSize ) ) ) {
|
|
m_mQueryProtector.lock();
|
|
m_nQueryMessage = ARGUS_DATA_MSG_REGISTER;
|
|
m_mQueryProtector.unlock();
|
|
//Waiting For Responce
|
|
while( 1 ){
|
|
if( m_bShutdownCommunication ) break;
|
|
m_mQueryProtector.lock();
|
|
if( m_nQueryMessage == 0 ) {
|
|
m_mQueryProtector.unlock();
|
|
break;
|
|
};
|
|
m_mQueryProtector.unlock();
|
|
SLEEP(1);
|
|
};
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
|
|
};
|
|
return m_bRegisteredWithRemotePeer;
|
|
};
|
|
|
|
bool mojoRemoteClient::peer_send_evt( ARGUS_DATA_EVENT *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_EVT;
|
|
uint16_t i = 0;
|
|
for(; i < sizeof(ARGUS_DATA_EVENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
|
|
return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
|
|
};
|
|
|
|
bool mojoRemoteClient::peer_send_hello( ARGUS_DATA_MOJO_INFO *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_INFO;
|
|
uint16_t i = 0;
|
|
for(; i < sizeof(ARGUS_DATA_MOJO_INFO); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
|
|
return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
|
|
};
|
|
|
|
bool mojoRemoteClient::peer_send_status( ARGUS_DATA_MOJO_STATUS *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_KEEP_ALIVE;
|
|
uint16_t i = 0;
|
|
for(; i < sizeof(ARGUS_DATA_MOJO_STATUS); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
|
|
return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
|
|
};
|
|
|
|
bool mojoRemoteClient::peer_send_time_sync_req( ARGUS_DATA_TIME_SYNC_REQ *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_TIME_SYNC;
|
|
uint16_t i = 0;
|
|
for(; i < sizeof(ARGUS_DATA_TIME_SYNC_REQ); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
|
|
return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
|
|
};
|
|
|
|
bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
|
|
return true;
|
|
};
|
|
|
|
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 ){
|
|
bool retVal = false;
|
|
LOG("Add Evt!");
|
|
ARGUS_DATA_EVENT *tPac = argus_rpc_convert_evt( Evt );
|
|
if( tPac ){
|
|
retVal = peer_send_evt( tPac );
|
|
free( tPac );
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|