From 1029be7ae433b76e623d6ff66afc58477364bb7c Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:37:47 +0200 Subject: [PATCH 01/12] WiFi add SSID() and RSSI() --- libraries/SocketWrapper/WiFi.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index c83846b2..c223a72b 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -75,14 +75,12 @@ class WiFiClass: public NetworkInterface } int status() { - struct wifi_iface_status status = { 0 }; - - if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &status, + if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, sizeof(struct wifi_iface_status))) { return WL_NO_SHIELD; } - if (status.state >= WIFI_STATE_ASSOCIATED) { + if (sta_state.state >= WIFI_STATE_ASSOCIATED) { return WL_CONNECTED; } else { return WL_DISCONNECTED; @@ -94,12 +92,28 @@ class WiFiClass: public NetworkInterface // TODO: borrow code from mbed core for scan results handling } + char* SSID() { + if (status() == WL_CONNECTED) { + return (char *)sta_state.ssid; + } + return nullptr; + } + + int32_t RSSI() { + if (status() == WL_CONNECTED) { + return sta_state.rssi; + } + return 0; + } + private: struct net_if *sta_iface = nullptr; struct net_if *ap_iface = nullptr; struct wifi_connect_req_params ap_config; struct wifi_connect_req_params sta_config; + + struct wifi_iface_status sta_state = { 0 }; }; extern WiFiClass WiFi; From cb8a82d604256d94db70fab14309a9d4dfa895df Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:38:23 +0200 Subject: [PATCH 02/12] WiFi allow status() to be called before begin() --- libraries/SocketWrapper/WiFi.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index c223a72b..43b3bc58 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -75,6 +75,8 @@ class WiFiClass: public NetworkInterface } int status() { + sta_iface = net_if_get_wifi_sta(); + netif = sta_iface; if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, sizeof(struct wifi_iface_status))) { return WL_NO_SHIELD; From 3ddeff96b0e60e8649bcf878db0ce7dbad9c7d3c Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:40:27 +0200 Subject: [PATCH 03/12] WiFi wait for event NET_EVENT_WIFI_CONNECT_RESULT in blocking mode --- libraries/SocketWrapper/WiFi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index 43b3bc58..eb0f275b 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -37,7 +37,7 @@ class WiFiClass: public NetworkInterface NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); if (blocking) { - net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_AP_STA_CONNECTED, NULL, NULL, NULL, K_FOREVER); + net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); } return true; From 55934df737bd1bea4eda1ff4c06c8042c4ad2442 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:41:15 +0200 Subject: [PATCH 04/12] WiFi make begin() blocking by default --- libraries/SocketWrapper/WiFi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index eb0f275b..b366270f 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -16,7 +16,7 @@ class WiFiClass: public NetworkInterface WiFiClass() {} ~WiFiClass() {} - bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = false) { + bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { sta_iface = net_if_get_wifi_sta(); netif = sta_iface; sta_config.ssid = (const uint8_t *)ssid; @@ -43,7 +43,7 @@ class WiFiClass: public NetworkInterface return true; } - bool beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = false) { + bool beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true) { if (ap_iface != NULL) { return false; } From d5c8070342c0dd6a353b2aa549b6313b75959035 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:43:51 +0200 Subject: [PATCH 05/12] WiFi make begin() return status() --- libraries/SocketWrapper/WiFi.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index b366270f..378c19ec 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -16,7 +16,7 @@ class WiFiClass: public NetworkInterface WiFiClass() {} ~WiFiClass() {} - bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { + int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { sta_iface = net_if_get_wifi_sta(); netif = sta_iface; sta_config.ssid = (const uint8_t *)ssid; @@ -40,10 +40,10 @@ class WiFiClass: public NetworkInterface net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); } - return true; + return status(); } - bool beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true) { + int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true) { if (ap_iface != NULL) { return false; } @@ -71,7 +71,7 @@ class WiFiClass: public NetworkInterface net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER); } - return true; + return status(); } int status() { From 3fbe03c1ef7f2eca86b731b180db1752b7a6993b Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:49:00 +0200 Subject: [PATCH 06/12] WiFi add WiFiWebclient example --- .../examples/WiFiWebClient/WiFiWebClient.ino | 106 ++++++++++++++++++ .../examples/WiFiWebClient/arduino_secrets.h | 2 + 2 files changed, 108 insertions(+) create mode 100644 libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino create mode 100644 libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino b/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino new file mode 100644 index 00000000..a5687041 --- /dev/null +++ b/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino @@ -0,0 +1,106 @@ +/* + Web client + + This sketch connects to a website (http://example.com) using the WiFi module. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +// IPAddress server(93,184,216,34); // IP address for example.com (no DNS) +char server[] = "example.com"; // host name for example.com (using DNS) + +ZephyrClient client; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to Wifi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + Serial.println(status); + // wait 3 seconds for connection: + delay(3000); + } + Serial.println("Connected to wifi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /index.html HTTP/1.1"); + client.print("Host: "); + client.println(server); + client.println("Connection: close"); + client.println(); + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while (true); + } +} + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h b/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h new file mode 100644 index 00000000..0c9fdd55 --- /dev/null +++ b/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" From 90a283e4e6f3367bf6caba4f8a792e24cae0543c Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 5 Aug 2025 14:09:20 +0200 Subject: [PATCH 07/12] Splitting the definition of Wifi interface from socket wrapper --- libraries/WiFi/library.properties | 10 ++++++++++ libraries/{SocketWrapper => WiFi/src}/WiFi.cpp | 0 libraries/{SocketWrapper => WiFi/src}/WiFi.h | 0 .../src}/utility/wl_definitions.h | 0 4 files changed, 10 insertions(+) create mode 100644 libraries/WiFi/library.properties rename libraries/{SocketWrapper => WiFi/src}/WiFi.cpp (100%) rename libraries/{SocketWrapper => WiFi/src}/WiFi.h (100%) rename libraries/{SocketWrapper => WiFi/src}/utility/wl_definitions.h (100%) diff --git a/libraries/WiFi/library.properties b/libraries/WiFi/library.properties new file mode 100644 index 00000000..844b5bdf --- /dev/null +++ b/libraries/WiFi/library.properties @@ -0,0 +1,10 @@ +name=WiFi +version=1.0.0 +author=Arduino +maintainer=Arduino +sentence=Enables WIFI connection +paragraph=With this library you can use the WiFi to connect to Internet. +category=Communication +url=https://github.com/arduino/ArduinoCore-zephyr/tree/master/libraries/WiFi +architectures=renesas,renesas_portenta +includes=WiFi.h diff --git a/libraries/SocketWrapper/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp similarity index 100% rename from libraries/SocketWrapper/WiFi.cpp rename to libraries/WiFi/src/WiFi.cpp diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/WiFi/src/WiFi.h similarity index 100% rename from libraries/SocketWrapper/WiFi.h rename to libraries/WiFi/src/WiFi.h diff --git a/libraries/SocketWrapper/utility/wl_definitions.h b/libraries/WiFi/src/utility/wl_definitions.h similarity index 100% rename from libraries/SocketWrapper/utility/wl_definitions.h rename to libraries/WiFi/src/utility/wl_definitions.h From 9d680e6a438f68bf3bfd62524ac5cd64019ec226 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 5 Aug 2025 14:17:28 +0200 Subject: [PATCH 08/12] [Wifi] added firmwareVersion method with preprocessor --- libraries/WiFi/src/WiFi.cpp | 11 +++++++++++ libraries/WiFi/src/WiFi.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 4290fe2e..33f197bc 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -1,3 +1,14 @@ #include "WiFi.h" WiFiClass WiFi; + +String WiFiClass::firmwareVersion() { // TODO integrate fw version detection +#if defined(ARDUINO_PORTENTA_C33) + return "v1.5.0"; +#elif defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) ||\ + defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_NICLA_SENSE_ME) + return "v0.0.0"; +#else + return "v0.0.0"; +#endif +} diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 378c19ec..2e2e6197 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -108,6 +108,8 @@ class WiFiClass: public NetworkInterface return 0; } + String firmwareVersion(); + private: struct net_if *sta_iface = nullptr; struct net_if *ap_iface = nullptr; From 24899dd4b55cd560cc3387d5de7eace3b124a75c Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 7 Aug 2025 13:41:11 +0200 Subject: [PATCH 09/12] moved example to wifi lib --- .../examples/WiFiWebClient/WiFiWebClient.ino | 0 .../examples/WiFiWebClient/arduino_secrets.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename libraries/{SocketWrapper => WiFi}/examples/WiFiWebClient/WiFiWebClient.ino (100%) rename libraries/{SocketWrapper => WiFi}/examples/WiFiWebClient/arduino_secrets.h (100%) diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino b/libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino similarity index 100% rename from libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino rename to libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h b/libraries/WiFi/examples/WiFiWebClient/arduino_secrets.h similarity index 100% rename from libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h rename to libraries/WiFi/examples/WiFiWebClient/arduino_secrets.h From d088e7e2ce6c78ab6587df037200a042bb1b5e01 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 7 Aug 2025 13:48:25 +0200 Subject: [PATCH 10/12] codestyle unification --- libraries/WiFi/src/WiFi.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 2e2e6197..05eec616 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -27,7 +27,7 @@ class WiFiClass: public NetworkInterface sta_config.security = WIFI_SECURITY_TYPE_PSK; sta_config.channel = WIFI_CHANNEL_ANY; sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; - sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; + sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, sizeof(struct wifi_connect_req_params)); @@ -56,7 +56,7 @@ class WiFiClass: public NetworkInterface ap_config.security = WIFI_SECURITY_TYPE_PSK; ap_config.channel = channel; ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; - ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; + ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, sizeof(struct wifi_connect_req_params)); @@ -82,7 +82,7 @@ class WiFiClass: public NetworkInterface return WL_NO_SHIELD; } - if (sta_state.state >= WIFI_STATE_ASSOCIATED) { + if (sta_state.state >= WIFI_STATE_ASSOCIATED) { return WL_CONNECTED; } else { return WL_DISCONNECTED; From 7cd50a10ca13c27d7078500f5219e5231daafa7d Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 7 Aug 2025 16:37:05 +0200 Subject: [PATCH 11/12] moving implementation into cpp file --- libraries/WiFi/src/WiFi.cpp | 90 +++++++++++++++++++++++++++++++++++++ libraries/WiFi/src/WiFi.h | 89 +++--------------------------------- 2 files changed, 95 insertions(+), 84 deletions(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 33f197bc..0150b167 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -12,3 +12,93 @@ String WiFiClass::firmwareVersion() { // TODO integrate fw version detection return "v0.0.0"; #endif } + + +int WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_type security, bool blocking) { + sta_iface = net_if_get_wifi_sta(); + netif = sta_iface; + sta_config.ssid = (const uint8_t *)ssid; + sta_config.ssid_length = strlen(ssid); + sta_config.psk = (const uint8_t *)passphrase; + sta_config.psk_length = strlen(passphrase); + // TODO: change these fields with scan() results + sta_config.security = WIFI_SECURITY_TYPE_PSK; + sta_config.channel = WIFI_CHANNEL_ANY; + sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; + sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; + + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + return false; + } + + // FIXME verify that non in blocking version dhcp can be called even if connect is not completed + NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); + if (blocking) { + net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); + } + + return status(); +} + +int WiFiClass::beginAP(char* ssid, char* passphrase, int channel, bool blocking) { + if (ap_iface != NULL) { + return false; + } + ap_iface = net_if_get_wifi_sap(); + netif = ap_iface; + ap_config.ssid = (const uint8_t *)ssid; + ap_config.ssid_length = strlen(ssid); + ap_config.psk = (const uint8_t *)passphrase; + ap_config.psk_length = strlen(passphrase); + ap_config.security = WIFI_SECURITY_TYPE_PSK; + ap_config.channel = channel; + ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; + ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; + + int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, + sizeof(struct wifi_connect_req_params)); + + if (ret) { + return false; + } + + enable_dhcpv4_server(ap_iface); + + if (blocking) { + net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER); + } + + return status(); +} + +int WiFiClass::status() { + sta_iface = net_if_get_wifi_sta(); + netif = sta_iface; + if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, + sizeof(struct wifi_iface_status))) { + return WL_NO_SHIELD; + } + + if (sta_state.state >= WIFI_STATE_ASSOCIATED) { + return WL_CONNECTED; + } else { + return WL_DISCONNECTED; + } + return WL_NO_SHIELD; +} + +char* WiFiClass::SSID() { + if (status() == WL_CONNECTED) { + return (char *)sta_state.ssid; + } + return nullptr; +} + +int32_t WiFiClass::RSSI() { + if (status() == WL_CONNECTED) { + return sta_state.rssi; + } + return 0; +} diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 05eec616..d424639d 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -16,97 +16,18 @@ class WiFiClass: public NetworkInterface WiFiClass() {} ~WiFiClass() {} - int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { - sta_iface = net_if_get_wifi_sta(); - netif = sta_iface; - sta_config.ssid = (const uint8_t *)ssid; - sta_config.ssid_length = strlen(ssid); - sta_config.psk = (const uint8_t *)passphrase; - sta_config.psk_length = strlen(passphrase); - // TODO: change these fields with scan() results - sta_config.security = WIFI_SECURITY_TYPE_PSK; - sta_config.channel = WIFI_CHANNEL_ANY; - sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; - sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; + int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true); - int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, - sizeof(struct wifi_connect_req_params)); - if (ret) { - return false; - } + int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true); - NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); - if (blocking) { - net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); - } - - return status(); - } - - int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true) { - if (ap_iface != NULL) { - return false; - } - ap_iface = net_if_get_wifi_sap(); - netif = ap_iface; - ap_config.ssid = (const uint8_t *)ssid; - ap_config.ssid_length = strlen(ssid); - ap_config.psk = (const uint8_t *)passphrase; - ap_config.psk_length = strlen(passphrase); - ap_config.security = WIFI_SECURITY_TYPE_PSK; - ap_config.channel = channel; - ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; - ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; - - int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, - sizeof(struct wifi_connect_req_params)); - - if (ret) { - return false; - } - - enable_dhcpv4_server(ap_iface); - - if (blocking) { - net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER); - } - - return status(); - } - - int status() { - sta_iface = net_if_get_wifi_sta(); - netif = sta_iface; - if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, - sizeof(struct wifi_iface_status))) { - return WL_NO_SHIELD; - } - - if (sta_state.state >= WIFI_STATE_ASSOCIATED) { - return WL_CONNECTED; - } else { - return WL_DISCONNECTED; - } - return WL_NO_SHIELD; - } + int status(); int8_t scanNetworks() { // TODO: borrow code from mbed core for scan results handling } - char* SSID() { - if (status() == WL_CONNECTED) { - return (char *)sta_state.ssid; - } - return nullptr; - } - - int32_t RSSI() { - if (status() == WL_CONNECTED) { - return sta_state.rssi; - } - return 0; - } + char* SSID(); + int32_t RSSI(); String firmwareVersion(); From b8b4f8e8c2b8ca451ea29691f976b25d87c0fdbe Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 7 Aug 2025 16:37:34 +0200 Subject: [PATCH 12/12] fixing blocking version of begin --- libraries/WiFi/src/WiFi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 0150b167..02c1826a 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -33,11 +33,11 @@ int WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_type secur return false; } - // FIXME verify that non in blocking version dhcp can be called even if connect is not completed - NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); if (blocking) { net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); } + // FIXME verify that non in blocking version dhcp can be called even if connect is not completed + NetworkInterface::begin(blocking, NET_EVENT_WIFI_MASK); return status(); }