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.

956 lines
35 KiB

6 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
​
6 years ago
5 years ago
​
6 years ago
​
5 years ago
​
​
6 years ago
​
​
5 years ago
​
​
6 years ago
​
​
6 years ago
5 years ago
​
6 years ago
5 years ago
6 years ago
5 years ago
​
​
6 years ago
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
6 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
6 years ago
5 years ago
6 years ago
​
​
​
​
5 years ago
​
6 years ago
​
​
5 years ago
5 years ago
5 years ago
​
​
5 years ago
​
​
​
5 years ago
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
5 years ago
​
​
​
​
​
​
​
6 years ago
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
5 years ago
5 years ago
5 years ago
​
​
​
​
6 years ago
​
​
5 years ago
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
6 years ago
5 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
5 years ago
​
​
​
​
5 years ago
​
​
6 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
  1. #include "LRF_TwinConnector.h"
  2. #ifdef SLIM_KERNEL
  3. #include "LRF_SlimKernel.h"
  4. #else
  5. #include "LRF_Kernel.h"
  6. #endif
  7. #ifndef PRU_SLAVE
  8. #include "LRF_TargetTracker.h"
  9. #endif
  10. #define LOG( X ) m_pLog->Log( "TWINC", this, X )
  11. #ifdef DEBUG
  12. #define LOG_ERROR( X ) {\
  13. char _tBfrErrMSG234[256];\
  14. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  15. m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  16. }
  17. #else
  18. #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
  19. #endif
  20. #define SLOG( X ) Me->m_pLog->Log( "TWINC_T", Me, X )
  21. #ifdef DEBUG
  22. #define SLOG_ERROR( X ) {\
  23. char _tBfrErrMSG234[256];\
  24. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  25. Me->m_pLog->Log( "TWINC_T", Me, _LOG_ERROR, _tBfrErrMSG234 );\
  26. }
  27. #else
  28. #define SLOG_ERROR( X ) Me->m_pLog->Log( "TWINC_T", Me, _LOG_ERROR, X );
  29. #endif
  30. LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
  31. m_pLog = basicLogCollection::getInstance();
  32. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
  33. m_pEngine = NULL;
  34. m_pCurrentTH = NULL;
  35. m_bDrawThreadStatus = TH_PRE_INIT;
  36. #ifdef PRU_SLAVE
  37. m_nMasterPort = 0;
  38. memset( m_cMasterAddress, 0 , 16 );
  39. m_eWM = TWI_STAGE_WAITING_CONNECTION;
  40. memset(&m_sSystemStatus,0, sizeof(TWI_PEER_STATUS));
  41. #else
  42. m_nMainSocket = -1;
  43. m_nBindPort = 0;
  44. memset( m_cBindAddress, 0, 16 );
  45. m_eWM = TWI_STAGE_BEFORE_BIND;
  46. cleanupInfo();
  47. #endif
  48. };
  49. LRF_TwinConnector::~LRF_TwinConnector(){
  50. waitDrawThreadEnd( true );
  51. waitThreadEnd( true );
  52. #ifdef PRU_SLAVE
  53. if( m_oDataTransport != NULL ) delete( (clientPort *) m_oDataTransport );
  54. #else
  55. if( m_oDataTransport != NULL ) delete( (serverPort *) m_oDataTransport );
  56. #endif
  57. };
  58. void LRF_TwinConnector::setWM( TWI_WM NWM ){
  59. m_mWMProtector.lock();
  60. m_eWM = NWM;
  61. m_mWMProtector.unlock();
  62. };
  63. TWI_WM LRF_TwinConnector::getWM(){
  64. TWI_WM rv;
  65. m_mWMProtector.lock();
  66. rv = m_eWM;
  67. m_mWMProtector.unlock();
  68. return rv;
  69. };
  70. void LRF_TwinConnector::setCMD( TWIN_COMMAND NCMD ){
  71. m_mCMDProtector.lock();
  72. m_eCCMD = NCMD;
  73. m_bCMDTO = 10; // 1 sec
  74. m_mCMDProtector.unlock();
  75. };
  76. TWIN_COMMAND LRF_TwinConnector::getCMD(){
  77. TWIN_COMMAND rv;
  78. m_mCMDProtector.lock();
  79. rv = m_eCCMD;
  80. m_mCMDProtector.unlock();
  81. return rv;
  82. };
  83. bool LRF_TwinConnector::readyToConsumeTH( void ){
  84. if ( m_mDrawProtector.try_lock()) {
  85. m_mDrawProtector.unlock();
  86. return true;
  87. };
  88. return false;
  89. };
  90. bool LRF_TwinConnector::consumeTH( uint8_t kID, thresholdNode *ptr, uint32_t THsize ){
  91. int RetVal;
  92. if ( ptr == NULL) return false;
  93. if ( THsize == 0 ) return false;
  94. m_mStatusProtector.lock();
  95. #ifdef PRU_SLAVE
  96. m_sSystemStatus.TH_READY &= ~(1 << kID);
  97. #else
  98. m_sSystemStatus.MASTER_STATUS.TH_READY &= ~(1 << kID);
  99. #endif
  100. m_mStatusProtector.unlock();
  101. cleanTHData( kID );
  102. if( m_bDrawThreadStatus > TH_ARMED ) {
  103. RetVal = pthread_join( m_sDrawThreadHandler, NULL );
  104. if( RetVal != 0 ){
  105. LOG_ERROR("Thread Join Error!");
  106. return false;
  107. };
  108. };
  109. m_mDrawProtector.lock();
  110. m_pCurrentTH = ptr;
  111. m_bDrawThreadStatus = TH_ARMED;
  112. RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this );
  113. if( RetVal == -1 ) {
  114. LOG_ERROR("Image Draw Thread Creation Failed.");
  115. m_mDrawProtector.unlock();
  116. };
  117. return true;
  118. };
  119. #ifdef PRU_SLAVE
  120. bool LRF_TwinConnector::create(){
  121. if( m_pSettings!= NULL ) {
  122. config_setting_t *pSetting;
  123. int tVal;
  124. char tBuffer[256];
  125. CSTR tcVal = NULL;
  126. if (!config_setting_lookup_string( m_pSettings, "MASTER_ADDRESS", (const char**)&tcVal)){ //&m_cMasterAddress )) {
  127. strncpy( m_cMasterAddress, "10.0.0.1", 15 );
  128. pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
  129. config_setting_set_string(pSetting, m_cMasterAddress);
  130. basicSettingsProvider::getInstance()->compromise();
  131. } else {
  132. strncpy( m_cMasterAddress, tcVal, 16 );
  133. sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_cMasterAddress );
  134. LOG( tBuffer );
  135. };
  136. if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
  137. tVal = m_nMasterPort = 6564;
  138. pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
  139. config_setting_set_bool( pSetting, tVal );
  140. basicSettingsProvider::getInstance()->compromise();
  141. } else {
  142. m_nMasterPort = tVal;
  143. sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
  144. LOG( tBuffer );
  145. };
  146. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  147. LOG("Save Settings");
  148. basicSettingsProvider::getInstance()->validate();
  149. };
  150. } else {
  151. LOG_ERROR("No Settings Provided!");
  152. return false;
  153. };
  154. if( !protocolInternalLRF3D_SLAVE::create() ) return false;
  155. m_eWM = TWI_STAGE_WAITING_CONNECTION;
  156. return init();
  157. };
  158. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  159. char tB[128];
  160. if( Package == NULL ) return false;
  161. if( Package->m_pData == NULL ) return false;
  162. if (Package->m_nSize < TWI_CMD_INDEX ) return false;
  163. TWIN_COMMAND cCMD = (TWIN_COMMAND)Package->m_pData[TWI_CMD_INDEX];
  164. //Any In Command Switchs to CMD mode
  165. sprintf(tB,"Data IN! CODE: %d", cCMD );
  166. LOG( tB );
  167. switch( cCMD ) {
  168. case TWC_SET_CMD_MODE:{
  169. LOG("Set CMD Mode");
  170. //First Disable Events Propogation then Send ACK
  171. setWM( TWI_STAGE_REQUEST_MODE );
  172. sendACK( TWC_SET_CMD_MODE );
  173. break;
  174. };
  175. case TWC_SET_EVT_MODE:{
  176. LOG("Set EVT Mode");
  177. //First Send ACK then Enable Events Propogation
  178. sendACK( TWC_SET_EVT_MODE );
  179. setWM( TWI_STAGE_EVENT_MODE );
  180. break;
  181. };
  182. case TWC_GET_VERSION:{
  183. LOG("Get Version");
  184. sendVersion( &gsSOFT_INFO );
  185. break;
  186. };
  187. case TWC_GET_MODE:{
  188. LOG("Get Mode");
  189. sendMode( getWM() );
  190. break;
  191. };
  192. };
  193. return true;
  194. };
  195. /// <summary>
  196. /// Main Loop Of Slave Part Of Connector
  197. /// </summary>
  198. /// <returns>Nothing</returns>
  199. void LRF_TwinConnector::m_fThreadProc( void ){
  200. TWI_CMD_CONTAINER * tCMD = NULL;
  201. int ClientSocket = -1;
  202. struct sockaddr ClientAddress;
  203. socklen_t ClientAddressSize;
  204. long long cMilliseconds;
  205. long long cStatusTO = 0;
  206. long long cDrawTO = getMilliseconds();
  207. m_bThreadStatus = TH_WORKING;
  208. while( m_bThreadStatus < TH_ENDED ){
  209. cMilliseconds = getMilliseconds();
  210. /*
  211. // TH Saving procedure
  212. if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){
  213. if( m_mDrawProtector.try_lock() ) {
  214. // The Mutex will be unlocked by the End of the thread procedure
  215. int RetVal;
  216. if( m_bDrawThreadStatus > TH_ARMED ) {
  217. RetVal = pthread_join( m_sDrawThreadHandler, NULL );
  218. if( RetVal != 0 ){
  219. LOG_ERROR("Thread Join Error!");
  220. };
  221. };
  222. m_bDrawThreadStatus = TH_ARMED;
  223. RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this );
  224. if( RetVal == -1 ) {
  225. LOG_ERROR("Image Draw Thread Creation Failed.");
  226. m_mDrawProtector.unlock();
  227. } else cDrawTO = cMilliseconds;
  228. };
  229. };
  230. */
  231. if ( getWM() >= TWI_STAGE_CONNECTED ) {
  232. if( m_oDataTransport!= NULL ) {
  233. if (m_oDataTransport->getConnectionStatus() == CS_DISCONNECTED) {
  234. setWM( TWI_STAGE_WAITING_CONNECTION );
  235. continue;
  236. };
  237. } else LOG_ERROR("Port Object Missed!");
  238. };
  239. switch( getWM() ) {
  240. case TWI_STAGE_WAITING_CONNECTION:{
  241. if( m_oDataTransport != NULL ){
  242. delete( (clientPort *) m_oDataTransport );
  243. m_oDataTransport = NULL;
  244. } else {
  245. clientPort *tPort = new clientPort( m_cMasterAddress, m_nMasterPort, true );
  246. if( tPort!= NULL ){
  247. if( tPort->create() ){
  248. setTransport( tPort );
  249. setWM( TWI_STAGE_CONNECTED );
  250. } else {
  251. delete( tPort );
  252. SLEEP( TWI_SLOW_LOOP_SLEEP );
  253. };
  254. } else LOG_ERROR( "Port Not Created!");
  255. };
  256. break;
  257. };
  258. case TWI_STAGE_REQUEST_MODE:{
  259. switch( getCMD() ){
  260. case TWC_GET_STATUS:{
  261. if( m_pEngine ){
  262. LRF_WORK_MODE tKM = m_pEngine->getWM();
  263. } else {
  264. //sendStatus();
  265. };
  266. setCMD( TWC_NO_COMMAND );
  267. break;
  268. };
  269. //case
  270. };
  271. break;
  272. };
  273. case TWI_STAGE_EVENT_MODE:{
  274. //Fetch Event from Queue and send to master
  275. break;
  276. };
  277. };
  278. SLEEP( TWI_SLOW_LOOP_SLEEP );
  279. };
  280. };
  281. #else //Master
  282. void LRF_TwinConnector::cleanupInfo(void){
  283. memset( &m_sSystemStatus, 0, sizeof( TWIN_STATUS ));
  284. memcpy( &m_sSystemStatus.MASTER_NFO, &gsSOFT_INFO, sizeof(SOFTWARE_INFO));
  285. };
  286. bool LRF_TwinConnector::create(){
  287. if( m_pSettings!= NULL ) {
  288. config_setting_t *pSetting;
  289. int tVal;
  290. char tBuffer[256];
  291. CSTR tcVal = NULL;
  292. if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&tcVal )){ //(const char**)&m_cBindAddress)) {
  293. strncpy( m_cBindAddress, "10.0.0.1", 15 );
  294. pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
  295. config_setting_set_string(pSetting, m_cBindAddress);
  296. basicSettingsProvider::getInstance()->compromise();
  297. } else {
  298. strncpy( m_cBindAddress, tcVal, 16 );
  299. sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_cBindAddress );
  300. LOG( tBuffer );
  301. };
  302. if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
  303. tVal = m_nBindPort = 6564;
  304. pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
  305. config_setting_set_bool( pSetting, tVal );
  306. basicSettingsProvider::getInstance()->compromise();
  307. } else {
  308. m_nBindPort = tVal;
  309. sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
  310. LOG( tBuffer );
  311. };
  312. if( basicSettingsProvider::getInstance()->isCompromised() ) {
  313. LOG("Save Settings");
  314. basicSettingsProvider::getInstance()->validate();
  315. };
  316. } else {
  317. LOG_ERROR("No Settings Provided!");
  318. return false;
  319. };
  320. int RetCode;
  321. struct hostent * h_info;
  322. struct sockaddr_in * MainSocketAddress = NULL;
  323. if( !protocolInternalLRF3D_MASTER::create() ) return false;
  324. m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  325. if( m_nMainSocket == -1){
  326. LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
  327. return false;
  328. };
  329. MainSocketAddress = ( struct sockaddr_in * ) malloc( sizeof( struct sockaddr_in ) );
  330. if( MainSocketAddress == NULL ){
  331. LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  332. return false;
  333. };
  334. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  335. h_info = gethostbyname ( m_cBindAddress );
  336. if ( h_info == NULL ){
  337. LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
  338. return false;
  339. };
  340. //*****************************************
  341. //* there we building our network address *
  342. //*****************************************
  343. bcopy( h_info->h_addr, &(MainSocketAddress->sin_addr), h_info->h_length );
  344. MainSocketAddress->sin_family = h_info->h_addrtype;
  345. MainSocketAddress->sin_port = htons( m_nBindPort );
  346. RetCode = bind( m_nMainSocket ,(struct sockaddr *) MainSocketAddress, sizeof( struct sockaddr_in ) );
  347. if ( RetCode != 0 ){
  348. LOG_ERROR( "Error Binding to Port/Address" );
  349. return false;
  350. };
  351. RetCode = listen( m_nMainSocket, 128);
  352. if ( RetCode != 0 ){
  353. LOG_ERROR( "Cannot Listen Port/Address" );
  354. return false;
  355. };
  356. U8 dontblock;
  357. dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
  358. dontblock |= O_NONBLOCK | O_NDELAY;
  359. RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
  360. if ( RetCode == 0 ){
  361. LOG( "Set N-Blocking Mode of Accepting Socket" );
  362. } else {
  363. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  364. };
  365. m_eWM = TWI_STAGE_WAITING_CONNECTION;
  366. return init();
  367. };
  368. bool LRF_TwinConnector::packetHandler( protocolPackage *Package ){
  369. char tB[128];
  370. if( Package == NULL ) return false;
  371. if( Package->m_pData == NULL ) return false;
  372. if( Package->m_nSize < TWI_CMD_INDEX ) return false;
  373. TWIN_COMMAND tCMD = (TWIN_COMMAND) Package->m_pData[TWI_CMD_INDEX];
  374. sprintf(tB,"Data IN! Code: %d", tCMD );
  375. LOG(tB);
  376. switch( tCMD ) {
  377. case TWC_ACK_CMD:{
  378. LOG( "CMD ACK");
  379. TWIN_COMMAND confirmedCMD = (TWIN_COMMAND) Package->m_pData[TWI_PLS_INDEX];
  380. if( getCMD() != confirmedCMD ) {
  381. setWM( TWI_STAGE_RETRY_CMD );
  382. } else {
  383. //Last Command Confirmed -> Clean
  384. setCMD( TWC_NO_COMMAND );
  385. switch ( confirmedCMD ) {
  386. case TWC_DO_REBOOT:{
  387. //After Confirmation -> Disconnect
  388. setWM( TWI_STAGE_DISCONNECTING );
  389. break;
  390. };
  391. case TWC_SET_EVT_MODE:{
  392. setWM( TWI_STAGE_EVENT_MODE );
  393. break;
  394. };
  395. case TWC_SET_CMD_MODE:{
  396. setWM( TWI_STAGE_REQUEST_STATUS );
  397. break;
  398. };
  399. };
  400. };
  401. break;
  402. };
  403. case TWC_CURRENT_MODE:{
  404. TWI_WM tWM = ( TWI_WM ) Package->m_pData[TWI_PLS_INDEX];
  405. sprintf(tB,"Cur Slave Mode Is: %d", tWM );
  406. LOG(tB);
  407. if ( tWM == TWI_STAGE_EVENT_MODE ) {
  408. setWM( TWI_STAGE_SET_COMMAND_MODE );
  409. } else {
  410. setWM( TWI_STAGE_REQUEST_STATUS );
  411. };
  412. break;
  413. };
  414. case TWC_TH_DATA:{
  415. if( getWM() == TWI_STAGE_TH_TRANSFER ) {
  416. };
  417. break;
  418. };
  419. case TWC_CURRENT_STATUS:{
  420. LOG( "Cur Status");
  421. setWM( TWI_STAGE_REQUEST_STATUS );
  422. break;
  423. };
  424. case TWC_CURRENT_VERSION:{
  425. LOG( "CUr Version");
  426. m_sSystemStatus.SLAVE_STATUS.KRNL_WM = WM_INIT; // At least it Works;
  427. memcpy( &m_sSystemStatus.SLAVE_NFO, Package->m_pData + TWI_PLS_INDEX, sizeof(SOFTWARE_INFO));
  428. setWM( TWI_STAGE_REQUEST_MODE );
  429. break;
  430. };
  431. case TWC_PUSH_TARGET:{
  432. LOG( "Target");
  433. if( m_pTracker ) {
  434. targetRecord *tTARGET = (targetRecord * ) ( Package->m_pData + TWI_PLS_INDEX );
  435. //All Targets Came From Slave
  436. m_pTracker->validateTarget( tTARGET, false );
  437. };
  438. break;
  439. };
  440. };
  441. return true;
  442. };
  443. void LRF_TwinConnector::m_fThreadProc( void ){
  444. int ClientSocket = -1;
  445. struct sockaddr ClientAddress;
  446. socklen_t ClientAddressSize;
  447. long long cMilliseconds;
  448. long long cStatusTO = 0;
  449. long long cDrawTO = 0;
  450. m_bThreadStatus = TH_WORKING;
  451. while( m_bThreadStatus < TH_ENDED ){
  452. cMilliseconds = getMilliseconds();
  453. /*
  454. // TH Saving procedure
  455. if( cDrawTO == 0 ) {
  456. cDrawTO = cMilliseconds;
  457. } else if ( ( cMilliseconds - cDrawTO ) > TWI_DRAW_LOOP_MS ){
  458. #ifdef DEBUG
  459. LOG("Try to Dump TH Images");
  460. #endif
  461. if( m_mDrawProtector.try_lock() ) {
  462. #ifdef DEBUG
  463. LOG("Mutex Grabed");
  464. #endif
  465. if( m_bDrawThreadStatus > TH_ARMED ) {
  466. pthread_join( m_sDrawThreadHandler, NULL );
  467. };
  468. cDrawTO = cMilliseconds;
  469. m_bDrawThreadStatus = TH_ARMED;
  470. int RetVal = pthread_create( &m_sDrawThreadHandler, NULL, m_fDrawThreadEntryPoint, ( void * ) this );
  471. if( RetVal == -1 ) {
  472. LOG_ERROR("Image Draw Thread Creation Failed.");
  473. m_mDrawProtector.unlock();
  474. };
  475. };
  476. };
  477. */
  478. // Port exists - SLAVE is connected
  479. if( m_oDataTransport != NULL ){
  480. // Check if still connected
  481. if( m_oDataTransport->getConnectionStatus() == CS_DISCONNECTED ){
  482. // Handle Disconnection
  483. LOG("HANDLE DISCONNECT!");
  484. setWM( TWI_STAGE_WAITING_CONNECTION );
  485. delete( (serverPort *) m_oDataTransport );
  486. m_oDataTransport = NULL;
  487. //Reset Flags
  488. cStatusTO = 0;
  489. continue;
  490. };
  491. } else {
  492. serverPort *tPort = NULL;
  493. // (re)Establish Connection
  494. ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
  495. ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
  496. if ( ClientSocket == -1 ) {
  497. switch( errno ){
  498. case EAGAIN:{
  499. SLEEP( TWI_QUICK_LOOP_SLEEP );
  500. continue;
  501. };
  502. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  503. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  504. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  505. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  506. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  507. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  508. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  509. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  510. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  511. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  512. case ENOBUFS:
  513. 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;
  514. case EPROTO: LOG_ERROR( "Protocol error."); break;
  515. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  516. case ENOSR:
  517. case ESOCKTNOSUPPORT:
  518. case EPROTONOSUPPORT:
  519. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  520. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  521. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  522. };
  523. m_bThreadStatus = TH_ENDED;
  524. } else {
  525. char tstr[CQUERY];
  526. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, CQUERY );
  527. LOG( "SLAVE LRF Connection Accepted" );
  528. tPort = new serverPort( ClientSocket );
  529. };
  530. if( tPort != NULL ){
  531. if ( tPort->create() ) {
  532. setTransport(tPort);
  533. // Reset Stage
  534. LOG("State->Connected");
  535. setWM( TWI_STAGE_CONNECTED );
  536. } else {
  537. LOG_ERROR("Failure Create Server Connection!");
  538. delete( tPort );
  539. };
  540. };
  541. };
  542. switch ( getWM() ) {
  543. case TWI_STAGE_DISCONNECTING:{
  544. delete( (serverPort *) m_oDataTransport );
  545. m_oDataTransport = NULL;
  546. setWM( TWI_STAGE_WAITING_CONNECTION );
  547. //Reset Flags
  548. cStatusTO = 0;
  549. break;
  550. };
  551. case TWI_STAGE_CONNECTED:{
  552. if( sendPlainCMD( TWC_GET_VERSION ) ){
  553. setCMD( TWC_GET_VERSION );
  554. setWM( TWI_STAGE_RESPONCE_WAITING );
  555. };
  556. break;
  557. };
  558. case TWI_STAGE_REQUEST_MODE:{
  559. if ( sendPlainCMD( TWC_GET_MODE ) ) {
  560. setCMD( TWC_GET_MODE );
  561. setWM( TWI_STAGE_RESPONCE_WAITING );
  562. };
  563. break;
  564. };
  565. case TWI_STAGE_SET_COMMAND_MODE:{
  566. if ( sendPlainCMD( TWC_SET_CMD_MODE ) ) {
  567. setCMD( TWC_SET_CMD_MODE );
  568. setWM( TWI_STAGE_RESPONCE_WAITING );
  569. };
  570. break;
  571. };
  572. case TWI_STAGE_REQUEST_STATUS:{
  573. if( sendPlainCMD( TWC_GET_STATUS ) ){
  574. setWM( TWI_STAGE_RESPONCE_WAITING );
  575. };
  576. break;
  577. };
  578. case TWI_STAGE_TIME_SYNC_MODE:{
  579. if ( sendPlainCMD( TWC_INIT_TSINC ) ) {
  580. setWM( TWI_STAGE_RESPONCE_WAITING );
  581. };
  582. break;
  583. };
  584. case TWI_STAGE_RETRY_CMD:{
  585. if( getCMD() ){
  586. m_bCMDTO = 10;
  587. if( sendPlainCMD(getCMD()) ){
  588. setWM( TWI_STAGE_RESPONCE_WAITING );
  589. };
  590. } else setWM( TWI_STAGE_REQUEST_MODE );
  591. break;
  592. };
  593. case TWI_STAGE_RESPONCE_WAITING:{
  594. if ( m_bCMDTO ) {
  595. m_bCMDTO--;
  596. SLEEP(TWI_SLOW_LOOP_SLEEP);
  597. } else setWM( TWI_STAGE_RETRY_CMD );
  598. break;
  599. };
  600. };
  601. };
  602. };
  603. bool LRF_TwinConnector::attachTargetTracker( LRF_TargetTracker *ptr ){
  604. if(ptr == NULL) return false;
  605. m_pTracker = ptr;
  606. return false;
  607. };
  608. bool LRF_TwinConnector::updateSettings( void ){
  609. return true;
  610. };
  611. #endif //!PRU_SLAVE
  612. TWI_CMD_CONTAINER *LRF_TwinConnector::fetchCMD(){
  613. TWI_CMD_CONTAINER *retVal = NULL;
  614. m_mQUEUEProtector.lock();
  615. if( m_qCMD.size() ) {
  616. retVal = m_qCMD.front();
  617. m_qCMD.pop();
  618. };
  619. m_mQUEUEProtector.unlock();
  620. return retVal;
  621. };
  622. void LRF_TwinConnector::pushCMD( TWI_CMD_CONTAINER *ptr ){
  623. if ( ptr ) {
  624. m_mQUEUEProtector.lock();
  625. m_qCMD.push(ptr);
  626. m_mQUEUEProtector.unlock();
  627. };
  628. };
  629. void LRF_TwinConnector::pushCMD( TWIN_COMMAND CMD, void *ptr, size_t DataSize ){
  630. TWI_CMD_CONTAINER *tVal = (TWI_CMD_CONTAINER *) malloc(sizeof(TWI_CMD_CONTAINER));
  631. if (tVal) {
  632. tVal->CMD = CMD;
  633. tVal->DATA = ptr;
  634. tVal->SIZE = DataSize;
  635. m_mQUEUEProtector.lock();
  636. m_qCMD.push(tVal);
  637. m_mQUEUEProtector.unlock();
  638. };
  639. };
  640. #ifdef PRU_SLAVE
  641. bool LRF_TwinConnector::validateTarget( targetRecord *ptr ){
  642. if( ptr == NULL ) return false;
  643. pushCMD( TWC_PUSH_TARGET, ptr, sizeof( targetRecord ) );
  644. return true;
  645. };
  646. #endif
  647. bool LRF_TwinConnector::cleanTHData( uint8_t kid ){
  648. char tBuffer[1024];
  649. FILE *FID = NULL;
  650. if( kid > 31 ) kid = 31;
  651. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  652. if( !fileExist( "data" ) ){
  653. int RetVal = mkdir( "data", STORAGE_DIR_MODE );
  654. if( RetVal < 0 ) {
  655. LOG_ERROR( "Error Creating DATA DIR!" );
  656. return false;
  657. };
  658. };
  659. FID = fopen( tBuffer, "wb+" );
  660. if( FID == NULL ){
  661. LOG_ERROR( "Error Creating DATA FILE!" );
  662. return false;
  663. };
  664. uint32_t ZZ = 0;
  665. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  666. fwrite( &ZZ, 4, 1, FID );
  667. };
  668. fclose( FID );
  669. return true;
  670. };
  671. bool LRF_TwinConnector::loadTHData( uint8_t kid, thresholdNode *ptr ){
  672. FILE *FID;
  673. char tBuffer[1024];
  674. if( ptr == NULL ) return false;
  675. if( kid > 31 ) kid = 31;
  676. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  677. FID = fopen( tBuffer, "rb" );
  678. if( FID == NULL ) {
  679. LOG_ERROR( "Error Opening DATA FILE!" );
  680. return false;
  681. };
  682. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  683. fread( &ptr[i], TH_NODE_SIZE, 1, FID );
  684. };
  685. fclose( FID );
  686. return true;
  687. };
  688. bool LRF_TwinConnector::saveTHData( uint8_t kid, thresholdNode *ptr ){
  689. FILE *FID;
  690. char tBuffer[1024];
  691. if( ptr == NULL ) return false;
  692. if( kid > 31 ) kid = 31;
  693. sprintf( tBuffer, "data/th_data_%d.raw", kid );
  694. FID = fopen( tBuffer, "wb+" );
  695. if( FID == NULL ) {
  696. LOG_ERROR( "Error Creating DATA FILE!" );
  697. return false;
  698. };
  699. for( uint32_t i = 0; i < TH_SIZE_NODES; i++ ){
  700. fwrite( &ptr[i], TH_NODE_SIZE, 1, FID );
  701. };
  702. fclose( FID );
  703. return true;
  704. };
  705. void *LRF_TwinConnector::m_fDrawThreadEntryPoint( void *ptr ){
  706. LRF_TwinConnector *Me;
  707. if( ptr == NULL ) return NULL;
  708. Me = (LRF_TwinConnector*) ptr;
  709. #ifdef DEBUG
  710. SLOG("TH Thread OK");
  711. #endif
  712. if( Me->m_bDrawThreadStatus == TH_ARMED ) {
  713. #ifdef DEBUG
  714. SLOG("TH Image Thread ARMED!");
  715. #endif
  716. Me->m_bDrawThreadStatus = TH_WORKING;
  717. if( Me!= NULL ) {
  718. // Store Local TH Draws
  719. thresholdNode *tMem;
  720. //uint32_t tSize;
  721. #ifdef DEBUG
  722. SLOG("Kernel is Defined!");
  723. #endif
  724. for( uint8_t i=0; i<16; i++ ) {
  725. // just in case if we must end work
  726. if( Me->m_bDrawThreadStatus != TH_WORKING ) break;
  727. //tMem = Me->getTreshold( tSize, i );
  728. tMem = Me->m_pCurrentTH;
  729. if( tMem!= NULL ) {
  730. char tBuffer[256];
  731. #ifdef TH_USE_PICTURES
  732. sprintf( tBuffer, "data/th_data_%d.png", i );
  733. #ifdef DEBUG
  734. SLOG("SAVE TH Image ...");
  735. SLOG( tBuffer );
  736. #endif
  737. if( !Me->saveTHPicture( tMem, tBuffer, false )){
  738. SLOG_ERROR("TH PNG save failure!");
  739. }
  740. #ifdef PRU_SLAVE
  741. else {
  742. //Me->m_mStatusProtector.lock();
  743. //Me->m_nStatus |= ( 1 << i ) << 16;
  744. //Me->m_mStatusProtector.unlock();
  745. }
  746. #endif
  747. ;
  748. #ifdef DEBUG
  749. SLOG("SAVE TH Image DONE!");
  750. #endif
  751. SLEEP(10);
  752. #else //TH_USE_PICTURES
  753. sprintf( tBuffer, "data/th_data_%d.raw", i );
  754. #ifdef DEBUG
  755. SLOG("SAVE TH Data ...");
  756. SLOG( tBuffer );
  757. #endif
  758. FILE *fd;
  759. fd = fopen( tBuffer, "wb+");
  760. if( fd != NULL ){
  761. uint32_t Size = TH_SIZE_NODES;
  762. fwrite( &Size, sizeof( uint32_t ), 1, fd );
  763. fwrite( tMem, sizeof( thresholdNode ), Size, fd );
  764. fclose( fd );
  765. #ifdef PRU_SLAVE
  766. //Me->m_mStatusProtector.lock();
  767. //Me->m_nStatus |= ( 1 << i ) << 16;
  768. //Me->m_mStatusProtector.unlock();
  769. #endif
  770. } else {
  771. SLOG_ERROR("TH RAW save failure!");
  772. };
  773. #ifdef DEBUG
  774. SLOG("SAVE TH Data DONE!");
  775. #endif
  776. #endif //TWI_TH_USE_PICTURES
  777. };
  778. };
  779. };
  780. };
  781. Me->m_bDrawThreadStatus = TH_EXIT;
  782. Me->m_mDrawProtector.unlock();
  783. pthread_exit( NULL );
  784. return NULL;
  785. };
  786. void LRF_TwinConnector::waitDrawThreadEnd( bool forced ){
  787. if( m_bDrawThreadStatus > TH_WORKING || m_bDrawThreadStatus < TH_ARMED ) return;
  788. if( forced ) m_bDrawThreadStatus = TH_ENDED;
  789. while( m_bDrawThreadStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  790. pthread_join( m_sDrawThreadHandler, NULL );
  791. };
  792. #ifdef TH_USE_PICTURES
  793. #define STB_IMAGE_IMPLEMENTATION
  794. #include "../../data/stbi/stb_image.h"
  795. #define STB_IMAGE_WRITE_IMPLEMENTATION
  796. #include "../../data/stbi/stb_image_write.h"
  797. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  798. #undef STB_IMAGE_IMPLEMENTATION
  799. bool LRF_TwinConnector::saveTHPicture( thresholdNode *ptr, CSTR fileName, bool monochrome, bool agressive ){
  800. uint8_t *tMemory = NULL;
  801. thresholdNode *cNode = NULL;
  802. uint8_t *cPixel = NULL;
  803. uint8_t cSpace;
  804. if( monochrome ) {
  805. cSpace = 1;
  806. } else {
  807. cSpace = 4;
  808. };
  809. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  810. if( tMemory == NULL ){
  811. LOG_ERROR("Image Memory Alloc Eror!");
  812. return false;
  813. };
  814. for( int j = 0; j < Q_SCAN_COUNT; j++ ) {
  815. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  816. //cNode = ptr+(j*i);
  817. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  818. #ifdef PRU_SLAVE
  819. cNode = ptr+makeIds( i, j , false );
  820. cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  821. #else
  822. cNode = ptr+makeIds( i, j , true );
  823. cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  824. //cNode = ptr + ( i * j );
  825. //cPixel = tMemory + ( cSpace * i * j );
  826. #endif
  827. if( !agressive ) SLEEP(1);
  828. if( monochrome ) {
  829. #ifdef CHECK_SUN_BIT
  830. if( cNode->values.sbit ) {
  831. cPixel[0] = 0;
  832. } else {
  833. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  834. };
  835. #else
  836. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  837. #endif
  838. } else {
  839. #ifdef CHECK_SUN_BIT
  840. if( cNode->values.sbit ) {
  841. // Set Red
  842. cPixel[0] = 0x00;
  843. cPixel[1] = 0xff;
  844. cPixel[2] = 0x00;
  845. cPixel[3] = 0xff;
  846. } else {
  847. if( cNode->values.cnt == 0 ) {
  848. // Set Blue
  849. cPixel[0] = 0x00;
  850. cPixel[1] = 0x00;
  851. cPixel[2] = 0xff;
  852. cPixel[3] = 0xff;
  853. } else {
  854. uint32_t tColor;
  855. float tVal = ((float) cNode->values.min ) / 4096.f;
  856. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  857. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  858. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  859. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  860. cPixel[3] = tColor & 0xFF;
  861. };
  862. };
  863. #else
  864. if( cNode->values.cnt == 0 ) {
  865. // Set Blue
  866. cPixel[0] = 0x00;
  867. cPixel[1] = 0x00;
  868. cPixel[2] = 0xff;
  869. cPixel[3] = 0xff;
  870. } else {
  871. uint32_t tColor;
  872. float tVal = ((float) cNode->values.min ) / 4096.f;
  873. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  874. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  875. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  876. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  877. cPixel[3] = tColor & 0xFF;
  878. };
  879. #endif
  880. };
  881. };
  882. };
  883. int RetVal = stbi_write_png( fileName, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  884. free( tMemory );
  885. if( RetVal == 0 ) return false;
  886. return true;
  887. };
  888. #endif //TWI_TH_USE_PICTURES
  889. #undef SLOG
  890. #undef SLOG_ERROR
  891. #undef LOG
  892. #undef LOG_ERROR