1616
1717#define DHCP_OPTION_NTP (42 )
1818
19+ #undef LOG_INF
1920#define LOG_INF (...)
2021
2122#ifdef SPECIALIZE_FOR_ETHERNET
@@ -31,6 +32,11 @@ enum EthernetHardwareStatus {
3132};
3233#endif
3334
35+ #ifdef SPECIALIZE_FOR_WIFI
36+ #include " utility/wl_definitions.h"
37+ #include < zephyr/net/wifi_mgmt.h>
38+ #endif
39+
3440class NetworkInterface {
3541private:
3642 int iface_index = -1 ;
@@ -49,8 +55,6 @@ class NetworkInterface {
4955 return ;
5056 }
5157
52- // printk("Interface %p has IP\n", iface);
53-
5458 for (i = 0 ; i < NET_IF_MAX_IPV4_ADDR; i++) {
5559 char buf[NET_IPV4_ADDR_LEN];
5660
@@ -124,16 +128,18 @@ class NetworkInterface {
124128
125129 void setMACAddress (const uint8_t * mac);
126130
127- bool begin () {
131+ bool begin (bool blocking = true , uint32_t additional_event_mask = 0 ) {
128132 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 );
130136 }
131137
132- // Manual functions
138+ // TODO: manual functions for setting IP address, subnet mask, gateway, etc.
133139 // net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
134140 // net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
135141
136- #ifdef SPECIALIZE_FOR_ETHERNET
142+ #if defined( SPECIALIZE_FOR_ETHERNET) && DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
137143 EthernetLinkStatus linkStatus () {
138144 if (net_if_is_up (net_if_get_by_index (iface_index))) {
139145 return LinkON;
@@ -151,4 +157,66 @@ class NetworkInterface {
151157 }
152158 }
153159#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
154222};
0 commit comments