diff --git a/components/esp_rainmaker/include/esp_rmaker_ota.h b/components/esp_rainmaker/include/esp_rmaker_ota.h index 3b975e5..5dd023a 100644 --- a/components/esp_rainmaker/include/esp_rmaker_ota.h +++ b/components/esp_rainmaker/include/esp_rmaker_ota.h @@ -199,6 +199,18 @@ esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t handle, esp_rmaker_o * @return error on failure */ esp_err_t esp_rmaker_ota_fetch(void); + +/** Fetch OTA Info with a delay + * + * For OTA using Topics, this API can be used to explicitly ask the backend if an OTA is available + * after a delay (in seconds) passed as an argument. + * + * @param[in] time Delay (in seconds) + * + * @return ESP_OK if the OTA fetch timer was created. + * @return error on failure + */ +esp_err_t esp_rmaker_ota_fetch_with_delay(int time); #ifdef __cplusplus } #endif diff --git a/components/esp_rainmaker/src/ota/esp_rmaker_ota.c b/components/esp_rainmaker/src/ota/esp_rmaker_ota.c index 1b39c88..4896930 100644 --- a/components/esp_rainmaker/src/ota/esp_rmaker_ota.c +++ b/components/esp_rainmaker/src/ota/esp_rmaker_ota.c @@ -342,7 +342,6 @@ ota_end: return ESP_FAIL; } - static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { @@ -357,7 +356,9 @@ static void event_handler(void* arg, esp_event_base_t event_base, s_ota_rollback_timer = NULL; } if (ota->type == OTA_USING_TOPICS) { - esp_rmaker_ota_fetch(); + if (esp_rmaker_ota_fetch_with_delay(RMAKER_OTA_FETCH_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to create OTA Fetch timer."); + } } } diff --git a/components/esp_rainmaker/src/ota/esp_rmaker_ota_internal.h b/components/esp_rainmaker/src/ota/esp_rmaker_ota_internal.h index d638bc6..021545b 100644 --- a/components/esp_rainmaker/src/ota/esp_rmaker_ota_internal.h +++ b/components/esp_rainmaker/src/ota/esp_rmaker_ota_internal.h @@ -21,6 +21,7 @@ #define RMAKER_OTA_NVS_NAMESPACE "rmaker_ota" #define RMAKER_OTA_JOB_ID_NVS_NAME "rmaker_ota_id" #define RMAKER_OTA_UPDATE_FLAG_NVS_NAME "ota_update" +#define RMAKER_OTA_FETCH_DELAY 5 typedef struct { esp_rmaker_ota_type_t type; diff --git a/components/esp_rainmaker/src/ota/esp_rmaker_ota_using_topics.c b/components/esp_rainmaker/src/ota/esp_rmaker_ota_using_topics.c index bc60a94..bf03ad6 100644 --- a/components/esp_rainmaker/src/ota/esp_rmaker_ota_using_topics.c +++ b/components/esp_rainmaker/src/ota/esp_rmaker_ota_using_topics.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include #include @@ -263,7 +265,7 @@ esp_err_t esp_rmaker_ota_fetch(void) return err; } -void esp_rmaker_ota_timer_cb_fetch(void *priv) +void esp_rmaker_ota_autofetch_timer_cb(void *priv) { esp_rmaker_ota_fetch(); } @@ -295,11 +297,13 @@ static void esp_rmaker_ota_work_fn(void *priv_data) esp_rmaker_ota_subscribe(priv_data); #ifdef CONFIG_ESP_RMAKER_OTA_AUTOFETCH if (ota->ota_in_progress != true) { - esp_rmaker_ota_fetch(); + if (esp_rmaker_ota_fetch_with_delay(RMAKER_OTA_FETCH_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to create OTA Fetch timer."); + } } if (ota_autofetch_period > 0) { esp_timer_create_args_t autofetch_timer_conf = { - .callback = esp_rmaker_ota_timer_cb_fetch, + .callback = esp_rmaker_ota_autofetch_timer_cb, .arg = priv_data, .dispatch_method = ESP_TIMER_TASK, .name = "ota_autofetch_tm" @@ -307,7 +311,7 @@ static void esp_rmaker_ota_work_fn(void *priv_data) if (esp_timer_create(&autofetch_timer_conf, &ota_autofetch_timer) == ESP_OK) { esp_timer_start_periodic(ota_autofetch_timer, ota_autofetch_period); } else { - ESP_LOGE(TAG, "Failes to create OTA Autofetch timer"); + ESP_LOGE(TAG, "Failed to create OTA Autofetch timer"); } } #endif /* CONFIG_ESP_RMAKER_OTA_AUTOFETCH */ @@ -322,3 +326,20 @@ esp_err_t esp_rmaker_ota_enable_using_topics(esp_rmaker_ota_t *ota) } return err; } + +static void esp_rmaker_ota_fetch_timer_cb(TimerHandle_t xTimer) +{ + esp_rmaker_ota_fetch(); + xTimerDelete(xTimer, 0); +} + +esp_err_t esp_rmaker_ota_fetch_with_delay(int time) +{ + TimerHandle_t timer = xTimerCreate(NULL, (time * 1000) / portTICK_PERIOD_MS, pdFALSE, NULL, esp_rmaker_ota_fetch_timer_cb); + if (timer == NULL) { + return ESP_ERR_NO_MEM; + } else { + xTimerStart(timer, 0); + } + return ESP_OK; +} \ No newline at end of file