92 lines
2.4 KiB
C
92 lines
2.4 KiB
C
#include "PicoW_WiFi_Operator.h"
|
|
|
|
#include <pico/cyw43_arch.h>
|
|
|
|
static enum wifi_operator_mode_t wifi_operator_mode = WIFI_OPERATOR_MODE_UNINITIALIZED;
|
|
|
|
int wifi_operator_init(enum wifi_operator_mode_t mode)
|
|
{
|
|
wifi_operator_mode = mode;
|
|
|
|
if(cyw43_wifi_pm(&cyw43_state, WIFI_OPERATOR_POWERSAVE_MODE))
|
|
{
|
|
puts("Failed to apply WiFi operator powersave mode.");
|
|
return PICO_ERROR_GENERIC;
|
|
}
|
|
|
|
if(wifi_operator_mode == WIFI_OPERATOR_MODE_ACCESS_POINT)
|
|
{
|
|
// Setup as access point
|
|
cyw43_arch_enable_ap_mode(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, WIFI_OPERATOR_AUTH);
|
|
cyw43_wifi_ap_set_channel(&cyw43_state, WIFI_OPERATOR_CHANNEL);
|
|
|
|
return PICO_OK;
|
|
}
|
|
else if(wifi_operator_mode == WIFI_OPERATOR_MODE_STATION)
|
|
{
|
|
// Setup as station
|
|
cyw43_arch_enable_sta_mode();
|
|
|
|
for(int ret = 1; ret; )
|
|
{
|
|
puts("Attempting to connect to WiFi network.");
|
|
|
|
int ret = cyw43_arch_wifi_connect_timeout_ms(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, WIFI_OPERATOR_AUTH, WIFI_OPERATOR_CONNECTION_TIMEOUT_MS);
|
|
|
|
switch(ret)
|
|
{
|
|
case PICO_ERROR_TIMEOUT:
|
|
puts("Timeout reached while trying to connect to WiFi network.");
|
|
break;
|
|
|
|
case PICO_ERROR_BADAUTH:
|
|
puts("Failed to autentificate to WiFi network.");
|
|
return ret;
|
|
|
|
default:
|
|
puts("Failed to connect to WiFi network.");
|
|
return ret;
|
|
}
|
|
}
|
|
}
|
|
|
|
wifi_operator_mode = WIFI_OPERATOR_MODE_UNINITIALIZED;
|
|
|
|
puts("WiFi operator mode not valid.");
|
|
|
|
return PICO_ERROR_INVALID_ARG;
|
|
}
|
|
|
|
int wifi_operator_exit(void)
|
|
{
|
|
if(cyw43_wifi_pm(&cyw43_state, CYW43_DEFAULT_PM))
|
|
{
|
|
puts("Failed to reset WiFi operator powersave mode.");
|
|
return PICO_ERROR_GENERIC;
|
|
}
|
|
|
|
if(wifi_operator_mode == WIFI_OPERATOR_MODE_ACCESS_POINT)
|
|
{
|
|
// Exit as access point
|
|
cyw43_arch_disable_ap_mode();
|
|
|
|
|
|
wifi_operator_mode = WIFI_OPERATOR_MODE_UNINITIALIZED;
|
|
|
|
return PICO_OK;
|
|
}
|
|
else if(wifi_operator_mode == WIFI_OPERATOR_MODE_STATION)
|
|
{
|
|
// Exit as station
|
|
cyw43_arch_disable_sta_mode();
|
|
|
|
wifi_operator_mode = WIFI_OPERATOR_MODE_UNINITIALIZED;
|
|
|
|
return PICO_OK;
|
|
}
|
|
|
|
puts("WiFi operator was not initialized correctly.");
|
|
|
|
return PICO_ERROR_INVALID_ARG;
|
|
}
|