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.

706 lines
23 KiB

5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseNodeEthernet2Serial.h"
  2. #include "rgooseHardwareManager.h"
  3. #include "../data/protocol/protocol.h"
  4. #define LOG( X ) m_pLog->Log( "E2UART", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG2w34[256];\
  8. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "E2UART", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "E2UART", this, _LOG_ERROR, X );
  13. #endif
  14. ARGUS_ETH2UART::ARGUS_ETH2UART( uint32_t nodeID ) : argusNetNode( nodeID ), argusHardwareResource(), basicTransport(), basicDualThread(){
  15. m_pLog = basicLogCollection::getInstance();
  16. memset( &m_sUartData, 0, sizeof(m_sUartData));
  17. m_nInputBufferSize = 0;
  18. m_nOutputBufferSize = 0;
  19. m_iDeviceFD = -1;
  20. m_iMasterFD = -1;
  21. m_iInFID = -1;
  22. m_iOutFID = -1;
  23. LOG("CREATE res!");
  24. argusHardwareResource::create( m_sNodeData.aux_parameter, ARGUS_HARDWARE_PROVIDER_SERIAL );
  25. };
  26. ARGUS_ETH2UART::~ARGUS_ETH2UART(){
  27. closePort();
  28. argusHardwareManager * tH = argusHardwareManager::getInstance();
  29. if ( tH ) {
  30. tH->resignResource( this );
  31. };
  32. };
  33. bool ARGUS_ETH2UART::create( void ){
  34. DBC_UART_DATA *tDATA = NULL;
  35. LOG("CREATE!");
  36. if( m_sResourceSettings.id == 0 ) return false;
  37. if( m_sResourceSettings.aux_id == 0 ) return false;
  38. tDATA = m_pDBC->getUartData( m_sResourceSettings.aux_id );
  39. if( tDATA!= NULL ) {
  40. memcpy( &m_sUartData, tDATA, sizeof(DBC_UART_DATA));
  41. #ifdef DEBUG
  42. LOG("KOKO!");
  43. LOG( m_sUartData.path );
  44. #endif
  45. free( tDATA );
  46. m_bValid = create_SOL();
  47. };
  48. if ( m_bValid ) {
  49. argusHardwareManager * tH = argusHardwareManager::getInstance();
  50. if ( tH ) {
  51. tH->assignResource( this );
  52. };
  53. };
  54. return m_bValid;
  55. };
  56. bool ARGUS_ETH2UART::validate( void ){
  57. if( m_bValid ) {
  58. m_bValid = pingDevice();
  59. if( !m_bValid ){
  60. LOG_ERROR("No Ping to device!");
  61. closePort();
  62. };
  63. };
  64. #ifdef DEBUG_EXTRA
  65. LOG("Check Settings For Changes");
  66. #endif
  67. DBC_UART_DATA *tDATA = m_pDBC->getUartData( m_sResourceSettings.aux_id );
  68. if( tDATA!= NULL ){
  69. bool bFlag = true;
  70. if( strcmp(tDATA->path,m_sUartData.path)!= 0 ) bFlag = false;
  71. //if( bFlag && ( m_sUartData.speed != tDATA->speed ) ) bFlag = false; //Speed for SOL port Erelevant
  72. if( bFlag && ( m_sUartData.mode != tDATA->mode ) ) bFlag = false;
  73. if( bFlag && ( m_sUartData.port != tDATA->port ) ) bFlag = false;
  74. if( !bFlag ) {
  75. #ifdef DEBUG
  76. LOG("Settings Has been Changed!");
  77. #endif
  78. if( m_bValid ) closePort(); // if port not closed
  79. // Apply new settings
  80. memcpy( &m_sUartData, tDATA, sizeof(DBC_UART_DATA));
  81. #ifdef DEBUG
  82. LOG("Reconect with new settings");
  83. #endif
  84. m_bValid = create_SOL();
  85. };
  86. free( tDATA );
  87. } else {
  88. LOG_ERROR("Error Fetch DB Data!");
  89. };
  90. // Finally in any case If Not Connected -> try to Connect!
  91. if( !m_bValid ) {
  92. #ifdef DEBUG
  93. LOG("Try to Conncet");
  94. #endif
  95. m_bValid = create_SOL();
  96. };
  97. return m_bValid;
  98. };
  99. #include <netinet/ip_icmp.h>
  100. // Define the Packet Constants
  101. // ping packet size
  102. #define PING_PKT_S 64
  103. // Automatic port number
  104. #define PORT_NO 0
  105. // Automatic port number
  106. #define PING_SLEEP_RATE 1000000
  107. // Gives the timeout delay for receiving packets
  108. // in seconds
  109. #define RECV_TIMEOUT 1
  110. // ping packet structure
  111. struct ping_pkt {
  112. struct icmphdr hdr;
  113. char msg[PING_PKT_S-sizeof(struct icmphdr)];
  114. };
  115. bool ARGUS_ETH2UART::pingDevice(){
  116. bool RetVal = false;
  117. if( m_bValid ) {
  118. #ifdef DEBUG_EXTRA
  119. LOG("Pinging Device!");
  120. #endif
  121. int sockfd;
  122. int ttl_val=64, msg_count=0, i, flag=1, msg_received_count=0;
  123. struct ping_pkt pckt;
  124. struct sockaddr_in ping_addr;
  125. struct sockaddr_in r_addr;
  126. struct timeval tv_out;
  127. tv_out.tv_sec = 0;
  128. tv_out.tv_usec = 300000; // 1/3 of second
  129. ping_addr.sin_family = AF_INET;
  130. ping_addr.sin_addr.s_addr = inet_addr( m_sUartData.path );
  131. //ping_addr.sin_port = htons( m_nPort );
  132. #ifdef DEBUG_EXTRA
  133. LOG( m_sUartData.path );
  134. #endif
  135. sockfd = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
  136. if( sockfd < 0 ){
  137. LOG_ERROR("Socket file descriptor not received!");
  138. return false;
  139. };
  140. // set socket options at ip to TTL and value to 64,
  141. // change to what you want by setting ttl_val
  142. if( setsockopt( sockfd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0 ){
  143. LOG_ERROR("Setting socket options to TTL failed!");
  144. shutdown( sockfd, SHUT_RDWR );
  145. close( sockfd );
  146. return false;
  147. };
  148. // setting timeout of recv setting
  149. setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv_out, sizeof tv_out );
  150. // flag is whether packet was sent or not
  151. flag=1;
  152. //filling packet
  153. bzero(&pckt, sizeof(pckt));
  154. pckt.hdr.type = ICMP_ECHO;
  155. pckt.hdr.un.echo.id = getpid();
  156. for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0';
  157. pckt.msg[i] = 0;
  158. pckt.hdr.un.echo.sequence = msg_count++;
  159. pckt.hdr.checksum = checksumPing( &pckt, sizeof(pckt)); //checksum(&pckt, sizeof(pckt));
  160. //SLEEP( 300 );
  161. //send packet
  162. if( sendto( sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&ping_addr, sizeof(ping_addr)) <= 0 ){
  163. LOG_ERROR("Packet Sending Failed!");
  164. shutdown( sockfd, SHUT_RDWR );
  165. close( sockfd );
  166. return false;
  167. };
  168. //receive packet
  169. socklen_t addr_len = sizeof(r_addr);
  170. if( recvfrom( sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &addr_len) <= 0 ){
  171. LOG_ERROR("Packet receive failed!");
  172. shutdown( sockfd, SHUT_RDWR );
  173. close( sockfd );
  174. return false;
  175. } else {
  176. #ifdef DEBUG_EXTRA
  177. LOG("Device Is OK!");
  178. #endif
  179. RetVal = true;
  180. };
  181. shutdown( sockfd, SHUT_RDWR );
  182. close( sockfd );
  183. };
  184. return RetVal;
  185. };
  186. bool ARGUS_ETH2UART::create_SOL(){
  187. /*
  188. if( m_sUartData.mode == SOL_NOT_DEFINED ) {
  189. LOG_ERROR("No Connection Type Defined!");
  190. return false;
  191. };
  192. */
  193. if( m_sUartData.port == 0 ) {
  194. LOG_ERROR("No Port Defined!");
  195. return false;
  196. };
  197. if( m_sUartData.speed == 0 ){
  198. LOG_ERROR("No Speed Defined!");
  199. return false;
  200. };
  201. if( m_sUartData.path[0] == (char)0 ) {
  202. LOG_ERROR("No Adress Defined!");
  203. return false;
  204. };
  205. switch( m_sUartData.mode ) {
  206. case SOL_UDP_CLIENT:{
  207. #ifdef DEBUG
  208. LOG("PORT IS CLIENT - Create UDP Server");
  209. #endif
  210. #ifdef __linux__
  211. // Creating socket file descriptor
  212. if ( (m_iDeviceFD = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
  213. LOG_ERROR("UDP Socket Creation Failed!");
  214. return false;
  215. };
  216. fcntl( m_iDeviceFD, F_SETFL, O_NONBLOCK);
  217. // Filling server information
  218. struct sockaddr_in tServerAddr;
  219. memset( &tServerAddr, 0, sizeof(tServerAddr) );
  220. tServerAddr.sin_family = AF_INET; // IPv4
  221. tServerAddr.sin_addr.s_addr = INADDR_ANY;
  222. tServerAddr.sin_port = htons( m_sUartData.port );
  223. // Bind the socket with the server address
  224. if ( bind( m_iDeviceFD, (const struct sockaddr *)&tServerAddr, sizeof(tServerAddr)) < 0 ){
  225. LOG_ERROR("UDP Bind Failed");
  226. return false;
  227. };
  228. #else
  229. WSADATA wsaData;
  230. // Initialize Winsock version 2.2
  231. char tBuff[128];
  232. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  233. sprintf(tBuff, "WSAStartup failed with error %ld", WSAGetLastError());
  234. LOG_ERROR(tBuff);
  235. return false;
  236. } else {
  237. sprintf(tBuff, "The Winsock DLL status is %s.", wsaData.szSystemStatus);
  238. LOG(tBuff);
  239. };
  240. // Set up a SOCKADDR_IN structure that will tell bind that we
  241. // want to receive datagrams from all interfaces using port 5150.
  242. // The IPv4 family
  243. ReceiverAddr.sin_family = AF_INET;
  244. // Port no. 5150
  245. ReceiverAddr.sin_port = htons( m_nPort );
  246. // From all interface (0.0.0.0)
  247. ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  248. // Associate the address information with the socket using bind.
  249. // At this point you can receive datagrams on your bound socket.
  250. if( bind(m_sSocket, (SOCKADDR*)&ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR ){
  251. sprintf( tBuff, "Server: bind() failed!Error : %ld.", WSAGetLastError());
  252. // Close the socket
  253. closesocket(m_sSocket);
  254. // Do the clean up
  255. WSACleanup();
  256. // and exit with error
  257. LOG_ERROR(tBuff);
  258. return false;
  259. } else {
  260. LOG("UDP Server: bind() is OK!");
  261. };
  262. // Some info on the receiver side...
  263. SOCKADDR_IN tAddr;
  264. int tLen = sizeof(SOCKADDR_IN);
  265. getsockname(m_sSocket, (SOCKADDR*)&tAddr, &tLen);
  266. sprintf( tBuff, "Receiving IP(s) used : %s", inet_ntoa(tAddr.sin_addr));
  267. LOG(tBuff);
  268. sprintf(tBuff,"Receiving port used : %d", htons(tAddr.sin_port));
  269. LOG(tBuff);
  270. SelectTiming = recvfromTimeOutUDP(m_sSocket, 10, 0);
  271. #endif
  272. break;
  273. };
  274. case SOL_TCP_CLIENT:{
  275. #ifdef DEBUG
  276. LOG("PORT IS CLIENT - Create TCP Server");
  277. #endif
  278. break;
  279. };
  280. case SOL_UDP_SERVER:{
  281. #ifdef DEBUG
  282. LOG("PORT IS SERVER - Create UDP Client");
  283. #endif
  284. #ifdef __linux__
  285. // Creating socket file descriptor
  286. if ( (m_iDeviceFD = socket( AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  287. LOG_ERROR("Socket Creation Failed!");
  288. return false;
  289. };
  290. fcntl( m_iDeviceFD, F_SETFL, O_NONBLOCK );
  291. // Filling server information
  292. struct sockaddr_in tServerAddr;
  293. memset( &tServerAddr, 0, sizeof(tServerAddr) );
  294. tServerAddr.sin_family = AF_INET;
  295. tServerAddr.sin_addr.s_addr = INADDR_ANY;
  296. tServerAddr.sin_port = htons( m_sUartData.port );
  297. #else
  298. #endif
  299. break;
  300. };
  301. case SOL_TCP_SERVER:{
  302. #ifdef DEBUG
  303. LOG("PORT IS SERVER - Create TCP Client");
  304. #endif
  305. // socket create and varification
  306. m_iDeviceFD = socket( AF_INET, SOCK_STREAM, 0 );
  307. if( m_iDeviceFD == -1) {
  308. LOG_ERROR("Socket creation failed!");
  309. return false;
  310. }
  311. #ifdef DEBUG
  312. else LOG("Socket successfully created");
  313. #endif
  314. // assign IP, PORT
  315. struct sockaddr_in tServerAddr;
  316. memset( &tServerAddr, 0, sizeof(tServerAddr) );
  317. tServerAddr.sin_family = AF_INET;
  318. tServerAddr.sin_addr.s_addr = inet_addr( m_sUartData.path );
  319. tServerAddr.sin_port = htons( m_sUartData.port );
  320. // connect the client socket to server socket
  321. if( connect( m_iDeviceFD, (struct sockaddr *)&tServerAddr, sizeof(tServerAddr) ) !=0 ){
  322. char cBuffer[128];
  323. sprintf(cBuffer,"ERROR connect to [%s:%d] (%s)", m_sUartData.path, m_sUartData.port, strerror(errno) );
  324. LOG_ERROR( cBuffer );
  325. return false;
  326. }
  327. #ifdef DEBUG
  328. else LOG("Connected to the server");
  329. #endif
  330. // Set non-blocking
  331. int arg;
  332. if( (arg = fcntl( m_iDeviceFD, F_GETFL, NULL)) < 0) {
  333. char tBuf[128];
  334. sprintf( tBuf, "Error fcntl(..., F_GETFL) (%s)", strerror(errno));
  335. LOG_ERROR( tBuf );
  336. return false;
  337. };
  338. arg |= O_NONBLOCK;
  339. if( fcntl( m_iDeviceFD, F_SETFL, arg) < 0) {
  340. char tBuf[128];
  341. sprintf( tBuf, "Error fcntl(..., F_SETFL) (%s)", strerror(errno));
  342. LOG_ERROR( tBuf );
  343. return false;
  344. };
  345. setConnectionStatus( CS_CONNECTED );
  346. break;
  347. };
  348. };
  349. return init();
  350. };
  351. bool ARGUS_ETH2UART::closePort(){
  352. char tBuff[128];
  353. setConnectionStatus( CS_DISCONNECTED );
  354. if( m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT ){
  355. uninit();
  356. };
  357. if( m_iDeviceFD != -1 ) {
  358. switch( m_sUartData.mode ) {
  359. case SOL_UDP_CLIENT:
  360. case SOL_TCP_CLIENT:
  361. case SOL_UDP_SERVER: {
  362. #ifdef __linux__
  363. close( m_iDeviceFD );
  364. #else
  365. // When your application is finished receiving datagrams close the socket.
  366. LOG("Server: Finished receiving.Closing the listening socket...");
  367. if( closesocket(m_sSocket) != 0 ){
  368. sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
  369. LOG_ERROR(tBuff);
  370. } else {
  371. LOG("closesocket() is OK...");
  372. };
  373. // When your application is finished call WSACleanup.
  374. LOG("Server: Cleaning up...");
  375. if (WSACleanup() != 0) {
  376. sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
  377. LOG_ERROR(tBuff);
  378. } else {
  379. LOG("WSACleanup() is OK");
  380. };
  381. #endif
  382. break;
  383. };
  384. case SOL_TCP_SERVER:{
  385. close( m_iDeviceFD );
  386. break;
  387. };
  388. };
  389. m_iDeviceFD = -1;
  390. };
  391. return true;
  392. };
  393. void ARGUS_ETH2UART::m_fThreadAProc(void){
  394. int index;
  395. int RetVal;
  396. m_bThreadAStatus = TH_WORKING;
  397. #ifdef DEBUG
  398. LOG("Sender Thread Started!");
  399. #endif
  400. while (m_bThreadAStatus < TH_ENDED) {
  401. m_mBufferProtectorMutex.lock();
  402. if( m_nOutputBufferSize > 0 ){
  403. index = 0;
  404. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  405. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  406. if( RetVal > 0 ) index += RetVal;
  407. if( RetVal == -1 ){
  408. setConnectionStatus( CS_DISCONNECTED );
  409. m_nOutputBufferSize = 0;
  410. m_bThreadBStatus = TH_ENDED;
  411. m_bThreadAStatus = TH_ENDED;
  412. LOG_ERROR("Connection Droped on Sending Data");
  413. };
  414. };
  415. m_nOutputBufferSize = 0;
  416. m_mBufferProtectorMutex.unlock();
  417. } else {
  418. m_mBufferProtectorMutex.unlock();
  419. SLEEP(5);
  420. };
  421. };
  422. };
  423. void ARGUS_ETH2UART::m_fThreadBProc(void){
  424. m_bThreadBStatus = TH_WORKING;
  425. int RetVal;
  426. #ifdef DEBUG
  427. LOG("Recipient Thread Started!");
  428. #endif
  429. while( m_bThreadBStatus < TH_ENDED ){
  430. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize);
  431. if( RetVal == -1 ){
  432. setConnectionStatus(CS_DISCONNECTED);
  433. m_nInputBufferSize = 0;
  434. //if(!getStatus()) {
  435. m_bThreadBStatus = TH_ENDED;
  436. m_bThreadAStatus = TH_ENDED;
  437. //} else SLEEP(100);
  438. LOG_ERROR("Connection Droped on Reception Data");
  439. } else if( m_nInputBufferSize > 0 ){
  440. do{
  441. m_mQueueProtectorMutex.lock();
  442. if( m_vParsers.size() ){
  443. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  444. m_nInputBufferSize = 0;
  445. m_mQueueProtectorMutex.unlock();
  446. } else {
  447. m_nInputBufferSize = 0;
  448. m_mQueueProtectorMutex.unlock();
  449. #ifdef DEBUG_EXTRA
  450. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  451. #endif
  452. SLEEP(100);
  453. };
  454. } while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
  455. } else {
  456. SLEEP(5);
  457. };
  458. };
  459. };
  460. bool ARGUS_ETH2UART::sendData( uint8_t *Data, int DataSize ){
  461. if( m_bThreadAStatus != TH_WORKING ) {
  462. if( m_bThreadAStatus < TH_WORKING ) {
  463. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  464. } else {
  465. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  466. }
  467. return false;
  468. };
  469. m_mBufferProtectorMutex.lock();
  470. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  471. m_mBufferProtectorMutex.unlock();
  472. LOG_ERROR("Data Too BIG");
  473. return false;
  474. };
  475. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  476. m_nOutputBufferSize += DataSize;
  477. m_mBufferProtectorMutex.unlock();
  478. return true;
  479. };
  480. bool ARGUS_ETH2UART::reconnect( void ){
  481. closePort();
  482. m_bValid = create_SOL();
  483. return m_bValid;
  484. };
  485. int ARGUS_ETH2UART::recptBlock( unsigned char *Buffer, int *BufferLength ){
  486. #ifdef __linux__
  487. int RetVal = 0;
  488. #else
  489. DWORD RetVal = 0;
  490. #endif
  491. if( m_eConnectionStatus == CS_CONNECTED ){
  492. #ifdef __linux__
  493. if( m_iDeviceFD > -1 ) {
  494. struct sockaddr ClientAddress;
  495. socklen_t addr_len = sizeof( ClientAddress ); //len is value/resuslt
  496. //RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len );
  497. RetVal = recvfrom( m_iDeviceFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_DONTWAIT, ( struct sockaddr *) &ClientAddress, &addr_len );
  498. if( RetVal < 1 ){
  499. if( errno!=EAGAIN && errno!=EINTR ) {
  500. LOG_ERROR("Peer Closes Connection!");
  501. shutdown( m_iDeviceFD, 2 );
  502. close( m_iDeviceFD );
  503. *BufferLength = 0;
  504. m_iDeviceFD = -1;
  505. setConnectionStatus( CS_DISCONNECTED );
  506. return -1;
  507. };
  508. };
  509. } else {
  510. LOG_ERROR("Not Connected!");
  511. *BufferLength = 0;
  512. setConnectionStatus( CS_DISCONNECTED );
  513. return -1;
  514. };
  515. #else
  516. switch ( m_eType ) {
  517. case SOL_UDP_CLIENT:
  518. case SOL_TCP_CLIENT:
  519. case SOL_UDP_SERVER: {
  520. SOCKADDR_IN tAddr;
  521. int tLen = sizeof(SOCKADDR_IN);
  522. RetVal = recvfrom( m_sSocket, (char*)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0, (struct sockaddr*) &tAddr, &tLen);
  523. if (RetVal == SOCKET_ERROR){
  524. char cBuffer[165];
  525. sprintf(cBuffer,"recvfrom() failed with error code : %d", WSAGetLastError());
  526. LOG_ERROR(cBuffer);
  527. };
  528. break;
  529. };
  530. };
  531. #endif
  532. } else {
  533. //make prep for connection SOL_TCP_SERVER
  534. if( m_sUartData.mode == SOL_TCP_CLIENT ){
  535. //ACCEPT
  536. struct sockaddr ClientAddress;
  537. socklen_t size = (socklen_t) sizeof( struct sockaddr_in );
  538. //int ClientSocket;
  539. m_iDeviceFD = accept( m_iMasterFD, &ClientAddress, &size );
  540. if( m_iDeviceFD < 0 ) {
  541. setConnectionStatus( CS_DISCONNECTED );
  542. LOG_ERROR("Connection Accept Failure!");
  543. };
  544. } else {
  545. LOG_ERROR("RCPT ON not connected!");
  546. shutdown( m_iDeviceFD, 2 );
  547. close( m_iDeviceFD );
  548. *BufferLength = 0;
  549. m_iDeviceFD = -1;
  550. setConnectionStatus( CS_DISCONNECTED );
  551. };
  552. return -1; // Exit after unsuccess reconnect
  553. };
  554. if( RetVal > 0 ){
  555. #ifdef DEBUG_SERIAL_COMMUNICATION
  556. char MsgBuffer[CLOG];
  557. if( RetVal > 1 || RetVal == 0 ) {
  558. sprintf( MsgBuffer, "Received %d bytes", RetVal );
  559. } else {
  560. sprintf( MsgBuffer, "Received 1 byte");
  561. };
  562. LOG( MsgBuffer );
  563. for (int i = 0; i < RetVal; i++) {
  564. sprintf(MsgBuffer, "0x%02X", Buffer[i]);
  565. LOG(MsgBuffer);
  566. };
  567. #endif
  568. *BufferLength = RetVal;
  569. return RetVal;
  570. };
  571. *BufferLength = 0;
  572. if (RetVal == -1) return 0;
  573. if (RetVal == 0){
  574. errno = 0;
  575. return 0;
  576. };
  577. /* r < -1, should never happen. */
  578. errno = EIO;
  579. return 0;
  580. };
  581. int ARGUS_ETH2UART::sendBlock(unsigned char* Data, int DataSize) {
  582. #ifdef __linux__
  583. int RetVal = 0;
  584. #else
  585. DWORD RetVal = 0;
  586. #endif
  587. if( m_eConnectionStatus == CS_CONNECTED ){
  588. #ifdef __linux__
  589. if( m_iDeviceFD > -1 ){
  590. #ifdef DEBUG_EXTRA
  591. char cBuffer[256];
  592. sprintf(cBuffer, "Write into Port %d bytes", DataSize);
  593. LOG(cBuffer);
  594. #endif
  595. struct sockaddr ClientAddress;
  596. socklen_t addr_len = sizeof(ClientAddress);
  597. RetVal = sendto( m_iDeviceFD, (const char*)Data, DataSize, MSG_CONFIRM, (const struct sockaddr*) &ClientAddress, addr_len);
  598. if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK)) {
  599. LOG_ERROR("Error acures while writing data into the Socket");
  600. shutdown( m_iDeviceFD, 2);
  601. close( m_iDeviceFD);
  602. m_iDeviceFD = -1;
  603. setConnectionStatus(CS_DISCONNECTED);
  604. return -1;
  605. };
  606. if (RetVal < 0) RetVal = 0;
  607. #ifdef DEBUG_EXTRA
  608. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  609. LOG(cBuffer);
  610. #ifdef DEBUG_SERIAL_COMMUNICATION
  611. for(int zz=0; zz < RetVal; zz++ ) {
  612. sprintf(cBuffer, "SD: 0x%02X", (uint8_t)Data[zz]);
  613. LOG(cBuffer);
  614. };
  615. #endif
  616. #endif
  617. } else {
  618. LOG_ERROR("Not Connected!");
  619. //m_bConnected = false;
  620. setConnectionStatus(CS_DISCONNECTED);
  621. return -1;
  622. };
  623. #else // Windows
  624. if (sendto( m_sSocket, (char *)Data, DataSize, 0, (struct sockaddr*) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR) {
  625. char cBuffer[256];
  626. sprintf(cBuffer, "sendto() failed with error code : %d", WSAGetLastError());
  627. LOG_ERROR( cBuffer );
  628. return -1;
  629. };
  630. #endif
  631. } else {
  632. #ifdef DEBUG
  633. LOG( "Port is not Ready to Send" );
  634. #endif
  635. return -1;
  636. };
  637. return RetVal;
  638. };
  639. #undef LOG_ERROR
  640. #undef LOG