16
16
17
17
#define DHCP_OPTION_NTP (42 )
18
18
19
+ #undef LOG_INF
19
20
#define LOG_INF (...)
20
21
21
22
#ifdef SPECIALIZE_FOR_ETHERNET
@@ -31,6 +32,11 @@ enum EthernetHardwareStatus {
31
32
};
32
33
#endif
33
34
35
+ #ifdef SPECIALIZE_FOR_WIFI
36
+ #include " utility/wl_definitions.h"
37
+ #include < zephyr/net/wifi_mgmt.h>
38
+ #endif
39
+
34
40
class NetworkInterface {
35
41
private:
36
42
int iface_index = -1 ;
@@ -49,8 +55,6 @@ class NetworkInterface {
49
55
return ;
50
56
}
51
57
52
- // printk("Interface %p has IP\n", iface);
53
-
54
58
for (i = 0 ; i < NET_IF_MAX_IPV4_ADDR; i++) {
55
59
char buf[NET_IPV4_ADDR_LEN];
56
60
@@ -124,16 +128,18 @@ class NetworkInterface {
124
128
125
129
void setMACAddress (const uint8_t * mac);
126
130
127
- bool begin () {
131
+ bool begin (bool blocking = true , uint32_t additional_event_mask = 0 ) {
128
132
dhcp ();
129
- net_mgmt_event_wait_on_iface (net_if_get_by_index (iface_index), NET_EVENT_IPV4_ADDR_ADD, NULL , NULL , NULL , K_FOREVER); return 0 ;
133
+ int ret = net_mgmt_event_wait_on_iface (net_if_get_by_index (iface_index), NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
134
+ NULL , NULL , NULL , blocking ? K_FOREVER : K_SECONDS (1 ));
135
+ return (ret == 0 );
130
136
}
131
137
132
- // Manual functions
138
+ // TODO: manual functions for setting IP address, subnet mask, gateway, etc.
133
139
// net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
134
140
// net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
135
141
136
- #ifdef SPECIALIZE_FOR_ETHERNET
142
+ #if defined( SPECIALIZE_FOR_ETHERNET) && DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
137
143
EthernetLinkStatus linkStatus () {
138
144
if (net_if_is_up (net_if_get_by_index (iface_index))) {
139
145
return LinkON;
@@ -151,4 +157,66 @@ class NetworkInterface {
151
157
}
152
158
}
153
159
#endif
160
+
161
+ #ifdef SPECIALIZE_FOR_WIFI
162
+
163
+ #define NET_EVENT_WIFI_MASK \
164
+ (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
165
+ NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
166
+ NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED | \
167
+ NET_EVENT_WIFI_SCAN_RESULT)
168
+
169
+ struct net_if *sta_iface;
170
+ struct net_if *ap_iface;
171
+
172
+ struct wifi_connect_req_params ap_config;
173
+ struct wifi_connect_req_params sta_config;
174
+
175
+ bool begin (const char * ssid, const char * passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = false ) {
176
+ sta_iface = net_if_get_wifi_sta ();
177
+
178
+ sta_config.ssid = (const uint8_t *)ssid;
179
+ sta_config.ssid_length = strlen (ssid);
180
+ sta_config.psk = (const uint8_t *)passphrase;
181
+ sta_config.psk_length = strlen (passphrase);
182
+ // TODO: change these fields with scan() results
183
+ sta_config.security = WIFI_SECURITY_TYPE_PSK;
184
+ sta_config.channel = WIFI_CHANNEL_ANY;
185
+ sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
186
+ sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
187
+
188
+ int ret = net_mgmt (NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
189
+ sizeof (struct wifi_connect_req_params ));
190
+ if (ret) {
191
+ return false ;
192
+ }
193
+
194
+ begin (false , NET_EVENT_WIFI_MASK);
195
+ if (blocking) {
196
+ net_mgmt_event_wait_on_iface (sta_iface, NET_EVENT_WIFI_AP_STA_CONNECTED, NULL , NULL , NULL , K_FOREVER);
197
+ }
198
+
199
+ return true ;
200
+ }
201
+
202
+ int status () {
203
+ struct wifi_iface_status status = { 0 };
204
+
205
+ if (net_mgmt (NET_REQUEST_WIFI_IFACE_STATUS, net_if_get_by_index (iface_index), &status,
206
+ sizeof (struct wifi_iface_status ))) {
207
+ return WL_NO_SHIELD;
208
+ }
209
+
210
+ if (status.state >= WIFI_STATE_ASSOCIATED) {
211
+ return WL_CONNECTED;
212
+ } else {
213
+ return WL_DISCONNECTED;
214
+ }
215
+ return WL_NO_SHIELD;
216
+ }
217
+
218
+ int8_t scanNetworks () {
219
+ // TODO: borrow code from mbed core for scan results handling
220
+ }
221
+ #endif
154
222
};
0 commit comments