1
0

ctime.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-08-21 zhangjun copy from minilibc
  9. * 2020-09-07 Meco Man combine gcc armcc iccarm
  10. * 2021-02-05 Meco Man add timegm()
  11. * 2021-02-07 Meco Man fixed gettimeofday()
  12. * 2021-02-08 Meco Man add settimeofday() stime()
  13. * 2021-02-10 Meco Man add ctime_r() and re-implement ctime()
  14. * 2021-02-11 Meco Man fix bug #3183 - align days[] and months[] to 4 bytes
  15. * 2021-02-12 Meco Man add errno
  16. * 2012-12-08 Bernard <clock_time.c> fix the issue of _timevalue.tv_usec initialization,
  17. * which found by Rob <rdent@iinet.net.au>
  18. * 2021-02-12 Meco Man move all of the functions located in <clock_time.c> to this file
  19. * 2021-03-15 Meco Man fixed a bug of leaking memory in asctime()
  20. * 2021-05-01 Meco Man support fixed timezone
  21. * 2021-07-21 Meco Man implement that change/set timezone APIs
  22. * 2023-07-03 xqyjlj refactor posix time and timer
  23. */
  24. #include "sys/time.h"
  25. #include <ktime.h>
  26. #include <rthw.h>
  27. #include <rtthread.h>
  28. #include <sys/errno.h>
  29. #include <unistd.h>
  30. #include "drivers/rtc.h"
  31. #ifdef RT_USING_SMART
  32. #include "lwp.h"
  33. #endif
  34. #ifdef RT_USING_POSIX_DELAY
  35. #include <delay.h>
  36. #endif
  37. #if defined( RT_USING_RTC ) || defined( RT_USING_CPUTIME)
  38. #include <rtdevice.h>
  39. #endif
  40. #define DBG_TAG "time"
  41. #define DBG_LVL DBG_INFO
  42. #include <rtdbg.h>
  43. #define _WARNING_NO_RTC "Cannot find a RTC device!"
  44. /* seconds per day */
  45. #define SPD 24*60*60
  46. /* days per month -- nonleap! */
  47. static const short __spm[13] =
  48. {
  49. 0,
  50. (31),
  51. (31 + 28),
  52. (31 + 28 + 31),
  53. (31 + 28 + 31 + 30),
  54. (31 + 28 + 31 + 30 + 31),
  55. (31 + 28 + 31 + 30 + 31 + 30),
  56. (31 + 28 + 31 + 30 + 31 + 30 + 31),
  57. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
  58. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
  59. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
  60. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
  61. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
  62. };
  63. rt_align(4) static const char *days = "Sun Mon Tue Wed Thu Fri Sat ";
  64. rt_align(4) static const char *months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  65. #ifndef __isleap
  66. static int __isleap(int year)
  67. {
  68. /* every fourth year is a leap year except for century years that are
  69. * not divisible by 400. */
  70. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  71. return (!(year % 4) && ((year % 100) || !(year % 400)));
  72. }
  73. #endif
  74. static void num2str(char *c, int i)
  75. {
  76. c[0] = i / 10 + '0';
  77. c[1] = i % 10 + '0';
  78. }
  79. static rt_err_t _control_rtc(int cmd, void *arg)
  80. {
  81. #ifdef RT_USING_RTC
  82. static rt_device_t device = RT_NULL;
  83. rt_err_t rst = -RT_ERROR;
  84. if (device == RT_NULL)
  85. {
  86. device = rt_device_find("rtc");
  87. }
  88. /* read timestamp from RTC device */
  89. if (device != RT_NULL)
  90. {
  91. if (rt_device_open(device, 0) == RT_EOK)
  92. {
  93. rst = rt_device_control(device, cmd, arg);
  94. rt_device_close(device);
  95. }
  96. }
  97. else
  98. {
  99. LOG_W(_WARNING_NO_RTC);
  100. return -RT_ENOSYS;
  101. }
  102. return rst;
  103. #else
  104. LOG_W(_WARNING_NO_RTC);
  105. return -RT_ENOSYS;
  106. #endif /* RT_USING_RTC */
  107. }
  108. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  109. {
  110. int i;
  111. int work;
  112. if(timep == RT_NULL || r == RT_NULL)
  113. {
  114. rt_set_errno(EFAULT);
  115. return RT_NULL;
  116. }
  117. rt_memset(r, RT_NULL, sizeof(struct tm));
  118. work = *timep % (SPD);
  119. r->tm_sec = work % 60;
  120. work /= 60;
  121. r->tm_min = work % 60;
  122. r->tm_hour = work / 60;
  123. work = (int)(*timep / (SPD));
  124. r->tm_wday = (4 + work) % 7;
  125. for (i = 1970;; ++i)
  126. {
  127. int k = __isleap(i) ? 366 : 365;
  128. if (work >= k)
  129. work -= k;
  130. else
  131. break;
  132. }
  133. r->tm_year = i - 1900;
  134. r->tm_yday = work;
  135. r->tm_mday = 1;
  136. if (__isleap(i) && (work > 58))
  137. {
  138. if (work == 59)
  139. r->tm_mday = 2; /* 29.2. */
  140. work -= 1;
  141. }
  142. for (i = 11; i && (__spm[i] > work); --i);
  143. r->tm_mon = i;
  144. r->tm_mday += work - __spm[i];
  145. r->tm_isdst = tz_is_dst();
  146. return r;
  147. }
  148. RTM_EXPORT(gmtime_r);
  149. struct tm* gmtime(const time_t* t)
  150. {
  151. static struct tm tmp;
  152. return gmtime_r(t, &tmp);
  153. }
  154. RTM_EXPORT(gmtime);
  155. struct tm* localtime_r(const time_t* t, struct tm* r)
  156. {
  157. time_t local_tz;
  158. local_tz = *t + (time_t)tz_get() * 3600;
  159. return gmtime_r(&local_tz, r);
  160. }
  161. RTM_EXPORT(localtime_r);
  162. struct tm* localtime(const time_t* t)
  163. {
  164. static struct tm tmp;
  165. return localtime_r(t, &tmp);
  166. }
  167. RTM_EXPORT(localtime);
  168. time_t mktime(struct tm * const t)
  169. {
  170. time_t timestamp;
  171. timestamp = timegm(t);
  172. timestamp = timestamp - 3600 * (time_t)tz_get();
  173. return timestamp;
  174. }
  175. RTM_EXPORT(mktime);
  176. char* asctime_r(const struct tm *t, char *buf)
  177. {
  178. if(t == RT_NULL || buf == RT_NULL)
  179. {
  180. rt_set_errno(EFAULT);
  181. return RT_NULL;
  182. }
  183. rt_memset(buf, RT_NULL, 26);
  184. /* Checking input validity */
  185. if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2))
  186. {
  187. LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
  188. *(int*) buf = *(int*) days;
  189. *(int*) (buf + 4) = *(int*) months;
  190. num2str(buf + 8, t->tm_mday);
  191. if (buf[8] == '0')
  192. buf[8] = ' ';
  193. buf[10] = ' ';
  194. num2str(buf + 11, t->tm_hour);
  195. buf[13] = ':';
  196. num2str(buf + 14, t->tm_min);
  197. buf[16] = ':';
  198. num2str(buf + 17, t->tm_sec);
  199. buf[19] = ' ';
  200. num2str(buf + 20, 2000 / 100);
  201. num2str(buf + 22, 2000 % 100);
  202. buf[24] = '\n';
  203. buf[25] = '\0';
  204. return buf;
  205. }
  206. /* "Wed Jun 30 21:49:08 1993\n" */
  207. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  208. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  209. num2str(buf + 8, t->tm_mday);
  210. if (buf[8] == '0')
  211. buf[8] = ' ';
  212. buf[10] = ' ';
  213. num2str(buf + 11, t->tm_hour);
  214. buf[13] = ':';
  215. num2str(buf + 14, t->tm_min);
  216. buf[16] = ':';
  217. num2str(buf + 17, t->tm_sec);
  218. buf[19] = ' ';
  219. num2str(buf + 20, (t->tm_year + 1900) / 100);
  220. num2str(buf + 22, (t->tm_year + 1900) % 100);
  221. buf[24] = '\n';
  222. buf[25] = '\0';
  223. return buf;
  224. }
  225. RTM_EXPORT(asctime_r);
  226. char* asctime(const struct tm *timeptr)
  227. {
  228. static char buf[26];
  229. return asctime_r(timeptr, buf);
  230. }
  231. RTM_EXPORT(asctime);
  232. char *ctime_r(const time_t * tim_p, char * result)
  233. {
  234. struct tm tm;
  235. return asctime_r(localtime_r(tim_p, &tm), result);
  236. }
  237. RTM_EXPORT(ctime_r);
  238. char* ctime(const time_t *tim_p)
  239. {
  240. return asctime(localtime(tim_p));
  241. }
  242. RTM_EXPORT(ctime);
  243. #ifndef __ICCARM__
  244. double difftime(time_t time1, time_t time2)
  245. {
  246. return (double)(time1 - time2);
  247. }
  248. #endif /* __ICCARM__ */
  249. RTM_EXPORT(difftime);
  250. RTM_EXPORT(strftime); /* inherent in the toolchain */
  251. /**
  252. * Returns the current time.
  253. *
  254. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  255. *
  256. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  257. * If timer is not a NULL pointer, the return value is also stored in timer.
  258. *
  259. */
  260. rt_weak time_t time(time_t *t)
  261. {
  262. time_t _t;
  263. if (_control_rtc(RT_DEVICE_CTRL_RTC_GET_TIME, &_t) != RT_EOK)
  264. {
  265. rt_set_errno(EFAULT);
  266. return -1;
  267. }
  268. if (t)
  269. *t = _t;
  270. return _t;
  271. }
  272. RTM_EXPORT(time);
  273. rt_weak clock_t clock(void)
  274. {
  275. return rt_tick_get(); // TODO should return cpu usage time
  276. }
  277. RTM_EXPORT(clock);
  278. int stime(const time_t *t)
  279. {
  280. if ((t != RT_NULL) && (_control_rtc(RT_DEVICE_CTRL_RTC_SET_TIME, (void *)t) == RT_EOK))
  281. {
  282. return 0;
  283. }
  284. rt_set_errno(EFAULT);
  285. return -1;
  286. }
  287. RTM_EXPORT(stime);
  288. time_t timegm(struct tm * const t)
  289. {
  290. time_t day;
  291. time_t i;
  292. time_t years;
  293. if(t == RT_NULL)
  294. {
  295. rt_set_errno(EFAULT);
  296. return (time_t)-1;
  297. }
  298. years = (time_t)t->tm_year - 70;
  299. if (t->tm_sec > 60) /* seconds after the minute - [0, 60] including leap second */
  300. {
  301. t->tm_min += t->tm_sec / 60;
  302. t->tm_sec %= 60;
  303. }
  304. if (t->tm_min >= 60) /* minutes after the hour - [0, 59] */
  305. {
  306. t->tm_hour += t->tm_min / 60;
  307. t->tm_min %= 60;
  308. }
  309. if (t->tm_hour >= 24) /* hours since midnight - [0, 23] */
  310. {
  311. t->tm_mday += t->tm_hour / 24;
  312. t->tm_hour %= 24;
  313. }
  314. if (t->tm_mon >= 12) /* months since January - [0, 11] */
  315. {
  316. t->tm_year += t->tm_mon / 12;
  317. t->tm_mon %= 12;
  318. }
  319. while (t->tm_mday > __spm[1 + t->tm_mon])
  320. {
  321. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  322. {
  323. --t->tm_mday;
  324. }
  325. t->tm_mday -= __spm[t->tm_mon];
  326. ++t->tm_mon;
  327. if (t->tm_mon > 11)
  328. {
  329. t->tm_mon = 0;
  330. ++t->tm_year;
  331. }
  332. }
  333. if (t->tm_year < 70)
  334. {
  335. rt_set_errno(EINVAL);
  336. return (time_t) -1;
  337. }
  338. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  339. day = years * 365 + (years + 1) / 4;
  340. /* After 2100 we have to substract 3 leap years for every 400 years
  341. This is not intuitive. Most mktime implementations do not support
  342. dates after 2059, anyway, so we might leave this out for it's
  343. bloat. */
  344. if (years >= 131)
  345. {
  346. years -= 131;
  347. years /= 100;
  348. day -= (years >> 2) * 3 + 1;
  349. if ((years &= 3) == 3)
  350. years--;
  351. day -= years;
  352. }
  353. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  354. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  355. /* day is now the number of days since 'Jan 1 1970' */
  356. i = 7;
  357. t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */
  358. i = 24;
  359. day *= i;
  360. i = 60;
  361. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  362. }
  363. RTM_EXPORT(timegm);
  364. int gettimeofday(struct timeval *tv, struct timezone *tz)
  365. {
  366. /* The use of the timezone structure is obsolete;
  367. * the tz argument should normally be specified as NULL.
  368. * The tz_dsttime field has never been used under Linux.
  369. * Thus, the following is purely of historic interest.
  370. */
  371. if(tz != RT_NULL)
  372. {
  373. tz->tz_dsttime = DST_NONE;
  374. tz->tz_minuteswest = -(tz_get() * 60);
  375. }
  376. if (tv != RT_NULL)
  377. {
  378. tv->tv_sec = 0;
  379. tv->tv_usec = 0;
  380. if (_control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv) == RT_EOK)
  381. return 0;
  382. }
  383. rt_set_errno(EINVAL);
  384. return -1;
  385. }
  386. RTM_EXPORT(gettimeofday);
  387. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  388. {
  389. /* The use of the timezone structure is obsolete;
  390. * the tz argument should normally be specified as NULL.
  391. * The tz_dsttime field has never been used under Linux.
  392. * Thus, the following is purely of historic interest.
  393. */
  394. if (tv != RT_NULL && tv->tv_usec >= 0 && tv->tv_sec >= 0)
  395. {
  396. if (_control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMEVAL, (void *)tv) == RT_EOK)
  397. return 0;
  398. }
  399. rt_set_errno(EINVAL);
  400. return -1;
  401. }
  402. RTM_EXPORT(settimeofday);
  403. #ifdef RT_USING_POSIX_DELAY
  404. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  405. {
  406. struct timespec old_ts = {0};
  407. struct timespec new_ts = {0};
  408. if (rqtp == RT_NULL)
  409. {
  410. rt_set_errno(EFAULT);
  411. return -1;
  412. }
  413. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  414. {
  415. rt_set_errno(EINVAL);
  416. return -1;
  417. }
  418. unsigned long ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  419. rt_ktime_boottime_get_ns(&old_ts);
  420. rt_ktime_hrtimer_ndelay(ns);
  421. if (rt_get_errno() == -RT_EINTR)
  422. {
  423. if (rmtp)
  424. {
  425. rt_ktime_boottime_get_ns(&new_ts);
  426. rmtp->tv_sec = 0;
  427. rmtp->tv_nsec =
  428. (old_ts.tv_nsec + ns) - ((new_ts.tv_sec - old_ts.tv_sec) * NANOSECOND_PER_SECOND + new_ts.tv_nsec);
  429. if (rmtp->tv_nsec > NANOSECOND_PER_SECOND)
  430. {
  431. rmtp->tv_nsec %= NANOSECOND_PER_SECOND;
  432. rmtp->tv_sec += rmtp->tv_nsec / NANOSECOND_PER_SECOND;
  433. }
  434. }
  435. rt_set_errno(EINTR);
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. RTM_EXPORT(nanosleep);
  441. #endif /* RT_USING_POSIX_DELAY */
  442. #ifdef RT_USING_POSIX_CLOCK
  443. int clock_getres(clockid_t clockid, struct timespec *res)
  444. {
  445. if (res == RT_NULL)
  446. {
  447. rt_set_errno(EFAULT);
  448. return -1;
  449. }
  450. switch (clockid)
  451. {
  452. case CLOCK_REALTIME: // use RTC
  453. case CLOCK_REALTIME_COARSE:
  454. return _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMERES, res);
  455. case CLOCK_MONOTONIC: // use cputimer
  456. case CLOCK_MONOTONIC_COARSE:
  457. case CLOCK_MONOTONIC_RAW:
  458. case CLOCK_BOOTTIME:
  459. case CLOCK_PROCESS_CPUTIME_ID:
  460. case CLOCK_THREAD_CPUTIME_ID:
  461. res->tv_sec = 0;
  462. res->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL);
  463. return 0;
  464. default:
  465. rt_set_errno(EINVAL);
  466. return -1;
  467. }
  468. }
  469. RTM_EXPORT(clock_getres);
  470. int clock_gettime(clockid_t clockid, struct timespec *tp)
  471. {
  472. if (tp == RT_NULL)
  473. {
  474. rt_set_errno(EFAULT);
  475. return -1;
  476. }
  477. switch (clockid)
  478. {
  479. case CLOCK_REALTIME: // use RTC
  480. case CLOCK_REALTIME_COARSE:
  481. return _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, tp);
  482. case CLOCK_MONOTONIC: // use boottime
  483. case CLOCK_MONOTONIC_COARSE:
  484. case CLOCK_MONOTONIC_RAW:
  485. case CLOCK_BOOTTIME:
  486. return rt_ktime_boottime_get_ns(tp);
  487. case CLOCK_PROCESS_CPUTIME_ID:
  488. case CLOCK_THREAD_CPUTIME_ID:
  489. return rt_ktime_boottime_get_ns(tp); // TODO not yet implemented
  490. default:
  491. tp->tv_sec = 0;
  492. tp->tv_nsec = 0;
  493. rt_set_errno(EINVAL);
  494. return -1;
  495. }
  496. }
  497. RTM_EXPORT(clock_gettime);
  498. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
  499. {
  500. struct timespec ts = {0};
  501. rt_err_t err;
  502. if (rqtp == RT_NULL)
  503. {
  504. rt_set_errno(EFAULT);
  505. return -1;
  506. }
  507. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  508. {
  509. rt_set_errno(EINVAL);
  510. return -1;
  511. }
  512. switch (clockid)
  513. {
  514. case CLOCK_REALTIME: // use RTC
  515. if (flags & TIMER_ABSTIME)
  516. err = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, &ts);
  517. break;
  518. case CLOCK_MONOTONIC: // use boottime
  519. case CLOCK_PROCESS_CPUTIME_ID:
  520. if (flags & TIMER_ABSTIME)
  521. err = rt_ktime_boottime_get_ns(&ts);
  522. break;
  523. default:
  524. rt_set_errno(EINVAL);
  525. return -1;
  526. }
  527. if (err != RT_EOK)
  528. return err;
  529. int64_t ns = rqtp->tv_nsec - ts.tv_nsec + (rqtp->tv_sec - ts.tv_sec) * NANOSECOND_PER_SECOND;
  530. if (ns <= 0)
  531. return 0;
  532. if (flags & TIMER_ABSTIME)
  533. {
  534. ts.tv_nsec = ns % NANOSECOND_PER_SECOND;
  535. ts.tv_sec = ns / NANOSECOND_PER_SECOND;
  536. return nanosleep(&ts, rmtp);
  537. }
  538. else
  539. {
  540. return nanosleep(rqtp, rmtp);
  541. }
  542. }
  543. RTM_EXPORT(clock_nanosleep);
  544. int clock_settime(clockid_t clockid, const struct timespec *tp)
  545. {
  546. if (tp == RT_NULL)
  547. {
  548. rt_set_errno(EFAULT);
  549. return -1;
  550. }
  551. if (tp->tv_sec < 0 || tp->tv_nsec < 0 || tp->tv_nsec >= NANOSECOND_PER_SECOND)
  552. {
  553. rt_set_errno(EINVAL);
  554. return -1;
  555. }
  556. switch (clockid)
  557. {
  558. case CLOCK_REALTIME:
  559. return _control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMESPEC, (void *)tp);
  560. case CLOCK_REALTIME_COARSE:
  561. case CLOCK_MONOTONIC:
  562. case CLOCK_MONOTONIC_COARSE:
  563. case CLOCK_MONOTONIC_RAW:
  564. case CLOCK_BOOTTIME:
  565. case CLOCK_PROCESS_CPUTIME_ID:
  566. case CLOCK_THREAD_CPUTIME_ID:
  567. rt_set_errno(EPERM);
  568. return -1;
  569. default:
  570. rt_set_errno(EINVAL);
  571. return -1;
  572. }
  573. }
  574. RTM_EXPORT(clock_settime);
  575. int rt_timespec_to_tick(const struct timespec *time)
  576. {
  577. int tick;
  578. int nsecond, second;
  579. struct timespec tp = {0};
  580. RT_ASSERT(time != RT_NULL);
  581. tick = RT_WAITING_FOREVER;
  582. if (time != NULL)
  583. {
  584. /* get current tp */
  585. clock_gettime(CLOCK_REALTIME, &tp);
  586. if ((time->tv_nsec - tp.tv_nsec) < 0)
  587. {
  588. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  589. second = time->tv_sec - tp.tv_sec - 1;
  590. }
  591. else
  592. {
  593. nsecond = time->tv_nsec - tp.tv_nsec;
  594. second = time->tv_sec - tp.tv_sec;
  595. }
  596. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  597. if (tick < 0) tick = 0;
  598. }
  599. return tick;
  600. }
  601. RTM_EXPORT(rt_timespec_to_tick);
  602. #endif /* RT_USING_POSIX_CLOCK */
  603. #ifdef RT_USING_POSIX_TIMER
  604. #include <resource_id.h>
  605. #define ACTIVE 1
  606. #define NOT_ACTIVE 0
  607. struct timer_obj
  608. {
  609. struct rt_ktime_hrtimer hrtimer;
  610. void (*sigev_notify_function)(union sigval val);
  611. union sigval val;
  612. struct timespec interval; /* Reload value */
  613. struct timespec value; /* Reload value */
  614. unsigned long reload; /* Reload value in ms */
  615. rt_uint32_t status;
  616. int sigev_signo;
  617. clockid_t clockid;
  618. #ifdef RT_USING_SMART
  619. pid_t pid;
  620. #endif
  621. };
  622. static void rtthread_timer_wrapper(void *timerobj)
  623. {
  624. struct timer_obj *timer;
  625. timer = (struct timer_obj *)timerobj;
  626. if (timer->reload == 0U)
  627. {
  628. timer->status = NOT_ACTIVE;
  629. }
  630. timer->reload = ((timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) * RT_KTIME_RESMUL) /
  631. rt_ktime_cputimer_getres();
  632. if (timer->reload)
  633. {
  634. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  635. rt_ktime_hrtimer_start(&timer->hrtimer);
  636. }
  637. #ifdef RT_USING_SMART
  638. sys_kill(timer->pid, timer->sigev_signo);
  639. #else
  640. if(timer->sigev_notify_function != RT_NULL)
  641. {
  642. (timer->sigev_notify_function)(timer->val);
  643. }
  644. #endif
  645. }
  646. #define TIMER_ID_MAX 50
  647. static struct timer_obj *_g_timerid[TIMER_ID_MAX];
  648. static void *timer_id[TIMER_ID_MAX];
  649. static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id);
  650. /**
  651. * @brief Create a per-process timer.
  652. *
  653. * This API does not accept SIGEV_THREAD as valid signal event notification
  654. * type.
  655. *
  656. * See IEEE 1003.1
  657. */
  658. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
  659. {
  660. static int num = 0;
  661. int _timerid = 0;
  662. struct timer_obj *timer;
  663. char timername[RT_NAME_MAX] = {0};
  664. if (evp == RT_NULL || timerid == RT_NULL)
  665. {
  666. rt_set_errno(EINVAL);
  667. return -1;
  668. }
  669. if (evp->sigev_notify == SIGEV_THREAD) // TODO need to implement
  670. {
  671. rt_set_errno(EINVAL);
  672. return -1;
  673. }
  674. switch (clockid)
  675. {
  676. case CLOCK_REALTIME:
  677. case CLOCK_REALTIME_ALARM:
  678. case CLOCK_MONOTONIC:
  679. case CLOCK_BOOTTIME:
  680. case CLOCK_BOOTTIME_ALARM:
  681. case CLOCK_PROCESS_CPUTIME_ID:
  682. case CLOCK_THREAD_CPUTIME_ID:
  683. break; // Only these ids are supported
  684. default:
  685. rt_set_errno(EINVAL);
  686. return -1;
  687. }
  688. timer = rt_malloc(sizeof(struct timer_obj));
  689. if(timer == RT_NULL)
  690. {
  691. rt_set_errno(ENOMEM);
  692. return -1;
  693. }
  694. rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
  695. num %= 100;
  696. timer->sigev_signo = evp->sigev_signo;
  697. #ifdef RT_USING_SMART
  698. timer->pid = lwp_self()->pid;
  699. #endif
  700. timer->sigev_notify_function = evp->sigev_notify_function;
  701. timer->val = evp->sigev_value;
  702. timer->interval.tv_sec = 0;
  703. timer->interval.tv_nsec = 0;
  704. timer->reload = 0U;
  705. timer->status = NOT_ACTIVE;
  706. timer->clockid = clockid;
  707. rt_ktime_hrtimer_init(&timer->hrtimer, timername, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
  708. rtthread_timer_wrapper, timer);
  709. _timerid = resource_id_get(&id_timer);
  710. if (_timerid < 0)
  711. {
  712. LOG_E("_timerid overflow!");
  713. return -1; /* todo:memory leak */
  714. }
  715. _g_timerid[_timerid] = timer;
  716. *timerid = (timer_t)(rt_ubase_t)_timerid;
  717. return 0;
  718. }
  719. RTM_EXPORT(timer_create);
  720. /**
  721. * @brief Delete a per-process timer.
  722. *
  723. * See IEEE 1003.1
  724. */
  725. int timer_delete(timer_t timerid)
  726. {
  727. struct timer_obj *timer;
  728. rt_ubase_t ktimerid;
  729. ktimerid = (rt_ubase_t)timerid;
  730. if (ktimerid < 0 || ktimerid >= TIMER_ID_MAX)
  731. {
  732. rt_set_errno(EINVAL);
  733. return -1;
  734. }
  735. if (_g_timerid[ktimerid] == NULL)
  736. {
  737. rt_set_errno(EINVAL);
  738. LOG_E("can not find timer!");
  739. return -1;
  740. }
  741. timer = _g_timerid[ktimerid];
  742. resource_id_put(&id_timer, ktimerid);
  743. if (timer == RT_NULL)
  744. {
  745. rt_set_errno(EINVAL);
  746. return -1;
  747. }
  748. if (timer->status == ACTIVE)
  749. {
  750. timer->status = NOT_ACTIVE;
  751. rt_ktime_hrtimer_stop(&timer->hrtimer);
  752. }
  753. rt_ktime_hrtimer_detach(&timer->hrtimer);
  754. rt_free(timer);
  755. return 0;
  756. }
  757. RTM_EXPORT(timer_delete);
  758. /**
  759. *
  760. * Return the overrun count for the last timer expiration.
  761. * It is subefficient to create a new structure to get overrun count.
  762. **/
  763. int timer_getoverrun(timer_t timerid)
  764. {
  765. rt_set_errno(ENOSYS);
  766. return -1;
  767. }
  768. /**
  769. * @brief Get amount of time left for expiration on a per-process timer.
  770. *
  771. * See IEEE 1003.1
  772. */
  773. int timer_gettime(timer_t timerid, struct itimerspec *its)
  774. {
  775. struct timer_obj *timer;
  776. rt_uint32_t seconds, nanoseconds;
  777. timer = _g_timerid[(rt_ubase_t)timerid];
  778. if (timer == NULL)
  779. {
  780. rt_set_errno(EINVAL);
  781. return -1;
  782. }
  783. if (its == NULL)
  784. {
  785. rt_set_errno(EFAULT);
  786. return -1;
  787. }
  788. if (timer->status == ACTIVE)
  789. {
  790. unsigned long remain_cnt;
  791. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_cnt);
  792. nanoseconds = ((remain_cnt - rt_ktime_cputimer_getcnt()) * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
  793. seconds = nanoseconds / NANOSECOND_PER_SECOND;
  794. nanoseconds = nanoseconds % NANOSECOND_PER_SECOND;
  795. its->it_value.tv_sec = (rt_int32_t)seconds;
  796. its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
  797. }
  798. else
  799. {
  800. /* Timer is disarmed */
  801. its->it_value.tv_sec = 0;
  802. its->it_value.tv_nsec = 0;
  803. }
  804. /* The interval last set by timer_settime() */
  805. its->it_interval = timer->interval;
  806. return 0;
  807. }
  808. RTM_EXPORT(timer_gettime);
  809. /**
  810. * @brief Sets expiration time of per-process timer.
  811. *
  812. * See IEEE 1003.1
  813. */
  814. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  815. struct itimerspec *ovalue)
  816. {
  817. struct timespec ts = {0};
  818. rt_err_t err = RT_EOK;
  819. struct timer_obj *timer;
  820. timer = _g_timerid[(rt_ubase_t)timerid];
  821. if (timer == NULL ||
  822. value->it_interval.tv_nsec < 0 ||
  823. value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
  824. value->it_interval.tv_sec < 0 ||
  825. value->it_value.tv_nsec < 0 ||
  826. value->it_value.tv_nsec >= NANOSECOND_PER_SECOND ||
  827. value->it_value.tv_sec < 0)
  828. {
  829. rt_set_errno(EINVAL);
  830. return -1;
  831. }
  832. /* Save time to expire and old reload value. */
  833. if (ovalue != NULL)
  834. {
  835. timer_gettime(timerid, ovalue);
  836. }
  837. /* Stop the timer if the value is 0 */
  838. if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
  839. {
  840. if (timer->status == ACTIVE)
  841. {
  842. rt_ktime_hrtimer_stop(&timer->hrtimer);
  843. }
  844. timer->status = NOT_ACTIVE;
  845. return 0;
  846. }
  847. switch (timer->clockid)
  848. {
  849. case CLOCK_REALTIME:
  850. case CLOCK_REALTIME_ALARM:
  851. if (flags & TIMER_ABSTIME)
  852. err = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, &ts);
  853. break;
  854. case CLOCK_MONOTONIC:
  855. case CLOCK_BOOTTIME:
  856. case CLOCK_BOOTTIME_ALARM:
  857. case CLOCK_PROCESS_CPUTIME_ID:
  858. case CLOCK_THREAD_CPUTIME_ID:
  859. if (flags & TIMER_ABSTIME)
  860. err = rt_ktime_boottime_get_ns(&ts);
  861. break;
  862. default:
  863. rt_set_errno(EINVAL);
  864. return -1;
  865. }
  866. if (err != RT_EOK)
  867. return err;
  868. int64_t ns = value->it_value.tv_nsec - ts.tv_nsec + (value->it_value.tv_sec - ts.tv_sec) * NANOSECOND_PER_SECOND;
  869. if (ns <= 0)
  870. return 0;
  871. unsigned long res = rt_ktime_cputimer_getres();
  872. timer->reload = (ns * RT_KTIME_RESMUL) / res;
  873. timer->interval.tv_sec = value->it_interval.tv_sec;
  874. timer->interval.tv_nsec = value->it_interval.tv_nsec;
  875. timer->value.tv_sec = value->it_value.tv_sec;
  876. timer->value.tv_nsec = value->it_value.tv_nsec;
  877. if (timer->status == ACTIVE)
  878. {
  879. rt_ktime_hrtimer_stop(&timer->hrtimer);
  880. }
  881. timer->status = ACTIVE;
  882. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  883. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  884. else
  885. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  886. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  887. rt_ktime_hrtimer_start(&timer->hrtimer);
  888. return 0;
  889. }
  890. RTM_EXPORT(timer_settime);
  891. #endif /* RT_USING_POSIX_TIMER */
  892. /* timezone */
  893. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  894. #define RT_LIBC_DEFAULT_TIMEZONE 8
  895. #endif
  896. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  897. void tz_set(int8_t tz)
  898. {
  899. rt_base_t level;
  900. level = rt_hw_interrupt_disable();
  901. _current_timezone = tz;
  902. rt_hw_interrupt_enable(level);
  903. }
  904. int8_t tz_get(void)
  905. {
  906. return _current_timezone;
  907. }
  908. int8_t tz_is_dst(void)
  909. {
  910. return 0;
  911. }