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.

884 lines
22 KiB

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