From 8e2a456d7a1ffff4a35ba9f4c5a199eac14e132e Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 15 Apr 2021 14:01:55 +0800 Subject: [PATCH 1/3] implement set_timeval --- components/libc/compilers/common/sys/time.h | 5 +- components/libc/compilers/common/time.c | 185 +++++++++++++------- include/rtdef.h | 8 +- 3 files changed, 130 insertions(+), 68 deletions(-) diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 2a40b7b805..ef1c810608 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -40,7 +40,7 @@ struct timeval { #endif #endif /* _TIMEVAL_DEFINED */ -#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) && !defined (__ICCARM__) && !defined (_WIN32) +#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && !defined (__ICCARM__) && !defined (_WIN32) struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* and nanoseconds */ @@ -56,6 +56,9 @@ int stime(const time_t *t); time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); +#if defined(__ARMCC_VERSION) || defined (__ICCARM__) +struct tm *gmtime_r(const time_t *timep, struct tm *r); +#endif #ifdef RT_USING_POSIX #include diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index c2a3c8fcbf..75adb817da 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -68,6 +68,104 @@ static void num2str(char *c, int i) c[1] = i % 10 + '0'; } +/** + * Get time from RTC device (without timezone) + * @param tv: struct timeval + * @return -1 failure; 1 success + */ +static int get_timeval(struct timeval *tv) +{ +#ifdef RT_USING_RTC + static rt_device_t device = RT_NULL; +#endif + rt_err_t rst = -RT_ERROR; + + if (tv == RT_NULL) + return -1; + + /* default is 0 */ + tv->tv_sec = 0; + tv->tv_usec = 0; + +#ifdef RT_USING_RTC + /* optimization: find rtc device only first */ + if (device == RT_NULL) + { + device = rt_device_find("rtc"); + } + + /* read timestamp from RTC device */ + if (device != RT_NULL) + { + if (rt_device_open(device, 0) == RT_EOK) + { + rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec); + rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec); + rt_device_close(device); + } + } + else + { + /* LOG_W will cause a recursive printing if ulog timestamp function is turned on */ + rt_kprintf("Cannot find a RTC device to provide time!\r\n"); + return -1; + } + + return (rst < 0) ? -1 : 1; + +#else + /* LOG_W will cause a recursive printing if ulog timestamp function is turned on */ + rt_kprintf("Cannot find a RTC device to provide time!\r\n"); + return -1; +#endif /* RT_USING_RTC */ +} + +/** + * Set time to RTC device (without timezone) + * @param tv: struct timeval + * @return -1 failure; 1 success + */ +static int set_timeval(struct timeval *tv) +{ + rt_err_t rst = -RT_ERROR; +#ifdef RT_USING_RTC + static rt_device_t device = RT_NULL; +#endif + + if (tv == RT_NULL) + return -1; + +#ifdef RT_USING_RTC + /* optimization: find rtc device only first */ + if (device == RT_NULL) + { + device = rt_device_find("rtc"); + } + + /* read timestamp from RTC device */ + if (device != RT_NULL) + { + if (rt_device_open(device, 0) == RT_EOK) + { + rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec); + rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME_US, &tv->tv_usec); + rt_device_close(device); + } + } + else + { + LOG_W("Cannot find a RTC device to provide time!"); + return -1; + } + + return (rst < 0) ? -1 : 1; + +#else + LOG_W("Cannot find a RTC device to provide time!"); + return -1; +#endif /* RT_USING_RTC */ +} + struct tm *gmtime_r(const time_t *timep, struct tm *r) { time_t i; @@ -183,49 +281,6 @@ char* ctime(const time_t *tim_p) } RTM_EXPORT(ctime); -/*-1 failure; 1 success*/ -static int get_timeval(struct timeval *tv) -{ - if (tv == RT_NULL) - return -1; - - /* default is not available */ - tv->tv_sec = -1; - /* default is 0 */ - tv->tv_usec = 0; - -#ifdef RT_USING_RTC - static rt_device_t device = RT_NULL; - - /* optimization: find rtc device only first */ - if (device == RT_NULL) - { - device = rt_device_find("rtc"); - } - - /* read timestamp from RTC device */ - if (device != RT_NULL) - { - if (rt_device_open(device, 0) == RT_EOK) - { - rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec); - rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec); - rt_device_close(device); - } - } -#endif /* RT_USING_RTC */ - - if (tv->tv_sec == (time_t) -1) - { - /* LOG_W will cause a recursive printing if ulog timestamp function is turned on */ - rt_kprintf("Cannot find a RTC device to provide time!\r\n"); - tv->tv_sec = 0; - return -1; - } - - return 1; -} - /** * Returns the current time. * @@ -239,7 +294,7 @@ RT_WEAK time_t time(time_t *t) { struct timeval now; - if(get_timeval(&now)>0) + if(get_timeval(&now) > 0) { if (t) { @@ -263,28 +318,24 @@ RTM_EXPORT(clock); int stime(const time_t *t) { -#ifdef RT_USING_RTC - rt_device_t device; + struct timeval tv; - /* read timestamp from RTC device. */ - device = rt_device_find("rtc"); - if (rt_device_open(device, 0) == RT_EOK) + if (!t) + { + errno = EFAULT; + return -1; + } + + tv.tv_sec = *t; + if (set_timeval(&tv) > 0) + { + return 0; + } + else { - rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, (void*)t); - rt_device_close(device); - } - else - { - LOG_W("Cannot find a RTC device to provide time!"); errno = EFAULT; return -1; } - return 0; -#else - LOG_W("Cannot find a RTC device to provide time!"); - errno = EFAULT; - return -1; -#endif /* RT_USING_RTC */ } RTM_EXPORT(stime); @@ -366,7 +417,7 @@ RTM_EXPORT(timegm); /* TODO: timezone */ int gettimeofday(struct timeval *tv, struct timezone *tz) { - if (tv != RT_NULL && get_timeval(tv)>0) + if (tv != RT_NULL && get_timeval(tv) > 0) { return 0; } @@ -385,7 +436,15 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) { if(tv->tv_sec >= 0 && tv->tv_usec >= 0) { - return stime((const time_t *)&tv->tv_sec); + if(set_timeval((struct timeval *)tv) > 0) + { + return 0; + } + else + { + errno = EFAULT; + return -1; + } } else { diff --git a/include/rtdef.h b/include/rtdef.h index aba8a6f12d..111ac4a12c 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -964,10 +964,10 @@ enum rt_device_class_type #define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */ #define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */ #define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */ -#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x12 /**< get alarm */ -#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */ -#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x14 /**< get microsecond time */ -#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x15 /**< set microsecond time */ +#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */ +#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */ +#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */ +#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */ typedef struct rt_device *rt_device_t; From b74022e2c4274698aa26a18cbd6df72842f8ac38 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 15 Apr 2021 14:31:23 +0800 Subject: [PATCH 2/3] mktime support fixed timezone --- components/libc/compilers/common/time.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 75adb817da..638735b77c 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -234,7 +234,13 @@ RTM_EXPORT(localtime); /* TODO: timezone */ time_t mktime(struct tm * const t) { - return timegm(t); + time_t timestamp; + int utc_plus; + + utc_plus = 8; /* GMT: UTC+8 */ + timestamp = timegm(t); + timestamp = timestamp - 3600 * utc_plus; + return timestamp; } RTM_EXPORT(mktime); From 46d51a99f1b92738a2955245527ad8e18bcf96a5 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 15 Apr 2021 16:34:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96get=5Ftimeval=20set=5Ftim?= =?UTF-8?q?eval=20=E6=9D=A1=E4=BB=B6=E7=BC=96=E8=AF=91=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/time.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 638735b77c..33d8907165 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -77,7 +77,6 @@ static int get_timeval(struct timeval *tv) { #ifdef RT_USING_RTC static rt_device_t device = RT_NULL; -#endif rt_err_t rst = -RT_ERROR; if (tv == RT_NULL) @@ -87,7 +86,6 @@ static int get_timeval(struct timeval *tv) tv->tv_sec = 0; tv->tv_usec = 0; -#ifdef RT_USING_RTC /* optimization: find rtc device only first */ if (device == RT_NULL) { @@ -127,15 +125,13 @@ static int get_timeval(struct timeval *tv) */ static int set_timeval(struct timeval *tv) { - rt_err_t rst = -RT_ERROR; #ifdef RT_USING_RTC static rt_device_t device = RT_NULL; -#endif + rt_err_t rst = -RT_ERROR; if (tv == RT_NULL) return -1; -#ifdef RT_USING_RTC /* optimization: find rtc device only first */ if (device == RT_NULL) { @@ -311,7 +307,7 @@ RT_WEAK time_t time(time_t *t) else { errno = EFAULT; - return -1; + return ((time_t)-1); } } RTM_EXPORT(time);