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.

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