diff --git a/kal/posix/src/time.c b/kal/posix/src/time.c old mode 100755 new mode 100644 index c3d67ae7..798310ee --- a/kal/posix/src/time.c +++ b/kal/posix/src/time.c @@ -55,16 +55,15 @@ STATIC const UINT16 g_daysInMonth[2][13] = { STATIC const UINT8 g_montbl[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -static UINT64 g_rtcTimeBase = 0; -static UINT64 g_systickBase = (UINT64)-1; - /* - * Time zone information, stored in minutes, + * Time zone information, stored in seconds, * negative values indicate the east of UTC, * positive values indicate the west of UTC. */ -static INT32 g_rtcTimeZone = -480; -static struct tm g_tm = {0}; +long timezone = -8 * 60 * 60; // defaults to CST: 8 hours east of the Prime Meridian + +/* internal shared struct tm object for localtime and gmtime */ +static struct tm g_tm; int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { @@ -405,33 +404,28 @@ int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct clock_t clock(void) { - return LOS_TickCountGet() * OS_MS_PER_TICK; + clock_t clk; + struct timespec hwTime; + OsGetHwTime(&hwTime); + + clk = hwTime.tv_sec * CLOCKS_PER_SEC; + clk += hwTime.tv_nsec / (OS_SYS_NS_PER_SECOND / CLOCKS_PER_SEC); + + return clk; } time_t time(time_t *timer) { - UINT64 usec = 0; - time_t sec; - INT32 rtcRet; + struct timespec ts; - rtcRet = HalGetRtcTime(&usec); - if (rtcRet != 0) { - UINT64 currentTime; - UINT64 tickDelta; - UINT64 currentTick = LOS_TickCountGet(); - if (currentTick > g_systickBase) { - tickDelta = currentTick - g_systickBase; - } - currentTime = g_rtcTimeBase + tickDelta * OS_MS_PER_TICK; - sec = currentTime / OS_SYS_MS_PER_SECOND; - } else { - sec = usec / OS_SYS_US_PER_SECOND; + if (-1 == clock_gettime(CLOCK_REALTIME, &ts)) { + return (time_t)-1; } if (timer != NULL) { - *timer = sec; + *timer = ts.tv_sec; } - return sec; + return ts.tv_sec; } /* @@ -494,23 +488,23 @@ static INT32 ConvertSecs2Utc(time_t t, INT32 offset, struct tm *tp) days -= daysInMonth[month]; tp->tm_mon = month; tp->tm_mday = days + 1; + tp->__tm_gmtoff = -offset; + tp->__tm_zone = NULL; + tp->tm_isdst = 0; return 1; } -struct tm *gmtime_r(const time_t *timer, struct tm *tp) +struct tm *gmtime_r(const time_t *timep, struct tm *result) { - time_t t64; - UINT32 intSave; - if ((timer == NULL) || (tp == NULL)) { + if ((timep == NULL) || (result == NULL)) { + errno = EFAULT; return NULL; } - intSave = LOS_IntLock(); - t64 = *timer; - if (!ConvertSecs2Utc(t64, 0, tp)) { - tp = NULL; + if (!ConvertSecs2Utc(*timep, 0, result)) { + errno = EINVAL; + return NULL; } - (void)LOS_IntRestore(intSave); - return tp; + return result; } struct tm *gmtime(const time_t *timer) @@ -518,22 +512,17 @@ struct tm *gmtime(const time_t *timer) return gmtime_r(timer, &g_tm); } -struct tm *localtime_r(const time_t *timer, struct tm *tp) +struct tm *localtime_r(const time_t *timep, struct tm *result) { - UINT32 intSave; - time_t t64; - INT32 offset; - if ((timer == NULL) || (tp == NULL)) { + if ((timep == NULL) || (result == NULL)) { + errno = EFAULT; return NULL; } - intSave = LOS_IntLock(); - t64 = *timer; - offset = -(g_rtcTimeZone * SECS_PER_MIN); - if (!ConvertSecs2Utc(t64, offset, tp)) { - tp = NULL; + if (!ConvertSecs2Utc(*timep, -timezone, result)) { + errno = EINVAL; + return NULL; } - (void)LOS_IntRestore(intSave); - return tp; + return result; } struct tm *localtime(const time_t *timer) @@ -569,78 +558,89 @@ static time_t ConvertUtc2Secs(struct tm *tm) } seconds += (tm->tm_mday - 1) * SECS_PER_DAY; - seconds += tm->tm_hour * SECS_PER_HOUR + tm->tm_min * SECS_PER_MIN + tm->tm_sec; + seconds += tm->__tm_gmtoff; // add time zone to get UTC time return seconds; } time_t mktime(struct tm *tmptr) { - struct tm tempTime; time_t timeInSeconds; if (tmptr == NULL) { - return 0; + errno = EFAULT; + return (time_t)-1; } - if (tmptr->tm_year < (EPOCH_YEAR - TM_YEAR_BASE)) { - return 0; + + /* tm_isdst is not supported and is ignored */ + if (tmptr->tm_year < (EPOCH_YEAR - TM_YEAR_BASE) || + tmptr->__tm_gmtoff > (TIME_ZONE_MAX * SECS_PER_MIN) || + tmptr->__tm_gmtoff < (TIME_ZONE_MIN * SECS_PER_MIN) || + tmptr->tm_sec > 60 || tmptr->tm_sec < 0 || /* Seconds [0-60] */ + tmptr->tm_min > 59 || tmptr->tm_min < 0 || /* Minutes [0-59] */ + tmptr->tm_hour > 23 || tmptr->tm_hour < 0 || /* Hours [0-23] */ + tmptr->tm_mday > 31 || tmptr->tm_mday < 1 || /* Day of the month [1-31] */ + tmptr->tm_mon > 11 || tmptr->tm_mon < 0) { /* Month [0-11] */ + errno = EOVERFLOW; + return (time_t)-1; } - tempTime = *tmptr; - timeInSeconds = ConvertUtc2Secs(&tempTime); - timeInSeconds += g_rtcTimeZone * SECS_PER_MIN; + timeInSeconds = ConvertUtc2Secs(tmptr); + /* normalize tm_wday and tm_yday */ + ConvertSecs2Utc(timeInSeconds, -tmptr->__tm_gmtoff, tmptr); return timeInSeconds; } int gettimeofday(struct timeval *tv, void *ptz) { - INT32 rtcRet; - INT32 timeZone = 0; - UINT64 usec = 0; - UINT64 currentTime; - UINT64 tickDelta = 0; - UINT64 currentTick; - + struct timespec ts; struct timezone *tz = (struct timezone *)ptz; - if ((tv == NULL) && (tz == NULL)) { - return -1; - } + if (tv != NULL) { - rtcRet = HalGetRtcTime(&usec); - if (rtcRet != 0) { - currentTick = LOS_TickCountGet(); - if (currentTick > g_systickBase) { - tickDelta = currentTick - g_systickBase; - } - currentTime = g_rtcTimeBase + tickDelta * OS_MS_PER_TICK; - tv->tv_sec = currentTime / OS_SYS_MS_PER_SECOND; - tv->tv_usec = (currentTime % OS_SYS_MS_PER_SECOND) * OS_SYS_MS_PER_SECOND; - } else { - tv->tv_sec = usec / OS_SYS_US_PER_SECOND; - tv->tv_usec = usec % OS_SYS_US_PER_SECOND; + if (-1 == clock_gettime(CLOCK_REALTIME, &ts)) { + return -1; } + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / OS_SYS_NS_PER_US; } - HalGetRtcTimeZone(&timeZone); if (tz != NULL) { - tz->tz_minuteswest = timeZone; + tz->tz_minuteswest = timezone / SECS_PER_MIN; + tz->tz_dsttime = 0; } return 0; } int settimeofday(const struct timeval *tv, const struct timezone *tz) { - UINT64 usec; - if ((tv == NULL) || (tz == NULL)) { + struct timespec ts; + INT32 rtcTimeZone = timezone; + + if (tv == NULL) { + errno = EFAULT; return -1; } - g_rtcTimeBase = tv->tv_sec * OS_SYS_MS_PER_SECOND + tv->tv_usec / OS_SYS_MS_PER_SECOND; - g_systickBase = LOS_TickCountGet(); - if ((tz->tz_minuteswest > TIME_ZONE_MIN) && - (tz->tz_minuteswest < TIME_ZONE_MAX)) { - g_rtcTimeZone = tz->tz_minuteswest; + if (tz != NULL) { + if ((tz->tz_minuteswest >= TIME_ZONE_MIN) && + (tz->tz_minuteswest <= TIME_ZONE_MAX)) { + rtcTimeZone = tz->tz_minuteswest * SECS_PER_MIN; + } else { + errno = EINVAL; + return -1; + } } - usec = tv->tv_sec * OS_SYS_US_PER_SECOND + tv->tv_usec; - HalSetRtcTime(g_rtcTimeBase, &usec); - HalSetRtcTimeZone(g_rtcTimeZone); + + if (tv->tv_usec >= OS_SYS_US_PER_SECOND) { + errno = EINVAL; + return -1; + } + + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec * OS_SYS_NS_PER_US; + if (-1 == clock_settime(CLOCK_REALTIME, &ts)) { + return -1; + } + + timezone = rtcTimeZone; + return 0; } diff --git a/kal/posix/src/time_internal.h b/kal/posix/src/time_internal.h index e7181d19..a8fc596f 100644 --- a/kal/posix/src/time_internal.h +++ b/kal/posix/src/time_internal.h @@ -61,8 +61,8 @@ #define DAYS_PER_NORMAL_YEAR 365 #define DAYS_PER_LEAP_YEAR 366 #define BEGIN_WEEKDAY 4 -#define TIME_ZONE_MAX 720 /* 12 * 60 */ -#define TIME_ZONE_MIN (-840) /* -14 * 60 */ +#define TIME_ZONE_MAX 720 /* UTC-12:00 , the last time zone */ +#define TIME_ZONE_MIN (-840) /* UTC+14:00 , the first time zone */ /* * Nonzero if YEAR is a leap year (every 4 years, @@ -72,14 +72,6 @@ #define IS_LEAP_YEAR(year) \ (((year) % 4 == 0) && (((year) % 100 != 0) || ((year) % 400 == 0))) #endif -/* The lowest two bytes indicate minutes of the time zone */ -#ifndef OFFSET_TO_MINUTE -#define OFFSET_TO_MINUTE(time) (((time) < 0) ? (-(time)) : (time)) -#endif -/* The highest 31 bytes, 1 indicates eastern time zoneļ¼? indicates western time zone */ -#ifndef TIME_ZONE_SIGN -#define TIME_ZONE_SIGN(time) ((time) >> 31) -#endif #define DIV(a, b) (((a) / (b)) - ((a) % (b) < 0)) #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) diff --git a/kernel/arch/arm/cortex-m3/keil/los_timer.c b/kernel/arch/arm/cortex-m3/keil/los_timer.c index 9b0d392c..bf871fea 100644 --- a/kernel/arch/arm/cortex-m3/keil/los_timer.c +++ b/kernel/arch/arm/cortex-m3/keil/los_timer.c @@ -135,36 +135,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/arm/cortex-m33/gcc/los_timer.c b/kernel/arch/arm/cortex-m33/gcc/los_timer.c index f73cd37a..eaa4c0af 100644 --- a/kernel/arch/arm/cortex-m33/gcc/los_timer.c +++ b/kernel/arch/arm/cortex-m33/gcc/los_timer.c @@ -134,11 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/arm/cortex-m4/gcc/los_timer.c b/kernel/arch/arm/cortex-m4/gcc/los_timer.c index 5120261c..eaa4c0af 100644 --- a/kernel/arch/arm/cortex-m4/gcc/los_timer.c +++ b/kernel/arch/arm/cortex-m4/gcc/los_timer.c @@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/arm/cortex-m4/iar/los_timer.c b/kernel/arch/arm/cortex-m4/iar/los_timer.c index 5120261c..eaa4c0af 100644 --- a/kernel/arch/arm/cortex-m4/iar/los_timer.c +++ b/kernel/arch/arm/cortex-m4/iar/los_timer.c @@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/arm/cortex-m7/gcc/los_timer.c b/kernel/arch/arm/cortex-m7/gcc/los_timer.c index 5120261c..eaa4c0af 100644 --- a/kernel/arch/arm/cortex-m7/gcc/los_timer.c +++ b/kernel/arch/arm/cortex-m7/gcc/los_timer.c @@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/arm/cortex-m7/iar/los_timer.c b/kernel/arch/arm/cortex-m7/iar/los_timer.c index 5120261c..eaa4c0af 100644 --- a/kernel/arch/arm/cortex-m7/iar/los_timer.c +++ b/kernel/arch/arm/cortex-m7/iar/los_timer.c @@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) __ISB(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - return; -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/arch/include/los_context.h b/kernel/arch/include/los_context.h index 058d7104..493f33ff 100755 --- a/kernel/arch/include/los_context.h +++ b/kernel/arch/include/los_context.h @@ -82,7 +82,7 @@ extern VOID *HalTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack); * * @see None. */ -LITE_OS_SEC_TEXT_MINOR VOID HalSysExit(VOID); +LITE_OS_SEC_TEXT_MINOR VOID HalSysExit(VOID) NORETURN; /* * * @ingroup los_context diff --git a/kernel/arch/include/los_timer.h b/kernel/arch/include/los_timer.h old mode 100755 new mode 100644 index 0021ef2a..1f773d20 --- a/kernel/arch/include/los_timer.h +++ b/kernel/arch/include/los_timer.h @@ -69,16 +69,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep); VOID HalDelay(UINT32 ticks); -UINT64 HalGetExpandTick(VOID); - -INT32 HalGetRtcTime(UINT64 *usec); - -INT32 HalGetRtcTimeZone(INT32 *timeZone); - -INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec); - -INT32 HalSetRtcTimeZone(INT32 timeZone); - /** * @ingroup los_timer * @brief Get systick cycle. diff --git a/kernel/arch/risc-v/riscv32/gcc/los_timer.c b/kernel/arch/risc-v/riscv32/gcc/los_timer.c index e1ce4c77..b8bd84ac 100644 --- a/kernel/arch/risc-v/riscv32/gcc/los_timer.c +++ b/kernel/arch/risc-v/riscv32/gcc/los_timer.c @@ -44,7 +44,6 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -#define NS_PER_SECOND 1000000000.0 WEAK UINT32 HalTickStart(OS_TICK_HANDLER handler) { @@ -108,36 +107,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep) wfi(); } -WEAK VOID HalDelay(UINT32 ticks) -{ - -} - -WEAK UINT64 HalGetExpandTick(VOID) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTime(UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalGetRtcTimeZone(INT32 *timeZone) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTime(UINT64 utcTime, UINT64 *usec) -{ - return LOS_OK; -} - -WEAK INT32 HalSetRtcTimeZone(INT32 timeZone) -{ - return LOS_OK; -} - #ifdef __cplusplus #if __cplusplus } diff --git a/kernel/include/los_task.h b/kernel/include/los_task.h index 402f5636..e0af07fc 100644 --- a/kernel/include/los_task.h +++ b/kernel/include/los_task.h @@ -504,8 +504,8 @@ extern VOID LOS_Msleep(UINT32 mSecs); * @see */ extern UINT32 LOS_Start(VOID); -extern VOID LOS_Reboot(VOID); -extern VOID LOS_Panic(const CHAR *fmt, ...); +extern VOID LOS_Reboot(VOID) NORETURN; +extern VOID LOS_Panic(const CHAR *fmt, ...) NORETURN; /** diff --git a/utils/los_compiler.h b/utils/los_compiler.h index 94d08417..6d1579ca 100755 --- a/utils/los_compiler.h +++ b/utils/los_compiler.h @@ -70,6 +70,10 @@ extern "C" { #define CLZ __iar_builtin_CLZ #endif +#ifndef NORETURN +#define NORETURN __attribute__ ((__noreturn__)) +#endif + /* for ARM Compiler */ #elif defined(__CC_ARM) @@ -97,6 +101,10 @@ extern "C" { #define CLZ __clz #endif +#ifndef NORETURN +#define NORETURN __declspec(noreturn) +#endif + #pragma anon_unions /* for GNU Compiler */ @@ -126,6 +134,10 @@ extern "C" { #define CLZ __builtin_clz #endif +#ifndef NORETURN +#define NORETURN __attribute__ ((__noreturn__)) +#endif + #else #error Unknown compiler. #endif