diff --git a/latest/Firmware/WiFiAdapter/CMakeLists.txt b/latest/Firmware/WiFiAdapter/CMakeLists.txt index 730e7ef..1777def 100755 --- a/latest/Firmware/WiFiAdapter/CMakeLists.txt +++ b/latest/Firmware/WiFiAdapter/CMakeLists.txt @@ -5,6 +5,6 @@ cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -set(EXTRA_COMPONENT_DIRS bsp) +set(EXTRA_COMPONENT_DIRS bsp core html) list (APPEND EXTRA_COMPONENT_DIRS components) project(MaianaWiFiAdapter) diff --git a/latest/Firmware/WiFiAdapter/core/CMakeLists.txt b/latest/Firmware/WiFiAdapter/core/CMakeLists.txt new file mode 100644 index 0000000..7ceb9a0 --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/CMakeLists.txt @@ -0,0 +1,13 @@ +set (SOURCES + wifi.c + configuration.c + http_server.c + http_handlers.c + ) +idf_component_register(SRCS ${SOURCES} + INCLUDE_DIRS . + REQUIRES + nvs_flash + esp_http_server + ) + \ No newline at end of file diff --git a/latest/Firmware/WiFiAdapter/core/configuration.c b/latest/Firmware/WiFiAdapter/core/configuration.c new file mode 100644 index 0000000..cc4255c --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/configuration.c @@ -0,0 +1,68 @@ +#include "configuration.h" +#include "nvs_flash.h" +#include "esp_mac.h" + +static char __ssid[48] = {0}; +static char __ap_mac[32] = {0}; +static char __st_mac[32] = {0}; + +const char *config_ap_mac_address() +{ + if ( __ap_mac[0] == 0 ) + { + uint8_t mac[6]; + esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); + sprintf(__ap_mac, "%.2X%.2X%.2X%.2X%.2X%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + } + + return __ap_mac; +} + +const char *config_st_mac_address() +{ + if ( __st_mac[0] == 0 ) + { + uint8_t mac[6]; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + sprintf(__st_mac, "%.2X%.2X%.2X%.2X%.2X%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + } + + return __st_mac; +} + +wifi_operation_mode_t config_get_wifi_operation_mode() +{ + return WIFI_OPEN_AP; +} + +const char *config_get_ssid() +{ + switch(config_get_wifi_operation_mode()) + { + case WIFI_OPEN_AP: + case WIFI_SECURE_AP: + if ( __ssid[0] == 0 ) + { + const char *mac = config_ap_mac_address(); + sprintf(__ssid, "MAIANA_%s", mac+6); + } + break; + case WIFI_STATION: + if ( __ssid[0] == 0 ) + { + const char *mac = config_st_mac_address(); + sprintf(__ssid, "MAIANA_%s", mac+6); + } + break; + default: + return NULL; + } + + return __ssid; +} + +const char *config_get_password() +{ + return NULL; +} + diff --git a/latest/Firmware/WiFiAdapter/core/configuration.h b/latest/Firmware/WiFiAdapter/core/configuration.h new file mode 100644 index 0000000..e975efb --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/configuration.h @@ -0,0 +1,24 @@ +#ifndef __CONFIGURATION_H__ +#define __CONFIGURATION_H__ + +#include + +typedef enum +{ + WIFI_OPEN_AP, + WIFI_SECURE_AP, + WIFI_STATION +} wifi_operation_mode_t; + +wifi_operation_mode_t config_get_wifi_operation_mode(); + +const char *config_get_ssid(); + +const char *config_get_password(); + +const char *config_ap_mac_address(); + +const char *config_st_mac_address(); + + +#endif diff --git a/latest/Firmware/WiFiAdapter/core/http_handlers.c b/latest/Firmware/WiFiAdapter/core/http_handlers.c new file mode 100644 index 0000000..a3637cd --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/http_handlers.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "configuration.h" + +extern const char index_html[] asm("index_html"); + +static const char *TAG = "httpd"; + + +void send_std_headers(httpd_req_t *req) +{ + httpd_resp_set_hdr(req, "Connection", "Close"); + httpd_resp_set_hdr(req, "Cache-Control", "no-cache"); +} + +esp_err_t http_root_handler(httpd_req_t *req) +{ + ESP_LOGI(TAG, "Request: %s", req->uri); + if ( strcmp(req->uri, "/") == 0 || strcmp(req->uri, "/index.html") == 0 ) + { + send_std_headers(req); + esp_err_t err = httpd_resp_send(req, index_html, HTTPD_RESP_USE_STRLEN); + ESP_LOGI(TAG, "GET %s %d %d", req->uri, strlen(index_html), err); + } + else if ( strcmp(req->uri, "/favicon.ico") == 0 ) + { + httpd_resp_send_404(req); + } + return ESP_OK; +} + + +/** URI definitions */ + +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 +}; + +/** Registration */ +void register_http_handlers(httpd_handle_t handle) +{ + httpd_register_uri_handler(handle, &uri_root1); + //httpd_register_uri_handler(handle, &uri_root2); +} diff --git a/latest/Firmware/WiFiAdapter/core/http_server.c b/latest/Firmware/WiFiAdapter/core/http_server.c new file mode 100644 index 0000000..f059fec --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/http_server.c @@ -0,0 +1,47 @@ +#include "http_server.h" +#include +#include +#include +#include +#include +#include +#include "configuration.h" + +static httpd_handle_t __handle = NULL; + +static const char *TAG = "httpd"; + +extern void register_http_handlers(httpd_handle_t); + +void start_httpd() +{ + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + config.uri_match_fn = httpd_uri_match_wildcard; + config.max_uri_handlers = 20; + //config.max_open_sockets = 1; + + if (httpd_start(&__handle, &config) == ESP_OK) + { + register_http_handlers(__handle); + ESP_LOGI(TAG, "Started HTTPD"); + } + } + +void stop_httpd() +{ + if (__handle) + { + httpd_stop(__handle); + __handle = NULL; + ESP_LOGI(TAG, "Stopped HTTPD"); + } +} + +bool is_httpd_running() +{ + return __handle != NULL; +} + + + + diff --git a/latest/Firmware/WiFiAdapter/core/http_server.h b/latest/Firmware/WiFiAdapter/core/http_server.h new file mode 100644 index 0000000..236d700 --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/http_server.h @@ -0,0 +1,10 @@ +#ifndef __HTTP_SERVER_H__ +#define __HTTP_SERVER_H__ + +#include + +void start_httpd(); +void stop_httpd(); +bool is_httpd_running(); + +#endif diff --git a/latest/Firmware/WiFiAdapter/core/wifi.c b/latest/Firmware/WiFiAdapter/core/wifi.c new file mode 100644 index 0000000..5e0306f --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/wifi.c @@ -0,0 +1,76 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_mac.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "wifi.h" +#include "configuration.h" + +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); + } +} + +void wifi_init_softap(void) +{ + esp_netif_init(); + esp_netif_create_default_wifi_ap(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &wifi_event_handler, + NULL, + NULL)); + + wifi_operation_mode_t mode = config_get_wifi_operation_mode(); + wifi_config_t wifi_config = {0}; + const char *ssid = config_get_ssid(); + + switch(mode) + { + case WIFI_OPEN_AP: + { + memcpy(wifi_config.ap.ssid, ssid, strlen(ssid)); + wifi_config.ap.ssid_len = strlen(ssid); + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + 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_SECURE_AP: + { + + } + break; + case WIFI_STATION: + { + + } + break; + } + + esp_wifi_start(); +} + +void wifi_start() +{ + wifi_init_softap(); +} \ No newline at end of file diff --git a/latest/Firmware/WiFiAdapter/core/wifi.h b/latest/Firmware/WiFiAdapter/core/wifi.h new file mode 100644 index 0000000..e08c967 --- /dev/null +++ b/latest/Firmware/WiFiAdapter/core/wifi.h @@ -0,0 +1,8 @@ +#ifndef __WIFI_H__ +#define __WIFI_H__ + +void wifi_start(); + + + +#endif diff --git a/latest/Firmware/WiFiAdapter/html/CMakeLists.txt b/latest/Firmware/WiFiAdapter/html/CMakeLists.txt new file mode 100644 index 0000000..164a348 --- /dev/null +++ b/latest/Firmware/WiFiAdapter/html/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(EMBED_TXTFILES + "index.html" + ) diff --git a/latest/Firmware/WiFiAdapter/html/index.html b/latest/Firmware/WiFiAdapter/html/index.html new file mode 100644 index 0000000..503ccd1 --- /dev/null +++ b/latest/Firmware/WiFiAdapter/html/index.html @@ -0,0 +1,8 @@ + + + MAIANA Configuration + + +

This is the content

+ + diff --git a/latest/Firmware/WiFiAdapter/main/CMakeLists.txt b/latest/Firmware/WiFiAdapter/main/CMakeLists.txt index 0bbc5b4..56c79f7 100755 --- a/latest/Firmware/WiFiAdapter/main/CMakeLists.txt +++ b/latest/Firmware/WiFiAdapter/main/CMakeLists.txt @@ -2,5 +2,8 @@ idf_component_register(SRCS "MaianaWiFiAdapter.c" INCLUDE_DIRS . ../bsp - REQUIRES app_update + ../core + REQUIRES + app_update + nvs_flash ) diff --git a/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c b/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c index f011910..b9b5483 100755 --- a/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c +++ b/latest/Firmware/WiFiAdapter/main/MaianaWiFiAdapter.c @@ -3,6 +3,9 @@ #include "bsp.h" #include #include "esp_log.h" +#include "nvs_flash.h" +#include "wifi.h" +#include "http_server.h" void uart_rx_cb(char *s) { @@ -27,9 +30,22 @@ 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(); 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); }