WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
w5500/socket.c
Go to the documentation of this file.
1 
12 //#define FILE_LOG_SILENCE
13 #include "common/common.h"
14 //#include "device/socket.h"
15 
16 
17 extern uint8 I_STATUS[TOTAL_SOCK_NUM];
18 //static uint16 RMASK[MAX_SOCK_NUM]; //< Variable for Rx buffer MASK in each channel */
19 extern uint16 SSIZE[TOTAL_SOCK_NUM]; //< Max Tx buffer size by each channel */
20 extern uint16 RSIZE[TOTAL_SOCK_NUM]; //< Max Rx buffer size by each channel */
21 //static uint16 SBUFBASEADDRESS[MAX_SOCK_NUM]; //< Tx buffer base address by each channel */
22 //static uint16 RBUFBASEADDRESS[MAX_SOCK_NUM]; //< Rx buffer base address by each channel */
23 
24 static uint8 DNS[4]={0};
25 static dhcp_mode DHCP = NETINFO_STATIC;
26 static uint16 local_port = 0xC000; // Dynamic Port: C000(49152) ~ FFFF(65535)
27 
28 static uint32 tcp_close_elapse[TOTAL_SOCK_NUM] = {0,};
29 static uint32 tcp_resend_elapse[TOTAL_SOCK_NUM] = {0,};
30 static uint16 txrd_checker[TOTAL_SOCK_NUM];
31 
32 
33 void device_init(uint8 *tx_size, uint8 *rx_size)
34 {
36  device_mem_init(tx_size, rx_size);
37 }
38 
39 void device_SW_reset(void)
40 {
41  setMR(MR_RST);
42  DBGA("MR value is %02x",IINCHIP_READ_COMMON(WIZC_MR));
43 }
44 
45 void device_mem_init(uint8 *tx_size, uint8 *rx_size)
46 {
47  int16 i, mul;
48  int16 ssum, rsum;
49 
50  DBG("device_mem_init()");
51 
52  ssum = 0;
53  rsum = 0;
54 
55  for (i=0; i<TOTAL_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel
56  {
57  IINCHIP_WRITE_SOCKETREG( i, WIZS_TXMEM_SIZE, tx_size[i]);
58  IINCHIP_WRITE_SOCKETREG( i, WIZS_RXMEM_SIZE, rx_size[i]);
59 
60  DBGA("tx_size[%d]: %d, Sn_TXMEM_SIZE = %d",i, tx_size[i], IINCHIP_READ_SOCKETREG( i, WIZS_TXMEM_SIZE));
61  DBGA("rx_size[%d]: %d, Sn_RXMEM_SIZE = %d",i, rx_size[i], IINCHIP_READ_SOCKETREG( i, WIZS_RXMEM_SIZE));
62 
63  SSIZE[i] = (int16)(0);
64  RSIZE[i] = (int16)(0);
65 
66  if(ssum <= 16384) { //if(ssum <= 8192)
67 #if 1 //--4Channel사용시 적용 안됨 --20120522
68  if(tx_size[i]==1 || tx_size[i]==2 || tx_size[i]==4 || tx_size[i]==8 || tx_size[i]==16)
69 #else
70  if(tx_size[i]==1 || tx_size[i]==2 || tx_size[i]==4 || tx_size[i]==8)
71 #endif
72  mul = tx_size[i];
73  else mul = 2; // by Ssoo Default 2K --20120522
74  SSIZE[i] = 0x400 * mul;
75  SMASK[i] = 0x400 * mul - 1;
76  }
77  if(rsum <= 16384) { //if(rsum <= 8192)
78 #if 1 //--4Channel사용시 적용 안됨 --20120522
79  if(rx_size[i]==1 || rx_size[i]==2 || rx_size[i]==4 || rx_size[i]==8 || rx_size[i]==16)
80 #else
81  if(rx_size[i]==1 || rx_size[i]==2 || rx_size[i]==4 || rx_size[i]==8)
82 #endif
83  mul = rx_size[i];
84  else mul = 2; // by Ssoo Default 2K --20120522
85  RSIZE[i] = 0x400 * mul;
86  RMASK[i] = 0x400 * mul - 1;
87  }
88 
89  ssum += SSIZE[i];
90  rsum += RSIZE[i];
91 
92 // if (i != 0) { // Sets base address of Tx and Rx memory for channel #1,#2,#3
93 // SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1];
94 // RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1];
95 // }
96  DBGA("ch = %d",i);
97  DBGA("SBUFBASEADDRESS = %d",(uint16)SBUFBASEADDRESS[i]);
98  DBGA("RBUFBASEADDRESS = %d",(uint16)RBUFBASEADDRESS[i]);
99  DBGA("SSIZE = %d",SSIZE[i]);
100  DBGA("RSIZE = %d",RSIZE[i]);
101  }
102 }
103 
104 void SetNetInfo(wiz_NetInfo *netinfo)
105 {
106  if(netinfo->mac[0] != 0x00 || netinfo->mac[1] != 0x00 || netinfo->mac[2] != 0x00 ||
107  netinfo->mac[3] != 0x00 || netinfo->mac[4] != 0x00 || netinfo->mac[5] != 0x00)
108  setSHAR(netinfo->mac); // set local MAC address
109  if(netinfo->ip[0] != 0x00 || netinfo->ip[1] != 0x00 || netinfo->ip[2] != 0x00 ||
110  netinfo->ip[3] != 0x00) setSIPR(netinfo->ip); // set local IP address
111  if(netinfo->sn[0] != 0x00 || netinfo->sn[1] != 0x00 || netinfo->sn[2] != 0x00 ||
112  netinfo->sn[3] != 0x00) setSUBR(netinfo->sn); // set Subnet mask
113  if(netinfo->gw[0] != 0x00 || netinfo->gw[1] != 0x00 || netinfo->gw[2] != 0x00 ||
114  netinfo->gw[3] != 0x00) setGAR(netinfo->gw); // set Gateway address
115  if(netinfo->dns[0] != 0x00 || netinfo->dns[1] != 0x00 || netinfo->dns[2] != 0x00 ||
116  netinfo->dns[3] != 0x00){
117  DNS[0] = netinfo->dns[0];
118  DNS[1] = netinfo->dns[1];
119  DNS[2] = netinfo->dns[2];
120  DNS[3] = netinfo->dns[3];
121  }
122 
123  if(netinfo->dhcp != 0) DHCP = netinfo->dhcp;
124 }
125 
127 {
128  uint8 zero[6] = {0,};
129 
130  DBGA("Reset Address(%d)", member);
131  switch(member) {
132  //case NI_MAC_ADDR: // If need, uncomment
133  // setSHAR(zero);
134  // break;
135  case NI_IP_ADDR:
136  setSIPR(zero);
137  break;
138  case NI_SN_MASK:
139  setSUBR(zero);
140  break;
141  case NI_GW_ADDR:
142  setGAR(zero);
143  break;
144  case NI_DNS_ADDR:
145  DNS[0] = DNS[1] = DNS[2] = DNS[3] = 0;
146  break;
147  default:
148  ERRA("wrong member value (%d)", member);
149  }
150 }
151 
152 void GetNetInfo(wiz_NetInfo *netinfo)
153 {
154  getSHAR(netinfo->mac); // get local MAC address
155  getSIPR(netinfo->ip); // get local IP address
156  getSUBR(netinfo->sn); // get subnet mask address
157  getGAR(netinfo->gw); // get gateway address
158  netinfo->dns[0] = DNS[0];
159  netinfo->dns[1] = DNS[1];
160  netinfo->dns[2] = DNS[2];
161  netinfo->dns[3] = DNS[3];
162  netinfo->dhcp = DHCP;
163 }
164 
165 void GetDstInfo(uint8 s, uint8 *dstip, uint16 *dstport)
166 {
167  getDIPR(s, dstip);
168  getDPORT(s, dstport);
169 }
170 
171 void SetSocketOption(uint8 option_type, uint16 option_value)
172 {
173  switch(option_type){
174  case 0:
175  setRTR(option_value); // set retry duration for data transmission, connection, closing ...
176  break;
177  case 1:
178  setRCR((uint8)(option_value&0x00FF)); // set retry count (above the value, assert timeout interrupt)
179  break;
180  case 2:
181  setIMR((uint8)(option_value&0x00FF)); // set interrupt mask.
182  break;
183  default:
184  break;
185  }
186 }
187 
188 int8 GetTCPSocketStatus(uint8 s)
189 {
190  if(s > TOTAL_SOCK_NUM) {
191  ERRA("wrong socket number(%d)", s);
192  return SOCKERR_NOT_TCP;
193  }
194 
195  switch(getSn_SR(s)){
196  case SOCK_CLOSED: return SOCKSTAT_CLOSED; // closed
197  case SOCK_INIT: return SOCKSTAT_INIT; // init state
198  case SOCK_LISTEN: return SOCKSTAT_LISTEN; // listen state
199  case SOCK_SYNSENT: return SOCKSTAT_SYNSENT; // connection state
200  case SOCK_SYNRECV: return SOCKSTAT_SYNRECV; // connection state
201  case SOCK_ESTABLISHED: return SOCKSTAT_ESTABLISHED; // success to connect
202  case SOCK_FIN_WAIT: return SOCKSTAT_FIN_WAIT; // closing state
203  case SOCK_CLOSING: return SOCKSTAT_CLOSING; // closing state
204  case SOCK_TIME_WAIT: return SOCKSTAT_TIME_WAIT; // closing state
205  case SOCK_CLOSE_WAIT: return SOCKSTAT_CLOSE_WAIT; // closing state
206  case SOCK_LAST_ACK: return SOCKSTAT_LAST_ACK; // closing state
207  default:
208  //if((IINCHIP_READ(Sn_MR(Sn_MR_TCP))&0x0F) != Sn_MR_TCP)
209  //return SOCKERR_NOT_UDP;
210  if((IINCHIP_READ_SOCKETREG(s, WIZC_MR)&0x0F) != Sn_MR_TCP)
211  return SOCKERR_NOT_TCP;
212  else return SOCKERR_WRONG_STATUS;
213  }
214 }
215 
216 int8 GetUDPSocketStatus(uint8 s)
217 {
218  if(s > TOTAL_SOCK_NUM) {
219  ERRA("wrong socket number(%d)", s);
220  return SOCKERR_NOT_UDP;
221  }
222 
223  switch(getSn_SR(s)){
224  case SOCK_CLOSED: return SOCKSTAT_CLOSED; // closed
225  case SOCK_UDP: return SOCKSTAT_UDP; // udp socket
226 #if 0
227  case SOCK_MACRAW: return 12; // mac raw mode socket
228  case SOCK_PPPOE: return 13; // pppoe socket
229 #endif
230  default:
231  //if((IINCHIP_READ(Sn_MR(Sn_MR_UDP))&0x0F) != Sn_MR_UDP)
232  //return SOCKERR_NOT_UDP;
233  if((IINCHIP_READ_SOCKETREG(s, WIZC_MR)&0x0F) != Sn_MR_UDP)
234  return SOCKERR_NOT_UDP;
235  else return SOCKERR_WRONG_STATUS;
236  }
237 }
238 
240 {
241  return getSn_TX_FSR(s); // get socket TX free buf size
242 }
243 
245 {
246  return getSn_RX_RSR(s); // get socket RX recv buf size
247 }
248 
249 int8 TCPServerOpen(uint8 s, uint16 port)
250 {
251  if(s > TOTAL_SOCK_NUM) {
252  ERRA("wrong socket number(%d)", s);
253  return SOCKERR_NOT_TCP;
254  } else DBG("start");
255 
256  if (port == 0) { // if don't set the source port, set local_port number.
257  if(local_port == 0xffff) local_port = 0xc000;
258  else local_port++;
259  port = local_port;
260  }
261 
262  TCPClose(s);
263  //IINCHIP_WRITE(Sn_MR(s),Sn_MR_TCP);
264  //IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
265  //IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
266  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR
267  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
268  //DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
269 
270  IINCHIP_WRITE_SOCKETREG(s, WIZS_MR, Sn_MR_TCP);
271  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 0, (uint8)((port & 0xff00) >> 8));
272  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 1, (uint8)(port & 0x00ff));
273  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_OPEN); // run sockinit Sn_CR
274  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
275  DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ_SOCKETREG(s, WIZS_SR), IINCHIP_READ_SOCKETREG(s, WIZS_MR));
276 
277  //if (IINCHIP_READ(Sn_SR(s)) != SOCK_INIT) {
278  //DBGA("wrong status(%d)", IINCHIP_READ(Sn_SR(s)));
279  if(IINCHIP_READ_SOCKETREG(s, WIZS_SR) != SOCK_INIT) {
280  DBGA("wrong status(%d)", IINCHIP_READ_SOCKETREG(s, WIZS_SR));
281  return SOCKERR_WRONG_STATUS;
282  } else {
283  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_LISTEN);
284  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
285  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_LISTEN);
286  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
287  }
288 
289  return RET_OK;
290 }
291 
292 int8 TCPClientOpen(uint8 s, uint16 sport, uint8 *dip, uint16 dport)
293 {
294  int8 ret;
295 
296  DBG("start");
297  ret = TCPCltOpenNB(s, sport, dip, dport);
298  if(ret != RET_OK) return ret;
299 
300  do {
301  ret = TCPConnChk(s);
302  } while(ret == SOCKERR_BUSY);
303 
304  return ret;
305 }
306 
307 int8 TCPCltOpenNB(uint8 s, uint16 sport, uint8 *dip, uint16 dport)
308 {
309  uint8 srcip[4], snmask[4];
310 
311  if(s > TOTAL_SOCK_NUM) {
312  ERRA("wrong socket number(%d)", s);
313  return SOCKERR_NOT_TCP;
314  } else if(dip == NULL) {
315  ERR("NULL Dst IP");
316  return SOCKERR_WRONG_ARG;
317  } else DBG("start");
318 
319  if (sport == 0) { // if don't set the source port, set local_port number.
320  if(local_port == 0xffff) local_port = 0xc000;
321  else local_port++;
322  sport = local_port;
323  }
324 
325  TCPClose(s);
326  //IINCHIP_WRITE(Sn_MR(s),Sn_MR_TCP);
327  //IINCHIP_WRITE(Sn_PORT0(s),(uint8)((sport & 0xff00) >> 8));
328  //IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(sport & 0x00ff));
329  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR
330  //while(IINCHIP_READ(Sn_CR(s)) ); // wait to process the command...
331  //DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
332 
333  IINCHIP_WRITE_SOCKETREG(s, WIZS_MR, Sn_MR_TCP);
334  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 0, (uint8)((sport & 0xff00) >> 8));
335  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 1, (uint8)(sport & 0x00ff));
336  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_OPEN); // run sockinit Sn_CR
337  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
338  DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ_SOCKETREG(s, WIZS_SR), IINCHIP_READ_SOCKETREG(s, WIZS_MR));
339 
340  getSIPR(srcip);
341  getSUBR(snmask);
342 
343  if( ((dip[0] == 0xFF) && (dip[1] == 0xFF) &&
344  (dip[2] == 0xFF) && (dip[3] == 0xFF)) ||
345  ((dip[0] == 0x00) && (dip[1] == 0x00) &&
346  (dip[2] == 0x00) && (dip[3] == 0x00)) || (sport == 0x00) )
347  {
348  DBG("invalid ip or port");
349  DBGA("SOCK(%d)-[%02x.%02x.%02x.%02x, %d]",s,
350  dip[0], dip[1], dip[2], dip[3] , sport);
351  return SOCKERR_WRONG_ARG;
352  }
353  else if( (srcip[0]==0 && srcip[1]==0 && srcip[2]==0 && srcip[3]==0) &&
354  (snmask[0]!=0 || snmask[1]!=0 || snmask[2]!=0 || snmask[3]!=0) ) //Mikej : ARP Errata
355  {
356  DBG("Source IP is NULL while SN Mask is Not NULL");
357  return SOCKERR_NULL_SRC_IP;
358  }
359  else
360  {
361  //IINCHIP_WRITE(Sn_DIPR0(s),dip[0]); // set destination IP
362  //IINCHIP_WRITE((Sn_DIPR0(s) + 1),dip[1]);
363  //IINCHIP_WRITE((Sn_DIPR0(s) + 2),dip[2]);
364  //IINCHIP_WRITE((Sn_DIPR0(s) + 3),dip[3]);
365  //IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((dport & 0xff00) >> 8));
366  //IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(dport & 0x00ff));
368  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_CONNECT);
369  //while (IINCHIP_READ(Sn_CR(s)) ); // wait for completion
370  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 0, dip[0]); // set destination IP
371  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 1, dip[1]);
372  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 2, dip[2]);
373  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 3, dip[3]);
374  IINCHIP_WRITE_SOCKETREG(s, WIZS_DPORT0 + 0, (uint8)((dport & 0xff00) >> 8));
375  IINCHIP_WRITE_SOCKETREG(s, WIZS_DPORT0 + 1, (uint8)(dport & 0x00ff));
376  //SetSubnet(sn); // for ARP Errata
377  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR,Sn_CR_CONNECT);
378  while (IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait for completion
379  }
380 
381  return RET_OK;
382 }
383 
384 int8 TCPConnChk(uint8 s)
385 {
386  uint8 socksr;
387 
388  if(s > TOTAL_SOCK_NUM) {
389  ERRA("wrong socket number(%d)", s);
390  return SOCKERR_NOT_TCP;
391  }
392 
393  //socksr = IINCHIP_READ(Sn_SR(s));
394  socksr = IINCHIP_READ_SOCKETREG(s, WIZS_SR);
395 
396  //if(socksr == SOCK_ESTABLISHED || socksr == SOCK_SYNSENT) { ????????
397  if(socksr == SOCK_ESTABLISHED) {
398  //ClearSubnet(); // for ARP Errata
399  return RET_OK;
400  //} else if(IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT) {
401  //IINCHIP_WRITE(Sn_IR(s), (Sn_IR_TIMEOUT)); // clear TIMEOUT Interrupt
402  } else if(IINCHIP_READ_SOCKETREG(s, WIZS_IR) & Sn_IR_TIMEOUT) {
403  IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, Sn_IR_TIMEOUT); // clear TIMEOUT Interrupt
404  //ClearSubnet(); // for ARP Errata
405  return SOCKERR_TIME_OUT;
406  }
407 
408  return SOCKERR_BUSY;
409 }
410 
411 int8 TCPClose(uint8 s)
412 {
413  int8 ret;
414 
415  DBG("start");
416  ret = TCPCloseNB(s);
417  if(ret != RET_OK) return ret;
418 
419  do {
420  ret = TCPCloseCHK(s);
421  } while(ret == SOCKERR_BUSY);
422 
423  return ret;
424 }
425 
426 int8 TCPCloseNB(uint8 s)
427 {
428  uint8 status;
429 
430  if(s > TOTAL_SOCK_NUM) {
431  ERRA("wrong socket number(%d)", s);
432  return SOCKERR_NOT_TCP;
433  } else DBG("start");
434 
435  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_DISCON);
436  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
437  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_DISCON);
438  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
439 
440  status = getSn_SR(s);
441  if(status == SOCK_CLOSED) return SOCKERR_WRONG_STATUS;
442  else tcp_close_elapse[s] = wizpf_get_systick();
443 
444  return RET_OK;
445 }
446 
447 int8 TCPCloseCHK(uint8 s)
448 {
449 #define TIMEOUT_CLOSE_WAIT 200
450  uint8 status;
451 
452  if(s > TOTAL_SOCK_NUM) {
453  ERRA("wrong socket number(%d)", s);
454  return SOCKERR_NOT_TCP;
455  } else DBG("start");
456 
457  status = getSn_SR(s);
458  if(status == SOCK_CLOSED) goto END_OK;
459  else if(wizpf_tick_elapse(tcp_close_elapse[s]) < TIMEOUT_CLOSE_WAIT)
460  return SOCKERR_BUSY;
461 
462  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
463  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
464  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_CLOSE);
465  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
466 
467 END_OK:
468  //IINCHIP_WRITE(Sn_IR(s), 0xFF); // interrupt all clear
469  IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, 0xFF); // interrupt all clear
470  return RET_OK;
471 }
472 
473 int8 TCPClsRcvCHK(uint8 s)
474 {
475 #define TIMEOUT_CLOSE_WAIT 200
476  uint8 status;
477 
478  if(s > TOTAL_SOCK_NUM) {
479  ERRA("wrong socket number(%d)", s);
480  return SOCKERR_NOT_TCP;
481  }
482 
483  status = getSn_SR(s);
484  if(status == SOCK_CLOSED) goto END_OK;
485  if(status == SOCK_CLOSE_WAIT) {
486  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
487  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
488  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_CLOSE);
489  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
490  } else return SOCKERR_BUSY;
491 
492 END_OK:
493  //IINCHIP_WRITE(Sn_IR(s), 0xFF); // interrupt all clear
494  IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, 0xFF); // interrupt all clear
495  return RET_OK;
496 }
497 
498 int32 TCPSend(uint8 s, const int8 *buf, uint16 len)
499 {
500  int32 ret;
501 
502  while(1) {
503  ret = TCPSendNB(s, buf, len);
504  if(ret == RET_OK) break;
505  if(ret != SOCKERR_BUSY) return ret;
506  }
507 
508  while(1) {
509  ret = TCPSendCHK(s);
510  if(ret >= 0 || ret != SOCKERR_BUSY) break;
511  }
512 
513  return ret;
514 }
515 
516 int8 TCPSendNB(uint8 s, const int8 *buf, uint16 len)
517 {
518  uint8 status = 0;
519 
520  if(s > TOTAL_SOCK_NUM) {
521  ERRA("wrong socket number(%d)", s);
522  return SOCKERR_NOT_TCP;
523  } else if(len == 0) {
524  ERR("Zero length");
525  return SOCKERR_WRONG_ARG;
526  } else DBG("start");
527 
528  status = getSn_SR(s);
529  if(status == SOCK_CLOSED) return SOCKERR_CLOSED;
530  //if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
531  if((IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
532 
533  if(status == SOCK_FIN_WAIT) return SOCKERR_FIN_WAIT;
534  if(status != SOCK_ESTABLISHED && status != SOCK_CLOSE_WAIT) return SOCKERR_NOT_ESTABLISHED;
535 
536  init_windowfull_retry_cnt(s);
537  if(len > getIINCHIP_TxMAX(s)) len = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
538  if(GetSocketTxFreeBufferSize(s) < len) return SOCKERR_BUSY;
539 
540  send_data_processing(s, (uint8*)buf, len); // copy data
541 
542  //txrd_checker[s] = IINCHIP_READ(Sn_TX_RD0(s));
543  //txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
544 
545  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
546  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
547 
548  txrd_checker[s] = IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 0);
549  txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 1);
550 
551  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR,Sn_CR_SEND);
552  while (IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
553 
554  return RET_OK;
555 }
556 
557 int32 TCPReSend(uint8 s)
558 {
559  int32 ret;
560 
561  while(1) {
562  ret = TCPReSendNB(s);
563  if(ret == RET_OK) break;
564  if(ret != SOCKERR_BUSY) return ret;
565  }
566 
567  while(1) {
568  ret = TCPSendCHK(s);
569  if(ret >= 0 || ret != SOCKERR_BUSY) break;
570  }
571 
572  return ret;
573 }
574 
575 int8 TCPReSendNB(uint8 s)
576 {
577  uint8 status=0;
578 
579  if(s > TOTAL_SOCK_NUM) {
580  ERRA("wrong socket number(%d)", s);
581  return SOCKERR_NOT_TCP;
582  } else DBG("start");
583 
584  status = getSn_SR(s);
585  if(status == SOCK_CLOSED) return SOCKERR_CLOSED;
586  //if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
587  if((IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
588 
589  if(status == SOCK_FIN_WAIT) return SOCKERR_FIN_WAIT;
590  if(status != SOCK_ESTABLISHED && status != SOCK_CLOSE_WAIT) return SOCKERR_NOT_ESTABLISHED;
591 
592  status = incr_windowfull_retry_cnt(s);
593  if(status == 1) tcp_resend_elapse[s] = wizpf_get_systick();
594  else if(status > WINDOWFULL_MAX_RETRY_NUM) return SOCKERR_WINDOW_FULL;
595  else if(wizpf_tick_elapse(tcp_resend_elapse[s]) < WINDOWFULL_WAIT_TIME)
596  return SOCKERR_BUSY;
597 
598  //txrd_checker[s] = IINCHIP_READ(Sn_TX_RD0(s));
599  //txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
600 
601  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
602  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
603 
604  txrd_checker[s] = IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 0);
605  txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 1);
606 
607  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR,Sn_CR_SEND);
608  while (IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
609 
610  return RET_OK;
611 }
612 
613 int32 TCPSendCHK(uint8 s)
614 {
615  uint16 txrd;
616 
617  //if(!(IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK)) {
618  //if(IINCHIP_READ(Sn_SR(s)) == SOCK_CLOSED) {
619  if(!(IINCHIP_READ_SOCKETREG(s, WIZS_IR) & Sn_IR_SEND_OK)) {
620  if(IINCHIP_READ_SOCKETREG(s, WIZS_SR) == SOCK_CLOSED) {
621  DBG("SOCK_CLOSED");
622  TCPClose(s);
623  return SOCKERR_CLOSED;
624  }
625  return SOCKERR_BUSY;
626  //} else IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
627  } else IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, Sn_IR_SEND_OK);
628 
629  //txrd = IINCHIP_READ(Sn_TX_RD0(s));
630  //txrd = (txrd << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
631 
632  txrd = IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 0);
633  txrd = (txrd << 8) + IINCHIP_READ_SOCKETREG(s, WIZS_TX_RD0 + 1);
634 
635  if(txrd > txrd_checker[s]) return txrd - txrd_checker[s];
636  else return (0xffff - txrd_checker[s]) + txrd + 1;
637 }
638 
639 int32 TCPRecv(uint8 s, int8 *buf, uint16 len)
640 {
641  uint8 status = 0;
642  uint16 RSR_len = 0;
643 
644  if(s > TOTAL_SOCK_NUM) {
645  ERRA("wrong socket number(%d)", s);
646  return SOCKERR_NOT_TCP;
647  } else if(len == 0) {
648  ERR("Zero length");
649  return SOCKERR_WRONG_ARG;
650  }
651 
652  RSR_len = GetSocketRxRecvBufferSize(s); // Check Receive Buffer
653  if(RSR_len == 0){
654  status = getSn_SR(s);
655  if(status == SOCK_CLOSED) return SOCKERR_CLOSED;
656  //if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
657  if((IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x0F) != Sn_MR_TCP) return SOCKERR_NOT_TCP;
658 
659  if(status == SOCK_CLOSE_WAIT) return SOCKERR_CLOSE_WAIT;
660  if(status != SOCK_ESTABLISHED && status != SOCK_CLOSE_WAIT) return SOCKERR_NOT_ESTABLISHED;
661  } else {
662  if(len < RSR_len) RSR_len = len;
663  recv_data_processing(s, (uint8*)buf, RSR_len);
664  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
665  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
666 
667  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV);
668  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
669  }
670 
671  return RSR_len;
672 }
673 
674 int8 UDPOpen(uint8 s, uint16 port)
675 {
676  if(s > TOTAL_SOCK_NUM) {
677  ERRA("wrong socket number(%d)", s);
678  return SOCKERR_NOT_UDP;
679  } else DBG("start");
680 
681  if (port == 0) { // if don't set the source port, set local_port number.
682  if(local_port == 0xffff) local_port = 0xc000;
683  else local_port++;
684  port = local_port;
685  }
686 
687  UDPClose(s);
688 
689  //IINCHIP_WRITE(Sn_MR(s),Sn_MR_UDP);
690  //IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
691  //IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
692  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR
693  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
694  //DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
695 
696  IINCHIP_WRITE_SOCKETREG(s, WIZS_MR, Sn_MR_UDP);
697  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 0, (uint8)((port & 0xff00) >> 8));
698  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 + 1, (uint8)(port & 0x00ff));
699  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_OPEN); // run sockinit Sn_CR
700  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
701  DBGA("Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ_SOCKETREG(s, WIZS_SR), IINCHIP_READ_SOCKETREG(s, WIZS_MR));
702 
703  return RET_OK;
704 }
705 
706 int8 UDPClose(uint8 s)
707 {
708  if(s > TOTAL_SOCK_NUM) {
709  ERRA("wrong socket number(%d)", s);
710  return SOCKERR_NOT_UDP;
711  } else DBG("start");
712 
713  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
714  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
715  //IINCHIP_WRITE(Sn_IR(s), 0xFF); // interrupt all clear
716 
717  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_CLOSE);
718  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
719  IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, 0xFF); // interrupt all clear
720 
721  return RET_OK;
722 }
723 
724 int32 UDPSend(uint8 s, const int8 *buf, uint16 len, uint8 *addr, uint16 port)
725 {
726  int32 ret = 0;
727 
728  ret = UDPSendNB(s, buf, len, addr, port);
729  if(ret < RET_OK) return ret;
730  else len = ret;
731 
732  do {
733  ret = UDPSendCHK(s);
734  if(ret == RET_OK) return len;
735  if(ret != SOCKERR_BUSY) return ret;
736  } while(1);
737 }
738 
739 int32 UDPSendNB(uint8 s, const int8 *buf, uint16 len, uint8 *addr, uint16 port)
740 {
741  uint8 srcip[4], snmask[4], status = 0;
742 
743  if(s > TOTAL_SOCK_NUM) {
744  ERRA("wrong socket number(%d)", s);
745  return SOCKERR_NOT_UDP;
746  } else if(len == 0 || addr == NULL) {
747  if(len == 0) ERR("Zero length");
748  else ERR("NULL Dst IP");
749  return SOCKERR_WRONG_ARG;
750  } else DBG("start");
751 
752  status = getSn_SR(s);
753  if(status == SOCK_CLOSED) return SOCKERR_CLOSED;
754  //if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_UDP) return SOCKERR_NOT_UDP;
755  if((IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x0F) != Sn_MR_UDP) return SOCKERR_NOT_UDP;
756  if(status != SOCK_UDP) return SOCKERR_NOT_UDP;
757 
758  if (len > getIINCHIP_TxMAX(s)) len = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
759 
760  getSIPR(srcip);
761  getSUBR(snmask);
762 
763  if((addr[0]==0x00 && addr[1]==0x00 && addr[2]==0x00 &&
764  addr[3]==0x00) || (port==0x00))
765  {
766  DBG("invalid ip or port");
767  DBGA("SOCK(%d)-[%02x.%02x.%02x.%02x, %d, %d]",s,
768  addr[0], addr[1], addr[2], addr[3] , port, len);
769  return SOCKERR_WRONG_ARG;
770  }
771  else if( (srcip[0]==0 && srcip[1]==0 && srcip[2]==0 && srcip[3]==0) &&
772  (snmask[0]!=0 || snmask[1]!=0 || snmask[2]!=0 || snmask[3]!=0) ) //Mikej : ARP Errata
773  {
774  DBG("Source IP is NULL while SN Mask is Not NULL");
775  return SOCKERR_NULL_SRC_IP;
776  }
777  else
778  {
779  //IINCHIP_WRITE(Sn_DIPR0(s),addr[0]);
780  //IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
781  //IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
782  //IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
783  //IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8));
784  //IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff));
785  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 0, addr[0]);
786  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 1, addr[1]);
787  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 2, addr[2]);
788  IINCHIP_WRITE_SOCKETREG(s, WIZS_DIPR0 + 3, addr[3]);
789  IINCHIP_WRITE_SOCKETREG(s, WIZS_DPORT0 + 0,(uint8)((port & 0xff00) >> 8));
790  IINCHIP_WRITE_SOCKETREG(s, WIZS_DPORT0 + 1,(uint8)(port & 0x00ff));
791 
792  send_data_processing(s, (uint8*)buf, len); // copy data
793  //SetSubnet(sn); // for ARP Errata
794 
795  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
796  //while(IINCHIP_READ(Sn_CR(s))); // wait to process the command...
797  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_SEND);
798  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // wait to process the command...
799  }
800 
801  return len;
802 }
803 
804 int8 UDPSendCHK(uint8 s)
805 {
806  //uint8 ir = IINCHIP_READ(Sn_IR(s));
807  uint8 ir = IINCHIP_READ_SOCKETREG(s, WIZS_IR);
808 
809  //DBGA("WATCH UDP Send CHK - sock(%d)", s);
810  if(!(ir & Sn_IR_SEND_OK)) {
811  if(ir & Sn_IR_TIMEOUT) {
812  DBG("send fail");
813  //IINCHIP_WRITE(Sn_IR(s), (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); // clear SEND_OK & TIMEOUT
814  IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); // clear SEND_OK & TIMEOUT Interrupt
815  return SOCKERR_TIME_OUT;
816  }
817  return SOCKERR_BUSY;
818  //} else IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
819  } else IINCHIP_WRITE_SOCKETREG(s, WIZS_IR, Sn_IR_SEND_OK);
820  //ClearSubnet(); // for ARP Errata
821 
822  return RET_OK;
823 }
824 
825 int32 UDPRecv(uint8 s, int8 *buf, uint16 len, uint8 *addr, uint16 *port)
826 {
827  uint8 prebuf[8], status = 0;
828  uint16 tmp_len = 0, RSR_len = 0;
829 
830  if(s > TOTAL_SOCK_NUM) {
831  ERRA("wrong socket number(%d)", s);
832  return SOCKERR_NOT_UDP;
833  } else if(len == 0) {
834  ERR("Zero length");
835  return SOCKERR_WRONG_ARG;
836  }
837 
838  status = getSn_SR(s);
839  if(status == SOCK_CLOSED) return SOCKERR_CLOSED;
840  //if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_UDP) return SOCKERR_NOT_UDP;
841  if((IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x0F) != Sn_MR_UDP) return SOCKERR_NOT_UDP;
842  if(status != SOCK_UDP) return SOCKERR_NOT_UDP;
843 
844  RSR_len = GetSocketRxRecvBufferSize(s); // Check Receive Buffer of W5500
845  if(RSR_len < 8) {
846  DBGA("wrong data received (%d)", RSR_len);
847  recv_data_ignore(s, RSR_len);
848  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
849  //while(IINCHIP_READ(Sn_CR(s)));
850 
851  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV);
852  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR));
853 
854  return SOCKERR_NOT_SPECIFIED;
855  } else {
856  recv_data_processing(s, prebuf, 8);
857  //IINCHIP_WRITE(Sn_CR(s), Sn_CR_RECV); // 데이터를 처리한 후 이것을 해줘야 적용됨
858  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV); // 데이터를 처리한 후 이것을 해줘야 적용됨
859 
860  if(addr) { // read peer's IP address, port number.
861  addr[0] = prebuf[0];
862  addr[1] = prebuf[1];
863  addr[2] = prebuf[2];
864  addr[3] = prebuf[3];
865  }
866  if(port) {
867  *port = prebuf[4];
868  *port = (*port << 8) + prebuf[5];
869  }
870  tmp_len = prebuf[6];
871  tmp_len = (tmp_len << 8) + prebuf[7];
872  //while(IINCHIP_READ(Sn_CR(s))); // IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV); 명령 후 해야함, 시간 벌려고 바로 안함
873 
874  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR)); // IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV); 명령 후 해야함, 시간 벌려고 바로 안함
875 
876  DBGA("UDP Recv - addr(%d.%d.%d.%d:%d), t(%d), R(%d)",
877  addr[0], addr[1], addr[2], addr[3], *port, tmp_len, RSR_len);
878  if(tmp_len == 0) {
879  ERR("UDP Recv len Zero - remove rest all");
880  recv_data_ignore(s, GetSocketRxRecvBufferSize(s));
881  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
882  //while(IINCHIP_READ(Sn_CR(s)));
883 
884  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV);
885  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR));
886 
887  return SOCKERR_NOT_SPECIFIED;
888  }
889  RSR_len = tmp_len;
890  }
891 
892  if(len < RSR_len) {
893  tmp_len = RSR_len - len;
894  RSR_len = len;
895  DBGA("Recv buffer not enough - len(%d)", len);
896  } else tmp_len = 0;
897 
898  //switch (IINCHIP_READ(Sn_MR(s)) & 0x07)
899  switch (IINCHIP_READ_SOCKETREG(s, WIZS_MR) & 0x07)
900  {
901  case Sn_MR_UDP:
902  recv_data_processing(s, (uint8*)buf, RSR_len);
903  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
904  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV);
905 
906  if(tmp_len) {
907  //while(IINCHIP_READ(Sn_CR(s)));
908 
909  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR));
910  DBG("Ignore rest data");
911  recv_data_ignore(s, tmp_len); // 안버리면 이후 처리가 곤란함
912  //IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
913  //while(IINCHIP_READ(Sn_CR(s)));
914 
915  IINCHIP_WRITE_SOCKETREG(s, WIZS_CR, Sn_CR_RECV);
916  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR));
917  tmp_len = GetSocketRxRecvBufferSize(s);
918  if(tmp_len) DBGA("another rest data(%d)", tmp_len);
919  else DBG("No rest data");
920  }
921  break;
922  case Sn_MR_IPRAW:
923  case Sn_MR_MACRAW:
924  default :
925  break;
926  }
927  //while(IINCHIP_READ(Sn_CR(s)));
928  while(IINCHIP_READ_SOCKETREG(s, WIZS_CR));
929 
930  return RSR_len;
931 }
932 
933 
934 
935 
936 
937 
938 
939 
940 
941 
942 
943 
944 
945 
946 
947 #if 0
948 uint8 socket(uint8 s, uint8 protocol, uint16 port, uint8 flag)
949 {
950  uint8 ret;
951 #ifdef __DEF_IINCHIP_DBG__
952  printf("socket()\r\n");
953 #endif
954  if ( ((protocol&0x0F) == Sn_MR_TCP) || ((protocol&0x0F) == Sn_MR_UDP) || ((protocol&0x0F) == Sn_MR_IPRAW) || ((protocol&0x0F) == Sn_MR_MACRAW) || ((protocol&0x0F) == Sn_MR_PPPOE) )
955  {
956  close(s);
957  IINCHIP_WRITE_SOCKETREG(s, WIZS_MR ,protocol | flag);
958  if (port != 0) {
959  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 ,(uint8)((port & 0xff00) >> 8));
960  IINCHIP_WRITE_SOCKETREG(s, (WIZS_PORT0 + 1),(uint8)(port & 0x00ff));
961  } else {
962  local_port++; // if don't set the source port, set local_port number.
963  IINCHIP_WRITE_SOCKETREG(s, WIZS_PORT0 ,(uint8)((local_port & 0xff00) >> 8));
964  IINCHIP_WRITE_SOCKETREG(s, (WIZS_PORT0 + 1),(uint8)(local_port & 0x00ff));
965  }
966  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_OPEN); // run sockinit Sn_CR
967 
968  /* wait to process the command... */
969  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) )
970  ;
971  /* ------- */
972  ret = 1;
973  }
974  else
975  {
976  ret = 0;
977  }
978 #ifdef __DEF_IINCHIP_DBG__
979  printf("Sn_SR = %.2x , Protocol = %.2x\r\n", IINCHIP_READ_SOCKETREG(s, WIZC_SR ), IINCHIP_READ_SOCKETREG(s, WIZC_MR ));
980 #endif
981  return ret;
982 }
983 
984 void close(uint8 s)
985 {
986 #ifdef __DEF_IINCHIP_DBG__
987  printf("close()\r\n");
988 #endif
989 
990  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_CLOSE);
991 
992  /* wait to process the command... */
993  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) )
994  ;
995  /* ------- */
996  /* all clear */
997  IINCHIP_WRITE_SOCKETREG( s, WIZS_IR , 0xFF);
998 }
999 
1000 uint8 listen(uint8 s)
1001 {
1002  uint8 ret;
1003 #ifdef __DEF_IINCHIP_DBG__
1004  printf("listen()\r\n");
1005 #endif
1006  if (IINCHIP_READ_SOCKETREG(s, WIZS_SR ) == SOCK_INIT)
1007  {
1008  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_LISTEN);
1009  /* wait to process the command... */
1010  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) )
1011  ;
1012  /* ------- */
1013  ret = 1;
1014  }
1015  else
1016  {
1017  ret = 0;
1018 #ifdef __DEF_IINCHIP_DBG__
1019  printf("Fail[invalid ip,port]\r\n");
1020 #endif
1021  }
1022  return ret;
1023 }
1024 
1025 uint8 connect(uint8 s, uint8 * addr, uint16 port)
1026 {
1027  uint8 ret;
1028 #ifdef __DEF_IINCHIP_DBG__
1029  printf("connect()\r\n");
1030 #endif
1031  if
1032  (
1033  ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
1034  ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
1035  (port == 0x00)
1036  )
1037  {
1038  ret = 0;
1039 #ifdef __DEF_IINCHIP_DBG__
1040  printf("Fail[invalid ip,port]\r\n");
1041 #endif
1042  }
1043  else
1044  {
1045  ret = 1;
1046  // set destination IP
1047  IINCHIP_WRITE_SOCKETREG( s, WIZS_DIPR0 ,addr[0]);
1048  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 1),addr[1]);
1049  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 2),addr[2]);
1050  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 3),addr[3]);
1051  IINCHIP_WRITE_SOCKETREG( s, WIZS_DPORT0 ,(uint8)((port & 0xff00) >> 8));
1052  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DPORT0 + 1),(uint8)(port & 0x00ff));
1053 
1054  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_CONNECT);
1055  /* wait for completion */
1056  while ( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) ) ;
1057 
1058 
1059  }
1060 
1061  return ret;
1062 }
1063 
1064 void disconnect(uint8 s)
1065 {
1066 #ifdef __DEF_IINCHIP_DBG__
1067  printf("disconnect()\r\n");
1068 #endif
1069  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_DISCON);
1070 
1071  /* wait to process the command... */
1072  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) )
1073  ;
1074  /* ------- */
1075 }
1076 
1077 #define __FERRSIZE_PRINF__
1078 #ifdef __FERRSIZE_PRINF__
1079 //uint16 pre_freesize = 0;
1080 #endif
1081 uint16 send(uint8 s, const uint8 * buf, uint16 len)
1082 {
1083  uint8 status=0;
1084  uint16 ret=0;
1085  uint16 freesize=0;
1086 
1087 #ifdef __DEF_IINCHIP_DBG__
1088  printf("send()\r\n");
1089 #endif
1090  if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
1091  else ret = len;
1092 
1093  // if freebuf is available, start.
1094  do
1095  {
1096  freesize = getSn_TX_FSR(s);
1097 #ifdef __FERRSIZE_PRINF__
1098  if(freesize < ret)
1099  {
1100  //if(pre_freesize != freesize)
1101  //{
1102  //printf("socket %d freesize(%d) empty or error, dlen : %d \r\n", s, freesize, ret);
1103  //pre_freesize = freesize;
1104  //}
1105  }
1106 #endif
1107  }while (freesize < ret);
1108 
1109  // copy data
1110  send_data_processing(s, (uint8 *)buf, ret);
1111  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_SEND);
1112 
1113  /* wait to process the command... */
1114  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) );
1115 
1116  while ( (IINCHIP_READ_SOCKETREG(s, WIZS_IR ) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
1117  {
1118 
1119  status = IINCHIP_READ_SOCKETREG(s, WIZS_SR);
1120  if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT) )
1121  {
1122  printf("CH: %d Impossible case 1, socket state: %x\r\n", s, status);
1123  //ret = 0;
1124  //break;
1125  }
1126 
1127  if(IINCHIP_READ_SOCKETREG(s, WIZS_IR ) == SOCK_CLOSED)
1128  {
1129  printf("SEND_OK Problem!!\r\n");
1130  close(s);
1131  return 0;
1132  }
1133  }
1134  IINCHIP_WRITE_SOCKETREG( s, WIZS_IR , Sn_IR_SEND_OK);
1135 
1136 #ifdef __DEF_IINCHIP_INT__
1137  putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
1138 #else
1139  IINCHIP_WRITE_SOCKETREG( s, WIZS_IR , Sn_IR_SEND_OK);
1140 #endif
1141 
1142  return ret;
1143 }
1144 
1145 uint16 recv(uint8 s, uint8 * buf, uint16 len)
1146 {
1147  uint16 ret=0;
1148 #ifdef __DEF_IINCHIP_DBG__
1149  printf("recv()\r\n");
1150 #endif
1151  if ( len > 0 )
1152  {
1153  recv_data_processing(s, buf, len);
1154  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_RECV);
1155  /* wait to process the command... */
1156  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ));
1157  /* ------- */
1158  ret = len;
1159  }
1160  return ret;
1161 }
1162 
1163 uint16 sendto(uint8 s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port)
1164 {
1165  uint16 ret=0;
1166 
1167 #ifdef __DEF_IINCHIP_DBG__
1168  printf("sendto()\r\n");
1169 #endif
1170  if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
1171  else ret = len;
1172 
1173  if( ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || ((port == 0x00)) )//||(ret == 0) )
1174  {
1175  /* added return value */
1176  ret = 0;
1177 #ifdef __DEF_IINCHIP_DBG__
1178  printf("%d Fail[%.2x.%.2x.%.2x.%.2x, %.d, %d]\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len);
1179  printf("Fail[invalid ip,port]\r\n");
1180 #endif
1181  }
1182  else
1183  {
1184 #ifndef __DEF_IINCHIP_DBG__
1185 printf("\r\nDestination IP : %d.%d.%d.%d", addr[0],addr[1],addr[2],addr[3]);
1186 #endif
1187  IINCHIP_WRITE_SOCKETREG( s, WIZS_DIPR0 ,addr[0]);
1188  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 1),addr[1]);
1189  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 2),addr[2]);
1190  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DIPR0 + 3),addr[3]);
1191  IINCHIP_WRITE_SOCKETREG( s, WIZS_DPORT0 ,(uint8)((port & 0xff00) >> 8));
1192  IINCHIP_WRITE_SOCKETREG( s, (WIZS_DPORT0 + 1),(uint8)(port & 0x00ff));
1193  // copy data
1194  send_data_processing(s, (uint8 *)buf, ret);
1195 
1196  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_SEND);
1197  /* wait to process the command... */
1198  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR ) )
1199  ;
1200  /* ------- */
1201 
1202  while( (IINCHIP_READ_SOCKETREG(s, WIZS_IR ) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
1203  {
1204  if (IINCHIP_READ_SOCKETREG(s, WIZS_IR ) & Sn_IR_TIMEOUT)
1205  {
1206 #ifndef __DEF_IINCHIP_DBG__
1207  printf("\r\n Sendto fail.\r\n");
1208 #endif
1209  /* clear interrupt */
1210  IINCHIP_WRITE_SOCKETREG( s, WIZS_IR , (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT Interrupt */
1211  return 0;
1212  }
1213  }
1214 
1215  IINCHIP_WRITE_SOCKETREG( s, WIZS_IR , Sn_IR_SEND_OK);
1216  }
1217  return ret;
1218 }
1219 
1220 uint16 recvfrom(uint8 s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port)
1221 {
1222  uint8 head[8];
1223  uint16 data_len=0;
1224  uint16 ptr=0;
1225 #ifdef __DEF_IINCHIP_DBG__
1226  printf("recvfrom()\r\n");
1227 #endif
1228 
1229  if ( len > 0 )
1230  {
1231  ptr = IINCHIP_READ_SOCKETREG(s, WIZS_RX_RD0 );
1232  ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ_SOCKETREG(s, WIZS_RX_RD0 + 1);
1233 #ifdef __DEF_IINCHIP_DBG__
1234  printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
1235 #endif
1236  switch (IINCHIP_READ_SOCKETREG(s, WIZC_MR ) & 0x07)
1237  {
1238  case Sn_MR_UDP :
1239  IINCHIP_READ_RXBUF_SEQ(s, ptr, 0x08, head);
1240  ptr += 8;
1241  // read peer's IP address, port number.
1242  addr[0] = head[0];
1243  addr[1] = head[1];
1244  addr[2] = head[2];
1245  addr[3] = head[3];
1246  *port = head[4];
1247  *port = (*port << 8) + head[5];
1248  data_len = head[6];
1249  data_len = (data_len << 8) + head[7];
1250 
1251 #ifndef __DEF_IINCHIP_DBG__
1252  printf("UDP msg arrived\r\n");
1253  printf("source Port : %d\r\n", *port);
1254  printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
1255 #endif
1256 
1257  IINCHIP_READ_RXBUF_SEQ(s, ptr, data_len, buf);
1258  ptr += data_len;
1259 
1260  IINCHIP_WRITE_SOCKETREG( s, WIZS_RX_RD0 ,(uint8)((ptr & 0xff00) >> 8));
1261  IINCHIP_WRITE_SOCKETREG( s, (WIZS_RX_RD0 + 1),(uint8)(ptr & 0x00ff));
1262  break;
1263 
1264  case Sn_MR_IPRAW :
1265  IINCHIP_READ_RXBUF_SEQ(s, ptr, 0x06, head);
1266  ptr += 6;
1267 
1268  addr[0] = head[0];
1269  addr[1] = head[1];
1270  addr[2] = head[2];
1271  addr[3] = head[3];
1272  data_len = head[4];
1273  data_len = (data_len << 8) + head[5];
1274 
1275 #ifdef __DEF_IINCHIP_DBG__
1276  printf("IP RAW msg arrived\r\n");
1277  printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
1278 #endif
1279 
1280  IINCHIP_READ_RXBUF_SEQ(s, ptr, data_len, buf);
1281  ptr += data_len;
1282 
1283 
1284  IINCHIP_WRITE_SOCKETREG( s, WIZS_RX_RD0 ,(uint8)((ptr & 0xff00) >> 8));
1285  IINCHIP_WRITE_SOCKETREG( s, (WIZS_RX_RD0 + 1),(uint8)(ptr & 0x00ff));
1286  break;
1287  case Sn_MR_MACRAW :
1288  IINCHIP_READ_RXBUF_SEQ(s, ptr, 2, head);
1289  ptr+=2;
1290  data_len = head[0];
1291  data_len = (data_len<<8) + head[1] - 2;
1292  if(data_len > 1514)
1293  {
1294  printf("data_len over 1514\r\n");
1295  while(1);
1296  }
1297 
1298  IINCHIP_READ_RXBUF_SEQ(s, ptr, data_len, buf);
1299 
1300  ptr += data_len;
1301 
1302  IINCHIP_WRITE_SOCKETREG( s, WIZS_RX_RD0 ,(uint8)((ptr & 0xff00) >> 8));
1303  IINCHIP_WRITE_SOCKETREG( s, (WIZS_RX_RD0 + 1),(uint8)(ptr & 0x00ff));
1304 
1305 #ifdef __DEF_IINCHIP_DGB__
1306  printf("MAC RAW msg arrived\r\n");
1307  printf("dest mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
1308  printf("src mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]);
1309  printf("type =%.2X%.2X\r\n",buf[12],buf[13]);
1310 #endif
1311  break;
1312 
1313  default :
1314  break;
1315  }
1316  IINCHIP_WRITE_SOCKETREG( s, WIZS_CR ,Sn_CR_RECV);
1317 
1318  /* wait to process the command... */
1319  while( IINCHIP_READ_SOCKETREG(s, WIZS_CR) ) ;
1320  /* ------- */
1321  }
1322 #ifdef __DEF_IINCHIP_DBG__
1323  printf("recvfrom() end ..\r\n");
1324 #endif
1325  return data_len;
1326 }
1327 
1328 void macraw_open(void)
1329 {
1330  uint8 sock_num;
1331  uint16 dummyPort = 0;
1332  uint8 mFlag = 0;
1333  sock_num = 0;
1334 
1335 
1336  close(sock_num); // Close the 0-th socket
1337  socket(sock_num, Sn_MR_MACRAW, dummyPort,mFlag); // OPen the 0-th socket with MACRAW mode
1338 }
1339 
1340 
1341 uint16 macraw_send( const uint8 * buf, uint16 len )
1342 {
1343  uint16 ret = 0;
1344  int16 idx = 0;
1345  uint8 sock_num;
1346  sock_num = 0;
1347 
1348 #ifndef __DEF_IINCHIP_DBG__
1349  printf("macsend()\r\n");
1350 #endif
1351 
1352  if (len > getIINCHIP_TxMAX(sock_num)) ret = getIINCHIP_TxMAX(sock_num); // check size not to exceed MAX size.
1353  else ret = len;
1354 
1355 #ifdef __DEF_IINCHIP_DGB__
1356  printf("ret : %d \r\n", ret);
1357  printf("MAC RAW msg SEND\r\n");
1358  printf("dest mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
1359  printf("src mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]);
1360  printf("type =%.2X%.2X\r\n",buf[12],buf[13]);
1361  for(idx=0; idx<(ret-14); idx++)
1362  {
1363  if( (idx%16)==0 ) printf("\r\n");
1364  printf("%.2X ", buf[idx+14]);
1365  }
1366  printf("\r\n");
1367 #endif
1368 
1369  send_data_processing(sock_num, (uint8 *)buf, len);
1370 
1371  //W5400 SEND COMMAND
1372  IINCHIP_WRITE_SOCKETREG(sock_num, WIZS_CR,Sn_CR_SEND);
1373  while( IINCHIP_READ_SOCKETREG(sock_num, WIZS_CR) );
1374  while ( (IINCHIP_READ_SOCKETREG(sock_num, WIZS_IR) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK );
1375  IINCHIP_WRITE_SOCKETREG(sock_num, WIZS_IR, Sn_IR_SEND_OK);
1376 
1377 #ifdef __DEF_IINCHIP_DBG__
1378  printf("macsend() over\r\n");
1379 #endif
1380  return ret;
1381 }
1382 
1383 uint16 macraw_recv( uint8 * buf, uint16 len )
1384 {
1385  uint8 sock_num;
1386  uint16 data_len=0;
1387  uint16 dummyPort = 0;
1388  uint16 ptr = 0;
1389  uint8 mFlag = 0;
1390  sock_num = 0;
1391 
1392 #ifdef __DEF_IINCHIP_DBG__
1393  printf("macrecv()\r\n");
1394 #endif
1395 
1396  if ( len > 0 )
1397  {
1398 
1399 #ifdef __DEF_IINCHIP_DBG__
1400  printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
1401 #endif
1402  data_len = 0;
1403 
1404  ptr = IINCHIP_READ_SOCKETREG(0, WIZS_RX_RD0);
1405  ptr = (uint16)((ptr & 0x00ff) << 8) + IINCHIP_READ_SOCKETREG(0, WIZS_RX_RD0 + 1);
1406  //-- read_data(s, (uint8 *)ptr, data, len); // read data
1407  data_len = IINCHIP_READ_RXBUF(0, ptr);
1408  ptr++;
1409  data_len = ((data_len<<8) + IINCHIP_READ_RXBUF(0, ptr)) - 2;
1410  ptr++;
1411 
1412 #ifdef __DEF_IINCHIP_DBG__
1413  printf("\r\nptr: %X, data_len: %X", ptr, data_len);
1414 #endif
1415  if(data_len > 1514)
1416  {
1417  printf("data_len over 1514\r\n");
1418  printf("\r\nptr: %X, data_len: %X", ptr, data_len);
1419 
1420 #if 0
1421  IINCHIP_READ_RXBUF_SEQ(sock_num, 0, 64, (uint8*)(buf));
1422  uint16 idx;
1423  //for(idx=0; idx<data_len; idx++)
1424  for(idx=0; idx<1024; idx++)
1425  {
1426  if(idx>0 && (idx%16)==0) printf("\r\n");
1427  printf("%.2X, ", buf[idx]);
1428  }
1429  printf("------------\r\n");
1430 #endif
1431  while(1);
1433  close(sock_num); // Close the 0-th socket
1434  socket(sock_num, Sn_MR_MACRAW, dummyPort,mFlag); // OPen the 0-th socket with MACRAW mode
1435  return 0;
1436  }
1437 
1438  IINCHIP_READ_RXBUF_SEQ(sock_num, ptr, data_len, (uint8*)(buf));
1439  ptr += data_len;
1440 #ifdef __DEF_IINCHIP_DBG__
1441  printf("ptr: %X \r\n", ptr);
1442 #endif
1443  IINCHIP_WRITE_SOCKETREG(sock_num, WIZS_RX_RD0,(uint8)((ptr & 0xff00) >> 8));
1444  IINCHIP_WRITE_SOCKETREG(sock_num, (WIZS_RX_RD0 + 1),(uint8)(ptr & 0x00ff));
1445  IINCHIP_WRITE_SOCKETREG(sock_num, WIZS_CR, Sn_CR_RECV);
1446  while( IINCHIP_READ_SOCKETREG(sock_num, WIZS_CR) ) ;
1447  }
1448 
1449 #ifdef __DEF_IINCHIP_DBG__
1450  printf("macrecv() end ..\r\n");
1451 #endif
1452 
1453  return data_len;
1454 }
1455 
1456 
1457 #endif