diff --git a/components/RestAPI/CMakeLists.txt b/components/RestAPI/CMakeLists.txt new file mode 100644 index 0000000..4037f21 --- /dev/null +++ b/components/RestAPI/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register(SRCS "RestAPI/RestAPI.cpp" + INCLUDE_DIRS "RestAPI" + # REQUIRES CommandManager mongoose + REQUIRES mongoose +) \ No newline at end of file diff --git a/components/RestAPI/RestAPI/RestAPI.cpp b/components/RestAPI/RestAPI/RestAPI.cpp new file mode 100644 index 0000000..5780de3 --- /dev/null +++ b/components/RestAPI/RestAPI/RestAPI.cpp @@ -0,0 +1,85 @@ +#include "RestAPI.hpp" + +RestAPI::RestAPI(std::string url) +{ + this->url = url; + // updates + routes.emplace("/api/update/wifi/", &RestAPI::handle_wifi); + routes.emplace("/api/update/device/", &RestAPI::handle_update_device); + routes.emplace("/api/update/camera/", &RestAPI::handle_update_camera); + + // post will reset it + // resets + routes.emplace("/api/reset/config/", &RestAPI::handle_reset_config); + routes.emplace("/api/reset/wifi/", &RestAPI::handle_reset_wifi_config); + routes.emplace("/api/reset/txpower/", &RestAPI::handle_reset_txpower_config); + routes.emplace("/api/reset/camera/", &RestAPI::handle_reset_camera_config); + + // gets + routes.emplace("/api/get/config/", &RestAPI::handle_get_config); + routes.emplace("/api/reboot/", &RestAPI::handle_reboot); + routes.emplace("/api/reboot/", &RestAPI::handle_reboot); + + // reboots + routes.emplace("/api/reboot/device/", &RestAPI::handle_reboot); + routes.emplace("/api/reboot/camera/", &RestAPI::handle_camera_reboot); + + // heartbeat + routes.emplace("/api/ping/", &RestAPI::pong); + + // special + routes.emplace("/api/save/", &RestAPI::handle_save); +} + +void RestAPI::begin() +{ + mg_log_set(MG_LL_DEBUG); + mg_mgr_init(&mgr); + // every route is handled through this class, with commands themselves by a command manager + // hence we pass a pointer to this in mg_http_listen + mg_http_listen(&mgr, this->url.c_str(), (mg_event_handler_t)RestAPIHelpers::event_handler, this); +} + +void RestAPI::handle_request(struct mg_connection *connection, int event, void *event_data) +{ + if (event == MG_EV_HTTP_MSG) + { + struct mg_http_message *message = (struct mg_http_message *)event_data; + std::string uri = std::string(message->uri.buf, message->uri.len); + + bool found = false; + for (auto it = this->routes.begin(); it != this->routes.end(); it++) + { + if (it->first == uri) + { + found = true; + RequestContext *context = new RequestContext{ + .connection = connection, + }; + auto method = it->second; + (*this.*method)(context); + } + } + + if (!found) + { + mg_http_reply(connection, 404, "", "Wrong URL"); + } + } +} + +void RestAPIHelpers::event_handler(struct mg_connection *connection, int event, void *event_data) +{ + RestAPI *rest_api_handler = static_cast(connection->fn_data); + rest_api_handler->handle_request(connection, event, event_data); +} + +void RestAPI::poll() +{ + mg_mgr_poll(&mgr, 100); +} + +void RestAPI::handle_reboot(RequestContext *context) +{ + mg_http_reply(context->connection, 200, "", "Yes sir"); +} \ No newline at end of file diff --git a/components/RestAPI/RestAPI/RestAPI.hpp b/components/RestAPI/RestAPI/RestAPI.hpp new file mode 100644 index 0000000..da0725b --- /dev/null +++ b/components/RestAPI/RestAPI/RestAPI.hpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +#include "esp_log.h" + +struct RequestContext +{ + mg_connection *connection; +}; + +class RestAPI +{ + using route_handler = void (RestAPI::*)(RequestContext *); + typedef std::unordered_map route_map; + std::string url; + route_map routes; + + struct mg_mgr mgr; + +private: + void handle_reboot(RequestContext *context); + +public: + // this will also need command manager + RestAPI(std::string url); + void begin(); + void handle_request(struct mg_connection *connection, int event, void *event_data); + void poll(); +}; + +namespace RestAPIHelpers +{ + void event_handler(struct mg_connection *connection, int event, void *event_data); +}; \ No newline at end of file diff --git a/components/StreamServer/StreamServer/StreamServer.hpp b/components/StreamServer/StreamServer/StreamServer.hpp index 244b7b7..49eb89b 100644 --- a/components/StreamServer/StreamServer/StreamServer.hpp +++ b/components/StreamServer/StreamServer/StreamServer.hpp @@ -9,7 +9,7 @@ #include "esp_http_server.h" #include "esp_timer.h" #include -#include +#include #include extern WebSocketLogger webSocketLogger; diff --git a/dependencies.lock b/dependencies.lock index 35a5d5e..c626075 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -1,6 +1,6 @@ dependencies: espressif/cmake_utilities: - component_hash: 351350613ceafba240b761b4ea991e0f231ac7a9f59a9ee901f751bddc0bb18f + component_hash: 7e20811092150b668a710cfbf43164721b23bac766811f4e68036450a3265fba dependencies: - name: idf require: private @@ -8,18 +8,19 @@ dependencies: source: registry_url: https://components.espressif.com/ type: service - version: 0.5.3 + version: 1.0.0 espressif/esp32-camera: - component_hash: 154da98781f3ee98f6dbbdd3aaec97497051fb1a570c300fc0fd3a946874a02e + component_hash: a82de4ee0b383bd34695935385c6a6c720129084580c2bbb55dce76eb2a3788f dependencies: [] source: registry_url: https://components.espressif.com/ type: service - version: 2.0.12 + version: 2.0.13 espressif/led_strip: component_hash: 28c6509a727ef74925b372ed404772aeedf11cce10b78c3f69b3c66799095e2d dependencies: - name: idf + registry_url: https://components.espressif.com require: private version: '>=4.4' source: @@ -62,6 +63,7 @@ dependencies: require: private version: '>=0.15.0~10' - name: idf + registry_url: https://components.espressif.com require: private version: '>=5.0' source: diff --git a/main/openiris_main.cpp b/main/openiris_main.cpp index 51b84ba..78b75e0 100644 --- a/main/openiris_main.cpp +++ b/main/openiris_main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -35,6 +36,7 @@ WiFiManager wifiManager(deviceConfig); MDNSManager mdnsManager(deviceConfig); CameraManager cameraHandler(deviceConfig); StreamServer streamServer(80); +RestAPI restAPI("http://0.0.0.0:81"); #ifdef CONFIG_WIRED_MODE UVCStreamManager uvcStream; @@ -97,6 +99,7 @@ extern "C" void app_main(void) deviceConfig.load(); wifiManager.Begin(); mdnsManager.start(); + restAPI.begin(); cameraHandler.setupCamera(); streamServer.startStreamServer(); @@ -107,6 +110,7 @@ extern "C" void app_main(void) while (1) { ledManager.handleLED(); + restAPI.poll(); vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); } }