Skip to content

Commit f26e6a9

Browse files
committed
initial: add WiFi APIs
1 parent 1d680ef commit f26e6a9

File tree

5 files changed

+164
-6
lines changed

5 files changed

+164
-6
lines changed

libraries/SocketWrapper/SocketHelpers.h

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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+
3440
class NetworkInterface {
3541
private:
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
};

libraries/SocketWrapper/WiFi.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "WiFi.h"
2+
3+
NetworkInterface WiFi(1);

libraries/SocketWrapper/WiFi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define SPECIALIZE_FOR_WIFI
2+
#include "SocketHelpers.h"
3+
4+
extern NetworkInterface WiFi;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
wl_definitions.h - Library for Arduino Wifi shield.
3+
Copyright (c) 2011-2014 Arduino. All right reserved.
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
This library is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
Lesser General Public License for more details.
12+
You should have received a copy of the GNU Lesser General Public
13+
License along with this library; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
*/
16+
/*
17+
* wl_definitions.h
18+
*
19+
* Created on: Mar 6, 2011
20+
* Author: dlafauci
21+
*/
22+
23+
#ifndef WL_DEFINITIONS_H_
24+
#define WL_DEFINITIONS_H_
25+
26+
// Maximum size of a SSID
27+
#define WL_SSID_MAX_LENGTH 32
28+
// Length of passphrase. Valid lengths are 8-63.
29+
#define WL_WPA_KEY_MAX_LENGTH 63
30+
// Length of key in bytes. Valid values are 5 and 13.
31+
#define WL_WEP_KEY_MAX_LENGTH 13
32+
// Size of a MAC-address or BSSID
33+
#define WL_MAC_ADDR_LENGTH 6
34+
// Size of a MAC-address or BSSID
35+
#define WL_IPV4_LENGTH 4
36+
// Maximum size of a SSID list
37+
#define WL_NETWORKS_LIST_MAXNUM 10
38+
// Maxmium number of socket
39+
#define MAX_SOCK_NUM 4
40+
// Socket not available constant
41+
#define SOCK_NOT_AVAIL 255
42+
// Default state value for Wifi state field
43+
#define NA_STATE -1
44+
//Maximum number of attempts to establish wifi connection
45+
#define WL_MAX_ATTEMPT_CONNECTION 10
46+
47+
typedef enum {
48+
WL_NO_SHIELD = 255,
49+
WL_NO_MODULE = 255,
50+
WL_IDLE_STATUS = 0,
51+
WL_NO_SSID_AVAIL,
52+
WL_SCAN_COMPLETED,
53+
WL_CONNECTED,
54+
WL_CONNECT_FAILED,
55+
WL_CONNECTION_LOST,
56+
WL_DISCONNECTED,
57+
WL_AP_LISTENING,
58+
WL_AP_CONNECTED,
59+
WL_AP_FAILED
60+
} wl_status_t;
61+
62+
/* Encryption modes */
63+
enum wl_enc_type { /* Values map to 802.11 Cipher Algorithm Identifier */
64+
ENC_TYPE_WEP = 5,
65+
ENC_TYPE_TKIP = 2,
66+
ENC_TYPE_WPA = ENC_TYPE_TKIP,
67+
ENC_TYPE_CCMP = 4,
68+
ENC_TYPE_WPA2 = ENC_TYPE_CCMP,
69+
ENC_TYPE_GCMP = 6,
70+
ENC_TYPE_WPA3 = ENC_TYPE_GCMP,
71+
/* ... except these two, 7 and 8 are reserved in 802.11-2007 */
72+
ENC_TYPE_NONE = 7,
73+
ENC_TYPE_UNKNOWN = 9,
74+
ENC_TYPE_AUTO = 8
75+
};
76+
77+
#endif /* WL_DEFINITIONS_H_ */

loader/llext_exports.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ FORCE_EXPORT_SYM(net_dhcpv4_add_option_callback);
6464
#endif
6565

6666
#if defined(CONFIG_NET_MGMT_EVENT)
67+
FORCE_EXPORT_SYM(net_mgmt_NET_REQUEST_WIFI_CONNECT);
68+
FORCE_EXPORT_SYM(net_mgmt_NET_REQUEST_WIFI_IFACE_STATUS);
6769
FORCE_EXPORT_SYM(net_mgmt_add_event_callback);
6870
FORCE_EXPORT_SYM(net_mgmt_event_wait_on_iface);
6971
#endif
7072

73+
#if defined(CONFIG_WIFI)
74+
FORCE_EXPORT_SYM(net_if_get_wifi_sta);
75+
#endif
76+
7177
#if defined(CONFIG_BT)
7278
FORCE_EXPORT_SYM(bt_enable_raw);
7379
FORCE_EXPORT_SYM(bt_hci_raw_set_mode);

0 commit comments

Comments
 (0)