From f836876f51464b7c3807ac45dc8810d1763d784d Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Fri, 8 Jul 2022 16:51:31 +0200 Subject: [PATCH] [zephyr] Do not initialize real time clock When there was no support for Last Known UTC Time in the common code we added a similar but incomplete solution to the Zephyr platform code that initializes the real time clock to the firmware build time. That solution is incorrect as it doesn't allow for updating the Last Known UTC Time, so we should rather return an error if no time synchronization is in place. Signed-off-by: Damian Krolik --- config/nrfconnect/chip-module/CMakeLists.txt | 5 ----- config/zephyr/Kconfig | 7 ------- scripts/examples/nrfconnect_example.sh | 2 +- src/platform/BUILD.gn | 6 ------ src/platform/Zephyr/SystemTimeSupport.cpp | 20 +++++++++----------- 5 files changed, 10 insertions(+), 30 deletions(-) diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index 92d88e95112fa6..70edc0c7087865 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -236,11 +236,6 @@ if (CONFIG_CHIP_ENABLE_DNSSD_SRP) chip_gn_arg_string("chip_mdns" "platform") endif() -if (CONFIG_CHIP_FIRMWARE_BUILD_UNIX_TIME) - string(TIMESTAMP CHIP_FIRMWARE_BUILD_UNIX_TIME "%s") - chip_gn_arg_string("chip_device_config_firmware_build_unix_time" ${CHIP_FIRMWARE_BUILD_UNIX_TIME}) -endif() - if (CHIP_PROJECT_CONFIG) chip_gn_arg_string("chip_project_config_include" ${CHIP_PROJECT_CONFIG}) chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG}) diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig index b197f0491a9689..b17cb0780a4bec 100644 --- a/config/zephyr/Kconfig +++ b/config/zephyr/Kconfig @@ -229,13 +229,6 @@ config CHIP_MALLOC_SYS_HEAP_SIZE endif -config CHIP_FIRMWARE_BUILD_UNIX_TIME - bool "Make Unix time of compilation available in source code" - default y - help - When enabled, the Unix time of the firmware build is exposed to the - source code and used to initialize the last known UTC time. - config APP_LINK_WITH_CHIP bool "Link 'app' with Connected Home over IP" default y diff --git a/scripts/examples/nrfconnect_example.sh b/scripts/examples/nrfconnect_example.sh index c7378923f82bbf..9c2ffa69380f75 100755 --- a/scripts/examples/nrfconnect_example.sh +++ b/scripts/examples/nrfconnect_example.sh @@ -23,7 +23,7 @@ BOARD="$2" shift 2 # Disable debug symbols and firmware build time to increase ccache hit ratio in CI -COMMON_CI_FLAGS=(-DCONFIG_CHIP_DEBUG_SYMBOLS=n -DCONFIG_CHIP_FIRMWARE_BUILD_UNIX_TIME=n) +COMMON_CI_FLAGS=(-DCONFIG_CHIP_DEBUG_SYMBOLS=n) if [[ ! -f "$APP/nrfconnect/CMakeLists.txt" || -z "$BOARD" ]]; then echo "Usage: $0 " >&2 diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index a5bb4df51d30e2..1ca7fee1a8fdd9 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -41,9 +41,6 @@ if (chip_device_platform != "none" && chip_device_platform != "external") { # Time the firmware was built. chip_device_config_firmware_build_time = "" - # Unix time the firmware was built at (seconds since the epoch) - chip_device_config_firmware_build_unix_time = "" - # Use OpenThread ftd or mtd library chip_device_config_thread_ftd = chip_openthread_ftd @@ -142,9 +139,6 @@ if (chip_device_platform != "none" && chip_device_platform != "external") { if (chip_device_config_firmware_build_time != "") { defines += [ "CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME=\"${chip_device_config_firmware_build_time}\"" ] } - if (chip_device_config_firmware_build_unix_time != "") { - defines += [ "CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME=${chip_device_config_firmware_build_unix_time}" ] - } if (chip_use_transitional_commissionable_data_provider) { defines += [ "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=1" ] diff --git a/src/platform/Zephyr/SystemTimeSupport.cpp b/src/platform/Zephyr/SystemTimeSupport.cpp index 3e847e17635603..08d8408bd8aa38 100644 --- a/src/platform/Zephyr/SystemTimeSupport.cpp +++ b/src/platform/Zephyr/SystemTimeSupport.cpp @@ -42,15 +42,10 @@ ClockImpl gClockImpl; namespace { -// Last known UTC time in Unix format. -#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME -Microseconds64 gRealTime = Seconds64(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_UNIX_TIME); -#else -Microseconds64 gRealTime = Seconds64(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD); -#endif +constexpr Microseconds64 kUnknownRealTime = Seconds64::zero(); -// Monotonic time of setting the last known UTC time. -Microseconds64 gRealTimeSetTime; +// Unix epoch time of boot event +Microseconds64 gBootRealTime = kUnknownRealTime; } // namespace @@ -66,7 +61,11 @@ Milliseconds64 ClockImpl::GetMonotonicMilliseconds64() CHIP_ERROR ClockImpl::GetClock_RealTime(Microseconds64 & aCurTime) { - aCurTime = GetMonotonicMicroseconds64() - gRealTimeSetTime + gRealTime; + // The real time can be configured by an application if it has access to a reliable time source. + // Otherwise, just return an error so that Matter stack can fallback to Last Known UTC Time. + ReturnErrorCodeIf(gBootRealTime == kUnknownRealTime, CHIP_ERROR_INCORRECT_STATE); + + aCurTime = gBootRealTime + GetMonotonicMicroseconds64(); return CHIP_NO_ERROR; } @@ -83,8 +82,7 @@ CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Milliseconds64 & aCurTime) CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime) { - gRealTime = aNewCurTime; - gRealTimeSetTime = GetMonotonicMicroseconds64(); + gBootRealTime = aNewCurTime - GetMonotonicMicroseconds64(); return CHIP_NO_ERROR; }