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.

795 lines
23 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolInfoStress.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( "InfoStress", this, X )
  9. #define LOGS( X ) basicLogCollection::getInstance()->Log( "InfoStress", NULL, X )
  10. #define LOG_ERROR( X ) m_pLog->Log( "InfoStress", 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 protocolInfoStress::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 protocolInfoStress::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. protocolInfoStress::protocolInfoStress( config_setting_t *Settings ){
  149. m_pLog = basicLogCollection::getInstance();
  150. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INOSTRESS_CONNECTION");
  151. m_pKafka = NULL;
  152. m_pKafkaConf = NULL;
  153. m_pKafkaBrokers = (CSTR)malloc(CLOG);
  154. if( m_pKafkaBrokers == NULL ) return;
  155. memset( m_pKafkaBrokers, 0 , CLOG);
  156. m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
  157. if( m_pScannerName == NULL ) return;
  158. memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
  159. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  160. if( m_pServerIP == NULL ) return;
  161. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  162. m_pAccessURL = (CSTR)malloc(CTEXT);
  163. if (m_pAccessURL == NULL) return;
  164. memset(m_pAccessURL, 0, CTEXT);
  165. m_bCurrentStatus = false;
  166. m_bMitigationStatus = false;
  167. if( m_pSettings != NULL ){
  168. int intVar;
  169. double floatVar;
  170. config_setting_t *pSetting;
  171. if (Settings != NULL) {
  172. m_bEnabled = true;
  173. } else {
  174. if (!config_setting_lookup_bool(m_pSettings, "ENABLE", &intVar)) {
  175. intVar = m_bEnabled = false;
  176. LOG("PROTOCOL DISABLED[default]");
  177. pSetting = config_setting_add(m_pSettings, "ENABLE", CONFIG_TYPE_BOOL);
  178. config_setting_set_bool(pSetting, intVar);
  179. basicSettingsProvider::getInstance()->compromise();
  180. } else {
  181. m_bEnabled = (bool)intVar;
  182. };
  183. };
  184. if ( m_bEnabled ) {
  185. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LON", &floatVar)) {
  186. m_fLon = floatVar = 35.0938962;
  187. pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT);
  188. config_setting_set_float(pSetting, floatVar);
  189. basicSettingsProvider::getInstance()->compromise();
  190. } else {
  191. m_fLon = floatVar;
  192. };
  193. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar)) {
  194. m_fLat = floatVar = 32.904208;
  195. pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT);
  196. config_setting_set_float(pSetting, floatVar);
  197. basicSettingsProvider::getInstance()->compromise();
  198. } else {
  199. m_fLat = floatVar;
  200. };
  201. if (!config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar)) {
  202. intVar = m_bUseSSL = false;
  203. LOG("PROTOCOL DISABLED[default]");
  204. pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL);
  205. config_setting_set_bool(pSetting, intVar);
  206. basicSettingsProvider::getInstance()->compromise();
  207. } else {
  208. m_bUseSSL = (bool)intVar;
  209. };
  210. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) {
  211. strncpy(m_pScannerName, "LRF_2D_001", 64);
  212. LOG(m_pScannerName);
  213. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  214. config_setting_set_string(pSetting, m_pScannerName);
  215. basicSettingsProvider::getInstance()->compromise();
  216. };
  217. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) {
  218. m_nScannerDimentions = 2;
  219. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  220. config_setting_set_int(pSetting, m_nScannerDimentions);
  221. basicSettingsProvider::getInstance()->compromise();
  222. };
  223. if (!config_setting_lookup_string(m_pSettings, "CEP_IP_ADDRESS", (const char**)&m_pServerIP)) {
  224. strncpy(m_pServerIP, "192.168.200.204", 16);
  225. LOG(m_pServerIP);
  226. pSetting = config_setting_add(m_pSettings, "CEP_IP_ADDRESS", CONFIG_TYPE_STRING);
  227. config_setting_set_string(pSetting, m_pServerIP);
  228. basicSettingsProvider::getInstance()->compromise();
  229. };
  230. LOG(m_pServerIP);
  231. if (!config_setting_lookup_int(m_pSettings, "CEP_IP_PORT", &m_nServerPort)) {
  232. m_nServerPort = 80;
  233. pSetting = config_setting_add(m_pSettings, "CEP_IP_PORT", CONFIG_TYPE_INT);
  234. config_setting_set_int(pSetting, m_nServerPort);
  235. basicSettingsProvider::getInstance()->compromise();
  236. };
  237. if (!config_setting_lookup_string(m_pSettings, "ACCESS_URL", (const char**)&m_pAccessURL)) {
  238. strncpy( m_pAccessURL, DFSL_DEFENDER_DEFAULT_URL, CTEXT );
  239. pSetting = config_setting_add(m_pSettings, "ACCESS_URL", CONFIG_TYPE_STRING);
  240. config_setting_set_string(pSetting, m_pAccessURL);
  241. basicSettingsProvider::getInstance()->compromise();
  242. };
  243. LOG(m_pAccessURL);
  244. };
  245. basicSettingsProvider::getInstance()->validate();
  246. };
  247. };
  248. /**
  249. * @brief Message delivery report callback.
  250. *
  251. * This callback is called exactly once per message, indicating if
  252. * the message was succesfully delivered
  253. * (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
  254. * failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
  255. *
  256. * The callback is triggered from rd_kafka_poll() and executes on
  257. * the application's thread.
  258. */
  259. static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){
  260. if (rkmessage->err)
  261. fprintf(stderr, "%% Message delivery failed: %s\n",
  262. rd_kafka_err2str(rkmessage->err));
  263. else
  264. fprintf(stderr,
  265. "%% Message delivered (%zd bytes, "
  266. "partition %"PRId32")\n",
  267. rkmessage->len, rkmessage->partition);
  268. /* The rkmessage is destroyed automatically by librdkafka */
  269. };
  270. protocolInfoStress::~protocolInfoStress(){
  271. if(m_pKafka!=NULL){
  272. /* Wait for final messages to be delivered or fail.
  273. * rd_kafka_flush() is an abstraction over rd_kafka_poll() which
  274. * waits for all messages to be delivered. */
  275. LOG("Flushing final messages..");
  276. rd_kafka_flush( m_pKafka, 3000 );
  277. /* If the output queue is still not empty there is an issue
  278. * with producing messages to the clusters. */
  279. if( rd_kafka_outq_len( m_pKafka ) > 0 ) LOG_ERROR("Some messages are not delivered!");
  280. rd_kafka_destroy(m_pKafka);
  281. };
  282. if(m_pKafkaConf!=NULL)rd_kafka_destroy(m_pKafkaConf);
  283. if(m_pScannerName!=NULL)free(m_pScannerName);
  284. if(m_pServerIP!=NULL)free(m_pServerIP);
  285. if( m_pAccessURL != NULL)free(m_pAccessURL);
  286. };
  287. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
  288. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp
  289. bool protocolInfoStress::init(){
  290. char tBuffer[CLOG];
  291. if( strlen( m_pKafkaBrokers ) == 0 ) {
  292. LOG_ERROR("No Kafka Brokers are defined!");
  293. return false;
  294. };
  295. m_pKafkaConf = rd_kafka_conf_new();
  296. if( m_pKafkaConf == NULL ) {
  297. LOG_ERROR("Eror Creating Kafka Conf!");
  298. return false;
  299. };
  300. if( rd_kafka_conf_set( m_pKafkaConf, "bootstrap.servers", m_pKafkaBrokers, tBuffer, CLOG ) != RD_KAFKA_CONF_OK ){
  301. LOG_ERROR( tBuffer );
  302. return false;
  303. };
  304. /* Set the delivery report callback.
  305. * This callback will be called once per message to inform
  306. * the application if delivery succeeded or failed.
  307. * See dr_msg_cb() above.
  308. * The callback is only triggered from rd_kafka_poll() and
  309. * rd_kafka_flush(). */
  310. rd_kafka_conf_set_dr_msg_cb( m_pKafkaConf, dr_msg_cb );
  311. /*
  312. * Create producer instance.
  313. *
  314. * NOTE: rd_kafka_new() takes ownership of the conf object
  315. * and the application must not reference it again after
  316. * this call.
  317. */
  318. m_pKafka = rd_kafka_new( RD_KAFKA_PRODUCER, m_pKafkaConf, tBuffer, CLOG );
  319. if( m_pKafka == NULL ){
  320. LOG_ERROR( tBuffer );
  321. return false;
  322. };
  323. return true;
  324. };
  325. void protocolInfoStress::setScannerName( CSTR Name ){
  326. sprintf( m_pScannerName, "%s", Name );
  327. };
  328. void protocolInfoStress::setProtocolType( bool DeviceIs3D ){
  329. if( DeviceIs3D ) {
  330. m_nScannerDimentions = 3;
  331. } else {
  332. m_nScannerDimentions = 2;
  333. };
  334. };
  335. CSTR protocolInfoStress::assembleStatusData( bool StatusOK ){
  336. CSTR RetVal = NULL;
  337. CSTR TIME_STAMP;
  338. CSTR tSRC;
  339. CSTR tTARGET;
  340. CSTR tID;
  341. RetVal = (CSTR) malloc(CLOG);
  342. if ( RetVal != NULL ) {
  343. TIME_STAMP = getSHDTimeStamp();
  344. if( TIME_STAMP != NULL ) {
  345. tTARGET = (CSTR) malloc(128);
  346. if( tTARGET!= NULL) {
  347. tSRC = (CSTR) malloc(128);
  348. if( tSRC ) {
  349. tID = (CSTR) malloc(64);
  350. if( tID != NULL ) {
  351. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  352. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  353. sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] );
  354. sprintf( RetVal,
  355. PACKAGE_SE_STATUS_DATA_ELEMENT,
  356. defenderTypeDescription[DC_STATUS],
  357. TIME_STAMP,
  358. tSRC,
  359. "DFSL_LIDAR_SENSOR_STATUS",
  360. DFSL_DATA_TYPE_STATUS,
  361. tID,
  362. tTARGET
  363. );
  364. free( tID );
  365. free( tSRC );
  366. free( tTARGET );
  367. free( TIME_STAMP );
  368. } else {
  369. free( tSRC );
  370. free( tTARGET );
  371. free( TIME_STAMP );
  372. free( RetVal );
  373. }
  374. } else {
  375. free( tTARGET );
  376. free( TIME_STAMP );
  377. free( RetVal );
  378. };
  379. } else {
  380. free( TIME_STAMP );
  381. free( RetVal );
  382. };
  383. } else {
  384. free( RetVal );
  385. RetVal = NULL;
  386. };
  387. };
  388. return RetVal;
  389. };
  390. CSTR protocolInfoStress::assembleFaultData( uint8_t Code, CSTR Desc ){
  391. CSTR RetVal;
  392. CSTR TIME_STAMP;
  393. CSTR tSRC;
  394. CSTR tTARGET;
  395. CSTR tID;
  396. RetVal = (CSTR) malloc(CLOG);
  397. if ( RetVal != NULL ) {
  398. TIME_STAMP = getSHDTimeStamp();
  399. if( TIME_STAMP != NULL ) {
  400. tTARGET = (CSTR) malloc(128);
  401. if( tTARGET!= NULL) {
  402. tSRC = (CSTR) malloc(128);
  403. if( tSRC ) {
  404. tID = (CSTR) malloc(64);
  405. if( tID != NULL ) {
  406. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  407. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  408. sprintf( tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc );
  409. sprintf( RetVal,
  410. PACKAGE_SE_STATUS_DATA_ELEMENT,
  411. Desc,
  412. TIME_STAMP,
  413. tSRC,
  414. "DFSL_LIDARE_SENSOR_FAULT_DATA",
  415. DFSL_DATA_TYPE_ERROR,
  416. tID,
  417. tTARGET
  418. );
  419. free( tID );
  420. free( tSRC );
  421. free( tTARGET );
  422. free( TIME_STAMP );
  423. } else {
  424. free( tSRC );
  425. free( tTARGET );
  426. free( TIME_STAMP );
  427. free( RetVal );
  428. }
  429. } else {
  430. free( tTARGET );
  431. free( TIME_STAMP );
  432. free( RetVal );
  433. };
  434. } else {
  435. free( TIME_STAMP );
  436. free( RetVal );
  437. };
  438. } else {
  439. free( RetVal );
  440. RetVal = NULL;
  441. };
  442. };
  443. return RetVal;
  444. };
  445. CSTR protocolInfoStress::assembleTargetData( int targetID, int Range, int Width, int Azimuth, int Elevation ){
  446. CSTR RetVal;
  447. CSTR TIME_STAMP;
  448. CSTR tSRC;
  449. CSTR tTARGET;
  450. CSTR tID;
  451. RetVal = (CSTR) malloc(CLOG);
  452. if ( RetVal != NULL ) {
  453. TIME_STAMP = getSHDTimeStamp();
  454. if( TIME_STAMP != NULL ) {
  455. tTARGET = (CSTR) malloc(128);
  456. if( tTARGET!= NULL) {
  457. tSRC = (CSTR) malloc(128);
  458. if( tSRC ) {
  459. tID = (CSTR) malloc(64);
  460. if( tID != NULL ) {
  461. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  462. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  463. if( m_nScannerDimentions > 2 ) {
  464. sprintf( tTARGET, DFSL_3D_TARGET_DTO, Azimuth, Elevation, Range );
  465. } else {
  466. sprintf( tTARGET, DFSL_2D_TARGET_DTO, Azimuth, Range, Width );
  467. };
  468. sprintf( RetVal,
  469. PACKAGE_SE_TARGET_DATA_ELEMENT,
  470. defenderTypeDescription[DC_ALARM],
  471. TIME_STAMP,
  472. tSRC,
  473. "DFSL_TARGET_DATA",
  474. DFSL_DATA_TYPE_TARGET,
  475. tID,
  476. tTARGET
  477. );
  478. free( tID );
  479. free( tSRC );
  480. free( tTARGET );
  481. free( TIME_STAMP );
  482. } else {
  483. free( tSRC );
  484. free( tTARGET );
  485. free( TIME_STAMP );
  486. free( RetVal );
  487. }
  488. } else {
  489. free( tTARGET );
  490. free( TIME_STAMP );
  491. free( RetVal );
  492. };
  493. } else {
  494. free( TIME_STAMP );
  495. free( RetVal );
  496. };
  497. } else {
  498. free( RetVal );
  499. RetVal = NULL;
  500. };
  501. };
  502. return RetVal;
  503. };
  504. static uint32_t DEFENDER_MESSAGE_ID = 0;
  505. size_t protocolInfoStress::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){
  506. char tBuffer[CLOG];
  507. size_t RetVal;
  508. CSTR tBuf;
  509. RetVal = 0;
  510. tBuf = getSHDTimeStamp();
  511. DEFENDER_MESSAGE_ID++;
  512. //memset( tBuffer, 0, CLOG );
  513. if( tBuf != NULL ){
  514. //char ID[64];
  515. switch ( Type ) {
  516. case DC_STATUS:
  517. Severity = 0;
  518. break;
  519. case DC_ERROR:
  520. Severity = 2;
  521. break;
  522. case DC_ALARM:
  523. Severity = 4;
  524. break;
  525. default:
  526. Severity = 2;
  527. };
  528. //sprintf( ID, "%s", MD5String( tBuf ) );
  529. if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D;
  530. sprintf( tBuffer,
  531. DFSL_SE_ENVELOPE,
  532. //DEFENDER_MESSAGE_ID,
  533. defenderCategories[Type],
  534. defenderTypeSeverity[Severity],
  535. tBuf, tBuf,
  536. Location,
  537. Data
  538. );
  539. RetVal = strlen(tBuffer);
  540. memcpy( Buffer, tBuffer, RetVal );
  541. Buffer[RetVal] = (char) 0;
  542. free( tBuf );
  543. };
  544. return RetVal;
  545. };
  546. bool protocolInfoStress::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
  547. if( Buffer!= NULL ){
  548. CSTR tBuf;
  549. tBuf = assembleStatusData( Status );
  550. #ifdef DEBUG
  551. //LOG(tBuf);
  552. #endif
  553. if( tBuf != NULL ) {
  554. char DFSL_LOCATION[256];
  555. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  556. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf );
  557. #ifdef DEBUG
  558. LOG((CSTR)Buffer);
  559. #endif
  560. free( tBuf );
  561. return true;
  562. }
  563. };
  564. return false;
  565. };
  566. bool protocolInfoStress::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
  567. if( Buffer!= NULL ){
  568. CSTR tBuf;
  569. tBuf = assembleFaultData( Type, Description );
  570. if( tBuf != NULL ){
  571. char DFSL_LOCATION[256];
  572. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  573. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf );
  574. free( tBuf );
  575. return true;
  576. };
  577. };
  578. return false;
  579. };
  580. bool protocolInfoStress::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){
  581. if( Buffer!= NULL ){
  582. CSTR tBuf;
  583. tBuf = assembleTargetData( targetID, Range, Width, Angle, 0 );
  584. if( tBuf != NULL ){
  585. char DFSL_LOCATION[256];
  586. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  587. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf );
  588. free( tBuf );
  589. return true;
  590. };
  591. };
  592. return false;
  593. };
  594. bool protocolInfoStress::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){
  595. if( Buffer!= NULL ){
  596. CSTR tBuf;
  597. tBuf = assembleTargetData( targetID, Range, 0, Angle1, Angle2 );
  598. #ifdef DEBUG
  599. //LOG(tBuf);
  600. #endif
  601. if( tBuf != NULL ){
  602. char DFSL_LOCATION[256];
  603. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  604. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf );
  605. #ifdef DEBUG
  606. LOG((CSTR)Buffer);
  607. #endif
  608. free( tBuf );
  609. return true;
  610. };
  611. };
  612. return false;
  613. };
  614. bool protocolInfoStress::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
  615. /*if( Buffer!= NULL ){
  616. CSTR tBuf;
  617. tBuf = getSHTimeStamp();
  618. if( tBuf != NULL ){
  619. sprintf(
  620. ((char *)(Buffer+BufferSize)),
  621. PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
  622. tBuf
  623. );
  624. BufferSize = strlen( (char *)Buffer );
  625. free( tBuf );
  626. return true;
  627. };
  628. };*/
  629. return false;
  630. };
  631. bool protocolInfoStress::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
  632. /*if( Buffer!= NULL ){
  633. sprintf(
  634. ((char *)(Buffer+BufferSize)),
  635. PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
  636. CallBackPath
  637. );
  638. BufferSize = strlen( (char *)Buffer );
  639. return true;
  640. };*/
  641. return false;
  642. };
  643. bool protocolInfoStress::Alarm2D( float Angle, float Range, float Width, int ID ){
  644. bool RetVal= false;
  645. char Buffer[1024];
  646. int BufferSize = 0;
  647. CSTR URL;
  648. CSTR UID;
  649. if(!m_bEnabled) return false;
  650. UID = buildUniqID();
  651. if( UID!= NULL ){
  652. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  653. #ifdef DEBUG
  654. LOG(URL);
  655. #endif
  656. RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
  657. if( URL != NULL ){
  658. RetVal = issuePost( URL, Buffer, BufferSize );
  659. free( URL );
  660. };
  661. free( UID );
  662. };
  663. return RetVal;
  664. };
  665. bool protocolInfoStress::Alarm3D( float Angle1, float Angle2, float Range, int ID ){
  666. bool RetVal= false;
  667. char Buffer[1024];
  668. int BufferSize = 0;
  669. CSTR URL;
  670. CSTR UID;
  671. if(!m_bEnabled) return false;
  672. UID = buildUniqID();
  673. if( UID!= NULL ){
  674. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  675. #ifdef DEBUG
  676. LOG(URL);
  677. #endif
  678. RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
  679. if( URL != NULL ){
  680. RetVal = issuePost( URL, Buffer, BufferSize );
  681. free( URL );
  682. };
  683. free( UID );
  684. };
  685. return RetVal;
  686. };
  687. bool protocolInfoStress::VisibilityChanged( bool Worsened ){
  688. bool RetVal= false;
  689. char Buffer[1024];
  690. int BufferSize = 0;
  691. CSTR URL;
  692. if(!m_bEnabled) return false;
  693. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  694. #ifdef DEBUG
  695. LOG(URL);
  696. #endif
  697. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
  698. if( URL != NULL ){
  699. RetVal = issuePost( URL, Buffer, BufferSize );
  700. free( URL );
  701. };
  702. return RetVal;
  703. };
  704. bool protocolInfoStress::StatusUpdate( void ){
  705. bool RetVal= false;
  706. char Buffer[1024];
  707. int BufferSize = 0;
  708. CSTR URL;
  709. if(!m_bEnabled) return false;
  710. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  711. #ifdef DEBUG
  712. LOG(URL);
  713. #endif
  714. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
  715. if( URL != NULL ){
  716. RetVal = issuePost( URL, Buffer, BufferSize );
  717. free( URL );
  718. };
  719. return RetVal;
  720. };
  721. bool protocolInfoStress::ErrorReporting( int lvl, CSTR Descr ){
  722. bool RetVal= false;
  723. char Buffer[1024];
  724. int BufferSize = 0;
  725. CSTR URL;
  726. if(!m_bEnabled) return false;
  727. URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName );
  728. #ifdef DEBUG
  729. LOG(URL);
  730. #endif
  731. RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
  732. if( URL != NULL ){
  733. RetVal = issuePost( URL, Buffer, BufferSize );
  734. free( URL );
  735. };
  736. return RetVal;
  737. };
  738. bool protocolInfoStress::StartTimeSynch( void ){
  739. bool RetVal= false;
  740. //LOG("TimeSYnc");
  741. /*char Buffer[1024];
  742. int BufferSize = 0;
  743. CSTR URL = NULL;
  744. URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
  745. RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
  746. if( URL != NULL ){
  747. RetVal = issuePost( URL, Buffer, BufferSize );
  748. free( URL );
  749. };*/
  750. return RetVal;
  751. };
  752. #undef LOG
  753. #undef LOG_ERROR