diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index f15570aaf92c34..67a8c304661c7b 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -20,7 +20,6 @@ assert(chip_device_platform == "esp32") static_library("ESP32") { sources = [ - "../FreeRTOS/SystemTimeSupport.cpp", "../SingletonConfigurationManager.cpp", "BLEManagerImpl.h", "CHIPDevicePlatformConfig.h", @@ -45,6 +44,7 @@ static_library("ESP32") { "PlatformManagerImpl.cpp", "PlatformManagerImpl.h", "SystemTimeSupport.cpp", + "SystemTimeSupport.h", "bluedroid/BLEManagerImpl.cpp", "nimble/BLEManagerImpl.cpp", ] diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index b3836f1d50f7ab..bd47cdfaf8976c 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -123,6 +124,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // to finish the initialization process. ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl_FreeRTOS::_InitChipStack()); + ReturnErrorOnFailure(System::Clock::InitClock_RealTime()); exit: return chip::DeviceLayer::Internal::ESP32Utils::MapError(err); } diff --git a/src/platform/ESP32/SystemTimeSupport.cpp b/src/platform/ESP32/SystemTimeSupport.cpp index d802ea8ed4d389..67d83ac3153fdc 100644 --- a/src/platform/ESP32/SystemTimeSupport.cpp +++ b/src/platform/ESP32/SystemTimeSupport.cpp @@ -25,8 +25,8 @@ /* this file behaves like a config.h, comes first */ #include -#include #include +#include #include @@ -112,6 +112,15 @@ CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime) return CHIP_NO_ERROR; } +CHIP_ERROR InitClock_RealTime() +{ + Clock::Microseconds64 curTime = + Clock::Microseconds64((static_cast(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) * UINT64_C(1000000))); + // Use CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD as the initial value of RealTime. + // Then the RealTime obtained from GetClock_RealTime will be always valid. + return System::SystemClock().SetClock_RealTime(curTime); +} + } // namespace Clock } // namespace System } // namespace chip diff --git a/src/platform/ESP32/SystemTimeSupport.h b/src/platform/ESP32/SystemTimeSupport.h new file mode 100644 index 00000000000000..da38a26ced25c2 --- /dev/null +++ b/src/platform/ESP32/SystemTimeSupport.h @@ -0,0 +1,28 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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. + */ + +#include + +namespace chip { +namespace System { +namespace Clock { + +CHIP_ERROR InitClock_RealTime(); + +} // namespace Clock +} // namespace System +} // namespace chip