aboutsummaryrefslogtreecommitdiff
path: root/main/http_server.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2022-12-01 22:44:20 -0700
committerJosh Rahm <joshuarahm@gmail.com>2022-12-01 22:44:20 -0700
commit10a5077234e9b76dc100b9029a9e2d71801e7242 (patch)
tree13e42a3c91f1672c6ce7b6daba776366aa71ef85 /main/http_server.c
parent05e3f6bb7293c771629c7b3599392c8f335e7dd8 (diff)
downloadesp32-ws2812b-main.tar.gz
esp32-ws2812b-main.tar.bz2
esp32-ws2812b-main.zip
Run clang-formatHEADmain
Diffstat (limited to 'main/http_server.c')
-rw-r--r--main/http_server.c121
1 files changed, 72 insertions, 49 deletions
diff --git a/main/http_server.c b/main/http_server.c
index e4c426c..0345324 100644
--- a/main/http_server.c
+++ b/main/http_server.c
@@ -19,9 +19,11 @@ static void handle_set_int(httpd_req_t* req, int* val)
char param[32];
if (httpd_query_key_value(buf, "add", param, sizeof(param)) == ESP_OK) {
*val += atoi(param);
- } else if (httpd_query_key_value(buf, "set", param, sizeof(param)) == ESP_OK) {
+ } else if (
+ httpd_query_key_value(buf, "set", param, sizeof(param)) == ESP_OK) {
*val = atoi(param);
- } else if (httpd_query_key_value(buf, "sub", param, sizeof(param)) == ESP_OK) {
+ } else if (
+ httpd_query_key_value(buf, "sub", param, sizeof(param)) == ESP_OK) {
*val -= atoi(param);
} else {
ESP_LOGI(TAG, "No valid parameters.");
@@ -90,29 +92,34 @@ static void handle_set_uint32_t(httpd_req_t* req, uint32_t* val)
*val = tmp;
}
-#define STATE_PARAM(typ, attr, ...) \
- static esp_err_t handle_set_ ## attr (httpd_req_t *req) { \
- ESP_LOGI(TAG, "Handling [%s] (%s)", #attr, req->uri); \
- ws_params_t* params = (ws_params_t*) req->user_ctx; \
+#define STATE_PARAM(typ, attr, ...) \
+ static esp_err_t handle_set_##attr(httpd_req_t* req) \
+ { \
+ 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", "*"); \
- handle_set_ ## typ (req, &params->state. attr); \
- httpd_resp_send(req, NULL, 0); \
- return ESP_OK; \
- } \
- static httpd_uri_t http_ ## attr ## _handler = { \
- .uri = "/" #attr, \
- .method = HTTP_POST, \
- .handler = handle_set_ ## attr, \
- .user_ctx = NULL \
- };
+ handle_set_##typ(req, &params->state.attr); \
+ httpd_resp_send(req, NULL, 0); \
+ return ESP_OK; \
+ } \
+ static httpd_uri_t http_##attr##_handler = { \
+ .uri = "/" #attr, \
+ .method = HTTP_POST, \
+ .handler = handle_set_##attr, \
+ .user_ctx = NULL};
#include "state_params.i"
#undef STATE_PARAM
static size_t write_uint32_t_value(
char** out, size_t len, const char* attr, const char* dn, uint32_t value)
{
- size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"uint32_t\",\"value\":\"%u\",\"disp\":\"%s\"}",
- attr, (unsigned int) value, dn);
+ size_t newlen = snprintf(
+ *out,
+ len,
+ "\"%s\":{\"type\":\"uint32_t\",\"value\":\"%u\",\"disp\":\"%s\"}",
+ attr,
+ (unsigned int)value,
+ dn);
*out += newlen;
return len - newlen;
}
@@ -120,8 +127,13 @@ static size_t write_uint32_t_value(
static size_t write_uint8_t_value(
char** out, size_t len, const char* attr, const char* dn, uint8_t value)
{
- size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"uint8_t\",\"value\":\"%u\",\"disp\":\"%s\"}",
- attr, (unsigned int)value, dn);
+ size_t newlen = snprintf(
+ *out,
+ len,
+ "\"%s\":{\"type\":\"uint8_t\",\"value\":\"%u\",\"disp\":\"%s\"}",
+ attr,
+ (unsigned int)value,
+ dn);
*out += newlen;
return len - newlen;
}
@@ -129,8 +141,13 @@ static size_t write_uint8_t_value(
static size_t write_int_value(
char** out, size_t len, const char* attr, const char* dn, int value)
{
- size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"int\",\"value\":\"%d\",\"disp\":\"%s\"}",
- attr, value, dn);
+ size_t newlen = snprintf(
+ *out,
+ len,
+ "\"%s\":{\"type\":\"int\",\"value\":\"%d\",\"disp\":\"%s\"}",
+ attr,
+ value,
+ dn);
*out += newlen;
return len - newlen;
}
@@ -138,47 +155,55 @@ static size_t write_int_value(
static size_t write_bool_value(
char** out, size_t len, const char* attr, const char* dn, bool value)
{
- size_t newlen = snprintf(*out, len, "\"%s\":{\"type\":\"bool\",\"value\":\"%s\",\"disp\":\"%s\"}",
- attr, value ? "on" : "off", dn);
+ size_t newlen = snprintf(
+ *out,
+ len,
+ "\"%s\":{\"type\":\"bool\",\"value\":\"%s\",\"disp\":\"%s\"}",
+ attr,
+ value ? "on" : "off",
+ dn);
*out += newlen;
return len - newlen;
}
static esp_err_t handle_get_params(httpd_req_t* req)
{
- httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); \
+ httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
size_t len = NUMBER_PARAMS * 128;
char out_buffer[len + 1];
out_buffer[len] = 0;
char* out_ptr = out_buffer;
- ws_params_t* ws_params = (ws_params_t*) req->user_ctx;
+ ws_params_t* ws_params = (ws_params_t*)req->user_ctx;
bool write_comma = false;
httpd_resp_set_type(req, "application/json");
if (len > 1) {
*out_ptr = '{';
- out_ptr ++;
- len --;
+ out_ptr++;
+ len--;
}
-#define STATE_PARAM(typ, attr, display, ...) \
- if (write_comma && len > 1) { \
- *(out_ptr++) = ','; \
- len --; \
- } \
- len = write_ ## typ ## _value( \
- &out_ptr, len, #attr, display, ws_params->state. attr); \
+#define STATE_PARAM(typ, attr, display, ...) \
+ if (write_comma && len > 1) { \
+ *(out_ptr++) = ','; \
+ len--; \
+ } \
+ len = write_##typ##_value( \
+ &out_ptr, len, #attr, display, ws_params->state.attr); \
write_comma = true;
#include "state_params.i"
#undef STATE_PARAM
if (len > 3) {
- *(out_ptr++) = '}'; len --;
- *(out_ptr++) = '\r'; len --;
- *(out_ptr++) = '\n'; len --;
+ *(out_ptr++) = '}';
+ len--;
+ *(out_ptr++) = '\r';
+ len--;
+ *(out_ptr++) = '\n';
+ len--;
}
if (len > 1) {
*(out_ptr++) = 0;
- len --;
+ len--;
}
httpd_resp_set_status(req, HTTPD_200);
@@ -188,11 +213,10 @@ static esp_err_t handle_get_params(httpd_req_t* req)
}
static httpd_uri_t http_params_get_handler = {
- .uri = "/params",
- .method = HTTP_GET,
- .handler = handle_get_params,
- .user_ctx = NULL
-};
+ .uri = "/params",
+ .method = HTTP_GET,
+ .handler = handle_get_params,
+ .user_ctx = NULL};
httpd_handle_t start_webserver(ws_params_t* params)
{
@@ -203,14 +227,13 @@ httpd_handle_t start_webserver(ws_params_t* params)
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;
httpd_register_uri_handler(server, &http_params_get_handler);
-#define STATE_PARAM(typ, attr, ...) \
- ESP_LOGI(TAG, "Registering URI handler [%s]", #attr); \
- http_ ## attr ## _handler.user_ctx = params; \
- httpd_register_uri_handler(server, & http_ ## attr ## _handler);
+#define STATE_PARAM(typ, attr, ...) \
+ ESP_LOGI(TAG, "Registering URI handler [%s]", #attr); \
+ http_##attr##_handler.user_ctx = params; \
+ httpd_register_uri_handler(server, &http_##attr##_handler);
#include "state_params.i"
#undef STATE_PARAM