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.

747 lines
20 KiB

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