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.

503 lines
14 KiB

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