Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_server: Add a way to set raw NVS parameters #380

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 68 additions & 24 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,10 @@ static esp_err_t rest_common_get_handler(httpd_req_t * req)
return ESP_OK;
}

static esp_err_t PATCH_update_swarm(httpd_req_t * req)
static esp_err_t recv_http_req(httpd_req_t * req, char * const buf)
{
// Set CORS headers
if (set_cors_headers(req) != ESP_OK) {
httpd_resp_send_500(req);
return ESP_FAIL;
}

int total_len = req->content_len;
int cur_len = 0;
char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch;
int received = 0;
if (total_len >= SCRATCH_BUFSIZE) {
/* Respond with 500 Internal Server Error */
Expand All @@ -221,6 +214,22 @@ static esp_err_t PATCH_update_swarm(httpd_req_t * req)
}
buf[total_len] = '\0';

return ESP_OK;
}

static esp_err_t PATCH_update_swarm(httpd_req_t * req)
{
// Set CORS headers
if (set_cors_headers(req) != ESP_OK) {
httpd_resp_send_500(req);
return ESP_FAIL;
}

char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch;
if (ESP_OK != recv_http_req(req, buf)) {
return ESP_FAIL;
}

nvs_config_set_string(NVS_CONFIG_SWARM, buf);
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
Expand Down Expand Up @@ -248,25 +257,10 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
return ESP_FAIL;
}

int total_len = req->content_len;
int cur_len = 0;
char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch;
int received = 0;
if (total_len >= SCRATCH_BUFSIZE) {
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
if (ESP_OK != recv_http_req(req, buf)) {
return ESP_FAIL;
}
while (cur_len < total_len) {
received = httpd_req_recv(req, buf + cur_len, total_len);
if (received <= 0) {
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to post control value");
return ESP_FAIL;
}
cur_len += received;
}
buf[total_len] = '\0';

cJSON * root = cJSON_Parse(buf);
cJSON * item;
Expand Down Expand Up @@ -333,6 +327,44 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
return ESP_OK;
}

static esp_err_t PATCH_update_raw(httpd_req_t * req)
{
// Set CORS headers
if (set_cors_headers(req) != ESP_OK) {
httpd_resp_send_500(req);
return ESP_FAIL;
}

char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch;
if (ESP_OK != recv_http_req(req, buf)) {
return ESP_FAIL;
}

cJSON * root = cJSON_Parse(buf);
cJSON * item;
cJSON_ArrayForEach(item, root) {
cJSON * const type_j = cJSON_GetObjectItem(item, "type");
if (!type_j) continue;
const char * const type = type_j->valuestring;
cJSON * const val = cJSON_GetObjectItem(item, "val");
if (!val) continue;

if (!strcmp(type, "str")) {
nvs_config_set_string(item->string, val->valuestring);
} else if (!strcmp(type, "u16")) {
nvs_config_set_u16(item->string, val->valueint);
} else if (!strcmp(type, "u64")) {
nvs_config_set_u64(item->string, val->valuedouble);
} else if (!strcmp(type, "erase")) {
nvs_config_erase(item->string);
}
}

cJSON_Delete(root);
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}

static esp_err_t POST_restart(httpd_req_t * req)
{
ESP_LOGI(TAG, "Restarting System because of API Request");
Expand Down Expand Up @@ -760,6 +792,18 @@ esp_err_t start_rest_server(void * pvParameters)
};
httpd_register_uri_handler(server, &system_options_uri);

httpd_uri_t update_system_raw_uri = {
.uri = "/api/system/raw", .method = HTTP_PATCH, .handler = PATCH_update_raw, .user_ctx = rest_context};
httpd_register_uri_handler(server, &update_system_raw_uri);

httpd_uri_t system_raw_options_uri = {
.uri = "/api/system/raw",
.method = HTTP_OPTIONS,
.handler = handle_options_request,
.user_ctx = NULL,
};
httpd_register_uri_handler(server, &system_raw_options_uri);

httpd_uri_t update_post_ota_firmware = {
.uri = "/api/system/OTA", .method = HTTP_POST, .handler = POST_OTA_update, .user_ctx = NULL};
httpd_register_uri_handler(server, &update_post_ota_firmware);
Expand Down
18 changes: 18 additions & 0 deletions main/nvs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ void nvs_config_set_u64(const char * key, const uint64_t value)
}
nvs_close(handle);
}

void nvs_config_erase(const char * key)
{

nvs_handle handle;
esp_err_t err;
err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Could not open nvs");
return;
}

err = nvs_erase_key(handle, key);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Could not erase nvs key: %s", key);
}
nvs_close(handle);
}
1 change: 1 addition & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ uint16_t nvs_config_get_u16(const char * key, const uint16_t default_value);
void nvs_config_set_u16(const char * key, const uint16_t value);
uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value);
void nvs_config_set_u64(const char * key, const uint64_t value);
void nvs_config_erase(const char * key);

#endif // MAIN_NVS_CONFIG_H