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.

394 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
  1. #include "mojoRemoteClient.h"
  2. #include "mojoKernel.h"
  3. #define LOG( X ) m_pLog->Log( "RPC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "RPC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "RPC", this, _LOG_ERROR, X );
  12. #endif
  13. mojoRemoteClient::mojoRemoteClient( mojoKernel * ptr ):protocolParser(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_pPort = NULL;
  16. m_pMaster = ptr;
  17. m_bShutdownCommunication = true;
  18. m_bCommunicationEstablished = false;
  19. m_bRegisteredWithRemotePeer = false;
  20. };
  21. mojoRemoteClient::~mojoRemoteClient(){
  22. waitThreadEnd( true );
  23. if( m_bRegisteredWithRemotePeer ) {
  24. //Unregister Mojo
  25. };
  26. m_bShutdownCommunication = true;
  27. m_bCommunicationEstablished = false;
  28. m_bRegisteredWithRemotePeer = false;
  29. if ( m_pPort ) delete( m_pPort );
  30. };
  31. bool mojoRemoteClient::create(){
  32. if( m_pMaster == NULL ) return false;
  33. if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true;
  34. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  35. return init();
  36. };
  37. int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){
  38. //char tB[64];
  39. if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) {
  40. if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){
  41. uint32_t SIZE = Buffer[1];
  42. SIZE = ( SIZE << 8 ) | Buffer[2];
  43. SIZE = ( SIZE << 8 ) | Buffer[3];
  44. SIZE = ( SIZE << 8 ) | Buffer[4];
  45. //sprintf( tB, "Pack Size: %08X", SIZE );
  46. //LOG(tB);
  47. if ( ( SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) {
  48. uint16_t tCRC = checksum16( Buffer, SIZE + ARGUS_HEADER_SIZE );
  49. uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE];
  50. cCRC = ( cCRC << 8 ) | Buffer[SIZE + ARGUS_HEADER_SIZE+1];
  51. //sprintf( tB, "cCRC: %04X tCRC: %04X", tCRC, cCRC );
  52. //LOG(tB);
  53. if ( tCRC == cCRC ) return 0;
  54. } else return 1; // to grou
  55. } else return 1; //to grow
  56. };
  57. return -1; // to skip
  58. };
  59. protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){
  60. protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage));
  61. if ( tPack ) {
  62. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  63. tPack->m_nSize = Buffer[1];
  64. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[2];
  65. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[3];
  66. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[4];
  67. tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
  68. tPack->m_pData = (U8 *) malloc( tPack->m_nSize );
  69. if ( tPack->m_pData ) {
  70. memcpy( tPack->m_pData, Buffer, tPack->m_nSize );
  71. } else {
  72. free( tPack );
  73. tPack = NULL;
  74. };
  75. };
  76. return tPack;
  77. };
  78. bool mojoRemoteClient::packetHandler( protocolPackage *Package ){
  79. uint32_t tQueryMessage;
  80. m_mQueryProtector.lock();
  81. tQueryMessage = m_nQueryMessage;
  82. m_mQueryProtector.unlock();
  83. switch( Package->m_nCode ){
  84. case ARGUS_DATA_MSG_REGISTER: {
  85. if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) {
  86. m_mQueryProtector.lock();
  87. m_nQueryMessage = 0;
  88. m_mQueryProtector.unlock();
  89. };
  90. m_bRegisteredWithRemotePeer = true;
  91. break;
  92. };
  93. case ARGUS_DATA_MSG_DISABLED:
  94. case ARGUS_DATA_MSG_ACCESS_DENIED:{
  95. if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) {
  96. m_mQueryProtector.lock();
  97. m_nQueryMessage = 0;
  98. m_mQueryProtector.unlock();
  99. };
  100. m_bRegisteredWithRemotePeer = false;
  101. break;
  102. };
  103. case ARGUS_DATA_MSG_TIME_SYNC:{
  104. struct timespec tS = { 0 };
  105. uint64_t cT = getMilliseconds();
  106. ARGUS_DATA_TIME_SYNC_ACK *tPTR = (ARGUS_DATA_TIME_SYNC_ACK *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
  107. uint64_t eTime = tPTR->ATS + ( (cT - tPTR->STS) >> 1 );
  108. tS.tv_sec = eTime / 1000;
  109. tS.tv_nsec = ( eTime % 1000 ) * 1000000;
  110. if( clock_settime( CLOCK_REALTIME, &tS ) != 0){
  111. LOG_ERROR("Time Correction Failed!");
  112. switch ( errno ) {
  113. case EACCES: LOG_ERROR("Not have write permission for the dynamic POSIX clock device indicated!"); break;
  114. case EFAULT: LOG_ERROR("tS points outside the accessible address space!"); break;
  115. case EINVAL: LOG_ERROR("Parameter specified is invalid!"); break;
  116. 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;
  117. case ENOTSUP: LOG_ERROR("The operation is not supported by the dynamic POSIX clock device specified!"); break;
  118. case EPERM: LOG_ERROR("clock_settime() does not have permission to set the clock indicated!"); break;
  119. };
  120. };
  121. #ifdef DEBUG_EXTRA
  122. LOG("Time Sync ACK!");
  123. char tB[256];
  124. sprintf( tB,"RT:%lld ST:%lld CT:%lld", tPTR->STS, tPTR->ATS, cT );
  125. LOG( tB );
  126. sprintf( tB,"tv_s:%lld tv_ns:%lld", tS.tv_sec, tS.tv_nsec );
  127. LOG( tB );
  128. #endif
  129. break;
  130. };
  131. };
  132. return true;
  133. };
  134. void mojoRemoteClient::m_fThreadProc( void ){
  135. time_t tTimeTSeconds = 0;
  136. time_t cTimeTSeconds = 0;
  137. uint8_t bTO_COUNTER = 0;
  138. uint32_t tSecondsCounter = 0;
  139. m_bThreadStatus = TH_WORKING;
  140. LOG("Connector Thread Started");
  141. while ( m_bThreadStatus < TH_ENDED ) {
  142. // seconds Counter (for internal purposes)
  143. cTimeTSeconds = time( NULL );
  144. if ( cTimeTSeconds!=tTimeTSeconds ) {
  145. //Process each second
  146. tSecondsCounter++;
  147. tTimeTSeconds = cTimeTSeconds;
  148. switch ( m_bAutomationStage ) {
  149. case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
  150. if ( bTO_COUNTER ) {
  151. bTO_COUNTER--;
  152. } else {
  153. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  154. };
  155. break;
  156. };
  157. case ARGUS_RPC_STAGE_IDLE:{
  158. uint8_t tS = m_pMaster->m_pDBC->getLRFDataStatus( 1 );
  159. ARGUS_DATA_MOJO_STATUS cSTATUS;
  160. DBC_SYS_LOAD_INFO cSYSSTAT;
  161. cSTATUS.STATUSES = 0xff00ff00 | tS;
  162. m_pMaster->getSystemUtilization( &cSYSSTAT );
  163. cSTATUS.CPU_LOAD = round( cSYSSTAT.cpu15 );
  164. cSTATUS.CPU_TEMP = round( cSYSSTAT.temperature );
  165. //if( cSTATUS.CPU_LOAD == 0xFF ) cSTATUS.CPU_LOAD = 0;
  166. //if( cSTATUS.CPU_TEMP == 0xFF ) cSTATUS.CPU_TEMP = 0;
  167. cSTATUS.DISK_UTI = cSYSSTAT.disk;
  168. cSTATUS.MEMO_UTI = cSYSSTAT.mem;
  169. peer_send_status( &cSTATUS );
  170. break;
  171. };
  172. };
  173. if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) {
  174. LOG("Req Time Sync!");
  175. ARGUS_DATA_TIME_SYNC_REQ tTR;
  176. tTR.STS = getMilliseconds();
  177. peer_send_time_sync_req( &tTR );
  178. };
  179. };
  180. switch ( m_bAutomationStage ) {
  181. case ARGUS_RPC_STAGE_CONNECT:{
  182. #ifdef DEBUG
  183. char tB[128];
  184. sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  185. LOG( tB );
  186. #endif
  187. if( m_pMaster->m_sSiteData.remote_port ) {
  188. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  189. if( m_pPort ) {
  190. if( m_pPort->create() ) {
  191. LOG("Connection Sucessfull");
  192. m_bShutdownCommunication = false;
  193. m_bCommunicationEstablished = true;
  194. m_bRegisteredWithRemotePeer = false;
  195. setTransport( m_pPort, false );
  196. m_bAutomationStage = ARGUS_RPC_STAGE_REGISTER;
  197. } else {
  198. delete( m_pPort );
  199. m_pPort = NULL;
  200. m_bShutdownCommunication = true;
  201. m_bCommunicationEstablished = false;
  202. m_bRegisteredWithRemotePeer = false;
  203. bTO_COUNTER = 30; //30 sec to wait
  204. m_bAutomationStage = ARGUS_RPC_STAGE_WAIT_RECONNECT;
  205. };
  206. };
  207. };
  208. break;
  209. };
  210. case ARGUS_RPC_STAGE_WAIT_RECONNECT: break;
  211. case ARGUS_RPC_STAGE_REGISTER:{
  212. if( peer_send_reg_req( m_pMaster->m_sIdentity )){
  213. //m_nQueryMessage = ARGUS_RPC_STAGE_REGISTER;
  214. m_bAutomationStage = ARGUS_RPC_STAGE_SEND_HELLO;
  215. bTO_COUNTER = 1;
  216. };
  217. break;
  218. };
  219. case ARGUS_RPC_STAGE_SEND_HELLO:{
  220. ARGUS_DATA_MOJO_INFO tINFO;
  221. tINFO.MOJO_WORK_MODE = 2;
  222. tINFO.UPPER_ID = 2;
  223. tINFO.BOTTOM_ID = 3;
  224. tINFO.CAM_ID = 4;
  225. tINFO.CPU_COUNT = m_pMaster->m_sSysInfo.CPU_CORES;
  226. tINFO.TOT_DISK = m_pMaster->m_sSysInfo.T_DISK;
  227. tINFO.TOT_MEMO = m_pMaster->m_sSysInfo.T_MEM;
  228. if( peer_send_hello( &tINFO ) ) {
  229. m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
  230. };
  231. break;
  232. };
  233. case ARGUS_RPC_STAGE_IDLE:{
  234. break;
  235. };
  236. };
  237. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  238. };
  239. LOG("Connector Thread Ended!");
  240. };
  241. /*
  242. Register With Server and make Time Sync With Server
  243. */
  244. bool mojoRemoteClient::peer_send_reg_req( CSTR identity ){
  245. if( identity == NULL ) return false;
  246. if( !m_bCommunicationEstablished ) return false;
  247. if( m_bRegisteredWithRemotePeer ) return true;
  248. uint8_t tBuffer[256];
  249. uint32_t tPackSize = ARGUS_HEADER_SIZE+33;
  250. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER;
  251. memcpy( tBuffer+1+ARGUS_HEADER_SIZE, identity, 32 );
  252. if( m_pPort->sendData( tBuffer, argus_rpc_finishupPackage( tBuffer, tPackSize ) ) ) {
  253. m_mQueryProtector.lock();
  254. m_nQueryMessage = ARGUS_DATA_MSG_REGISTER;
  255. m_mQueryProtector.unlock();
  256. //Waiting For Responce
  257. while( 1 ){
  258. if( m_bShutdownCommunication ) break;
  259. m_mQueryProtector.lock();
  260. if( m_nQueryMessage == 0 ) {
  261. m_mQueryProtector.unlock();
  262. break;
  263. };
  264. m_mQueryProtector.unlock();
  265. SLEEP(1);
  266. };
  267. m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
  268. };
  269. return m_bRegisteredWithRemotePeer;
  270. };
  271. bool mojoRemoteClient::peer_send_evt( ARGUS_DATA_EVENT *ptr ){
  272. if( ptr == NULL ) return false;
  273. if( !m_bCommunicationEstablished ) return false;
  274. if( !m_bRegisteredWithRemotePeer ) return false;
  275. uint8_t tBuffer[1024];
  276. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_EVT;
  277. uint16_t i = 0;
  278. for(; i < sizeof(ARGUS_DATA_EVENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  279. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  280. };
  281. bool mojoRemoteClient::peer_send_hello( ARGUS_DATA_MOJO_INFO *ptr ){
  282. if( ptr == NULL ) return false;
  283. if( !m_bCommunicationEstablished ) return false;
  284. if( !m_bRegisteredWithRemotePeer ) return false;
  285. uint8_t tBuffer[1024];
  286. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_INFO;
  287. uint16_t i = 0;
  288. for(; i < sizeof(ARGUS_DATA_MOJO_INFO); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  289. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  290. };
  291. bool mojoRemoteClient::peer_send_status( ARGUS_DATA_MOJO_STATUS *ptr ){
  292. if( ptr == NULL ) return false;
  293. if( !m_bCommunicationEstablished ) return false;
  294. if( !m_bRegisteredWithRemotePeer ) return false;
  295. uint8_t tBuffer[1024];
  296. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_KEEP_ALIVE;
  297. uint16_t i = 0;
  298. for(; i < sizeof(ARGUS_DATA_MOJO_STATUS); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  299. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  300. };
  301. bool mojoRemoteClient::peer_send_time_sync_req( ARGUS_DATA_TIME_SYNC_REQ *ptr ){
  302. if( ptr == NULL ) return false;
  303. if( !m_bCommunicationEstablished ) return false;
  304. if( !m_bRegisteredWithRemotePeer ) return false;
  305. uint8_t tBuffer[1024];
  306. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_TIME_SYNC;
  307. uint16_t i = 0;
  308. for(; i < sizeof(ARGUS_DATA_TIME_SYNC_REQ); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  309. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  310. };
  311. bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  312. return true;
  313. };
  314. bool mojoRemoteClient::validate(){
  315. if( m_bCommunicationEstablished ){
  316. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  317. m_bCommunicationEstablished = false;
  318. if( m_pPort ) {
  319. // Cancel all ongoing Queries
  320. m_bShutdownCommunication = true;
  321. setTransport( NULL, false );
  322. delete( m_pPort );
  323. };
  324. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  325. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  326. if( m_pPort ) {
  327. if( m_pPort->create() ) {
  328. m_bShutdownCommunication = false;
  329. m_bCommunicationEstablished = true;
  330. m_bRegisteredWithRemotePeer = false;
  331. setTransport( m_pPort, false );
  332. } else {
  333. delete( m_pPort );
  334. m_pPort = NULL;
  335. };
  336. };
  337. */
  338. };
  339. } else {
  340. /*
  341. if( m_pPort ) {
  342. // Cancel all ongoing Queries
  343. m_bShutdownCommunication = true;
  344. setTransport( NULL, false );
  345. delete( m_pPort );
  346. };
  347. //Try To connect
  348. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  349. if( m_pPort ) {
  350. if( m_pPort->create() ) {
  351. m_bShutdownCommunication = false;
  352. m_bCommunicationEstablished = true;
  353. m_bRegisteredWithRemotePeer = false;
  354. setTransport( m_pPort, false );
  355. } else {
  356. delete( m_pPort );
  357. m_pPort = NULL;
  358. };
  359. };
  360. */
  361. };
  362. return true;
  363. };
  364. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  365. bool retVal = false;
  366. LOG("Add Evt!");
  367. ARGUS_DATA_EVENT *tPac = argus_rpc_convert_evt( Evt );
  368. if( tPac ){
  369. retVal = peer_send_evt( tPac );
  370. free( tPac );
  371. };
  372. return retVal;
  373. };
  374. #undef LOG
  375. #undef LOG_ERROR