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.

499 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolSafeShore.h"
  2. #include "../../alg/generic/md5.h"
  3. #define LOG(X) m_pLog->Log("SafeShore", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("SafeShore", this, _LOG_ERROR, X)
  5. CSTR systemStatusMode[] = {"Operational", "Degraded"};
  6. CSTR systemFaultCode[] = {"Critical", "Warning", "Info"};
  7. CSTR getSHTimeStamp(void)
  8. {
  9. CSTR RetVal;
  10. #ifdef __linux__
  11. RetVal = (CSTR) malloc(64);
  12. if (RetVal != NULL)
  13. {
  14. sprintf(RetVal, "%llu", getTimeValue());
  15. };
  16. #else
  17. RetVal = (CSTR) malloc(64);
  18. if (RetVal != NULL)
  19. {
  20. sprintf(RetVal, "%I64d", getTimeValue());
  21. };
  22. #endif
  23. return RetVal;
  24. }
  25. CSTR protocolSafeShore::buildURL(CSTR templ, int dim, CSTR devID)
  26. {
  27. CSTR RetVal;
  28. char tpl[256];
  29. RetVal = (CSTR) malloc(256);
  30. sprintf(tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ);
  31. sprintf(RetVal, tpl, dim, devID);
  32. return RetVal;
  33. }
  34. CSTR protocolSafeShore::buildUniqID(void)
  35. {
  36. CSTR RetVal;
  37. RetVal = (CSTR) malloc(64);
  38. sprintf(RetVal, "%s", MD5String(getSHTimeStamp()));
  39. return RetVal;
  40. }
  41. /* Default Constructor */
  42. protocolSafeShore::protocolSafeShore(config_setting_t * Settings) :
  43. protocolHTTP(basicSettingsProvider::getInstance()->getSubNode(Settings, "SAFESHORE_CONNECTION"))
  44. {
  45. m_pLog = basicLogCollection::getInstance();
  46. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "SAFESHORE_CONNECTION");
  47. m_pScannerName = (CSTR) malloc(OS_SCANNER_NAME_SIZE);
  48. if (m_pScannerName == NULL)
  49. {
  50. return;
  51. }
  52. memset(m_pScannerName, 0, OS_SCANNER_NAME_SIZE);
  53. m_pServerIP = (CSTR) malloc(OS_LCU_ADDRESS_SIZE);
  54. if (m_pServerIP == NULL)
  55. {
  56. return;
  57. }
  58. memset(m_pServerIP, 0, OS_LCU_ADDRESS_SIZE);
  59. m_bCurrentStatus = false;
  60. if (m_pSettings != NULL)
  61. {
  62. int intVar;
  63. config_setting_t * pSetting;
  64. if (Settings != NULL)
  65. {
  66. m_bEnabled = true;
  67. }
  68. else
  69. {
  70. if (! config_setting_lookup_bool(m_pSettings, "ENABLE", &intVar))
  71. {
  72. intVar = m_bEnabled = false;
  73. LOG("PROTOCOL DISABLED[default]");
  74. pSetting = config_setting_add(m_pSettings, "ENABLE", CONFIG_TYPE_BOOL);
  75. config_setting_set_bool(pSetting, intVar);
  76. basicSettingsProvider::getInstance()->compromise();
  77. }
  78. else
  79. {
  80. m_bEnabled = (bool) intVar;
  81. };
  82. };
  83. if (m_bEnabled)
  84. {
  85. if (! config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char **) &m_pScannerName))
  86. {
  87. strncpy(m_pScannerName, "LRF_2D_001", 64);
  88. LOG(m_pScannerName);
  89. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  90. config_setting_set_string(pSetting, m_pScannerName);
  91. basicSettingsProvider::getInstance()->compromise();
  92. };
  93. if (! config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions))
  94. {
  95. m_nScannerDimentions = 2;
  96. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  97. config_setting_set_int(pSetting, m_nScannerDimentions);
  98. basicSettingsProvider::getInstance()->compromise();
  99. };
  100. if (! config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char **) &m_pServerIP))
  101. {
  102. strncpy(m_pServerIP, "192.168.200.204", 16);
  103. LOG(m_pServerIP);
  104. pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
  105. config_setting_set_string(pSetting, m_pServerIP);
  106. basicSettingsProvider::getInstance()->compromise();
  107. };
  108. if (! config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort))
  109. {
  110. m_nServerPort = 80;
  111. pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
  112. config_setting_set_int(pSetting, m_nServerPort);
  113. basicSettingsProvider::getInstance()->compromise();
  114. };
  115. };
  116. basicSettingsProvider::getInstance()->validate();
  117. };
  118. }
  119. /*
  120. 1.2 GHz
  121. Number of Cores
  122. Quad-Core
  123. Type
  124. Tegra 3
  125. Processor Number
  126. T30SL
  127. Manufacturer
  128. NVIDIA
  129. Verify that you have the following installed on your system
  130. Ubuntu 16.04 LTS (with GCC 5.4)
  131. NVIDIA Display Driver ver. 375
  132. NVIDIA VideoSDK 7.1
  133. cuDNN 6 & TensorRT 2.1
  134. NVIDIA CUDA 8.0
  135. Download the DeepStream 1.0 tar file (below)
  136. Extract the package at the location of your choice
  137. Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
  138. */
  139. protocolSafeShore::~protocolSafeShore()
  140. {
  141. if (m_pScannerName != NULL)
  142. {
  143. free(m_pScannerName);
  144. }
  145. if (m_pServerIP != NULL)
  146. {
  147. free(m_pServerIP);
  148. }
  149. }
  150. void protocolSafeShore::setScannerName(CSTR Name)
  151. {
  152. sprintf(m_pScannerName, "%s", Name);
  153. }
  154. void protocolSafeShore::setProtocolType(bool DeviceIs3D)
  155. {
  156. if (DeviceIs3D)
  157. {
  158. m_nScannerDimentions = 3;
  159. }
  160. else
  161. {
  162. m_nScannerDimentions = 2;
  163. };
  164. }
  165. bool protocolSafeShore::buildStatusPackage(unsigned char * Buffer, int & BufferSize, bool Status)
  166. {
  167. if (Buffer != NULL)
  168. {
  169. CSTR tBuf;
  170. tBuf = getSHTimeStamp();
  171. if (tBuf != NULL)
  172. {
  173. if (Status)
  174. {
  175. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[0], tBuf);
  176. }
  177. else
  178. {
  179. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[1], tBuf);
  180. };
  181. BufferSize = strlen((char *) Buffer);
  182. free(tBuf);
  183. return true;
  184. };
  185. };
  186. return false;
  187. }
  188. bool protocolSafeShore::buildFaultPackage(unsigned char * Buffer, int & BufferSize, unsigned char Type, CSTR Description)
  189. {
  190. if (Buffer != NULL)
  191. {
  192. CSTR tBuf;
  193. tBuf = getSHTimeStamp();
  194. if (tBuf != NULL)
  195. {
  196. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_FAULT, systemFaultCode[Type], tBuf, Description);
  197. BufferSize = strlen((char *) Buffer);
  198. free(tBuf);
  199. return true;
  200. };
  201. };
  202. return false;
  203. }
  204. bool protocolSafeShore::buildEvent2DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width)
  205. {
  206. if (Buffer != NULL)
  207. {
  208. CSTR tBuf;
  209. tBuf = getSHTimeStamp();
  210. if (tBuf != NULL)
  211. {
  212. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT2D, msgUID, tBuf, targetID, Angle, Range, Width);
  213. BufferSize = strlen((char *) Buffer);
  214. free(tBuf);
  215. return true;
  216. };
  217. };
  218. return false;
  219. }
  220. bool protocolSafeShore::buildEvent3DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range)
  221. {
  222. if (Buffer != NULL)
  223. {
  224. CSTR tBuf;
  225. tBuf = getSHTimeStamp();
  226. if (tBuf != NULL)
  227. {
  228. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT3D, msgUID, tBuf, targetID, Angle1, Angle2, Range);
  229. BufferSize = strlen((char *) Buffer);
  230. free(tBuf);
  231. return true;
  232. };
  233. };
  234. return false;
  235. }
  236. bool protocolSafeShore::buildClockSyncPackage(unsigned char * Buffer, int & BufferSize)
  237. {
  238. if (Buffer != NULL)
  239. {
  240. CSTR tBuf;
  241. tBuf = getSHTimeStamp();
  242. if (tBuf != NULL)
  243. {
  244. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, tBuf);
  245. BufferSize = strlen((char *) Buffer);
  246. free(tBuf);
  247. return true;
  248. };
  249. };
  250. return false;
  251. }
  252. bool protocolSafeShore::buildCallBackPackage(unsigned char * Buffer, int & BufferSize, CSTR CallBackPath)
  253. {
  254. if (Buffer != NULL)
  255. {
  256. sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, CallBackPath);
  257. BufferSize = strlen((char *) Buffer);
  258. return true;
  259. };
  260. return false;
  261. }
  262. bool protocolSafeShore::Alarm2D(int Angle, int Range, int Width, int ID)
  263. {
  264. bool RetVal = false;
  265. char Buffer[1024];
  266. int BufferSize = 0;
  267. CSTR URL;
  268. CSTR UID;
  269. if (! m_bEnabled)
  270. {
  271. return false;
  272. }
  273. UID = buildUniqID();
  274. if (UID != NULL)
  275. {
  276. URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
  277. RetVal = buildEvent2DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width);
  278. if (URL != NULL)
  279. {
  280. RetVal = issuePost(URL, Buffer, BufferSize);
  281. free(URL);
  282. };
  283. free(UID);
  284. };
  285. return RetVal;
  286. }
  287. bool protocolSafeShore::Alarm3D(int Angle1, int Angle2, int Range, int ID)
  288. {
  289. bool RetVal = false;
  290. char Buffer[1024];
  291. int BufferSize = 0;
  292. CSTR URL;
  293. CSTR UID;
  294. if (! m_bEnabled)
  295. {
  296. return false;
  297. }
  298. UID = buildUniqID();
  299. if (UID != NULL)
  300. {
  301. URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
  302. LOG(URL);
  303. RetVal = buildEvent3DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range);
  304. if (URL != NULL)
  305. {
  306. RetVal = issuePost(URL, Buffer, BufferSize);
  307. free(URL);
  308. };
  309. free(UID);
  310. };
  311. return RetVal;
  312. }
  313. bool protocolSafeShore::VisibilityChanged(bool Worsened)
  314. {
  315. bool RetVal = false;
  316. char Buffer[1024];
  317. int BufferSize = 0;
  318. CSTR URL;
  319. URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
  320. RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, ! Worsened);
  321. if (URL != NULL)
  322. {
  323. RetVal = issuePost(URL, Buffer, BufferSize);
  324. free(URL);
  325. };
  326. return RetVal;
  327. }
  328. bool protocolSafeShore::StatusUpdate(void)
  329. {
  330. bool RetVal = false;
  331. char Buffer[1024];
  332. int BufferSize = 0;
  333. CSTR URL;
  334. if (! m_bEnabled)
  335. {
  336. return false;
  337. }
  338. URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
  339. RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, m_bCurrentStatus);
  340. if (URL != NULL)
  341. {
  342. RetVal = issuePost(URL, Buffer, BufferSize);
  343. free(URL);
  344. };
  345. return RetVal;
  346. }
  347. bool protocolSafeShore::ErrorReporting(int lvl, CSTR Descr)
  348. {
  349. bool RetVal = false;
  350. char Buffer[1024];
  351. int BufferSize = 0;
  352. CSTR URL;
  353. if (! m_bEnabled)
  354. {
  355. return false;
  356. }
  357. URL = buildURL(SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName);
  358. RetVal = buildFaultPackage((unsigned char *) Buffer, BufferSize, lvl, Descr);
  359. if (URL != NULL)
  360. {
  361. RetVal = issuePost(URL, Buffer, BufferSize);
  362. free(URL);
  363. };
  364. return RetVal;
  365. }
  366. bool protocolSafeShore::StartTimeSynch(void)
  367. {
  368. bool RetVal = false;
  369. char Buffer[1024];
  370. int BufferSize = 0;
  371. CSTR URL = NULL;
  372. if (! m_bEnabled)
  373. {
  374. return false;
  375. }
  376. URL = buildURL(SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName);
  377. RetVal = buildClockSyncPackage((unsigned char *) Buffer, BufferSize);
  378. if (URL != NULL)
  379. {
  380. RetVal = issuePost(URL, Buffer, BufferSize);
  381. free(URL);
  382. };
  383. return RetVal;
  384. }
  385. bool protocolSafeShore::packetHandler(protocolPackage * Package)
  386. {
  387. // LOG("PACKET HANDLER");
  388. if (! m_bEnabled)
  389. {
  390. return false;
  391. }
  392. if (Package == NULL)
  393. {
  394. return false;
  395. }
  396. if (Package->m_nSize == 0)
  397. {
  398. return false;
  399. }
  400. if (Package->m_pData == NULL)
  401. {
  402. return false;
  403. }
  404. if (Package->m_nCode != 200)
  405. {
  406. return false;
  407. }
  408. try
  409. {
  410. XMLNode node1 = XMLNode::parseString((const char *) Package->m_pData, "root");
  411. if (node1.isEmpty())
  412. {
  413. // char BUBU[128];
  414. // sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen(pPackage), Package->m_nSize );
  415. // LOG_ERROR(BUBU);
  416. }
  417. else
  418. {
  419. LOG(node1.getName());
  420. if (strstr(node1.getName(), "ClockSync") != NULL)
  421. {
  422. #ifdef __linux__
  423. unsigned long long t0, t1, t2, t3;
  424. #else
  425. unsigned __int64 t0, t1, t2, t3;
  426. #endif
  427. XMLNode node3 = node1.getChildNode("t0", 0);
  428. XMLNode node4 = node1.getChildNode("t1", 0);
  429. XMLNode node5 = node1.getChildNode("t2", 0);
  430. t3 = getTimeValue();
  431. #ifdef __linux__
  432. t0 = atoll(node3.getText(0));
  433. t1 = atoll(node4.getText(0));
  434. t2 = atoll(node5.getText(0));
  435. #else
  436. t0 = _atoi64(node3.getText(0));
  437. t1 = _atoi64(node4.getText(0));
  438. t2 = _atoi64(node5.getText(0));
  439. #endif
  440. int offset = ((t1 - t0) + (t2 - t3)) / 2;
  441. int latency = (t3 - t0) - (t2 - t1);
  442. int jagajaga = offset + latency;
  443. // if( 0 & correctSHTime( jagajaga ) ){
  444. // LOG("Time is Synced");
  445. // g_iTimeDiff = 0;
  446. // } else {
  447. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  448. correctSoftTime(jagajaga);
  449. //};
  450. };
  451. };
  452. }
  453. catch (exception e)
  454. {
  455. };
  456. return true;
  457. }
  458. #undef LOG
  459. #undef LOG_ERROR