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.

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