Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Feb 16, 2024
1 parent 959cc8e commit ab8b26e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 70 deletions.
1 change: 1 addition & 0 deletions examples/energy-management-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/energy-management-app/energy-management-common/src"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/time"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util"
Expand Down
7 changes: 5 additions & 2 deletions examples/energy-management-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <common/Esp32AppServer.h>
#include <common/Esp32ThreadInit.h>
#if CONFIG_ENABLE_SNTP_TIME_SYNC
#include <common/TimeSync.h>
#include <time/TimeSync.h>
#endif
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#include "spi_flash_mmap.h"
Expand Down Expand Up @@ -151,7 +151,10 @@ static void InitServer(intptr_t context)
ApplicationInit();

#if CONFIG_ENABLE_SNTP_TIME_SYNC
Esp32Time::TimeSycnInit();
char ntpServerUrl[] = "pool.ntp.org";
uint16_t mSyncNtpTimeIntervalDay = 1;
chip::Esp32TimeSync mEsp32TimeSync(ntpServerUrl, mSyncNtpTimeIntervalDay);
mEsp32TimeSync.Init();
#endif
}

Expand Down
29 changes: 0 additions & 29 deletions examples/platform/esp32/common/TimeSync.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
* limitations under the License.
*/

#include <esp_err.h>
#include <esp_log.h>

#if CONFIG_ENABLE_SNTP_TIME_SYNC
#include "TimeSync.h"
#include <esp_sntp.h>

static const char * TAG = "ESP_TIME_SYNC";
#include <lib/support/logging/CHIPLogging.h>

#define REF_TIME 1546300800 /* 01-Jan-2019 00:00:00 */
/* Timer interval once every day (24 Hours) */
#define TIME_PERIOD (CONFIG_SNTP_SYNC_INTERVAL_DAY * 86400000ULL)
#define TIME_PERIOD_SEC 86400000ULL
namespace chip {

namespace Esp32Time {

esp_err_t GetLocalTimeString(char * buf, size_t buf_len)
CHIP_ERROR Esp32TimeSync::GetLocalTimeString(char * buf, size_t buf_len)
{
struct tm timeinfo;
char strftime_buf[64];
Expand All @@ -41,59 +36,53 @@ esp_err_t GetLocalTimeString(char * buf, size_t buf_len)
size_t print_size = snprintf(buf, buf_len, "%s, DST: %s", strftime_buf, timeinfo.tm_isdst ? "Yes" : "No");
if (print_size >= buf_len)
{
ESP_LOGE(TAG, "Buffer size %d insufficient for localtime string. Required size: %d", buf_len, print_size);
return ESP_ERR_INVALID_ARG;
ChipLogError(DeviceLayer, "Buffer size %d insufficient for localtime string. Required size: %d", buf_len, print_size);
return CHIP_ERROR_INVALID_ARGUMENT;
}
return ESP_OK;
return CHIP_NO_ERROR;
}

bool ValidateTime(void)
bool Esp32TimeSync::ValidateTime()
{
time_t now;
time(&now);
if (now > REF_TIME)
{
return true;
}
return false;
return (now > REF_TIME);
}
static esp_err_t PrintCurrentTime(void)

CHIP_ERROR Esp32TimeSync::PrintCurrentTime()
{
char local_time[64];
if (GetLocalTimeString(local_time, sizeof(local_time)) == ESP_OK)
if (GetLocalTimeString(local_time, sizeof(local_time)) == CHIP_NO_ERROR)
{
if (ValidateTime() == false)
if (!ValidateTime())
{
ESP_LOGI(TAG, "Time not synchronised yet.");
ChipLogProgress(DeviceLayer, "Time not synchronised yet.");
}
ESP_LOGI(TAG, "The current time is: %s.", local_time);
return ESP_OK;
ChipLogProgress(DeviceLayer, "The current time is: %s.", local_time);
return CHIP_NO_ERROR;
}
return ESP_FAIL;
return CHIP_ERROR_INTERNAL;
}

static void TimeSyncCallback(struct timeval * tv)
void Esp32TimeSync::TimeSyncCallback(struct timeval * tv)
{
ESP_LOGI(TAG, "SNTP Synchronised.");
ChipLogProgress(DeviceLayer, "SNTP Synchronised.");
PrintCurrentTime();
}

esp_err_t TimeSycnInit(void)
CHIP_ERROR Esp32TimeSync::Init()
{
if (esp_sntp_enabled())
{
ESP_LOGI(TAG, "SNTP already initialized.");
return ESP_OK;
ChipLogProgress(DeviceLayer, "SNTP already initialized.");
return CHIP_NO_ERROR;
}
char * sntp_server_name = CONFIG_SNTP_SERVER_NAME;
ESP_LOGI(TAG, "Initializing SNTP. Using the SNTP server: %s", sntp_server_name);
ChipLogProgress(DeviceLayer, "Initializing SNTP. Using the SNTP server: %s", mSntpServerName);
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
esp_sntp_setservername(0, sntp_server_name);
esp_sntp_set_sync_interval(TIME_PERIOD);
esp_sntp_setservername(0, mSntpServerName);
esp_sntp_set_sync_interval(TIME_PERIOD_SEC * mSyncSntpIntervalDay);
esp_sntp_init();
sntp_set_time_sync_notification_cb(TimeSyncCallback);
return ESP_OK;
return CHIP_NO_ERROR;
}

} // namespace Esp32Time
#endif
} // namespace chip
47 changes: 47 additions & 0 deletions examples/platform/esp32/time/TimeSync.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#include <esp_sntp.h>
#include <lib/core/CHIPError.h>
#include <lib/support/Span.h>

namespace chip {
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 buffer[100] = { 0 };
char * mSntpServerName;
uint16_t mSyncSntpIntervalDay;
};
} // namespace chip

0 comments on commit ab8b26e

Please sign in to comment.