samples: wifi: Fix mgmt conn req

When a WIFI driver is slow to get interface up the system not connect
to WIFI access point.  This add some tries to let driver be proper
initialize to allow accept connection request.

Signed-off-by: Gerson Fernando Budke <nandojve@gmail.com>
This commit is contained in:
Gerson Fernando Budke 2021-05-22 18:33:31 -03:00 committed by Kumar Gala
commit 1a2804c307
2 changed files with 25 additions and 7 deletions

View file

@ -39,6 +39,9 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb,
void wifi_connect(void)
{
int nr_tries = 10;
int ret = 0;
net_mgmt_init_event_callback(&wifi_shell_mgmt_cb,
wifi_mgmt_event_handler,
NET_EVENT_WIFI_CONNECT_RESULT);
@ -62,9 +65,16 @@ void wifi_connect(void)
LOG_INF("WIFI try connecting to %s...", CONFIG_TAGOIO_HTTP_WIFI_SSID);
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
&cnx_params, sizeof(struct wifi_connect_req_params))) {
return;
/* Let's wait few seconds to allow wifi device be on-line */
while (nr_tries-- > 0) {
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
sizeof(struct wifi_connect_req_params));
if (ret == 0) {
break;
}
LOG_INF("Connect request failed %d. Waiting iface be up...", ret);
k_msleep(500);
}
while (connected == 0) {

View file

@ -111,6 +111,7 @@ void main(void)
}
#if defined(CONFIG_WIFI)
int nr_tries = 10;
struct net_if *iface = net_if_get_default();
static struct wifi_connect_req_params cnx_params = {
.ssid = CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID,
@ -124,11 +125,18 @@ void main(void)
cnx_params.ssid_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_SSID);
cnx_params.psk_length = strlen(CONFIG_UPDATEHUB_SAMPLE_WIFI_PSK);
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
&cnx_params, sizeof(struct wifi_connect_req_params))) {
LOG_ERR("Error connecting to WiFi");
return;
/* Let's wait few seconds to allow wifi device be on-line */
while (nr_tries-- > 0) {
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
sizeof(struct wifi_connect_req_params));
if (ret == 0) {
break;
}
LOG_INF("Connect request failed %d. Waiting iface be up...", ret);
k_msleep(500);
}
#elif defined(CONFIG_MODEM_GSM_PPP)
const struct device *uart_dev =
device_get_binding(CONFIG_MODEM_GSM_UART_NAME);