Skip to content

Commit

Permalink
Again addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Feb 27, 2024
1 parent 9b67bca commit 2e82631
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 99 deletions.
155 changes: 77 additions & 78 deletions examples/energy-management-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
extern const char insights_auth_key_start[] asm("_binary_insights_auth_key_txt_start");
extern const char insights_auth_key_end[] asm("_binary_insights_auth_key_txt_end");
extern const char
insights_auth_key_start[] asm("_binary_insights_auth_key_txt_start");
extern const char
insights_auth_key_end[] asm("_binary_insights_auth_key_txt_end");
#endif

static const char * TAG = "energy-management-app";
static const char *TAG = "energy-management-app";

static AppDeviceCallbacks EchoCallbacks;
static DeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
Expand All @@ -98,122 +100,119 @@ DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
DeviceLayer::ESP32SecureCertDACProvider gSecureCertDACProvider;
#endif // CONFIG_SEC_CERT_DAC_PROVIDER

chip::Credentials::DeviceAttestationCredentialsProvider * get_dac_provider(void)
{
chip::Credentials::DeviceAttestationCredentialsProvider *
get_dac_provider(void) {
#if CONFIG_SEC_CERT_DAC_PROVIDER
return &gSecureCertDACProvider;
return &gSecureCertDACProvider;
#elif CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
return &sFactoryDataProvider;
return &sFactoryDataProvider;
#else // EXAMPLE_DAC_PROVIDER
return chip::Credentials::Examples::GetExampleDACProvider();
return chip::Credentials::Examples::GetExampleDACProvider();
#endif
}

} // namespace

void ApplicationInit()
{
ESP_LOGD(TAG, "Energy Management App: ApplicationInit()");
EvseApplicationInit();
void ApplicationInit() {
ESP_LOGD(TAG, "Energy Management App: ApplicationInit()");
EvseApplicationInit();
}

void ApplicationShutdown()
{
ESP_LOGD(TAG, "Energy Management App: ApplicationShutdown()");
EvseApplicationShutdown();
void ApplicationShutdown() {
ESP_LOGD(TAG, "Energy Management App: ApplicationShutdown()");
EvseApplicationShutdown();
}

static void InitServer(intptr_t context)
{
// Print QR Code URL
PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));
static void InitServer(intptr_t context) {
// Print QR Code URL
PrintOnboardingCodes(
chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE));

DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate);
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
DeviceCallbacksDelegate::Instance().SetAppDelegate(
&sAppDeviceCallbacksDelegate);
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND
// Initialize device attestation config
#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
esp_insights_config_t config = {
.log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT,
.auth_key = insights_auth_key_start,
};
esp_insights_config_t config = {
.log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING |
ESP_DIAG_LOG_TYPE_EVENT,
.auth_key = insights_auth_key_start,
};

esp_err_t ret = esp_insights_init(&config);
esp_err_t ret = esp_insights_init(&config);

if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret);
}
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret);
}

static Tracing::Insights::ESP32Backend backend;
Tracing::Register(backend);
static Tracing::Insights::ESP32Backend backend;
Tracing::Register(backend);
#endif

// Application code should always be initialised after the initialisation of server.
ApplicationInit();
// Application code should always be initialised after the initialisation of
// server.
ApplicationInit();

#if CONFIG_ENABLE_SNTP_TIME_SYNC
const char kNtpServerUrl[] = "pool.ntp.org";
const uint16_t kSyncNtpTimeIntervalDay = 1;
chip::Esp32TimeSync mEsp32TimeSync;
mEsp32TimeSync.Init(kNtpServerUrl, kSyncNtpTimeIntervalDay);
const char kNtpServerUrl[] = "pool.ntp.org";
const uint16_t kSyncNtpTimeIntervalDay = 1;
chip::Esp32TimeSync::Init(kNtpServerUrl, kSyncNtpTimeIntervalDay);
#endif
}

extern "C" void app_main()
{
// Initialize the ESP NVS layer.
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}
err = esp_event_loop_create_default();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s", esp_err_to_name(err));
return;
}
extern "C" void app_main() {
// Initialize the ESP NVS layer.
esp_err_t err = nvs_flash_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}
err = esp_event_loop_create_default();
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s",
esp_err_to_name(err));
return;
}
#if CONFIG_ENABLE_PW_RPC
chip::rpc::Init();
chip::rpc::Init();
#endif

ESP_LOGI(TAG, "==================================================");
ESP_LOGI(TAG, "chip-esp32-energy-management-example starting");
ESP_LOGI(TAG, "==================================================");
ESP_LOGI(TAG, "==================================================");
ESP_LOGI(TAG, "chip-esp32-energy-management-example starting");
ESP_LOGI(TAG, "==================================================");

#if CONFIG_ENABLE_CHIP_SHELL
#if CONFIG_OPENTHREAD_CLI
chip::RegisterOpenThreadCliCommands();
chip::RegisterOpenThreadCliCommands();
#endif
chip::LaunchShell();
chip::LaunchShell();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (Internal::ESP32Utils::InitWiFiStack() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to initialize WiFi stack");
return;
}
if (Internal::ESP32Utils::InitWiFiStack() != CHIP_NO_ERROR) {
ESP_LOGE(TAG, "Failed to initialize WiFi stack");
return;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
return;
}
CHIPDeviceManager &deviceMgr = CHIPDeviceManager::GetInstance();
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
if (error != CHIP_NO_ERROR) {
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
return;
}

#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
SetCommissionableDataProvider(&sFactoryDataProvider);
SetCommissionableDataProvider(&sFactoryDataProvider);
#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER
SetDeviceInstanceInfoProvider(&sFactoryDataProvider);
SetDeviceInstanceInfoProvider(&sFactoryDataProvider);
#endif
#endif

SetDeviceAttestationCredentialsProvider(get_dac_provider());
ESPOpenThreadInit();
SetDeviceAttestationCredentialsProvider(get_dac_provider());
ESPOpenThreadInit();

chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
chip::DeviceLayer::PlatformMgr().ScheduleWork(
InitServer, reinterpret_cast<intptr_t>(nullptr));
}
27 changes: 14 additions & 13 deletions examples/platform/esp32/time/TimeSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#include <lib/support/CHIPMemString.h>
#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_SEC 86400000ULL
static constexpr time_t kMinValidTimeStampEpoch = 1704067200; // 1 Jan 2019
static constexpr uint32_t kSecondsInADay = 24*60*60;

namespace {
const uint8_t kMaxNtpServerStringSize = 128;
char mSntpServerName[kMaxNtpServerStringSize];

static CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len)
CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len)
{
struct tm timeinfo;
char strftime_buf[64];
Expand All @@ -41,7 +41,7 @@ static CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len)
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
size_t print_size = snprintf(buf, buf_len, "%s, DST: %s", strftime_buf, timeinfo.tm_isdst ? "Yes" : "No");
if (print_size >= buf_len)
if (print_size >= (buf_len - 1))
{
ChipLogError(DeviceLayer, "Buffer size %d insufficient for localtime string. Required size: %d", static_cast<int>(buf_len),
static_cast<int>(print_size));
Expand All @@ -50,17 +50,17 @@ static CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len)
return CHIP_NO_ERROR;
}

static bool ValidateTime()
bool ValidateTime()
{
time_t now;
time(&now);
return (now > REF_TIME);
return (now > kMinValidTimeStampEpoch);
}

static CHIP_ERROR PrintCurrentTime()
CHIP_ERROR PrintCurrentTime()
{
char local_time[64] = { 0 };
ReturnErrorCodeIf(GetLocalTimeString(local_time, sizeof(local_time)) != CHIP_NO_ERROR, CHIP_ERROR_BUFFER_TOO_SMALL);
ReturnErrorOnFailure(GetLocalTimeString(local_time, sizeof(local_time)));
if (!ValidateTime())
{
ChipLogProgress(DeviceLayer, "Time not synchronised yet.");
Expand All @@ -70,7 +70,7 @@ static CHIP_ERROR PrintCurrentTime()
return CHIP_NO_ERROR;
}

static void TimeSyncCallback(struct timeval * tv)
void TimeSyncCallback(struct timeval * tv)
{
ChipLogProgress(DeviceLayer, "SNTP Synchronised.");
if (PrintCurrentTime() != CHIP_NO_ERROR)
Expand All @@ -82,8 +82,8 @@ static void TimeSyncCallback(struct timeval * tv)
} // anonymous namespace

namespace chip {

void Esp32TimeSync::Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay)
namespace Esp32TimeSync {
void Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay)
{
chip::Platform::CopyString(mSntpServerName, aSntpServerName);
if (esp_sntp_enabled())
Expand All @@ -93,8 +93,9 @@ void Esp32TimeSync::Init(const char * aSntpServerName, const uint16_t aSyncSntpI
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 * aSyncSntpIntervalDay);
esp_sntp_set_sync_interval(kSecondsInADay * aSyncSntpIntervalDay);
esp_sntp_init();
sntp_set_time_sync_notification_cb(TimeSyncCallback);
}
} // namespace Esp32TimeSync
} // namespace chip
10 changes: 2 additions & 8 deletions examples/platform/esp32/time/TimeSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
#include <lib/support/Span.h>

namespace chip {

/**
* Class to sync real time on esp32 using ntp server.
*/

class Esp32TimeSync
namespace Esp32TimeSync
{
public:
void Init(const char * aSntpServerName, uint16_t aSyncSntpIntervalDay);
};
} // namespace Esp32TimeSync
} // namespace chip

0 comments on commit 2e82631

Please sign in to comment.