From a8ce2cdf01d13328ffb04c88ee47c097fe324d4f Mon Sep 17 00:00:00 2001 From: Peter Antypas Date: Sun, 4 Dec 2022 20:17:59 -0800 Subject: [PATCH] Cleaned up http handlers --- .../Firmware/WiFiAdapter/core/configuration.c | 52 ++++++- .../Firmware/WiFiAdapter/core/configuration.h | 96 ++++++++++++ .../Firmware/WiFiAdapter/core/http_handlers.c | 140 +++++++++++++++--- latest/Firmware/WiFiAdapter/core/wifi.c | 69 ++++++--- .../WiFiAdapter/main/MaianaWiFiAdapter.c | 17 +-- 5 files changed, 315 insertions(+), 59 deletions(-) diff --git a/latest/Firmware/WiFiAdapter/core/configuration.c b/latest/Firmware/WiFiAdapter/core/configuration.c index cc4255c..0c9d7e1 100644 --- a/latest/Firmware/WiFiAdapter/core/configuration.c +++ b/latest/Firmware/WiFiAdapter/core/configuration.c @@ -3,9 +3,34 @@ #include "esp_mac.h" static char __ssid[48] = {0}; +static char __password[60] = {0}; + static char __ap_mac[32] = {0}; static char __st_mac[32] = {0}; +// NVS Keys +#define WIFI_MODE_KEY "wifi_mode" +#define WIFI_SSID_KEY "wifi_ssid" +#define WIFI_PASSWORD_KEY "wifi_pwd" + +#define NMEA_MODE_KEY "nmea_mode" +#define NMEA_IP_ADDR_KEY "nmea_ip" +#define NMEA_PORT_KEY "nmea_port" +#define NMEA_INCL_GNSS_KEY "nmea_gnss" + +static nvs_handle_t __nvs = 0; + +void config_init() +{ + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + nvs_flash_erase(); + nvs_flash_init(); + } + nvs_open("maiana", NVS_READWRITE, &__nvs); +} + const char *config_ap_mac_address() { if ( __ap_mac[0] == 0 ) @@ -30,25 +55,27 @@ const char *config_st_mac_address() return __st_mac; } -wifi_operation_mode_t config_get_wifi_operation_mode() -{ - return WIFI_OPEN_AP; -} + +//////////////////////////////////////////////////////////////////////////////////// +// WiFi +//////////////////////////////////////////////////////////////////////////////////// const char *config_get_ssid() { + size_t len = sizeof __ssid; + switch(config_get_wifi_operation_mode()) { case WIFI_OPEN_AP: case WIFI_SECURE_AP: - if ( __ssid[0] == 0 ) + if ( nvs_get_str(__nvs, WIFI_SSID_KEY, __ssid, &len) == ESP_ERR_NVS_NOT_FOUND || __ssid[0] == 0 ) { const char *mac = config_ap_mac_address(); sprintf(__ssid, "MAIANA_%s", mac+6); } break; case WIFI_STATION: - if ( __ssid[0] == 0 ) + if ( nvs_get_str(__nvs, WIFI_SSID_KEY, __ssid, &len) == ESP_ERR_NVS_NOT_FOUND || __ssid[0] == 0 ) { const char *mac = config_st_mac_address(); sprintf(__ssid, "MAIANA_%s", mac+6); @@ -63,6 +90,17 @@ const char *config_get_ssid() const char *config_get_password() { - return NULL; + if ( nvs_get_str(__nvs, WIFI_PASSWORD_KEY, __password, sizeof __password) == ESP_ERR_NVS_NOT_FOUND ) + return NULL; + + return __password; } +wifi_operation_mode_t config_get_wifi_operation_mode() +{ + int mode = -1; + if ( nvs_get_i32(__nvs, WIFI_MODE_KEY, &mode) == ESP_ERR_NVS_NOT_FOUND ) + return WIFI_OPEN_AP; + + else return (wifi_operation_mode_t)mode; +} \ No newline at end of file diff --git a/latest/Firmware/WiFiAdapter/core/configuration.h b/latest/Firmware/WiFiAdapter/core/configuration.h index e975efb..266ac00 100644 --- a/latest/Firmware/WiFiAdapter/core/configuration.h +++ b/latest/Firmware/WiFiAdapter/core/configuration.h @@ -2,6 +2,8 @@ #define __CONFIGURATION_H__ #include +#include +#include typedef enum { @@ -10,15 +12,109 @@ typedef enum WIFI_STATION } wifi_operation_mode_t; +typedef enum +{ + NMEA_TCP_LISTENER, + NMEA_TCP_SENDER, + NMEA_UDP_SENDER +} nmea_gateway_mode_t; + +void config_init(); + +/*** WiFi Configuration ***/ + +/** + * @brief Returns the WiFi operation mode + * + * @return wifi_operation_mode_t + */ wifi_operation_mode_t config_get_wifi_operation_mode(); +/** + * @brief Returns the SSID (regardless of mode) + * + * @return const char* + */ const char *config_get_ssid(); +/** + * @brief Returns the password (if applicable) + * + * @return const char* + */ const char *config_get_password(); +/** + * @brief Configures the WiFi interface. Will not take effect until reboot. + * + * @param mode Desired mode + * @param ssid Desired SSID + * @param password Desired password. May be NULL if not applicable. + */ +void config_wifi(wifi_operation_mode_t mode, const char *ssid, const char *password); + +/*** NMEA configuration ***/ + +/** + * @brief Returns NMEA gateway mode + * + * @return nmea_gateway_mode_t + */ +nmea_gateway_mode_t config_get_nmea_gateway_mode(); + +/** + * @brief Returns the IP address (not applicable for NMEA_TCP_LISTENER) + * + * @return const char* + */ +const char *config_get_nmea_gateway_ip(); + +/** + * @brief Returns the TCP port (applicable to all modes) + * + * @return uint16_t + */ +uint16_t config_get_nmea_gateway_port(); + +/** + * @brief Indicates whether the NMEA output stream includes GNSS sentences + * + * @return true + * @return false + */ +bool config_nmea_stream_includes_gnss(); + +/** + * @brief Congigures the NMEA gateway + * + * @param mode Desired mode + * @param ip Target IP (for senders only) + * @param port TCP port + * @param include_gnss Whether or not to include GNSS sentences + */ +void config_nmea_gateway(nmea_gateway_mode_t mode, const char *ip, uint16_t port, bool include_gnss); + +/*** Misc ***/ + +/** + * @brief Returns the AP MAC address + * + * @return const char* + */ const char *config_ap_mac_address(); +/** + * @brief Returns the station MAC address + * + * @return const char* + */ const char *config_st_mac_address(); +/** + * @brief Resets everything to defaults. Will take effect after reboot. + * + */ +void config_reset_all(); + #endif diff --git a/latest/Firmware/WiFiAdapter/core/http_handlers.c b/latest/Firmware/WiFiAdapter/core/http_handlers.c index 33098ae..7b90fd0 100644 --- a/latest/Firmware/WiFiAdapter/core/http_handlers.c +++ b/latest/Firmware/WiFiAdapter/core/http_handlers.c @@ -34,29 +34,14 @@ esp_err_t http_html_handler(httpd_req_t *req, const char *html) return err; } + esp_err_t http_root_handler(httpd_req_t *req) { ESP_LOGI(TAG, "Request: %s", req->uri); - if ( strstr(req->uri, "/images") ) - { - return http_img_handler(req); - } - else if ( strcmp(req->uri, "/") == 0 || strstr(req->uri, "index.html") ) + if ( strcmp(req->uri, "/") == 0 || strstr(req->uri, "index.html") ) { return http_html_handler(req, index_html); } - else if ( strstr(req->uri, "wifi.html") ) - { - return http_html_handler(req, wifi_html); - } - else if ( strstr(req->uri, "ais.html") ) - { - return http_html_handler(req, ais_html); - } - else if ( strstr(req->uri, "nmea.html") ) - { - return http_html_handler(req, nmea_html); - } else { httpd_resp_send_404(req); @@ -64,7 +49,22 @@ esp_err_t http_root_handler(httpd_req_t *req) return ESP_OK; } -esp_err_t http_img_handler(httpd_req_t *req) +esp_err_t http_wifi_handler(httpd_req_t *req) +{ + return http_html_handler(req, wifi_html); +} + +esp_err_t http_ais_handler(httpd_req_t *req) +{ + return http_html_handler(req, ais_html); +} + +esp_err_t http_nmea_handler(httpd_req_t *req) +{ + return http_html_handler(req, nmea_html); +} + +esp_err_t http_image_handler(httpd_req_t *req) { ESP_LOGI(TAG, "Request: %s", req->uri); if ( strstr(req->uri, "maiana-logo.jpg") ) @@ -83,19 +83,111 @@ esp_err_t http_img_handler(httpd_req_t *req) } -/** URI definitions */ - -httpd_uri_t uri_root = +esp_err_t http_wifi_post_handler(httpd_req_t *req) { - .uri = "*", + return ESP_OK; +} + +esp_err_t http_ais_post_handler(httpd_req_t *req) +{ + return ESP_OK; +} + +esp_err_t http_nmea_post_handler(httpd_req_t *req) +{ + return ESP_OK; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// URIs +//////////////////////////////////////////////////////////////////////////////////////// + + +httpd_uri_t uri_root1 = +{ + .uri = "/", .method = HTTP_GET, .handler = http_root_handler, .user_ctx = NULL }; +httpd_uri_t uri_root2 = +{ + .uri = "/index.html", + .method = HTTP_GET, + .handler = http_root_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_images = +{ + .uri = "/images/*", + .method = HTTP_GET, + .handler = http_image_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_wifi = +{ + .uri = "/wifi.html", + .method = HTTP_GET, + .handler = http_wifi_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_ais = +{ + .uri = "/ais.html", + .method = HTTP_GET, + .handler = http_ais_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_nmea = +{ + .uri = "/nmea.html", + .method = HTTP_GET, + .handler = http_nmea_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_wifi_post = +{ + .uri = "/wifi.html", + .method = HTTP_POST, + .handler = http_wifi_post_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_ais_post = +{ + .uri = "/ais.html", + .method = HTTP_POST, + .handler = http_ais_post_handler, + .user_ctx = NULL +}; + +httpd_uri_t uri_nmea_post = +{ + .uri = "/nmea.html", + .method = HTTP_POST, + .handler = http_nmea_post_handler, + .user_ctx = NULL +}; + +//////////////////////////////////////////////////////////////////////////////////////// +// Registration +//////////////////////////////////////////////////////////////////////////////////////// -/** Registration */ void register_http_handlers(httpd_handle_t handle) { - httpd_register_uri_handler(handle, &uri_root); + httpd_register_uri_handler(handle, &uri_root1); + httpd_register_uri_handler(handle, &uri_root2); + httpd_register_uri_handler(handle, &uri_images); + httpd_register_uri_handler(handle, &uri_wifi); + httpd_register_uri_handler(handle, &uri_ais); + httpd_register_uri_handler(handle, &uri_nmea); + httpd_register_uri_handler(handle, &uri_wifi_post); + httpd_register_uri_handler(handle, &uri_ais_post); + httpd_register_uri_handler(handle, &uri_nmea_post); } diff --git a/latest/Firmware/WiFiAdapter/core/wifi.c b/latest/Firmware/WiFiAdapter/core/wifi.c index 5e0306f..6c5e8ad 100644 --- a/latest/Firmware/WiFiAdapter/core/wifi.c +++ b/latest/Firmware/WiFiAdapter/core/wifi.c @@ -9,26 +9,52 @@ static const char *TAG = "wifi"; - - static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { - if (event_id == WIFI_EVENT_AP_STACONNECTED) { - wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; - ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", - MAC2STR(event->mac), event->aid); - } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { - wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; - ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", - MAC2STR(event->mac), event->aid); + switch(event_id) + { + case WIFI_EVENT_AP_STACONNECTED: + { + wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", + MAC2STR(event->mac), event->aid); + break; } + case WIFI_EVENT_AP_STADISCONNECTED: + { + wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; + ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", + MAC2STR(event->mac), event->aid); + break; + } + case WIFI_EVENT_STA_START: + { + esp_wifi_connect(); + break; + } + case WIFI_EVENT_STA_CONNECTED: + { + break; + } + case WIFI_EVENT_STA_DISCONNECTED: + { + esp_wifi_connect(); + break; + } + default: + break; + } + } -void wifi_init_softap(void) +void wifi_start() { esp_netif_init(); - esp_netif_create_default_wifi_ap(); + wifi_operation_mode_t mode = config_get_wifi_operation_mode(); + + if ( mode != WIFI_STATION ) + esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); @@ -39,9 +65,9 @@ void wifi_init_softap(void) NULL, NULL)); - wifi_operation_mode_t mode = config_get_wifi_operation_mode(); wifi_config_t wifi_config = {0}; const char *ssid = config_get_ssid(); + const char *password = config_get_password(); switch(mode) { @@ -57,12 +83,23 @@ void wifi_init_softap(void) break; case WIFI_SECURE_AP: { + memcpy(wifi_config.ap.ssid, ssid, strlen(ssid)); + wifi_config.ap.ssid_len = strlen(ssid); + strncpy((char *)wifi_config.ap.password, password, sizeof wifi_config.ap.password); + + wifi_config.ap.authmode = WIFI_AUTH_WPA2_PSK; + wifi_config.ap.max_connection = 8; + esp_wifi_set_mode(WIFI_MODE_AP); + esp_wifi_set_config(WIFI_IF_AP, &wifi_config); } break; case WIFI_STATION: { - + memcpy(wifi_config.sta.ssid, ssid, strlen(ssid)); + strncpy((char *)wifi_config.sta.password, password, sizeof wifi_config.ap.password); + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); } break; } @@ -70,7 +107,3 @@ void wifi_init_softap(void) esp_wifi_start(); } -void wifi_start() -{ - wifi_init_softap(); -} \ No newline at end of file diff --git a/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c b/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c index b9b5483..9a4c921 100755 --- a/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c +++ b/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c @@ -6,13 +6,15 @@ #include "nvs_flash.h" #include "wifi.h" #include "http_server.h" +#include "configuration.h" + void uart_rx_cb(char *s) { if ( strlen(s) == 0 ) return; - //printf(s); + printf(s); } void btn_press_handler(void *args, esp_event_base_t base, int32_t id, void *data) @@ -30,21 +32,16 @@ void btn_press_handler(void *args, esp_event_base_t base, int32_t id, void *data void app_main(void) { - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) - { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - - esp_event_loop_create_default(); + + config_init(); + bsp_set_uart_rx_cb(uart_rx_cb); bsp_hw_init(); + esp_event_handler_register(BSP_EVENT, BSP_TX_BTN_EVENT, btn_press_handler, NULL); wifi_start(); - //sleep(10); start_httpd(); vTaskDelete(NULL);