Skip to content

Commit c99b150

Browse files
authored
Merge pull request #11356 from kyle-cypress/pr/whd-link-state-fix
Fix WHD link state change event handling
2 parents 6805486 + 0b2e7bb commit c99b150

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

features/netsocket/emac-drivers/TARGET_WHD/interface/WhdSTAInterface.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ struct whd_scan_userdata {
4646

4747
static whd_scan_userdata interal_scan_data;
4848
static whd_scan_result_t internal_scan_result;
49+
static uint16_t sta_link_update_entry = 0xFF;
50+
static const whd_event_num_t sta_link_change_events[] = {
51+
WLC_E_SET_SSID, WLC_E_LINK, WLC_E_AUTH, WLC_E_ASSOC, WLC_E_DEAUTH_IND, WLC_E_DISASSOC_IND, WLC_E_DISASSOC,
52+
WLC_E_REASSOC, WLC_E_PSK_SUP, WLC_E_ACTION_FRAME_COMPLETE, WLC_E_NONE
53+
};
54+
4955

5056
extern "C" void whd_emac_wifi_link_state_changed(whd_interface_t ifp, whd_bool_t state_up);
5157

@@ -143,6 +149,30 @@ whd_security_t whd_fromsecurity(nsapi_security_t sec)
143149
}
144150
}
145151

152+
static void *whd_wifi_link_state_change_handler(whd_interface_t ifp,
153+
const whd_event_header_t *event_header,
154+
const uint8_t *event_data,
155+
void *handler_user_data)
156+
{
157+
UNUSED_PARAMETER(event_data);
158+
159+
if (event_header->bsscfgidx >= WHD_INTERFACE_MAX) {
160+
WPRINT_WHD_DEBUG(("%s: event_header: Bad interface\n", __FUNCTION__));
161+
return NULL;
162+
}
163+
164+
if (event_header->event_type == WLC_E_DEAUTH_IND ||
165+
event_header->event_type == WLC_E_DISASSOC_IND) {
166+
whd_emac_wifi_link_state_changed(ifp, WHD_FALSE);
167+
}
168+
169+
if (whd_wifi_is_ready_to_transceive(ifp) == WHD_SUCCESS) {
170+
whd_emac_wifi_link_state_changed(ifp, WHD_TRUE);
171+
}
172+
173+
return handler_user_data;
174+
}
175+
146176
MBED_WEAK WhdSTAInterface::OlmInterface &WhdSTAInterface::OlmInterface::get_default_instance()
147177
{
148178
static OlmInterface olm;
@@ -204,12 +234,19 @@ nsapi_error_t WhdSTAInterface::connect()
204234

205235
#define MAX_RETRY_COUNT ( 5 )
206236
int i;
237+
whd_result_t res;
207238

208239
// initialize wiced, this is noop if already init
209240
if (!_whd_emac.powered_up) {
210241
_whd_emac.power_up();
211242
}
212243

244+
res = whd_management_set_event_handler(_whd_emac.ifp, sta_link_change_events,
245+
whd_wifi_link_state_change_handler, NULL, &sta_link_update_entry);
246+
if (res != WHD_SUCCESS) {
247+
return whd_toerror(res);
248+
}
249+
213250
if (!_interface) {
214251
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
215252
if (err != NSAPI_ERROR_OK) {
@@ -239,7 +276,6 @@ nsapi_error_t WhdSTAInterface::connect()
239276
whd_security_t security = whd_fromsecurity(_security);
240277

241278
// join the network
242-
whd_result_t res;
243279
for (i = 0; i < MAX_RETRY_COUNT; i++) {
244280
res = (whd_result_t)whd_wifi_join(_whd_emac.ifp,
245281
&ssid,
@@ -290,6 +326,12 @@ nsapi_error_t WhdSTAInterface::disconnect()
290326
if (res != WHD_SUCCESS) {
291327
return whd_toerror(res);
292328
}
329+
whd_emac_wifi_link_state_changed(_whd_emac.ifp, WHD_FALSE);
330+
331+
res = whd_wifi_deregister_event_handler(_whd_emac.ifp, sta_link_update_entry);
332+
if (res != WHD_SUCCESS) {
333+
return whd_toerror(res);
334+
}
293335

294336
// de-init Offload Manager
295337
if (_olm != NULL) {

features/netsocket/emac-drivers/TARGET_WHD/interface/whd_emac.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ bool WHD_EMAC::power_up()
115115
if (CY_RSLT_SUCCESS == res) {
116116
drvp = cybsp_get_wifi_driver();
117117
powered_up = true;
118-
ifp->whd_link_update_callback = whd_emac_wifi_link_state_changed;
119118
if (link_state && emac_link_state_cb) {
120119
emac_link_state_cb(link_state);
121120
}
@@ -291,5 +290,3 @@ extern "C"
291290
}
292291

293292
} // extern "C"
294-
295-

0 commit comments

Comments
 (0)