Skip to content

Commit 5f9dcc9

Browse files
authoredOct 28, 2024
Merge pull request #976 from pennam/wifi-timeout
WiFi: add setTimeout()
2 parents 3947c86 + 9280863 commit 5f9dcc9

File tree

3 files changed

+174
-11
lines changed

3 files changed

+174
-11
lines changed
 

Diff for: ‎libraries/WiFi/src/WiFi.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ int arduino::WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_t
5353

5454
wifi_if->attach(&arduino::WiFiClass::statusCallback);
5555

56-
scanNetworks();
57-
58-
if (isVisible(ssid)) {
59-
// Set the network security mode from the scan result.
60-
_security = ap_list[connected_ap].get_security();
61-
} else {
62-
// For hidden networks, the security mode must be set explicitly.
63-
// if ENC_TYPE_UNKNOWN this means that is the default value and so the user
64-
// has not set it... no worth trying, it is probably an unknown (not hidden)
65-
// interface
66-
if(security == ENC_TYPE_UNKNOWN) {
56+
if(security == ENC_TYPE_UNKNOWN) {
57+
scanNetworks();
58+
if (isVisible(ssid)) {
59+
// Set the network security mode from the scan result.
60+
_security = ap_list[connected_ap].get_security();
61+
} else {
62+
// For hidden networks, the security mode must be set explicitly.
63+
// if ENC_TYPE_UNKNOWN this means that is the default value and so the user
64+
// has not set it... no worth trying, it is probably an unknown (not hidden)
65+
// interface
6766
_currentNetworkStatus = WL_CONNECT_FAILED;
6867
return _currentNetworkStatus;
6968
}
69+
} else {
7070
_security = enum2sec(security);
7171
}
7272

@@ -75,6 +75,7 @@ int arduino::WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_t
7575
wifi_if->set_network(_ip, _netmask, _gateway);
7676
}
7777

78+
wifi_if->set_timeout(_timeout);
7879
nsapi_error_t result = wifi_if->connect(ssid, passphrase, _security);
7980

8081
if(result == NSAPI_ERROR_IS_CONNECTED) {
@@ -297,6 +298,10 @@ unsigned long arduino::WiFiClass::getTime() {
297298
return 0;
298299
}
299300

301+
void arduino::WiFiClass::setTimeout(unsigned long timeout) {
302+
_timeout = timeout;
303+
}
304+
300305
void arduino::WiFiClass::statusCallback(nsapi_event_t status, intptr_t param)
301306
{
302307
if (((param == NSAPI_STATUS_DISCONNECTED) ||

Diff for: ‎libraries/WiFi/src/WiFi.h

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ class WiFiClass : public MbedSocketClass {
168168

169169
unsigned long getTime();
170170

171+
/*
172+
* Configure WiFi join timeout in milliseconds. Default value is 7s.
173+
*/
174+
void setTimeout(unsigned long timeout);
175+
171176
friend class WiFiClient;
172177
friend class WiFiServer;
173178
friend class WiFiUDP;
@@ -183,6 +188,7 @@ class WiFiClass : public MbedSocketClass {
183188
WiFiAccessPoint* ap_list = nullptr;
184189
uint8_t connected_ap;
185190
nsapi_security_t _security;
191+
unsigned long _timeout = 7000;
186192
int setSSID(const char* ssid);
187193
void ensureDefaultAPNetworkConfiguration();
188194
static void* handleAPEvents(whd_interface_t ifp, const whd_event_header_t* event_header, const uint8_t* event_data, void* handler_user_data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
From efd54c8990ba5b437eb4eb8b786b7e48941b03f1 Mon Sep 17 00:00:00 2001
2+
From: pennam <m.pennasilico@arduino.cc>
3+
Date: Mon, 21 Oct 2024 11:27:36 +0200
4+
Subject: [PATCH] WHD: add join timeout parameter to WiFiSTAInterface and
5+
drivers
6+
7+
---
8+
.../emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp | 7 ++++---
9+
.../emac/COMPONENT_WHD/interface/WhdSTAInterface.h | 6 ++++++
10+
.../COMPONENT_WHD/wifi-host-driver/inc/whd_wifi_api.h | 2 +-
11+
.../COMPONENT_WHD/wifi-host-driver/src/whd_wifi_api.c | 9 ++++++---
12+
connectivity/netsocket/include/netsocket/WiFiInterface.h | 7 +++++++
13+
5 files changed, 24 insertions(+), 7 deletions(-)
14+
15+
diff --git a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
16+
index c933203d36..f7631a0583 100644
17+
--- a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
18+
+++ b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.cpp
19+
@@ -211,7 +211,8 @@ WhdSTAInterface::WhdSTAInterface(WHD_EMAC &emac, OnboardNetworkStack &stack, Olm
20+
_security(NSAPI_SECURITY_NONE),
21+
_whd_emac(emac),
22+
_olm(&olm),
23+
- _iface_shared(shared)
24+
+ _iface_shared(shared),
25+
+ _timeout(7000)
26+
{
27+
}
28+
29+
@@ -334,7 +335,7 @@ nsapi_error_t WhdSTAInterface::connect()
30+
res = (whd_result_t)whd_wifi_join(_whd_emac.ifp,
31+
&ssid,
32+
security,
33+
- (const uint8_t *)_pass, strlen(_pass));
34+
+ (const uint8_t *)_pass, strlen(_pass), _timeout);
35+
}
36+
else
37+
{
38+
@@ -345,7 +346,7 @@ nsapi_error_t WhdSTAInterface::connect()
39+
res = (whd_result_t)whd_wifi_join(_whd_emac.ifp,
40+
&ssid,
41+
security,
42+
- (const uint8_t *)_pass, key_length);
43+
+ (const uint8_t *)_pass, key_length, _timeout);
44+
}
45+
if (res == WHD_SUCCESS) {
46+
break;
47+
diff --git a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.h b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.h
48+
index 4dd1098947..bfe933bac7 100644
49+
--- a/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.h
50+
+++ b/connectivity/drivers/emac/COMPONENT_WHD/interface/WhdSTAInterface.h
51+
@@ -119,6 +119,11 @@ public:
52+
return 0;
53+
}
54+
55+
+ nsapi_error_t set_timeout(uint32_t timeout)
56+
+ {
57+
+ _timeout = timeout;
58+
+ }
59+
+
60+
/** Set blocking status of interface.
61+
* Nonblocking mode unsupported.
62+
*
63+
@@ -257,6 +262,7 @@ private:
64+
nsapi_security_t _security;
65+
WHD_EMAC &_whd_emac;
66+
OlmInterface *_olm;
67+
+ uint32_t _timeout;
68+
whd_interface_shared_info_t &_iface_shared;
69+
};
70+
71+
diff --git a/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/inc/whd_wifi_api.h b/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/inc/whd_wifi_api.h
72+
index f3b73214cb..291bd23de8 100755
73+
--- a/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/inc/whd_wifi_api.h
74+
+++ b/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/inc/whd_wifi_api.h
75+
@@ -281,7 +281,7 @@ extern uint32_t whd_wifi_stop_scan(whd_interface_t ifp);
76+
* Error code if an error occurred
77+
*/
78+
extern uint32_t whd_wifi_join(whd_interface_t ifp, const whd_ssid_t *ssid, whd_security_t auth_type,
79+
- const uint8_t *security_key, uint8_t key_length);
80+
+ const uint8_t *security_key, uint8_t key_length, uint32_t timeout);
81+
82+
/** Joins a specific Wi-Fi network
83+
*
84+
diff --git a/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/src/whd_wifi_api.c b/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/src/whd_wifi_api.c
85+
index 5294104ab4..8a8f411ef9 100755
86+
--- a/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/src/whd_wifi_api.c
87+
+++ b/connectivity/drivers/wifi/COMPONENT_WHD/wifi-host-driver/src/whd_wifi_api.c
88+
@@ -294,6 +294,8 @@ static const uint16_t mcs_data_rate_lookup_table[32][2][2] =
89+
},
90+
};
91+
92+
+static whd_wifi_join_timeout = DEFAULT_JOIN_ATTEMPT_TIMEOUT;
93+
+
94+
95+
/******************************************************
96+
* Static Function prototypes
97+
@@ -1334,7 +1336,7 @@ static uint32_t whd_wifi_join_wait_for_complete(whd_interface_t ifp, cy_semaphor
98+
99+
while (!done)
100+
{
101+
- result = cy_rtos_get_semaphore(semaphore, DEFAULT_JOIN_ATTEMPT_TIMEOUT / 10, WHD_FALSE);
102+
+ result = cy_rtos_get_semaphore(semaphore, whd_wifi_join_timeout / 10, WHD_FALSE);
103+
whd_assert("Get semaphore failed", (result == CY_RSLT_SUCCESS) || (result == CY_RTOS_TIMEOUT) );
104+
REFERENCE_DEBUG_ONLY_VARIABLE(result);
105+
106+
@@ -1345,7 +1347,7 @@ static uint32_t whd_wifi_join_wait_for_complete(whd_interface_t ifp, cy_semaphor
107+
}
108+
109+
cy_rtos_get_time(&current_time);
110+
- done = (whd_bool_t)( (current_time - start_time) >= DEFAULT_JOIN_ATTEMPT_TIMEOUT );
111+
+ done = (whd_bool_t)( (current_time - start_time) >= whd_wifi_join_timeout );
112+
}
113+
114+
if (result != WHD_SUCCESS)
115+
@@ -1574,7 +1576,7 @@ uint32_t whd_wifi_join_specific(whd_interface_t ifp, const whd_scan_result_t *ap
116+
}
117+
118+
uint32_t whd_wifi_join(whd_interface_t ifp, const whd_ssid_t *ssid, whd_security_t auth_type,
119+
- const uint8_t *security_key, uint8_t key_length)
120+
+ const uint8_t *security_key, uint8_t key_length, uint32_t timeout)
121+
{
122+
cy_semaphore_t join_sema;
123+
whd_result_t result;
124+
@@ -1616,6 +1618,7 @@ uint32_t whd_wifi_join(whd_interface_t ifp, const whd_ssid_t *ssid, whd_security
125+
ssid_params->SSID_len = htod32(ssid->length);
126+
memcpy(ssid_params->SSID, ssid->value, ssid_params->SSID_len);
127+
result = whd_cdc_send_ioctl(ifp, CDC_SET, WLC_SET_SSID, buffer, 0);
128+
+ whd_wifi_join_timeout = timeout;
129+
130+
if (result == WHD_SUCCESS)
131+
{
132+
diff --git a/connectivity/netsocket/include/netsocket/WiFiInterface.h b/connectivity/netsocket/include/netsocket/WiFiInterface.h
133+
index 4fd7fc6fb8..c13cab4312 100644
134+
--- a/connectivity/netsocket/include/netsocket/WiFiInterface.h
135+
+++ b/connectivity/netsocket/include/netsocket/WiFiInterface.h
136+
@@ -59,6 +59,13 @@ public:
137+
*/
138+
virtual nsapi_error_t set_channel(uint8_t channel) = 0;
139+
140+
+ /** Set the Wi-Fi network join timeout.
141+
+ *
142+
+ * @param timeout joint timeout in milliseconds (Default: 7000).
143+
+ * @return NSAPI_ERROR_OK on success, or error code on failure.
144+
+ */
145+
+ virtual nsapi_error_t set_timeout(uint32_t timeout) = 0;
146+
+
147+
/** Get the current radio signal strength for active connection.
148+
*
149+
* @return Connection strength in dBm (negative value),
150+
--
151+
2.45.2
152+

0 commit comments

Comments
 (0)