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