From b591e354428d69efd10b7913a51117a05cc177ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 8 Oct 2022 11:10:29 +0200 Subject: [PATCH 1/3] Switch to OTA firmware --- src/modules/AdminModule.cpp | 12 +++++++++ src/platform/esp32/BleOta.cpp | 46 +++++++++++++++++++++++++++++++++++ src/platform/esp32/BleOta.h | 18 ++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/platform/esp32/BleOta.cpp create mode 100644 src/platform/esp32/BleOta.h diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index b2ed44a05..a3c9a9865 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -2,6 +2,7 @@ #include "Channels.h" #include "MeshService.h" #include "NodeDB.h" +#include "BleOta.h" #include "Router.h" #include "configuration.h" #include "main.h" @@ -103,6 +104,17 @@ bool AdminModule::handleReceivedProtobuf(const MeshPacket &mp, AdminMessage *r) rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000); break; } + case AdminMessage_reboot_ota_seconds_tag: { + int32_t s = r->reboot_ota_seconds; + if (BleOta::getOtaAppVersion().isEmpty()) { + DEBUG_MSG("No OTA firmware available, just rebooting\n"); + }else{ + BleOta::switchToOtaApp(); + } + DEBUG_MSG("Rebooting to OTA in %d seconds\n", s); + rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000); + break; + } case AdminMessage_shutdown_seconds_tag: { int32_t s = r->shutdown_seconds; DEBUG_MSG("Shutdown in %d seconds\n", s); diff --git a/src/platform/esp32/BleOta.cpp b/src/platform/esp32/BleOta.cpp new file mode 100644 index 000000000..8062eede6 --- /dev/null +++ b/src/platform/esp32/BleOta.cpp @@ -0,0 +1,46 @@ +#include "Arduino.h" +#include "BleOta.h" +#include + +static const String MESHTASTIC_OTA_APP_PROJECT_NAME("Meshtastic-OTA"); + +const esp_partition_t* BleOta::findEspOtaAppPartition() { + const esp_partition_t *part + = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, nullptr); + + esp_app_desc_t app_desc; + esp_err_t ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); + + if (ret != ESP_OK || MESHTASTIC_OTA_APP_PROJECT_NAME != app_desc.project_name) { + part + = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, + nullptr); + ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); + } + + if (ret == ESP_OK && MESHTASTIC_OTA_APP_PROJECT_NAME == app_desc.project_name) { + return part; + } else { + return nullptr; + } +} + +String BleOta::getOtaAppVersion() { + const esp_partition_t *part = findEspOtaAppPartition(); + esp_app_desc_t app_desc; + esp_err_t ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); + String version; + if (ret == ESP_OK) { + version = app_desc.version; + } + return version; +} + +bool BleOta::switchToOtaApp() { + bool success = false; + const esp_partition_t *part = findEspOtaAppPartition(); + if (part) { + success = (ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_set_boot_partition(part)) == ESP_OK); + } + return success; +} \ No newline at end of file diff --git a/src/platform/esp32/BleOta.h b/src/platform/esp32/BleOta.h new file mode 100644 index 000000000..4c598f36d --- /dev/null +++ b/src/platform/esp32/BleOta.h @@ -0,0 +1,18 @@ +#ifndef BLEOTA_H +#define BLEOTA_H + +#include + +class BleOta { + public: + explicit BleOta() {}; + + static String getOtaAppVersion(); + static bool switchToOtaApp(); + + private: + String mUserAgent; + static const esp_partition_t *findEspOtaAppPartition(); +}; + +#endif //BLEOTA_H \ No newline at end of file From 30b1bd85d9e50efeac51defc9b20f3cef775f372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 9 Oct 2022 11:19:33 +0200 Subject: [PATCH 2/3] fix build for non-esp32 --- src/modules/AdminModule.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index a3c9a9865..63c0454c1 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -2,7 +2,9 @@ #include "Channels.h" #include "MeshService.h" #include "NodeDB.h" +#ifdef ARCH_ESP32 #include "BleOta.h" +#endif #include "Router.h" #include "configuration.h" #include "main.h" From 20eaddee581c3339ea4c4405fb320bbd89de24ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 9 Oct 2022 11:35:19 +0200 Subject: [PATCH 3/3] - fix conditional include for nRF52 (no OTA there) - fix compiler warning in Canned Messages --- src/modules/AdminModule.cpp | 8 ++++++-- src/modules/CannedMessageModule.cpp | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 63c0454c1..883ed0ac3 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -108,12 +108,16 @@ bool AdminModule::handleReceivedProtobuf(const MeshPacket &mp, AdminMessage *r) } case AdminMessage_reboot_ota_seconds_tag: { int32_t s = r->reboot_ota_seconds; +#ifdef ARCH_ESP32 if (BleOta::getOtaAppVersion().isEmpty()) { - DEBUG_MSG("No OTA firmware available, just rebooting\n"); + DEBUG_MSG("No OTA firmware available, scheduling regular reboot in %d seconds\n", s); }else{ BleOta::switchToOtaApp(); + DEBUG_MSG("Rebooting to OTA in %d seconds\n", s); } - DEBUG_MSG("Rebooting to OTA in %d seconds\n", s); +#else + DEBUG_MSG("Not on ESP32, scheduling regular reboot in %d seconds\n", s); +#endif rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000); break; } diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index a21b63779..29ea20257 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -292,7 +292,7 @@ int32_t CannedMessageModule::runOnce() if(this->dest == NODENUM_BROADCAST) { this->dest = nodeDB.getNodeNum(); } - for (int i = 0; i < numNodes; i++) { + for (unsigned int i = 0; i < numNodes; i++) { if (nodeDB.getNodeByIndex(i)->num == this->dest) { this->dest = (i > 0) ? nodeDB.getNodeByIndex(i-1)->num : nodeDB.getNodeByIndex(numNodes-1)->num; break; @@ -313,7 +313,7 @@ int32_t CannedMessageModule::runOnce() if(this->dest == NODENUM_BROADCAST) { this->dest = nodeDB.getNodeNum(); } - for (int i = 0; i < numNodes; i++) { + for (unsigned int i = 0; i < numNodes; i++) { if (nodeDB.getNodeByIndex(i)->num == this->dest) { this->dest = (i < numNodes-1) ? nodeDB.getNodeByIndex(i+1)->num : nodeDB.getNodeByIndex(0)->num; break;