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; }
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; }
// 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; }
int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len) { uint8_t tmp = 0; uint16_t recvsize = 0; CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKDATA(); recvsize = getSn_RxMAX(sn); if(recvsize < len) len = recvsize; while(1) { recvsize = getSn_RX_RSR(sn); tmp = getSn_SR(sn); if (tmp != SOCK_ESTABLISHED) { if(tmp == SOCK_CLOSE_WAIT) { if(recvsize != 0) break; else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn)) { // dpgeorge: Getting here seems to be an orderly shutdown of the // socket, and trying to get POSIX behaviour we return 0 because: // "If no messages are available to be received and the peer has per‐ // formed an orderly shutdown, recv() shall return 0". // TODO this return value clashes with SOCK_BUSY in non-blocking mode. WIZCHIP_EXPORT(close)(sn); return 0; } } else { WIZCHIP_EXPORT(close)(sn); return SOCKERR_SOCKSTATUS; } } if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY; if(recvsize != 0) break; MICROPY_THREAD_YIELD(); }; if(recvsize < len) len = recvsize; wiz_recv_data(sn, buf, len); setSn_CR(sn,Sn_CR_RECV); while(getSn_CR(sn)); return len; }
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; }
int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len) { uint8_t tmp = 0; uint16_t recvsize = 0; CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKDATA(); recvsize = getSn_RxMAX(sn); if(recvsize < len) len = recvsize; while(1) { recvsize = getSn_RX_RSR(sn); tmp = getSn_SR(sn); if (tmp != SOCK_ESTABLISHED) { if(tmp == SOCK_CLOSE_WAIT) { if(recvsize != 0) break; else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn)) { close(sn); return SOCKERR_SOCKSTATUS; } } else { close(sn); return SOCKERR_SOCKSTATUS; } } if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY; if(recvsize != 0) break; HAL_Delay(1); }; if(recvsize < len) len = recvsize; wiz_recv_data(sn, buf, len); setSn_CR(sn,Sn_CR_RECV); while(getSn_CR(sn)); return len; }
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port) { uint8_t tmp = 0; uint16_t freesize = 0; CHECK_SOCKNUM(); switch(getSn_MR(sn) & 0x0F) { case Sn_MR_UDP: case Sn_MR_MACRAW: break; default: return SOCKERR_SOCKMODE; } CHECK_SOCKDATA(); if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID; if(port == 0) return SOCKERR_PORTZERO; tmp = getSn_SR(sn); if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS; setSn_DIPR(sn,addr); setSn_DPORT(sn,port); freesize = getSn_TxMAX(sn); if (len > freesize) len = freesize; // check size not to exceed MAX size. while(1) { freesize = getSn_TX_FSR(sn); if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY; if(len <= freesize) break; }; wiz_send_data(sn, buf, len); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR(0); #endif setSn_CR(sn,Sn_CR_SEND); /* wait to process the command... */ while(getSn_CR(sn)); #if _WIZCHIP_ == 5200 // for W5200 ARP errata setSUBR((uint8_t*)"\x00\x00\x00\x00"); #endif while(1) { tmp = getSn_IR(sn); if(tmp & Sn_IR_SENDOK) { setSn_IR(sn, Sn_IR_SENDOK); break; } //M:20131104 //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT; else if(tmp & Sn_IR_TIMEOUT) { setSn_IR(sn, Sn_IR_TIMEOUT); return SOCKERR_TIMEOUT; } //////////// } return len; }
/** @brief This Socket function get the TX free buffer size. @return size of TX free buffer size. */ uint16 GetSocketTxFreeBufferSize(SOCKET s) { return getSn_TX_FSR(s); // get socket TX free buf size }
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port) { uint8_t tmp = 0; uint16_t freesize = 0; uint32_t taddr; CHECK_SOCKNUM(); switch(getSn_MR(sn) & 0x0F) { case Sn_MR_UDP: case Sn_MR_MACRAW: // break; // #if ( _WIZCHIP_ < 5200 ) case Sn_MR_IPRAW: break; // #endif default: return SOCKERR_SOCKMODE; } CHECK_SOCKDATA(); //M20140501 : For avoiding fatal error on memory align mismatched //if(*((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(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID; if((taddr == 0) && ((getSn_MR(sn)&Sn_MR_MACRAW) != Sn_MR_MACRAW)) return SOCKERR_IPINVALID; if((port == 0) && ((getSn_MR(sn)&Sn_MR_MACRAW) != Sn_MR_MACRAW)) return SOCKERR_PORTZERO; tmp = getSn_SR(sn); //#if ( _WIZCHIP_ < 5200 ) if((tmp != SOCK_MACRAW) && (tmp != SOCK_UDP) && (tmp != SOCK_IPRAW)) return SOCKERR_SOCKSTATUS; //#else // if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS; //#endif setSn_DIPR(sn,addr); setSn_DPORT(sn,port); freesize = getSn_TxMAX(sn); if (len > freesize) len = freesize; // check size not to exceed MAX size. while(1) { freesize = getSn_TX_FSR(sn); if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY; if(len <= freesize) break; }; wiz_send_data(sn, buf, len); #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata) getSIPR((uint8_t*)&taddr); if(taddr == 0) { getSUBR((uint8_t*)&taddr); setSUBR((uint8_t*)"\x00\x00\x00\x00"); } else taddr = 0; #endif //A20150601 : For W5300 #if _WIZCHIP_ == 5300 setSn_TX_WRSR(sn, len); #endif // setSn_CR(sn,Sn_CR_SEND); /* wait to process the command... */ while(getSn_CR(sn)); while(1) { tmp = getSn_IR(sn); if(tmp & Sn_IR_SENDOK) { setSn_IR(sn, Sn_IR_SENDOK); break; } //M:20131104 //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT; else if(tmp & Sn_IR_TIMEOUT) { setSn_IR(sn, Sn_IR_TIMEOUT); //M20150409 : Fixed the lost of sign bits by type casting. //len = (uint16_t)SOCKERR_TIMEOUT; //break; #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata) if(taddr) setSUBR((uint8_t*)&taddr); #endif return SOCKERR_TIMEOUT; } //////////// } #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata) if(taddr) setSUBR((uint8_t*)&taddr); #endif //M20150409 : Explicit Type Casting //return len; return (int32_t)len; }
int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len) { uint8_t tmp = 0; uint16_t recvsize = 0; //A20150601 : For integarating with W5300 #if _WIZCHIP_ == 5300 uint8_t head[2]; uint16_t mr; #endif // CHECK_SOCKNUM(); CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKDATA(); recvsize = getSn_RxMAX(sn); if(recvsize < len) len = recvsize; //A20150601 : For Integrating with W5300 #if _WIZCHIP_ == 5300 //sock_pack_info[sn] = PACK_COMPLETED; // for clear if(sock_remained_size[sn] == 0) { #endif // while(1) { recvsize = getSn_RX_RSR(sn); tmp = getSn_SR(sn); if (tmp != SOCK_ESTABLISHED) { if(tmp == SOCK_CLOSE_WAIT) { if(recvsize != 0) break; else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn)) { close(sn); return SOCKERR_SOCKSTATUS; } } else { close(sn); return SOCKERR_SOCKSTATUS; } } if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY; if(recvsize != 0) break; }; #if _WIZCHIP_ == 5300 } #endif //A20150601 : For integrating with W5300 #if _WIZCHIP_ == 5300 if((sock_remained_size[sn] == 0) || (getSn_MR(sn) & Sn_MR_ALIGN)) { mr = getMR(); if((getSn_MR(sn) & Sn_MR_ALIGN)==0) { wiz_recv_data(sn,head,2); if(mr & MR_FS) recvsize = (((uint16_t)head[1]) << 8) | ((uint16_t)head[0]); else recvsize = (((uint16_t)head[0]) << 8) | ((uint16_t)head[1]); sock_pack_info[sn] = PACK_FIRST; } sock_remained_size[sn] = recvsize; } if(len > sock_remained_size[sn]) len = sock_remained_size[sn]; recvsize = len; if(sock_pack_info[sn] & PACK_FIFOBYTE) { *buf = sock_remained_byte[sn]; buf++; sock_pack_info[sn] &= ~(PACK_FIFOBYTE); recvsize -= 1; sock_remained_size[sn] -= 1; } if(recvsize != 0) { wiz_recv_data(sn, buf, recvsize); setSn_CR(sn,Sn_CR_RECV); while(getSn_CR(sn)); } sock_remained_size[sn] -= recvsize; if(sock_remained_size[sn] != 0) { sock_pack_info[sn] |= PACK_REMAINED; if(recvsize & 0x1) sock_pack_info[sn] |= PACK_FIFOBYTE; } else sock_pack_info[sn] = PACK_COMPLETED; if(getSn_MR(sn) & Sn_MR_ALIGN) sock_remained_size[sn] = 0; //len = recvsize; #else if(recvsize < len) len = recvsize; wiz_recv_data(sn, buf, len); setSn_CR(sn,Sn_CR_RECV); while(getSn_CR(sn)); #endif //M20150409 : Explicit Type Casting //return len; return (int32_t)len; }
int32_t 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); //M20150401 : Typing Error //#if _WZICHIP_ == 5200 #if _WIZCHIP_ == 5200 if(getSn_TX_RD(sn) != sock_next_rd[sn]) { setSn_CR(sn,Sn_CR_SEND); while(getSn_CR(sn)); return SOCK_BUSY; } #endif sock_is_sending &= ~(1<<sn); } else if(tmp & Sn_IR_TIMEOUT) { 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)) { close(sn); return SOCKERR_SOCKSTATUS; } if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY; if(len <= freesize) break; } wiz_send_data(sn, buf, len); #if _WIZCHIP_ == 5200 sock_next_rd[sn] = getSn_TX_RD(sn) + len; #endif #if _WIZCHIP_ == 5300 setSn_TX_WRSR(sn,len); #endif setSn_CR(sn,Sn_CR_SEND); /* wait to process the command... */ while(getSn_CR(sn)); sock_is_sending |= (1 << sn); //M20150409 : Explicit Type Casting //return len; return (int32_t)len; }
void httpServer_run(uint8_t seqnum) { uint8_t s; // socket number uint16_t len; uint32_t gettime = 0; #ifdef _HTTPSERVER_DEBUG_ uint8_t destip[4] = {0, }; uint16_t destport = 0; #endif http_request = (st_http_request *)pHTTP_RX; // Structure of HTTP Request parsed_http_request = (st_http_request *)pHTTP_TX; // Get the H/W socket number s = getHTTPSocketNum(seqnum); /* HTTP Service Start */ switch(getSn_SR(s)) { case SOCK_ESTABLISHED: // Interrupt clear if(getSn_IR(s) & Sn_IR_CON) { setSn_IR(s, Sn_IR_CON); } // HTTP Process states switch(HTTPSock_Status[seqnum].sock_status) { case STATE_HTTP_IDLE : if ((len = getSn_RX_RSR(s)) > 0) { if (len > DATA_BUF_SIZE) len = DATA_BUF_SIZE; len = recv(s, (uint8_t *)http_request, len); *(((uint8_t *)http_request) + len) = '\0'; parse_http_request(parsed_http_request, (uint8_t *)http_request); #ifdef _HTTPSERVER_DEBUG_ getSn_DIPR(s, destip); destport = getSn_DPORT(s); printf("\r\n"); printf("> HTTPSocket[%d] : HTTP Request received ", s); printf("from %d.%d.%d.%d : %d\r\n", destip[0], destip[1], destip[2], destip[3], destport); #endif #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [State] STATE_HTTP_REQ_DONE\r\n", s); #endif // HTTP 'response' handler; includes send_http_response_header / body function http_process_handler(s, parsed_http_request); gettime = get_httpServer_timecount(); // Check the TX socket buffer for End of HTTP response sends while(getSn_TX_FSR(s) != (getSn_TXBUF_SIZE(s)*1024)) { if((get_httpServer_timecount() - gettime) > 3) { #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [State] STATE_HTTP_REQ_DONE: TX Buffer clear timeout\r\n", s); #endif break; } } if(HTTPSock_Status[seqnum].file_len > 0) HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_INPROC; else HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_DONE; // Send the 'HTTP response' end } break; case STATE_HTTP_RES_INPROC : /* Repeat: Send the remain parts of HTTP responses */ #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [State] STATE_HTTP_RES_INPROC\r\n", s); #endif // Repeatedly send remaining data to client send_http_response_body(s, 0, http_response, 0, 0); if(HTTPSock_Status[seqnum].file_len == 0) HTTPSock_Status[seqnum].sock_status = STATE_HTTP_RES_DONE; break; case STATE_HTTP_RES_DONE : #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : [State] STATE_HTTP_RES_DONE\r\n", s); #endif // Socket file info structure re-initialize HTTPSock_Status[seqnum].file_len = 0; HTTPSock_Status[seqnum].file_offset = 0; HTTPSock_Status[seqnum].file_start = 0; HTTPSock_Status[seqnum].sock_status = STATE_HTTP_IDLE; //#ifdef _USE_SDCARD_ // f_close(&fs); //#endif #ifdef _USE_WATCHDOG_ HTTPServer_WDT_Reset(); #endif http_disconnect(s); break; default : break; } break; case SOCK_CLOSE_WAIT: #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : ClOSE_WAIT\r\n", s); // if a peer requests to close the current connection #endif disconnect(s); break; case SOCK_CLOSED: #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : CLOSED\r\n", s); #endif if(socket(s, Sn_MR_TCP, HTTP_SERVER_PORT, 0x00) == s) /* Reinitialize the socket */ { #ifdef _HTTPSERVER_DEBUG_ printf("> HTTPSocket[%d] : OPEN\r\n", s); #endif } break; case SOCK_INIT: listen(s); break; case SOCK_LISTEN: break; default : break; } // end of switch #ifdef _USE_WATCHDOG_ HTTPServer_WDT_Reset(); #endif }
/** @brief Analyse HTTP request and then services WEB. */ void process_HTTP( SOCKET s, /**< http server socket */ uint8_t * buffer, /**< buffer pointer included http request */ uint16_t length /**< length of http request */ ) { uint8_t * name; uint16_t bytes_read; TickType_t wait_send; FIL source_file; /* File object for the source file */ parse_HTTP_request(pHTTPRequest, buffer); // After analysing request, convert into pHTTPRequest /* method Analyse */ switch (pHTTPRequest->METHOD) { case METHOD_HEAD: case METHOD_GET : case METHOD_POST : name = get_HTTP_URI_name(pHTTPRequest->URI); if (!strcmp((const char *)name, "/")) strcpy((char *)name,"index.htm"); // If URI is "/", respond by index.htm #ifdef WEB_DEBUG if(strlen( (const char *)name) < MAX_INT_STR ) xSerialPrintf_P(PSTR("\r\nPAGE : %s "), name); else xSerialPrint_P(PSTR("\r\nFILENAME TOO LONG")); #endif find_HTTP_URI_type(&pHTTPRequest->TYPE, name); //Check file type (HTML, TEXT, ICO, GIF, JPEG, ZIP are included) // OK now we start to respond to the info we've decoded. /* Open the specified file stored in the SD card FAT32 */ if (f_open(&source_file, (const TCHAR *)name, FA_OPEN_EXISTING | FA_READ)) { // if file open failure memcpy_P( (char *)pHTTPResponse, ERROR_HTML_PAGE, strnlen_P(ERROR_HTML_PAGE, FILE_BUFFER_SIZE) ); #ifdef WEB_DEBUG xSerialPrint_P(PSTR("HTTP Unknown file or page.\r\n")); xSerialPrintf_P(PSTR("HTTP Response...\r\n%s\r\nResponse Size: %u \r\n"), pHTTPResponse, strlen_P(ERROR_HTML_PAGE)); #endif send( s, (const uint8_t*)pHTTPResponse, strlen_P(ERROR_HTML_PAGE)); } else { // if file open success make_HTTP_response_header( pHTTPResponse, pHTTPRequest->TYPE, source_file.fsize); #ifdef WEB_DEBUG xSerialPrintf_P(PSTR("HTTP Opened file: %s Source Size: %u \r\n"), name, source_file.fsize); xSerialPrintf_P(PSTR("HTTP Response Header...\r\n%s\r\nResponse Header Size: %u \r\n"), pHTTPResponse, strlen((char*)pHTTPResponse )); #endif send(s, (const uint8_t*)pHTTPResponse, strlen((char*)pHTTPResponse )); wait_send = xTaskGetTickCount(); while(getSn_TX_FSR(s)!= WIZCHIP_getTxMAX(s)) { if( (xTaskGetTickCount() - wait_send) > (WEBSERVER_SOCKET_TIMEOUT / portTICK_PERIOD_MS) ) // wait up to 1.5 Sec { #ifdef WEB_DEBUG xSerialPrint_P(PSTR("HTTP Response head send fail\r\n")); #endif break; } vTaskDelay( 0 ); // yield until next tick. } for (;;) { if ( f_read(&source_file, pHTTPResponse, (sizeof(uint8_t)*(FILE_BUFFER_SIZE) ), &bytes_read) || bytes_read == 0 ) break; // read error or reached end of file if(pHTTPRequest->TYPE == PTYPE_HTML) // if we've got a html document, there might be some system variables to set { *(pHTTPResponse + bytes_read + 1) = 0; // make the buffer a string, null terminated bytes_read = replace_sys_env_value(pHTTPResponse, bytes_read); // Replace html system environment value to real value } if (send(s, (const uint8_t*)pHTTPResponse, bytes_read) != bytes_read) break; // TCP/IP send error wait_send = xTaskGetTickCount(); while(getSn_TX_FSR(s)!= WIZCHIP_getTxMAX(s)) { if( (xTaskGetTickCount() - wait_send) > (WEBSERVER_SOCKET_TIMEOUT / portTICK_PERIOD_MS) ) // wait up to 1.5 Sec { #ifdef WEB_DEBUG xSerialPrint_P(PSTR("HTTP Response body send fail\r\n")); #endif break; } vTaskDelay( 0 ); // yield until next tick. } } f_close(&source_file); eeprom_busy_wait(); eeprom_update_dword( &pagesServed, eeprom_read_dword(&pagesServed) +1 ); } break; case METHOD_ERR : memcpy_P( (char *)pHTTPResponse, ERROR_REQUEST_PAGE, strnlen_P(ERROR_REQUEST_PAGE, FILE_BUFFER_SIZE) ); #ifdef WEB_DEBUG xSerialPrint_P(PSTR("HTTP Method Error.\r\n")); xSerialPrintf_P(PSTR("HTTP Response...\r\n%s\r\nResponse Size: %u \r\n"), pHTTPResponse, strlen_P(ERROR_REQUEST_PAGE)); #endif send( s, (const uint8_t*)pHTTPResponse, strlen_P(ERROR_REQUEST_PAGE)); break; default : break; } }
int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port) { uint8_t tmp = 0; uint16_t freesize = 0; CHECK_SOCKNUM(); switch(getSn_MR(sn) & 0x0F) { case Sn_MR_UDP: case Sn_MR_MACRAW: break; default: return SOCKERR_SOCKMODE; } CHECK_SOCKDATA(); //M20140501 : For avoiding fatal error on memory align mismatched //if(*((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; tmp = getSn_SR(sn); if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS; setSn_DIPR(sn,addr); setSn_DPORT(sn,port); freesize = getSn_TxMAX(sn); if (len > freesize) len = freesize; // check size not to exceed MAX size. while(1) { freesize = getSn_TX_FSR(sn); if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED; 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 // for W5200 ARP errata setSUBR(wizchip_getsubn()); #endif setSn_CR(sn,Sn_CR_SEND); /* wait to process the command... */ while(getSn_CR(sn)); while(1) { tmp = getSn_IR(sn); if(tmp & Sn_IR_SENDOK) { setSn_IR(sn, Sn_IR_SENDOK); break; } //M:20131104 //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT; else if(tmp & 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 len; }
/** @brief This function used to send the data in TCP mode @return 1 for success else 0. */ uint16 send( SOCKET s, /**< the socket index */ const uint8 * buf, /**< a pointer to data */ uint16 len /**< the data size to be send */ ) { uint8 status=0; uint16 ret=0; uint16 freesize=0; #ifdef __DEF_IINCHIP_DBG__ printf("send()\r\n"); #endif if (len > IINCHIP_TxMAX) ret = IINCHIP_TxMAX; // check size not to exceed MAX size. else ret = len; // if freebuf is available, start. do { freesize = getSn_TX_FSR(s); status = wiz_read_byte(Sn_SR(s)); if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT)) { ret = 0; break; } #ifdef __DEF_IINCHIP_DBG__ printf("socket %d freesize(%d) empty or error\r\n", s, freesize); #endif } while (freesize < ret); // copy data send_data_processing(s, (uint8 *)buf, ret); /* ------- */ /* +2008.01 bj */ #ifdef __DEF_IINCHIP_INT__ while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) #else while ( (wiz_read_byte(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) #endif { /* m2008.01 [bj] : reduce code */ if ( wiz_read_byte(Sn_SR(s)) == SOCK_CLOSED ) { #ifdef __DEF_IINCHIP_DBG__ printf("SOCK_CLOSED.\r\n"); #endif close(s); return 0; } } /* +2008.01 bj */ #ifdef __DEF_IINCHIP_INT__ putISR(s, getISR(s) & (~Sn_IR_SEND_OK)); #else wiz_write_byte(Sn_IR(s), Sn_IR_SEND_OK); #endif return ret; }