/*JSON{ "type" : "idle", "class" : "Telnet", "generate" : "jswrap_telnet_idle" } */ bool jswrap_telnet_idle(void) { // get a handle to the network, no network -> can't do anything at all JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return false; // if we're supposed to be off, then make sure we're disconnected if (tnSrvMode == MODE_OFF) { if (tnSrv.sock > 0) { telnetStop(&net); } networkFree(&net); return false; } // we're supposed to be on, make sure we're listening if (tnSrv.sock == 0) { memset(&tnSrv, 0, sizeof(TelnetServer)); telnetStart(&net); if (tnSrv.sock == 0) { networkFree(&net); return false; // seems like there's a problem... } } // looks like we are listening, now deal with actual connected sockets bool active = false; active |= telnetAccept(&net); active |= telnetRecv(&net); active |= telnetSendBuf(&net); //if (active) printf("tnSrv: idle=%d\n", active); networkFree(&net); return active; }
void jswrap_net_server_listen(JsVar *parent, int port) { JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return; serverListen(&net, parent, port); networkFree(&net); }
/*JSON{ "type":"idle", "generate" : "jswrap_http_idle" }*/ bool jswrap_http_idle() { JsNetwork net; if (!networkGetFromVar(&net)) return false; bool b = httpIdle(&net); networkFree(&net); return b; }
void jswrap_net_server_close(JsVar *parent) { JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return; serverClose(&net, parent); networkFree(&net); }
/*JSON{ "type" : "method", "class" : "Socket", "name" : "write", "generate" : "jswrap_net_socket_write", "params" : [ ["data","JsVar","A string containing data to send"] ], "return" : ["bool","For note compatibility, the boolean false. When the send buffer is empty, a `drain` event will be sent"] }*/ bool jswrap_net_socket_write(JsVar *parent, JsVar *data) { JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return false; clientRequestWrite(&net, parent, data); networkFree(&net); return false; }
/*JSON{ "type" : "method", "class" : "WLAN", "name" : "getIP", "generate" : "jswrap_wlan_getIP", "return" : ["JsVar",""] } Get the current IP address */ JsVar *jswrap_wlan_getIP(JsVar *wlanObj) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsError("Not connected to the internet"); return 0; } JsNetwork net; if (!networkGetFromVar(&net)) return 0; tNetappIpconfigRetArgs ipconfig; netapp_ipconfig(&ipconfig); networkFree(&net); /* If byte 1 is 0 we don't have a valid address */ if (ipconfig.aucIP[3] == 0) return 0; JsVar *data = jsvNewWithFlags(JSV_OBJECT); networkPutAddressAsString(data, "ip", &ipconfig.aucIP[0], -4, 10, '.'); networkPutAddressAsString(data, "subnet", &ipconfig.aucSubnetMask[0], -4, 10, '.'); networkPutAddressAsString(data, "gateway", &ipconfig.aucDefaultGateway[0], -4, 10, '.'); networkPutAddressAsString(data, "dhcp", &ipconfig.aucDHCPServer[0], -4, 10, '.'); networkPutAddressAsString(data, "dns", &ipconfig.aucDNSServer[0], -4, 10, '.'); networkPutAddressAsString(data, "mac", &ipconfig.uaMacAddr[0], -6, 16, 0); return data; }
/*JSON{ "type" : "method", "class" : "Ethernet", "name" : "getIP", "generate" : "jswrap_ethernet_getIP", "return" : ["JsVar",""] } Get the current IP address, subnet, gateway and mac address. */ JsVar *jswrap_ethernet_getIP(JsVar *wlanObj) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsError("Not connected to the internet"); return 0; } JsNetwork net; if (!networkGetFromVar(&net)) return 0; wiz_NetInfo gWIZNETINFO; ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); /* If byte 1 is 0 we don't have a valid address */ JsVar *data = jsvNewWithFlags(JSV_OBJECT); networkPutAddressAsString(data, "ip", &gWIZNETINFO.ip[0], 4, 10, '.'); networkPutAddressAsString(data, "subnet", &gWIZNETINFO.sn[0], 4, 10, '.'); networkPutAddressAsString(data, "gateway", &gWIZNETINFO.gw[0], 4, 10, '.'); networkPutAddressAsString(data, "dns", &gWIZNETINFO.dns[0], 4, 10, '.'); networkPutAddressAsString(data, "mac", &gWIZNETINFO.mac[0], 6, 16, ':'); networkFree(&net); return data; }
/*JSON{ "type" : "idle", "generate" : "jswrap_net_idle" }*/ bool jswrap_net_idle() { JsNetwork net; if (!networkGetFromVar(&net)) return false; net.idle(&net); bool b = socketIdle(&net); networkFree(&net); return b; }
/*JSON{ "type" : "kill", "generate" : "jswrap_net_kill" }*/ void jswrap_net_kill() { JsNetwork net; if (networkWasCreated()) { if (!networkGetFromVar(&net)) return; socketKill(&net); networkFree(&net); } }
/*JSON{ "type" : "method", "class" : "Socket", "name" : "end", "generate" : "jswrap_net_socket_end", "params" : [ ["data","JsVar","A string containing data to send"] ] } Close this socket - optional data to append as an argument */ void jswrap_net_socket_end(JsVar *parent, JsVar *data) { JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return; if (!jsvIsUndefined(data)) jswrap_net_socket_write(parent, data); clientRequestEnd(&net, parent); networkFree(&net); }
/*JSON{ "type" : "staticmethod", "class" : "NetworkJS", "name" : "create", "generate" : "jswrap_networkjs_create", "params" : [ ["obj","JsVar","An object containing functions to access the network device"] ], "return" : ["JsVar","The object passed in"] } Initialise the WIZnet module and return an Ethernet object. For instance: ``` require("NetworkJS").create({ create : function(host,port) { // Create a socket and return its index, host is a string, port is an integer. // If host isn't defined, create a server socket console.log("Create",host,port); return 1; }, close : function(sckt) { // Close the socket. returns nothing }, accept : function(sckt) { // Accept the connection on the server socket. Returns socket number or -1 if no connection return -1; }, recv : function(sckt, maxLen) { // Receive data. Returns a string (even if empty). // If non-string returned, socket is then closed return null;//or ""; }, send : function(sckt, data) { // Send data (as string). Returns the number of bytes sent - 0 is ok. // Less than 0 return data.length; } }); ``` */ JsVar *jswrap_networkjs_create(JsVar *obj) { JsNetwork net; networkCreate(&net, JSNETWORKTYPE_JS); networkSet(&net); networkFree(&net); net_js_setObj(obj); networkState = NETWORKSTATE_ONLINE; return jsvLockAgain(obj); }
/*JSON{ "type" : "method", "class" : "WLAN", "name" : "disconnect", "generate" : "jswrap_wlan_disconnect" } Completely uninitialise and power down the CC3000. After this you'll have to use ```require("CC3000").connect()``` again. */ void jswrap_wlan_disconnect(JsVar *wlanObj) { JsNetwork net; if (!networkGetFromVar(&net)) return; jsvObjectSetChildAndUnLock(wlanObj,JS_HIDDEN_CHAR_STR"DIS", jsvNewFromBool(true)); networkState = NETWORKSTATE_OFFLINE; // force offline //wlan_disconnect(); wlan_stop(); networkFree(&net); }
/*JSON{ "type" : "method", "class" : "WLAN", "name" : "reconnect", "generate" : "jswrap_wlan_reconnect" } Completely uninitialise and power down the CC3000, then reconnect to the old access point. */ void jswrap_wlan_reconnect(JsVar *wlanObj) { JsNetwork net; if (!networkGetFromVar(&net)) return; JsVar *ap = jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"AP", 0); JsVar *key = jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"KEY", 0); JsVar *cb = jsvObjectGetChild(wlanObj,CC3000_ON_STATE_CHANGE, 0); jswrap_wlan_disconnect(wlanObj); jswrap_wlan_connect(wlanObj, ap, key, cb); jsvUnLock3(ap, key, cb); networkFree(&net); }
/*JSON{ "type":"staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "description" : "Initialise the WIZnet module and return an Ethernet object", "params" : [ ], "return" : ["JsVar", "An Ethernet Object"], "return_object":"Ethernet" }*/ JsVar *jswrap_wiznet_connect() { JsVar *ethObj = jspNewObject(0, "Ethernet"); // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); // CS Configuration jshSetPinStateIsManual(ETH_CS_PIN, false); jshPinOutput(ETH_CS_PIN, 1); // de-assert CS // Wiznet reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZCHIP Initialized fail.\r\n"); return 0; } /* PHY link status check */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); return 0; } } while (tmp == PHY_LINK_OFF); JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/*JSON{ "type" : "staticmethod", "class" : "net", "name" : "connect", "generate_full" : "jswrap_net_connect(options, callback, ST_NORMAL)", "params" : [ ["options","JsVar","An object containing host,port fields"], ["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."] ], "return" : ["JsVar","Returns a new net.Socket object"], "return_object" : "Socket" } Create a socket connection */ JsVar *jswrap_net_connect(JsVar *options, JsVar *callback, SocketType socketType) { bool unlockOptions = false; if (jsvIsString(options)) { options = jswrap_url_parse(options, false); unlockOptions = true; } if (!jsvIsObject(options)) { jsError("Expecting Options to be an Object but it was %t", options); return 0; } #ifdef USE_TLS if ((socketType&ST_TYPE_MASK) == ST_HTTP) { JsVar *protocol = jsvObjectGetChild(options, "protocol", 0); if (protocol && jsvIsStringEqual(protocol, "https:")) { socketType |= ST_TLS; } jsvUnLock(protocol); } #endif // Make sure we have a function as callback, or nothing (which is OK too) JsVar *skippedCallback = jsvSkipName(callback); if (!jsvIsUndefined(skippedCallback)) { if (!jsvIsFunction(skippedCallback)) { jsError("Expecting Callback Function but got %t", skippedCallback); jsvUnLock(skippedCallback); return 0; } jsvUnLock(skippedCallback); } else { callback = NULL; } JsVar *rq = clientRequestNew(socketType, options, callback); if (unlockOptions) jsvUnLock(options); if ((socketType&ST_TYPE_MASK) != ST_HTTP) { JsNetwork net; if (networkGetFromVarIfOnline(&net)) { clientRequestConnect(&net, rq); } networkFree(&net); } return rq; }
/*JSON{ "type" : "staticmethod", "class" : "CC3000", "name" : "connect", "generate" : "jswrap_cc3000_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default). SPI should be 1,000,000 baud, and set to 'mode 1'"], ["cs", "pin", "The pin to use for Chip Select"], ["en", "pin", "The pin to use for Enable"], ["irq", "pin", "The pin to use for Interrupts"] ], "return" : ["JsVar","A WLAN Object"], "return_object" : "WLAN" } Initialise the CC3000 and return a WLAN object */ JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = WLAN_CLK_PIN; inf.pinMISO = WLAN_MISO_PIN; inf.pinMOSI = WLAN_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1 jshSPISetup(WLAN_SPI, &inf); spiDevice = WLAN_SPI; } if (!jshIsPinValid(cs)) cs = WLAN_CS_PIN; if (!jshIsPinValid(en)) en = WLAN_EN_PIN; if (!jshIsPinValid(irq)) irq = WLAN_IRQ_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_CC3000); net.data.device = spiDevice; net.data.pinCS = cs; net.data.pinEN = en; net.data.pinIRQ = irq; networkSet(&net); JsVar *wlanObj = jspNewObject(0, "WLAN"); cc3000_initialise(wlanObj); networkFree(&net); return wlanObj; }
/*JSON{ "type" : "method", "class" : "ESPWifi", "name" : "connect", "generate" : "jswrap_esp8266_connect", "params" : [ ["ap","JsVar","Access point name"], ["key","JsVar","WPA2 key (or undefined for unsecured connection)"], ["callback","JsVar","Function to call back with connection status. It has one argument which is one of 'connect'/'disconnect'/'dhcp'"] ], "return" : ["bool",""] } Connect to an access point */ bool jswrap_esp8266_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callback) { NOT_USED(wlanObj); JsNetwork net; if (!networkGetFromVar(&net)) return false; // 'AT+CWMODE=1\r' ? seems to be the default JsVar *msg = jsvVarPrintf("AT+CWJAP=%q,%q\r", vAP, vKey); esp8266_send(msg); jsvUnLock(msg); if (!esp8266_wait_for("OK",500, false)) return false; networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return true; }
/*JSON{ "type" : "staticmethod", "class" : "http", "name" : "get", "generate" : "jswrap_http_get", "params" : [ ["options","JsVar","An object containing host,port,path,method fields"], ["callback","JsVar","A function(res) that will be called when a connection is made. You can then call `res.on('data', function(data) { ... })` and `res.on('close', function() { ... })` to deal with the response."] ], "return" : ["JsVar","Returns a new httpCRq object"], "return_object" : "httpCRq" } Create an HTTP Request - convenience function for ```http.request()```. `options.method` is set to 'get', and end is called automatically. See [the Internet page](/Internet) for more usage examples. */ JsVar *jswrap_http_get(JsVar *options, JsVar *callback) { JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return 0; if (jsvIsObject(options)) { // if options is a string - it will be parsed, and GET will be set automatically JsVar *method = jsvNewFromString("GET"); jsvUnLock2(jsvAddNamedChild(options, method, "method"), method); } JsVar *skippedCallback = jsvSkipName(callback); if (!jsvIsUndefined(skippedCallback) && !jsvIsFunction(skippedCallback)) { jsError("Expecting Callback Function but got %t", skippedCallback); jsvUnLock(skippedCallback); return 0; } jsvUnLock(skippedCallback); JsVar *cliReq = jswrap_net_connect(options, callback, ST_HTTP); if (cliReq) clientRequestEnd(&net, cliReq); networkFree(&net); return cliReq; }
void telnetSendChar(char ch) { if (tnSrv.sock == 0 || tnSrv.cliSock == 0) return; if (tnSrv.txBufLen >= TX_CHUNK) { // buffer overflow :-( if (!ovf) { printf("tnSrv: send overflow!\n"); ovf = true; } } else { ovf = false; tnSrv.txBuf[tnSrv.txBufLen++] = ch; } // if the buffer has a bunch of chars then try to send, else it'll happen // at idle time. if (tnSrv.txBufLen < TX_CHUNK/4) return; JsNetwork net; if (!networkGetFromVarIfOnline(&net)) return; telnetSendBuf(&net); networkFree(&net); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266", "name" : "connect", "generate" : "jswrap_esp8266_connect_device", "params" : [ ["serial","JsVar","The Serial port used for communications with the ESP8266 (must already be setup)"], ["callback","JsVar","Function to call back when connected"] ], "return" : ["JsVar","An ESP8266 Object"], "return_object" : "ESP8266" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_esp8266_connect_device(JsVar *usart, JsVar *callback) { IOEventFlags usartDevice; usartDevice = jsiGetDeviceFromClass(usart); if (!DEVICE_IS_USART(usartDevice)) { jsExceptionHere(JSET_ERROR, "Expecting USART device, got %q", usart); return 0; } JsNetwork net; networkCreate(&net, JSNETWORKTYPE_ESP8266); net.data.device = usartDevice; networkSet(&net); JsVar *wifiObj = 0; JsVar *cmd = jsvNewFromString("AT+RST\r\n"); esp8266_send(cmd); jsvUnLock(cmd); if (esp8266_wait_for("OK", 100, false)) { if (esp8266_wait_for("ready", 4000, false)) { networkState = NETWORKSTATE_ONLINE; wifiObj = jspNewObject(0, "ESPWifi"); } else { jsExceptionHere(JSET_ERROR, "Module not ready"); } } else { jsExceptionHere(JSET_ERROR, "No Acknowledgement"); } networkFree(&net); if (callback) jsiQueueEvents(callback, 0, 0); return wifiObj; }
/*JSON{ "type" : "method", "class" : "WLAN", "name" : "connect", "generate" : "jswrap_wlan_connect", "params" : [ ["ap","JsVar","Access point name"], ["key","JsVar","WPA2 key (or undefined for unsecured connection)"], ["callback","JsVar","Function to call back with connection status. It has one argument which is one of 'connect'/'disconnect'/'dhcp'"] ], "return" : ["bool","True if connection succeeded, false if it didn't."] } Connect to a wireless network */ bool jswrap_wlan_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callback) { if (!(jsvIsUndefined(callback) || jsvIsFunction(callback))) { jsError("Expecting callback Function but got %t", callback); return 0; } JsNetwork net; if (!networkGetFromVar(&net)) return false; // if previously completely disconnected, try and reconnect if (jsvGetBoolAndUnLock(jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"DIS",0))) { cc3000_initialise(wlanObj); jsvObjectSetChildAndUnLock(wlanObj,JS_HIDDEN_CHAR_STR"DIS", jsvNewFromBool(false)); } if (jsvIsFunction(callback)) { jsvObjectSetChild(wlanObj, CC3000_ON_STATE_CHANGE, callback); } jsvObjectSetChild(wlanObj,JS_HIDDEN_CHAR_STR"AP", vAP); // no unlock intended jsvObjectSetChild(wlanObj,JS_HIDDEN_CHAR_STR"KEY", vKey); // no unlock intended char ap[32]; char key[32]; unsigned long security = WLAN_SEC_UNSEC; jsvGetString(vAP, ap, sizeof(ap)); if (jsvIsString(vKey)) { security = WLAN_SEC_WPA2; jsvGetString(vKey, key, sizeof(key)); } // might want to set wlan_ioctl_set_connection_policy bool connected = wlan_connect(security, ap, (long)strlen(ap), NULL, (unsigned char*)key, (long)strlen(key))==0; networkFree(&net); // note that we're only online (for networkState) when DHCP succeeds return connected; }
/*JSON{ "type" : "method", "class" : "WLAN", "name" : "setIP", "generate" : "jswrap_wlan_setIP", "params" : [ ["options","JsVar","Object containing IP address options `{ ip : '1,2,3,4', subnet, gateway, dns }`, or do not supply an object in otder to force DHCP."] ], "return" : ["bool","True on success"] } Set the current IP address for get an IP from DHCP (if no options object is specified). **Note:** Changes are written to non-volatile memory, but will only take effect after calling `wlan.reconnect()` */ bool jswrap_wlan_setIP(JsVar *wlanObj, JsVar *options) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsError("Not connected to the internet"); return false; } JsNetwork net; if (!networkGetFromVar(&net)) return false; tNetappIpconfigRetArgs ipconfig; netapp_ipconfig(&ipconfig); if (jsvIsObject(options)) { _wlan_getIP_set_address(options, "ip", &ipconfig.aucIP[0]); _wlan_getIP_set_address(options, "subnet", &ipconfig.aucSubnetMask[0]); _wlan_getIP_set_address(options, "gateway", &ipconfig.aucDefaultGateway[0]); _wlan_getIP_set_address(options, "dns", &ipconfig.aucDNSServer[0]); } else { // DHCP - just set all values to 0 *((unsigned long*)&ipconfig.aucIP[0]) = 0; *((unsigned long*)&ipconfig.aucSubnetMask) = 0; *((unsigned long*)&ipconfig.aucDefaultGateway) = 0; } bool result = netapp_dhcp( (unsigned long *)&ipconfig.aucIP[0], (unsigned long *)&ipconfig.aucSubnetMask[0], (unsigned long *)&ipconfig.aucDefaultGateway[0], (unsigned long *)&ipconfig.aucDNSServer[0]) == 0; networkFree(&net); return result; }
/*JSON{ "type" : "method", "class" : "Ethernet", "name" : "getIP", "generate" : "jswrap_ethernet_getIP", "params" : [ ["options","JsVar","An optional `callback(err, ipinfo)` function to be called back with the IP information."] ], "return" : ["JsVar",""] } Get the current IP address, subnet, gateway and mac address. */ JsVar *jswrap_ethernet_getIP(JsVar *wlanObj, JsVar *callback) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsExceptionHere(JSET_ERROR, "Not connected to the internet"); return 0; } JsNetwork net; if (!networkGetFromVar(&net)) return 0; wiz_NetInfo gWIZNETINFO; ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); /* If byte 1 is 0 we don't have a valid address */ JsVar *data = jsvNewObject(); networkPutAddressAsString(data, "ip", &gWIZNETINFO.ip[0], 4, 10, '.'); networkPutAddressAsString(data, "subnet", &gWIZNETINFO.sn[0], 4, 10, '.'); networkPutAddressAsString(data, "gateway", &gWIZNETINFO.gw[0], 4, 10, '.'); networkPutAddressAsString(data, "dns", &gWIZNETINFO.dns[0], 4, 10, '.'); networkPutAddressAsString(data, "mac", &gWIZNETINFO.mac[0], 6, 16, ':'); networkFree(&net); // Schedule callback if a function was provided if (jsvIsFunction(callback)) { JsVar *params[2]; params[0] = jsvNewWithFlags(JSV_NULL); params[1] = data; jsiQueueEvents(NULL, callback, params, 2); jsvUnLock(params[0]); } return data; }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/*JSON{ "type" : "method", "class" : "Ethernet", "name" : "setIP", "generate" : "jswrap_ethernet_setIP", "params" : [ ["options","JsVar","Object containing IP address options `{ ip : '1,2,3,4', subnet, gateway, dns, mac }`, or do not supply an object in order to force DHCP."] ], "return" : ["bool","True on success"] } Set the current IP address or get an IP from DHCP (if no options object is specified) If 'mac' is specified as an option, it must be a string of the form `"00:01:02:03:04:05"` */ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsError("Not connected to the internet"); return false; } JsNetwork net; if (!networkGetFromVar(&net)) return false; bool success = false; wiz_NetInfo gWIZNETINFO; ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); if (!gWIZNETINFO.mac[0] && !gWIZNETINFO.mac[1] && !gWIZNETINFO.mac[2] && !gWIZNETINFO.mac[3] && !gWIZNETINFO.mac[4] && !gWIZNETINFO.mac[5]) { // wow - no mac address - WIZ550BoB? Set up a simple one // in WIZnet's range of addresses gWIZNETINFO.mac[0]=0x00; gWIZNETINFO.mac[1]=0x08; gWIZNETINFO.mac[2]=0xDC; gWIZNETINFO.mac[3]=0x01; gWIZNETINFO.mac[4]=0x02; gWIZNETINFO.mac[5]=0x03; } if (jsvIsObject(options)) { _eth_getIP_set_address(options, "ip", &gWIZNETINFO.ip[0]); _eth_getIP_set_address(options, "subnet", &gWIZNETINFO.sn[0]); _eth_getIP_set_address(options, "gateway", &gWIZNETINFO.gw[0]); _eth_getIP_set_address(options, "dns", &gWIZNETINFO.dns[0]); JsVar *info = jsvObjectGetChild(options, "mac", 0); if (info) { char buf[64]; jsvGetString(info, buf, sizeof(buf)); networkParseMACAddress(&gWIZNETINFO.mac[0], buf); // TODO: check failure? jsvUnLock(info); } gWIZNETINFO.dhcp = NETINFO_STATIC; success = true; } else { // DHCP uint8_t DHCPisSuccess = getIP_DHCPS(net_wiznet_getFreeSocket(), &gWIZNETINFO); if (DHCPisSuccess == 1) { // info in lease_time.lVal success = true; } else { jsWarn("DHCP failed"); success = false; } } ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); networkFree(&net); return success; }
/*JSON{ "type":"kill", "generate" : "jswrap_http_kill" }*/ void jswrap_http_kill() { JsNetwork net; if (!networkGetFromVar(&net)) return; httpKill(&net); networkFree(&net); }
/*JSON{ "type" : "method", "class" : "Ethernet", "name" : "setIP", "generate" : "jswrap_ethernet_setIP", "params" : [ ["options","JsVar","Object containing IP address options `{ ip : '1,2,3,4', subnet, gateway, dns, mac }`, or do not supply an object in order to force DHCP."], ["options","JsVar","An optional `callback(err)` function to invoke when ip is set. `err==null` on success, or a string on failure."] ], "return" : ["bool","True on success"] } Set the current IP address or get an IP from DHCP (if no options object is specified) If 'mac' is specified as an option, it must be a string of the form `"00:01:02:03:04:05"` */ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options, JsVar *callback) { NOT_USED(wlanObj); if (networkState != NETWORKSTATE_ONLINE) { jsExceptionHere(JSET_ERROR, "Not connected to the internet"); return false; } JsNetwork net; if (!networkGetFromVar(&net)) return false; const char *errorMessage = 0; wiz_NetInfo gWIZNETINFO; ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); if (!gWIZNETINFO.mac[0] && !gWIZNETINFO.mac[1] && !gWIZNETINFO.mac[2] && !gWIZNETINFO.mac[3] && !gWIZNETINFO.mac[4] && !gWIZNETINFO.mac[5]) { // wow - no mac address - WIZ550BoB? Set up a simple one // in WIZnet's range of addresses gWIZNETINFO.mac[0]=0x00; gWIZNETINFO.mac[1]=0x08; gWIZNETINFO.mac[2]=0xDC; gWIZNETINFO.mac[3]=0x01; gWIZNETINFO.mac[4]=0x02; gWIZNETINFO.mac[5]=0x03; } if (jsvIsObject(options)) { _eth_getIP_set_address(options, "ip", &gWIZNETINFO.ip[0]); _eth_getIP_set_address(options, "subnet", &gWIZNETINFO.sn[0]); _eth_getIP_set_address(options, "gateway", &gWIZNETINFO.gw[0]); _eth_getIP_set_address(options, "dns", &gWIZNETINFO.dns[0]); JsVar *info = jsvObjectGetChild(options, "mac", 0); if (info) { char buf[64]; jsvGetString(info, buf, sizeof(buf)); networkParseMACAddress(&gWIZNETINFO.mac[0], buf); // TODO: check failure? jsvUnLock(info); } gWIZNETINFO.dhcp = NETINFO_STATIC; errorMessage = 0; // all ok } else { // DHCP uint8_t DHCPisSuccess = getIP_DHCPS(net_wiznet_getFreeSocket(), &gWIZNETINFO); if (DHCPisSuccess == 1) { // info in lease_time.lVal errorMessage = 0; // all ok } else { errorMessage = "DHCP failed"; jsWarn(errorMessage); } } ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); networkFree(&net); // Schedule callback if a function was provided if (jsvIsFunction(callback)) { JsVar *params[1]; params[0] = errorMessage ? jsvNewFromString(errorMessage) : jsvNewWithFlags(JSV_NULL); jsiQueueEvents(NULL, callback, params, 1); jsvUnLock(params[0]); } return errorMessage==0; }