[OTA] Free memory by deleting BT tasks when launching OTA (#1573)

* [OTA] Free memory by deleting BT taskswhen launching OTA

and restart if fail
Also factorize restart function for ESP
This commit is contained in:
Florian authored and GitHub committed 2023-03-24 10:12:08 -05:00
1 parent d1e9e8bf9b
commit 338fc43825
2 files changed
+21 -48

No files matched your search

+5 -7
View File
@@ -801,12 +801,11 @@ void stopProcessing() {
Log.notice(F("Stop BLE processing" CR)); Log.notice(F("Stop BLE processing" CR));
ProcessLock = true; ProcessLock = true;
delay(BTConfig.scanDuration < 2000 ? BTConfig.scanDuration : 2000); delay(BTConfig.scanDuration < 2000 ? BTConfig.scanDuration : 2000);
} //Suspending and deleting tasks to free memory for OTA operations
vTaskSuspend(xCoreTaskHandle);
void startProcessing() { vTaskDelete(xCoreTaskHandle);
Log.notice(F("Start BLE processing" CR)); vTaskSuspend(xProcBLETaskHandle);
ProcessLock = false; vTaskDelete(xProcBLETaskHandle);
vTaskResume(xCoreTaskHandle);
} }
void coreTask(void* pvParameters) { void coreTask(void* pvParameters) {
@@ -847,7 +846,6 @@ void coreTask(void* pvParameters) {
} }
} else { } else {
Log.trace(F("BLE core task canceled by processLock" CR)); Log.trace(F("BLE core task canceled by processLock" CR));
vTaskSuspend(xCoreTaskHandle);
} }
} }
} }
+16 -41
View File
@@ -1068,10 +1068,8 @@ void setOTA() {
last_ota_activity_millis = 0; last_ota_activity_millis = 0;
ErrorIndicatorOFF(); ErrorIndicatorOFF();
SendReceiveIndicatorOFF(); SendReceiveIndicatorOFF();
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
lpDisplayPrint("OTA done"); lpDisplayPrint("OTA done");
ESPRestart();
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100))); Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
@@ -1081,9 +1079,6 @@ void setOTA() {
last_ota_activity_millis = millis(); last_ota_activity_millis = millis();
ErrorIndicatorOFF(); ErrorIndicatorOFF();
SendReceiveIndicatorOFF(); SendReceiveIndicatorOFF();
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
Serial.printf("Error[%u]: ", error); Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) if (error == OTA_AUTH_ERROR)
Log.error(F("Auth Failed" CR)); Log.error(F("Auth Failed" CR));
@@ -1095,6 +1090,7 @@ void setOTA() {
Log.error(F("Receive Failed" CR)); Log.error(F("Receive Failed" CR));
else if (error == OTA_END_ERROR) else if (error == OTA_END_ERROR)
Log.error(F("End Failed" CR)); Log.error(F("End Failed" CR));
ESPRestart();
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
} }
@@ -1148,6 +1144,14 @@ void setupTLS(bool self_signed, uint8_t index) {
} }
#endif #endif
void ESPRestart() {
#if defined(ESP32)
ESP.restart();
#elif defined(ESP8266)
ESP.reset();
#endif
}
#if defined(ESPWifiManualSetup) #if defined(ESPWifiManualSetup)
void setup_wifi() { void setup_wifi() {
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
@@ -1814,9 +1818,6 @@ void syncNTP() {
if (count >= 60) { if (count >= 60) {
Log.error(F("Unable to update - invalid time" CR)); Log.error(F("Unable to update - invalid time" CR));
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
return; return;
} }
} }
@@ -2276,20 +2277,14 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
# ifndef ESPWifiManualSetup # ifndef ESPWifiManualSetup
saveMqttConfig(); saveMqttConfig();
# endif # endif
# ifdef ESP32 ESPRestart();
ESP.restart();
# elif ESP8266
ESP.reset();
# endif
break; break;
} }
SendReceiveIndicatorOFF(); SendReceiveIndicatorOFF();
ErrorIndicatorOFF(); ErrorIndicatorOFF();
# if defined(ZgatewayBT) && defined(ESP32) ESPRestart();
startProcessing();
# endif
} }
} }
} }
@@ -2303,11 +2298,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
const char* cmd = SYSdata["cmd"]; const char* cmd = SYSdata["cmd"];
Log.notice(F("Command: %s" CR), cmd); Log.notice(F("Command: %s" CR), cmd);
if (strstr(cmd, restartCmd) != NULL) { //restart if (strstr(cmd, restartCmd) != NULL) { //restart
# if defined(ESP8266) ESPRestart();
ESP.reset();
# else
ESP.restart();
# endif
} else if (strstr(cmd, eraseCmd) != NULL) { //erase and restart } else if (strstr(cmd, eraseCmd) != NULL) { //erase and restart
# ifndef ESPWifiManualSetup # ifndef ESPWifiManualSetup
setup_wifimanager(true); setup_wifimanager(true);
@@ -2340,17 +2331,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower)) # if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
setESP32WifiPorotocolTxPower(); setESP32WifiPorotocolTxPower();
# endif # endif
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
# if defined(ESP8266)
ESP.reset();
# else
ESP.restart();
# endif
}
} }
# if defined(ZgatewayBT) && defined(ESP32) ESPRestart();
startProcessing();
# endif
} }
# ifdef MQTTsetMQTT # ifdef MQTTsetMQTT
@@ -2442,9 +2424,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
} }
connectMQTT(); connectMQTT();
} }
# if defined(ZgatewayBT) && defined(ESP32) ESPRestart();
startProcessing();
# endif
} }
# endif # endif
if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) { if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) {
@@ -2517,10 +2497,5 @@ String toString(uint32_t input) {
*/ */
void watchdogReboot(byte reason) { void watchdogReboot(byte reason) {
Log.warning(F("Rebooting for reason code %d" CR), reason); Log.warning(F("Rebooting for reason code %d" CR), reason);
#if defined(ESP32) ESPRestart();
ESP.restart();
#elif defined(ESP8266)
ESP.reset();
#else // Insert other architectures here
#endif
} }