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.

483 lines
13 KiB

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. config_setting_t *pSetting;
  121. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
  122. strncpy( m_pScannerName, "LRF_2D_001", 64 );
  123. LOG( m_pScannerName );
  124. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  125. config_setting_set_string(pSetting, m_pScannerName);
  126. basicSettingsProvider::getInstance()->compromise();
  127. };
  128. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
  129. m_nScannerDimentions = 2;
  130. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  131. config_setting_set_int(pSetting, m_nScannerDimentions);
  132. basicSettingsProvider::getInstance()->compromise();
  133. };
  134. if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)){
  135. strncpy( m_pServerIP, "192.168.200.204", 16 );
  136. LOG( m_pServerIP );
  137. pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
  138. config_setting_set_string(pSetting, m_pServerIP);
  139. basicSettingsProvider::getInstance()->compromise();
  140. };
  141. if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)){
  142. m_nServerPort = 80;
  143. pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
  144. config_setting_set_int(pSetting, m_nServerPort);
  145. basicSettingsProvider::getInstance()->compromise();
  146. };
  147. basicSettingsProvider::getInstance()->validate();
  148. };
  149. };
  150. /*
  151. 1.2 GHz
  152. Number of Cores
  153. Quad-Core
  154. Type
  155. Tegra 3
  156. Processor Number
  157. T30SL
  158. Manufacturer
  159. NVIDIA
  160. Verify that you have the following installed on your system
  161. Ubuntu 16.04 LTS (with GCC 5.4)
  162. NVIDIA Display Driver ver. 375
  163. NVIDIA VideoSDK 7.1
  164. cuDNN 6 & TensorRT 2.1
  165. NVIDIA CUDA 8.0
  166. Download the DeepStream 1.0 tar file (below)
  167. Extract the package at the location of your choice
  168. Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
  169. */
  170. protocolSafeShore::~protocolSafeShore(){
  171. if(m_pScannerName!=NULL)free(m_pScannerName);
  172. if(m_pServerIP!=NULL)free(m_pServerIP);
  173. };
  174. void protocolSafeShore::setScannerName( CSTR Name ){
  175. sprintf( m_pScannerName, "%s", Name );
  176. };
  177. void protocolSafeShore::setProtocolType( bool DeviceIs3D ){
  178. if( DeviceIs3D ) {
  179. m_nScannerDimentions = 3;
  180. } else {
  181. m_nScannerDimentions = 2;
  182. };
  183. };
  184. bool protocolSafeShore::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
  185. if( Buffer!= NULL ){
  186. CSTR tBuf;
  187. tBuf = getSHTimeStamp();
  188. if( tBuf != NULL ){
  189. if( Status ){
  190. sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[0], tBuf );
  191. } else {
  192. sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[1], tBuf );
  193. };
  194. BufferSize = strlen( (char *)Buffer );
  195. free( tBuf );
  196. return true;
  197. };
  198. };
  199. return false;
  200. };
  201. bool protocolSafeShore::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
  202. if( Buffer!= NULL ){
  203. CSTR tBuf;
  204. tBuf = getSHTimeStamp();
  205. if( tBuf != NULL ){
  206. sprintf(
  207. ((char *)(Buffer+BufferSize)),
  208. PACKAGE_ENVELOPE_TEMPLATE_FAULT,
  209. systemFaultCode[Type],
  210. tBuf,
  211. Description
  212. );
  213. BufferSize = strlen( (char *)Buffer );
  214. free( tBuf );
  215. return true;
  216. };
  217. };
  218. return false;
  219. };
  220. bool protocolSafeShore::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width){
  221. if( Buffer!= NULL ){
  222. CSTR tBuf;
  223. tBuf = getSHTimeStamp();
  224. if( tBuf != NULL ){
  225. sprintf(
  226. ((char *)(Buffer+BufferSize)),
  227. PACKAGE_ENVELOPE_TEMPLATE_EVT2D,
  228. msgUID,
  229. tBuf,
  230. targetID,
  231. Angle,
  232. Range,
  233. Width
  234. );
  235. BufferSize = strlen( (char *)Buffer );
  236. free( tBuf );
  237. return true;
  238. };
  239. };
  240. return false;
  241. };
  242. bool protocolSafeShore::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range ){
  243. if( Buffer!= NULL ){
  244. CSTR tBuf;
  245. tBuf = getSHTimeStamp();
  246. if( tBuf != NULL ){
  247. sprintf(
  248. ((char *)(Buffer+BufferSize)),
  249. PACKAGE_ENVELOPE_TEMPLATE_EVT3D,
  250. msgUID,
  251. tBuf,
  252. targetID,
  253. Angle1,
  254. Angle2,
  255. Range
  256. );
  257. BufferSize = strlen( (char *)Buffer );
  258. free( tBuf );
  259. return true;
  260. };
  261. };
  262. return false;
  263. };
  264. bool protocolSafeShore::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
  265. if( Buffer!= NULL ){
  266. CSTR tBuf;
  267. tBuf = getSHTimeStamp();
  268. if( tBuf != NULL ){
  269. sprintf(
  270. ((char *)(Buffer+BufferSize)),
  271. PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
  272. tBuf
  273. );
  274. BufferSize = strlen( (char *)Buffer );
  275. free( tBuf );
  276. return true;
  277. };
  278. };
  279. return false;
  280. };
  281. bool protocolSafeShore::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
  282. if( Buffer!= NULL ){
  283. sprintf(
  284. ((char *)(Buffer+BufferSize)),
  285. PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
  286. CallBackPath
  287. );
  288. BufferSize = strlen( (char *)Buffer );
  289. return true;
  290. };
  291. return false;
  292. };
  293. bool protocolSafeShore::Alarm2D( int Angle, int Range, int Width, int ID ){
  294. bool RetVal= false;
  295. char Buffer[1024];
  296. int BufferSize = 0;
  297. CSTR URL;
  298. CSTR UID;
  299. UID = buildUniqID();
  300. if( UID!= NULL ){
  301. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  302. RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
  303. if( URL != NULL ){
  304. RetVal = issuePost( URL, Buffer, BufferSize );
  305. free( URL );
  306. };
  307. free( UID );
  308. };
  309. return RetVal;
  310. };
  311. bool protocolSafeShore::Alarm3D( int Angle1, int Angle2, int Range, int ID ){
  312. bool RetVal= false;
  313. char Buffer[1024];
  314. int BufferSize = 0;
  315. CSTR URL;
  316. CSTR UID;
  317. UID = buildUniqID();
  318. if( UID!= NULL ){
  319. URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
  320. RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
  321. if( URL != NULL ){
  322. RetVal = issuePost( URL, Buffer, BufferSize );
  323. free( URL );
  324. };
  325. free( UID );
  326. };
  327. return RetVal;
  328. };
  329. bool protocolSafeShore::VisibilityChanged( bool Worsened ){
  330. bool RetVal= false;
  331. char Buffer[1024];
  332. int BufferSize = 0;
  333. CSTR URL;
  334. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  335. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
  336. if( URL != NULL ){
  337. RetVal = issuePost( URL, Buffer, BufferSize );
  338. free( URL );
  339. };
  340. return RetVal;
  341. };
  342. bool protocolSafeShore::StatusUpdate( void ){
  343. bool RetVal= false;
  344. char Buffer[1024];
  345. int BufferSize = 0;
  346. CSTR URL;
  347. URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
  348. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
  349. if( URL != NULL ){
  350. RetVal = issuePost( URL, Buffer, BufferSize );
  351. free( URL );
  352. };
  353. return RetVal;
  354. };
  355. bool protocolSafeShore::ErrorReporting( int lvl, CSTR Descr ){
  356. bool RetVal= false;
  357. char Buffer[1024];
  358. int BufferSize = 0;
  359. CSTR URL;
  360. URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName );
  361. RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
  362. if( URL != NULL ){
  363. RetVal = issuePost( URL, Buffer, BufferSize );
  364. free( URL );
  365. };
  366. return RetVal;
  367. };
  368. bool protocolSafeShore::StartTimeSynch( void ){
  369. bool RetVal= false;
  370. char Buffer[1024];
  371. int BufferSize = 0;
  372. CSTR URL = NULL;
  373. URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
  374. RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
  375. if( URL != NULL ){
  376. RetVal = issuePost( URL, Buffer, BufferSize );
  377. free( URL );
  378. };
  379. return RetVal;
  380. };
  381. bool protocolSafeShore::packetHandler( protocolPackage *Package ){
  382. //LOG("PACKET HANDLER");
  383. if( Package==NULL )return false;
  384. if( Package->m_nSize == 0 ) return false;
  385. if( Package->m_pData == NULL ) return false;
  386. if( Package->m_nCode != 200 ) return false;
  387. try{
  388. XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
  389. if( node1.isEmpty() ){
  390. //char BUBU[128];
  391. //sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen(pPackage), Package->m_nSize );
  392. //LOG_ERROR(BUBU);
  393. } else {
  394. LOG( node1.getName() );
  395. if( strstr(node1.getName(),"ClockSync")!=NULL ){
  396. #ifdef __linux__
  397. unsigned long long t0, t1, t2, t3;
  398. #else
  399. unsigned __int64 t0, t1, t2, t3;
  400. #endif
  401. XMLNode node3 = node1.getChildNode("t0", 0);
  402. XMLNode node4 = node1.getChildNode("t1", 0);
  403. XMLNode node5 = node1.getChildNode("t2", 0);
  404. t3 = getSHTimeValue();
  405. #ifdef __linux__
  406. t0 = atoll(node3.getText(0));
  407. t1 = atoll(node4.getText(0));
  408. t2 = atoll(node5.getText(0));
  409. #else
  410. t0 = _atoi64(node3.getText(0));
  411. t1 = _atoi64(node4.getText(0));
  412. t2 = _atoi64(node5.getText(0));
  413. #endif
  414. int offset = ((t1-t0)+(t2-t3))/2;
  415. int latency = (t3-t0)-(t2-t1);
  416. int jagajaga = offset+latency;
  417. //if( 0 & correctSHTime( jagajaga ) ){
  418. // LOG("Time is Synced");
  419. // g_iTimeDiff = 0;
  420. //} else {
  421. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  422. g_iTimeDiff = jagajaga;
  423. //};
  424. };
  425. };
  426. }catch( exception e ){
  427. return false;
  428. };
  429. return true;
  430. };
  431. #undef LOG
  432. #undef LOG_ERROR