|
1 | 1 | #include "WiFi.h" |
2 | 2 |
|
3 | | -WiFiClass WiFi; |
4 | | - |
5 | | -WiFiClass *WiFiClass::instance = nullptr; |
| 3 | +// Static Wi-Fi state instance |
| 4 | +struct WiFiState { |
| 5 | + struct net_if *sta_iface = nullptr; |
| 6 | + struct net_if *ap_iface = nullptr; |
| 7 | + struct wifi_connect_req_params ap_config; |
| 8 | + struct wifi_connect_req_params sta_config; |
| 9 | + struct wifi_iface_status sta_state = {0}; |
| 10 | + struct wifi_scan_result scanResults[MAX_SCAN_RESULTS]; |
| 11 | + uint8_t resultCount = 0; |
| 12 | + struct net_mgmt_event_callback wifiCb; |
| 13 | + bool soughtNetworkFound = false; |
| 14 | + bool scanSequenceFinished = false; |
| 15 | +}; |
| 16 | + |
| 17 | +WiFiClass::WiFiClass() { |
| 18 | +} |
6 | 19 |
|
7 | | -String WiFiClass::firmwareVersion() { |
8 | | -#if defined(ARDUINO_PORTENTA_C33) |
9 | | - return "v1.5.0"; |
10 | | -#else |
11 | | - return "v0.0.0"; |
12 | | -#endif |
| 20 | +WiFiClass::~WiFiClass() { |
13 | 21 | } |
14 | 22 |
|
15 | | -int begin(const char *ssid, const char *passphrase, |
16 | | - wifi_security_type security = WIFI_SECURITY_TYPE_NONE, bool blocking = true) { |
17 | | - sta_iface = net_if_get_wifi_sta(); |
18 | | - netif = sta_iface; |
19 | | - sta_config.ssid = (const uint8_t *)ssid; |
20 | | - sta_config.ssid_length = strlen(ssid); |
21 | | - sta_config.psk = (const uint8_t *)passphrase; |
22 | | - sta_config.psk_length = strlen(passphrase); |
23 | | - |
24 | | - // The user might provide the security type as well |
25 | | - if (security != WIFI_SECURITY_TYPE_NONE) { |
26 | | - sta_config.security = security; |
| 23 | +// Static instance of Wi-Fi state |
| 24 | +struct WiFiState WiFiClass::wifiState; |
| 25 | + |
| 26 | +int WiFiClass::begin(const char *ssid, const char *passphrase, wl_enc_type security, |
| 27 | + bool blocking) { |
| 28 | + wifi_security_type wifi_security = convert_enc_type_to_security_type(security); |
| 29 | + |
| 30 | + wifiState.sta_iface = net_if_get_wifi_sta(); |
| 31 | + netif = wifiState.sta_iface; |
| 32 | + wifiState.sta_config.ssid = (const uint8_t *)ssid; |
| 33 | + wifiState.sta_config.ssid_length = strlen(ssid); |
| 34 | + wifiState.sta_config.psk = (const uint8_t *)passphrase; |
| 35 | + wifiState.sta_config.psk_length = strlen(passphrase); |
| 36 | + |
| 37 | + // Set Wi-Fi security type if specified |
| 38 | + if (wifi_security != WIFI_SECURITY_TYPE_NONE) { |
| 39 | + wifiState.sta_config.security = wifi_security; |
27 | 40 | } else { |
28 | | - sta_config.security = WIFI_SECURITY_TYPE_PSK; |
| 41 | + wifiState.sta_config.security = WIFI_SECURITY_TYPE_PSK; |
29 | 42 | } |
30 | | - sta_config.channel = WIFI_CHANNEL_ANY; |
31 | | - sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; |
32 | | - sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; |
| 43 | + |
| 44 | + wifiState.sta_config.channel = WIFI_CHANNEL_ANY; |
| 45 | + wifiState.sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; |
| 46 | + wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; |
33 | 47 |
|
34 | 48 | // Register the Wi-Fi event callback |
35 | | - net_mgmt_init_event_callback(&wifiCb, scanEventDispatcher, |
| 49 | + net_mgmt_init_event_callback(&wifiState.wifiCb, scanEventDispatcher, |
36 | 50 | NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE); |
| 51 | + net_mgmt_add_event_callback(&wifiState.wifiCb); |
37 | 52 |
|
38 | | - net_mgmt_add_event_callback(&wifiCb); |
39 | | - |
40 | | - // If the network we are scanning for is found, the connection parameters will be updated |
41 | | - // automatically; |
42 | | - (void)scanNetworks(); // This is a blocking function call |
| 53 | + // Trigger a network scan |
| 54 | + (void)scanNetworks(); // Blocking call |
43 | 55 |
|
44 | | - // Attempt to connect with either default parameters, or the updated ones after the scan |
45 | | - // completed |
46 | | - if ((sta_config.ssid != NULL) && (sta_config.ssid_length != 0u) && (sta_config.psk != NULL) && |
47 | | - (sta_config.psk_length != 0u)) |
48 | | - |
49 | | - { |
50 | | - int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, |
| 56 | + // Attempt to connect to the network if configuration is valid |
| 57 | + if (wifiState.sta_config.ssid && wifiState.sta_config.psk) { |
| 58 | + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, wifiState.sta_iface, &wifiState.sta_config, |
51 | 59 | sizeof(struct wifi_connect_req_params)); |
52 | 60 | if (ret) { |
53 | 61 | return false; |
54 | 62 | } |
55 | 63 |
|
56 | 64 | NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); |
57 | 65 | if (blocking) { |
58 | | - net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, |
59 | | - K_FOREVER); |
| 66 | + net_mgmt_event_wait_on_iface(wifiState.sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, |
| 67 | + NULL, NULL, K_FOREVER); |
60 | 68 | } |
61 | 69 | } |
62 | 70 |
|
63 | 71 | return status(); |
64 | 72 | } |
65 | 73 |
|
66 | 74 | bool WiFiClass::beginAP(char *ssid, char *passphrase, int channel, bool blocking) { |
67 | | - if (ap_iface != NULL) { |
68 | | - return false; |
| 75 | + if (wifiState.ap_iface != nullptr) { |
| 76 | + return false; // AP already initialized |
69 | 77 | } |
70 | | - ap_iface = net_if_get_wifi_sap(); |
71 | | - netif = ap_iface; |
72 | | - ap_config.ssid = (const uint8_t *)ssid; |
73 | | - ap_config.ssid_length = strlen(ssid); |
74 | | - ap_config.psk = (const uint8_t *)passphrase; |
75 | | - ap_config.psk_length = strlen(passphrase); |
76 | | - ap_config.security = WIFI_SECURITY_TYPE_PSK; |
77 | | - ap_config.channel = channel; |
78 | | - ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; |
79 | | - ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; |
80 | | - int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, |
| 78 | + |
| 79 | + wifiState.ap_iface = net_if_get_wifi_sap(); |
| 80 | + netif = wifiState.ap_iface; |
| 81 | + wifiState.ap_config.ssid = (const uint8_t *)ssid; |
| 82 | + wifiState.ap_config.ssid_length = strlen(ssid); |
| 83 | + wifiState.ap_config.psk = (const uint8_t *)passphrase; |
| 84 | + wifiState.ap_config.psk_length = strlen(passphrase); |
| 85 | + wifiState.ap_config.security = WIFI_SECURITY_TYPE_PSK; |
| 86 | + wifiState.ap_config.channel = channel; |
| 87 | + wifiState.ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; |
| 88 | + wifiState.ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; |
| 89 | + |
| 90 | + int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, wifiState.ap_iface, &wifiState.ap_config, |
81 | 91 | sizeof(struct wifi_connect_req_params)); |
82 | 92 | if (ret) { |
83 | 93 | return false; |
84 | 94 | } |
85 | | - enable_dhcpv4_server(ap_iface); |
| 95 | + |
| 96 | + enable_dhcpv4_server(wifiState.ap_iface); |
| 97 | + |
86 | 98 | if (blocking) { |
87 | | - net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, |
88 | | - K_FOREVER); |
| 99 | + net_mgmt_event_wait_on_iface(wifiState.ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, |
| 100 | + NULL, NULL, K_FOREVER); |
89 | 101 | } |
| 102 | + |
90 | 103 | return true; |
91 | 104 | } |
92 | 105 |
|
93 | 106 | int WiFiClass::status() { |
94 | | - sta_iface = net_if_get_wifi_sta(); |
95 | | - netif = sta_iface; |
96 | | - if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, |
| 107 | + wifiState.sta_iface = net_if_get_wifi_sta(); |
| 108 | + netif = wifiState.sta_iface; |
| 109 | + if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &wifiState.sta_state, |
97 | 110 | sizeof(struct wifi_iface_status))) { |
98 | 111 | return WL_NO_SHIELD; |
99 | 112 | } |
100 | | - if (sta_state.state >= WIFI_STATE_ASSOCIATED) { |
| 113 | + if (wifiState.sta_state.state >= WIFI_STATE_ASSOCIATED) { |
101 | 114 | return WL_CONNECTED; |
102 | 115 | } else { |
103 | 116 | return WL_DISCONNECTED; |
104 | 117 | } |
105 | | - return WL_NO_SHIELD; |
106 | 118 | } |
107 | 119 |
|
108 | 120 | int8_t WiFiClass::scanNetworks() { |
109 | | - resultCount = 0u; |
110 | | - setScanSequenceFinished(false); |
111 | | - setSoughtNetworkFound(false); |
| 121 | + wifiState.resultCount = 0u; |
| 122 | + wifiState.soughtNetworkFound = false; |
| 123 | + wifiState.scanSequenceFinished = false; |
112 | 124 |
|
113 | 125 | // Trigger a new scan |
114 | | - net_mgmt(NET_REQUEST_WIFI_SCAN, sta_iface, nullptr, 0u); |
| 126 | + net_mgmt(NET_REQUEST_WIFI_SCAN, wifiState.sta_iface, nullptr, 0u); |
115 | 127 |
|
116 | | - // Wait for the scan to finish. This is by design a blocking call |
117 | | - while (getScanSequenceFinished() != true) |
| 128 | + // Wait for the scan to finish (this is a blocking call) |
| 129 | + while (!wifiState.scanSequenceFinished) |
118 | 130 | ; |
119 | 131 |
|
120 | | - return resultCount; |
| 132 | + return wifiState.resultCount; |
| 133 | +} |
| 134 | + |
| 135 | +void WiFiClass::scanEventDispatcher(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, |
| 136 | + struct net_if *iface) { |
| 137 | + // Use the global Wi-Fi state instance to handle the event |
| 138 | + if (wifiState.sta_iface != nullptr) { |
| 139 | + WiFi.handleScanEvent(cb, mgmt_event, iface); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +void WiFiClass::handleScanEvent(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, |
| 144 | + struct net_if *iface) { |
| 145 | + if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) { |
| 146 | + const struct wifi_scan_result *entry = |
| 147 | + reinterpret_cast<const struct wifi_scan_result *>(cb->info); |
| 148 | + if (wifiState.resultCount < MAX_SCAN_RESULTS) { |
| 149 | + memcpy(&wifiState.scanResults[wifiState.resultCount], entry, |
| 150 | + sizeof(struct wifi_scan_result)); |
| 151 | + wifiState.resultCount++; |
| 152 | + |
| 153 | + // Compare SSID of the scanned network with the desired network SSID |
| 154 | + if (!memcmp(entry->ssid, wifiState.sta_config.ssid, entry->ssid_length)) { |
| 155 | + wifiState.sta_config.security = entry->security; |
| 156 | + wifiState.sta_config.channel = entry->channel; |
| 157 | + wifiState.sta_config.band = entry->band; |
| 158 | + wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ; |
| 159 | + |
| 160 | + wifiState.soughtNetworkFound = true; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) { |
| 166 | + wifiState.scanSequenceFinished = true; |
| 167 | + |
| 168 | + if (wifiState.resultCount == 0) { |
| 169 | + printk("No networks found.\n"); |
| 170 | + } |
| 171 | + } |
121 | 172 | } |
122 | 173 |
|
123 | 174 | char *WiFiClass::SSID() { |
124 | 175 | if (status() == WL_CONNECTED) { |
125 | | - return (char *)sta_state.ssid; |
| 176 | + return (char *)wifiState.sta_state.ssid; |
126 | 177 | } |
127 | 178 | return nullptr; |
128 | 179 | } |
129 | 180 |
|
130 | 181 | int32_t WiFiClass::RSSI() { |
131 | 182 | if (status() == WL_CONNECTED) { |
132 | | - return sta_state.rssi; |
| 183 | + return wifiState.sta_state.rssi; |
133 | 184 | } |
134 | 185 | return 0; |
135 | 186 | } |
| 187 | + |
| 188 | +String WiFiClass::firmwareVersion() { |
| 189 | +#if defined(ARDUINO_PORTENTA_C33) |
| 190 | + return "v1.5.0"; |
| 191 | +#else |
| 192 | + return "v0.0.0"; |
| 193 | +#endif |
| 194 | +} |
| 195 | + |
| 196 | +wifi_security_type WiFiClass::convert_enc_type_to_security_type(wl_enc_type enc_type) { |
| 197 | + switch (enc_type) { |
| 198 | + case ENC_TYPE_WEP: |
| 199 | + return WIFI_SECURITY_TYPE_WEP; |
| 200 | + case ENC_TYPE_WPA: |
| 201 | + return WIFI_SECURITY_TYPE_WPA_PSK; // Could also map to WPA_AUTO_PERSONAL |
| 202 | + case ENC_TYPE_WPA2: |
| 203 | + return WIFI_SECURITY_TYPE_PSK; // Could also map to WPA_AUTO_PERSONAL |
| 204 | + case ENC_TYPE_WPA3: |
| 205 | + return WIFI_SECURITY_TYPE_SAE; // Could also map to SAE_AUTO |
| 206 | + case ENC_TYPE_NONE: |
| 207 | + return WIFI_SECURITY_TYPE_NONE; |
| 208 | + case ENC_TYPE_UNKNOWN: |
| 209 | + case ENC_TYPE_AUTO: |
| 210 | + return WIFI_SECURITY_TYPE_UNKNOWN; |
| 211 | + default: |
| 212 | + return WIFI_SECURITY_TYPE_UNKNOWN; // Default case for any undefined or unexpected values |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +// Global Wi-Fi object, uses the static wifiState struct |
| 217 | +WiFiClass WiFi; |
0 commit comments