Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemTimer.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file defines the chip::System::Timer class and its
22  *      related types used for representing an in-progress one-shot
23  *      timer.
24  */
25
26 #pragma once
27
28 // Include configuration headers
29 #include <system/SystemConfig.h>
30
31 // Include dependent headers
32 #include <support/DLLUtil.h>
33
34 #include <system/SystemClock.h>
35 #include <system/SystemError.h>
36 #include <system/SystemObject.h>
37 #include <system/SystemStats.h>
38
39 namespace chip {
40 namespace System {
41
42 class Layer;
43
44 /**
45  * @class Timer
46  *
47  * @brief
48  *  This is an internal class to CHIP System Layer, used to represent an in-progress one-shot timer. There is no real public
49  *  interface available for the application layer. The static public methods used to acquire current system time are intended for
50  *  internal use.
51  *
52  */
53 class DLL_EXPORT Timer : public Object
54 {
55     friend class Layer;
56
57 public:
58     /**
59      *  Represents an epoch in the local system timescale, usually the POSIX timescale.
60      *
61      *  The units are dependent on the context. If used with values returned by GetCurrentEpoch, the units are milliseconds.
62      */
63     typedef uint64_t Epoch;
64
65     static Epoch GetCurrentEpoch();
66     static bool IsEarlierEpoch(const Epoch & first, const Epoch & second);
67
68     typedef void (*OnCompleteFunct)(Layer * aLayer, void * aAppState, Error aError);
69     OnCompleteFunct OnComplete;
70
71     Error Start(uint32_t aDelayMilliseconds, OnCompleteFunct aOnComplete, void * aAppState);
72     Error Cancel();
73
74     static void GetStatistics(chip::System::Stats::count_t & aNumInUse, chip::System::Stats::count_t & aHighWatermark);
75
76 private:
77     static ObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> sPool;
78
79     Epoch mAwakenEpoch;
80
81     void HandleComplete();
82
83     Error ScheduleWork(OnCompleteFunct aOnComplete, void * aAppState);
84
85 #if CHIP_SYSTEM_CONFIG_USE_LWIP
86     Timer * mNextTimer;
87
88     static Error HandleExpiredTimers(Layer & aLayer);
89 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
90
91     // Not defined
92     Timer(const Timer &) = delete;
93     Timer & operator=(const Timer &) = delete;
94 };
95
96 inline void Timer::GetStatistics(chip::System::Stats::count_t & aNumInUse, chip::System::Stats::count_t & aHighWatermark)
97 {
98     sPool.GetStatistics(aNumInUse, aHighWatermark);
99 }
100
101 } // namespace System
102 } // namespace chip