From 21c63bcaddef6786200957142969f87851293345 Mon Sep 17 00:00:00 2001 From: bogdan Date: Sun, 16 Apr 2023 17:47:52 +0200 Subject: [PATCH] cron expression validation and error reports --- HTML/application.html | 4 +++- HTML/res/cron.js | 1 + components/esp_cron | 2 +- main/include/AppConfiguration.h | 1 + main/include/CronTimers.h | 3 ++- main/src/CronTimers.c | 27 ++++++++++++++++++++++++++- main/src/HTTPPrintCustom.c | 7 +++++++ 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/HTML/application.html b/HTML/application.html index 5f00fbc..19e89c8 100644 --- a/HTML/application.html +++ b/HTML/application.html @@ -28,6 +28,7 @@ timarr = [ ~crontmr(14)~, ~crontmr(15)~ ]; +function showcronerr(err) {if(err != "") alert(err);} @@ -51,8 +52,9 @@ timarr = [ diff --git a/HTML/res/cron.js b/HTML/res/cron.js index 60bd0f7..96e3640 100644 --- a/HTML/res/cron.js +++ b/HTML/res/cron.js @@ -248,3 +248,4 @@ function drawtimers(tarr) { target.innerHTML = content; } + diff --git a/components/esp_cron b/components/esp_cron index 6785ad0..e6cb151 160000 --- a/components/esp_cron +++ b/components/esp_cron @@ -1 +1 @@ -Subproject commit 6785ad092344b6c391fa24b0a0b25ffd258d79e6 +Subproject commit e6cb1512fd3fb4fdb07ee1132d625f861331447d diff --git a/main/include/AppConfiguration.h b/main/include/AppConfiguration.h index 2e66c95..e60dc7a 100644 --- a/main/include/AppConfiguration.h +++ b/main/include/AppConfiguration.h @@ -42,6 +42,7 @@ #define CRON_TIMERS_NUMBER (16) #define TIMER_NAME_LENGTH (16) #define TIMER_CRONSTRING_LENGTH (32) +#define CRON_EXPRESS_MAX_LENGTH (128) typedef struct { diff --git a/main/include/CronTimers.h b/main/include/CronTimers.h index 2dddadc..ced2364 100644 --- a/main/include/CronTimers.h +++ b/main/include/CronTimers.h @@ -24,9 +24,10 @@ #include "esp_err.h" #include "cron.h" #include "jobs.h" - +#include "ccronexpr.h" esp_err_t InitCronSheduler(); esp_err_t ReloadCronSheduler(); +char* GetCronError(); #endif /* MAIN_INCLUDE_CRONTIMERS_H_ */ diff --git a/main/src/CronTimers.c b/main/src/CronTimers.c index b7c67b7..dd429f8 100644 --- a/main/src/CronTimers.c +++ b/main/src/CronTimers.c @@ -26,6 +26,12 @@ #define TAG "CRON_TIMER" static cron_job *JobsList[CRON_TIMERS_NUMBER]; +static char cron_express_error[CRON_EXPRESS_MAX_LENGTH]; + +char* GetCronError() +{ + return cron_express_error; +} void custom_cron_job_callback(cron_job *job) { @@ -43,20 +49,39 @@ esp_err_t InitCronSheduler() return res; } + +const char* check_expr(const char* expr) { + const char* err = NULL; + cron_expr test; + memset(&test, 0, sizeof(test)); + cron_parse_expr(expr, &test, &err); + return err; +} + esp_err_t ReloadCronSheduler() { //remove all jobs ESP_LOGI(TAG,"Cron stop call result %d",cron_stop()); cron_job_clear_all(); //check if we have jobs to run + bool isExpressError = false; for (int i = 0; i < CRON_TIMERS_NUMBER; i++) { - if (!GetAppConf()->Timers[i].del && GetAppConf()->Timers[i].enab) + const char* err = check_expr(GetAppConf()->Timers[i].cron); + if(err) + { + snprintf (cron_express_error, CRON_EXPRESS_MAX_LENGTH-1, "In timer %d expression error:%s", i+1, err); + ESP_LOGE(TAG, "%s", cron_express_error); + isExpressError = true; + continue; + } + else if (!GetAppConf()->Timers[i].del && GetAppConf()->Timers[i].enab) { JobsList[i] = cron_job_create(GetAppConf()->Timers[i].cron, custom_cron_job_callback, (void*) &GetAppConf()->Timers[i]); } } + if(!isExpressError) cron_express_error[0] = 0x00; //clear last cron expression parse int jobs_num = cron_job_node_count(); ESP_LOGI(TAG, "In config presents %d jobs", jobs_num); diff --git a/main/src/HTTPPrintCustom.c b/main/src/HTTPPrintCustom.c index ad2aee7..d278572 100644 --- a/main/src/HTTPPrintCustom.c +++ b/main/src/HTTPPrintCustom.c @@ -23,6 +23,7 @@ #include "webguiapp.h" #include "jWrite.h" #include "AppConfiguration.h" +#include "CronTimers.h" static void HTTPPrint_crontmr(char *VarData, void *arg) { @@ -46,6 +47,11 @@ static void HTTPPrint_crontmr(char *VarData, void *arg) } } +static void HTTPPrint_cronerr(char *VarData, void *arg) +{ + snprintf(VarData, MAX_DYNVAR_LENGTH, GetCronError()); +} + //Default string if not found handler static void HTTPPrint_DEF(char *VarData, void *arg) { @@ -60,6 +66,7 @@ static void HTTPPrint_status_fail(char *VarData, void *arg) dyn_var_handler_t HANDLERS_ARRAY_CUST[] = { { "crontmr", sizeof("crontmr") - 1, &HTTPPrint_crontmr }, + { "cronerr", sizeof("cronerr") - 1, &HTTPPrint_cronerr }, /*ERROR report*/ { "status_fail", sizeof("status_fail") - 1, &HTTPPrint_status_fail },