diff options
| Commit | 86243d89d8afc2f9f42127fe9fdb52fad4ae18c3 (patch) | |
|---|---|---|
| Author | 2024-05-19 18:49:41 +0300 | |
| Committer | 2024-05-19 18:49:41 +0300 | |
| Files | 1a579315b5ae9f9abd51d022537365983d068806 | |
| uni-projects-86243d89d8afc2f9f42127fe9fdb52fad4ae18c3.tar uni-projects-86243d89d8afc2f9f42127fe9fdb52fad4ae18c3.tar.gz uni-projects-86243d89d8afc2f9f42127fe9fdb52fad4ae18c3.zip | ||
| Parent | fd85aa16b32abeae6c9a0524f790fb47dff6ac1c (diff) | |
[iot/goam] Cleaned up and simplified webserver config
5 files changed, 101 insertions, 140 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/main.c | 5 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.c | 229 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/webserver.h | 4 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.c | 2 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/wifi.h | 1 |
diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c index e4c976b..ee3e2d4 100644 --- a/Year 3/Internet of things/goam/main/main.c +++ b/Year 3/Internet of things/goam/main/main.c @@ -68,8 +68,9 @@ void app_main(void) printf("Starting up!\n"); wifi_init(); - server_init(); - start_server(); + webserver_init(); + wifi_start(); // Automatically starts server too + ptc06_init(); buttons_init(); diff --git a/Year 3/Internet of things/goam/main/webserver.c b/Year 3/Internet of things/goam/main/webserver.c index 8a633f6..c4eea5b 100644 --- a/Year 3/Internet of things/goam/main/webserver.c +++ b/Year 3/Internet of things/goam/main/webserver.c @@ -12,168 +12,125 @@ #include "esp_tls.h" #include <esp_wifi.h> -static const char *TAG = "example"; +#include "webserver.h" -#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64) - -static httpd_handle_t server; - -/* An HTTP GET handler */ -static esp_err_t hello_get_handler(httpd_req_t *req) -{ - char* buf; - size_t buf_len; +static const char *TAG = "webserver"; - /* Get header value string length and allocate memory for length + 1, - * extra byte for null termination */ - buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1; - if (buf_len > 1) { - buf = malloc(buf_len); - /* Copy null terminated value string into buffer */ - if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) { - ESP_LOGI(TAG, "Found header => Host: %s", buf); - } - free(buf); - } - - buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1; - if (buf_len > 1) { - buf = malloc(buf_len); - if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) { - ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf); - } - free(buf); - } +#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64) - buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1; - if (buf_len > 1) { - buf = malloc(buf_len); - if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) { - ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf); - } - free(buf); - } +static httpd_handle_t server = NULL; - /* Read URL query string length and allocate memory for length + 1, - * extra byte for null termination */ - buf_len = httpd_req_get_url_query_len(req) + 1; - if (buf_len > 1) { - buf = malloc(buf_len); - if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) { - ESP_LOGI(TAG, "Found URL query => %s", buf); - char param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN], dec_param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN] = {0}; - /* Get value of expected key from query string */ - if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) { - ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param); - /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ - /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param); */ - } - if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) { - ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param); - /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ - /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param); */ - } - if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) { - ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param); - /* example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN)); */ - /* ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param); */ - } - } - free(buf); - } +static bool request_get_header(httpd_req_t *req, const char* header_name, char* buf, size_t* buf_len) { + /* char* buf; */ + /* size_t buf_len; */ + /* request_get_header(req, "Host", buf, &buf_len); */ + /* free(buf); */ - /* Set some custom headers */ - httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1"); - httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2"); + /* Get header value string length and allocate memory for length + 1, + * extra byte for null termination */ + *buf_len = httpd_req_get_hdr_value_len(req, header_name) + 1; + if (*buf_len > 1) { + buf = malloc(*buf_len); + /* Copy null terminated value string into buffer */ + if (httpd_req_get_hdr_value_str(req, header_name, buf, *buf_len) == ESP_OK) { + ESP_LOGI(TAG, "Found header => %s: %s", header_name, buf); + return true; + } - /* Send response with custom headers and body set as the - * string passed in user context*/ - const char* resp_str = (const char*) req->user_ctx; - httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); + free(buf); + return false; + } +} - /* After sending the HTTP response the old HTTP request - * headers are lost. Check if HTTP request headers can be read now. */ - if (httpd_req_get_hdr_value_len(req, "Host") == 0) { - ESP_LOGI(TAG, "Request headers lost"); - } - return ESP_OK; +static bool request_get_query(httpd_req_t *req, const char* query_name, char* buf, size_t* buf_len) { + /* Read URL query string length and allocate memory for length + 1, + * extra byte for null termination */ + *buf_len = httpd_req_get_url_query_len(req) + 1; + if (*buf_len > 1) { + buf = malloc(*buf_len); + if (httpd_req_get_url_query_str(req, buf, *buf_len) == ESP_OK) { + ESP_LOGI(TAG, "Found URL query => %s", buf); + char param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN]; + /* Get value of expected key from query string */ + if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) { + ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param); + return true; + } + } + free(buf); + return false; + } } -static const httpd_uri_t hello = { - .uri = "/hello", - .method = HTTP_GET, - .handler = hello_get_handler, - /* Let's pass response string in user - * context to demonstrate it's usage */ - .user_ctx = "Hello World!" -}; -static httpd_handle_t start_webserver() +static esp_err_t get_handler(httpd_req_t *req) { - httpd_handle_t server = NULL; - httpd_config_t config = HTTPD_DEFAULT_CONFIG(); -#if CONFIG_IDF_TARGET_LINUX - // Setting port as 8001 when building for Linux. Port 80 can be used only by a priviliged user in linux. - // So when a unpriviliged user tries to run the application, it throws bind error and the server is not started. - // Port 8001 can be used by an unpriviliged user as well. So the application will not throw bind error and the - // server will be started. - config.server_port = 8001; -#endif // !CONFIG_IDF_TARGET_LINUX - config.lru_purge_enable = true; + /* Set some custom headers */ + /* httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1"); */ - // Start the httpd server - ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); - if (httpd_start(&server, &config) == ESP_OK) { - // Set URI handlers - ESP_LOGI(TAG, "Registering URI handlers"); - httpd_register_uri_handler(server, &hello); - #if CONFIG_EXAMPLE_BASIC_AUTH - httpd_register_basic_auth(server); - #endif - return server; - } + /* Send response with custom headers and body set as the + * string passed in user context*/ + const char resp_str[] = "<html><head><title>Hello</title></head><body>World</body></html>"; + httpd_resp_send(req, resp_str, sizeof(resp_str)); - ESP_LOGI(TAG, "Error starting server!"); - return NULL; + /* After sending the HTTP response the old HTTP request + * headers are lost. Check if HTTP request headers can be read now. */ + if (httpd_req_get_hdr_value_len(req, "Host") == 0) { + ESP_LOGI(TAG, "Request headers lost"); + } + return ESP_OK; } +static const httpd_uri_t get_page = { + .uri = "/*", + .method = HTTP_GET, + .handler = get_handler, + .user_ctx = NULL +}; + /* static void disconnect_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) + int32_t event_id, void* event_data) { - httpd_handle_t* server = (httpd_handle_t*) arg; - if (*server) { - ESP_LOGI(TAG, "Stopping webserver"); - if (stop_webserver(*server) == ESP_OK) { - *server = NULL; - } else { - ESP_LOGE(TAG, "Failed to stop http server"); - } - } + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server) { + ESP_LOGI(TAG, "Stopping webserver"); + if (stop_webserver(*server) == ESP_OK) { + *server = NULL; + } else { + ESP_LOGE(TAG, "Failed to stop http server"); + } + } } */ -static void connect_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) -{ - httpd_handle_t* server = (httpd_handle_t*) arg; - if (*server == NULL) { - ESP_LOGI(TAG, "Starting webserver"); - *server = start_webserver(); - } +static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + if (server == NULL) { + webserver_start(); + } } -void server_init() { - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); - /* ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); */ +void webserver_init() { + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, NULL)); + /* ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); */ } +void webserver_start() +{ + server = NULL; + + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + config.lru_purge_enable = true; + config.uri_match_fn = httpd_uri_match_wildcard; + + ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); + if (httpd_start(&server, &config) != ESP_OK) { + ESP_LOGI(TAG, "Error starting server!"); + return; + } -void start_server() { - server = start_webserver(); + ESP_LOGI(TAG, "Registering URI handlers"); - while (server) { - sleep(5); - } + httpd_register_uri_handler(server, &get_page); } diff --git a/Year 3/Internet of things/goam/main/webserver.h b/Year 3/Internet of things/goam/main/webserver.h index 8d722bf..b4111b1 100644 --- a/Year 3/Internet of things/goam/main/webserver.h +++ b/Year 3/Internet of things/goam/main/webserver.h @@ -1,4 +1,4 @@ #pragma once -void server_init(); -void start_server(); +void webserver_init(); +void webserver_start(); diff --git a/Year 3/Internet of things/goam/main/wifi.c b/Year 3/Internet of things/goam/main/wifi.c index 655bd77..5c76096 100644 --- a/Year 3/Internet of things/goam/main/wifi.c +++ b/Year 3/Internet of things/goam/main/wifi.c @@ -106,6 +106,8 @@ void wifi_init() { wifi_init_events(); wifi_init_static_station(); +} +void wifi_start() { ESP_ERROR_CHECK(esp_wifi_start()); } diff --git a/Year 3/Internet of things/goam/main/wifi.h b/Year 3/Internet of things/goam/main/wifi.h index 8bcc36e..9c23266 100644 --- a/Year 3/Internet of things/goam/main/wifi.h +++ b/Year 3/Internet of things/goam/main/wifi.h @@ -1,3 +1,4 @@ #pragma once void wifi_init(); +void wifi_start(); |
