diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDispalyOTA.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDispalyOTA.cpp new file mode 100644 index 0000000..d73541a --- /dev/null +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDispalyOTA.cpp @@ -0,0 +1,70 @@ +#include + +ESPMegaDisplayOTA::ESPMegaDisplayOTA(ESPMegaDisplay *display, ESPMegaWebServer *webServer) { + this->display = display; + this->server = webServer->getServer(); + char ota_begin_path[100]; + char ota_write_path[100]; + char ota_end_path[100]; + sprintf(ota_begin_path, "%s/ota/begin", base_path); + sprintf(ota_write_path, "%s/ota/write", base_path); + sprintf(ota_end_path, "%s/ota/end", base_path); + this->otaUpdateBeginWebHandler = new AsyncCallbackJsonWebHandler(ota_begin_path, std::bind(&ESPMegaDisplayOTA::otaUpdateBeginHandler, this, std::placeholders::_1, std::placeholders::_2)); + this->otaUpdateWriteWebHandler = new AsyncCallbackJsonWebHandler(ota_write_path, std::bind(&ESPMegaDisplayOTA::otaUpdateWriteHandler, this, std::placeholders::_1, std::placeholders::_2)); + this->otaUpdateEndWebHandler = new AsyncCallbackJsonWebHandler(ota_end_path, std::bind(&ESPMegaDisplayOTA::otaUpdateEndHandler, this, std::placeholders::_1, std::placeholders::_2)); +} + +void ESPMegaDisplayOTA::begin(const char* base_path) { + this->server->addHandler(this->otaUpdateBeginWebHandler); + this->server->addHandler(this->otaUpdateWriteWebHandler); + this->server->addHandler(this->otaUpdateEndWebHandler); +} + +void ESPMegaDisplayOTA::otaUpdateBeginHandler(AsyncWebServerRequest *request, JsonVariant &json) { + // The content type of the request is application/json + // The body of the request is a JSON object with the following field: + // - size: the size of the update + + // Parse the JSON object + JsonObject content = json.as(); + // Check if the size field is present + if(!content.containsKey("size")) + return; + // Get the size field + this->updateSize = content["size"].as(); + // Begin the update + if(!this->display->beginUpdate(this->updateSize)) { + // If the update cannot be started, return an error + request->send(500, "application/json", "{\"status\": \"error\"}"); + } else { + // If the update can be started, return a success + request->send(200, "application/json", "{\"status\": \"success\"}"); + } + +} +void ESPMegaDisplayOTA::otaUpdateWriteHandler(AsyncWebServerRequest *request, JsonVariant &json) { + // The content type of the request is application/json + // The body of the request is a JSON object with the following field: + // - size: the size of the update in bytes + // - data: the data to write + + //Parse the JSON object + JsonObject content = json.as(); + // Check if the size and data fields are present + if(!content.containsKey("size") || !content.containsKey("data")) + request->send(500, "application/json", "{\"status\": \"error\", \"message\": \"The size or data field is missing\"}"); + return; + // Get the size field + size_t size = content["size"].as(); + if(size>4096) { + // If the size is greater than 4096 bytes, return an error + request->send(500, "application/json", "{\"status\": \"error\", \"message\": \"The size of the update is too big\"}"); + return; + } + // Get the data field + JsonArray data = content["data"].as(); + // Write the data +} +void ESPMegaDisplayOTA::otaUpdateEndHandler(AsyncWebServerRequest *request, JsonVariant &json) { + +} diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp index 5110b9e..64b7672 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp @@ -624,7 +624,7 @@ void ESPMegaDisplay::giveSerialMutex() * @param size The size of the update. * @return True if the OTA update is started, false otherwise. */ -bool ESPMegaDisplay::beginOTA(size_t size) +bool ESPMegaDisplay::beginUpdate(size_t size) { if (xSemaphoreTake(this->serialMutex, DISPLAY_MUTEX_TAKE_TIMEOUT) == pdFALSE) { @@ -658,7 +658,7 @@ bool ESPMegaDisplay::beginOTA(size_t size) { if (this->displayAdapter->read() == 0x05) { - ESP_LOGV("ESPMegaDisplay", "LCD OTA Subroutine is ready to receive data."); + ESP_LOGV("ESPMegaDisplay", "LCD Update Subroutine is ready to receive data."); return true; } } @@ -666,22 +666,22 @@ bool ESPMegaDisplay::beginOTA(size_t size) // FLush the serial recieve buffer while (this->displayAdapter->available()) this->displayAdapter->read(); - ESP_LOGE("ESPMegaDisplay", "LCD OTA Subroutine failed to initialize."); + ESP_LOGE("ESPMegaDisplay", "LCD Update Subroutine failed to initialize."); return false; } /** - * @brief Writes data to the display during an OTA update. + * @brief Writes data to the display during an update. * @param data The data to write. * @param size The size of the data. * @return True if the data is written, false otherwise. */ -bool ESPMegaDisplay::writeOTA(uint8_t *data, size_t size) +bool ESPMegaDisplay::writeUpdate(uint8_t *data, size_t size) { // Check if the data size is too large if(size>4096) { - ESP_LOGE("ESPMegaDisplay", "LCD OTA Subroutine failed to write data, data size is too large."); + ESP_LOGE("ESPMegaDisplay", "LCD Update Subroutine failed to write data, data size is too large."); return false; } // Flush the serial recieve buffer @@ -699,19 +699,19 @@ bool ESPMegaDisplay::writeOTA(uint8_t *data, size_t size) { if (this->displayAdapter->read() == 0x05) { - ESP_LOGV("ESPMegaDisplay", "LCD OTA Subroutine is ready to receive data."); + ESP_LOGV("ESPMegaDisplay", "LCD Update Subroutine is ready to receive data."); return true; } } } - ESP_LOGE("ESPMegaDisplay", "LCD OTA Subroutine failed to write data."); + ESP_LOGE("ESPMegaDisplay", "LCD Update Subroutine failed to write data."); return false; } /** - * @brief Ends an LCD OTA update. + * @brief Ends an LCD update. */ -void ESPMegaDisplay::endOTA() +void ESPMegaDisplay::endUpdate() { xSemaphoreGive(this->serialMutex); this->reset(); diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp index ce0ef88..0010ed5 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp @@ -37,9 +37,9 @@ class ESPMegaDisplay void unregisterPayloadCallback(uint16_t handle); void takeSerialMutex(); void giveSerialMutex(); - bool beginOTA(size_t size); - bool writeOTA(uint8_t* data, size_t size); - void endOTA(); + bool beginUpdate(size_t size); + bool writeUpdate(uint8_t* data, size_t size); + void endUpdate(); protected: SemaphoreHandle_t serialMutex; uint8_t currentPage; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplayOTA.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplayOTA.hpp new file mode 100644 index 0000000..83a8b95 --- /dev/null +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplayOTA.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +class ESPMegaDisplayOTA { + public: + ESPMegaDisplayOTA(ESPMegaDisplay *display, ESPMegaWebServer *webServer); + void begin(const char* base_path); + private: + AsyncCallbackJsonWebHandler *otaUpdateBeginWebHandler; + AsyncCallbackJsonWebHandler *otaUpdateWriteWebHandler; + AsyncCallbackJsonWebHandler *otaUpdateEndWebHandler; + void otaUpdateBeginHandler(AsyncWebServerRequest *request, JsonVariant &json); + void otaUpdateWriteHandler(AsyncWebServerRequest *request, JsonVariant &json); + void otaUpdateEndHandler(AsyncWebServerRequest *request, JsonVariant &json); + AsyncWebServer *server; + ESPMegaDisplay *display; + size_t updateSize; + size_t updateProgress; + StaticJsonDocument<512> contentJSON; +}; \ No newline at end of file