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.

369 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolInternalLRF3D.h"
  2. #define LOG( X ) m_pLog->Log( "protocolLRF3D", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG234[256];\
  6. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, _tBfrErrMSG234 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, X );
  11. #endif
  12. protocolInternalLRF3D::protocolInternalLRF3D(): protocolParser(){
  13. m_pLog = basicLogCollection::getInstance();
  14. };
  15. protocolInternalLRF3D::~protocolInternalLRF3D(){
  16. };
  17. int protocolInternalLRF3D::packetChecker( U8 *Buffer, size_t BufferSize ){
  18. #ifdef DUBUG_COMMUNICATION
  19. char tmp[32];
  20. int i;
  21. for( i=0;i< BufferSize; i++ ) {
  22. sprintf( tmp, " 0x%02X", Buffer[i] );
  23. LOG(tmp);
  24. };
  25. #endif
  26. if( Buffer[0] == 0xAA ){
  27. if( BufferSize > 5 ) {
  28. #ifdef PRU_SLAVE
  29. if ((Buffer[1] & 0x80) == 0x80 ){
  30. #else
  31. if ((Buffer[1] & 0x80) == 0x00 ){
  32. #endif
  33. size_t pLength;
  34. pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
  35. if( BufferSize >= ( pLength + 4 ) ) {
  36. U8 pCheckSum;
  37. pCheckSum = checksumLGS( Buffer, pLength+3 );
  38. if( Buffer[pLength+3] == pCheckSum ) {
  39. #ifdef DUBUG_COMMUNICATION
  40. LOG((CSTR)"PacketChecker: Packet Is Recognized (0)");
  41. #endif
  42. return 0; // Ok
  43. } else {
  44. #ifdef DUBUG_COMMUNICATION
  45. LOG((CSTR)"PacketChecker: Packet Not Recognized CHK SUM(-1)");
  46. #endif
  47. return -1; // To shift
  48. };
  49. };
  50. } else {
  51. #ifdef DUBUG_COMMUNICATION
  52. LOG((CSTR)"PacketChecker: Packet Not Recognized TAG (-1)");
  53. #endif
  54. return -1; // To shiftSeverity Code Description Project File Line Suppression State
  55. };
  56. };
  57. #ifdef DUBUG_COMMUNICATION
  58. LOG((CSTR)"PacketChecker: Packet Not Recognized SMALL SIZE(1)");
  59. #endif
  60. return 1; // To grow
  61. };
  62. #ifdef DUBUG_COMMUNICATION
  63. LOG((CSTR)"PacketChecker: Packet Not Recognized START TAG(-1)");
  64. #endif
  65. return -1; // To shift
  66. };
  67. protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t BufferSize ){
  68. protocolPackage *RetVal;
  69. #ifdef DUBUG_COMMUNICATION
  70. char tmp[32];
  71. int i;
  72. for( i=0;i< BufferSize; i++ ) {
  73. sprintf( tmp, " 0x%02X", Buffer[i] );
  74. LOG(tmp);
  75. };
  76. #endif
  77. if( Buffer[0] == 0xAA ){
  78. if( BufferSize > 5 ){
  79. #ifdef PRU_SLAVE
  80. if ((Buffer[1] & 0x80) == 0x80 ) {
  81. #else
  82. if ((Buffer[1] & 0x80) == 0x00 ) {
  83. #endif
  84. size_t pLength;
  85. pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
  86. if( BufferSize >= ( pLength+4 ) ) {
  87. U8 pCheckSum;
  88. pCheckSum = checksumLGS(Buffer, pLength+3 );
  89. if( Buffer[pLength+3] == pCheckSum ) {
  90. RetVal = (protocolPackage *) malloc(sizeof(protocolPackage));
  91. if( RetVal == NULL ) return NULL;
  92. RetVal->m_pData =(U8*)malloc( pLength+4 );
  93. if( RetVal->m_pData == NULL ) {
  94. free( RetVal );
  95. return NULL;
  96. };
  97. RetVal->m_nSize = pLength+4;
  98. RetVal->m_nCode = Buffer[CMD_IDX];
  99. memcpy( RetVal->m_pData, Buffer, RetVal->m_nSize );
  100. #ifdef DUBUG_COMMUNICATION
  101. LOG((CSTR)"PacketExtractor: Packet Is Extracted (0)");
  102. #endif
  103. return RetVal;
  104. };
  105. };
  106. } else {
  107. LOG_ERROR("TAG mismatch!");
  108. };
  109. };
  110. #ifdef DUBUG_COMMUNICATION
  111. LOG((CSTR)"PacketExtractor: Packet Not Extracted(1)!!!");
  112. #endif
  113. return NULL;
  114. };
  115. #ifdef DUBUG_COMMUNICATION
  116. LOG((CSTR)"PacketExtractor: Packet Not Extracted(-1)");
  117. #endif
  118. return NULL;
  119. };
  120. #undef LOG
  121. #undef LOG_ERROR
  122. #ifdef PRU_SLAVE
  123. #define LOG( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, X )
  124. #ifdef DEBUG
  125. #define LOG_ERROR( X ) {\
  126. char _tBfrErrMSG234[256];\
  127. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  128. m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, _tBfrErrMSG234 );\
  129. }
  130. #else
  131. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, X );
  132. #endif
  133. protocolInternalLRF3D_SLAVE::protocolInternalLRF3D_SLAVE( config_setting_t *Settings) : protocolInternalLRF3D() {
  134. m_pLog = basicLogCollection::getInstance();
  135. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"MASTER_LRF_CONNECTION" );
  136. m_bFailureReason = 0;
  137. m_bSettingsIsLoadded = false;
  138. m_pMasterAddress = (CSTR) malloc( 16 );
  139. m_pPort = NULL;
  140. };
  141. protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){
  142. if( m_pMasterAddress != NULL ) free( m_pMasterAddress );
  143. };
  144. bool protocolInternalLRF3D_SLAVE::create( void ){
  145. config_setting_t *pSetting;
  146. int tVal;
  147. char tBuffer[256];
  148. m_bFailureReason = 1;
  149. if( m_pSettings == NULL ) return false;
  150. if ( m_pMasterAddress == NULL ) return false;
  151. if ( !m_bSettingsIsLoadded ){
  152. if (!config_setting_lookup_string(m_pSettings, "MASTER_ADDRESS", (const char**)&m_pMasterAddress)) {
  153. strncpy( m_pMasterAddress, "10.0.0.1", 15 );
  154. pSetting = config_setting_add(m_pSettings, "MASTER_ADDRESS", CONFIG_TYPE_STRING);
  155. config_setting_set_string(pSetting, m_pMasterAddress);
  156. basicSettingsProvider::getInstance()->compromise();
  157. } else {
  158. sprintf( tBuffer, "MASTER IP Address Read From Settings: %s", m_pMasterAddress );
  159. LOG( tBuffer );
  160. };
  161. if( !config_setting_lookup_int( m_pSettings, "MASTER_PORT", &tVal ) ){
  162. tVal = m_nMasterPort = 6564;
  163. pSetting = config_setting_add( m_pSettings, "MASTER_PORT", CONFIG_TYPE_INT );
  164. config_setting_set_bool( pSetting, tVal );
  165. basicSettingsProvider::getInstance()->compromise();
  166. } else {
  167. m_nMasterPort = tVal;
  168. sprintf( tBuffer, "MASTER PORT Read From Settings: %d", tVal );
  169. LOG( tBuffer );
  170. };
  171. //basicSettingsProvider::getInstance()->validate();
  172. m_bSettingsIsLoadded = true;
  173. };
  174. m_bFailureReason = 2;
  175. if( m_pPort == NULL ){
  176. m_pPort = new clientPort( m_pMasterAddress, m_nMasterPort, true );
  177. if( m_pPort == NULL ) return false;
  178. m_pPort->addRecipient( this );
  179. m_bFailureReason = 3;
  180. if( m_pPort->getConnectionStatus() != CS_CONNECTED ) return false;
  181. } else {
  182. m_bFailureReason = 3;
  183. if( !m_pPort->connectTo( m_pMasterAddress, m_nMasterPort ) ) return false;
  184. };
  185. m_bFailureReason = 0;
  186. return true;
  187. };
  188. bool protocolInternalLRF3D_SLAVE::sendTrasholdData( uint8_t lrf_id, uint32_t dataIdx ){
  189. };
  190. bool protocolInternalLRF3D_SLAVE::sendTargets( void ){
  191. };
  192. bool protocolInternalLRF3D_SLAVE::sendStatus( void ){
  193. if( m_oDataTransport == NULL ) return false;
  194. uint8_t Buffer[16];
  195. Buffer[3] = 0xDC;
  196. Buffer[4] = ( m_nStatus >> 24 ) & 0xFF;
  197. Buffer[5] = ( m_nStatus >> 16 ) & 0xFF;
  198. Buffer[6] = ( m_nStatus >> 8 ) & 0xFF;
  199. Buffer[7] = m_nStatus & 0xFF;
  200. #ifdef DEBUG
  201. LOG("setStatus");
  202. #endif
  203. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
  204. };
  205. #else
  206. #define LOG( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, X )
  207. #ifdef DEBUG
  208. #define LOG_ERROR( X ) {\
  209. char _tBfrErrMSG234[256];\
  210. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  211. m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, _tBfrErrMSG234 );\
  212. }
  213. #else
  214. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, X );
  215. #endif
  216. bool protocolInternalLRF3D_MASTER::create( void ){
  217. config_setting_t *pSetting;
  218. int tVal;
  219. char tBuffer[256];
  220. m_bFailureReason = 1;
  221. if( m_pSettings == NULL ) return false;
  222. if ( m_pBindAddress == NULL ) return false;
  223. if ( !m_bSettingsIsLoadded ){
  224. if (!config_setting_lookup_string(m_pSettings, "BIND_ADDRESS", (const char**)&m_pBindAddress)) {
  225. strncpy( m_pBindAddress, "10.0.0.1", 15 );
  226. pSetting = config_setting_add(m_pSettings, "BIND_ADDRESS", CONFIG_TYPE_STRING);
  227. config_setting_set_string(pSetting, m_pBindAddress);
  228. basicSettingsProvider::getInstance()->compromise();
  229. } else {
  230. sprintf( tBuffer, "BIND to IP Address Read From Settings: %s", m_pBindAddress );
  231. LOG( tBuffer );
  232. };
  233. if( !config_setting_lookup_int( m_pSettings, "BIND_PORT", &tVal ) ){
  234. tVal = m_nBindPort = 6564;
  235. pSetting = config_setting_add( m_pSettings, "BIND_PORT", CONFIG_TYPE_INT );
  236. config_setting_set_bool( pSetting, tVal );
  237. basicSettingsProvider::getInstance()->compromise();
  238. } else {
  239. m_nBindPort = tVal;
  240. sprintf( tBuffer, "BIND to PORT Read From Settings: %d", tVal );
  241. LOG( tBuffer );
  242. };
  243. //basicSettingsProvider::getInstance()->validate();
  244. m_bSettingsIsLoadded = true;
  245. };
  246. m_bFailureReason = 2;
  247. if( m_pPort == NULL ){
  248. struct sockaddr ClientAddress;
  249. socklen_t size;
  250. int ClientSocket;
  251. size = (socklen_t) sizeof( struct sockaddr_in );
  252. ClientSocket = accept ( MainSocket, &ClientAddress, &size );
  253. if ( ClientSocket == -1 ) {
  254. switch( errno ){
  255. case EAGAIN:{
  256. // Checkup client disconnection
  257. if( m_vHandlersPool.size()) {
  258. for( vectorNetHandlers::iterator index = m_vHandlersPool.begin(); index < m_vHandlersPool.end(); index++ ) {
  259. LRF_ClientHandler *tClient = (LRF_ClientHandler *)(*index);
  260. if( tClient->getConnectionStatus() == CS_DISCONNECTED ){
  261. // Remove Client Handler
  262. LOG("Remove Client");
  263. delete( tClient );
  264. m_vHandlersPool.erase( index );
  265. LOG("Client Stoped");
  266. LOG("Remove Client Done");
  267. };
  268. };
  269. };
  270. SLEEP(10);
  271. continue;
  272. };
  273. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  274. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  275. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  276. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  277. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  278. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  279. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  280. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  281. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  282. case EFAULT: LOG_ERROR( "The addr argument is not in a writeable part of the user address space."); break;
  283. case ENOBUFS:
  284. 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;
  285. case EPROTO: LOG_ERROR( "Protocol error."); break;
  286. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  287. case ENOSR:
  288. case ESOCKTNOSUPPORT:
  289. case EPROTONOSUPPORT:
  290. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  291. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  292. default: LOG_ERROR( " Unhanded error in Kernel"); break;
  293. };
  294. m_bThreadStatus = TH_ENDED;
  295. } else {
  296. if ( ClientSocket >= 0 ) {
  297. if( m_bConnectionType == 1 ){
  298. char tstr[CQUERY];
  299. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  300. };
  301. LOG( "Client Connection Accepted" );
  302. m_pPort = new serverPort( ClientSocket );
  303. } else {
  304. LOG_ERROR( "Client Socket Error" );
  305. switch( errno ){
  306. //case EAGAIN: continue;
  307. case EWOULDBLOCK: LOG_ERROR( " The socket is marked non-blocking and no connections are present to be accepted." ); break;
  308. case EBADF: LOG_ERROR( " The descriptor is invalid." ); break;
  309. case ECONNABORTED: LOG_ERROR( " A connection has been aborted." ); break;
  310. case EINTR:{// If Interrupted by Signal - No Harm was done
  311. LOG_ERROR( " The system call was interrupted by a signal that was caught before a valid connection arrived." );
  312. continue;
  313. };
  314. case EINVAL: LOG_ERROR( " Socket is not listening for connections, or addrlen is invalid (e.g., is negative)." ); break;
  315. case EMFILE: LOG_ERROR( " The per-process limit of open file descriptors has been reached."); break;
  316. case ENFILE: LOG_ERROR( " The system limit on the total number of open files has been reached."); break;
  317. case ENOTSOCK: LOG_ERROR( " The descriptor references a file, not a socket."); break;
  318. case EOPNOTSUPP: LOG_ERROR( " The referenced socket is not of type SOCK_STREAM." ); break;
  319. case EFAULT: LOG_ERROR( " The addr argument is not in a writable part of the user address space." ); break;
  320. case ENOBUFS:
  321. 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;
  322. case EPROTO: LOG_ERROR( " Protocol error." ); break;
  323. case EPERM: LOG_ERROR( " Firewall rules forbid connection." ); break;
  324. case ENOSR:
  325. case ESOCKTNOSUPPORT:
  326. case EPROTONOSUPPORT:
  327. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)" ); break;
  328. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  329. default: LOG_ERROR( " Unhandled error in Kernel" ); break;
  330. };
  331. return false;
  332. };
  333. };
  334. if( m_pPort == NULL ) return false;
  335. m_pPort->addRecipient( this );
  336. m_bFailureReason = 3;
  337. if( m_pPort->getConnectionStatus() != CS_CONNECTED ) return false;
  338. } else {
  339. m_bFailureReason = 3;
  340. if( !m_pPort->connectTo( m_pMasterAddress, m_nMasterPort ) ) return false;
  341. };
  342. m_bFailureReason = 0;
  343. return true;
  344. };
  345. #endif
  346. #undef LOG
  347. #undef LOG_ERROR