From 6268fbbe885a07cd9adb2cb741fef2e785d1be6d Mon Sep 17 00:00:00 2001 From: Rohit Jadhav <69809379+jadhavrohit924@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:04:07 +0530 Subject: [PATCH] [ESP32] Fix sntp initialization (#32781) --- examples/platform/esp32/time/TimeSync.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/platform/esp32/time/TimeSync.cpp b/examples/platform/esp32/time/TimeSync.cpp index e5086b834ed4b2..1e688c29c6f693 100644 --- a/examples/platform/esp32/time/TimeSync.cpp +++ b/examples/platform/esp32/time/TimeSync.cpp @@ -22,7 +22,7 @@ #include static constexpr time_t kMinValidTimeStampEpoch = 1704067200; // 1 Jan 2019 -static constexpr uint32_t kSecondsInADay = 24 * 60 * 60; +static constexpr uint32_t kMilliSecondsInADay = 24 * 60 * 60 * 1000; namespace { const uint8_t kMaxNtpServerStringSize = 128; @@ -86,6 +86,11 @@ namespace chip { namespace Esp32TimeSync { void Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay) { + if (!aSyncSntpIntervalDay) + { + ChipLogError(DeviceLayer, "Invalid SNTP synchronization time interval."); + return; + } chip::Platform::CopyString(sSntpServerName, aSntpServerName); if (esp_sntp_enabled()) { @@ -94,9 +99,9 @@ void Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay) ChipLogProgress(DeviceLayer, "Initializing SNTP. Using the SNTP server: %s", sSntpServerName); esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); esp_sntp_setservername(0, sSntpServerName); - esp_sntp_set_sync_interval(kSecondsInADay * aSyncSntpIntervalDay); - esp_sntp_init(); + esp_sntp_set_sync_interval(kMilliSecondsInADay * aSyncSntpIntervalDay); sntp_set_time_sync_notification_cb(TimeSyncCallback); + esp_sntp_init(); } } // namespace Esp32TimeSync } // namespace chip