loopback.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <stdio.h>
  2. #include "loopback.h"
  3. #include "socket.h"
  4. #include "wizchip_conf.h"
  5. #if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
  6. int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
  7. {
  8. int32_t ret;
  9. uint16_t size = 0, sentsize=0;
  10. #ifdef _LOOPBACK_DEBUG_
  11. uint8_t destip[4];
  12. uint16_t destport;
  13. #endif
  14. switch(getSn_SR(sn))
  15. {
  16. case SOCK_ESTABLISHED :
  17. if(getSn_IR(sn) & Sn_IR_CON)
  18. {
  19. #ifdef _LOOPBACK_DEBUG_
  20. getSn_DIPR(sn, destip);
  21. destport = getSn_DPORT(sn);
  22. printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
  23. #endif
  24. setSn_IR(sn,Sn_IR_CON);
  25. }
  26. if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
  27. {
  28. if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
  29. ret = recv(sn, buf, size);
  30. if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
  31. size = (uint16_t) ret;
  32. sentsize = 0;
  33. while(size != sentsize)
  34. {
  35. ret = send(sn, buf+sentsize, size-sentsize);
  36. if(ret < 0)
  37. {
  38. close(sn);
  39. return ret;
  40. }
  41. sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
  42. }
  43. }
  44. break;
  45. case SOCK_CLOSE_WAIT :
  46. #ifdef _LOOPBACK_DEBUG_
  47. //printf("%d:CloseWait\r\n",sn);
  48. #endif
  49. if((ret = disconnect(sn)) != SOCK_OK) return ret;
  50. #ifdef _LOOPBACK_DEBUG_
  51. printf("%d:Socket Closed\r\n", sn);
  52. #endif
  53. break;
  54. case SOCK_INIT :
  55. #ifdef _LOOPBACK_DEBUG_
  56. printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
  57. #endif
  58. if( (ret = listen(sn)) != SOCK_OK) return ret;
  59. break;
  60. case SOCK_CLOSED:
  61. #ifdef _LOOPBACK_DEBUG_
  62. //printf("%d:TCP server loopback start\r\n",sn);
  63. #endif
  64. if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
  65. #ifdef _LOOPBACK_DEBUG_
  66. //printf("%d:Socket opened\r\n",sn);
  67. #endif
  68. break;
  69. default:
  70. break;
  71. }
  72. return 1;
  73. }
  74. int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
  75. {
  76. int32_t ret; // return value for SOCK_ERRORs
  77. uint16_t size = 0, sentsize=0;
  78. // Destination (TCP Server) IP info (will be connected)
  79. // >> loopback_tcpc() function parameter
  80. // >> Ex)
  81. // uint8_t destip[4] = {192, 168, 0, 214};
  82. // uint16_t destport = 5000;
  83. // Port number for TCP client (will be increased)
  84. static uint16_t any_port = 50000;
  85. // Socket Status Transitions
  86. // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
  87. switch(getSn_SR(sn))
  88. {
  89. case SOCK_ESTABLISHED :
  90. if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
  91. {
  92. #ifdef _LOOPBACK_DEBUG_
  93. printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
  94. #endif
  95. setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
  96. }
  97. //////////////////////////////////////////////////////////////////////////////////////////////
  98. // Data Transaction Parts; Handle the [data receive and send] process
  99. //////////////////////////////////////////////////////////////////////////////////////////////
  100. if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
  101. {
  102. if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
  103. ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
  104. if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
  105. size = (uint16_t) ret;
  106. sentsize = 0;
  107. // Data sentsize control
  108. while(size != sentsize)
  109. {
  110. ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
  111. if(ret < 0) // Send Error occurred (sent data length < 0)
  112. {
  113. close(sn); // socket close
  114. return ret;
  115. }
  116. sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
  117. }
  118. }
  119. //////////////////////////////////////////////////////////////////////////////////////////////
  120. break;
  121. case SOCK_CLOSE_WAIT :
  122. #ifdef _LOOPBACK_DEBUG_
  123. //printf("%d:CloseWait\r\n",sn);
  124. #endif
  125. if((ret=disconnect(sn)) != SOCK_OK) return ret;
  126. #ifdef _LOOPBACK_DEBUG_
  127. printf("%d:Socket Closed\r\n", sn);
  128. #endif
  129. break;
  130. case SOCK_INIT :
  131. #ifdef _LOOPBACK_DEBUG_
  132. printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
  133. #endif
  134. if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
  135. break;
  136. case SOCK_CLOSED:
  137. close(sn);
  138. if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
  139. if(any_port == 0xffff) any_port = 50000;
  140. return ret; // TCP socket open with 'any_port' port number
  141. }
  142. #ifdef _LOOPBACK_DEBUG_
  143. //printf("%d:TCP client loopback start\r\n",sn);
  144. //printf("%d:Socket opened\r\n",sn);
  145. #endif
  146. break;
  147. default:
  148. break;
  149. }
  150. return 1;
  151. }
  152. int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
  153. {
  154. int32_t ret;
  155. uint16_t size, sentsize;
  156. uint8_t destip[4];
  157. uint16_t destport;
  158. switch(getSn_SR(sn))
  159. {
  160. case SOCK_UDP :
  161. if((size = getSn_RX_RSR(sn)) > 0)
  162. {
  163. if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
  164. ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
  165. if(ret <= 0)
  166. {
  167. #ifdef _LOOPBACK_DEBUG_
  168. printf("%d: recvfrom error. %ld\r\n",sn,ret);
  169. #endif
  170. return ret;
  171. }
  172. size = (uint16_t) ret;
  173. sentsize = 0;
  174. while(sentsize != size)
  175. {
  176. ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
  177. if(ret < 0)
  178. {
  179. #ifdef _LOOPBACK_DEBUG_
  180. printf("%d: sendto error. %ld\r\n",sn,ret);
  181. #endif
  182. return ret;
  183. }
  184. sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
  185. }
  186. }
  187. break;
  188. case SOCK_CLOSED:
  189. #ifdef _LOOPBACK_DEBUG_
  190. //printf("%d:UDP loopback start\r\n",sn);
  191. #endif
  192. if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
  193. return ret;
  194. #ifdef _LOOPBACK_DEBUG_
  195. printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
  196. #endif
  197. break;
  198. default :
  199. break;
  200. }
  201. return 1;
  202. }
  203. #endif