IssueNo:#I3IK07

Description:Resolve the conflict.
Feature or Bugfix:Feature
Binary Source:No

Change-Id: I882dcb9e523cab30e6ab4889099984fe0302f92e
This commit is contained in:
zhushengle 2021-04-17 11:03:45 +08:00
commit c5f517667a
13 changed files with 105 additions and 297 deletions

176
kal/posix/src/time.c Executable file → Normal file
View File

@ -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 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, * negative values indicate the east of UTC,
* positive values indicate the west of UTC. * positive values indicate the west of UTC.
*/ */
static INT32 g_rtcTimeZone = -480; long timezone = -8 * 60 * 60; // defaults to CST: 8 hours east of the Prime Meridian
static struct tm g_tm = {0};
/* internal shared struct tm object for localtime and gmtime */
static struct tm g_tm;
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) 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) 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) time_t time(time_t *timer)
{ {
UINT64 usec = 0; struct timespec ts;
time_t sec;
INT32 rtcRet;
rtcRet = HalGetRtcTime(&usec); if (-1 == clock_gettime(CLOCK_REALTIME, &ts)) {
if (rtcRet != 0) { return (time_t)-1;
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 (timer != NULL) { 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]; days -= daysInMonth[month];
tp->tm_mon = month; tp->tm_mon = month;
tp->tm_mday = days + 1; tp->tm_mday = days + 1;
tp->__tm_gmtoff = -offset;
tp->__tm_zone = NULL;
tp->tm_isdst = 0;
return 1; 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; if ((timep == NULL) || (result == NULL)) {
UINT32 intSave; errno = EFAULT;
if ((timer == NULL) || (tp == NULL)) {
return NULL; return NULL;
} }
intSave = LOS_IntLock(); if (!ConvertSecs2Utc(*timep, 0, result)) {
t64 = *timer; errno = EINVAL;
if (!ConvertSecs2Utc(t64, 0, tp)) { return NULL;
tp = NULL;
} }
(void)LOS_IntRestore(intSave); return result;
return tp;
} }
struct tm *gmtime(const time_t *timer) struct tm *gmtime(const time_t *timer)
@ -518,22 +512,17 @@ struct tm *gmtime(const time_t *timer)
return gmtime_r(timer, &g_tm); 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; if ((timep == NULL) || (result == NULL)) {
time_t t64; errno = EFAULT;
INT32 offset;
if ((timer == NULL) || (tp == NULL)) {
return NULL; return NULL;
} }
intSave = LOS_IntLock(); if (!ConvertSecs2Utc(*timep, -timezone, result)) {
t64 = *timer; errno = EINVAL;
offset = -(g_rtcTimeZone * SECS_PER_MIN); return NULL;
if (!ConvertSecs2Utc(t64, offset, tp)) {
tp = NULL;
} }
(void)LOS_IntRestore(intSave); return result;
return tp;
} }
struct tm *localtime(const time_t *timer) 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_mday - 1) * SECS_PER_DAY;
seconds += tm->tm_hour * SECS_PER_HOUR + tm->tm_min * SECS_PER_MIN + tm->tm_sec; 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; return seconds;
} }
time_t mktime(struct tm *tmptr) time_t mktime(struct tm *tmptr)
{ {
struct tm tempTime;
time_t timeInSeconds; time_t timeInSeconds;
if (tmptr == NULL) { 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(tmptr);
timeInSeconds = ConvertUtc2Secs(&tempTime); /* normalize tm_wday and tm_yday */
timeInSeconds += g_rtcTimeZone * SECS_PER_MIN; ConvertSecs2Utc(timeInSeconds, -tmptr->__tm_gmtoff, tmptr);
return timeInSeconds; return timeInSeconds;
} }
int gettimeofday(struct timeval *tv, void *ptz) int gettimeofday(struct timeval *tv, void *ptz)
{ {
INT32 rtcRet; struct timespec ts;
INT32 timeZone = 0;
UINT64 usec = 0;
UINT64 currentTime;
UINT64 tickDelta = 0;
UINT64 currentTick;
struct timezone *tz = (struct timezone *)ptz; struct timezone *tz = (struct timezone *)ptz;
if ((tv == NULL) && (tz == NULL)) {
return -1;
}
if (tv != NULL) { if (tv != NULL) {
rtcRet = HalGetRtcTime(&usec); if (-1 == clock_gettime(CLOCK_REALTIME, &ts)) {
if (rtcRet != 0) { return -1;
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;
} }
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / OS_SYS_NS_PER_US;
} }
HalGetRtcTimeZone(&timeZone);
if (tz != NULL) { if (tz != NULL) {
tz->tz_minuteswest = timeZone; tz->tz_minuteswest = timezone / SECS_PER_MIN;
tz->tz_dsttime = 0;
} }
return 0; return 0;
} }
int settimeofday(const struct timeval *tv, const struct timezone *tz) int settimeofday(const struct timeval *tv, const struct timezone *tz)
{ {
UINT64 usec; struct timespec ts;
if ((tv == NULL) || (tz == NULL)) { INT32 rtcTimeZone = timezone;
if (tv == NULL) {
errno = EFAULT;
return -1; return -1;
} }
g_rtcTimeBase = tv->tv_sec * OS_SYS_MS_PER_SECOND + tv->tv_usec / OS_SYS_MS_PER_SECOND; if (tz != NULL) {
g_systickBase = LOS_TickCountGet(); if ((tz->tz_minuteswest >= TIME_ZONE_MIN) &&
if ((tz->tz_minuteswest > TIME_ZONE_MIN) && (tz->tz_minuteswest <= TIME_ZONE_MAX)) {
(tz->tz_minuteswest < TIME_ZONE_MAX)) { rtcTimeZone = tz->tz_minuteswest * SECS_PER_MIN;
g_rtcTimeZone = tz->tz_minuteswest; } else {
errno = EINVAL;
return -1;
}
} }
usec = tv->tv_sec * OS_SYS_US_PER_SECOND + tv->tv_usec;
HalSetRtcTime(g_rtcTimeBase, &usec); if (tv->tv_usec >= OS_SYS_US_PER_SECOND) {
HalSetRtcTimeZone(g_rtcTimeZone); 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; return 0;
} }

View File

@ -61,8 +61,8 @@
#define DAYS_PER_NORMAL_YEAR 365 #define DAYS_PER_NORMAL_YEAR 365
#define DAYS_PER_LEAP_YEAR 366 #define DAYS_PER_LEAP_YEAR 366
#define BEGIN_WEEKDAY 4 #define BEGIN_WEEKDAY 4
#define TIME_ZONE_MAX 720 /* 12 * 60 */ #define TIME_ZONE_MAX 720 /* UTC-12:00 , the last time zone */
#define TIME_ZONE_MIN (-840) /* -14 * 60 */ #define TIME_ZONE_MIN (-840) /* UTC+14:00 , the first time zone */
/* /*
* Nonzero if YEAR is a leap year (every 4 years, * Nonzero if YEAR is a leap year (every 4 years,
@ -72,14 +72,6 @@
#define IS_LEAP_YEAR(year) \ #define IS_LEAP_YEAR(year) \
(((year) % 4 == 0) && (((year) % 100 != 0) || ((year) % 400 == 0))) (((year) % 4 == 0) && (((year) % 100 != 0) || ((year) % 400 == 0)))
#endif #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 DIV(a, b) (((a) / (b)) - ((a) % (b) < 0))
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))

View File

@ -135,36 +135,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -134,11 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __ISB();
} }
WEAK VOID HalDelay(UINT32 ticks)
{
return;
}
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -134,36 +134,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
__ISB(); __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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -82,7 +82,7 @@ extern VOID *HalTskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack);
* <ul><li>los_hw.h: the header file that contains the API declaration.</li></ul> * <ul><li>los_hw.h: the header file that contains the API declaration.</li></ul>
* @see None. * @see None.
*/ */
LITE_OS_SEC_TEXT_MINOR VOID HalSysExit(VOID); LITE_OS_SEC_TEXT_MINOR VOID HalSysExit(VOID) NORETURN;
/* * /* *
* @ingroup los_context * @ingroup los_context

10
kernel/arch/include/los_timer.h Executable file → Normal file
View File

@ -69,16 +69,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep);
VOID HalDelay(UINT32 ticks); 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 * @ingroup los_timer
* @brief Get systick cycle. * @brief Get systick cycle.

View File

@ -44,7 +44,6 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* __cplusplus */ #endif /* __cplusplus */
#define NS_PER_SECOND 1000000000.0
WEAK UINT32 HalTickStart(OS_TICK_HANDLER handler) WEAK UINT32 HalTickStart(OS_TICK_HANDLER handler)
{ {
@ -108,36 +107,6 @@ VOID HalEnterSleep(LOS_SysSleepEnum sleep)
wfi(); 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 #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }

View File

@ -504,8 +504,8 @@ extern VOID LOS_Msleep(UINT32 mSecs);
* @see * @see
*/ */
extern UINT32 LOS_Start(VOID); extern UINT32 LOS_Start(VOID);
extern VOID LOS_Reboot(VOID); extern VOID LOS_Reboot(VOID) NORETURN;
extern VOID LOS_Panic(const CHAR *fmt, ...); extern VOID LOS_Panic(const CHAR *fmt, ...) NORETURN;
/** /**

View File

@ -70,6 +70,10 @@ extern "C" {
#define CLZ __iar_builtin_CLZ #define CLZ __iar_builtin_CLZ
#endif #endif
#ifndef NORETURN
#define NORETURN __attribute__ ((__noreturn__))
#endif
/* for ARM Compiler */ /* for ARM Compiler */
#elif defined(__CC_ARM) #elif defined(__CC_ARM)
@ -97,6 +101,10 @@ extern "C" {
#define CLZ __clz #define CLZ __clz
#endif #endif
#ifndef NORETURN
#define NORETURN __declspec(noreturn)
#endif
#pragma anon_unions #pragma anon_unions
/* for GNU Compiler */ /* for GNU Compiler */
@ -126,6 +134,10 @@ extern "C" {
#define CLZ __builtin_clz #define CLZ __builtin_clz
#endif #endif
#ifndef NORETURN
#define NORETURN __attribute__ ((__noreturn__))
#endif
#else #else
#error Unknown compiler. #error Unknown compiler.
#endif #endif