diff --git a/src/flash_config/flash_config.h b/src/flash_config/flash_config.h deleted file mode 100644 index b28b04f64..000000000 --- a/src/flash_config/flash_config.h +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/flash_config/flash_vars_vars.h b/src/flash_config/flash_vars_vars.h deleted file mode 100644 index 6f50f4413..000000000 --- a/src/flash_config/flash_vars_vars.h +++ /dev/null @@ -1,12 +0,0 @@ - - -//#define DISABLE_FLASH_VARS_VARS - -#define BOOT_COMPLETE_SECONDS 30 - -// call at startup -void increment_boot_count(); -// call once started (>30s?) -void boot_complete(); -// call to return the number of boots since a boot_complete -int boot_failures(); \ No newline at end of file diff --git a/src/flash_config/flash_vars.c b/src/hal/bk7231/flash_vars.c similarity index 100% rename from src/flash_config/flash_vars.c rename to src/hal/bk7231/flash_vars.c diff --git a/src/flash_config/flash_vars.h b/src/hal/bk7231/flash_vars.h similarity index 100% rename from src/flash_config/flash_vars.h rename to src/hal/bk7231/flash_vars.h diff --git a/src/flash_config/flash_vars_vars.c b/src/hal/bk7231/flash_vars_vars.c similarity index 87% rename from src/flash_config/flash_vars_vars.c rename to src/hal/bk7231/flash_vars_vars.c index 7c77e7a7d..88a0bfae2 100644 --- a/src/flash_config/flash_vars_vars.c +++ b/src/hal/bk7231/flash_vars_vars.c @@ -9,7 +9,7 @@ // call at startup -void increment_boot_count(){ +void HAL_FlashVars_IncreaseBootCount(){ #ifndef DISABLE_FLASH_VARS_VARS FLASH_VARS_STRUCTURE data; @@ -28,7 +28,7 @@ void increment_boot_count(){ } // call once started (>30s?) -void boot_complete(){ +void HAL_FlashVars_SaveBootComplete(){ #ifndef DISABLE_FLASH_VARS_VARS FLASH_VARS_STRUCTURE data; // mark that we have completed a boot. @@ -46,8 +46,8 @@ void boot_complete(){ #endif } -// call to return the number of boots since a boot_complete -int boot_failures(){ +// call to return the number of boots since a HAL_FlashVars_SaveBootComplete +int HAL_FlashVars_GetBootFailures(){ int diff = 0; #ifndef DISABLE_FLASH_VARS_VARS diff = flash_vars.boot_count - flash_vars.boot_success_count; diff --git a/src/hal/bl602/hal_flashConfig_bl602.c b/src/hal/bl602/hal_flashConfig_bl602.c index fe6a5ef30..d4d6932f3 100644 --- a/src/hal/bl602/hal_flashConfig_bl602.c +++ b/src/hal/bl602/hal_flashConfig_bl602.c @@ -7,7 +7,7 @@ static int g_easyFlash_Ready = 0; -static void BL602_InitEasyFlashIfNeeded(){ +void BL602_InitEasyFlashIfNeeded(){ if(g_easyFlash_Ready==0){ easyflash_init(); g_easyFlash_Ready = 1; diff --git a/src/hal/bl602/hal_flashVars_bl602.c b/src/hal/bl602/hal_flashVars_bl602.c new file mode 100644 index 000000000..819af6b4b --- /dev/null +++ b/src/hal/bl602/hal_flashVars_bl602.c @@ -0,0 +1,84 @@ +#ifdef PLATFORM_BL602 + +#include "../hal_flashConfig.h" +#include "../../logging/logging.h" + +#include + + +void BL602_InitEasyFlashIfNeeded(); + +#define EASYFLASH_MY_BOOTCOUNTS "myBtCnts" + +typedef struct bl602_bootCounts_s { + unsigned short boot_count; // number of times the device has booted + unsigned short boot_success_count; // if a device boots completely (>30s), will equal boot_success_count +} bl602_bootCounts_t; + +bl602_bootCounts_t g_bootCounts; + + +static int BL602_ReadFlashVars(void *target, int dataLen){ + int readLen; + + BL602_InitEasyFlashIfNeeded(); + + ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_ReadFlashVars: will read %d bytes", dataLen); + readLen = ef_get_env_blob(EASYFLASH_MY_BOOTCOUNTS, target, dataLen , NULL); + ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_ReadFlashVars: really loaded %d bytes", readLen); + + return dataLen; +} + + + +static int BL602_SaveFlashVars(void *src, int dataLen){ + + EfErrCode res; + + BL602_InitEasyFlashIfNeeded(); + + res = ef_set_env_blob(EASYFLASH_MY_BOOTCOUNTS, src, dataLen); + if(res == EF_ENV_INIT_FAILED) { + ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_SaveFlashVars: EF_ENV_INIT_FAILED for %d bytes", dataLen); + return 0; + } + if(res == EF_ENV_ARG_ERR) { + ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_SaveFlashVars: EF_ENV_ARG_ERR for %d bytes", dataLen); + return 0; + } + ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_SaveFlashVars: saved %d bytes", dataLen); + return dataLen; +} + + +void HAL_FlashVars_SaveBootComplete(){ + g_bootCounts.boot_success_count = g_bootCounts.boot_count; + // save after set + BL602_SaveFlashVars(&g_bootCounts,sizeof(g_bootCounts)); +} + +int HAL_FlashVars_GetBootCount(){ + return g_bootCounts.boot_count; +} +int HAL_FlashVars_GetBootFailures(){ + int diff = 0; + diff = g_bootCounts.boot_count - g_bootCounts.boot_success_count; + return diff; +} +void HAL_FlashVars_IncreaseBootCount(){ + // defaults - in case read fails + g_bootCounts.boot_count = 0; + g_bootCounts.boot_success_count = 0; + // read saved + BL602_ReadFlashVars(&g_bootCounts,sizeof(g_bootCounts)); + g_bootCounts.boot_count++; + // save after increase + BL602_SaveFlashVars(&g_bootCounts,sizeof(g_bootCounts)); +} + + + +#endif // PLATFORM_XR809 + + diff --git a/src/hal/bl602/hal_pins_bl602.c b/src/hal/bl602/hal_pins_bl602.c index 7e83bd417..5e8b7c12b 100644 --- a/src/hal/bl602/hal_pins_bl602.c +++ b/src/hal/bl602/hal_pins_bl602.c @@ -87,7 +87,7 @@ void HAL_PIN_PWM_Start(int index) { return; } - addLogAdv(LOG_INFO, LOG_FEATURE_MAIN,"HAL_PIN_PWM_Start: pin %i chose pwm %i\r\n",index,pwm); + //addLogAdv(LOG_INFO, LOG_FEATURE_MAIN,"HAL_PIN_PWM_Start: pin %i chose pwm %i\r\n",index,pwm); // Frequency must be between 2000 and 800000 bl_pwm_init(pwm, index, 2000); bl_pwm_start(pwm); @@ -108,7 +108,7 @@ void HAL_PIN_PWM_Update(int index, int value) { --> Followed by PWM Output 0 (Low) for the remaining 75% of the PWM Cycle */ - addLogAdv(LOG_INFO, LOG_FEATURE_MAIN,"HAL_PIN_PWM_Update: pin %i had pwm %i, set %i\r\n",index,pwm,value); + //addLogAdv(LOG_INFO, LOG_FEATURE_MAIN,"HAL_PIN_PWM_Update: pin %i had pwm %i, set %i\r\n",index,pwm,value); bl_pwm_set_duty(pwm, duty); } diff --git a/src/hal/hal_flashVars.h b/src/hal/hal_flashVars.h new file mode 100644 index 000000000..99c915173 --- /dev/null +++ b/src/hal/hal_flashVars.h @@ -0,0 +1,12 @@ + +//#define DISABLE_FLASH_VARS_VARS + +#define BOOT_COMPLETE_SECONDS 30 + +// call at startup +void HAL_FlashVars_IncreaseBootCount(); +// call once started (>30s?) +void HAL_FlashVars_SaveBootComplete(); +// call to return the number of boots since a HAL_FlashVars_SaveBootComplete +int HAL_FlashVars_GetBootFailures(); +int HAL_FlashVars_GetBootCount(); \ No newline at end of file diff --git a/src/hal/xr809/hal_flashVars_xr809.c b/src/hal/xr809/hal_flashVars_xr809.c new file mode 100644 index 000000000..0a7a5aed6 --- /dev/null +++ b/src/hal/xr809/hal_flashVars_xr809.c @@ -0,0 +1,23 @@ +#ifdef PLATFORM_XR809 + +#include "../hal_flashConfig.h" +#include "../../logging/logging.h" + +void HAL_FlashVars_SaveBootComplete(){ +} + +int HAL_FlashVars_GetBootCount(){ + return 0; +} +int HAL_FlashVars_GetBootFailures(){ + int diff = 0; + return diff; +} +void HAL_FlashVars_IncreaseBootCount(){ +} + + + +#endif // PLATFORM_XR809 + + diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 88504acae..9edc3f6ab 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -212,11 +212,7 @@ int http_fn_index(http_request_t *request) { if(http_getArg(request->url,"restart",tmpA,sizeof(tmpA))) { poststr(request,"
Module will restart soon
"); -#if WINDOWS - -#else RESET_ScheduleModuleReset(3); -#endif } poststr(request,"
"); @@ -228,7 +224,8 @@ int http_fn_index(http_request_t *request) { poststr(request,"
"); - hprintf128(request,"

Cfg size: %i, change counter: %i, ota counter: %i!

",sizeof(g_cfg),g_cfg.changeCounter,g_cfg.otaCounter); + hprintf128(request,"

Cfg size: %i, change counter: %i, ota counter: %i, boot fails %i!

", + sizeof(g_cfg),g_cfg.changeCounter,g_cfg.otaCounter,Main_GetLastRebootBootFailures()); poststr(request,htmlReturnToMenu); HTTP_AddBuildFooter(request); diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index 3bde1c6ab..35c6591f0 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -7,6 +7,7 @@ #include "../jsmn/jsmn_h.h" #include "../ota/ota.h" #include "../hal/hal_wifi.h" +#include "../hal/hal_flashVars.h" #ifdef BK_LITTLEFS #include "../littlefs/our_lfs.h" #endif @@ -16,11 +17,8 @@ #elif PLATFORM_BL602 #else -#include "../flash_config/flash_config.h" #endif -#include "../new_cfg.h" -#include "../flash_config/flash_vars_vars.h" -#include "../flash_config/flash_vars.h" +#include "../new_cfg.h" // Commands register, execution API and cmd tokenizer #include "../cmnds/cmd_public.h" @@ -955,10 +953,10 @@ static int http_rest_get_flash_vars_test(http_request_t *request){ p = &data; } else { for (i = 0; i < increment; i++){ - increment_boot_count(); + HAL_FlashVars_IncreaseBootCount(); } for (i = 0; i < len; i++){ - boot_complete(); + HAL_FlashVars_SaveBootComplete(); } } diff --git a/src/new_common.h b/src/new_common.h index aba48483d..b96672b1e 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -200,7 +200,9 @@ char Tiny_CRC8(const char *data,int length); void RESET_ScheduleModuleReset(int delSeconds); int Main_IsConnectedToWiFi(); void Main_Init(); -void Main_OnEverySecond(); +void Main_OnEverySecond(); +int Main_GetLastRebootBootFailures(); + diff --git a/src/user_main.c b/src/user_main.c index 13338be2b..1a2518039 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -5,6 +5,7 @@ #include "hal/hal_wifi.h" #include "hal/hal_generic.h" +#include "hal/hal_flashVars.h" #include "new_common.h" #include "driver/drv_public.h" @@ -27,13 +28,6 @@ #include "littlefs/our_lfs.h" #endif -#if PLATFORM_XR809 - -#elif PLATFORM_BL602 -#else -#include "flash_config/flash_config.h" -#endif -#include "flash_config/flash_vars_vars.h" #include "driver/drv_ntp.h" @@ -49,38 +43,19 @@ static int g_reset = 0; // is connected to WiFi? int g_bHasWiFiConnected = 0; +static int g_bootFailures = 0; + static int g_saveCfgAfter = 0; #define LOG_FEATURE LOG_FEATURE_MAIN #if PLATFORM_XR809 -void boot_complete(){ - -} - -int boot_failures(){ - return 0; -} -void increment_boot_count(){ - -} size_t xPortGetFreeHeapSize() { return 0; } #endif #if PLATFORM_BL602 -void boot_complete(){ - -} - -int boot_failures(){ - return 0; -} -void increment_boot_count(){ - -} - @@ -198,7 +173,8 @@ void Main_OnEverySecond() // when we hit 30s, mark as boot complete. if (g_secondsElapsed == BOOT_COMPLETE_SECONDS){ - boot_complete(); + HAL_FlashVars_SaveBootComplete(); + g_bootFailures = HAL_FlashVars_GetBootFailures(); } if (g_openAP){ @@ -257,24 +233,26 @@ void app_on_generic_dbl_click(int btnIndex) int Main_IsConnectedToWiFi() { return g_bHasWiFiConnected; } +int Main_GetLastRebootBootFailures() { + return g_bootFailures; +} void Main_Init() { int bForceOpenAP = 0; - int bootFailures = 0; const char *wifi_ssid, *wifi_pass; // read or initialise the boot count flash area - increment_boot_count(); + HAL_FlashVars_IncreaseBootCount(); - bootFailures = boot_failures(); - if (bootFailures > 3){ + g_bootFailures = HAL_FlashVars_GetBootFailures(); + if (g_bootFailures > 3){ bForceOpenAP = 1; - ADDLOGF_INFO("###### force AP mode - boot failures %d", bootFailures); + ADDLOGF_INFO("###### force AP mode - boot failures %d", g_bootFailures); } - if (bootFailures > 4){ + if (g_bootFailures > 4){ bSafeMode = 1; - ADDLOGF_INFO("###### safe mode activated - boot failures %d", bootFailures); + ADDLOGF_INFO("###### safe mode activated - boot failures %d", g_bootFailures); } CFG_InitAndLoad(); @@ -350,7 +328,7 @@ void Main_Init() // but DON't run autoexec if we have had 2+ boot failures CMD_Init(); - if (bootFailures < 2){ + if (g_bootFailures < 2){ CMD_ExecuteCommand("exec autoexec.bat"); } } diff --git a/windowsTest_msvc2008.vcproj b/windowsTest_msvc2008.vcproj index aca896660..1845aee26 100644 --- a/windowsTest_msvc2008.vcproj +++ b/windowsTest_msvc2008.vcproj @@ -207,6 +207,22 @@ /> + + + + + + + + @@ -637,7 +653,7 @@ >