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.

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