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.

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