diff --git a/include/DebugTrace.h b/include/DebugTrace.h index aff80087..64cea4be 100644 --- a/include/DebugTrace.h +++ b/include/DebugTrace.h @@ -17,15 +17,17 @@ typedef struct { time_t heap_min_time; uint32_t backtrace[CONFIG_RESTART_DEBUG_STACK_DEPTH]; } re_restart_debug_t; +__NOINIT_ATTR static int8_t bootloop_panic_count; +void IRAM_ATTR debugUpdate(); #endif // RESTART_DEBUG_INFO -__NOINIT_ATTR static int8_t bootloop_panic_count; + extern "C" void __real_esp_panic_handler(void*); void printDebugTrace(); void sendDebugTraceAndFreeMemory(bool); -void IRAM_ATTR debugUpdate(); +void startWatchDog(); extern "C" bool verifyRollbackLater(); void verifyFirmware(); \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 6e651962..deeeddc3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -869,6 +869,7 @@ build_src_filter = [env:tiny_fromitems] lib_deps = + plerup/EspSoftwareSerial build_src_filter = + + @@ -878,5 +879,10 @@ build_src_filter = + + + + + + + + + + + + + + diff --git a/src/DebugTrace.cpp b/src/DebugTrace.cpp index 0f4112db..e4e39adc 100644 --- a/src/DebugTrace.cpp +++ b/src/DebugTrace.cpp @@ -1,6 +1,6 @@ #include "DebugTrace.h" #if defined(RESTART_DEBUG_INFO) && defined(ESP32) && !defined(esp32c3m_4mb) -//#ifdef RESTART_DEBUG_INFO +// #ifdef RESTART_DEBUG_INFO __NOINIT_ATTR static re_restart_debug_t _debug_info; #include "esp_debug_helpers.h" @@ -9,7 +9,6 @@ __NOINIT_ATTR static re_restart_debug_t _debug_info; #include "esp_err.h" #include "soc/soc_memory_layout.h" #include "soc/cpu.h" -#include "esp_ota_ops.h" // RU: Размер буфера для конвертации даты и времeни в строку #define CONFIG_FORMAT_STRFTIME_BUFFER_SIZE 32 @@ -213,12 +212,11 @@ void printDebugTrace() Serial.printf(CONFIG_MESSAGE_TG_VERSION_DEF, jsonReadStr(settingsFlashJson, F("name")), ESP_getResetReason().c_str(), ESP32GetResetReason(0).c_str(), ESP32GetResetReason(1).c_str()); } - - Serial.println(INFO_MESSAGE_DEBUG); + Serial.println(INFO_MESSAGE_DEBUG); } -void sendDebugTraceAndFreeMemory( bool postMsg) +void sendDebugTraceAndFreeMemory(bool postMsg) { // esp_register_shutdown_handler(debugUpdate); re_restart_debug_t debug = debugGet(); @@ -265,27 +263,25 @@ void sendDebugTraceAndFreeMemory( bool postMsg) } free(debug_heap); } -/* else - { - // Serial.println("DEVICE START"); - // Serial.printf(CONFIG_MESSAGE_TG_VERSION_DEF, - // FIRMWARE_VERSION, ESP_getResetReason().c_str(), ESP32GetResetReason(0).c_str(), ESP32GetResetReason(1).c_str()); - if (tlgrmItem && isNetworkActive()) + /* else { - char *msg; - msg = malloc_stringf(CONFIG_MESSAGE_TG_VERSION_DEF, - WiFi.localIP().toString(), FIRMWARE_VERSION, ESP_getResetReason().c_str(), ESP32GetResetReason(0).c_str(), ESP32GetResetReason(1).c_str()); - tlgrmItem->sendTelegramMsg(false, String(msg)); - free(msg); - } - };*/ - + // Serial.println("DEVICE START"); + // Serial.printf(CONFIG_MESSAGE_TG_VERSION_DEF, + // FIRMWARE_VERSION, ESP_getResetReason().c_str(), ESP32GetResetReason(0).c_str(), ESP32GetResetReason(1).c_str()); + if (tlgrmItem && isNetworkActive()) + { + char *msg; + msg = malloc_stringf(CONFIG_MESSAGE_TG_VERSION_DEF, + WiFi.localIP().toString(), FIRMWARE_VERSION, ESP_getResetReason().c_str(), ESP32GetResetReason(0).c_str(), ESP32GetResetReason(1).c_str()); + tlgrmItem->sendTelegramMsg(false, String(msg)); + free(msg); + } + };*/ } - -#else +#else // RESTART_DEBUG_INFO void printDebugTrace() {} void sendDebugTraceAndFreeMemory(bool) {} -void IRAM_ATTR debugUpdate() {} +//void IRAM_ATTR debugUpdate() {} extern "C" void __wrap_esp_panic_handler(void *info) { // Call the original panic handler function to finish processing this error (creating a core dump for example...) @@ -293,33 +289,60 @@ extern "C" void __wrap_esp_panic_handler(void *info) } #endif // RESTART_DEBUG_INFO +#if defined(ESP32) - extern "C" bool verifyRollbackLater(){ - Serial.printf("verifyRollbackLater OVERRIDDEN FUNCTION!"); - return true; - } +#include "esp_ota_ops.h" - void verifyFirmware(){ - Serial.printf("[SYSTEM] - Checking firmware...\n"); - const esp_partition_t *running = esp_ota_get_running_partition(); - esp_ota_img_states_t ota_state; - if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) { - const char* otaState = ota_state == ESP_OTA_IMG_NEW ? "ESP_OTA_IMG_NEW" - : ota_state == ESP_OTA_IMG_PENDING_VERIFY ? "ESP_OTA_IMG_PENDING_VERIFY" - : ota_state == ESP_OTA_IMG_VALID ? "ESP_OTA_IMG_VALID" - : ota_state == ESP_OTA_IMG_INVALID ? "ESP_OTA_IMG_INVALID" - : ota_state == ESP_OTA_IMG_ABORTED ? "ESP_OTA_IMG_ABORTED" - : "ESP_OTA_IMG_UNDEFINED"; - Serial.printf( "[System] - Ota state: %s\n",otaState); +#include +// 3 seconds WDT, reset in 1 seconds +#define WDT_TIMEOUT 10 - if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) { - if (esp_ota_mark_app_valid_cancel_rollback() == ESP_OK) { - Serial.printf( "[System] - App is valid, rollback cancelled successfully\n"); - } else { - Serial.printf("[System] - Failed to cancel rollback\n"); - } - } - }else{ - Serial.printf("[System] - OTA partition has no record in OTA data\n"); +void startWatchDog() +{ + esp_task_wdt_init(WDT_TIMEOUT, true); // enable panic so ESP32 restarts + esp_task_wdt_add(NULL); // add current thread to WDT watch +} + +extern "C" bool verifyRollbackLater() +{ + Serial.printf("verifyRollbackLater OVERRIDDEN FUNCTION!"); + return true; +} + +void verifyFirmware() +{ + Serial.printf("[SYSTEM] - Checking firmware...\n"); + const esp_partition_t *running = esp_ota_get_running_partition(); + esp_ota_img_states_t ota_state; + if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) + { + const char *otaState = ota_state == ESP_OTA_IMG_NEW ? "ESP_OTA_IMG_NEW" + : ota_state == ESP_OTA_IMG_PENDING_VERIFY ? "ESP_OTA_IMG_PENDING_VERIFY" + : ota_state == ESP_OTA_IMG_VALID ? "ESP_OTA_IMG_VALID" + : ota_state == ESP_OTA_IMG_INVALID ? "ESP_OTA_IMG_INVALID" + : ota_state == ESP_OTA_IMG_ABORTED ? "ESP_OTA_IMG_ABORTED" + : "ESP_OTA_IMG_UNDEFINED"; + Serial.printf("[System] - Ota state: %s\n", otaState); + + if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) + { + if (esp_ota_mark_app_valid_cancel_rollback() == ESP_OK) + { + Serial.printf("[System] - App is valid, rollback cancelled successfully\n"); } - } \ No newline at end of file + else + { + Serial.printf("[System] - Failed to cancel rollback\n"); + } + } + } + else + { + Serial.printf("[System] - OTA partition has no record in OTA data\n"); + } +} +#else //ESP32 +void startWatchDog() {} +//extern "C" bool verifyRollbackLater() {} +void verifyFirmware() {} +#endif \ No newline at end of file diff --git a/src/DeviceList.cpp b/src/DeviceList.cpp index 937ba315..eab59655 100644 --- a/src/DeviceList.cpp +++ b/src/DeviceList.cpp @@ -38,7 +38,7 @@ AsyncUDP asyncUdp; void udpListningInit() { #if defined(LIBRETINY) - if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210/* , WiFi.localIP() */)) { + if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210 , WiFi.localIP() )) { #else if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210)) { #endif diff --git a/src/Main.cpp b/src/Main.cpp index 4984cde1..5b42ddfc 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -7,6 +7,9 @@ #include #endif #include "DebugTrace.h" +#if defined(ESP32) +#include +#endif #if defined(esp32s2_4mb) || defined(esp32s3_16mb) #include #endif @@ -101,11 +104,13 @@ void setup() { Serial.begin(115200); Serial.flush(); //----------- Отладка EXCEPTION (функции с заглушками для отключения) --------- +#if defined(RESTART_DEBUG_INFO) && defined(ESP32) && !defined(esp32c3m_4mb) //Привязка коллбэк функции для вызова при перезагрузке esp_register_shutdown_handler(debugUpdate); +#endif // RESTART_DEBUG_INFO // Печать или оправка отладочной информации printDebugTrace(); - + startWatchDog(); Serial.println(); Serial.println(F("--------------started----------------")); @@ -151,7 +156,7 @@ void setup() { #endif SerialPrint("i", "i2c", F("i2c pins overriding done")); } - +#if defined(RESTART_DEBUG_INFO) && defined(ESP32) && !defined(esp32c3m_4mb) if (bootloop_panic_count >= 3) { resetSettingsFlashByPanic(); @@ -162,7 +167,7 @@ void setup() { SerialPrint("E", "CORE", F("CONFIG and SCENARIO reset !!!")); bootloop_panic_count = 0; } - +#endif // RESTART_DEBUG_INFO // настраиваем микроконтроллер configure("/config.json"); @@ -228,6 +233,11 @@ void setup() { // настраиваем секундные обслуживания системы ts.add( TIMES, 1000, [&](void *) { + // сброс WDT +#if defined(ESP32) + SerialPrint("i", "Task", "Resetting WDT..."); + esp_task_wdt_reset(); +#endif // сохраняем значения IoTItems в файл каждую секунду, если были изменения (установлены маркеры на сохранение) if (needSaveValues) { syncValuesFlashJson(); @@ -248,7 +258,9 @@ void setup() { Serial.println("--------test end---------"); stopErrorMarker(SETUPLAST_ERRORMARKER); +#if defined(RESTART_DEBUG_INFO) && defined(ESP32) && !defined(esp32c3m_4mb) bootloop_panic_count = 0; +#endif // RESTART_DEBUG_INFO } void loop() { diff --git a/src/NTP.cpp b/src/NTP.cpp index 185c6269..fe5ba80f 100644 --- a/src/NTP.cpp +++ b/src/NTP.cpp @@ -8,7 +8,7 @@ #include "utils/SerialPrint.h" void ntpInit() { -#if defined(USE_LIBRETINY) +#if defined(LIBRETINY) if (sntp_enabled()) { sntp_stop(); } @@ -23,6 +23,7 @@ void ntpInit() { ts.add( TIME, 1000, [&](void*) { unixTime = getSystemTime(); + //SerialPrint("I", F("NTP"), "TIME " + String(unixTime)); if (unixTime < MIN_DATETIME) { isTimeSynch = false; // SerialPrint("E", "NTP", "Time not synched"); @@ -62,8 +63,9 @@ void synchTime() { // force resync if (sntp_enabled()) { sntp_stop(); + } sntp_init(); - } + #else configTime(0, 0, "pool.ntp.org", "ru.pool.ntp.org", jsonReadStr(settingsFlashJson, F("ntp")).c_str()); #endif diff --git a/src/StandWebServer.cpp b/src/StandWebServer.cpp index 1e97c246..759ae8fe 100644 --- a/src/StandWebServer.cpp +++ b/src/StandWebServer.cpp @@ -599,57 +599,52 @@ void handleFileList() { if (path != "/" && !FileFS.exists(path)) { return replyBadRequest("BAD PATH"); } +FileFS.open(path.c_str()); + lfs_dir_t dir; + struct lfs_info info; + int err = lfs_dir_open(FileFS.getFS(), &dir, path.c_str()); + if (err) { + HTTP.send(500, "text/plain", "FAIL OPEN DIR"); + return; + } + String output = "["; + while (true) { + int res = lfs_dir_read(FileFS.getFS(), &dir, &info); + if (res < 0) { + lfs_dir_close(FileFS.getFS(), &dir); + return ; + } + + if (!res) { + break; + } + + Serial.printf("%s %d", info.name, info.type); -lfs_dir_t dir; -struct lfs_info info; -int err = lfs_dir_open(&lfs, &dir, path); -if (err) { - HTTP.send(500, "text/plain", "FAIL OPEN DIR"); - return; -} -String output = "["; -while (true) { - int res = lfs_dir_read(&lfs, &dir, &info); - if (res < 0) { - lfs_dir_close(&lfs, &dir); - return err; - } - - if (!res) { - break; - } - - Serial.printf("%s %d", info.name, info.type); + if (output != "[") { + output += ','; + } + output += "{\"type\":\""; + // output += (file.isDirectory()) ? "dir" : "file"; + if (info.type == LFS_TYPE_DIR) { + output += "dir"; + } else { + output += F("file\",\"size\":\""); + output += info.size; + } + + output += "\",\"name\":\""; + output += String(info.name); + output += "\"}"; + //file = root.openNextFile(); - if (output != "[") { - output += ','; - } - output += "{\"type\":\""; - // output += (file.isDirectory()) ? "dir" : "file"; - if (info.type == LFS_TYPE_DIR) { - output += "dir"; - } else { - output += F("file\",\"size\":\""); - output += info.size; } - output += "\",\"name\":\""; - output += String(info.name); - output += "\"}"; - //file = root.openNextFile(); - -} - -err = lfs_dir_close(&lfs, &dir); -if (err) { - HTTP.send(500, "text/plain", "FAIL CLOSE DIR"); - return; -} - - - - - + err = lfs_dir_close(FileFS.getFS(), &dir); + if (err) { + HTTP.send(500, "text/plain", "FAIL CLOSE DIR"); + return; + } output += "]"; HTTP.send(200, "text/json", output); diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 5dd27955..c37672b5 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -5,43 +5,15 @@ void* getAPI_Loging(String subtype, String params); void* getAPI_LogingDaily(String subtype, String params); void* getAPI_IoTMath(String subtype, String params); void* getAPI_owmWeather(String subtype, String params); -void* getAPI_Ping(String subtype, String params); void* getAPI_Timer(String subtype, String params); void* getAPI_Variable(String subtype, String params); void* getAPI_VButton(String subtype, String params); -void* getAPI_A02Distance(String subtype, String params); -void* getAPI_Acs712(String subtype, String params); -void* getAPI_AhtXX(String subtype, String params); void* getAPI_AnalogAdc(String subtype, String params); void* getAPI_BL0937(String subtype, String params); -void* getAPI_Bme280(String subtype, String params); -void* getAPI_Bmp280(String subtype, String params); -void* getAPI_Dht1122(String subtype, String params); -void* getAPI_Ds18b20(String subtype, String params); -void* getAPI_Impulse(String subtype, String params); -void* getAPI_MQgas(String subtype, String params); -void* getAPI_Pzem004_v2(String subtype, String params); -void* getAPI_RTC(String subtype, String params); -void* getAPI_S8(String subtype, String params); -void* getAPI_Sht20(String subtype, String params); -void* getAPI_Sht30(String subtype, String params); -void* getAPI_Sonar(String subtype, String params); void* getAPI_UART(String subtype, String params); void* getAPI_AnalogBtn(String subtype, String params); void* getAPI_ButtonIn(String subtype, String params); void* getAPI_ButtonOut(String subtype, String params); -void* getAPI_Buzzer(String subtype, String params); -void* getAPI_Encoder(String subtype, String params); -void* getAPI_IoTServo(String subtype, String params); -void* getAPI_Mcp23017(String subtype, String params); -void* getAPI_Mp3(String subtype, String params); -void* getAPI_Multitouch(String subtype, String params); -void* getAPI_Pcf8574(String subtype, String params); -void* getAPI_Pwm8266(String subtype, String params); -void* getAPI_TelegramLT(String subtype, String params); -void* getAPI_DwinI(String subtype, String params); -void* getAPI_Lcd2004(String subtype, String params); -void* getAPI_Oled64(String subtype, String params); void* getAPI(String subtype, String params) { void* tmpAPI; @@ -50,42 +22,14 @@ if ((tmpAPI = getAPI_Loging(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_LogingDaily(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_IoTMath(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_owmWeather(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Ping(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Variable(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_VButton(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_A02Distance(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Acs712(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_AhtXX(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_BL0937(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Bme280(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Bmp280(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Dht1122(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Impulse(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_MQgas(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Pzem004_v2(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_RTC(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_S8(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Sht30(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Sonar(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_UART(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_AnalogBtn(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_ButtonIn(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Buzzer(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Encoder(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Multitouch(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Pcf8574(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_DwinI(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Oled64(subtype, params)) != nullptr) return tmpAPI; return nullptr; } \ No newline at end of file