WIZnet improvements (specifically on HTTP server)

This commit is contained in:
Gordon Williams 2014-04-29 18:47:32 +01:00
parent 118600a3f1
commit 5bb18aa828
5 changed files with 26 additions and 12 deletions

View File

@ -27,6 +27,7 @@
Add ES6 Array.prototype.fill (fix #317)
Modified jsiQueueObjectCallbacks (and Object.emit) to support >2 args
Added support for SPI LSB-first
WIZnet improvements (specifically on HTTP server)
1v61 : Fix toString crash for large numbers
Support floating-point literals without a leading 0 - eg '.5' (fix #296)

View File

@ -6,6 +6,7 @@ File Include Section
#include "jsinteractive.h"
#include "jshardware.h"
#include "jsparse.h"
#include <stdio.h>
#include <string.h>
#include "socket.h"
@ -794,7 +795,7 @@ uint8_t getIP_DHCPS(uint8_t s, wiz_NetInfo *pWIZNETINFO)
//**************
//if (Recv_ConfigMsg() == MSG_SETTING_REQ) return(2);
if (DHCP_timeout == 1) {
if (DHCP_timeout == 1 || jspIsInterrupted()) {
jsiConsolePrintf("> => DHCP Timeout occurred\r\n");
return(0);
}

View File

@ -43,8 +43,10 @@
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
#include "jsparse.h"
#include "Ethernet/socket.h"
#define SOCK_ANY_PORT_NUM 0xC000;
static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
@ -190,10 +192,12 @@ int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
setSUBR(0);
#endif
setSn_CR(sn,Sn_CR_CONNECT);
while(getSn_CR(sn));
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);

View File

@ -188,6 +188,7 @@ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) {
success = true;
} else {
// DHCP
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
uint8_t DHCPisSuccess = getIP_DHCPS(net_wiznet_getFreeSocket(), &gWIZNETINFO);
if (DHCPisSuccess == 1) {
// info in lease_time.lVal

View File

@ -11,6 +11,7 @@
* Implementation of JsNetwork for WIZnet devices
* ----------------------------------------------------------------------------
*/
#include "jsinteractive.h"
#include "network.h"
#include "network_wiznet.h"
@ -30,6 +31,7 @@ typedef struct sockaddr_in sockaddr_in;
// name resolution
#include "wiznet/DNS/dns.h"
extern uint8_t Server_IP_Addr[4];
short wiznetSocketPorts[8];
#define WIZNET_SERVER_CLIENT 256 // sockets are only 0-255 so this is masked out
@ -84,16 +86,18 @@ int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short p
if (res == SOCKET_ERROR) {
jsError("Connect failed (err %d)\n", res );
}
} else { // ------------------------------------------------- no host (=server)
} else { // ------------------------------------------------- no host (=server)
sckt = socket(net_wiznet_getFreeSocket(), Sn_MR_TCP, port, SF_IO_NONBLOCK);
}
listen((uint8_t)sckt);
}
wiznetSocketPorts[sckt&7] = port;
return sckt;
}
/// destroys the given socket
void net_wiznet_closesocket(JsNetwork *net, int sckt) {
NOT_USED(net);
// close gracefully
// try and close gracefully
disconnect((uint8_t)sckt);
JsSysTime timeout = jshGetSystemTime()+jshGetTimeFromMilliseconds(1000);
uint8_t status;
@ -105,8 +109,8 @@ void net_wiznet_closesocket(JsNetwork *net, int sckt) {
// Wiznet is a bit strange in that it uses the same socket for server and client
if (sckt & WIZNET_SERVER_CLIENT) {
// so it's just closed, but put it into 'listen' mode again
int port = 80; // FIXME
sckt = socket((uint8_t)sckt, Sn_MR_TCP, port, SF_IO_NONBLOCK);
sckt = socket((uint8_t)sckt, Sn_MR_TCP, wiznetSocketPorts[sckt&7], SF_IO_NONBLOCK);
listen((uint8_t)sckt);
}
}
@ -118,10 +122,10 @@ int net_wiznet_accept(JsNetwork *net, int sckt) {
*/
// 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 theClient = listen((uint8_t)sckt);
if (theClient==SOCK_OK)
theClient = 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!)
return theClient;
int status = getSn_SR((uint8_t)sckt);
if (status == SOCK_ESTABLISHED)
return 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!)
return -1;
}
/// Receive data if possible. returns nBytes on success, 0 on no data, or -1 on failure
@ -133,13 +137,16 @@ int net_wiznet_recv(JsNetwork *net, int sckt, void *buf, size_t len) {
num = (int)recv(sckt,buf,len,0);
if (num==SOCK_BUSY) num=0;
}
if (jspIsInterrupted()) return -1;
return num;
}
/// Send data if possible. returns nBytes on success, 0 on no data, or -1 on failure
int net_wiznet_send(JsNetwork *net, int sckt, const void *buf, size_t len) {
NOT_USED(net);
return (int)send(sckt, buf, len, MSG_NOSIGNAL);
int r = (int)send(sckt, buf, len, MSG_NOSIGNAL);
if (jspIsInterrupted()) return -1;
return r;
}
void netSetCallbacks_wiznet(JsNetwork *net) {