Skip to content

Commit 8fb793c

Browse files
committed
WiFi: move function implementation into cpp file
1 parent ff09b2d commit 8fb793c

File tree

2 files changed

+92
-90
lines changed

2 files changed

+92
-90
lines changed

libraries/WiFi/src/WiFi.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,89 @@ String WiFiClass::firmwareVersion() {
99
return "v0.0.0";
1010
#endif
1111
}
12+
13+
int WiFiClass::begin(const char *ssid, const char *passphrase, wl_enc_type security,
14+
bool blocking) {
15+
sta_iface = net_if_get_wifi_sta();
16+
netif = sta_iface;
17+
sta_config.ssid = (const uint8_t *)ssid;
18+
sta_config.ssid_length = strlen(ssid);
19+
sta_config.psk = (const uint8_t *)passphrase;
20+
sta_config.psk_length = strlen(passphrase);
21+
// TODO: change these fields with scan() results
22+
sta_config.security = WIFI_SECURITY_TYPE_PSK;
23+
sta_config.channel = WIFI_CHANNEL_ANY;
24+
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
25+
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
26+
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
27+
sizeof(struct wifi_connect_req_params));
28+
if (ret) {
29+
return false;
30+
}
31+
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
32+
if (blocking) {
33+
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL,
34+
K_FOREVER);
35+
}
36+
return status();
37+
}
38+
39+
bool WiFiClass::beginAP(char *ssid, char *passphrase, int channel, bool blocking) {
40+
if (ap_iface != NULL) {
41+
return false;
42+
}
43+
ap_iface = net_if_get_wifi_sap();
44+
netif = ap_iface;
45+
ap_config.ssid = (const uint8_t *)ssid;
46+
ap_config.ssid_length = strlen(ssid);
47+
ap_config.psk = (const uint8_t *)passphrase;
48+
ap_config.psk_length = strlen(passphrase);
49+
ap_config.security = WIFI_SECURITY_TYPE_PSK;
50+
ap_config.channel = channel;
51+
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
52+
ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
53+
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
54+
sizeof(struct wifi_connect_req_params));
55+
if (ret) {
56+
return false;
57+
}
58+
enable_dhcpv4_server(ap_iface);
59+
if (blocking) {
60+
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL,
61+
K_FOREVER);
62+
}
63+
return true;
64+
}
65+
66+
int WiFiClass::status() {
67+
sta_iface = net_if_get_wifi_sta();
68+
netif = sta_iface;
69+
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state,
70+
sizeof(struct wifi_iface_status))) {
71+
return WL_NO_SHIELD;
72+
}
73+
if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
74+
return WL_CONNECTED;
75+
} else {
76+
return WL_DISCONNECTED;
77+
}
78+
return WL_NO_SHIELD;
79+
}
80+
81+
int8_t WiFiClass::scanNetworks() {
82+
// TODO: borrow code from mbed core for scan results handling
83+
}
84+
85+
char *WiFiClass::SSID() {
86+
if (status() == WL_CONNECTED) {
87+
return (char *)sta_state.ssid;
88+
}
89+
return nullptr;
90+
}
91+
92+
int32_t WiFiClass::RSSI() {
93+
if (status() == WL_CONNECTED) {
94+
return sta_state.rssi;
95+
}
96+
return 0;
97+
}

libraries/WiFi/src/WiFi.h

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,100 +18,16 @@ class WiFiClass : public NetworkInterface {
1818
}
1919

2020
int begin(const char *ssid, const char *passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN,
21-
bool blocking = true) {
22-
sta_iface = net_if_get_wifi_sta();
23-
netif = sta_iface;
24-
sta_config.ssid = (const uint8_t *)ssid;
25-
sta_config.ssid_length = strlen(ssid);
26-
sta_config.psk = (const uint8_t *)passphrase;
27-
sta_config.psk_length = strlen(passphrase);
28-
// TODO: change these fields with scan() results
29-
sta_config.security = WIFI_SECURITY_TYPE_PSK;
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-
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
35-
sizeof(struct wifi_connect_req_params));
36-
if (ret) {
37-
return false;
38-
}
39-
40-
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
41-
if (blocking) {
42-
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL,
43-
K_FOREVER);
44-
}
45-
46-
return status();
47-
}
48-
21+
bool blocking = true);
4922
bool beginAP(char *ssid, char *passphrase, int channel = WIFI_CHANNEL_ANY,
50-
bool blocking = false) {
51-
if (ap_iface != NULL) {
52-
return false;
53-
}
54-
ap_iface = net_if_get_wifi_sap();
55-
netif = ap_iface;
56-
ap_config.ssid = (const uint8_t *)ssid;
57-
ap_config.ssid_length = strlen(ssid);
58-
ap_config.psk = (const uint8_t *)passphrase;
59-
ap_config.psk_length = strlen(passphrase);
60-
ap_config.security = WIFI_SECURITY_TYPE_PSK;
61-
ap_config.channel = channel;
62-
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
63-
ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
23+
bool blocking = false);
6424

65-
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
66-
sizeof(struct wifi_connect_req_params));
25+
int status();
6726

68-
if (ret) {
69-
return false;
70-
}
71-
72-
enable_dhcpv4_server(ap_iface);
73-
74-
if (blocking) {
75-
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL,
76-
NULL, K_FOREVER);
77-
}
78-
79-
return true;
80-
}
27+
int8_t scanNetworks();
8128

82-
int status() {
83-
sta_iface = net_if_get_wifi_sta();
84-
netif = sta_iface;
85-
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state,
86-
sizeof(struct wifi_iface_status))) {
87-
return WL_NO_SHIELD;
88-
}
89-
90-
if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
91-
return WL_CONNECTED;
92-
} else {
93-
return WL_DISCONNECTED;
94-
}
95-
return WL_NO_SHIELD;
96-
}
97-
98-
int8_t scanNetworks() {
99-
// TODO: borrow code from mbed core for scan results handling
100-
}
101-
102-
char *SSID() {
103-
if (status() == WL_CONNECTED) {
104-
return (char *)sta_state.ssid;
105-
}
106-
return nullptr;
107-
}
108-
109-
int32_t RSSI() {
110-
if (status() == WL_CONNECTED) {
111-
return sta_state.rssi;
112-
}
113-
return 0;
114-
}
29+
char *SSID();
30+
int32_t RSSI();
11531

11632
String firmwareVersion();
11733

0 commit comments

Comments
 (0)