diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60f3262 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Config.h \ No newline at end of file diff --git a/Config.h.template b/Config.h.template new file mode 100644 index 0000000..eb80040 --- /dev/null +++ b/Config.h.template @@ -0,0 +1,11 @@ +#ifndef _PICOW_WIFI_OPERATOR_CONFIG_H +#define _PICOW_WIFI_OPERATOR_CONFIG_H + +#define WIFI_OPERATOR_AUTH CYW43_AUTH_WPA2_AES_PSK +#define WIFI_OPERATOR_CHANNEL 3 +#define WIFI_OPERATOR_POWERSAVE_MODE CYW43_NO_POWERSAVE_MODE + +#define WIFI_OPERATOR_SSID "myssid2000+" +#define WIFI_OPERATOR_PASSWORD "*aw3s0meP4ssUwUord*" + +#endif // _PICOW_WIFI_OPERATOR_CONFIG_H \ No newline at end of file diff --git a/PicoW_WiFi_Operator.c b/PicoW_WiFi_Operator.c index e69de29..7534c80 100644 --- a/PicoW_WiFi_Operator.c +++ b/PicoW_WiFi_Operator.c @@ -0,0 +1,82 @@ +#include "PicoW_WiFi_Operator.h" + +#include + +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(); + + int ret = cyw43_arch_wifi_connect_blocking(WIFI_OPERATOR_SSID, WIFI_OPERATOR_PASSWORD, WIFI_OPERATOR_AUTH); + + switch(ret) + { + case PICO_ERROR_BADAUTH: + puts("Failed to autentificate to WiFi network."); + break; + + case PICO_ERROR_CONNECT_FAILED: + puts("Failed to connect to WiFi network."); + break; + + default: + break; + } + + 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(); + + return PICO_OK; + } + else if(wifi_operator_mode == WIFI_OPERATOR_MODE_STATION) + { + // Exit as station + cyw43_arch_disable_sta_mode(); + + return PICO_OK; + } + + puts("WiFi operator was not initialized correctly."); + + return PICO_ERROR_INVALID_ARG; +} diff --git a/PicoW_WiFi_Operator.h b/PicoW_WiFi_Operator.h index e69de29..a9a5dac 100644 --- a/PicoW_WiFi_Operator.h +++ b/PicoW_WiFi_Operator.h @@ -0,0 +1,28 @@ +#ifndef _PICOW_WIFI_OPERATOR_H +#define _PICOW_WIFI_OPERATOR_H + +#include "Config.h" + +/** + * @brief WiFi operator mode. + * Set in initialisation + */ +enum wifi_operator_mode_t { + WIFI_OPERATOR_MODE_UNINITIALIZED, + WIFI_OPERATOR_MODE_ACCESS_POINT, + WIFI_OPERATOR_MODE_STATION +}; + +/** + * @brief Init the WiFi operator with the given mode. + * + * @param mode Mode to initialise the wifi operator to. + */ +int wifi_operator_init(enum wifi_operator_mode_t mode); + +/** + * @brief Exit the WiFi operator and reset to default parameters. + */ +int wifi_operator_exit(void); + +#endif // _PICOW_WIFI_OPERATOR_H \ No newline at end of file diff --git a/lwipopts.h b/lwipopts.h index e1aa230..8f50d6b 100644 --- a/lwipopts.h +++ b/lwipopts.h @@ -1,5 +1,5 @@ -#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H -#define _LWIPOPTS_EXAMPLE_COMMONH_H +#ifndef _LWIPOPTS_H +#define _LWIPOPTS_H // Common settings used in most of the pico_w examples // (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)