#include // to use printf #include "serial.h" #include "types.h" #include "loopback.h" #include "TCPIPcore.h" #include "socket.h" #include "ethernetDataProcessor.h" #include "serialDataProcessor.h" #include "ethernetDebugger.h" /*############################################################################## * Definition *############################################################################*/ /*############################################################################## * macro *############################################################################*/ /*############################################################################## * global veriable *############################################################################*/ volatile static uint8 tcpip_status=0; uint8 firsttime = 0; /*############################################################################## * Functions *############################################################################*/ /************************************************************************************************ Loopback TCPS ************************************************************************************************/ void loopback_tcps(SOCKET s, uint16 port, uint16 mode) { uint16 e_len; uint8 socket_i; uint8 ir; uint8 ir2; uint8 temp; uint8 com_imr; uint8 i=0; uint8 status; uint8 destip[4]; uint16 destport; uint8 data_buf[1024]; status = getSn_SR(s); MSG("getSn_SR : %02x",status,0,0); // modified by ldk (add Debug MSG) if(tcpip_status != status) tcpip_status = status; /* Serial to Ethernet */ //serialDataProcessor(s, status); // serialBypassProcessor(s); switch (status) { // modified by ldk (add UDP loopback) case SOCK_UDP: if(firsttime == 0){ IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV); firsttime = 1; } socket_i = getSn_IR(s); ir = *IR; ir2 = *IR2; com_imr = *IMR; e_len = getSn_RX_RSR(s); MSG("IR : %02x, com_imr : %02x",ir, com_imr, 0); MSG("socket_i : %02x, e_len : %02x, ir2 : %02x", socket_i, e_len, ir2); if(e_len > 0) { MSG("receive success", 0, 0, 0); e_len = recvfrom(s, data_buf, e_len,(uint8 *)destip, &destport); if(e_len != sendto(s, data_buf, e_len, (uint8 *)destip, destport)) { MSG("send fail", 0, 0, 0); }else{ MSG("send success", 0, 0, 0); } }else{ MSG("receive fail", 0, 0, 0); } break; case SOCK_ESTABLISHED: // if connection is established * /* Ethernet to Serial*/ if((e_len = getSn_RX_RSR(s)) > 0) // check Rx data { ethernetDataProcessor(s, e_len); } break; case SOCK_CLOSE_WAIT: /* If the client request to close */ disconnect(s); break; case SOCK_CLOSED: /* if a socket is closed */ close(s); //socket(s,Sn_MR_TCP,port,mode); socket(s,Sn_MR_UDP,port,mode); // modified by ldk (add UDP socket) break; case SOCK_INIT: /* if a socket is initiated */ listen(s); break; } } /************************************************************************************************ Get TCPIP status ************************************************************************************************/ uint8 get_tcpip_status(void) { return tcpip_status; }