1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _TIME_IMPL_H
33 #define _TIME_IMPL_H
34
35 #include <time.h>
36 #include <sys/time.h>
37 #include <stdint.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include "los_debug.h"
42 #include "los_task.h"
43 #include "los_swtmr.h"
44 #include "los_timer.h"
45 #include "los_context.h"
46 #include "los_compiler.h"
47
48 #define OS_SYS_NS_PER_US 1000
49 #define OS_SYS_NS_PER_SECOND 1000000000
50 #define OS_SYS_US_PER_SECOND 1000000
51 #define OS_SYS_MS_PER_SECOND 1000
52
53 #define TM_YEAR_BASE 1900
54 #define EPOCH_YEAR 1970
55 #define SECS_PER_MIN 60
56 #define MINS_PER_HOUR 60
57 #define SECS_PER_HOUR 3600 /* 60 * 60 */
58 #define SECS_PER_DAY 86400 /* 60 * 60 * 24 */
59 #define SECS_PER_NORMAL_YEAR 31536000 /* 60 * 60 * 24 * 365 */
60 #define DAYS_PER_WEEK 7
61 #define DAYS_PER_NORMAL_YEAR 365
62 #define DAYS_PER_LEAP_YEAR 366
63 #define BEGIN_WEEKDAY 4
64 #define TIME_ZONE_MAX 720 /* UTC-12:00 , the last time zone */
65 #define TIME_ZONE_MIN (-840) /* UTC+14:00 , the first time zone */
66
67 /*
68 * Nonzero if YEAR is a leap year (every 4 years,
69 * except every 100th isn't, and every 400th is).
70 */
71 #ifndef IS_LEAP_YEAR
72 #define IS_LEAP_YEAR(year) \
73 (((year) % 4 == 0) && (((year) % 100 != 0) || ((year) % 400 == 0)))
74 #endif
75
76 #define DIV(a, b) (((a) / (b)) - ((a) % (b) < 0))
77 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
78
79 /* internal functions */
ValidTimeSpec(const struct timespec * tp)80 STATIC INLINE BOOL ValidTimeSpec(const struct timespec *tp)
81 {
82 /* Fail a NULL pointer */
83 if (tp == NULL) {
84 return FALSE;
85 }
86
87 /* Fail illegal nanosecond values */
88 if ((tp->tv_nsec < 0) || (tp->tv_nsec >= OS_SYS_NS_PER_SECOND) || (tp->tv_sec < 0)) {
89 return FALSE;
90 }
91
92 return TRUE;
93 }
94
OsTimeSpec2Tick(const struct timespec * tp)95 STATIC INLINE UINT32 OsTimeSpec2Tick(const struct timespec *tp)
96 {
97 UINT64 tick, ns;
98
99 ns = (UINT64)tp->tv_sec * OS_SYS_NS_PER_SECOND + tp->tv_nsec;
100 /* Round up for ticks */
101 tick = (ns * LOSCFG_BASE_CORE_TICK_PER_SECOND + (OS_SYS_NS_PER_SECOND - 1)) / OS_SYS_NS_PER_SECOND;
102 if (tick > LOS_WAIT_FOREVER) {
103 tick = LOS_WAIT_FOREVER;
104 }
105 return (UINT32)tick;
106 }
107
OsTick2TimeSpec(struct timespec * tp,UINT32 tick)108 STATIC INLINE VOID OsTick2TimeSpec(struct timespec *tp, UINT32 tick)
109 {
110 UINT64 ns = ((UINT64)tick * OS_SYS_NS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND;
111 tp->tv_sec = (time_t)(ns / OS_SYS_NS_PER_SECOND);
112 tp->tv_nsec = (long)(ns % OS_SYS_NS_PER_SECOND);
113 }
114
OsGetTickTimeFromNow(const struct timespec * ts,clockid_t clockId,UINT64 * absTicks)115 STATIC INLINE INT32 OsGetTickTimeFromNow(const struct timespec *ts, clockid_t clockId, UINT64 *absTicks)
116 {
117 struct timespec tp;
118 UINT64 nseconds;
119 UINT64 currTime;
120 const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
121
122 if (!ValidTimeSpec(ts)) {
123 return EINVAL;
124 }
125
126 clock_gettime(clockId, &tp);
127 currTime = (UINT64)tp.tv_sec * OS_SYS_NS_PER_SECOND + tp.tv_nsec;
128 nseconds = (UINT64)ts->tv_sec * OS_SYS_NS_PER_SECOND + ts->tv_nsec;
129 if (currTime >= nseconds) {
130 return ETIMEDOUT;
131 }
132 *absTicks = ((nseconds - currTime) + nsPerTick - 1) / nsPerTick + 1;
133
134 return 0;
135 }
136 #endif
137
138