From 71f7ca0e55e661b217dbdd519da0be570fe34f10 Mon Sep 17 00:00:00 2001 From: MaBecker Date: Tue, 3 Dec 2024 08:15:00 +0100 Subject: [PATCH 1/3] ES32: add setIP and setAPIP --- ChangeLog | 1 + libs/network/esp32/jswrap_esp32_network.c | 99 +++++++++++++++++++++++ libs/network/jswrap_wifi.c | 4 +- 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 337003bd60..6aebe91ae7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -64,6 +64,7 @@ STM32F4: Add SDIO support STM32: Ensure we kick the WDT if auto kicking is enabled and in deep sleep (avoids having to to it manually and wait 30ms for USB to wake up/shut down) Allow a 'file receive' packet which can request Espruino sends a file as binary packets (also fix files not being closed if transmission fails) + ES32: add setIP and setAPIP 2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()' Bangle.js2: After Bangle.showTestScreen, put Bangle.js into a hard off state (not soft off) diff --git a/libs/network/esp32/jswrap_esp32_network.c b/libs/network/esp32/jswrap_esp32_network.c index 00b5e02539..b00e1a4acb 100644 --- a/libs/network/esp32/jswrap_esp32_network.c +++ b/libs/network/esp32/jswrap_esp32_network.c @@ -92,6 +92,8 @@ static bool g_isStaConnected = false; #define EXPECT_CB_EXCEPTION(jsCB) jsExceptionHere(JSET_ERROR, "Expecting callback function but got %v", jsCB) #define EXPECT_OPT_EXCEPTION(jsOPT) jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", jsOPT) +// Global data structure for setIP and setAPIP +tcpip_adapter_ip_info_t info; //===== mDNS static bool mdns_started = 0; @@ -1775,3 +1777,100 @@ void jswrap_wifi_getHostByName( dnsFoundCallback(hostname, NULL, NULL); } } + +// worker for jswrap_wifi_setIP and jswrap_wifi_setAPIP +static void setIP(JsVar *jsSettings, JsVar *jsCallback, int interface) { + char ipTmp[20]; + int len = 0; + // bool rc = false; + esp_err_t err; + + memset( &info, 0, sizeof(info) ); + +// first check parameter + if (!jsvIsObject(jsSettings)) { + EXPECT_OPT_EXCEPTION(jsSettings); + return; + } + +// get,check and store ip + JsVar *jsIP = jsvObjectGetChildIfExists(jsSettings, "ip"); + if (jsIP != NULL && !jsvIsString(jsIP)) { + EXPECT_OPT_EXCEPTION(jsIP); + jsvUnLock(jsIP); + return; + } + jsvGetString(jsIP, ipTmp, sizeof(ipTmp)-1); + info.ip.addr = networkParseIPAddress(ipTmp); + if ( info.ip.addr == 0) { + jsExceptionHere(JSET_ERROR, "Not a valid IP address"); + jsvUnLock(jsIP); + return; + } + jsvUnLock(jsIP); + +// get, check and store gw + JsVar *jsGW = jsvObjectGetChildIfExists(jsSettings, "gw"); + if (jsGW != NULL && !jsvIsString(jsGW)) { + EXPECT_OPT_EXCEPTION(jsGW); + jsvUnLock(jsGW); + return ; + } + jsvGetString(jsGW, ipTmp, sizeof(ipTmp)-1); + info.gw.addr = networkParseIPAddress(ipTmp); + if (info.gw.addr == 0) { + jsExceptionHere(JSET_ERROR, "Not a valid Gateway address"); + jsvUnLock(jsGW); + return; + } + jsvUnLock(jsGW); + +// netmask setting + JsVar *jsNM = jsvObjectGetChildIfExists(jsSettings, "netmask"); + if (jsNM != NULL && !jsvIsString(jsNM)) { + EXPECT_OPT_EXCEPTION(jsNM); + jsvUnLock(jsNM); + return; + } + jsvGetString(jsNM, ipTmp, sizeof(ipTmp)-1); + info.netmask.addr = networkParseIPAddress(ipTmp); + if (info.netmask.addr == 0) { + jsExceptionHere(JSET_ERROR, "Not a valid Netmask"); + jsvUnLock(jsNM); + return; + } + jsvUnLock(jsNM); +// set IP for station + if (interface == WIFI_IF_STA) { + tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA); + err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info); + } +// set IP for access point + else { + tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP); + err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info); + tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP); + } +// Schedule callback + if (jsvIsFunction(jsCallback)) { + JsVar *params[1]; + params[0] = err ? jsvNewWithFlags(JSV_NULL) : jsvNewFromString("Failure"); + jsiQueueEvents(NULL, jsCallback, params, 1); + jsvUnLock(params[0]); + } + else { + jsExceptionHere(JSET_ERROR, "Callback is not a function"); + } + return ; +}; + + +void jswrap_wifi_setIP(JsVar *jsSettings, JsVar *jsCallback) { + setIP(jsSettings, jsCallback, WIFI_IF_STA); + return ; +} + +void jswrap_wifi_setAPIP(JsVar *jsSettings, JsVar *jsCallback) { + setIP(jsSettings, jsCallback, WIFI_IF_AP); + return ; +} diff --git a/libs/network/jswrap_wifi.c b/libs/network/jswrap_wifi.c index d316b22502..36773d98e7 100644 --- a/libs/network/jswrap_wifi.c +++ b/libs/network/jswrap_wifi.c @@ -605,7 +605,7 @@ returns. "class" : "Wifi", "name" : "setIP", "generate" : "jswrap_wifi_setIP", - "#if" : "defined(ESP8266) || defined(ESPRUINOWIFI)", + "#if" : "defined(ESP8266) || defined(ESPRUINOWIFI) || defined(ESP32)", "params" : [ ["settings", "JsVar", "Configuration settings"], ["callback", "JsVar", "A `callback(err)` function to invoke when ip is set. `err==null` on success, or a string on failure."] @@ -623,7 +623,7 @@ The `settings` object must contain the following properties. "type" : "staticmethod", "class" : "Wifi", "name" : "setAPIP", - "#if" : "defined(ESPRUINOWIFI) || defined(ESP8266)", + "#if" : "defined(ESPRUINOWIFI) || defined(ESP8266) || defined(ESP32)", "generate" : "jswrap_wifi_setAPIP", "params" : [ ["settings", "JsVar", "Configuration settings"], From 130666479d9b0065f2f0334b85810476cded8917 Mon Sep 17 00:00:00 2001 From: MaBecker Date: Tue, 3 Dec 2024 08:18:22 +0100 Subject: [PATCH 2/3] ESP32: add setIP and setAPIP fix typo --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 6aebe91ae7..80e97c2012 100644 --- a/ChangeLog +++ b/ChangeLog @@ -64,7 +64,7 @@ STM32F4: Add SDIO support STM32: Ensure we kick the WDT if auto kicking is enabled and in deep sleep (avoids having to to it manually and wait 30ms for USB to wake up/shut down) Allow a 'file receive' packet which can request Espruino sends a file as binary packets (also fix files not being closed if transmission fails) - ES32: add setIP and setAPIP + ESP32: add setIP and setAPIP 2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()' Bangle.js2: After Bangle.showTestScreen, put Bangle.js into a hard off state (not soft off) From f103d6d92bb5bc48a4d1e6c0365807c9408e29dd Mon Sep 17 00:00:00 2001 From: MaBecker Date: Tue, 3 Dec 2024 23:06:29 +0100 Subject: [PATCH 3/3] ESP32: add setIP and setAPIP remove global var --- libs/network/esp32/jswrap_esp32_network.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libs/network/esp32/jswrap_esp32_network.c b/libs/network/esp32/jswrap_esp32_network.c index b00e1a4acb..633ba93ff6 100644 --- a/libs/network/esp32/jswrap_esp32_network.c +++ b/libs/network/esp32/jswrap_esp32_network.c @@ -92,9 +92,6 @@ static bool g_isStaConnected = false; #define EXPECT_CB_EXCEPTION(jsCB) jsExceptionHere(JSET_ERROR, "Expecting callback function but got %v", jsCB) #define EXPECT_OPT_EXCEPTION(jsOPT) jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", jsOPT) -// Global data structure for setIP and setAPIP -tcpip_adapter_ip_info_t info; - //===== mDNS static bool mdns_started = 0; @@ -1782,9 +1779,8 @@ void jswrap_wifi_getHostByName( static void setIP(JsVar *jsSettings, JsVar *jsCallback, int interface) { char ipTmp[20]; int len = 0; - // bool rc = false; esp_err_t err; - + tcpip_adapter_ip_info_t info; memset( &info, 0, sizeof(info) ); // first check parameter