Add admin serial settings view
This commit is contained in:
+49
-1
@@ -329,6 +329,52 @@ static esp_err_t status_handler(httpd_req_t *request)
|
||||
return error;
|
||||
}
|
||||
|
||||
static esp_err_t serial_settings_handler(httpd_req_t *request)
|
||||
{
|
||||
user_principal_t principal = {0};
|
||||
bool authorized = false;
|
||||
esp_err_t error = authorize_or_respond(request, &principal, &authorized);
|
||||
bool admin = authorized && principal.role == USER_ROLE_ADMIN;
|
||||
secure_wipe(&principal, sizeof(principal));
|
||||
if (error != ESP_OK || !authorized) return error;
|
||||
if (!admin)
|
||||
return send_plain_error(request, "403 Forbidden", "Administrator access required.\n");
|
||||
|
||||
serial_service_snapshot_t snapshot = {0};
|
||||
if (serial_service_get_snapshot(&snapshot) != ESP_OK) {
|
||||
error = httpd_resp_set_hdr(request, "Retry-After", "1");
|
||||
if (error != ESP_OK) return error;
|
||||
return send_plain_error(request, "503 Service Unavailable", "Serial snapshot unavailable.\n");
|
||||
}
|
||||
const serial_config_t *config = &snapshot.config;
|
||||
char response[256];
|
||||
/* Only firmware-owned enum names and numeric values, never CLI output. */
|
||||
int written = snprintf(response, sizeof(response),
|
||||
"{\"running\":%s,\"baud\":%" PRIu32 ",\"data_bits\":\"%s\","
|
||||
"\"parity\":\"%s\",\"stop_bits\":\"%s\",\"flow\":\"%s\","
|
||||
"\"dtr\":\"%s\",\"rts_threshold\":%" PRIu32 "}",
|
||||
snapshot.running ? "true" : "false", config->baud_rate,
|
||||
safe_string(serial_config_data_bits_to_string(config->data_bits)),
|
||||
safe_string(serial_config_parity_to_string(config->parity)),
|
||||
safe_string(serial_config_stop_bits_to_string(config->stop_bits)),
|
||||
safe_string(serial_config_flow_control_to_string(config->flow_control)),
|
||||
safe_string(serial_config_dtr_behavior_to_string(config->dtr_behavior)),
|
||||
config->rts_threshold);
|
||||
if (written < 0 || (size_t)written >= sizeof(response))
|
||||
return send_plain_error(request, "500 Internal Server Error", "Serial response overflow.\n");
|
||||
error = httpd_resp_set_type(request, "application/json; charset=utf-8");
|
||||
if (error == ESP_OK) error = set_common_headers(request);
|
||||
if (error == ESP_OK) error = httpd_resp_send(request, response, (ssize_t)written);
|
||||
if (error != ESP_OK) increment_counter(&s_counters.response_errors);
|
||||
return error;
|
||||
}
|
||||
|
||||
static const httpd_uri_t s_serial_settings_uri = {
|
||||
.uri = "/api/settings/serial",
|
||||
.method = HTTP_GET,
|
||||
.handler = serial_settings_handler,
|
||||
};
|
||||
|
||||
static const httpd_uri_t s_root_uri = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
@@ -517,7 +563,7 @@ esp_err_t web_server_start(void)
|
||||
config.httpd.max_open_sockets = 6;
|
||||
config.httpd.max_uri_handlers =
|
||||
sizeof(s_uri_handlers) / sizeof(s_uri_handlers[0]) +
|
||||
sizeof(s_auth_uris) / sizeof(s_auth_uris[0]) + 2U;
|
||||
sizeof(s_auth_uris) / sizeof(s_auth_uris[0]) + 3U;
|
||||
/* Exhaustion rejects new sockets, never evicts an existing serial writer. */
|
||||
config.httpd.lru_purge_enable = false;
|
||||
config.httpd.recv_wait_timeout = 1;
|
||||
@@ -563,6 +609,8 @@ esp_err_t web_server_start(void)
|
||||
(void)httpd_unregister_uri_handler(server, WEB_ADMIN_TICKET_URI, HTTP_POST);
|
||||
if (admin_error == ESP_OK && web_admin_transport_init() == ESP_OK)
|
||||
admin_transport_owned = web_admin_transport_attach(server) == ESP_OK;
|
||||
/* Optional settings allocation failure must not disable either terminal. */
|
||||
(void)web_httpd_register_optional_get(server, &s_serial_settings_uri);
|
||||
}
|
||||
if (error != ESP_OK) {
|
||||
web_cookie_auth_stop();
|
||||
|
||||
Reference in New Issue
Block a user