#include #include #include #include #include #include #include "esp_netif.h" #include "esp_tls_crypto.h" #include #include "esp_event.h" #include "esp_netif.h" #include "esp_tls.h" #include #include "webserver.h" #include "spiffs.h" #include "wifi.h" #include "main.h" static const char *TAG = "webserver"; #define MAX_CLIENTS 4 static httpd_handle_t server = NULL; /* * Helpers */ static char* get_value(char* buffer, const char* key, size_t key_size) { // Doesn't handle if key can be found in a value, but thats fine for our cases char* start = strstr(buffer, key); if (start == NULL) return NULL; start += key_size + 1; char* end = strchr(start, '&'); if (end == NULL) return start; *end = '\1'; return start; } /* * Page handlers */ static void logi_get(httpd_req_t *req) { /* 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, "Served %s", req->uri); } else { ESP_LOGE(TAG, "Couldn't serve %s", req->uri); } } static void logi_post(httpd_req_t *req) { /* 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, "Processed %s", req->uri); } else { ESP_LOGE(TAG, "Couldn't process %s", req->uri); } } // Index page is embedded in binary static const unsigned char* index_page = NULL; static size_t index_page_len = 0; static esp_err_t get_handler(httpd_req_t *req) { httpd_resp_send(req, (const char*)index_page, index_page_len); logi_get(req); return ESP_OK; } static const httpd_uri_t get_index = { .uri = "/", .method = HTTP_GET, .handler = get_handler, }; // Index page is embedded in binary static const unsigned char* favicon = NULL; static size_t favicon_len = 0; static esp_err_t get_favicon_handler(httpd_req_t *req) { ESP_ERROR_CHECK(httpd_resp_set_type(req, "image/x-icon")); httpd_resp_send(req, (const char*)favicon, favicon_len); logi_get(req); return ESP_OK; } static const httpd_uri_t get_favicon = { .uri = "/favicon.ico", .method = HTTP_GET, .handler = get_favicon_handler, }; static esp_err_t get_image_handler(httpd_req_t *req) { ESP_ERROR_CHECK(httpd_resp_set_type(req, "image/jpeg")); char* resp_str = NULL; size_t length = spiffs_read(req->uri, &resp_str); httpd_resp_send(req, resp_str, length); free(resp_str); logi_get(req); return ESP_OK; } static const httpd_uri_t get_image = { .uri = SPIFFS_PREFIX "/*", .method = HTTP_GET, .handler = get_image_handler, }; ; const char ssid_param[] = "ssid"; const char pass_param[] = "pass"; const char imagesave_param[] = "imagesave"; const char resolution_param[] = "resolution"; const char invertion_param[] = "invertion"; const char timelapse_period_param[] = "timelapse_period"; const char timelapse_change_param[] = "timelapse_change"; const char remote_photo_param[] = "remote_photo"; static void main_store_request(const char* value, void (*main_store)(int32_t)) { if (*value == '%') value++; main_store((int32_t)strtol(value, NULL, 16)); } static esp_err_t post_config_handler(httpd_req_t *req) { // Set buffer char* buffer = malloc(req->content_len + 1); httpd_req_recv(req, buffer, req->content_len); buffer[req->content_len] = '\0'; // just in case // Fix spaces for (char* iter = buffer; *iter != '\0'; iter++) { if (*iter == '+') *iter = ' '; } // Get values char* remote_photo = get_value(buffer, remote_photo_param, sizeof(remote_photo_param)-1); char* ssid = get_value(buffer, ssid_param, sizeof(ssid_param)-1); char* pass = get_value(buffer, pass_param, sizeof(pass_param)-1); char* imagesave = get_value(buffer, imagesave_param, sizeof(imagesave_param)-1); char* resolution = get_value(buffer, resolution_param, sizeof(resolution_param)-1); char* invertion = get_value(buffer, invertion_param, sizeof(invertion_param)-1); char* timelapse_period = get_value(buffer, timelapse_period_param, sizeof(timelapse_period_param)-1); char* timelapse_change = get_value(buffer, timelapse_change_param, sizeof(timelapse_change_param)-1); // Fix terminating zero for (char* iter = buffer; *iter != '\0'; iter++) { if (*iter == '\1') *iter = '\0'; } // Value logic if (remote_photo != NULL) { main_force_photo(); } if (ssid != NULL && pass != NULL) { wifi_store_network(ssid, pass); } if (imagesave != NULL) { main_store_request(imagesave, main_store_imagesave); } if (resolution != NULL) { main_store_request(resolution, main_store_resolution); } if (invertion != NULL) { main_store_request(invertion, main_store_invertion); } if (timelapse_period != NULL) { main_store_timelapse_period((int32_t)atoi(timelapse_period)); } if (timelapse_change != NULL) { if (*timelapse_change == '0') { main_stop_timelapse(); } else if (*timelapse_change == '1') { main_start_timelapse(); } } // Final free(buffer); httpd_resp_send(req, "", 0); logi_post(req); return ESP_OK; } static const httpd_uri_t post_config = { .uri = "/config", .method = HTTP_POST, .handler = post_config_handler, }; static esp_err_t ws_camera_handler(httpd_req_t *req) { if (req->method == HTTP_GET) { ESP_LOGI(TAG, "Established websocket connection"); return ESP_OK; } // Nobody should send websocket data, it's only for receiving return ESP_OK; } static const httpd_uri_t ws_camera = { .uri = "/camera", .method = HTTP_GET, .handler = ws_camera_handler, .is_websocket = true, }; /* * On network connection/disconnect */ static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { webserver_start(); } /* static void disconnect_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) { ESP_LOGI(TAG, "Stopping webserver"); if (stop_webserver(*server) == ESP_OK) { *server = NULL; } else { ESP_LOGE(TAG, "Failed to stop http server"); } } } */ /* * Public interface */ #define SET_EMBEDDED_PTR(var_store, var_size, var, conv_filename) \ extern const unsigned char var ## _start[] asm("_binary_" conv_filename "_start"); \ extern const unsigned char var ## _end[] asm("_binary_" conv_filename "_end"); \ var_store = (var ## _start); var_size = (var ## _end - var ## _start); 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)); */ SET_EMBEDDED_PTR(index_page, index_page_len, index_page, "index_html"); SET_EMBEDDED_PTR(favicon, favicon_len, favicon, "favicon_ico"); } void webserver_start() { if (server != NULL) { ESP_LOGW(TAG, "Tried starting the server while it was already started"); return; } httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT(); config.httpd.lru_purge_enable = true; config.httpd.uri_match_fn = httpd_uri_match_wildcard; config.httpd.max_open_sockets = MAX_CLIENTS; SET_EMBEDDED_PTR(config.servercert, config.servercert_len, cacert, "cacert_pem"); SET_EMBEDDED_PTR(config.prvtkey_pem, config.prvtkey_len, prvtkey, "prvtkey_pem"); if (httpd_ssl_start(&server, &config) != ESP_OK) { ESP_LOGE(TAG, "Couldn't start server"); server = NULL; return; } httpd_register_uri_handler(server, &get_index); httpd_register_uri_handler(server, &get_favicon); httpd_register_uri_handler(server, &get_image); httpd_register_uri_handler(server, &post_config); httpd_register_uri_handler(server, &ws_camera); ESP_LOGI(TAG, "Started server on port %d", config.httpd.server_port); } static httpd_ws_frame_t empty_value = { .fragmented = false, .type = HTTPD_WS_TYPE_BINARY, .payload = NULL, .len = 0, }; void ws_send_bin(const char* msg, size_t size) { httpd_ws_frame_t value = { .fragmented = false, .type = HTTPD_WS_TYPE_BINARY, .payload = (unsigned char*)msg, .len = size, }; size_t clients = MAX_CLIENTS; int client_fds[MAX_CLIENTS]; ESP_ERROR_CHECK(httpd_get_client_list(server, &clients, client_fds)); for (size_t i = 0; i < clients; i++) { if (httpd_ws_get_fd_info(server, client_fds[i]) != HTTPD_WS_CLIENT_WEBSOCKET) continue; ESP_LOGI(TAG, "Sending websocket data to fd:%d", client_fds[i]); httpd_ws_send_data(server, client_fds[i], &value); httpd_ws_send_data(server, client_fds[i], &empty_value); } }