1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NSTACKX_TIMER_H
17 #define NSTACKX_TIMER_H
18 
19 #include "nstackx_epoll.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define NSTACKX_MILLI_TICKS             1000
26 #define NSTACKX_MICRO_TICKS             1000000
27 #define NSTACKX_NANO_TICKS              1000000000
28 #define NSTACKX_MICRO_SEC_PER_MILLI_SEC (NSTACKX_MICRO_TICKS / NSTACKX_MILLI_TICKS)
29 #define NSTACKX_NANO_SEC_PER_MILLI_SEC  (NSTACKX_NANO_TICKS / NSTACKX_MILLI_TICKS)
30 #define NSTACKX_NANO_SEC_PER_MICRO_SEC  (NSTACKX_NANO_TICKS / NSTACKX_MICRO_TICKS)
31 
32 typedef void (*TimeoutHandle)(void *data);
33 
34 typedef struct  {
35     EpollTask task;
36     TimeoutHandle timeoutHandle;
37     void *data;
38     uint8_t disabled;
39 } Timer;
40 
41 NSTACKX_EXPORT uint32_t GetTimeDiffMs(const struct timespec *etv, const struct timespec *stv);
42 NSTACKX_EXPORT int32_t TimerSetTimeout(Timer *timer, uint32_t timeoutMs, uint8_t repeated);
43 NSTACKX_EXPORT int32_t TimerGetRemainTime(Timer *timer, uint32_t *remainTimeMsPtr);
44 NSTACKX_EXPORT Timer *TimerStart(EpollDesc epollfd, uint32_t ms, uint8_t repeated, TimeoutHandle handle, void *data);
45 NSTACKX_EXPORT void TimerDelete(Timer *timer);
46 NSTACKX_EXPORT uint32_t GetTimeDiffUs(const struct timespec *etv, const struct timespec *stv);
47 
48 #ifdef __cplusplus
49 }
50 #endif
51 
52 #endif // NSTACKX_TIMER_H
53