3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This is an internal header file that defines the interface to a platform-supplied
22 * function for retrieving the current system time.
27 // Include configuration headers
28 #include <system/SystemConfig.h>
30 // Include dependent headers
31 #include <support/DLLUtil.h>
33 #include <system/SystemError.h>
40 kTimerFactor_nano_per_micro = 1000, /** Number of nanoseconds in a microsecond. */
41 kTimerFactor_micro_per_milli = 1000, /** Number of microseconds in a millisecond. */
42 kTimerFactor_milli_per_unit = 1000, /** Number of milliseconds in a second. */
44 kTimerFactor_nano_per_milli = 1000000, /** Number of nanoseconds in a millisecond. */
45 kTimerFactor_micro_per_unit = 1000000 /** Number of microseconds in a second. */
53 * Platform-specific function for getting monotonic system time in microseconds.
55 * This function is expected to return elapsed time in microseconds since an arbitrary, platform-defined
56 * epoch. Platform implementations are obligated to return a value that is ever-increasing (i.e. never
57 * wraps) between reboots of the system. Additionally, the underlying time source is required to tick
58 * continuously during any system sleep modes that do not entail a restart upon wake.
60 * The epoch for time returned by this function is *not* required to be the same that for any of the
61 * other GetClock... functions, including GetClock_MonotonicMS().
63 * This function is expected to be thread-safe on any platform that employs threading.
66 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
67 * Layer should call System::Layer::GetClock_Monotonic().
69 * @returns Elapsed time in microseconds since an arbitrary, platform-defined epoch.
71 extern uint64_t GetClock_Monotonic();
75 * Platform-specific function for getting monotonic system time in milliseconds.
77 * This function is expected to return elapsed time in milliseconds since an arbitrary, platform-defined
78 * epoch. Platform implementations are obligated to return a value that is ever-increasing (i.e. never
79 * wraps) between reboots of the system. Additionally, the underlying time source is required to tick
80 * continuously during any system sleep modes that do not entail a restart upon wake.
82 * The epoch for time returned by this function is *not* required to be the same as that for any of the
83 * other GetClock... functions, including GetClock_Monotonic().
85 * This function is expected to be thread-safe on any platform that employs threading.
88 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
89 * Layer should call System::Layer::GetClock_MonotonicMS().
91 * @returns Elapsed time in milliseconds since an arbitrary, platform-defined epoch.
93 extern uint64_t GetClock_MonotonicMS();
97 * Platform-specific function for getting high-resolution monotonic system time in microseconds.
99 * This function is expected to return elapsed time in microseconds since an arbitrary, platform-defined
100 * epoch. Values returned by GetClock_MonotonicHiRes() are required to be ever-increasing (i.e. never
101 * wrap). However, the underlying timer is *not* required to tick continuously during system
104 * Platform are encouraged to implement GetClock_MonotonicHiRes() using a high-resolution timer
105 * that is not subject to gradual clock adjustments (slewing). On platforms without such a timer,
106 * GetClock_MonotonicHiRes() can return the same value as GetClock_Monotonic().
108 * The epoch for time returned by this function is not required to be the same that for any of the
109 * other GetClock... functions.
111 * This function is expected to be thread-safe on any platform that employs threading.
114 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
115 * Layer should call System::Layer::GetClock_MonotonicHiRes().
117 * @returns Elapsed time in microseconds since an arbitrary, platform-defined epoch.
119 extern uint64_t GetClock_MonotonicHiRes();
123 * Platform-specific function for getting the current real (civil) time in microsecond Unix time
126 * This function is expected to return the local platform's notion of current real time, expressed
127 * as a Unix time value scaled to microseconds. The underlying clock is required to tick at a
128 * rate of least at whole seconds (values of 1,000,000), but may tick faster.
130 * On those platforms that are capable of tracking real time, GetClock_RealTime() must return the
131 * error CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED whenever the system is unsynchronized with real time.
133 * Platforms that are incapable of tracking real time should not implement the GetClock_RealTime()
134 * function, thereby forcing link-time failures of features that depend on access to real time.
135 * Alternatively, such platforms may supply an implementation of GetClock_RealTime() that returns
136 * the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED.
138 * This function is expected to be thread-safe on any platform that employs threading.
141 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
142 * Layer should call System::Layer::GetClock_RealTime().
144 * @param[out] curTime The current time, expressed as Unix time scaled to microseconds.
146 * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
147 * @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
148 * If the platform is capable of tracking real time, but is
149 * is currently unsynchronized.
150 * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
151 * If the platform is incapable of tracking real time.
153 extern Error GetClock_RealTime(uint64_t & curTime);
157 * Platform-specific function for getting the current real (civil) time in millisecond Unix time
160 * This function is expected to return the local platform's notion of current real time, expressed
161 * as a Unix time value scaled to milliseconds.
163 * See the documentation for GetClock_RealTime() for details on the expected behavior.
166 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
167 * Layer should call System::Layer::GetClock_RealTimeMS().
169 * @param[out] curTimeMS The current time, expressed as Unix time scaled to milliseconds.
171 * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
172 * @retval #CHIP_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED
173 * If the platform is capable of tracking real time, but is
174 * is currently unsynchronized.
175 * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
176 * If the platform is incapable of tracking real time.
178 extern Error GetClock_RealTimeMS(uint64_t & curTimeMS);
182 * Platform-specific function for setting the current real (civil) time.
184 * CHIP calls this function to set the local platform's notion of current real time. The new current
185 * time is expressed as a Unix time value scaled to microseconds.
187 * Once set, underlying platform clock is expected to track real time with a granularity of at least whole
190 * On platforms that support tracking real time, the SetClock_RealTime() function must return the error
191 * CHIP_SYSTEM_ERROR_ACCESS_DENIED if the calling application does not have the privilege to set the
194 * Platforms that are incapable of tracking real time, or do not offer the ability to set real time,
195 * should not implement the SetClock_RealTime() function, thereby forcing link-time failures of features
196 * that depend on setting real time. Alternatively, such platforms may supply an implementation of
197 * SetClock_RealTime() that returns the error CHIP_SYSTEM_ERROR_NOT_SUPPORTED.
199 * This function is expected to be thread-safe on any platform that employs threading.
202 * This function is reserved for internal use by the CHIP System Layer. Users of the CHIP System
203 * Layer should call System::Layer::GetClock_RealTimeMS().
205 * @param[in] newCurTime The new current time, expressed as Unix time scaled to microseconds.
207 * @retval #CHIP_SYSTEM_NO_ERROR If the method succeeded.
208 * @retval #CHIP_SYSTEM_ERROR_NOT_SUPPORTED
209 * If the platform is incapable of tracking real time.
210 * @retval #CHIP_SYSTEM_ERROR_ACCESS_DENIED
211 * If the calling application does not have the privilege to set the
214 extern Error SetClock_RealTime(uint64_t newCurTime);
217 } // namespace Platform
218 } // namespace System