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.

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