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.

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