From ea116408890c30e4a266f4ae3cf6898836c7626b Mon Sep 17 00:00:00 2001 From: chopster44 Date: Mon, 1 Dec 2025 21:26:39 +0000 Subject: [PATCH] Successful file transfers, at acceptable speeds --- src/BLEOTA.cpp | 41 +++++++++++++++++++++++++++++++++++++---- src/BLEOTA.h | 6 ++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/BLEOTA.cpp b/src/BLEOTA.cpp index 5108fd4..9ff90dd 100644 --- a/src/BLEOTA.cpp +++ b/src/BLEOTA.cpp @@ -30,10 +30,13 @@ void BLEOTAClass::begin(BLEServer* server, char* versionNumber) { otaService->addCharacteristic(pVersionNumber_); pOTACommandDescriptor_->setValue("OTA Command"); pOTACommand_->addDescriptor(pOTACommandDescriptor_); + pOTACommand_->addDescriptor(new BLE2902()); + pOTACommand_->setCallbacks(cmdCallbacks_); otaService->addCharacteristic(pOTACommand_); pOTAFileDescriptor_->setValue("OTA File"); pOTAFile_->addDescriptor(pOTAFileDescriptor_); + pOTAFile_->setCallbacks(fileCallbacks_); otaService->addCharacteristic(pOTAFile_); otaService->start(); @@ -47,6 +50,18 @@ void BLEOTAClass::setOTAcommand(otaCommand cmd) { pOTACommand_->setValue(commandBuffer, 1); pOTACommand_->notify(); currentCommand_ = cmd; + Serial.printf("Set command to %d\n", commandBuffer[0]); +} + +void BLEOTAClass::sendFileProgress(uint32_t progress) { + uint8_t* commandBuffer = (uint8_t*)malloc(4); + commandBuffer[3] = (progress >> 24) & 0xFF; + commandBuffer[2] = (progress >> 16) & 0xFF; + commandBuffer[1] = (progress >> 8) & 0xFF; + commandBuffer[0] = progress & 0xFF; + pOTACommand_->setValue(commandBuffer, 4); + pOTACommand_->notify(); + Serial.printf("Sent file progress"); } void BLEOTAClass::loop() { @@ -56,6 +71,7 @@ void BLEOTAClass::loop() { } if (state_ == STAGE_DONE) { // reboot + Serial.println("Rebooting"); ESP.restart(); } } @@ -69,6 +85,7 @@ void BLEOTAClass::loop() { BLEOTAClass::OTACommandCallbacks::OTACommandCallbacks(BLEOTAClass* pParent) : pParent_(pParent) {}; void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { + Serial.println("Recieved cmd"); size_t cmdLength = pCharacteristic->getLength(); if (cmdLength > 4 && pParent_->state_ == STAGE_READY) { // error @@ -77,9 +94,9 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi } else if (cmdLength > 1 && pParent_->state_ == STAGE_READY) { // process file size uint8_t* cmdBuffer = pCharacteristic->getData(); - uint32_t cmd = ((uint32_t)cmdBuffer[0] << 24) | - ((uint32_t)cmdBuffer[1] << 16) | - ((uint32_t)cmdBuffer[2] << 8) | (uint32_t)cmdBuffer[3]; + uint32_t cmd = ((uint32_t)cmdBuffer[3] << 24) | + ((uint32_t)cmdBuffer[2] << 16) | + ((uint32_t)cmdBuffer[1] << 8) | (uint32_t)cmdBuffer[0]; pParent_->fileSize_ = cmd; // start update if (!Update.begin(pParent_->fileSize_, U_FLASH)) { @@ -90,7 +107,9 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi return; } pParent_->state_ = STAGE_FLASHING; - } else if (cmdLength == 0) { + pParent_->setOTAcommand(CMD_READY); + Serial.println("Starting to flash"); + } else if (cmdLength == 1) { // process cmd uint8_t cmd = pCharacteristic->getData()[0]; if (cmd == otaCommand::CMD_AGREE) { @@ -98,16 +117,20 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi if (!Update.end()) { // error Serial.println("Error: client and server ready, but Updater is not"); + Update.printError(Serial); pParent_->state_ = STAGE_ERROR; } else { // reboot logic pParent_->state_ = STAGE_DONE; + Serial.println("Set reboot flag"); } } else if (cmd == CMD_DISAGREE) { // process disagree // start over Update.abort(); + Serial.println("Aborting update, app disagree"); pParent_->fileProgress_ = 0; + pParent_->state_ = STAGE_READY; } else { Serial.println("Error: wrong command"); pParent_->state_ = STAGE_ERROR; @@ -127,6 +150,7 @@ BLEOTAClass::OTAFileCallbacks::OTAFileCallbacks(BLEOTAClass* pParent) : pParent_ void BLEOTAClass::OTAFileCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { size_t packetSize = pCharacteristic->getLength(); + Serial.println("Got packet"); if (packetSize == 0) { // problem // output error @@ -140,6 +164,15 @@ void BLEOTAClass::OTAFileCallbacks::onWrite(BLECharacteristic* pCharacteristic, pParent_->state_ = STAGE_ERROR; return; } + pParent_->fileProgress_ += packetSize; + Serial.printf("Packet Size: %d, Progress: %d, Total: %d\n", packetSize, pParent_->fileProgress_, pParent_->fileSize_); + if (pParent_->fileProgress_ >= pParent_->fileSize_) { + // send done + pParent_->setOTAcommand(CMD_DONE); + } else { + pParent_->sendFileProgress(pParent_->fileProgress_); + // pParent_->setOTAcommand(CMD_READY); + } } } diff --git a/src/BLEOTA.h b/src/BLEOTA.h index d2737c3..88eea4f 100644 --- a/src/BLEOTA.h +++ b/src/BLEOTA.h @@ -3,6 +3,7 @@ #include #include +#include #include #define OTA_SERVICE_UUID "71a4438e-fd52-4b15-b3d2-ec0e3e561900" @@ -22,7 +23,7 @@ class BLEOTAClass { void begin(BLEServer* server, char* versionNumber = "1.0.0"); void loop(); - typedef enum otaCommand { + enum otaCommand { CMD_ON = 0x00, CMD_READY = 0x01, CMD_DONE = 0x02, @@ -31,7 +32,7 @@ class BLEOTAClass { CMD_ERROR = 0x0F }; - typedef enum otaStage { + enum otaStage { STAGE_READY, STAGE_FLASHING, STAGE_DONE, @@ -40,6 +41,7 @@ class BLEOTAClass { private: void setOTAcommand(otaCommand cmd); + void sendFileProgress(uint32_t progress); class OTACommandCallbacks : public BLECharacteristicCallbacks { public: