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.

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