uint8_t tcp_socket_init(TCP_CONFIG *config) { static bool startup=false; if(!startup) { GPIO_Configuration(); Reset_W5200(); WIZ_SPI_Init(); Set_network(config->Source_IP, config->Gateway, config->MAC, config->Subnet); startup=true; while (getSn_SR(config->s) != SOCK_CLOSED); } if(socket(config->s,Sn_MR_TCP,config->Source_Port,0x00)== 0) /* reinitialize the socket */ { return 0; //TCP socket failed } while (getSn_SR(config->s) != SOCK_INIT); // Wait for socket to open IINCHIP_WRITE(Sn_CR(config->s), Sn_CR_LISTEN); // Start listening on port specified while (IINCHIP_READ(Sn_CR(config->s))); // Check status while (getSn_SR(config->s) != SOCK_LISTEN); // Wait for listen return 1; }
int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len) { uint8_t tmp=0; uint16_t freesize=0; CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKDATA(); tmp = getSn_SR(sn); if(tmp != SOCK_ESTABLISHED && tmp != SOCK_CLOSE_WAIT) return SOCKERR_SOCKSTATUS; if( sock_is_sending & (1<<sn) ) { tmp = getSn_IR(sn); if(tmp & Sn_IR_SENDOK) { setSn_IR(sn, Sn_IR_SENDOK); #if _WZICHIP_ == 5200 if(getSn_TX_RD(sn) != sock_next_rd[sn]) { setSn_CR(sn,Sn_CR_SEND); while(getSn_CR(sn)); return SOCKERR_BUSY; } #endif sock_is_sending &= ~(1<<sn); } else if(tmp & Sn_IR_TIMEOUT) { WIZCHIP_EXPORT(close)(sn); return SOCKERR_TIMEOUT; } else return SOCK_BUSY; } freesize = getSn_TxMAX(sn); if (len > freesize) len = freesize; // check size not to exceed MAX size. while(1) { freesize = getSn_TX_FSR(sn); tmp = getSn_SR(sn); if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT)) { WIZCHIP_EXPORT(close)(sn); return SOCKERR_SOCKSTATUS; } if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY; if(len <= freesize) break; MICROPY_THREAD_YIELD(); } wiz_send_data(sn, buf, len); #if _WIZCHIP_ == 5200 sock_next_rd[sn] = getSn_TX_RD(sn) + len; #endif setSn_CR(sn,Sn_CR_SEND); /* wait to process the command... */ while(getSn_CR(sn)); sock_is_sending |= (1 << sn); return len; }
//TCP-Server 처리 void ProcessTcpSever(void) { // if len is not volatile, it will overflow // and causes 'cstack' overstack error volatile int len; uint8_t *data_buf = (uint8_t) TX_BUF; uint16 max_bufsize = sizeof(data_buf); unsigned int port = MY_LISTEN_PORT; getSn = getSn_SR(SOCK_TCPS); switch (getSn_SR(SOCK_TCPS)) { case SOCK_INIT: lis = listen(SOCK_TCPS); break; case SOCK_ESTABLISHED: /* check Rx data */ if ((len = getSn_RX_RSR(SOCK_TCPS)) > 0) { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ if (len > max_bufsize) len = max_bufsize; /* the data size to read is MAX_BUF_SIZE. */ /* read the received data */ CheckCounter++; len = recv(SOCK_TCPS, data_buf, len); length = len; SendFlag = True; /* send the received data */ //send(SOCK_TCPS, data_buf, len); } break; case SOCK_CLOSE_WAIT: SendFlag = False; //If the client request to close disconnect(SOCK_TCPS); break; case SOCK_CLOSED: //reinitialize the socket if(socket(SOCK_TCPS, Sn_MR_TCP, port ,0x00) == 0) { printf("Fail to create socket."); } else { SendFlag = False; lis = listen(SOCK_TCPS); } break; } }
int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port) { CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKINIT(); //M20140501 : For avoiding fatal error on memory align mismatched //if( *((uint32_t*)addr) == 0xFFFFFFFF || *((uint32_t*)addr) == 0) return SOCKERR_IPINVALID; { uint32_t taddr; taddr = ((uint32_t)addr[0] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF); if (taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID; } // if(port == 0) return SOCKERR_PORTZERO; setSn_DIPR(sn,addr); setSn_DPORT(sn,port); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR(wizchip_getsubn()); #endif setSn_CR(sn,Sn_CR_CONNECT); while(getSn_CR(sn)); if(sock_io_mode & (1<<sn)) return SOCK_BUSY; while(getSn_SR(sn) != SOCK_ESTABLISHED) { if (getSn_SR(sn) == SOCK_CLOSED) { #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif return SOCKERR_SOCKCLOSED; } if (getSn_IR(sn) & Sn_IR_TIMEOUT) { setSn_IR(sn, Sn_IR_TIMEOUT); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif return SOCKERR_TIMEOUT; } MICROPY_THREAD_YIELD(); } #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif return SOCK_OK; }
void ip_task(void){ switch(sock_state){ case IP_STATE_NO_SOCKET: if(socket(0, Sn_MR_TCP, 5000, 0) == 0) sock_state = IP_STATE_SOCKET; break; case IP_STATE_SOCKET: switch (listen(0)){ case SOCK_OK : sock_state = IP_STATE_LISTEN; break; case SOCKERR_SOCKCLOSED : sock_state = IP_STATE_CLOSE; } break; case IP_STATE_LISTEN : switch(getSn_SR(0)){ case SOCK_LISTEN : _delay_ms(100); break; case SOCK_ESTABLISHED: sock_state = IP_STATE_ECHO; break; default: sock_state = IP_STATE_CLOSE; } break; case IP_STATE_CLOSE: disconnect(0); close(0); sock_state=IP_STATE_NO_SOCKET; break; case IP_STATE_ECHO: if (getSn_SR(0) == SOCK_ESTABLISHED) { sock_state = echo(); } else { sock_state = IP_STATE_CLOSE; } break; case IP_STATE_GREETING: if (getSn_SR(0) == SOCK_ESTABLISHED) { sock_state = greeting(); } else { sock_state = IP_STATE_CLOSE; } break; } }
void do_udp(void){ uint16 len = 0; uint8 buffer[2048];/*定义一个2KB的缓存*/ // uint8 pc_ip[4] = { 192, 168, 10, 10 };/*配置服务器的IP地址*/ // uint16 pc_port = 5000; /*定义计算机的一个端口并初始化*/ uint16 local_port = 5000; if(isGetIP){ switch (getSn_SR(SOCKET_UDP))/*获取socket 0的状态*/ { case SOCK_UDP:/*socket 的套接字初始化完成*/ if (getSn_IR(SOCKET_UDP) & Sn_IR_RECV) { setSn_IR(SOCKET_UDP, Sn_IR_RECV);/*Sn_IR的第0位置1*/ printf("Sendto Fail\r\n"); } if ((len = getSn_RX_RSR(SOCKET_UDP)) > 0) { len = recvfrom(SOCKET_UDP, buffer, len, remote_ip, &remote_port);/*W5500接收计算机发送来的数据*/ printf("%d.%d.%d.%d:%d \r\n", remote_ip[0], remote_ip[1], remote_ip[2], remote_ip[3], remote_port); if (len != sendto(SOCKET_UDP, buffer, len, remote_ip, remote_port)) { printf("Sendto Fail\r\n"); } } break; case SOCK_CLOSED:/*socket 关闭*/ socket(SOCKET_UDP, Sn_MR_UDP, local_port, 0);/*初始化socket 0的套接字*/ break; } } }
int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port) { CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKINIT(); if( *((uint32_t*)addr) == 0xFFFFFFFF || *((uint32_t*)addr) == 0) return SOCKERR_IPINVALID; if(port == 0) return SOCKERR_PORTZERO; setSn_DIPR(sn,addr); setSn_DPORT(sn,port); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR(0); #endif setSn_CR(sn,Sn_CR_CONNECT); while(getSn_CR(sn) && !jspIsInterrupted()); if(sock_io_mode & (1<<sn)) return SOCK_BUSY; while(getSn_SR(sn) != SOCK_ESTABLISHED) { if (jspIsInterrupted()) return SOCKERR_TIMEOUT; if (getSn_IR(sn) & Sn_IR_TIMEOUT) { setSn_IR(sn, Sn_IR_TIMEOUT); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif return SOCKERR_TIMEOUT; } } #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif return SOCK_OK; }
uint8_t EncClient::status() { if (_sock == 255) { return SOCK_CLOSED; } else { return getSn_SR(_sock); } }
void SingleBoardDataToSendToPC(int which, int index) { char PC_Buf[40] = ""; int mWhich, arrIdx; mWhich = which; arrIdx = index; switch(mWhich) { case EQ_ONE: sprintf(PC_Buf, "%s_%s_%s_%s_%s_%s\r\n", DAQBoardOne[arrIdx].Date, DAQBoardOne[arrIdx].Time, DAQBoardOne[arrIdx].AxisX, DAQBoardOne[arrIdx].AxisY, DAQBoardOne[arrIdx].AxisZ, DAQBoardOne[arrIdx].ATFC); break; case EQ_TWO: sprintf(PC_Buf, "%s_%s_%s_%s_%s_%s\r\n", DAQBoardTwo[arrIdx].Date, DAQBoardTwo[arrIdx].Time, DAQBoardTwo[arrIdx].AxisX, DAQBoardTwo[arrIdx].AxisY, DAQBoardTwo[arrIdx].AxisZ, DAQBoardTwo[arrIdx].ATFC); break; } if(getSn_SR(SOCK_TWO) == SOCK_ESTABLISHED) { // SOCK_TWO = PC Client /* send selected data */ send(SOCK_TWO, (uint8_t*)PC_Buf, strlen(PC_Buf), (bool)false); } }
/** *@brief UDP测试程序 *@param 无 *@return 无 */ void do_udp(void) { uint16 len=0; uint8 buff[2048]; /*定义一个2KB的缓存*/ switch(getSn_SR(SOCK_UDPS)) /*获取socket的状态*/ { case SOCK_CLOSED: /*socket处于关闭状态*/ socket(SOCK_UDPS,Sn_MR_UDP,local_port,0); /*初始化socket*/ break; case SOCK_UDP: /*socket初始化完成*/ delay_ms(10); if(getSn_IR(SOCK_UDPS) & Sn_IR_RECV) { setSn_IR(SOCK_UDPS, Sn_IR_RECV); /*清接收中断*/ } if((len=getSn_RX_RSR(SOCK_UDPS))>0) /*接收到数据*/ { recvfrom(SOCK_UDPS,buff, len, remote_ip,&remote_port); /*W5500接收计算机发送来的数据*/ buff[len-8]=0x00; /*添加字符串结束符*/ printf("%s\r\n",buff); /*打印接收缓存*/ sendto(SOCK_UDPS,buff,len-8, remote_ip, remote_port); /*W5500把接收到的数据发送给Remote*/ } break; } }
uint8_t Client::connect() { if (_sock != 255) return 0; for (int i = 0; i < MAX_SOCK_NUM; i++) { uint8_t s = getSn_SR(i); if (s == SOCK_CLOSED || s == SOCK_FIN_WAIT) { _sock = i; break; } } if (_sock == 255) return 0; _srcport++; if (_srcport + 1024 == 0) _srcport = 0; socket(_sock, Sn_MR_TCP, _srcport + 1024, 0); if (!::connect(_sock, _ip, _port)) { _sock = 255; return 0; } while (status() != SOCK_ESTABLISHED) { delay(1); if (status() == SOCK_CLOSED) { _sock = 255; return 0; } } return 1; }
/// If the given server socket can accept a connection, return it (or return < 0) int net_wiznet_accept(JsNetwork *net, int sckt) { NOT_USED(net); // On WIZnet the same server socket is reused for clients - keep track so we don't get confused // and try and allocate a new HTTP Server Client if (wiznetSocketAsServerClient & (1<<sckt)) return -1; /* CC3000/WIZnet works a different way - we set accept as nonblocking, * and then we just call it and see if it works or not... */ // we have a client waiting to connect... try to connect and see what happens // WIZnet's implementation doesn't use accept, it uses listen int status = getSn_SR((uint8_t)sckt); if (status == SOCK_ESTABLISHED) { wiznetSocketAsServerClient = wiznetSocketAsServerClient | (unsigned char)(1<<sckt); // mark that it's now being used as a client socket return ((int)sckt) | WIZNET_SERVER_CLIENT; // we deal with the client on the same socket (we use the flag so we know that it really is different!) } // WIZnet can get confused (somehow!) when handling repeated requests from the HTTP server if (status == SOCK_CLOSED || status == SOCK_CLOSE_WAIT) { // make sure we force-close again and re-init as a listener net_wiznet_closesocket(net, (uint16_t)(sckt | WIZNET_SERVER_CLIENT)); } return -1; }
/** @brief This function get the UDP socket status. @return UDP socket status. */ int8 GetUDPSocketStatus(SOCKET s) { int8 ret=0; switch(getSn_SR(s)) { case SOCK_CLOSED: /**< closed */ ret = (int8)STATUS_CLOSED; break; case SOCK_UDP: /**< udp socket */ ret = STATUS_UDP; break; #if 0 case SOCK_IPRAW: /**< ip raw mode socket */ ret = 11; break; case SOCK_MACRAW: /**< mac raw mode socket */ ret = 12; break; case SOCK_PPPOE: /**< pppoe socket */ ret = 13; break; #endif default: if((IINCHIP_READ(Sn_MR(s))&0x0F) != Sn_MR_UDP) ret = (int8)ERROR_NOT_UDP_SOCKET; break; } return ret; }
void SingleBoardDataToSendToPC(int which, int index) { char PC_Buf[45] = ""; int mWhich, arrIdx; mWhich = which; arrIdx = index; switch(mWhich) { case EQ_ONE: sprintf(PC_Buf, "%s_%s_%s_%s_%s_%s\r\n", DAQBoardOne[arrIdx].Date, DAQBoardOne[arrIdx].Time, DAQBoardOne[arrIdx].AxisX, DAQBoardOne[arrIdx].AxisY, DAQBoardOne[arrIdx].AxisZ, DAQBoardOne[arrIdx].ATFC); break; case EQ_TWO: sprintf(PC_Buf, "%s_%s_%s_%s_%s_%s\r\n", DAQBoardTwo[arrIdx].Date, DAQBoardTwo[arrIdx].Time, DAQBoardTwo[arrIdx].AxisX, DAQBoardTwo[arrIdx].AxisY, DAQBoardTwo[arrIdx].AxisZ, DAQBoardTwo[arrIdx].ATFC); break; } // code for stacking algorithm which will combine data from two boards into one // Only when socket is established, allow send data if(getSn_SR(SOCK_TWO) == SOCK_ESTABLISHED) { // SOCK_TWO = PC Client /* send selected data */ send(SOCK_TWO, (uint8_t*)PC_Buf, strlen(PC_Buf), (bool)false); } }
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port) { int32_t ret; uint16_t size, sentsize; uint8_t destip[4]; uint16_t destport; switch(getSn_SR(sn)) { case SOCK_UDP : if((size = getSn_RX_RSR(sn)) > 0) { if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport); if(ret <= 0) { #ifdef _LOOPBACK_DEBUG_ printk("%d: recvfrom error. %ld\r\n",sn,ret); #endif return ret; } size = (uint16_t) ret; sentsize = 0; while(sentsize != size) { ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport); if(ret < 0) { #ifdef _LOOPBACK_DEBUG_ printk("%d: sendto error. %ld\r\n",sn,ret); #endif return ret; } sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. } } break; case SOCK_CLOSED: #ifdef _LOOPBACK_DEBUG_ //printk("%d:UDP loopback start\r\n",sn); #endif if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn) return ret; #ifdef _LOOPBACK_DEBUG_ printk("%d:Opened, UDP loopback, port [%d]\r\n", sn, port); #endif break; default : break; } return 1; }
/** * @brief Initialise the socket & buffer for HTTP server */ uint8_t init_httpd_ch(SOCKET s) { uint8_t ret; ret = 0; if( getSn_SR(s) != SOCK_CLOSED ) // Check the preferred socket is available, { s = getSocket(SOCK_CLOSED, 0); // otherwise find free socket, if(s == MAX_SOCK_NUM ) // If there is no free socket? ret = 0; } else { ret = 1; } if(!socket(s, Sn_MR_TCP, IP_PORT_HTTP, 0x00)) // initialise the socket for DHCP service { xSerialPrintf_P(PSTR("HTTPD socket: %d, initialise fail..!\r\n"),s); ret = 0; } #ifdef HTTP_DEBUG else xSerialPrintf_P(PSTR("HTTPD socket: %d, initialise success..!\r\n"),s); #endif if(pHTTPRequest == NULL) // if there is no buffer allocated (pointer is NULL), then allocate request buffer for all HTTP functions. { if( !(pHTTPRequest = (HTTP_REQUEST *) pvPortMalloc( sizeof(HTTP_REQUEST) ))) { xSerialPrint_P(PSTR("HTTP Request Buffer: malloc fail..!\r\n")); ret = 0; } #ifdef HTTP_DEBUG else xSerialPrint_P(PSTR("HTTP Request Buffer: malloc success..!\r\n")); #endif } if(pHTTPResponse == NULL) // if there is no buffer allocated (pointer is NULL), then allocate response buffer for all HTTP functions. { if( !(pHTTPResponse = (uint8_t *) pvPortMalloc( sizeof(uint8_t) * (FILE_BUFFER_SIZE + 1) ))) { xSerialPrint_P(PSTR("HTTP Response Buffer: malloc fail..!\r\n")); vPortFree(pHTTPRequest); ret = 0; } #ifdef HTTP_DEBUG else xSerialPrint_P(PSTR("HTTP Response Buffer: malloc success..!\r\n")); #endif } HTTPD_SOCK = s; return ret; }
uint8_t check_for_connections(TCP_CONFIG *config) { if(!(getSn_SR(config->s) != SOCK_ESTABLISHED)) { while ((bytesReceived = getSn_RX_RSR(config->s)) == 0); // wait for connection return 1; } return 0; }
int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg) { CHECK_SOCKNUM(); switch(sotype) { case SO_FLAG: *(uint8_t*)arg = getSn_MR(sn) & 0xF0; break; case SO_TTL: *(uint8_t*) arg = getSn_TTL(sn); break; case SO_TOS: *(uint8_t*) arg = getSn_TOS(sn); break; case SO_MSS: *(uint16_t*) arg = getSn_MSSR(sn); break; case SO_DESTIP: getSn_DIPR(sn, (uint8_t*)arg); break; case SO_DESTPORT: *(uint16_t*) arg = getSn_DPORT(sn); break; #if _WIZCHIP_ > 5200 case SO_KEEPALIVEAUTO: CHECK_SOCKMODE(Sn_MR_TCP); *(uint16_t*) arg = getSn_KPALVTR(sn); break; #endif case SO_SENDBUF: *(uint16_t*) arg = getSn_TX_FSR(sn); break; case SO_RECVBUF: *(uint16_t*) arg = getSn_RX_RSR(sn); break; case SO_STATUS: *(uint8_t*) arg = getSn_SR(sn); break; case SO_REMAINSIZE: if(getSn_MR(sn) & Sn_MR_TCP) *(uint16_t*)arg = getSn_RX_RSR(sn); else *(uint16_t*)arg = sock_remained_size[sn]; break; case SO_PACKINFO: //CHECK_SOCKMODE(Sn_MR_TCP); #if _WIZCHIP_ != 5300 if((getSn_MR(sn) == Sn_MR_TCP)) return SOCKERR_SOCKMODE; #endif *(uint8_t*)arg = sock_pack_info[sn]; break; default: return SOCKERR_SOCKOPT; } return SOCK_OK; }
int32_t loopback_tcpc(uint8_t sn, uint8_t* destip, uint16_t destport){ int32_t ret; // return value for SOCK_ERRORs // Destination (TCP Server) IP info (will be connected) // >> loopback_tcpc() function parameter // >> Ex) // uint8_t destip[4] = {192, 168, 34, 131}; // uint16_t destport = 5000; // Port number for TCP client (will be increased) uint16_t any_port = 2; // Socket Status Transitions // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status) switch(getSn_SR(sn)) { case SOCK_ESTABLISHED : if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful { sprintf(gPtrintBuff, "%d:Connected to - %d.%d.%d.%d : %u\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport); uartPuts(gPtrintBuff); setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1' } break; case SOCK_CLOSE_WAIT : if((ret=disconnect(sn)) != SOCK_OK) return ret; sprintf(gPtrintBuff, "%d:Socket Closed\r\n", sn); uartPuts(gPtrintBuff); break; case SOCK_INIT : sprintf(gPtrintBuff, "%d:Try to connect to the %d.%d.%d.%d : %u\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport); uartPuts(gPtrintBuff); if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination) break; case SOCK_CLOSED: close(sn); if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn) return ret; // TCP socket open with 'any_port' port number sprintf(gPtrintBuff, "%d:TCP client loopback start\r\n",sn); uartPuts(gPtrintBuff); sprintf(gPtrintBuff, "%d:Socket opened\r\n",sn); uartPuts(gPtrintBuff); break; default: break; } return 1; }
uint8_t net_wiznet_getFreeSocket() { unsigned int i; for (i=0;i<8;i++) if (getSn_SR(i) == SOCK_CLOSED) // it's free! return (uint8_t)i; jsError("No free sockets found\n"); // out of range will probably just make it error out return 8; }
int8_t close(uint8_t sn) { CHECK_SOCKNUM(); //A20160426 : Applied the erratum 1 of W5300 #if (_WIZCHIP_ == 5300) //M20160503 : Wrong socket parameter. s -> sn //if( ((getSn_MR(s)& 0x0F) == Sn_MR_TCP) && (getSn_TX_FSR(s) != getSn_TxMAX(s)) ) if( ((getSn_MR(sn)& 0x0F) == Sn_MR_TCP) && (getSn_TX_FSR(sn) != getSn_TxMAX(sn)) ) { uint8_t destip[4] = {0, 0, 0, 1}; // TODO // You can wait for completing to sending data; // wait about 1 second; // if you have completed to send data, skip the code of erratum 1 // ex> wait_1s(); // if (getSn_TX_FSR(s) == getSn_TxMAX(s)) continue; // //M20160503 : The socket() of close() calls close() itself again. It occures a infinite loop - close()->socket()->close()->socket()-> ~ //socket(s,Sn_MR_UDP,0x3000,0); //sendto(s,destip,1,destip,0x3000); // send the dummy data to an unknown destination(0.0.0.1). setSn_MR(sn,Sn_MR_UDP); setSn_PORTR(sn, 0x3000); setSn_CR(sn,Sn_CR_OPEN); while(getSn_CR(sn) != 0); while(getSn_SR(sn) != SOCK_UDP); sendto(sn,destip,1,destip,0x3000); // send the dummy data to an unknown destination(0.0.0.1). }; #endif setSn_CR(sn,Sn_CR_CLOSE); /* wait to process the command... */ while( getSn_CR(sn) ); /* clear all interrupt of the socket. */ setSn_IR(sn, 0xFF); //A20150401 : Release the sock_io_mode of socket n. sock_io_mode &= ~(1<<sn); // sock_is_sending &= ~(1<<sn); sock_remained_size[sn] = 0; sock_pack_info[sn] = 0; while(getSn_SR(sn) != SOCK_CLOSED); return SOCK_OK; }
/// Receive data if possible. returns nBytes on success, 0 on no data, or -1 on failure int net_wiznet_recv(JsNetwork *net, int sckt, void *buf, size_t len) { NOT_USED(net); int num = 0; if (getSn_SR(sckt)!=SOCK_LISTEN) { // receive data - if none available it'll just return SOCK_BUSY num = (int)recv((uint8_t)sckt,buf,(uint16_t)len,0); if (num==SOCK_BUSY) num=0; } if (jspIsInterrupted()) return -1; return num; }
// Send the data, returning the length buffered size_t W5500Socket::Send(const uint8_t *data, size_t length) { MutexLocker lock(interface->interfaceMutex); if (CanSend() && length != 0 && getSn_SR(socketNum) == SOCK_ESTABLISHED) { // Check for previous send complete if (isSending) // are we already sending? { const uint8_t tmp = getSn_IR(socketNum); if (tmp & Sn_IR_SENDOK) // did the previous send complete? { setSn_IR(socketNum, Sn_IR_SENDOK); // if yes isSending = false; } else if (tmp & Sn_IR_TIMEOUT) // did it time out? { isSending = false; disconnectNoWait(socketNum); // if so, close the socket state = SocketState::aborted; return 0; // and release buffers etc. } else { return 0; // last send is still in progress } } if (!sendOutstanding) { wizTxBufferLeft = getSn_TX_FSR(socketNum); // get free buffer space if (wizTxBufferLeft == 0) { return 0; } wizTxBufferPtr = getSn_TX_WR(socketNum); } if (length > wizTxBufferLeft) { length = wizTxBufferLeft; } wiz_send_data_at(socketNum, data, length, wizTxBufferPtr); wizTxBufferLeft -= length; wizTxBufferPtr += length; sendOutstanding = true; if (wizTxBufferLeft == 0) { Send(); } return length; } return 0; }
uint8_t EncClient::connect() { if (_sock != 255) return 0; #ifdef ETHERSHIELD_DEBUG sprintf(_DEBUG, "EncClient::connect() DEBUG A. _sock = %d", _sock); #endif for (int i = 0; i < MAX_SOCK_NUM; i++) { uint8_t s = getSn_SR(i); if (s == SOCK_CLOSED || s == SOCK_FIN_WAIT) { _sock = i; break; } } #ifdef ETHERSHIELD_DEBUG sprintf(_DEBUG, "EncClient::connect() DEBUG B. _sock = %d", _sock); #endif if (_sock == 255) return 0; _srcport++; if (_srcport + 1024 == 0) _srcport = 0; socket(_sock, Sn_MR_TCP, _srcport + 1024, 0); #ifdef ETHERSHIELD_DEBUG sprintf(_DEBUG, "Client::connect() DEBUG C. _sock = %d", _sock); #endif if (!::connect(_sock, _ip, _port)) { _sock = 255; return 0; } #ifdef ETHERSHIELD_DEBUG sprintf(_DEBUG, "Client::connect() DEBUG D. _sock = %d", _sock); #endif while (status() != SOCK_ESTABLISHED) { delay(1); if (status() == SOCK_CLOSED) { _sock = 255; return 0; } } #ifdef ETHERSHIELD_DEBUG sprintf(_DEBUG, "EncClient::connect() DEBUG E. _sock = %d", _sock); #endif return 1; }
int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port) { CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKINIT(); //M20140501 : For avoiding fatal error on memory align mismatched //if( *((uint32_t*)addr) == 0xFFFFFFFF || *((uint32_t*)addr) == 0) return SOCKERR_IPINVALID; { uint32_t taddr; taddr = ((uint32_t)addr[0] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF); taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF); if( taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID; } // if(port == 0) return SOCKERR_PORTZERO; setSn_DIPR(sn,addr); setSn_DPORT(sn,port); setSn_CR(sn,Sn_CR_CONNECT); while(getSn_CR(sn)); if(sock_io_mode & (1<<sn)) return SOCK_BUSY; while(getSn_SR(sn) != SOCK_ESTABLISHED) { if (getSn_IR(sn) & Sn_IR_TIMEOUT) { setSn_IR(sn, Sn_IR_TIMEOUT); return SOCKERR_TIMEOUT; } if (getSn_SR(sn) == SOCK_CLOSED) { return SOCKERR_SOCKCLOSED; } } return SOCK_OK; }
int32_t loopback_udps_bsd(uint8_t sn, uint8_t* buf, uint16_t port) { int32_t ret; uint16_t size, sentsize; struct sockaddr_in sa; int len; switch(getSn_SR(sn)) { case SOCK_UDP : if((size = getSn_RX_RSR(sn)) > 0) { if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; ret = recvfrom(sn, buf, size, 0, (struct sockaddr*)&sa, (socklen_t*)&len); if(ret <= 0) { #ifdef _LOOPBACK_DEBUG_ printf("%d: recvfrom error. %ld\r\n",sn,ret); #endif return ret; } size = (uint16_t) ret; sentsize = 0; while(sentsize != size) { ret = sendto(sn, buf+sentsize, size-sentsize, 0, (struct sockaddr*)&sa, len); if(ret < 0) { #ifdef _LOOPBACK_DEBUG_ printf("%d: sendto error. %ld\r\n",sn,ret); #endif return ret; } sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. } } break; case SOCK_CLOSED: #ifdef _LOOPBACK_DEBUG_ printf("%d:UDP loopback start\r\n",sn); #endif if((ret = socket(AF_INET, SOCK_STREAM, IPPROTO_UDP)) != sn) return ret; #ifdef _LOOPBACK_DEBUG_ printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port); #endif break; default : break; } return 1; }
int8_t listen(uint8_t sn) { CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKINIT(); setSn_CR(sn,Sn_CR_LISTEN); while(getSn_CR(sn)); while(getSn_SR(sn) != SOCK_LISTEN) { close(sn); return SOCKERR_SOCKCLOSED; } return SOCK_OK; }
int8_t close(uint8_t sn) { CHECK_SOCKNUM(); setSn_CR(sn,Sn_CR_CLOSE); /* wait to process the command... */ while( getSn_CR(sn) ); /* clear all interrupt of the socket. */ setSn_IR(sn, 0xFF); sock_is_sending &= ~(1<<sn); sock_remained_size[sn] = 0; sock_pack_info[sn] = 0; while(getSn_SR(sn) != SOCK_CLOSED); return SOCK_OK; }
/// Receive data if possible. returns nBytes on success, 0 on no data, or -1 on failure int net_wiznet_recv(JsNetwork *net, int sckt, void *buf, size_t len) { NOT_USED(net); int num = 0; if (getSn_SR((uint8_t)sckt) == SOCK_LISTEN) { // socket is operating as a TCP server - something has gone wrong. // just return -1 to close this connection immediately return -1; } else { // receive data - if none available it'll just return SOCK_BUSY num = (int)recv((uint8_t)sckt,buf,(uint16_t)len,0); if (num==SOCK_BUSY) num=0; } if (jspIsInterrupted()) return -1; return num; }
void sender_tcpc(SOCKET s, uint16 port) { uint16 RSR_len; uint8 DATA_BUFF_IN[INPUT_SIZE_BUF]; switch (getSn_SR(s)) { case SOCK_ESTABLISHED: /* if connection is established */ if(ch_status[s]==1) { ch_status[s] = 2; } send(s, DATA_BUFF_IN, MAX_SIZE_BUF, (bool)WINDOWFULL_FLAG_OFF, FPGA); break; case SOCK_CLOSE_WAIT: /* If the client request to close */ if ((RSR_len = getSn_RX_RSR(s)) > 0) /* check Rx data */ { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ if (RSR_len > TX_RX_MAX_BUF_SIZE) RSR_len = TX_RX_MAX_BUF_SIZE; /* the data size to read is MAX_BUF_SIZE. */ // received_len = recv(s, DATA_BUFF_A, RSR_len); /* read the received data */ } disconnect(s); ch_status[s] = 0; break; case SOCK_CLOSED: /* if a socket is closed */ if(!ch_status[s]) { ch_status[s] = 1; } if(socket(s, Sn_MR_TCP, port, 0x00) == 0) /* reinitialize the socket */ { ch_status[s] = 0; } break; case SOCK_INIT: /* if a socket is initiated */ if(time_return() - presentTime >= (tick_second * 3)) /* For TCP client's connection request delay : 3 sec */ { /* Try to connect to TCP server(Socket, DestIP, DestPort) */ connect(s, Config_Msg.destip, Config_Msg.port); presentTime = time_return(); } break; default: break; } }