aboutsummaryrefslogtreecommitdiff
path: root/main/http_server.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2025-12-07 16:02:20 -0700
committerJosh Rahm <joshuarahm@gmail.com>2025-12-07 16:02:20 -0700
commit44c7f7a675d2cdb0281322f38be3227ef4fb1df2 (patch)
tree4da919b59129c1447fa9aec45e6cd51bf70dc071 /main/http_server.c
parentc9efee6f3ffcf19e139d72d946aa4d837bbcbb9e (diff)
downloadesp32-ws2812b-44c7f7a675d2cdb0281322f38be3227ef4fb1df2.tar.gz
esp32-ws2812b-44c7f7a675d2cdb0281322f38be3227ef4fb1df2.tar.bz2
esp32-ws2812b-44c7f7a675d2cdb0281322f38be3227ef4fb1df2.zip
Add a "reset defaults" button and handler.
Diffstat (limited to 'main/http_server.c')
-rw-r--r--main/http_server.c33
1 files changed, 31 insertions, 2 deletions
diff --git a/main/http_server.c b/main/http_server.c
index 745bfa6..16a7256 100644
--- a/main/http_server.c
+++ b/main/http_server.c
@@ -15,14 +15,16 @@
ESP_LOGI(TAG, "Handling [%s] (%s)", #attr, req->uri); \
ws_params_t* params = (ws_params_t*)req->user_ctx; \
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); \
+ ws_state_t snapshot; \
if (xSemaphoreTake(params->state_lock, portMAX_DELAY) != pdTRUE) { \
httpd_resp_send_err( \
req, HTTPD_500_INTERNAL_SERVER_ERROR, "lock failed"); \
return ESP_FAIL; \
} \
http_handle(typ)(req, &params->state.attr); \
- ws_state_save(&params->state); \
+ snapshot = params->state; \
xSemaphoreGive(params->state_lock); \
+ ws_state_save(&snapshot); \
httpd_resp_send(req, NULL, 0); \
return ESP_OK; \
} \
@@ -92,17 +94,44 @@ static httpd_uri_t http_params_get_handler = {
.handler = handle_get_params,
.user_ctx = NULL};
+static esp_err_t handle_reset(httpd_req_t* req)
+{
+ httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
+
+ ws_params_t* params = (ws_params_t*)req->user_ctx;
+ ws_state_t snapshot;
+ reset_parameters(params);
+ if (xSemaphoreTake(params->state_lock, portMAX_DELAY) != pdTRUE) {
+ httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "lock failed");
+ return ESP_FAIL;
+ }
+ snapshot = params->state;
+ xSemaphoreGive(params->state_lock);
+
+ ws_state_save(&snapshot);
+ httpd_resp_send(req, NULL, 0);
+ return ESP_OK;
+}
+
+static httpd_uri_t http_reset_handler = {
+ .uri = "/reset",
+ .method = HTTP_POST,
+ .handler = handle_reset,
+ .user_ctx = NULL};
+
httpd_handle_t start_webserver(ws_params_t* params)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.lru_purge_enable = true;
- config.max_uri_handlers = NUMBER_PARAMS + 1;
+ config.max_uri_handlers = NUMBER_PARAMS + 2;
ESP_LOGI(TAG, "Starting httpd server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
http_params_get_handler.user_ctx = params;
+ http_reset_handler.user_ctx = params;
httpd_register_uri_handler(server, &http_params_get_handler);
+ httpd_register_uri_handler(server, &http_reset_handler);
#define STATE_PARAM(typ, attr, ...) \
ESP_LOGI(TAG, "Registering URI handler [%s]", #attr); \