forked fromzephyrproject-rtos/arduino-core-zephyr
Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34
WiFi: add scan()#221
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
pennam wants to merge3 commits intoarduino:mainChoose a base branch frompennam:wifi-scan
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
WiFi: add scan()#221
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletionslibraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
267 changes: 190 additions & 77 deletionslibraries/WiFi/src/WiFi.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,97 +1,210 @@ | ||
| #include "WiFi.h" | ||
| // Static Wi-Fi state instance | ||
| struct WiFiState { | ||
| 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}; | ||
| struct wifi_scan_result scanResults[MAX_SCAN_RESULTS]; | ||
| uint8_t resultCount = 0; | ||
| struct net_mgmt_event_callback wifiCb; | ||
| bool soughtNetworkFound = false; | ||
| bool scanSequenceFinished = false; | ||
| }; | ||
| WiFiClass::WiFiClass() {} | ||
| WiFiClass::~WiFiClass() {} | ||
| // Static instance of Wi-Fi state | ||
| struct WiFiState WiFiClass::wifiState; | ||
| int WiFiClass::begin(const char *ssid, const char *passphrase, | ||
| wl_enc_type security, bool blocking) { | ||
| wifi_security_type wifi_security = convert_enc_type_to_security_type(security); | ||
| wifiState.sta_iface = net_if_get_wifi_sta(); | ||
| netif = wifiState.sta_iface; | ||
| wifiState.sta_config.ssid = (const uint8_t *)ssid; | ||
| wifiState.sta_config.ssid_length = strlen(ssid); | ||
| wifiState.sta_config.psk = (const uint8_t *)passphrase; | ||
| wifiState.sta_config.psk_length = strlen(passphrase); | ||
| // Set Wi-Fi security type if specified | ||
| if (wifi_security != WIFI_SECURITY_TYPE_NONE) { | ||
| wifiState.sta_config.security = wifi_security; | ||
| } else { | ||
| wifiState.sta_config.security = WIFI_SECURITY_TYPE_PSK; | ||
| } | ||
| wifiState.sta_config.channel = WIFI_CHANNEL_ANY; | ||
| wifiState.sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; | ||
| wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; | ||
| // Register the Wi-Fi event callback | ||
| net_mgmt_init_event_callback(&wifiState.wifiCb, scanEventDispatcher, | ||
| NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE); | ||
| net_mgmt_add_event_callback(&wifiState.wifiCb); | ||
| // Trigger a network scan | ||
| (void)scanNetworks(); // Blocking call | ||
| // Attempt to connect to the network if configuration is valid | ||
| if (wifiState.sta_config.ssid && wifiState.sta_config.psk) { | ||
| int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, wifiState.sta_iface, &wifiState.sta_config, | ||
| sizeof(struct wifi_connect_req_params)); | ||
| if (ret) { | ||
| return false; | ||
| } | ||
| NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); | ||
| if (blocking) { | ||
| net_mgmt_event_wait_on_iface(wifiState.sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); | ||
| } | ||
| } | ||
| return status(); | ||
| } | ||
| bool WiFiClass::beginAP(char *ssid, char *passphrase, int channel, bool blocking) { | ||
| if (wifiState.ap_iface != nullptr) { | ||
| return false; // AP already initialized | ||
| } | ||
| wifiState.ap_iface = net_if_get_wifi_sap(); | ||
| netif = wifiState.ap_iface; | ||
| wifiState.ap_config.ssid = (const uint8_t *)ssid; | ||
| wifiState.ap_config.ssid_length = strlen(ssid); | ||
| wifiState.ap_config.psk = (const uint8_t *)passphrase; | ||
| wifiState.ap_config.psk_length = strlen(passphrase); | ||
| wifiState.ap_config.security = WIFI_SECURITY_TYPE_PSK; | ||
| wifiState.ap_config.channel = channel; | ||
| wifiState.ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; | ||
| wifiState.ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; | ||
| int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, wifiState.ap_iface, &wifiState.ap_config, | ||
| sizeof(struct wifi_connect_req_params)); | ||
| if (ret) { | ||
| return false; | ||
| } | ||
| enable_dhcpv4_server(wifiState.ap_iface); | ||
| if (blocking) { | ||
| net_mgmt_event_wait_on_iface(wifiState.ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER); | ||
| } | ||
| return true; | ||
| } | ||
| int WiFiClass::status() { | ||
| wifiState.sta_iface = net_if_get_wifi_sta(); | ||
| netif = wifiState.sta_iface; | ||
| if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &wifiState.sta_state, | ||
| sizeof(struct wifi_iface_status))) { | ||
| return WL_NO_SHIELD; | ||
| } | ||
| if (wifiState.sta_state.state >= WIFI_STATE_ASSOCIATED) { | ||
| return WL_CONNECTED; | ||
| } else { | ||
| return WL_DISCONNECTED; | ||
| } | ||
| } | ||
| int8_t WiFiClass::scanNetworks() { | ||
| wifiState.resultCount = 0u; | ||
| wifiState.soughtNetworkFound = false; | ||
| wifiState.scanSequenceFinished = false; | ||
| // Trigger a new scan | ||
| net_mgmt(NET_REQUEST_WIFI_SCAN, wifiState.sta_iface, nullptr, 0u); | ||
| // Wait for the scan to finish (this is a blocking call) | ||
| while (!wifiState.scanSequenceFinished) | ||
| ; | ||
| return wifiState.resultCount; | ||
| } | ||
| void WiFiClass::scanEventDispatcher(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, | ||
| struct net_if *iface) { | ||
| // Use the global Wi-Fi state instance to handle the event | ||
| if (wifiState.sta_iface != nullptr) { | ||
| WiFi.handleScanEvent(cb, mgmt_event, iface); | ||
| } | ||
| } | ||
| void WiFiClass::handleScanEvent(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, | ||
| struct net_if *iface) { | ||
| if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) { | ||
| const struct wifi_scan_result *entry = reinterpret_cast<const struct wifi_scan_result *>(cb->info); | ||
| if (wifiState.resultCount < MAX_SCAN_RESULTS) { | ||
| memcpy(&wifiState.scanResults[wifiState.resultCount], entry, sizeof(struct wifi_scan_result)); | ||
| wifiState.resultCount++; | ||
| // Compare SSID of the scanned network with the desired network SSID | ||
| if (!memcmp(entry->ssid, wifiState.sta_config.ssid, entry->ssid_length)) { | ||
| wifiState.sta_config.security = entry->security; | ||
| wifiState.sta_config.channel = entry->channel; | ||
| wifiState.sta_config.band = entry->band; | ||
| wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; | ||
| wifiState.soughtNetworkFound = true; | ||
| } | ||
| } | ||
| } | ||
| if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) { | ||
| wifiState.scanSequenceFinished = true; | ||
| if (wifiState.resultCount == 0) { | ||
| printk("No networks found.\n"); | ||
| } | ||
| } | ||
| } | ||
| char *WiFiClass::SSID() { | ||
| if (status() == WL_CONNECTED) { | ||
| return (char *)wifiState.sta_state.ssid; | ||
| } | ||
| return nullptr; | ||
| } | ||
| int32_t WiFiClass::RSSI() { | ||
| if (status() == WL_CONNECTED) { | ||
| returnwifiState.sta_state.rssi; | ||
| } | ||
| return 0; | ||
| } | ||
| String WiFiClass::firmwareVersion() { | ||
| #if defined(ARDUINO_PORTENTA_C33) | ||
| return "v1.5.0"; | ||
| #else | ||
| return "v0.0.0"; | ||
| #endif | ||
| } | ||
| wifi_security_type WiFiClass::convert_enc_type_to_security_type(wl_enc_type enc_type) { | ||
| switch (enc_type) { | ||
| case ENC_TYPE_WEP: | ||
| return WIFI_SECURITY_TYPE_WEP; | ||
| case ENC_TYPE_WPA: | ||
| return WIFI_SECURITY_TYPE_WPA_PSK; // Could also map to WPA_AUTO_PERSONAL | ||
| case ENC_TYPE_WPA2: | ||
| return WIFI_SECURITY_TYPE_PSK; // Could also map to WPA_AUTO_PERSONAL | ||
| case ENC_TYPE_WPA3: | ||
| return WIFI_SECURITY_TYPE_SAE; // Could also map to SAE_AUTO | ||
| case ENC_TYPE_NONE: | ||
| return WIFI_SECURITY_TYPE_NONE; | ||
| case ENC_TYPE_UNKNOWN: | ||
| case ENC_TYPE_AUTO: | ||
| return WIFI_SECURITY_TYPE_UNKNOWN; | ||
| default: | ||
| return WIFI_SECURITY_TYPE_UNKNOWN; // Default case for any undefined or unexpected values | ||
| } | ||
| } | ||
| // Global Wi-Fi object, uses the static wifiState struct | ||
| WiFiClass WiFi; |
59 changes: 31 additions & 28 deletionslibraries/WiFi/src/WiFi.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,47 @@ | ||
| #ifndef WIFI_H | ||
| #define WIFI_H | ||
| #include "SocketHelpers.h" | ||
| #include "utility/wl_definitions.h" | ||
| #include <zephyr/net/wifi_mgmt.h> | ||
| // Max number of scan results to store | ||
| #define MAX_SCAN_RESULTS 20 | ||
| // Wi-Fi event mask for the various events | ||
| #define NET_EVENT_WIFI_MASK \ | ||
| (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ | ||
| NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ | ||
| NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED | \ | ||
| NET_EVENT_WIFI_SCAN_RESULT) | ||
| class WiFiClass : public NetworkInterface { | ||
| public: | ||
| WiFiClass(); | ||
| ~WiFiClass(); | ||
| int begin(const char *ssid, const char *passphrase, | ||
| wl_enc_type security = ENC_TYPE_WPA, bool blocking = true); | ||
| bool beginAP(char *ssid, char *passphrase, int channel = WIFI_CHANNEL_ANY, | ||
| bool blocking = false); | ||
| int status(); | ||
| int8_t scanNetworks(); | ||
| char *SSID(); | ||
| int32_t RSSI(); | ||
| String firmwareVersion(); | ||
| wifi_security_type convert_enc_type_to_security_type(wl_enc_type enc_type); | ||
| static struct WiFiState wifiState; // Static instance to hold Wi-Fi state | ||
| private: | ||
| static void scanEventDispatcher(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, | ||
| struct net_if *iface); | ||
| void handleScanEvent(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, | ||
| struct net_if *iface); | ||
| }; | ||
| extern WiFiClass WiFi; // Global Wi-Fi object | ||
| #endif // WIFI_H |
1 change: 1 addition & 0 deletionsloader/llext_exports.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.