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.
1035 lines
42 KiB
1035 lines
42 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_pCurrentCMD = NULL;
|
|
m_pMaster = ptr;
|
|
m_nOutbondSerial = 0;
|
|
m_nInbondSerial = 0;
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
m_bUseBackupServer = false;
|
|
m_bActiveServer = 0;
|
|
m_bCloseCommunicationReason = 0;
|
|
//m_pVideoHelper = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
|
|
memset(&m_sINFO, 0, sizeof(ARGUS_DATA_MOJO_INFO));
|
|
m_qCMD = new RPC_CMD_QUEUE();
|
|
};
|
|
|
|
mojoRemoteClient::~mojoRemoteClient(){
|
|
LOG("Destructor");
|
|
if( m_bRegisteredWithRemotePeer ) {
|
|
LOG("Flushing");
|
|
while ( m_bRegisteredWithRemotePeer ) {
|
|
m_mCurrentVTRProtector.lock();
|
|
if ( m_vVTRHelpers.size() == 0 ){
|
|
m_mCurrentVTRProtector.unlock();
|
|
break;
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
SLEEP( DEFAULT_THREAD_SLEEP_TIME );
|
|
};
|
|
m_mCurrentVTRProtector.lock();
|
|
if ( m_vVTRHelpers.size() ) {
|
|
for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) delete( *idx );
|
|
m_vVTRHelpers.clear();
|
|
} else {
|
|
//Send Disconmnect
|
|
enqueueDisconnect( m_bCloseCommunicationReason );
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
};
|
|
LOG("Flushing Done!");
|
|
waitThreadEnd( true );
|
|
flushTransactions();
|
|
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
LOG("Delete Port");
|
|
if ( m_pPort ) delete( m_pPort );
|
|
LOG("Delete Ring");
|
|
delete( m_qCMD );
|
|
LOG("Done");
|
|
};
|
|
|
|
bool mojoRemoteClient::create(){
|
|
if( m_pMaster == NULL ) return false;
|
|
m_pMaster->fillMojoInfo( &m_sINFO );
|
|
if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true;
|
|
m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
|
|
m_pCurrentCMD = NULL;
|
|
m_bActiveServer = 0;
|
|
m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && ( strlen( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
|
|
return init();
|
|
};
|
|
|
|
int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){
|
|
if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) {
|
|
if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){
|
|
uint32_t SIZE = Buffer[5];
|
|
SIZE = ( SIZE << 8 ) | Buffer[6];
|
|
SIZE = ( SIZE << 8 ) | Buffer[7];
|
|
SIZE = ( SIZE << 8 ) | Buffer[8];
|
|
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];
|
|
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[5];
|
|
tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[6];
|
|
tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[7];
|
|
tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[8];
|
|
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];
|
|
#ifdef DEBUG_EXTRA
|
|
char bB[129];
|
|
sprintf( bB, "Code: %02X", Package->m_nCode );
|
|
LOG( bB );
|
|
#endif
|
|
switch( Package->m_nCode ){
|
|
case ARGUS_DATA_MSG_REGISTER:
|
|
#ifdef DEBUG
|
|
LOG("Reg From Server");
|
|
#endif
|
|
m_bRegisteredWithRemotePeer = true;
|
|
setAutomationStage( ARGUS_RPC_STAGE_SEND_HELLO );
|
|
//finishTransaction();
|
|
break;
|
|
case ARGUS_DATA_MSG_ACCESS_DENIED: m_bRegisteredWithRemotePeer = false; LOG("ACC DENIED");
|
|
case ARGUS_DATA_MSG_DISABLED: finishTransaction(); LOG("MSG disabled"); break;
|
|
case ARGUS_DATA_MSG_STAT: finishTransaction(); break;
|
|
case ARGUS_DATA_MSG_INFO: finishTransaction(); break;
|
|
case ARGUS_DATA_MSG_GET_INFO: enqueueInfoMsg(); break;
|
|
case ARGUS_DATA_MSG_REQ_TIME_SYNC:{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("TSYNC REQ");
|
|
#endif
|
|
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 );
|
|
};
|
|
//Only On failure
|
|
finishTransaction();
|
|
break;
|
|
};
|
|
case ARGUS_DATA_MSG_EVT: finishTransaction(); break;
|
|
case ARGUS_DATA_MSG_VIDEO_INFO:{
|
|
//get DATABASE ID
|
|
uint64_t tDBID = Package->m_pData[ARGUS_HEADER_SIZE+1];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+2];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+3];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+4];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+5];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+6];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+7];
|
|
tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+8];
|
|
#ifdef DEBUG
|
|
char tZ[128];
|
|
sprintf( tZ, "Video Ack %lld ID", tDBID );
|
|
LOG( tZ );
|
|
#endif
|
|
m_mCurrentVTRProtector.lock();
|
|
if ( tDBID ) {
|
|
VTR_HELPER *tHELP = m_vVTRHelpers.at(0);
|
|
tHELP->ptr->setRemoteReferenceID( tDBID );
|
|
tHELP->state = 2;
|
|
tHELP->ts = time( NULL );
|
|
} else {
|
|
//Abort Transaction
|
|
m_vVTRHelpers.at(0)->state = 3;
|
|
m_mCurrentVTRProtector.unlock();
|
|
break;
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
};
|
|
case ARGUS_DATA_MSG_VIDEO_DATA:{
|
|
RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( tCMD ) {
|
|
uint32_t tSize;
|
|
tCMD->CMD = ARGUS_DATA_MSG_VIDEO_DATA;
|
|
m_mCurrentVTRProtector.lock();
|
|
if ( m_vVTRHelpers.size() == 0 ) {
|
|
tCMD->DATA = NULL;
|
|
tCMD->DATA_SIZE = 0;
|
|
if( !m_qCMD->push( tCMD ) ){
|
|
free( tCMD );
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
finishTransaction(); //?? Do we need it
|
|
break;
|
|
};
|
|
tCMD->DATA = (void *)m_vVTRHelpers.at(0)->ptr->getNextChunk(ARGUS_RPC_DEFAULT_MEDIA_CHUNK, &tSize);
|
|
m_mCurrentVTRProtector.unlock();
|
|
tCMD->DATA_SIZE = tSize;
|
|
#ifdef DEBUG_EXTRA
|
|
char tC[128];
|
|
sprintf( tC, "Sending Video Chunk %d bytes", tSize );
|
|
LOG( tC );
|
|
#endif
|
|
if ( tCMD->DATA ) {
|
|
/*
|
|
//Direct Send to peer data
|
|
if ( peer_send_rpc_pack( tCMD ) ) {
|
|
setTO( 1 );
|
|
free( tCMD->DATA );
|
|
free( tCMD );
|
|
if ( tSize < ARGUS_RPC_DEFAULT_MEDIA_CHUNK ) {
|
|
//Last Chunk
|
|
m_mCurrentVTRProtector.lock();
|
|
mojoVideoTransferHelper tPtr = m_qVTRHelpers.first();
|
|
delte( tPtr );
|
|
m_qVTRHelpers.pop();
|
|
if ( !m_qVTRHelpers.empty() ) {
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
};
|
|
break;
|
|
};
|
|
free( tCMD->DATA );
|
|
*/
|
|
// Send Via Queue
|
|
if( m_qCMD->push( tCMD ) ){
|
|
m_mCurrentVTRProtector.lock();
|
|
m_vVTRHelpers.at(0)->ts = time(NULL);
|
|
m_mCurrentVTRProtector.unlock();
|
|
finishTransaction(); //?? Do we need it
|
|
break;
|
|
};
|
|
free( tCMD->DATA );
|
|
} else {
|
|
LOG("Finish");
|
|
// Finish Send That File
|
|
m_mCurrentVTRProtector.lock();
|
|
m_vVTRHelpers.at(0)->state = 3;
|
|
m_mCurrentVTRProtector.unlock();
|
|
};
|
|
free( tCMD );
|
|
};
|
|
//Only On failure
|
|
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;
|
|
};
|
|
case ARGUS_DATA_MSG_RPC:{
|
|
ARGUS_DATA_RPC *tPTR = (ARGUS_DATA_RPC *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
|
|
m_pMaster->m_pDBC->pushMemCMD( tPTR->NODE_ID, tPTR->CMD, tPTR->DATA );
|
|
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();
|
|
setAutomationStage( ARGUS_RPC_STAGE_IDLE );
|
|
};
|
|
|
|
bool mojoRemoteClient::validateTransaction(){
|
|
if ( !m_bRegisteredWithRemotePeer ) return false;
|
|
if ( m_bShutdownCommunication ) return false;
|
|
m_mCurrentCMDProtector.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(){
|
|
if ( m_bShutdownCommunication ) return false;
|
|
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::enqueueDisconnect( uint8_t reason ){
|
|
static uint8_t pBuffer[400];
|
|
if( !m_bCommunicationEstablished ) return;
|
|
pBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_DISCONNECT;
|
|
pBuffer[ARGUS_HEADER_SIZE+1] = reason;
|
|
m_nOutbondSerial++;
|
|
m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 2 ) );
|
|
};
|
|
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) {
|
|
memcpy( tCMD->DATA, &m_sINFO, sizeof( ARGUS_DATA_MOJO_INFO ) );
|
|
LOG("ARGUS_DATA_MOJO_INFO");
|
|
if ( pushNewTransaction( tCMD ) ) return;
|
|
free( tCMD->DATA );
|
|
};
|
|
free( tCMD );
|
|
};
|
|
};
|
|
|
|
#include "../common/deviceNodeCamera.h"
|
|
#include "../common/deviceNodeSensor.h"
|
|
#include "../common/deviceNodeFactory.h"
|
|
|
|
void mojoRemoteClient::enqueueStatMsg(){
|
|
RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( tCMD ) {
|
|
tCMD->CMD = ARGUS_DATA_MSG_STAT;
|
|
tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_STATUS );
|
|
tCMD->DATA = malloc(tCMD->DATA_SIZE);
|
|
if( tCMD->DATA ){
|
|
memset( tCMD->DATA, 0, tCMD->DATA_SIZE );
|
|
ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA;
|
|
DBC_SYS_LOAD_INFO cSYSSTAT;
|
|
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;
|
|
|
|
deviceNode **tA = NULL;
|
|
uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ALS_SENSOR, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_LFS_SENSOR, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_200_SENSOR, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_360_SENSOR, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_3D_SENSOR, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_NET_PLS_200, tA );
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if( tCNT ){
|
|
for ( int i = 0; i < tCNT; i++) {
|
|
deviceNodeSensor *tSENS = (deviceNodeSensor*) tA[i];
|
|
if (tSENS) {
|
|
uint16_t ttt = tSENS->getSensStatus();
|
|
if ( ttt > 2 ) {
|
|
ttt = 1;
|
|
} else {
|
|
ttt = 2;
|
|
};
|
|
cSTATUS->STATUSES = cSTATUS->STATUSES | ( ttt <<(i*3) );
|
|
} else LOG("No Sensor PTR!!");
|
|
};
|
|
free( tA );
|
|
tA = NULL;
|
|
#ifdef DEBUG_EXTRA
|
|
} else {
|
|
LOG("No Sensors Defined/Enabled In the system!");
|
|
#endif
|
|
};
|
|
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_STATIC_CAMERA, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PTZ_CAMERA, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_STATIC_CAMERA, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PRESET_CAMERA, tA );
|
|
if( !tCNT ){
|
|
tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PRESET_CAMERA, tA );
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if( tCNT ){
|
|
for ( int i = 0; i < tCNT; i++) {
|
|
deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[i];
|
|
cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getCamStatus() > 0)?(ARGUS_RPC_STATUS_CAM_OK<<(i*3)):0);
|
|
cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getStreamStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+1)));
|
|
cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getPtzStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+2)));
|
|
};
|
|
free( tA );
|
|
#ifdef DEBUG_EXTRA
|
|
} else {
|
|
LOG("No Camera Defined/Enabled In the system!");
|
|
#endif
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[128];
|
|
sprintf( tB, "Final Status: 0x%04X", cSTATUS->STATUSES );
|
|
LOG( tB );
|
|
#endif
|
|
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::flushTransactions(){
|
|
m_mCurrentCMDProtector.lock();
|
|
if ( m_pCurrentCMD ) {
|
|
if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
|
|
free( m_pCurrentCMD );
|
|
};
|
|
while( m_pCurrentCMD = m_qCMD->pop() ){
|
|
if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
|
|
free( m_pCurrentCMD );
|
|
};
|
|
m_pCurrentCMD = NULL;
|
|
m_mCurrentCMDProtector.unlock();
|
|
};
|
|
|
|
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:{
|
|
flushTransactions();
|
|
if ( m_bUseBackupServer ) {
|
|
if ( m_bActiveServer == 0 ) {
|
|
#ifdef DEBUG
|
|
char tB[128];
|
|
sprintf(tB,"Creating Remote connection to Main Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
|
|
LOG( tB );
|
|
#endif
|
|
m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
|
|
|
|
} else {
|
|
#ifdef DEBUG
|
|
char tB[128];
|
|
sprintf(tB,"Creating Remote connection to Backup Server [%s:%d]", m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
|
|
LOG( tB );
|
|
#endif
|
|
m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_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 {
|
|
LOG_ERROR("Connection to Remote Server Failed!");
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
setTransport( NULL, false );
|
|
delete( m_pPort );
|
|
m_pPort = NULL;
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
m_bActiveServer = 1 - m_bActiveServer;
|
|
setTO( 30 ); //30 sec to wait
|
|
setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
|
|
};
|
|
};
|
|
} else {
|
|
if (m_pMaster->m_sSiteData.remote_port) {
|
|
#ifdef DEBUG
|
|
char tB[128];
|
|
sprintf(tB,"Creating Remote connection to Alone Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
|
|
LOG( tB );
|
|
#endif
|
|
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 {
|
|
LOG_ERROR("Connection to Remote Server Failed!");
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
setTransport( NULL, false );
|
|
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:{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Wait Reconnect!");
|
|
#endif
|
|
if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT );
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_REGISTER:{
|
|
#ifdef DEBUG
|
|
LOG("Stage Register!");
|
|
#endif
|
|
CSTR tIP = m_pPort->getLocalIp4();
|
|
if( tIP ) {
|
|
m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( m_pCurrentCMD ) {
|
|
m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
|
|
m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_REGISTRATION );//strlen( m_pMaster->m_sIdentity );
|
|
m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
|
|
if ( m_pCurrentCMD->DATA ) {
|
|
memset( m_pCurrentCMD->DATA, 0, m_pCurrentCMD->DATA_SIZE );
|
|
strncpy( ((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->identity, m_pMaster->m_sIdentity, 32 );
|
|
strncpy(((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->peer, tIP, 22);
|
|
if( peer_send_rpc_pack( m_pCurrentCMD ) ){
|
|
setTO( 3 ); // Wait for 3 seconds to be registered with server
|
|
setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
|
|
free( tIP );
|
|
break;
|
|
};
|
|
free( m_pCurrentCMD->DATA );
|
|
};
|
|
free( m_pCurrentCMD );
|
|
};
|
|
free( tIP );
|
|
};
|
|
m_pCurrentCMD = NULL;
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_SEND_HELLO:{
|
|
#ifdef DEBUG
|
|
LOG("Stage HELLO!");
|
|
#endif
|
|
m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( m_pCurrentCMD ) {
|
|
m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
|
|
m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
|
|
m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
|
|
if (m_pCurrentCMD->DATA) {
|
|
LOG("Send Client Info!");
|
|
m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
|
|
LOG("ARGUS_DATA_MOJO_INFO");
|
|
if ( peer_send_rpc_pack( m_pCurrentCMD ) ){
|
|
setTO( 1 ); // Wait for 3 seconds
|
|
setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
|
|
};
|
|
free( m_pCurrentCMD->DATA );
|
|
};
|
|
free( m_pCurrentCMD );
|
|
};
|
|
m_pCurrentCMD = NULL;
|
|
break;
|
|
};
|
|
case ARGUS_RPC_STAGE_WAIT_ACK:{
|
|
//LOG("Wait ACK!");
|
|
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:{
|
|
LOG("Stage Disconnect!");
|
|
// Gracefully Disconnect
|
|
//if( peer_send_disconnect() ){
|
|
// bTO_COUNTER = 1;
|
|
// setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
|
|
//} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
|
|
//break; ---> Go to reconnect to other server
|
|
};
|
|
case ARGUS_RPC_STAGE_CLOSE:{
|
|
// Just Disconnect
|
|
LOG("Stage Close!");
|
|
flushTransactions();
|
|
setTransport( NULL, false );
|
|
delete( m_pPort );
|
|
m_pPort = NULL;
|
|
m_bShutdownCommunication = true;
|
|
m_bCommunicationEstablished = false;
|
|
m_bRegisteredWithRemotePeer = false;
|
|
setTO( 30 ); //30 sec to wait
|
|
m_bActiveServer = 1 - m_bActiveServer;
|
|
setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
|
|
break;
|
|
};
|
|
};
|
|
m_mCurrentVTRProtector.lock();
|
|
if ( m_vVTRHelpers.size() ) {
|
|
//Handle New Video Transfer
|
|
LOG("CHK HLPR");
|
|
if( m_vVTRHelpers.at(0)->state == 0 ){
|
|
RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( tPack ) {
|
|
tPack->DATA_SIZE = sizeof( ARGUS_DATA_VIDEO_INFO );
|
|
tPack->DATA = malloc( tPack->DATA_SIZE );
|
|
if ( tPack->DATA ) {
|
|
memcpy( tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE );
|
|
tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
|
|
if( m_qCMD->push( tPack ) ){
|
|
m_vVTRHelpers.at(0)->state = 1;
|
|
m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
|
|
} else {
|
|
free( tPack->DATA );
|
|
free( tPack );
|
|
}
|
|
} else {
|
|
free( tPack );
|
|
};
|
|
};
|
|
};
|
|
/// Validate Stalled/ Finished Videos (Try To Send next time
|
|
for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) {
|
|
if ( (*idx)->state == 2 ) {
|
|
if ((cTimeTSeconds - (*idx)->ts) > 5) {
|
|
// The transfer is staled
|
|
(*idx)->state = 0;
|
|
}
|
|
};
|
|
if ( (*idx)->state == 3 ){
|
|
LOG("Del HLPR");
|
|
//Finished Transfer
|
|
delete( (*idx)->ptr );
|
|
free( (*idx) );
|
|
m_vVTRHelpers.erase(idx);
|
|
LOG("Erase HLPR done");
|
|
break; // must revalidate Beg-End conditions for FOR loop
|
|
};
|
|
};
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
};
|
|
|
|
if (validateTransaction()) {
|
|
if ( fetchNewTransaction() ) {
|
|
if( peer_send_rpc_pack( m_pCurrentCMD ) ) {
|
|
setTO( 1 );
|
|
setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
|
|
} else LOG_ERROR("Failure!");
|
|
} 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;
|
|
static uint8_t pBuffer[4194304]; //4Mb
|
|
if ( ptr == NULL ) return false;
|
|
if( !m_bCommunicationEstablished ) return false;
|
|
//if( !m_bRegisteredWithRemotePeer ) return false;
|
|
pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
|
|
//uint16_t i = 0;
|
|
//for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
|
|
memcpy( pBuffer+ARGUS_HEADER_SIZE+1, ptr->DATA, ptr->DATA_SIZE );
|
|
m_nOutbondSerial++;
|
|
//retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
|
|
retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+ptr->DATA_SIZE));
|
|
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;
|
|
// Revalidate Data!!!
|
|
//m_bActiveServer = 0;
|
|
//m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
|
|
|
|
/*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;
|
|
};
|
|
};
|
|
*/
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
} else {
|
|
LOG("Not Connected!");
|
|
#endif
|
|
/*
|
|
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;
|
|
};
|
|
|
|
VTR_HELPER *mojoRemoteClient::getHelperByDBID( uint64_t dbid ){
|
|
m_mCurrentVTRProtector.lock();
|
|
if( m_vVTRHelpers.size() ){
|
|
for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) {
|
|
if ( (*idx)->ptr->getRemoteReferenceID() == dbid ) {
|
|
m_mCurrentVTRProtector.unlock();
|
|
return (*idx);
|
|
};
|
|
};
|
|
};
|
|
m_mCurrentVTRProtector.unlock();
|
|
return NULL;
|
|
};
|
|
|
|
bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Add Evt!");
|
|
#endif
|
|
if ( Evt == NULL ) return false;
|
|
RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
|
|
if ( tPack ) {
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[128];
|
|
sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000 );
|
|
LOG( tB );
|
|
#endif
|
|
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;
|
|
};
|
|
|
|
bool mojoRemoteClient::addVideo( VIDEO_SEGMENT_INFO *ptr ){
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Add Video!");
|
|
#endif
|
|
if ( ptr == NULL ) return false;
|
|
VTR_HELPER *tHLPR = (VTR_HELPER*) malloc( sizeof(VTR_HELPER));
|
|
if ( tHLPR ) {
|
|
tHLPR->state = 0;
|
|
tHLPR->ptr = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
|
|
LOG("NW HLPR");
|
|
if ( tHLPR->ptr ) {
|
|
LOG("HLPR OK");
|
|
if ( tHLPR->ptr->init( ptr )) {
|
|
LOG("HLPR INIT OK");
|
|
m_mCurrentVTRProtector.lock();
|
|
m_vVTRHelpers.push_back( tHLPR );
|
|
m_mCurrentVTRProtector.unlock();
|
|
LOG("HLPR PUSH OK");
|
|
return true;
|
|
};
|
|
delete( tHLPR->ptr );
|
|
};
|
|
free (tHLPR);
|
|
};
|
|
return false;
|
|
};
|
|
|
|
mojoVideoTransferHelper::mojoVideoTransferHelper( uint32_t buffer_size ){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_bVideoExists = false;
|
|
m_bVideoOpened = false;
|
|
m_nBufferSize = buffer_size;
|
|
m_pBuffer = (uint8_t *) malloc( m_nBufferSize );
|
|
m_nCTSMS = 0;
|
|
m_nRemoteRefID = 0;
|
|
m_pFD = NULL;
|
|
memset( m_cCurrentFile, 0, MAX_PATH );
|
|
};
|
|
|
|
|
|
mojoVideoTransferHelper::~mojoVideoTransferHelper(){
|
|
LOG("HDESTRC");
|
|
abort();
|
|
LOG("HDESTRC DONE");
|
|
};
|
|
|
|
#include <sys/stat.h>
|
|
|
|
bool mojoVideoTransferHelper::init( VIDEO_SEGMENT_INFO *ptr ){
|
|
if ( m_pBuffer == NULL ) return false;
|
|
if ( m_bVideoExists ) {
|
|
if ( m_bVideoOpened ) fclose( m_pFD );
|
|
unlink(m_cCurrentFile);
|
|
};
|
|
if( !( m_bVideoExists = fileExist(ptr->scratchPath) ) ) return false;
|
|
m_nRemoteRefID = 0;
|
|
|
|
strncpy( m_cCurrentFile, ptr->scratchPath, MAX_PATH );
|
|
m_cCurrentFile[MAX_PATH-1] = (char)0;
|
|
struct stat st;
|
|
stat( m_cCurrentFile, &st );
|
|
m_sIn.DATA_SIZE = st.st_size;
|
|
m_sIn.DTS = ptr->streamTSS;
|
|
m_sIn.DMS = ptr->streamTMS;
|
|
m_sIn.FPS = ptr->streamFPS;
|
|
m_sIn.REF_ID = m_nRemoteRefID;
|
|
strncpy( m_sIn.FMT, ptr->recordEXT, 5 );
|
|
m_sIn.FMT[5]=(char)0;
|
|
m_sIn.WIDTH = ptr->streamWidth;
|
|
m_sIn.HEIGHT = ptr->streamHeight;
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[129];
|
|
sprintf( tB, "VideoInfo Init: [%dx%d]@%d TS: %d.%d", m_sIn.WIDTH, m_sIn.HEIGHT, m_sIn.FPS, m_sIn.DTS, m_sIn.DMS );
|
|
LOG(tB);
|
|
#endif
|
|
m_pFD = fopen( m_cCurrentFile, "r" );
|
|
if( m_bVideoOpened = m_pFD != NULL ) return true;
|
|
return false;
|
|
};
|
|
|
|
uint8_t *mojoVideoTransferHelper::getNextChunk( uint32_t req_chunk_size, uint32_t *ack_size ){
|
|
uint8_t *retVal = NULL;
|
|
if ( m_bVideoOpened ) {
|
|
retVal = (uint8_t *) malloc( req_chunk_size + 12 );//+DB_SOURCE_ID_x_64+Size_32
|
|
if ( retVal ) {
|
|
int32_t rv = fread( retVal+12, 1, req_chunk_size, m_pFD );
|
|
if ( rv < 0 ) {
|
|
free( retVal );
|
|
retVal = NULL;
|
|
abort();
|
|
} else {
|
|
*ack_size = rv+12;
|
|
|
|
retVal[0] = ( m_nRemoteRefID >> 56 ) & 0xFF;
|
|
retVal[1] = ( m_nRemoteRefID >> 48 ) & 0xFF;
|
|
retVal[2] = ( m_nRemoteRefID >> 40 ) & 0xFF;
|
|
retVal[3] = ( m_nRemoteRefID >> 32 ) & 0xFF;
|
|
retVal[4] = ( m_nRemoteRefID >> 24 ) & 0xFF;
|
|
retVal[5] = ( m_nRemoteRefID >> 16 ) & 0xFF;
|
|
retVal[6] = ( m_nRemoteRefID >> 8 ) & 0xFF;
|
|
retVal[7] = m_nRemoteRefID & 0xFF;
|
|
|
|
retVal[8] = ( rv >> 24 ) & 0xFF;
|
|
retVal[9] = ( rv >> 16 ) & 0xFF;
|
|
retVal[10] = ( rv >> 8 ) & 0xFF;
|
|
retVal[11] = rv & 0xFF;
|
|
|
|
if ( feof( m_pFD )){
|
|
fclose( m_pFD );
|
|
m_bVideoOpened = false;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
ARGUS_DATA_VIDEO_INFO *mojoVideoTransferHelper::getVideoInfo(){
|
|
return &m_sIn;
|
|
};
|
|
|
|
void mojoVideoTransferHelper::abort(){
|
|
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|