LCOV - code coverage report
Current view: top level - system - TimeSource.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 0 1 0.0 %
Date: 2024-02-15 08:20:41 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020 Project CHIP Authors
       4             :  *
       5             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6             :  *    you may not use this file except in compliance with the License.
       7             :  *    You may obtain a copy of the License at
       8             :  *
       9             :  *        http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  *    Unless required by applicable law or agreed to in writing, software
      12             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  *    See the License for the specific language governing permissions and
      15             :  *    limitations under the License.
      16             :  */
      17             : 
      18             : /**
      19             :  * @brief defines a generic time source interface that uses a real clock
      20             :  *        at runtime but can be substituted by a test one for unit tests.
      21             :  */
      22             : 
      23             : #pragma once
      24             : 
      25             : #include <lib/support/CodeUtils.h>
      26             : #include <stdlib.h>
      27             : #include <system/SystemClock.h>
      28             : 
      29             : namespace chip {
      30             : namespace Time {
      31             : 
      32             : enum class Source
      33             : {
      34             :     kSystem, // System time source
      35             :     kTest,   // Test time source
      36             : };
      37             : 
      38             : /**
      39             :  * Defines a generic time source within a system. System time and test times
      40             :  * are available.
      41             :  */
      42             : template <Source kSource>
      43             : class TimeSource
      44             : {
      45             : public:
      46             :     /**
      47             :      * Returns a monotonically increasing time in milliseconds since an arbitrary, platform-defined
      48             :      * epoch.
      49             :      *
      50             :      * Maintains requirements for the System::Platform::Layer clock implementation:
      51             :      *
      52             :      *  - Return a value that is ever-increasing (i.e. never * wraps) between reboots of the system.
      53             :      *  - The underlying time source is required to tick continuously during any system sleep modes
      54             :      *    such that the values do not entail a restart upon wake.
      55             :      *  - This function is expected to be thread-safe on any platform that employs threading.
      56             :      */
      57             :     System::Clock::Timestamp GetMonotonicTimestamp();
      58             : };
      59             : 
      60             : /**
      61             :  * A system time source, based on the system platform layer.
      62             :  */
      63             : template <>
      64             : class TimeSource<Source::kSystem>
      65             : {
      66             : public:
      67           0 :     System::Clock::Timestamp GetMonotonicTimestamp() { return System::SystemClock().GetMonotonicTimestamp(); }
      68             : };
      69             : 
      70             : /**
      71             :  * A test time source. Allows setting the current time.
      72             :  */
      73             : template <>
      74             : class TimeSource<Source::kTest>
      75             : {
      76             : public:
      77             :     System::Clock::Timestamp GetMonotonicTimestamp() { return mCurrentTime; }
      78             : 
      79             :     void SetMonotonicTimestamp(System::Clock::Timestamp value)
      80             :     {
      81             :         VerifyOrDie(value >= mCurrentTime);
      82             :         mCurrentTime = value;
      83             :     }
      84             : 
      85             : private:
      86             :     System::Clock::Timestamp mCurrentTime = System::Clock::kZero;
      87             : };
      88             : 
      89             : } // namespace Time
      90             : } // namespace chip

Generated by: LCOV version 1.14