Skip to content

Commit 1df10e1

Browse files
bogdanarduinopennam
authored andcommitted
WiFI: Removed getters and setters from the library;
changed to static struct for handling data transfer from callback to object.
1 parent 905339e commit 1df10e1

File tree

2 files changed

+217
-205
lines changed

2 files changed

+217
-205
lines changed

libraries/WiFi/src/WiFi.cpp

Lines changed: 189 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,210 @@
11
#include "WiFi.h"
22

3-
WiFiClass WiFi;
4-
5-
WiFiClass *WiFiClass::instance = nullptr;
6-
7-
String WiFiClass::firmwareVersion() {
8-
#if defined(ARDUINO_PORTENTA_C33)
9-
return "v1.5.0";
10-
#else
11-
return "v0.0.0";
12-
#endif
13-
}
14-
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;
27-
} else {
28-
sta_config.security = WIFI_SECURITY_TYPE_PSK;
29-
}
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;
33-
34-
// Register the Wi-Fi event callback
35-
net_mgmt_init_event_callback(&wifiCb, scanEventDispatcher,
36-
NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
37-
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
43-
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,
51-
sizeof(struct wifi_connect_req_params));
52-
if (ret) {
53-
return false;
54-
}
55-
56-
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
57-
if (blocking) {
58-
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL,
59-
K_FOREVER);
60-
}
61-
}
62-
63-
return status();
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+
WiFiClass::~WiFiClass() {}
19+
20+
// Static instance of Wi-Fi state
21+
struct WiFiState WiFiClass::wifiState;
22+
23+
int WiFiClass::begin(const char *ssid, const char *passphrase,
24+
wl_enc_type security, bool blocking) {
25+
wifi_security_type wifi_security = convert_enc_type_to_security_type(security);
26+
27+
wifiState.sta_iface = net_if_get_wifi_sta();
28+
netif = wifiState.sta_iface;
29+
wifiState.sta_config.ssid = (const uint8_t *)ssid;
30+
wifiState.sta_config.ssid_length = strlen(ssid);
31+
wifiState.sta_config.psk = (const uint8_t *)passphrase;
32+
wifiState.sta_config.psk_length = strlen(passphrase);
33+
34+
// Set Wi-Fi security type if specified
35+
if (wifi_security != WIFI_SECURITY_TYPE_NONE) {
36+
wifiState.sta_config.security = wifi_security;
37+
} else {
38+
wifiState.sta_config.security = WIFI_SECURITY_TYPE_PSK;
39+
}
40+
41+
wifiState.sta_config.channel = WIFI_CHANNEL_ANY;
42+
wifiState.sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
43+
wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
44+
45+
// Register the Wi-Fi event callback
46+
net_mgmt_init_event_callback(&wifiState.wifiCb, scanEventDispatcher,
47+
NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
48+
net_mgmt_add_event_callback(&wifiState.wifiCb);
49+
50+
// Trigger a network scan
51+
(void)scanNetworks(); // Blocking call
52+
53+
// Attempt to connect to the network if configuration is valid
54+
if (wifiState.sta_config.ssid && wifiState.sta_config.psk) {
55+
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, wifiState.sta_iface, &wifiState.sta_config,
56+
sizeof(struct wifi_connect_req_params));
57+
if (ret) {
58+
return false;
59+
}
60+
61+
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
62+
if (blocking) {
63+
net_mgmt_event_wait_on_iface(wifiState.sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER);
64+
}
65+
}
66+
67+
return status();
6468
}
6569

6670
bool WiFiClass::beginAP(char *ssid, char *passphrase, int channel, bool blocking) {
67-
if (ap_iface != NULL) {
68-
return false;
69-
}
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,
81-
sizeof(struct wifi_connect_req_params));
82-
if (ret) {
83-
return false;
84-
}
85-
enable_dhcpv4_server(ap_iface);
86-
if (blocking) {
87-
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL,
88-
K_FOREVER);
89-
}
90-
return true;
71+
if (wifiState.ap_iface != nullptr) {
72+
return false; // AP already initialized
73+
}
74+
75+
wifiState.ap_iface = net_if_get_wifi_sap();
76+
netif = wifiState.ap_iface;
77+
wifiState.ap_config.ssid = (const uint8_t *)ssid;
78+
wifiState.ap_config.ssid_length = strlen(ssid);
79+
wifiState.ap_config.psk = (const uint8_t *)passphrase;
80+
wifiState.ap_config.psk_length = strlen(passphrase);
81+
wifiState.ap_config.security = WIFI_SECURITY_TYPE_PSK;
82+
wifiState.ap_config.channel = channel;
83+
wifiState.ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
84+
wifiState.ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
85+
86+
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, wifiState.ap_iface, &wifiState.ap_config,
87+
sizeof(struct wifi_connect_req_params));
88+
if (ret) {
89+
return false;
90+
}
91+
92+
enable_dhcpv4_server(wifiState.ap_iface);
93+
94+
if (blocking) {
95+
net_mgmt_event_wait_on_iface(wifiState.ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER);
96+
}
97+
98+
return true;
9199
}
92100

93101
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,
97-
sizeof(struct wifi_iface_status))) {
98-
return WL_NO_SHIELD;
99-
}
100-
if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
101-
return WL_CONNECTED;
102-
} else {
103-
return WL_DISCONNECTED;
104-
}
105-
return WL_NO_SHIELD;
102+
wifiState.sta_iface = net_if_get_wifi_sta();
103+
netif = wifiState.sta_iface;
104+
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &wifiState.sta_state,
105+
sizeof(struct wifi_iface_status))) {
106+
return WL_NO_SHIELD;
107+
}
108+
if (wifiState.sta_state.state >= WIFI_STATE_ASSOCIATED) {
109+
return WL_CONNECTED;
110+
} else {
111+
return WL_DISCONNECTED;
112+
}
106113
}
107114

108115
int8_t WiFiClass::scanNetworks() {
109-
resultCount = 0u;
110-
setScanSequenceFinished(false);
111-
setSoughtNetworkFound(false);
116+
wifiState.resultCount = 0u;
117+
wifiState.soughtNetworkFound = false;
118+
wifiState.scanSequenceFinished = false;
112119

113-
// Trigger a new scan
114-
net_mgmt(NET_REQUEST_WIFI_SCAN, sta_iface, nullptr, 0u);
120+
// Trigger a new scan
121+
net_mgmt(NET_REQUEST_WIFI_SCAN, wifiState.sta_iface, nullptr, 0u);
115122

116-
// Wait for the scan to finish. This is by design a blocking call
117-
while (getScanSequenceFinished() != true)
118-
;
123+
// Wait for the scan to finish (this is a blocking call)
124+
while (!wifiState.scanSequenceFinished)
125+
;
119126

120-
return resultCount;
127+
return wifiState.resultCount;
128+
}
129+
130+
void WiFiClass::scanEventDispatcher(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
131+
struct net_if *iface) {
132+
// Use the global Wi-Fi state instance to handle the event
133+
if (wifiState.sta_iface != nullptr) {
134+
WiFi.handleScanEvent(cb, mgmt_event, iface);
135+
}
136+
}
137+
138+
void WiFiClass::handleScanEvent(struct net_mgmt_event_callback *cb, uint64_t mgmt_event,
139+
struct net_if *iface) {
140+
if (mgmt_event == NET_EVENT_WIFI_SCAN_RESULT) {
141+
const struct wifi_scan_result *entry = reinterpret_cast<const struct wifi_scan_result *>(cb->info);
142+
if (wifiState.resultCount < MAX_SCAN_RESULTS) {
143+
memcpy(&wifiState.scanResults[wifiState.resultCount], entry, sizeof(struct wifi_scan_result));
144+
wifiState.resultCount++;
145+
146+
// Compare SSID of the scanned network with the desired network SSID
147+
if (!memcmp(entry->ssid, wifiState.sta_config.ssid, entry->ssid_length)) {
148+
wifiState.sta_config.security = entry->security;
149+
wifiState.sta_config.channel = entry->channel;
150+
wifiState.sta_config.band = entry->band;
151+
wifiState.sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
152+
153+
wifiState.soughtNetworkFound = true;
154+
}
155+
}
156+
}
157+
158+
if (mgmt_event == NET_EVENT_WIFI_SCAN_DONE) {
159+
wifiState.scanSequenceFinished = true;
160+
161+
if (wifiState.resultCount == 0) {
162+
printk("No networks found.\n");
163+
}
164+
}
121165
}
122166

123167
char *WiFiClass::SSID() {
124-
if (status() == WL_CONNECTED) {
125-
return (char *)sta_state.ssid;
126-
}
127-
return nullptr;
168+
if (status() == WL_CONNECTED) {
169+
return (char *)wifiState.sta_state.ssid;
170+
}
171+
return nullptr;
128172
}
129173

130174
int32_t WiFiClass::RSSI() {
131-
if (status() == WL_CONNECTED) {
132-
return sta_state.rssi;
133-
}
134-
return 0;
175+
if (status() == WL_CONNECTED) {
176+
return wifiState.sta_state.rssi;
177+
}
178+
return 0;
135179
}
180+
181+
String WiFiClass::firmwareVersion() {
182+
#if defined(ARDUINO_PORTENTA_C33)
183+
return "v1.5.0";
184+
#else
185+
return "v0.0.0";
186+
#endif
187+
}
188+
189+
wifi_security_type WiFiClass::convert_enc_type_to_security_type(wl_enc_type enc_type) {
190+
switch (enc_type) {
191+
case ENC_TYPE_WEP:
192+
return WIFI_SECURITY_TYPE_WEP;
193+
case ENC_TYPE_WPA:
194+
return WIFI_SECURITY_TYPE_WPA_PSK; // Could also map to WPA_AUTO_PERSONAL
195+
case ENC_TYPE_WPA2:
196+
return WIFI_SECURITY_TYPE_PSK; // Could also map to WPA_AUTO_PERSONAL
197+
case ENC_TYPE_WPA3:
198+
return WIFI_SECURITY_TYPE_SAE; // Could also map to SAE_AUTO
199+
case ENC_TYPE_NONE:
200+
return WIFI_SECURITY_TYPE_NONE;
201+
case ENC_TYPE_UNKNOWN:
202+
case ENC_TYPE_AUTO:
203+
return WIFI_SECURITY_TYPE_UNKNOWN;
204+
default:
205+
return WIFI_SECURITY_TYPE_UNKNOWN; // Default case for any undefined or unexpected values
206+
}
207+
}
208+
209+
// Global Wi-Fi object, uses the static wifiState struct
210+
WiFiClass WiFi;

0 commit comments

Comments
 (0)