Skip to content

Commit 7cd50a1

Browse files
moving implementation into cpp file
1 parent d088e7e commit 7cd50a1

File tree

2 files changed

+95
-84
lines changed

2 files changed

+95
-84
lines changed

libraries/WiFi/src/WiFi.cpp

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

libraries/WiFi/src/WiFi.h

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,97 +16,18 @@ class WiFiClass: public NetworkInterface
1616
WiFiClass() {}
1717
~WiFiClass() {}
1818

19-
int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) {
20-
sta_iface = net_if_get_wifi_sta();
21-
netif = sta_iface;
22-
sta_config.ssid = (const uint8_t *)ssid;
23-
sta_config.ssid_length = strlen(ssid);
24-
sta_config.psk = (const uint8_t *)passphrase;
25-
sta_config.psk_length = strlen(passphrase);
26-
// TODO: change these fields with scan() results
27-
sta_config.security = WIFI_SECURITY_TYPE_PSK;
28-
sta_config.channel = WIFI_CHANNEL_ANY;
29-
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
30-
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
19+
int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true);
3120

32-
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
33-
sizeof(struct wifi_connect_req_params));
34-
if (ret) {
35-
return false;
36-
}
21+
int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true);
3722

38-
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
39-
if (blocking) {
40-
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER);
41-
}
42-
43-
return status();
44-
}
45-
46-
int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true) {
47-
if (ap_iface != NULL) {
48-
return false;
49-
}
50-
ap_iface = net_if_get_wifi_sap();
51-
netif = ap_iface;
52-
ap_config.ssid = (const uint8_t *)ssid;
53-
ap_config.ssid_length = strlen(ssid);
54-
ap_config.psk = (const uint8_t *)passphrase;
55-
ap_config.psk_length = strlen(passphrase);
56-
ap_config.security = WIFI_SECURITY_TYPE_PSK;
57-
ap_config.channel = channel;
58-
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
59-
ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
60-
61-
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
62-
sizeof(struct wifi_connect_req_params));
63-
64-
if (ret) {
65-
return false;
66-
}
67-
68-
enable_dhcpv4_server(ap_iface);
69-
70-
if (blocking) {
71-
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER);
72-
}
73-
74-
return status();
75-
}
76-
77-
int status() {
78-
sta_iface = net_if_get_wifi_sta();
79-
netif = sta_iface;
80-
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state,
81-
sizeof(struct wifi_iface_status))) {
82-
return WL_NO_SHIELD;
83-
}
84-
85-
if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
86-
return WL_CONNECTED;
87-
} else {
88-
return WL_DISCONNECTED;
89-
}
90-
return WL_NO_SHIELD;
91-
}
23+
int status();
9224

9325
int8_t scanNetworks() {
9426
// TODO: borrow code from mbed core for scan results handling
9527
}
9628

97-
char* SSID() {
98-
if (status() == WL_CONNECTED) {
99-
return (char *)sta_state.ssid;
100-
}
101-
return nullptr;
102-
}
103-
104-
int32_t RSSI() {
105-
if (status() == WL_CONNECTED) {
106-
return sta_state.rssi;
107-
}
108-
return 0;
109-
}
29+
char* SSID();
30+
int32_t RSSI();
11031

11132
String firmwareVersion();
11233

0 commit comments

Comments
 (0)