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.

711 lines
18 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolDefender.h"
  2. #include "../../alg/generic/md5.h"
  3. #define LOG( X ) m_pLog->Log( "Defender", this, X )
  4. #define LOGS( X ) basicLogCollection::getInstance()->Log( "Defender", NULL, X )
  5. #define LOG_ERROR( X ) m_pLog->Log( "Defender", this, _LOG_ERROR, X )
  6. CSTR defenderStatusMode[] = {
  7. "Operational",
  8. "Degraded"
  9. };
  10. CSTR defenderFaultCode[] = {
  11. "Critical",
  12. "Warning",
  13. "Info"
  14. };
  15. CSTR defenderCategories[] = {
  16. "STATUS",
  17. "ALARM",
  18. "ERROR"
  19. };
  20. CSTR defenderTypeDescription[] = {
  21. "Status Message",
  22. "Target Detection Event",
  23. "Fault Event"
  24. };
  25. CSTR defenderTypeSeverity[] = {
  26. "very-low",
  27. "low",
  28. "medium",
  29. "high",
  30. "very-high"
  31. };
  32. /* Global Time Diff Value to correct Timestamps */
  33. static int g_iTimeDiff = 0;
  34. /**
  35. * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  36. */
  37. //#define EPOCH_DIFF 11644473600LL
  38. #define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
  39. #ifdef __linux__
  40. unsigned long long getSHDTimeValue( void ){
  41. unsigned long long tMilliseconds;
  42. struct timeval tData;
  43. struct timezone tTZ;
  44. tMilliseconds = 0;
  45. tTZ.tz_minuteswest = 0;
  46. tTZ.tz_dsttime = 0;
  47. if( gettimeofday( &tData, &tTZ ) == -1 ){
  48. } else {
  49. tMilliseconds = tData.tv_sec;
  50. tMilliseconds = tMilliseconds *1000;
  51. tMilliseconds += tData.tv_usec / 1000;
  52. };
  53. #else
  54. /*
  55. void TimetToFileTime( time_t t, LPFILETIME pft ){
  56. LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  57. pft->dwLowDateTime = (DWORD) ll;
  58. pft->dwHighDateTime = ll >>32;
  59. }*/
  60. unsigned __int64 getSHDTimeValue( void ){
  61. unsigned __int64 tMilliseconds;
  62. SYSTEMTIME Ztime;
  63. GetSystemTime(&Ztime);
  64. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  65. tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
  66. tMilliseconds = tMilliseconds / 10000;
  67. #endif
  68. tMilliseconds += g_iTimeDiff;
  69. return tMilliseconds;
  70. };
  71. CSTR getSHDTimeStamp( void ){
  72. CSTR RetVal = NULL;
  73. time_t rawtime;
  74. uint32_t msec;
  75. struct tm * ptm = NULL;
  76. time ( &rawtime );
  77. ptm = gmtime ( &rawtime );
  78. msec = getSHTimeValue() % 1000;
  79. //2019-03-24T12:32:59.670Z
  80. if( ptm != NULL ) {
  81. RetVal = (CSTR) malloc( 128 );
  82. if( RetVal!=NULL ){
  83. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  84. (ptm->tm_year+1900),
  85. (ptm->tm_mon+1),
  86. ptm->tm_mday,
  87. ptm->tm_hour,
  88. ptm->tm_min,
  89. ptm->tm_sec,
  90. msec
  91. );
  92. };
  93. };
  94. return RetVal;
  95. };
  96. bool correctSHDTime( int diff ){
  97. #ifdef __linux__
  98. struct timeval tData;
  99. struct timezone tTZ;
  100. tTZ.tz_minuteswest = 0;
  101. tTZ.tz_dsttime = 0;
  102. if( gettimeofday( &tData, &tTZ ) == -1 ){
  103. return false;
  104. } else {
  105. tData.tv_sec += diff / 1000;
  106. tData.tv_usec += ( diff * 1000 ) % 1000000;
  107. };
  108. if( settimeofday( &tData, &tTZ ) == -1 ){
  109. return false;
  110. };
  111. #else
  112. unsigned __int64 tMilliseconds;
  113. SYSTEMTIME Ztime;
  114. GetSystemTime(&Ztime);
  115. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  116. tMilliseconds += diff * 10000;
  117. FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
  118. if( 0 == SetSystemTime(&Ztime) ) return false;
  119. #endif
  120. return true;
  121. };
  122. CSTR protocolDefender::buildURL( CSTR templ, int dim, CSTR devID ){
  123. CSTR RetVal;
  124. char tpl[256];
  125. RetVal = (CSTR) malloc( 256 );
  126. sprintf( tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ );
  127. sprintf( RetVal, tpl, dim, devID );
  128. return RetVal;
  129. };
  130. CSTR protocolDefender::buildUniqID( void ){
  131. CSTR RetVal;
  132. RetVal = (CSTR) malloc(64);
  133. sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) );
  134. return RetVal;
  135. };
  136. /* Default Constructor */
  137. protocolDefender::protocolDefender( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION") ){
  138. m_pLog = basicLogCollection::getInstance();
  139. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION");
  140. m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
  141. if( m_pScannerName == NULL ) return;
  142. memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
  143. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  144. if( m_pServerIP == NULL ) return;
  145. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  146. m_bCurrentStatus = false;
  147. if( m_pSettings != NULL ){
  148. config_setting_t *pSetting;
  149. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
  150. strncpy( m_pScannerName, "LRF_2D_001", 64 );
  151. LOG( m_pScannerName );
  152. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  153. config_setting_set_string(pSetting, m_pScannerName);
  154. basicSettingsProvider::getInstance()->compromise();
  155. };
  156. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
  157. m_nScannerDimentions = 2;
  158. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  159. config_setting_set_int(pSetting, m_nScannerDimentions);
  160. basicSettingsProvider::getInstance()->compromise();
  161. };
  162. if (!config_setting_lookup_string(m_pSettings, "CEP_IP_ADDRESS", (const char**)&m_pServerIP)){
  163. strncpy( m_pServerIP, "192.168.200.204", 16 );
  164. LOG( m_pServerIP );
  165. pSetting = config_setting_add(m_pSettings, "CEP_IP_ADDRESS", CONFIG_TYPE_STRING);
  166. config_setting_set_string(pSetting, m_pServerIP);
  167. basicSettingsProvider::getInstance()->compromise();
  168. };
  169. if (!config_setting_lookup_int(m_pSettings, "CEP_IP_PORT", &m_nServerPort)){
  170. m_nServerPort = 80;
  171. pSetting = config_setting_add(m_pSettings, "CEP_IP_PORT", CONFIG_TYPE_INT);
  172. config_setting_set_int(pSetting, m_nServerPort);
  173. basicSettingsProvider::getInstance()->compromise();
  174. };
  175. basicSettingsProvider::getInstance()->validate();
  176. };
  177. };
  178. /*
  179. 1.2 GHz
  180. Number of Cores
  181. Quad-Core
  182. Type
  183. Tegra 3
  184. Processor Number
  185. T30SL
  186. Manufacturer
  187. NVIDIA
  188. Verify that you have the following installed on your system
  189. Ubuntu 16.04 LTS (with GCC 5.4)
  190. NVIDIA Display Driver ver. 375
  191. NVIDIA VideoSDK 7.1
  192. cuDNN 6 & TensorRT 2.1
  193. NVIDIA CUDA 8.0
  194. Download the DeepStream 1.0 tar file (below)
  195. Extract the package at the location of your choice
  196. Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
  197. */
  198. protocolDefender::~protocolDefender(){
  199. if(m_pScannerName!=NULL)free(m_pScannerName);
  200. if(m_pServerIP!=NULL)free(m_pServerIP);
  201. };
  202. void protocolDefender::setScannerName( CSTR Name ){
  203. sprintf( m_pScannerName, "%s", Name );
  204. };
  205. void protocolDefender::setProtocolType( bool DeviceIs3D ){
  206. if( DeviceIs3D ) {
  207. m_nScannerDimentions = 3;
  208. } else {
  209. m_nScannerDimentions = 2;
  210. };
  211. };
  212. CSTR protocolDefender::assembleStatusData( bool StatusOK ){
  213. CSTR RetVal;
  214. CSTR tBuf;
  215. CSTR tSRC;
  216. CSTR tTARGET;
  217. CSTR tID;
  218. RetVal = (CSTR) malloc(CLOG);
  219. if ( RetVal != NULL ) {
  220. tBuf = getSHDTimeStamp();
  221. if( tBuf != NULL ) {
  222. tTARGET = (CSTR) malloc(128);
  223. if( tTARGET!= NULL) {
  224. tSRC = (CSTR) malloc(128);
  225. if( tSRC ) {
  226. tID = (CSTR) malloc(64);
  227. if( tID != NULL ) {
  228. sprintf( tID, "%s", MD5String( tBuf ) );
  229. sprintf( tSRC, DFSL_SensorSourceDto, DFSL_HARDWARE_TYPE, DFSL_SOURCE_NAME, m_pScannerName );
  230. sprintf( tTARGET, "{ \"status\": \"%s\" }", defenderStatusMode[((StatusOK)?1:0)] );
  231. sprintf( RetVal,
  232. PACKAGE_SE_DATA_STATUS_ELEMENT,
  233. defenderTypeDescription[DC_STATUS],
  234. tBuf,
  235. tSRC,
  236. "DFSL_STATUS_DATA",
  237. "EventLidarStatus",
  238. tID,
  239. tTARGET
  240. );
  241. free( tID );
  242. free( tSRC );
  243. free( tTARGET );
  244. free( tBuf );
  245. } else {
  246. free( tSRC );
  247. free( tTARGET );
  248. free( tBuf );
  249. free( RetVal );
  250. }
  251. } else {
  252. free( tTARGET );
  253. free( tBuf );
  254. free( RetVal );
  255. };
  256. } else {
  257. free( tBuf );
  258. free( RetVal );
  259. };
  260. } else {
  261. free( RetVal );
  262. RetVal = NULL;
  263. };
  264. };
  265. return RetVal;
  266. };
  267. CSTR protocolDefender::assembleFaultData( uint8_t Code, CSTR Desc ){
  268. CSTR RetVal;
  269. CSTR tBuf;
  270. CSTR tSRC;
  271. CSTR tTARGET;
  272. CSTR tID;
  273. RetVal = (CSTR) malloc(CLOG);
  274. if ( RetVal != NULL ) {
  275. tBuf = getSHDTimeStamp();
  276. if( tBuf != NULL ) {
  277. tTARGET = (CSTR) malloc(128);
  278. if( tTARGET!= NULL) {
  279. tSRC = (CSTR) malloc(128);
  280. if( tSRC ) {
  281. tID = (CSTR) malloc(64);
  282. if( tID != NULL ) {
  283. sprintf( tID, "%s", MD5String( tBuf ) );
  284. sprintf( tSRC, DFSL_SensorSourceDto, DFSL_HARDWARE_TYPE, DFSL_SOURCE_NAME, m_pScannerName );
  285. sprintf( tTARGET, "{ \"code\": %d, \"description\": \"%s\" }", Code, Desc );
  286. sprintf( RetVal,
  287. PACKAGE_SE_DATA_STATUS_ELEMENT,
  288. defenderTypeDescription[DC_ERROR],
  289. tBuf,
  290. tSRC,
  291. "DFSL_FAULT_DATA",
  292. "EventLidarFault",
  293. tID,
  294. tTARGET
  295. );
  296. free( tID );
  297. free( tSRC );
  298. free( tTARGET );
  299. free( tBuf );
  300. } else {
  301. free( tSRC );
  302. free( tTARGET );
  303. free( tBuf );
  304. free( RetVal );
  305. }
  306. } else {
  307. free( tTARGET );
  308. free( tBuf );
  309. free( RetVal );
  310. };
  311. } else {
  312. free( tBuf );
  313. free( RetVal );
  314. };
  315. } else {
  316. free( RetVal );
  317. RetVal = NULL;
  318. };
  319. };
  320. return RetVal;
  321. };
  322. CSTR protocolDefender::assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation ){
  323. CSTR RetVal;
  324. CSTR tBuf;
  325. CSTR tSRC;
  326. CSTR tTARGET;
  327. CSTR tID;
  328. RetVal = (CSTR) malloc(CLOG);
  329. if ( RetVal != NULL ) {
  330. tBuf = getSHDTimeStamp();
  331. if( tBuf != NULL ) {
  332. tTARGET = (CSTR) malloc(128);
  333. if( tTARGET!= NULL) {
  334. tSRC = (CSTR) malloc(128);
  335. if( tSRC ) {
  336. tID = (CSTR) malloc(64);
  337. if( tID != NULL ) {
  338. sprintf( tID, "%s", MD5String( tBuf ) );
  339. sprintf( tSRC, DFSL_SensorSourceDto, DFSL_HARDWARE_TYPE, DFSL_SOURCE_NAME, m_pScannerName );
  340. sprintf( tTARGET, DFSL_LidarTargetDto, targetID, Azimuth, Elevation, Range, Width );
  341. sprintf( RetVal,
  342. PACKAGE_SE_DATA_ELEMENT,
  343. defenderTypeDescription[DC_ALARM],
  344. tBuf,
  345. tSRC,
  346. "DFSL_TARGET_DATA",
  347. "EventLidarTarget",
  348. tID,
  349. tTARGET
  350. );
  351. free( tID );
  352. free( tSRC );
  353. free( tTARGET );
  354. free( tBuf );
  355. } else {
  356. free( tSRC );
  357. free( tTARGET );
  358. free( tBuf );
  359. free( RetVal );
  360. }
  361. } else {
  362. free( tTARGET );
  363. free( tBuf );
  364. free( RetVal );
  365. };
  366. } else {
  367. free( tBuf );
  368. free( RetVal );
  369. };
  370. } else {
  371. free( RetVal );
  372. RetVal = NULL;
  373. };
  374. };
  375. return RetVal;
  376. };
  377. static uint32_t DEFENDER_MESSAGE_ID = 0;
  378. size_t protocolDefender::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){
  379. char tBuffer[CLOG];
  380. size_t RetVal;
  381. CSTR tBuf;
  382. RetVal = 0;
  383. tBuf = getSHDTimeStamp();
  384. DEFENDER_MESSAGE_ID++;
  385. //memset( tBuffer, 0, CLOG );
  386. if( tBuf != NULL ){
  387. //char ID[64];
  388. switch ( Type ) {
  389. case DC_STATUS:
  390. Severity = 0;
  391. break;
  392. case DC_ERROR:
  393. Severity = 2;
  394. break;
  395. case DC_ALARM:
  396. Severity = 4;
  397. break;
  398. default:
  399. Severity = 2;
  400. };
  401. //sprintf( ID, "%s", MD5String( tBuf ) );
  402. sprintf( tBuffer,
  403. DFSL_SE_ENVELOPE,
  404. DEFENDER_MESSAGE_ID,
  405. defenderCategories[Type],
  406. defenderTypeSeverity[Severity],
  407. tBuf, tBuf, tBuf, tBuf,
  408. Location,
  409. Data
  410. );
  411. RetVal = strlen(tBuffer);
  412. memcpy( Buffer, tBuffer, RetVal );
  413. Buffer[RetVal] = (char) 0;
  414. free( tBuf );
  415. };
  416. return RetVal;
  417. };
  418. bool protocolDefender::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
  419. if( Buffer!= NULL ){
  420. CSTR tBuf;
  421. tBuf = assembleStatusData( Status );
  422. #ifdef DEBUG
  423. LOG(tBuf);
  424. #endif
  425. if( tBuf != NULL ) {
  426. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_DEFAULT_LOCATION, tBuf );
  427. #ifdef DEBUG
  428. LOG((CSTR)Buffer);
  429. #endif
  430. free( tBuf );
  431. return true;
  432. }
  433. };
  434. return false;
  435. };
  436. bool protocolDefender::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
  437. if( Buffer!= NULL ){
  438. CSTR tBuf;
  439. tBuf = assembleFaultData( Type, Description );
  440. if( tBuf != NULL ){
  441. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_DEFAULT_LOCATION, tBuf );
  442. free( tBuf );
  443. return true;
  444. };
  445. };
  446. return false;
  447. };
  448. bool protocolDefender::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width){
  449. if( Buffer!= NULL ){
  450. CSTR tBuf;
  451. tBuf = assembleTargetData( targetID, Range, Width, Angle, 0 );
  452. if( tBuf != NULL ){
  453. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_DEFAULT_LOCATION, tBuf );
  454. free( tBuf );
  455. return true;
  456. };
  457. };
  458. return false;
  459. };
  460. bool protocolDefender::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range ){
  461. if( Buffer!= NULL ){
  462. CSTR tBuf;
  463. tBuf = assembleTargetData( targetID, Range, 0, Angle1, Angle2 );
  464. #ifdef DEBUG
  465. LOG(tBuf);
  466. #endif
  467. if( tBuf != NULL ){
  468. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_DEFAULT_LOCATION, tBuf );
  469. #ifdef DEBUG
  470. LOG((CSTR)Buffer);
  471. #endif
  472. free( tBuf );
  473. return true;
  474. };
  475. };
  476. return false;
  477. };
  478. bool protocolDefender::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
  479. /*if( Buffer!= NULL ){
  480. CSTR tBuf;
  481. tBuf = getSHTimeStamp();
  482. if( tBuf != NULL ){
  483. sprintf(
  484. ((char *)(Buffer+BufferSize)),
  485. PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
  486. tBuf
  487. );
  488. BufferSize = strlen( (char *)Buffer );
  489. free( tBuf );
  490. return true;
  491. };
  492. };*/
  493. return false;
  494. };
  495. bool protocolDefender::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
  496. /*if( Buffer!= NULL ){
  497. sprintf(
  498. ((char *)(Buffer+BufferSize)),
  499. PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
  500. CallBackPath
  501. );
  502. BufferSize = strlen( (char *)Buffer );
  503. return true;
  504. };*/
  505. return false;
  506. };
  507. bool protocolDefender::Alarm2D( int Angle, int Range, int Width, int ID ){
  508. bool RetVal= false;
  509. char Buffer[1024];
  510. int BufferSize = 0;
  511. CSTR URL;
  512. CSTR UID;
  513. UID = buildUniqID();
  514. if( UID!= NULL ){
  515. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  516. RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
  517. if( URL != NULL ){
  518. RetVal = issuePost( URL, Buffer, BufferSize );
  519. free( URL );
  520. };
  521. free( UID );
  522. };
  523. return RetVal;
  524. };
  525. bool protocolDefender::Alarm3D( int Angle1, int Angle2, int Range, int ID ){
  526. bool RetVal= false;
  527. char Buffer[1024];
  528. int BufferSize = 0;
  529. CSTR URL;
  530. CSTR UID;
  531. UID = buildUniqID();
  532. if( UID!= NULL ){
  533. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  534. #ifdef DEBUG
  535. LOG(URL);
  536. #endif
  537. RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
  538. if( URL != NULL ){
  539. RetVal = issuePost( URL, Buffer, BufferSize );
  540. free( URL );
  541. };
  542. free( UID );
  543. };
  544. return RetVal;
  545. };
  546. bool protocolDefender::VisibilityChanged( bool Worsened ){
  547. bool RetVal= false;
  548. char Buffer[1024];
  549. int BufferSize = 0;
  550. CSTR URL;
  551. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  552. #ifdef DEBUG
  553. LOG(URL);
  554. #endif
  555. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
  556. if( URL != NULL ){
  557. RetVal = issuePost( URL, Buffer, BufferSize );
  558. free( URL );
  559. };
  560. return RetVal;
  561. };
  562. bool protocolDefender::StatusUpdate( void ){
  563. bool RetVal= false;
  564. char Buffer[1024];
  565. int BufferSize = 0;
  566. CSTR URL;
  567. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  568. #ifdef DEBUG
  569. LOG(URL);
  570. #endif
  571. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
  572. if( URL != NULL ){
  573. RetVal = issuePost( URL, Buffer, BufferSize );
  574. free( URL );
  575. };
  576. return RetVal;
  577. };
  578. bool protocolDefender::ErrorReporting( int lvl, CSTR Descr ){
  579. bool RetVal= false;
  580. char Buffer[1024];
  581. int BufferSize = 0;
  582. CSTR URL;
  583. URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName );
  584. #ifdef DEBUG
  585. LOG(URL);
  586. #endif
  587. RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
  588. if( URL != NULL ){
  589. RetVal = issuePost( URL, Buffer, BufferSize );
  590. free( URL );
  591. };
  592. return RetVal;
  593. };
  594. bool protocolDefender::StartTimeSynch( void ){
  595. bool RetVal= false;
  596. LOG("TimeSYnc");
  597. /*char Buffer[1024];
  598. int BufferSize = 0;
  599. CSTR URL = NULL;
  600. URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
  601. RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
  602. if( URL != NULL ){
  603. RetVal = issuePost( URL, Buffer, BufferSize );
  604. free( URL );
  605. };*/
  606. return RetVal;
  607. };
  608. bool protocolDefender::packetHandler( protocolPackage *Package ){
  609. //LOG("PACKET HANDLER");
  610. if( Package==NULL )return false;
  611. if( Package->m_nSize == 0 ) return false;
  612. if( Package->m_pData == NULL ) return false;
  613. if( Package->m_nCode != 200 ) return false;
  614. try{
  615. XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
  616. if( node1.isEmpty() ){
  617. char BUBU[128];
  618. sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen((CSTR)Package->m_pData), Package->m_nSize );
  619. LOG_ERROR(BUBU);
  620. } else {
  621. LOG( node1.getName() );
  622. if( strstr(node1.getName(),"ClockSync")!=NULL ){
  623. #ifdef __linux__
  624. unsigned long long t0, t1, t2, t3;
  625. #else
  626. unsigned __int64 t0, t1, t2, t3;
  627. #endif
  628. XMLNode node3 = node1.getChildNode("t0", 0);
  629. XMLNode node4 = node1.getChildNode("t1", 0);
  630. XMLNode node5 = node1.getChildNode("t2", 0);
  631. t3 = getSHTimeValue();
  632. #ifdef __linux__
  633. t0 = atoll(node3.getText(0));
  634. t1 = atoll(node4.getText(0));
  635. t2 = atoll(node5.getText(0));
  636. #else
  637. t0 = _atoi64(node3.getText(0));
  638. t1 = _atoi64(node4.getText(0));
  639. t2 = _atoi64(node5.getText(0));
  640. #endif
  641. int offset = ((t1-t0)+(t2-t3))/2;
  642. int latency = (t3-t0)-(t2-t1);
  643. int jagajaga = offset+latency;
  644. //if( 0 & correctSHTime( jagajaga ) ){
  645. // LOG("Time is Synced");
  646. // g_iTimeDiff = 0;
  647. //} else {
  648. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  649. g_iTimeDiff = jagajaga;
  650. //};
  651. };
  652. };
  653. }catch( exception e ){
  654. return false;
  655. };
  656. return true;
  657. };
  658. #undef LOG
  659. #undef LOG_ERROR