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.

467 lines
17 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "LRF_TwinConnector.h"
  2. #include "LRF_Kernel.h"
  3. #include "LRF_TargetTracker.h"
  4. #define LOG( X ) m_pLog->Log( "TWINC", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG234[256];\
  8. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
  13. #endif
  14. LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
  15. m_pLog = basicLogCollection::getInstance();
  16. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
  17. m_pProtocol = NULL;
  18. m_pPort = NULL;
  19. m_pEngine = NULL;
  20. #ifdef PRU_SLAVE
  21. m_nMasterPort = 0;
  22. m_pMasterAddress = (CSTR) malloc( 16 );
  23. #else
  24. m_nMainSocket = -1;
  25. m_nBindPort = 0;
  26. m_pBindAddress = (CSTR) malloc( 16 );
  27. #endif
  28. };
  29. LRF_TwinConnector::~LRF_TwinConnector(){
  30. if( m_pPort != NULL ) delete( m_pPort );
  31. if( m_pProtocol != NULL ) delete( m_pProtocol );
  32. #ifdef PRU_SLAVE
  33. if( m_pMasterAddress ) free( m_pMasterAddress );
  34. #else
  35. if( m_pBindAddress ) free( m_pBindAddress );
  36. #endif
  37. };
  38. bool LRF_TwinConnector::create(){
  39. if( m_pSettings!= NULL ) {
  40. config_setting_t *pSetting;
  41. int tVal;
  42. char tBuffer[256];
  43. #ifdef PRU_SLAVE
  44. if (!config_setting_lookup_string(m_pSettings, "MASTER_ADDRESS", (const char**)&m_pMasterAddress)) {
  45. strncpy( m_pMasterAddress, "10.0.0.1", 15 );
  46. pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
  47. config_setting_set_string(pSetting, m_pMasterAddress);
  48. basicSettingsProvider::getInstance()->compromise();
  49. } else {
  50. sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_pMasterAddress );
  51. LOG( tBuffer );
  52. };
  53. if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
  54. tVal = m_nMasterPort = 6564;
  55. pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
  56. config_setting_set_bool( pSetting, tVal );
  57. basicSettingsProvider::getInstance()->compromise();
  58. } else {
  59. m_nMasterPort = tVal;
  60. sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
  61. LOG( tBuffer );
  62. };
  63. #else
  64. if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&m_pBindAddress)) {
  65. strncpy( m_pBindAddress, "10.0.0.1", 15 );
  66. pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
  67. config_setting_set_string(pSetting, m_pBindAddress);
  68. basicSettingsProvider::getInstance()->compromise();
  69. } else {
  70. sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_pBindAddress );
  71. LOG( tBuffer );
  72. };
  73. if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
  74. tVal = m_nBindPort = 6564;
  75. pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
  76. config_setting_set_bool( pSetting, tVal );
  77. basicSettingsProvider::getInstance()->compromise();
  78. } else {
  79. m_nBindPort = tVal;
  80. sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
  81. LOG( tBuffer );
  82. };
  83. #endif
  84. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  85. LOG("Save Settings");
  86. basicSettingsProvider::getInstance()->validate();
  87. };
  88. } else {
  89. LOG_ERROR("No Settings Provided!");
  90. return false;
  91. };
  92. #ifdef PRU_SLAVE
  93. m_pProtocol = new protocolInternalLRF3D_SLAVE();
  94. if( m_pProtocol == NULL ){
  95. LOG_ERROR("Error creating protocol!");
  96. return false;
  97. };
  98. if( !m_pProtocol->create() ) return false;
  99. #else
  100. int RetCode;
  101. struct hostent * h_info;
  102. struct sockaddr_in * MainSocketAddress = NULL;
  103. m_pProtocol = new protocolInternalLRF3D_MASTER();
  104. if( m_pProtocol == NULL ){
  105. LOG_ERROR("Error creating protocol!");
  106. return false;
  107. };
  108. if( !m_pProtocol->create() ) return false;
  109. m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  110. if( m_nMainSocket == -1){
  111. LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
  112. return false;
  113. };
  114. MainSocketAddress = ( struct sockaddr_in * ) malloc( sizeof( struct sockaddr_in ) );
  115. if( MainSocketAddress == NULL ){
  116. LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  117. return false;
  118. };
  119. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  120. h_info = gethostbyname ( m_pBindAddress );
  121. if ( h_info == NULL ){
  122. LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
  123. return false;
  124. };
  125. //*****************************************
  126. //* there we building our network address *
  127. //*****************************************
  128. bcopy( h_info->h_addr, &(MainSocketAddress->sin_addr), h_info->h_length );
  129. MainSocketAddress->sin_family = h_info->h_addrtype;
  130. MainSocketAddress->sin_port = htons( m_nBindPort );
  131. RetCode = bind( m_nMainSocket ,(struct sockaddr *) MainSocketAddress, sizeof( struct sockaddr_in ) );
  132. if ( RetCode != 0 ){
  133. LOG_ERROR( "Error Binding to Port/Address" );
  134. return false;
  135. };
  136. RetCode = listen( m_nMainSocket, 128);
  137. if ( RetCode != 0 ){
  138. LOG_ERROR( "Cannot Listen Port/Address" );
  139. return false;
  140. };
  141. U8 dontblock;
  142. dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
  143. dontblock |= O_NONBLOCK | O_NDELAY;
  144. RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
  145. if ( RetCode == 0 ){
  146. LOG( "Set N-Blocking Mode of Accepting Socket" );
  147. } else {
  148. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  149. };
  150. #endif // PRU_SLAVE
  151. return init();
  152. };
  153. #ifdef PRU_SLAVE
  154. void LRF_TwinConnector::m_fThreadProc( void ){
  155. TWIN_CMD_REC * tCMD = NULL;
  156. int ClientSocket = -1;
  157. struct sockaddr ClientAddress;
  158. socklen_t ClientAddressSize;
  159. m_bThreadStatus = TH_WORKING;
  160. while( m_bThreadStatus < TH_ENDED ){
  161. if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
  162. // No CMD is EXEC so check new cmds
  163. CMD_PROTEC.lock();
  164. if( m_qCMD.size() > 0 ){
  165. tCMD = m_qCMD.front();
  166. m_qCMD.pop();
  167. };
  168. CMD_PROTEC.unlock();
  169. };
  170. if( m_pPort != NULL ){
  171. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  172. if( tCMD!= NULL ){
  173. //reset commands on disconnect
  174. m_bCCMD = TWC_NO_COMMAND;
  175. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  176. free( tCMD );
  177. tCMD = NULL;
  178. };
  179. delete( m_pPort );
  180. m_pPort = NULL;
  181. continue;
  182. };
  183. if( tCMD!= NULL ){
  184. if( m_bCCMD == TWC_NO_COMMAND ){
  185. // CMD not issued yet!
  186. switch( tCMD->CMD ){
  187. case TWC_PUSH_TARGET:{
  188. break;
  189. };
  190. };
  191. } else {
  192. // Waiting CMD exe status
  193. switch( m_bCCMD ){
  194. case TWC_SET_THDRAW:{
  195. break;
  196. };
  197. };
  198. if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
  199. // The command is finished - Will fetch next command from queue
  200. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  201. free( tCMD );
  202. tCMD = NULL;
  203. };
  204. };
  205. SLEEP( 5 );
  206. } else {
  207. SLEEP( 100 );
  208. };
  209. } else {
  210. LOG("(re)Establish Connection");
  211. m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
  212. if( m_pPort!= NULL ){
  213. m_pProtocol->setTransport( m_pPort );
  214. };
  215. };
  216. };
  217. };
  218. #else //Master
  219. void LRF_TwinConnector::m_fThreadProc( void ){
  220. TWIN_CMD_REC * tCMD = NULL;
  221. int ClientSocket = -1;
  222. struct sockaddr ClientAddress;
  223. socklen_t ClientAddressSize;
  224. long long cMilliseconds;
  225. long long cStatusTO = 0;
  226. m_bThreadStatus = TH_WORKING;
  227. while( m_bThreadStatus < TH_ENDED ){
  228. cMilliseconds = getMilliseconds();
  229. if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
  230. // No CMD is EXEC so check new cmds
  231. CMD_PROTEC.lock();
  232. if( m_qCMD.size() > 0 ){
  233. tCMD = m_qCMD.front();
  234. m_qCMD.pop();
  235. };
  236. CMD_PROTEC.unlock();
  237. };
  238. if( m_pPort != NULL ){
  239. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  240. if( tCMD!= NULL ){
  241. //reset commands on disconnect
  242. m_bCCMD = TWC_NO_COMMAND;
  243. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  244. free( tCMD );
  245. tCMD = NULL;
  246. };
  247. delete( m_pPort );
  248. m_pPort = NULL;
  249. continue;
  250. };
  251. if( tCMD!= NULL ){
  252. if( m_bCCMD == TWC_NO_COMMAND ){
  253. // CMD not issued yet!
  254. switch( tCMD->CMD ){
  255. case TWC_GET_STATUS:{
  256. if( m_pProtocol->requestStatus() ){
  257. m_bCCMD = tCMD->CMD;
  258. };
  259. break;
  260. };
  261. case TWC_GET_CONFIG:{
  262. break;
  263. };
  264. case TWC_SET_CONFIG:{
  265. break;
  266. };
  267. case TWC_GET_THDRAW:{
  268. break;
  269. };
  270. case TWC_DO_REBOOT:{
  271. break;
  272. };
  273. };
  274. } else {
  275. // Waiting CMD exe status
  276. switch( m_bCCMD ){
  277. case TWC_GET_STATUS:{
  278. if( m_pProtocol->getLastPackageType() == TWIN_STATUS_ACK ){
  279. m_bCCMD = TWC_NO_COMMAND;
  280. #ifdef DEBUG
  281. LOG("Registered Ack Status");
  282. #endif
  283. uint32_t nStatus = m_pProtocol->getStatus();
  284. if( ( nStatus & TWI_STATUS_TARGET_MASK ) > 0 ){
  285. #ifdef DEBUG
  286. LOG("Have new Targets!");
  287. #endif
  288. m_pProtocol->setTData( m_pTargets );
  289. m_pProtocol->requestTargets();
  290. m_bCCMD = TWC_GET_TARGETS;
  291. continue;
  292. };
  293. };
  294. break;
  295. };
  296. case TWC_GET_TARGETS:{
  297. if( m_pProtocol->getLastPackageType() == TWIN_TARGETS_DATA ){
  298. m_bCCMD = TWC_NO_COMMAND;
  299. #ifdef DEBUG
  300. LOG("Registered Ack Targets");
  301. #endif
  302. if( m_pTracker != NULL ) {
  303. uint8_t nCNT = (( m_pProtocol->getStatus() & TWI_STATUS_TARGET_MASK ) >> 8 );
  304. for( uint8_t index = 0; index < nCNT; index++ ){
  305. m_pTracker->validateTarget( &m_pTargets[index], false );
  306. };
  307. };
  308. };
  309. break;
  310. };
  311. };
  312. if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
  313. // The command is finished - Will fetch next command from queue
  314. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  315. free( tCMD );
  316. tCMD = NULL;
  317. };
  318. };
  319. SLEEP( TWI_QUICK_LOOP_SLEEP );
  320. } else {
  321. if( ( cMilliseconds - cStatusTO ) > TWI_STATUS_LOOP_MS ) {
  322. cStatusTO = cMilliseconds;
  323. m_pProtocol->requestStatus();
  324. m_bCCMD = TWC_GET_STATUS;
  325. LOG("Req Status");
  326. } else {
  327. SLEEP( TWI_SLOW_LOOP_SLEEP );
  328. };
  329. };
  330. } else {
  331. // (re)Establish Connection
  332. ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
  333. ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
  334. if ( ClientSocket == -1 ) {
  335. switch( errno ){
  336. case EAGAIN:{
  337. SLEEP(10);
  338. continue;
  339. };
  340. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  341. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  342. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  343. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  344. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  345. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  346. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  347. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  348. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  349. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  350. case ENOBUFS:
  351. 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;
  352. case EPROTO: LOG_ERROR( "Protocol error."); break;
  353. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  354. case ENOSR:
  355. case ESOCKTNOSUPPORT:
  356. case EPROTONOSUPPORT:
  357. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  358. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  359. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  360. };
  361. m_bThreadStatus = TH_ENDED;
  362. } else {
  363. char tstr[CQUERY];
  364. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, CQUERY );
  365. LOG( "SLAVE LRF Connection Accepted" );
  366. m_pPort = new serverPort( ClientSocket );
  367. };
  368. if( m_pPort!= NULL ){
  369. m_pProtocol->setTransport( m_pPort );
  370. };
  371. };
  372. };
  373. };
  374. #endif //!PRU_SLAVE
  375. bool LRF_TwinConnector::cleanTHData( uint8_t kid ){
  376. char tBuffer[1024];
  377. FILE *FID = NULL;
  378. if( kid > 31 ) kid = 31;
  379. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  380. if( !fileExist( "data" ) ){
  381. int RetVal = mkdir( "data", STORAGE_DIR_MODE );
  382. if( RetVal < 0 ) {
  383. LOG_ERROR( "Error Creating DATA DIR!" );
  384. return false;
  385. };
  386. };
  387. FID = fopen( tBuffer, "wb+" );
  388. if( FID == NULL ){
  389. LOG_ERROR( "Error Creating DATA FILE!" );
  390. return false;
  391. };
  392. uint32_t ZZ = 0;
  393. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  394. fwrite( &ZZ, 4, 1, FID );
  395. };
  396. fclose( FID );
  397. return true;
  398. };
  399. bool LRF_TwinConnector::loadTHData( uint8_t kid, thresholdNode *ptr ){
  400. FILE *FID;
  401. char tBuffer[1024];
  402. if( ptr == NULL ) return false;
  403. if( kid > 31 ) kid = 31;
  404. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  405. FID = fopen( tBuffer, "rb" );
  406. if( FID == NULL ) {
  407. LOG_ERROR( "Error Opening DATA FILE!" );
  408. return false;
  409. };
  410. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  411. fread( &ptr[i], TH_NODE_SIZE, 1, FID );
  412. };
  413. fclose( FID );
  414. return true;
  415. };
  416. bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){
  417. FILE *FID;
  418. char tBuffer[1024];
  419. if( ptr == NULL ) return false;
  420. if( kid > 31 ) kid = 31;
  421. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  422. FID = fopen( tBuffer, "wb+" );
  423. if( FID == NULL ) {
  424. LOG_ERROR( "Error Creating DATA FILE!" );
  425. return false;
  426. };
  427. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  428. fwrite( &ptr[i], TH_NODE_SIZE, 1, FID );
  429. };
  430. fclose( FID );
  431. return true;
  432. };
  433. #ifndef PRU_SLAVE
  434. bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){
  435. if(ptr == NULL) return false;
  436. m_pTracker = ptr;
  437. return false;
  438. };
  439. #endif
  440. #undef LOG
  441. #undef LOG_ERROR