13d8536b4Sopenharmony_ci/* 23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 43d8536b4Sopenharmony_ci * 53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met: 73d8536b4Sopenharmony_ci * 83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 93d8536b4Sopenharmony_ci * conditions and the following disclaimer. 103d8536b4Sopenharmony_ci * 113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 123d8536b4Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 133d8536b4Sopenharmony_ci * provided with the distribution. 143d8536b4Sopenharmony_ci * 153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 163d8536b4Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 173d8536b4Sopenharmony_ci * permission. 183d8536b4Sopenharmony_ci * 193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 303d8536b4Sopenharmony_ci */ 313d8536b4Sopenharmony_ci 323d8536b4Sopenharmony_ci#ifndef _TIME_IMPL_H 333d8536b4Sopenharmony_ci#define _TIME_IMPL_H 343d8536b4Sopenharmony_ci 353d8536b4Sopenharmony_ci#include <time.h> 363d8536b4Sopenharmony_ci#include <sys/time.h> 373d8536b4Sopenharmony_ci#include <stdint.h> 383d8536b4Sopenharmony_ci#include <errno.h> 393d8536b4Sopenharmony_ci#include <signal.h> 403d8536b4Sopenharmony_ci#include <unistd.h> 413d8536b4Sopenharmony_ci#include "los_debug.h" 423d8536b4Sopenharmony_ci#include "los_task.h" 433d8536b4Sopenharmony_ci#include "los_swtmr.h" 443d8536b4Sopenharmony_ci#include "los_timer.h" 453d8536b4Sopenharmony_ci#include "los_context.h" 463d8536b4Sopenharmony_ci#include "los_compiler.h" 473d8536b4Sopenharmony_ci 483d8536b4Sopenharmony_ci#define OS_SYS_NS_PER_US 1000 493d8536b4Sopenharmony_ci#define OS_SYS_NS_PER_SECOND 1000000000 503d8536b4Sopenharmony_ci#define OS_SYS_US_PER_SECOND 1000000 513d8536b4Sopenharmony_ci#define OS_SYS_MS_PER_SECOND 1000 523d8536b4Sopenharmony_ci 533d8536b4Sopenharmony_ci#define TM_YEAR_BASE 1900 543d8536b4Sopenharmony_ci#define EPOCH_YEAR 1970 553d8536b4Sopenharmony_ci#define SECS_PER_MIN 60 563d8536b4Sopenharmony_ci#define MINS_PER_HOUR 60 573d8536b4Sopenharmony_ci#define SECS_PER_HOUR 3600 /* 60 * 60 */ 583d8536b4Sopenharmony_ci#define SECS_PER_DAY 86400 /* 60 * 60 * 24 */ 593d8536b4Sopenharmony_ci#define SECS_PER_NORMAL_YEAR 31536000 /* 60 * 60 * 24 * 365 */ 603d8536b4Sopenharmony_ci#define DAYS_PER_WEEK 7 613d8536b4Sopenharmony_ci#define DAYS_PER_NORMAL_YEAR 365 623d8536b4Sopenharmony_ci#define DAYS_PER_LEAP_YEAR 366 633d8536b4Sopenharmony_ci#define BEGIN_WEEKDAY 4 643d8536b4Sopenharmony_ci#define TIME_ZONE_MAX 720 /* UTC-12:00 , the last time zone */ 653d8536b4Sopenharmony_ci#define TIME_ZONE_MIN (-840) /* UTC+14:00 , the first time zone */ 663d8536b4Sopenharmony_ci 673d8536b4Sopenharmony_ci/* 683d8536b4Sopenharmony_ci * Nonzero if YEAR is a leap year (every 4 years, 693d8536b4Sopenharmony_ci * except every 100th isn't, and every 400th is). 703d8536b4Sopenharmony_ci */ 713d8536b4Sopenharmony_ci#ifndef IS_LEAP_YEAR 723d8536b4Sopenharmony_ci#define IS_LEAP_YEAR(year) \ 733d8536b4Sopenharmony_ci (((year) % 4 == 0) && (((year) % 100 != 0) || ((year) % 400 == 0))) 743d8536b4Sopenharmony_ci#endif 753d8536b4Sopenharmony_ci 763d8536b4Sopenharmony_ci#define DIV(a, b) (((a) / (b)) - ((a) % (b) < 0)) 773d8536b4Sopenharmony_ci#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) 783d8536b4Sopenharmony_ci 793d8536b4Sopenharmony_ci/* internal functions */ 803d8536b4Sopenharmony_ciSTATIC INLINE BOOL ValidTimeSpec(const struct timespec *tp) 813d8536b4Sopenharmony_ci{ 823d8536b4Sopenharmony_ci /* Fail a NULL pointer */ 833d8536b4Sopenharmony_ci if (tp == NULL) { 843d8536b4Sopenharmony_ci return FALSE; 853d8536b4Sopenharmony_ci } 863d8536b4Sopenharmony_ci 873d8536b4Sopenharmony_ci /* Fail illegal nanosecond values */ 883d8536b4Sopenharmony_ci if ((tp->tv_nsec < 0) || (tp->tv_nsec >= OS_SYS_NS_PER_SECOND) || (tp->tv_sec < 0)) { 893d8536b4Sopenharmony_ci return FALSE; 903d8536b4Sopenharmony_ci } 913d8536b4Sopenharmony_ci 923d8536b4Sopenharmony_ci return TRUE; 933d8536b4Sopenharmony_ci} 943d8536b4Sopenharmony_ci 953d8536b4Sopenharmony_ciSTATIC INLINE UINT32 OsTimeSpec2Tick(const struct timespec *tp) 963d8536b4Sopenharmony_ci{ 973d8536b4Sopenharmony_ci UINT64 tick, ns; 983d8536b4Sopenharmony_ci 993d8536b4Sopenharmony_ci ns = (UINT64)tp->tv_sec * OS_SYS_NS_PER_SECOND + tp->tv_nsec; 1003d8536b4Sopenharmony_ci /* Round up for ticks */ 1013d8536b4Sopenharmony_ci tick = (ns * LOSCFG_BASE_CORE_TICK_PER_SECOND + (OS_SYS_NS_PER_SECOND - 1)) / OS_SYS_NS_PER_SECOND; 1023d8536b4Sopenharmony_ci if (tick > LOS_WAIT_FOREVER) { 1033d8536b4Sopenharmony_ci tick = LOS_WAIT_FOREVER; 1043d8536b4Sopenharmony_ci } 1053d8536b4Sopenharmony_ci return (UINT32)tick; 1063d8536b4Sopenharmony_ci} 1073d8536b4Sopenharmony_ci 1083d8536b4Sopenharmony_ciSTATIC INLINE VOID OsTick2TimeSpec(struct timespec *tp, UINT32 tick) 1093d8536b4Sopenharmony_ci{ 1103d8536b4Sopenharmony_ci UINT64 ns = ((UINT64)tick * OS_SYS_NS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND; 1113d8536b4Sopenharmony_ci tp->tv_sec = (time_t)(ns / OS_SYS_NS_PER_SECOND); 1123d8536b4Sopenharmony_ci tp->tv_nsec = (long)(ns % OS_SYS_NS_PER_SECOND); 1133d8536b4Sopenharmony_ci} 1143d8536b4Sopenharmony_ci 1153d8536b4Sopenharmony_ciSTATIC INLINE INT32 OsGetTickTimeFromNow(const struct timespec *ts, clockid_t clockId, UINT64 *absTicks) 1163d8536b4Sopenharmony_ci{ 1173d8536b4Sopenharmony_ci struct timespec tp; 1183d8536b4Sopenharmony_ci UINT64 nseconds; 1193d8536b4Sopenharmony_ci UINT64 currTime; 1203d8536b4Sopenharmony_ci const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND; 1213d8536b4Sopenharmony_ci 1223d8536b4Sopenharmony_ci if (!ValidTimeSpec(ts)) { 1233d8536b4Sopenharmony_ci return EINVAL; 1243d8536b4Sopenharmony_ci } 1253d8536b4Sopenharmony_ci 1263d8536b4Sopenharmony_ci clock_gettime(clockId, &tp); 1273d8536b4Sopenharmony_ci currTime = (UINT64)tp.tv_sec * OS_SYS_NS_PER_SECOND + tp.tv_nsec; 1283d8536b4Sopenharmony_ci nseconds = (UINT64)ts->tv_sec * OS_SYS_NS_PER_SECOND + ts->tv_nsec; 1293d8536b4Sopenharmony_ci if (currTime >= nseconds) { 1303d8536b4Sopenharmony_ci return ETIMEDOUT; 1313d8536b4Sopenharmony_ci } 1323d8536b4Sopenharmony_ci *absTicks = ((nseconds - currTime) + nsPerTick - 1) / nsPerTick + 1; 1333d8536b4Sopenharmony_ci 1343d8536b4Sopenharmony_ci return 0; 1353d8536b4Sopenharmony_ci} 1363d8536b4Sopenharmony_ci#endif 1373d8536b4Sopenharmony_ci 138