Skip to content

Commit

Permalink
[zephyr] Do not initialize real time clock (#20490) (#20505)
Browse files Browse the repository at this point in the history
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 <[email protected]>

Co-authored-by: Damian Królik <[email protected]>
  • Loading branch information
woody-apple and Damian-Nordic authored Jul 9, 2022
1 parent 7c5c3f0 commit 6874156
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 30 deletions.
5 changes: 0 additions & 5 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
7 changes: 0 additions & 7 deletions config/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/examples/nrfconnect_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <application> <board>" >&2
Expand Down
6 changes: 0 additions & 6 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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" ]
Expand Down
20 changes: 9 additions & 11 deletions src/platform/Zephyr/SystemTimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 6874156

Please sign in to comment.