Successful file transfers, at acceptable speeds

This commit is contained in:
2025-12-01 21:26:39 +00:00
parent 7f8c320b5a
commit ea11640889
2 changed files with 41 additions and 6 deletions

View File

@@ -30,10 +30,13 @@ void BLEOTAClass::begin(BLEServer* server, char* versionNumber) {
otaService->addCharacteristic(pVersionNumber_); otaService->addCharacteristic(pVersionNumber_);
pOTACommandDescriptor_->setValue("OTA Command"); pOTACommandDescriptor_->setValue("OTA Command");
pOTACommand_->addDescriptor(pOTACommandDescriptor_); pOTACommand_->addDescriptor(pOTACommandDescriptor_);
pOTACommand_->addDescriptor(new BLE2902());
pOTACommand_->setCallbacks(cmdCallbacks_);
otaService->addCharacteristic(pOTACommand_); otaService->addCharacteristic(pOTACommand_);
pOTAFileDescriptor_->setValue("OTA File"); pOTAFileDescriptor_->setValue("OTA File");
pOTAFile_->addDescriptor(pOTAFileDescriptor_); pOTAFile_->addDescriptor(pOTAFileDescriptor_);
pOTAFile_->setCallbacks(fileCallbacks_);
otaService->addCharacteristic(pOTAFile_); otaService->addCharacteristic(pOTAFile_);
otaService->start(); otaService->start();
@@ -47,6 +50,18 @@ void BLEOTAClass::setOTAcommand(otaCommand cmd) {
pOTACommand_->setValue(commandBuffer, 1); pOTACommand_->setValue(commandBuffer, 1);
pOTACommand_->notify(); pOTACommand_->notify();
currentCommand_ = cmd; 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() { void BLEOTAClass::loop() {
@@ -56,6 +71,7 @@ void BLEOTAClass::loop() {
} }
if (state_ == STAGE_DONE) { if (state_ == STAGE_DONE) {
// reboot // reboot
Serial.println("Rebooting");
ESP.restart(); ESP.restart();
} }
} }
@@ -69,6 +85,7 @@ void BLEOTAClass::loop() {
BLEOTAClass::OTACommandCallbacks::OTACommandCallbacks(BLEOTAClass* pParent) : pParent_(pParent) {}; BLEOTAClass::OTACommandCallbacks::OTACommandCallbacks(BLEOTAClass* pParent) : pParent_(pParent) {};
void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
Serial.println("Recieved cmd");
size_t cmdLength = pCharacteristic->getLength(); size_t cmdLength = pCharacteristic->getLength();
if (cmdLength > 4 && pParent_->state_ == STAGE_READY) { if (cmdLength > 4 && pParent_->state_ == STAGE_READY) {
// error // error
@@ -77,9 +94,9 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi
} else if (cmdLength > 1 && pParent_->state_ == STAGE_READY) { } else if (cmdLength > 1 && pParent_->state_ == STAGE_READY) {
// process file size // process file size
uint8_t* cmdBuffer = pCharacteristic->getData(); uint8_t* cmdBuffer = pCharacteristic->getData();
uint32_t cmd = ((uint32_t)cmdBuffer[0] << 24) | uint32_t cmd = ((uint32_t)cmdBuffer[3] << 24) |
((uint32_t)cmdBuffer[1] << 16) | ((uint32_t)cmdBuffer[2] << 16) |
((uint32_t)cmdBuffer[2] << 8) | (uint32_t)cmdBuffer[3]; ((uint32_t)cmdBuffer[1] << 8) | (uint32_t)cmdBuffer[0];
pParent_->fileSize_ = cmd; pParent_->fileSize_ = cmd;
// start update // start update
if (!Update.begin(pParent_->fileSize_, U_FLASH)) { if (!Update.begin(pParent_->fileSize_, U_FLASH)) {
@@ -90,7 +107,9 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi
return; return;
} }
pParent_->state_ = STAGE_FLASHING; pParent_->state_ = STAGE_FLASHING;
} else if (cmdLength == 0) { pParent_->setOTAcommand(CMD_READY);
Serial.println("Starting to flash");
} else if (cmdLength == 1) {
// process cmd // process cmd
uint8_t cmd = pCharacteristic->getData()[0]; uint8_t cmd = pCharacteristic->getData()[0];
if (cmd == otaCommand::CMD_AGREE) { if (cmd == otaCommand::CMD_AGREE) {
@@ -98,16 +117,20 @@ void BLEOTAClass::OTACommandCallbacks::onWrite(BLECharacteristic* pCharacteristi
if (!Update.end()) { if (!Update.end()) {
// error // error
Serial.println("Error: client and server ready, but Updater is not"); Serial.println("Error: client and server ready, but Updater is not");
Update.printError(Serial);
pParent_->state_ = STAGE_ERROR; pParent_->state_ = STAGE_ERROR;
} else { } else {
// reboot logic // reboot logic
pParent_->state_ = STAGE_DONE; pParent_->state_ = STAGE_DONE;
Serial.println("Set reboot flag");
} }
} else if (cmd == CMD_DISAGREE) { } else if (cmd == CMD_DISAGREE) {
// process disagree // process disagree
// start over // start over
Update.abort(); Update.abort();
Serial.println("Aborting update, app disagree");
pParent_->fileProgress_ = 0; pParent_->fileProgress_ = 0;
pParent_->state_ = STAGE_READY;
} else { } else {
Serial.println("Error: wrong command"); Serial.println("Error: wrong command");
pParent_->state_ = STAGE_ERROR; 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) { void BLEOTAClass::OTAFileCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
size_t packetSize = pCharacteristic->getLength(); size_t packetSize = pCharacteristic->getLength();
Serial.println("Got packet");
if (packetSize == 0) { if (packetSize == 0) {
// problem // problem
// output error // output error
@@ -140,6 +164,15 @@ void BLEOTAClass::OTAFileCallbacks::onWrite(BLECharacteristic* pCharacteristic,
pParent_->state_ = STAGE_ERROR; pParent_->state_ = STAGE_ERROR;
return; 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);
}
} }
} }

View File

@@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <BLEServer.h> #include <BLEServer.h>
#include <BLE2902.h>
#include <Update.h> #include <Update.h>
#define OTA_SERVICE_UUID "71a4438e-fd52-4b15-b3d2-ec0e3e561900" #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 begin(BLEServer* server, char* versionNumber = "1.0.0");
void loop(); void loop();
typedef enum otaCommand { enum otaCommand {
CMD_ON = 0x00, CMD_ON = 0x00,
CMD_READY = 0x01, CMD_READY = 0x01,
CMD_DONE = 0x02, CMD_DONE = 0x02,
@@ -31,7 +32,7 @@ class BLEOTAClass {
CMD_ERROR = 0x0F CMD_ERROR = 0x0F
}; };
typedef enum otaStage { enum otaStage {
STAGE_READY, STAGE_READY,
STAGE_FLASHING, STAGE_FLASHING,
STAGE_DONE, STAGE_DONE,
@@ -40,6 +41,7 @@ class BLEOTAClass {
private: private:
void setOTAcommand(otaCommand cmd); void setOTAcommand(otaCommand cmd);
void sendFileProgress(uint32_t progress);
class OTACommandCallbacks : public BLECharacteristicCallbacks { class OTACommandCallbacks : public BLECharacteristicCallbacks {
public: public: