From ba22bc03fa4a2e6337be8681469c7895c7967d40 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Fri, 16 Feb 2024 21:43:58 +0530 Subject: [PATCH] Did some restructoring to address review comments --- .../energy-management-app/esp32/main/main.cpp | 8 ++--- examples/platform/esp32/time/TimeSync.cpp | 36 ++++++++++--------- examples/platform/esp32/time/TimeSync.h | 23 ++++-------- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/examples/energy-management-app/esp32/main/main.cpp b/examples/energy-management-app/esp32/main/main.cpp index 3a4ab12aa5c2b6..329a4cc1e539be 100644 --- a/examples/energy-management-app/esp32/main/main.cpp +++ b/examples/energy-management-app/esp32/main/main.cpp @@ -151,10 +151,10 @@ static void InitServer(intptr_t context) ApplicationInit(); #if CONFIG_ENABLE_SNTP_TIME_SYNC - char ntpServerUrl[] = "pool.ntp.org"; - uint16_t mSyncNtpTimeIntervalDay = 1; - chip::Esp32TimeSync mEsp32TimeSync(ntpServerUrl, mSyncNtpTimeIntervalDay); - mEsp32TimeSync.Init(); + const char kNtpServerUrl[] = "pool.ntp.org"; + const uint16_t kSyncNtpTimeIntervalDay = 1; + chip::Esp32TimeSync mEsp32TimeSync; + mEsp32TimeSync.Init(kNtpServerUrl, kSyncNtpTimeIntervalDay); #endif } diff --git a/examples/platform/esp32/time/TimeSync.cpp b/examples/platform/esp32/time/TimeSync.cpp index 2dd98ae57fa88e..79cbabe4371d0f 100644 --- a/examples/platform/esp32/time/TimeSync.cpp +++ b/examples/platform/esp32/time/TimeSync.cpp @@ -23,9 +23,11 @@ #define REF_TIME 1546300800 /* 01-Jan-2019 00:00:00 */ /* Timer interval once every day (24 Hours) */ #define TIME_PERIOD_SEC 86400000ULL -namespace chip { +namespace { +const uint8_t kMaxNtpServerStringSize = 128; +char mSntpServerName[kMaxNtpServerStringSize]; -CHIP_ERROR Esp32TimeSync::GetLocalTimeString(char * buf, size_t buf_len) +static CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len) { struct tm timeinfo; char strftime_buf[64]; @@ -42,47 +44,47 @@ CHIP_ERROR Esp32TimeSync::GetLocalTimeString(char * buf, size_t buf_len) return CHIP_NO_ERROR; } -bool Esp32TimeSync::ValidateTime() +static bool ValidateTime() { time_t now; time(&now); return (now > REF_TIME); } -CHIP_ERROR Esp32TimeSync::PrintCurrentTime() +static CHIP_ERROR PrintCurrentTime() { char local_time[64]; - if (GetLocalTimeString(local_time, sizeof(local_time)) == CHIP_NO_ERROR) + ReturnErrorCodeIf(GetLocalTimeString(local_time, sizeof(local_time)) != CHIP_NO_ERROR, CHIP_ERROR_INVALID_ARGUMENT); + if (!ValidateTime()) { - if (!ValidateTime()) - { - ChipLogProgress(DeviceLayer, "Time not synchronised yet."); - } - ChipLogProgress(DeviceLayer, "The current time is: %s.", local_time); - return CHIP_NO_ERROR; + ChipLogProgress(DeviceLayer, "Time not synchronised yet."); } - return CHIP_ERROR_INTERNAL; + ChipLogProgress(DeviceLayer, "The current time is: %s.", local_time); + return CHIP_NO_ERROR; } -void Esp32TimeSync::TimeSyncCallback(struct timeval * tv) +static void TimeSyncCallback(struct timeval * tv) { ChipLogProgress(DeviceLayer, "SNTP Synchronised."); PrintCurrentTime(); } -CHIP_ERROR Esp32TimeSync::Init() +} // anonymous namespace + +namespace chip { + +void Esp32TimeSync::Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay) { + strcpy(mSntpServerName, aSntpServerName); if (esp_sntp_enabled()) { ChipLogProgress(DeviceLayer, "SNTP already initialized."); - return CHIP_NO_ERROR; } ChipLogProgress(DeviceLayer, "Initializing SNTP. Using the SNTP server: %s", mSntpServerName); esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); esp_sntp_setservername(0, mSntpServerName); - esp_sntp_set_sync_interval(TIME_PERIOD_SEC * mSyncSntpIntervalDay); + esp_sntp_set_sync_interval(TIME_PERIOD_SEC * aSyncSntpIntervalDay); esp_sntp_init(); sntp_set_time_sync_notification_cb(TimeSyncCallback); - return CHIP_NO_ERROR; } } // namespace chip diff --git a/examples/platform/esp32/time/TimeSync.h b/examples/platform/esp32/time/TimeSync.h index f0bb64abb2b416..4ce089a9837b0d 100644 --- a/examples/platform/esp32/time/TimeSync.h +++ b/examples/platform/esp32/time/TimeSync.h @@ -22,25 +22,14 @@ #include namespace chip { + +/** + * Class to sync real time on esp32 using ntp server. + */ + class Esp32TimeSync { public: - Esp32TimeSync(char * aSntpServerName, uint16_t aSyncSntpIntervalDay) - { - mSntpServerName = new char[strlen(aSntpServerName) + 1]; - strcpy(mSntpServerName, aSntpServerName); - mSyncSntpIntervalDay = aSyncSntpIntervalDay; - } - - CHIP_ERROR Init(); - -private: - static CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len); - static bool ValidateTime(); - static CHIP_ERROR PrintCurrentTime(); - static void TimeSyncCallback(struct timeval * tv); - - char * mSntpServerName; - uint16_t mSyncSntpIntervalDay; + void Init(const char * aSntpServerName, uint16_t aSyncSntpIntervalDay); }; } // namespace chip