Skip to content

Commit

Permalink
Did some restructoring to address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Feb 16, 2024
1 parent 4e542f0 commit ba22bc0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
8 changes: 4 additions & 4 deletions examples/energy-management-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
36 changes: 19 additions & 17 deletions examples/platform/esp32/time/TimeSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
23 changes: 6 additions & 17 deletions examples/platform/esp32/time/TimeSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,14 @@
#include <lib/support/Span.h>

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

0 comments on commit ba22bc0

Please sign in to comment.