|
|
|
|
@@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|