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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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.MOJO_ROLE = 2;
  223. tINFO.SENSORS_COUNT = 2;
  224. tINFO.UPID = 2;
  225. tINFO.DWID = 3;
  226. tINFO.CPU_COUNT = m_pMaster->m_sSysInfo.CPU_CORES;
  227. tINFO.TOT_DISK = m_pMaster->m_sSysInfo.T_DISK;
  228. tINFO.TOT_MEMO = m_pMaster->m_sSysInfo.T_MEM;
  229. if( peer_send_hello( &tINFO ) ) {
  230. m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
  231. };
  232. break;
  233. };
  234. case ARGUS_RPC_STAGE_IDLE:{
  235. break;
  236. };
  237. };
  238. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  239. };
  240. LOG("Connector Thread Ended!");
  241. };
  242. /*
  243. Register With Server and make Time Sync With Server
  244. */
  245. bool mojoRemoteClient::peer_send_reg_req( CSTR identity ){
  246. if( identity == NULL ) return false;
  247. if( !m_bCommunicationEstablished ) return false;
  248. if( m_bRegisteredWithRemotePeer ) return true;
  249. uint8_t tBuffer[256];
  250. uint32_t tPackSize = ARGUS_HEADER_SIZE+33;
  251. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER;
  252. memcpy( tBuffer+1+ARGUS_HEADER_SIZE, identity, 32 );
  253. if( m_pPort->sendData( tBuffer, argus_rpc_finishupPackage( tBuffer, tPackSize ) ) ) {
  254. m_mQueryProtector.lock();
  255. m_nQueryMessage = ARGUS_DATA_MSG_REGISTER;
  256. m_mQueryProtector.unlock();
  257. //Waiting For Responce
  258. while( 1 ){
  259. if( m_bShutdownCommunication ) break;
  260. m_mQueryProtector.lock();
  261. if( m_nQueryMessage == 0 ) {
  262. m_mQueryProtector.unlock();
  263. break;
  264. };
  265. m_mQueryProtector.unlock();
  266. SLEEP(1);
  267. };
  268. m_bAutomationStage = ARGUS_RPC_STAGE_IDLE;
  269. };
  270. return m_bRegisteredWithRemotePeer;
  271. };
  272. bool mojoRemoteClient::peer_send_evt( ARGUS_DATA_EVENT *ptr ){
  273. if( ptr == NULL ) return false;
  274. if( !m_bCommunicationEstablished ) return false;
  275. if( !m_bRegisteredWithRemotePeer ) return false;
  276. uint8_t tBuffer[1024];
  277. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_EVT;
  278. uint16_t i = 0;
  279. for(; i < sizeof(ARGUS_DATA_EVENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  280. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  281. };
  282. bool mojoRemoteClient::peer_send_hello( ARGUS_DATA_MOJO_INFO *ptr ){
  283. if( ptr == NULL ) return false;
  284. if( !m_bCommunicationEstablished ) return false;
  285. if( !m_bRegisteredWithRemotePeer ) return false;
  286. uint8_t tBuffer[1024];
  287. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_INFO;
  288. uint16_t i = 0;
  289. for(; i < sizeof(ARGUS_DATA_MOJO_INFO); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  290. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  291. };
  292. bool mojoRemoteClient::peer_send_status( ARGUS_DATA_MOJO_STATUS *ptr ){
  293. if( ptr == NULL ) return false;
  294. if( !m_bCommunicationEstablished ) return false;
  295. if( !m_bRegisteredWithRemotePeer ) return false;
  296. uint8_t tBuffer[1024];
  297. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_KEEP_ALIVE;
  298. uint16_t i = 0;
  299. for(; i < sizeof(ARGUS_DATA_MOJO_STATUS); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  300. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  301. };
  302. bool mojoRemoteClient::peer_send_time_sync_req( ARGUS_DATA_TIME_SYNC_REQ *ptr ){
  303. if( ptr == NULL ) return false;
  304. if( !m_bCommunicationEstablished ) return false;
  305. if( !m_bRegisteredWithRemotePeer ) return false;
  306. uint8_t tBuffer[1024];
  307. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_TIME_SYNC;
  308. uint16_t i = 0;
  309. for(; i < sizeof(ARGUS_DATA_TIME_SYNC_REQ); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  310. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, ARGUS_HEADER_SIZE+1+i ));
  311. };
  312. bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  313. return true;
  314. };
  315. bool mojoRemoteClient::validate(){
  316. if( m_bCommunicationEstablished ){
  317. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  318. m_bCommunicationEstablished = false;
  319. if( m_pPort ) {
  320. // Cancel all ongoing Queries
  321. m_bShutdownCommunication = true;
  322. setTransport( NULL, false );
  323. delete( m_pPort );
  324. };
  325. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  326. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  327. if( m_pPort ) {
  328. if( m_pPort->create() ) {
  329. m_bShutdownCommunication = false;
  330. m_bCommunicationEstablished = true;
  331. m_bRegisteredWithRemotePeer = false;
  332. setTransport( m_pPort, false );
  333. } else {
  334. delete( m_pPort );
  335. m_pPort = NULL;
  336. };
  337. };
  338. */
  339. };
  340. } else {
  341. /*
  342. if( m_pPort ) {
  343. // Cancel all ongoing Queries
  344. m_bShutdownCommunication = true;
  345. setTransport( NULL, false );
  346. delete( m_pPort );
  347. };
  348. //Try To connect
  349. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  350. if( m_pPort ) {
  351. if( m_pPort->create() ) {
  352. m_bShutdownCommunication = false;
  353. m_bCommunicationEstablished = true;
  354. m_bRegisteredWithRemotePeer = false;
  355. setTransport( m_pPort, false );
  356. } else {
  357. delete( m_pPort );
  358. m_pPort = NULL;
  359. };
  360. };
  361. */
  362. };
  363. return true;
  364. };
  365. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  366. bool retVal = false;
  367. LOG("Add Evt!");
  368. ARGUS_DATA_EVENT *tPac = argus_rpc_convert_evt( Evt );
  369. if( tPac ){
  370. retVal = peer_send_evt( tPac );
  371. free( tPac );
  372. };
  373. return retVal;
  374. };
  375. #undef LOG
  376. #undef LOG_ERROR