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.

673 lines
24 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
6 years ago
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
6 years ago
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
  1. #include "LRF_TwinConnector.h"
  2. #include "LRF_Kernel.h"
  3. #include "LRF_TargetTracker.h"
  4. #ifdef TWI_TH_USE_PICTURES
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #include "../../data/stbi/stb_image.h"
  7. #define STB_IMAGE_WRITE_IMPLEMENTATION
  8. #include "../../data/stbi/stb_image_write.h"
  9. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  10. #undef STB_IMAGE_IMPLEMENTATION
  11. #endif
  12. #define LOG( X ) m_pLog->Log( "TWINC", this, X )
  13. #ifdef DEBUG
  14. #define LOG_ERROR( X ) {\
  15. char _tBfrErrMSG234[256];\
  16. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  17. m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  18. }
  19. #else
  20. #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
  21. #endif
  22. #define SLOG( X ) Me->m_pLog->Log( "TWINC", Me, X )
  23. #ifdef DEBUG
  24. #define SLOG_ERROR( X ) {\
  25. char _tBfrErrMSG234[256];\
  26. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  27. Me->m_pLog->Log( "TWINC", Me, _LOG_ERROR, _tBfrErrMSG234 );\
  28. }
  29. #else
  30. #define SLOG_ERROR( X ) Me->m_pLog->Log( "TWINC", Me, _LOG_ERROR, X );
  31. #endif
  32. LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
  33. m_pLog = basicLogCollection::getInstance();
  34. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
  35. m_pPort = NULL;
  36. m_pEngine = NULL;
  37. m_nStatus = 0;
  38. #ifdef PRU_SLAVE
  39. m_nMasterPort = 0;
  40. m_pMasterAddress = (CSTR) malloc( 16 );
  41. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  42. #else
  43. m_nMainSocket = -1;
  44. m_nBindPort = 0;
  45. m_pBindAddress = (CSTR) malloc( 16 );
  46. m_bWorkingMode = TWI_STAGE_BEFORE_BIND;
  47. #endif
  48. };
  49. LRF_TwinConnector::~LRF_TwinConnector(){
  50. if( m_pPort != NULL ) delete( m_pPort );
  51. if (m_bDrawThreadStatus > TH_ARMED ) {
  52. waitDrawThreadEnd( true );
  53. // JOIN in Wait function!!
  54. //pthread_detach(m_sThreadHandler);
  55. //pthread_join(m_sThreadHandler, NULL);
  56. };
  57. #ifdef PRU_SLAVE
  58. if( m_pMasterAddress ) free( m_pMasterAddress );
  59. #else
  60. if( m_pBindAddress ) free( m_pBindAddress );
  61. #endif
  62. };
  63. bool LRF_TwinConnector::create(){
  64. if( m_pSettings!= NULL ) {
  65. config_setting_t *pSetting;
  66. int tVal;
  67. char tBuffer[256];
  68. #ifdef PRU_SLAVE
  69. if (!config_setting_lookup_string(m_pSettings, "MASTER_ADDRESS", (const char**)&m_pMasterAddress)) {
  70. strncpy( m_pMasterAddress, "10.0.0.1", 15 );
  71. pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
  72. config_setting_set_string(pSetting, m_pMasterAddress);
  73. basicSettingsProvider::getInstance()->compromise();
  74. } else {
  75. sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_pMasterAddress );
  76. LOG( tBuffer );
  77. };
  78. if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
  79. tVal = m_nMasterPort = 6564;
  80. pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
  81. config_setting_set_bool( pSetting, tVal );
  82. basicSettingsProvider::getInstance()->compromise();
  83. } else {
  84. m_nMasterPort = tVal;
  85. sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
  86. LOG( tBuffer );
  87. };
  88. #else
  89. if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&m_pBindAddress)) {
  90. strncpy( m_pBindAddress, "10.0.0.1", 15 );
  91. pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
  92. config_setting_set_string(pSetting, m_pBindAddress);
  93. basicSettingsProvider::getInstance()->compromise();
  94. } else {
  95. sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_pBindAddress );
  96. LOG( tBuffer );
  97. };
  98. if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
  99. tVal = m_nBindPort = 6564;
  100. pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
  101. config_setting_set_bool( pSetting, tVal );
  102. basicSettingsProvider::getInstance()->compromise();
  103. } else {
  104. m_nBindPort = tVal;
  105. sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
  106. LOG( tBuffer );
  107. };
  108. #endif
  109. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  110. LOG("Save Settings");
  111. basicSettingsProvider::getInstance()->validate();
  112. };
  113. } else {
  114. LOG_ERROR("No Settings Provided!");
  115. return false;
  116. };
  117. #ifdef PRU_SLAVE
  118. //m_pProtocol = new protocolInternalLRF3D_SLAVE();
  119. //if( m_pProtocol == NULL ){
  120. // LOG_ERROR("Error creating protocol!");
  121. // return false;
  122. //};
  123. if( !protocolInternalLRF3D_SLAVE::create() ) return false;
  124. #else
  125. int RetCode;
  126. struct hostent * h_info;
  127. struct sockaddr_in * MainSocketAddress = NULL;
  128. //m_pProtocol = new protocolInternalLRF3D_MASTER();
  129. //if( m_pProtocol == NULL ){
  130. // LOG_ERROR("Error creating protocol!");
  131. // return false;
  132. //};
  133. if( !protocolInternalLRF3D_MASTER::create() ) return false;
  134. m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  135. if( m_nMainSocket == -1){
  136. LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
  137. return false;
  138. };
  139. MainSocketAddress = ( struct sockaddr_in * ) malloc( sizeof( struct sockaddr_in ) );
  140. if( MainSocketAddress == NULL ){
  141. LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  142. return false;
  143. };
  144. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  145. h_info = gethostbyname ( m_pBindAddress );
  146. if ( h_info == NULL ){
  147. LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
  148. return false;
  149. };
  150. //*****************************************
  151. //* there we building our network address *
  152. //*****************************************
  153. bcopy( h_info->h_addr, &(MainSocketAddress->sin_addr), h_info->h_length );
  154. MainSocketAddress->sin_family = h_info->h_addrtype;
  155. MainSocketAddress->sin_port = htons( m_nBindPort );
  156. RetCode = bind( m_nMainSocket ,(struct sockaddr *) MainSocketAddress, sizeof( struct sockaddr_in ) );
  157. if ( RetCode != 0 ){
  158. LOG_ERROR( "Error Binding to Port/Address" );
  159. return false;
  160. };
  161. RetCode = listen( m_nMainSocket, 128);
  162. if ( RetCode != 0 ){
  163. LOG_ERROR( "Cannot Listen Port/Address" );
  164. return false;
  165. };
  166. U8 dontblock;
  167. dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
  168. dontblock |= O_NONBLOCK | O_NDELAY;
  169. RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
  170. if ( RetCode == 0 ){
  171. LOG( "Set N-Blocking Mode of Accepting Socket" );
  172. } else {
  173. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  174. };
  175. #endif // PRU_SLAVE
  176. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  177. return init();
  178. };
  179. void *LRF_TwinConnector::m_fDrawThreadEntryPoint( void *ptr ){
  180. LRF_TwinConnector *Me;
  181. if( ptr == NULL ) return NULL;
  182. Me = (LRF_TwinConnector*) ptr;
  183. #ifdef DEBUG
  184. SLOG("TH Thread OK");
  185. #endif
  186. if( Me->m_bDrawThreadStatus == TH_ARMED ) {
  187. #ifdef DEBUG
  188. SLOG("TH Image Thread ARMED!");
  189. #endif
  190. Me->m_bDrawThreadStatus = TH_WORKING;
  191. if( Me->m_pEngine!= NULL ) {
  192. // Store Local TH Draws
  193. thresholdNode *tMem;
  194. uint32_t tSize;
  195. #ifdef DEBUG
  196. SLOG("Kernel is Defined!");
  197. #endif
  198. for( uint8_t i=0; i<16; i++ ) {
  199. // just in case if we must end work
  200. if( Me->m_bDrawThreadStatus != TH_WORKING ) break;
  201. tMem = Me->m_pEngine->getTreshold( tSize, i );
  202. if( tMem!= NULL ) {
  203. char tBuffer[256];
  204. #ifdef TWI_TH_USE_PICTURES
  205. sprintf( tBuffer, "data/th_data_%d.png", i );
  206. #ifdef DEBUG
  207. SLOG("SAVE TH Image ...");
  208. SLOG( tBuffer );
  209. #endif
  210. if( !Me->saveTHPicture( tMem, tBuffer, false )){
  211. SLOG_ERROR("TH PNG save failure!");
  212. }
  213. #ifdef PRU_SLAVE
  214. else {
  215. Me->m_mStatusProtector.lock();
  216. Me->m_nStatus |= ( 1 << i ) << 16;
  217. Me->m_mStatusProtector.unlock();
  218. }
  219. #endif
  220. ;
  221. #ifdef DEBUG
  222. SLOG("SAVE TH Image DONE!");
  223. #endif
  224. SLEEP(10);
  225. #else //TWI_TH_USE_PICTURES
  226. sprintf( tBuffer, "data/th_data_%d.raw", i );
  227. #ifdef DEBUG
  228. SLOG("SAVE TH Data ...");
  229. SLOG( tBuffer );
  230. #endif
  231. FILE *fd;
  232. fd = fopen( tBuffer, "wb+");
  233. if( fd != NULL ){
  234. uint32_t Size = TH_SIZE_NODES;
  235. fwrite( &Size, sizeof( uint32_t ), 1, fd );
  236. fwrite( tMem, sizeof( thresholdNode ), Size, fd );
  237. fclose( fd );
  238. #ifdef PRU_SLAVE
  239. Me->m_mStatusProtector.lock();
  240. Me->m_nStatus |= ( 1 << i ) << 16;
  241. Me->m_mStatusProtector.unlock();
  242. #endif
  243. } else {
  244. SLOG_ERROR("TH RAW save failure!");
  245. };
  246. #ifdef DEBUG
  247. SLOG("SAVE TH Data DONE!");
  248. #endif
  249. #endif //TWI_TH_USE_PICTURES
  250. };
  251. };
  252. };
  253. };
  254. Me->m_bDrawThreadStatus = TH_EXIT;
  255. Me->m_mDrawProtector.unlock();
  256. pthread_exit( NULL );
  257. return NULL;
  258. };
  259. void LRF_TwinConnector::waitDrawThreadEnd( bool forced ){
  260. if( m_bDrawThreadStatus > TH_WORKING || m_bDrawThreadStatus < TH_ARMED ) return;
  261. if( forced ) m_bDrawThreadStatus = TH_ENDED;
  262. while( m_bDrawThreadStatus != TH_EXIT ){
  263. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  264. };
  265. pthread_join( m_sDrawThreadHandler, NULL );
  266. };
  267. #ifdef PRU_SLAVE
  268. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  269. if( Package == NULL ) return false;
  270. TWIN_COMMAND cCMD = (TWIN_COMMAND) Package->m_pData[3];
  271. switch( cCMD ) {
  272. case TWC_GET_STATUS:{
  273. m_mStatusProtector.lock();
  274. sendStatus( m_nStatus );
  275. m_mStatusProtector.unlock();
  276. break;
  277. };
  278. };
  279. return true;
  280. };
  281. void LRF_TwinConnector::m_fThreadProc( void ){
  282. TWIN_CMD_REC * tCMD = NULL;
  283. int ClientSocket = -1;
  284. struct sockaddr ClientAddress;
  285. socklen_t ClientAddressSize;
  286. long long cMilliseconds;
  287. long long cStatusTO = 0;
  288. long long cDrawTO = 0;
  289. m_bThreadStatus = TH_WORKING;
  290. while( m_bThreadStatus < TH_ENDED ){
  291. cMilliseconds = getMilliseconds();
  292. // TH Saving procedure
  293. if( cDrawTO == 0 ) {
  294. cDrawTO = cMilliseconds;
  295. } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){
  296. #ifdef DEBUG
  297. LOG("Try to Dump TH Images");
  298. #endif
  299. if( m_mDrawProtector.try_lock() ) {
  300. #ifdef DEBUG
  301. LOG("Mutex Grabed");
  302. #endif
  303. if( m_bDrawThreadStatus > TH_ARMED ) {
  304. pthread_join( m_sDrawThreadHandler, NULL );
  305. };
  306. cDrawTO = cMilliseconds;
  307. m_bDrawThreadStatus = TH_ARMED;
  308. int RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this );
  309. if( RetVal == -1 ) {
  310. LOG_ERROR("Image Draw Thread Creation Failed.");
  311. m_mDrawProtector.unlock();
  312. };
  313. }
  314. #ifdef DEBUG
  315. else {
  316. LOG("Mutex NOT Grabed");
  317. }
  318. #endif
  319. ;
  320. };
  321. if( m_pPort != NULL ){
  322. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  323. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  324. if( tCMD!= NULL ){
  325. //reset commands on disconnect
  326. m_bCCMD = TWC_NO_COMMAND;
  327. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  328. free( tCMD );
  329. tCMD = NULL;
  330. };
  331. delete( m_pPort );
  332. m_pPort = NULL;
  333. continue;
  334. } else {
  335. // Status Send procedure
  336. if( cStatusTO == 0 ) {
  337. cStatusTO = cMilliseconds;
  338. } else if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ){
  339. m_mStatusProtector.lock();
  340. if( !sendStatus( m_nStatus ) ){
  341. LOG_ERROR("Status Sending failure!");
  342. m_pPort->disconnect();
  343. };
  344. m_mStatusProtector.unlock();
  345. };
  346. };
  347. } else {
  348. LOG("(re)Establish Connection");
  349. m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
  350. if( m_pPort!= NULL ){
  351. setTransport( m_pPort );
  352. m_bWorkingMode = TWI_STAGE_CONNECTED;
  353. };
  354. };
  355. };
  356. };
  357. #else //Master
  358. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  359. if( Package == NULL ) return false;
  360. TWIN_COMMAND cCMD = (TWIN_COMMAND)Package->m_pData[3];
  361. switch( cCMD ) {
  362. case TWC_ACK_STATUS:{
  363. uint32_t tStatus;
  364. #ifdef DEBUG
  365. LOG("Status ACK reseived");
  366. #endif
  367. tStatus = ( Package->m_pData[4] << 24 ) | ( Package->m_pData[5] << 16 ) | ( Package->m_pData[6] << 8 ) | Package->m_pData[7];
  368. m_mStatusProtector.lock();
  369. m_nStatus = tStatus;
  370. m_mStatusProtector.unlock();
  371. break;
  372. };
  373. case TWC_TH_DATA:{
  374. };
  375. };
  376. m_mLastTypeProtector.lock();
  377. m_bLastPackageType = cCMD;
  378. m_mLastTypeProtector.unlock();
  379. return true;
  380. };
  381. void LRF_TwinConnector::m_fThreadProc( void ){
  382. int ClientSocket = -1;
  383. struct sockaddr ClientAddress;
  384. socklen_t ClientAddressSize;
  385. long long cMilliseconds;
  386. long long cStatusTO = 0;
  387. long long cDrawTO = 0;
  388. m_bThreadStatus = TH_WORKING;
  389. while( m_bThreadStatus < TH_ENDED ){
  390. cMilliseconds = getMilliseconds();
  391. // TH Saving procedure
  392. if( cDrawTO == 0 ) {
  393. cDrawTO = cMilliseconds;
  394. } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){
  395. #ifdef DEBUG
  396. LOG("Try to Dump TH Images");
  397. #endif
  398. if( m_mDrawProtector.try_lock() ) {
  399. #ifdef DEBUG
  400. LOG("Mutex Grabed");
  401. #endif
  402. if( m_bDrawThreadStatus > TH_ARMED ) {
  403. pthread_join( m_sDrawThreadHandler, NULL );
  404. };
  405. cDrawTO = cMilliseconds;
  406. m_bDrawThreadStatus = TH_ARMED;
  407. int RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this );
  408. if( RetVal == -1 ) {
  409. LOG_ERROR("Image Draw Thread Creation Failed.");
  410. m_mDrawProtector.unlock();
  411. };
  412. }
  413. #ifdef DEBUG
  414. else {
  415. LOG("Mutex NOT Grabed");
  416. }
  417. #endif
  418. ;
  419. };
  420. // Port exists - SLAVE is connected
  421. if( m_pPort != NULL ){
  422. // Check if still connected
  423. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  424. // Handle Disconnection
  425. m_bWorkingMode = TWI_STAGE_WAITING_CONNECTION;
  426. delete( m_pPort );
  427. m_pPort = NULL;
  428. //Reset Flags
  429. cStatusTO = 0;
  430. continue;
  431. };
  432. SLEEP( TWI_SLOW_LOOP_SLEEP );
  433. } else {
  434. // (re)Establish Connection
  435. ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
  436. ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
  437. if ( ClientSocket == -1 ) {
  438. switch( errno ){
  439. case EAGAIN:{
  440. SLEEP( TWI_QUICK_LOOP_SLEEP );
  441. continue;
  442. };
  443. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  444. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  445. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  446. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  447. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  448. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  449. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  450. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  451. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  452. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  453. case ENOBUFS:
  454. case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
  455. case EPROTO: LOG_ERROR( "Protocol error."); break;
  456. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  457. case ENOSR:
  458. case ESOCKTNOSUPPORT:
  459. case EPROTONOSUPPORT:
  460. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  461. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  462. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  463. };
  464. m_bThreadStatus = TH_ENDED;
  465. } else {
  466. char tstr[CQUERY];
  467. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, CQUERY );
  468. LOG( "SLAVE LRF Connection Accepted" );
  469. m_pPort = new serverPort( ClientSocket );
  470. };
  471. if( m_pPort!= NULL ){
  472. setTransport( m_pPort );
  473. // Reset Stage
  474. m_bWorkingMode = TWI_STAGE_CONNECTED;
  475. };
  476. };
  477. };
  478. };
  479. bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){
  480. if(ptr == NULL) return false;
  481. m_pTracker = ptr;
  482. return false;
  483. };
  484. bool LRF_TwinConnector::updateSettings( void ){
  485. return true;
  486. };
  487. #endif //!PRU_SLAVE
  488. bool LRF_TwinConnector::cleanTHData( uint8_t kid ){
  489. char tBuffer[1024];
  490. FILE *FID = NULL;
  491. if( kid > 31 ) kid = 31;
  492. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  493. if( !fileExist( "data" ) ){
  494. int RetVal = mkdir( "data", STORAGE_DIR_MODE );
  495. if( RetVal < 0 ) {
  496. LOG_ERROR( "Error Creating DATA DIR!" );
  497. return false;
  498. };
  499. };
  500. FID = fopen( tBuffer, "wb+" );
  501. if( FID == NULL ){
  502. LOG_ERROR( "Error Creating DATA FILE!" );
  503. return false;
  504. };
  505. uint32_t ZZ = 0;
  506. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  507. fwrite( &ZZ, 4, 1, FID );
  508. };
  509. fclose( FID );
  510. return true;
  511. };
  512. bool LRF_TwinConnector::loadTHData( uint8_t kid, thresholdNode *ptr ){
  513. FILE *FID;
  514. char tBuffer[1024];
  515. if( ptr == NULL ) return false;
  516. if( kid > 31 ) kid = 31;
  517. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  518. FID = fopen( tBuffer, "rb" );
  519. if( FID == NULL ) {
  520. LOG_ERROR( "Error Opening DATA FILE!" );
  521. return false;
  522. };
  523. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  524. fread( &ptr[i], TH_NODE_SIZE, 1, FID );
  525. };
  526. fclose( FID );
  527. return true;
  528. };
  529. bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){
  530. FILE *FID;
  531. char tBuffer[1024];
  532. if( ptr == NULL ) return false;
  533. if( kid > 31 ) kid = 31;
  534. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  535. FID = fopen( tBuffer, "wb+" );
  536. if( FID == NULL ) {
  537. LOG_ERROR( "Error Creating DATA FILE!" );
  538. return false;
  539. };
  540. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  541. fwrite( &ptr[i], TH_NODE_SIZE, 1, FID );
  542. };
  543. fclose( FID );
  544. return true;
  545. };
  546. #ifdef TWI_TH_USE_PICTURES
  547. bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome, bool agressive ){
  548. uint8_t *tMemory = NULL;
  549. thresholdNode *cNode = NULL;
  550. uint8_t *cPixel = NULL;
  551. uint8_t cSpace;
  552. if( monochrome ) {
  553. cSpace = 1;
  554. } else {
  555. cSpace = 4;
  556. };
  557. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  558. if( tMemory == NULL ){
  559. LOG_ERROR("Image Memory Alloc Eror!");
  560. return false;
  561. };
  562. for( int j = 0; j < Q_SCAN_COUNT; j++ ) {
  563. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  564. //cNode = ptr+(j*i);
  565. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  566. #ifdef PRU_SLAVE
  567. cNode = ptr+makeIds( i, j , false );
  568. cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  569. #else
  570. cNode = ptr+makeIds( i, j , true );
  571. cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  572. //cNode = ptr + ( i * j );
  573. //cPixel = tMemory + ( cSpace * i * j );
  574. #endif
  575. if( !agressive ) SLEEP(1);
  576. if( monochrome ) {
  577. #ifdef CHECK_SUN_BIT
  578. if( cNode->values.sbit ) {
  579. cPixel[0] = 0;
  580. } else {
  581. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  582. };
  583. #else
  584. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  585. #endif
  586. } else {
  587. #ifdef CHECK_SUN_BIT
  588. if( cNode->values.sbit ) {
  589. // Set Red
  590. cPixel[0] = 0x00;
  591. cPixel[1] = 0xff;
  592. cPixel[2] = 0x00;
  593. cPixel[3] = 0xff;
  594. } else {
  595. if( cNode->values.cnt == 0 ) {
  596. // Set Blue
  597. cPixel[0] = 0x00;
  598. cPixel[1] = 0x00;
  599. cPixel[2] = 0xff;
  600. cPixel[3] = 0xff;
  601. } else {
  602. uint32_t tColor;
  603. float tVal = ((float) cNode->values.min ) / 4096.f;
  604. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  605. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  606. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  607. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  608. cPixel[3] = tColor & 0xFF;
  609. };
  610. };
  611. #else
  612. if( cNode->values.cnt == 0 ) {
  613. // Set Blue
  614. cPixel[0] = 0x00;
  615. cPixel[1] = 0x00;
  616. cPixel[2] = 0xff;
  617. cPixel[3] = 0xff;
  618. } else {
  619. uint32_t tColor;
  620. float tVal = ((float) cNode->values.min ) / 4096.f;
  621. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  622. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  623. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  624. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  625. cPixel[3] = tColor & 0xFF;
  626. };
  627. #endif
  628. };
  629. };
  630. };
  631. int RetVal = stbi_write_png( fileName, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  632. free( tMemory );
  633. if( RetVal == 0 ) return false;
  634. return true;
  635. };
  636. #endif //TWI_TH_USE_PICTURES
  637. #undef SLOG
  638. #undef SLOG_ERROR
  639. #undef LOG
  640. #undef LOG_ERROR