From 222cf80b238c531b9d58b9f9ce4d4935ae3cefe4 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Tue, 27 Feb 2024 12:34:45 +0530 Subject: [PATCH] Again addressed review comments --- .../energy-management-app/esp32/main/main.cpp | 155 +++++++++--------- examples/platform/esp32/time/TimeSync.cpp | 27 +-- examples/platform/esp32/time/TimeSync.h | 10 +- 3 files changed, 93 insertions(+), 99 deletions(-) diff --git a/examples/energy-management-app/esp32/main/main.cpp b/examples/energy-management-app/esp32/main/main.cpp index 329a4cc1e539be..45d4fb30b174b2 100644 --- a/examples/energy-management-app/esp32/main/main.cpp +++ b/examples/energy-management-app/esp32/main/main.cpp @@ -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; @@ -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(nullptr)); + chip::DeviceLayer::PlatformMgr().ScheduleWork( + InitServer, reinterpret_cast(nullptr)); } diff --git a/examples/platform/esp32/time/TimeSync.cpp b/examples/platform/esp32/time/TimeSync.cpp index b12752ad35e540..2846575f7de12b 100644 --- a/examples/platform/esp32/time/TimeSync.cpp +++ b/examples/platform/esp32/time/TimeSync.cpp @@ -21,14 +21,14 @@ #include #include -#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]; @@ -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(buf_len), static_cast(print_size)); @@ -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."); @@ -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) @@ -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()) @@ -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 diff --git a/examples/platform/esp32/time/TimeSync.h b/examples/platform/esp32/time/TimeSync.h index 4ce089a9837b0d..f919d9277b0093 100644 --- a/examples/platform/esp32/time/TimeSync.h +++ b/examples/platform/esp32/time/TimeSync.h @@ -22,14 +22,8 @@ #include 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