Skip to content

Commit

Permalink
[zephyr] Initialize realtime clock (#18415)
Browse files Browse the repository at this point in the history
* [zephyr] Initialize realtime clock

When CONFIG_CHIP_FIRMWARE_BUILD_UNIX_TIME Kconfig option is
set (it is set by default), use the unix time of compilation
to initialize the last known UTC time on the device.

Signed-off-by: Damian Krolik <[email protected]>

* Apply code review
  • Loading branch information
Damian-Nordic authored May 14, 2022
1 parent 0a4701a commit 25e241e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
5 changes: 5 additions & 0 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ 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: 7 additions & 0 deletions config/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ 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
3 changes: 2 additions & 1 deletion scripts/examples/nrfconnect_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ APP="$1"
BOARD="$2"
shift 2

COMMON_CI_FLAGS=(-DCONFIG_CHIP_DEBUG_SYMBOLS=n)
# 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)

if [[ ! -f "$APP/nrfconnect/CMakeLists.txt" || -z "$BOARD" ]]; then
echo "Usage: $0 <application> <board>" >&2
Expand Down
6 changes: 6 additions & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ 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 @@ -141,6 +144,9 @@ 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
42 changes: 34 additions & 8 deletions src/platform/Zephyr/SystemTimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,58 @@ namespace System {
namespace Clock {

namespace Internal {

ClockImpl gClockImpl;

} // namespace Internal

Microseconds64 ClockImpl::GetMonotonicMicroseconds64(void)
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 * UINT64_C(1000000));
#else
Microseconds64 gRealTime = Seconds64(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD * UINT64_C(1000000));
#endif

// Monotonic time of setting the last known UTC time.
Microseconds64 gRealTimeSetTime;

} // namespace

Microseconds64 ClockImpl::GetMonotonicMicroseconds64()
{
return Microseconds64(k_ticks_to_us_floor64(k_uptime_ticks()));
}

Milliseconds64 ClockImpl::GetMonotonicMilliseconds64(void)
Milliseconds64 ClockImpl::GetMonotonicMilliseconds64()
{
return Milliseconds64(k_uptime_get());
}

CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime)
CHIP_ERROR ClockImpl::GetClock_RealTime(Microseconds64 & aCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
aCurTime = GetMonotonicMicroseconds64() - gRealTimeSetTime + gRealTime;

return CHIP_NO_ERROR;
}

CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime)
CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Milliseconds64 & aCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
Microseconds64 curTimeUs;

ReturnErrorOnFailure(GetClock_RealTime(curTimeUs));
aCurTime = std::chrono::duration_cast<Milliseconds64>(curTimeUs);

return CHIP_NO_ERROR;
}

CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime)
CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
gRealTime = aNewCurTime;
gRealTimeSetTime = GetMonotonicMicroseconds64();

return CHIP_NO_ERROR;
}

} // namespace Clock
Expand Down

0 comments on commit 25e241e

Please sign in to comment.