Keep retrying if cyw43_wifi_join fails (#1195)
Currently, if the network can't be found we fail almost immediately. Also fix error codes. Fixes #1054
This commit is contained in:
		@ -22,6 +22,8 @@ enum pico_error_codes {
 | 
				
			|||||||
    PICO_ERROR_NOT_PERMITTED = -4,
 | 
					    PICO_ERROR_NOT_PERMITTED = -4,
 | 
				
			||||||
    PICO_ERROR_INVALID_ARG = -5,
 | 
					    PICO_ERROR_INVALID_ARG = -5,
 | 
				
			||||||
    PICO_ERROR_IO = -6,
 | 
					    PICO_ERROR_IO = -6,
 | 
				
			||||||
 | 
					    PICO_ERROR_BADAUTH = -7,
 | 
				
			||||||
 | 
					    PICO_ERROR_CONNECT_FAILED = -8,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // !__ASSEMBLER__
 | 
					#endif // !__ASSEMBLER__
 | 
				
			||||||
 | 
				
			|||||||
@ -76,13 +76,19 @@ int cyw43_arch_wifi_connect_async(const char *ssid, const char *pw, uint32_t aut
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Connect to wireless, return with success when an IP address has been assigned
 | 
					// Connect to wireless, return with success when an IP address has been assigned
 | 
				
			||||||
int cyw43_arch_wifi_connect_until(const char *ssid, const char *pw, uint32_t auth, absolute_time_t until) {
 | 
					static int cyw43_arch_wifi_connect_until(const char *ssid, const char *pw, uint32_t auth, absolute_time_t until) {
 | 
				
			||||||
    int err = cyw43_arch_wifi_connect_async(ssid, pw, auth);
 | 
					    int err = cyw43_arch_wifi_connect_async(ssid, pw, auth);
 | 
				
			||||||
    if (err) return err;
 | 
					    if (err) return err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int status = CYW43_LINK_UP + 1;
 | 
					    int status = CYW43_LINK_UP + 1;
 | 
				
			||||||
    while(status >= 0 && status != CYW43_LINK_UP) {
 | 
					    while(status >= 0 && status != CYW43_LINK_UP) {
 | 
				
			||||||
        int new_status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
 | 
					        int new_status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
 | 
				
			||||||
 | 
					        // If there was no network, keep trying
 | 
				
			||||||
 | 
					        if (new_status == CYW43_LINK_NONET) {
 | 
				
			||||||
 | 
					            new_status = CYW43_LINK_JOIN;
 | 
				
			||||||
 | 
					            err = cyw43_arch_wifi_connect_async(ssid, pw, auth);
 | 
				
			||||||
 | 
					            if (err) return err;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        if (new_status != status) {
 | 
					        if (new_status != status) {
 | 
				
			||||||
            status = new_status;
 | 
					            status = new_status;
 | 
				
			||||||
            CYW43_ARCH_DEBUG("connect status: %s\n", cyw43_tcpip_link_status_name(status));
 | 
					            CYW43_ARCH_DEBUG("connect status: %s\n", cyw43_tcpip_link_status_name(status));
 | 
				
			||||||
@ -94,7 +100,15 @@ int cyw43_arch_wifi_connect_until(const char *ssid, const char *pw, uint32_t aut
 | 
				
			|||||||
        cyw43_arch_poll();
 | 
					        cyw43_arch_poll();
 | 
				
			||||||
        cyw43_arch_wait_for_work_until(until);
 | 
					        cyw43_arch_wait_for_work_until(until);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return status == CYW43_LINK_UP ? 0 : status;
 | 
					    // Turn status into a pico_error_codes, CYW43_LINK_NONET shouldn't happen as we fail with PICO_ERROR_TIMEOUT instead
 | 
				
			||||||
 | 
					    assert(status == CYW43_LINK_UP || status == CYW43_LINK_BADAUTH || status == CYW43_LINK_FAIL);
 | 
				
			||||||
 | 
					    if (status == CYW43_LINK_UP) {
 | 
				
			||||||
 | 
					        return PICO_OK; // success
 | 
				
			||||||
 | 
					    } else if (status == CYW43_LINK_BADAUTH) {
 | 
				
			||||||
 | 
					        return PICO_ERROR_BADAUTH;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        return PICO_ERROR_CONNECT_FAILED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int cyw43_arch_wifi_connect_blocking(const char *ssid, const char *pw, uint32_t auth) {
 | 
					int cyw43_arch_wifi_connect_blocking(const char *ssid, const char *pw, uint32_t auth) {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user