Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: add setIP and setAPIP #2581

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
Allow a 'file receive' packet which can request Espruino sends a file as binary packets (also fix files not being closed if transmission fails)
Fix parsing of semicolons in DO with a statement: `do print(a);while(a--);`
In SAVE_ON_FLASH builds (Microbit 1) remove getSerial, Math.LN*/LOG*SQRT* constants, passwords, Serial/I2C/SPI.find, Date.toUTCString
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)
Expand Down
97 changes: 96 additions & 1 deletion libs/network/esp32/jswrap_esp32_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +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)


//===== mDNS
static bool mdns_started = 0;

Expand Down Expand Up @@ -1775,3 +1774,99 @@ 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;
esp_err_t err;
tcpip_adapter_ip_info_t info;
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 ;
}
4 changes: 2 additions & 2 deletions libs/network/jswrap_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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."]
Expand All @@ -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"],
Expand Down
Loading