Added info and reboot api into the backend. Created the basics for a backend server.

This commit is contained in:
2026-03-03 00:03:24 -05:00
parent 37291557eb
commit 59364ac22d
5 changed files with 248 additions and 47 deletions

View File

@@ -1,14 +1,17 @@
#pragma once
// SDK
#include "cJSON.h"
#include "esp_app_format.h"
#include "esp_chip_info.h"
#include "esp_http_server.h"
#include "esp_ota_ops.h"
#include "esp_system.h"
#include "esp_timer.h"
// Project
#include "appstate.hpp"
#include "types.hpp"
static esp_err_t api_system_info_handler(httpd_req_t *req) {
internal esp_err_t api_system_info_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
@@ -38,14 +41,10 @@ static esp_err_t api_system_info_handler(httpd_req_t *req) {
const esp_app_desc_t *app_desc = esp_app_get_description();
cJSON_AddStringToObject(root, "firmware", app_desc->version);
// Relying on internal variables from main.cpp due to unity build
extern bool ethernetInitialized;
extern bool wifiInitialized;
const char *conn_type = "offline";
if (ethernetInitialized) {
if (g_Ethernet_Initialized) {
conn_type = "ethernet";
} else if (wifiInitialized) {
} else if (g_Wifi_Initialized) {
conn_type = "wifi";
}
cJSON_AddStringToObject(root, "connection", conn_type);
@@ -59,8 +58,8 @@ static esp_err_t api_system_info_handler(httpd_req_t *req) {
return ESP_OK;
}
static const httpd_uri_t api_system_info_uri = {.uri = "/api/system/info",
.method = HTTP_GET,
.handler =
api_system_info_handler,
.user_ctx = NULL};
internal const httpd_uri_t api_system_info_uri = {.uri = "/api/system/info",
.method = HTTP_GET,
.handler =
api_system_info_handler,
.user_ctx = NULL};

View File

@@ -1,14 +1,13 @@
#pragma once
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "types.hpp"
static void restart_timer_callback(void *arg) { esp_restart(); }
internal void restart_timer_callback(void *arg) { esp_restart(); }
static esp_err_t api_system_reboot_handler(httpd_req_t *req) {
internal esp_err_t api_system_reboot_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
@@ -24,18 +23,20 @@ static esp_err_t api_system_reboot_handler(httpd_req_t *req) {
.callback = &restart_timer_callback,
.arg = (void *)0,
.dispatch_method = ESP_TIMER_TASK,
.name = "restart_timer"};
.name = "restart_timer",
.skip_unhandled_events = false};
esp_timer_handle_t restart_timer;
esp_timer_create(&restart_timer_args, &restart_timer);
// Schedule reboot 1 second from now to allow HTTP response to flush
esp_timer_start_once(restart_timer, 1000000);
// Schedule reboot 1 second (in microseconds) from now to allow HTTP response
// to flush
esp_timer_start_once(restart_timer, 1'000'000);
return ESP_OK;
}
static const httpd_uri_t api_system_reboot_uri = {.uri = "/api/system/reboot",
.method = HTTP_POST,
.handler =
api_system_reboot_handler,
.user_ctx = NULL};
internal const httpd_uri_t api_system_reboot_uri = {
.uri = "/api/system/reboot",
.method = HTTP_POST,
.handler = api_system_reboot_handler,
.user_ctx = NULL};