ctime.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. * 2023-07-16 Shell update signal generation routine for lwp
  24. * adapt to new api and do the signal handling in thread context
  25. * 2023-08-12 Meco Man re-implement RT-Thread lightweight timezone API
  26. */
  27. #include "sys/time.h"
  28. #include <rthw.h>
  29. #include <rtdevice.h>
  30. #include <drivers/rtc.h>
  31. #include <sys/errno.h>
  32. #include <unistd.h>
  33. #ifdef RT_USING_SMART
  34. #include <lwp.h>
  35. #endif
  36. #ifdef RT_USING_POSIX_DELAY
  37. #include <delay.h>
  38. #endif
  39. #ifdef RT_USING_KTIME
  40. #include <ktime.h>
  41. #endif
  42. #define DBG_TAG "time"
  43. #define DBG_LVL DBG_INFO
  44. #include <rtdbg.h>
  45. #define _WARNING_NO_RTC "Cannot find a RTC device!"
  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(RT_ALIGN_SIZE) static const char *days = "Sun Mon Tue Wed Thu Fri Sat ";
  64. rt_align(RT_ALIGN_SIZE) 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. /* lightweight timezone and daylight saving time */
  109. #ifdef RT_LIBC_USING_LIGHT_TZ_DST
  110. #ifndef RT_LIBC_TZ_DEFAULT_HOUR
  111. #define RT_LIBC_TZ_DEFAULT_HOUR (8U)
  112. #endif /* RT_LIBC_TZ_DEFAULT_HOUR */
  113. #ifndef RT_LIBC_TZ_DEFAULT_MIN
  114. #define RT_LIBC_TZ_DEFAULT_MIN (0U)
  115. #endif /* RT_LIBC_TZ_DEFAULT_MIN */
  116. #ifndef RT_LIBC_TZ_DEFAULT_SEC
  117. #define RT_LIBC_TZ_DEFAULT_SEC (0U)
  118. #endif /* RT_LIBC_TZ_DEFAULT_SEC */
  119. static volatile int32_t _current_tz_offset_sec = \
  120. RT_LIBC_TZ_DEFAULT_HOUR * 3600U + RT_LIBC_TZ_DEFAULT_MIN * 60U + RT_LIBC_TZ_DEFAULT_SEC;
  121. /* return current timezone offset in seconds */
  122. void rt_tz_set(int32_t offset_sec)
  123. {
  124. rt_base_t level;
  125. level = rt_hw_interrupt_disable();
  126. _current_tz_offset_sec = offset_sec;
  127. rt_hw_interrupt_enable(level);
  128. }
  129. int32_t rt_tz_get(void)
  130. {
  131. int32_t offset_sec;
  132. rt_base_t level;
  133. level = rt_hw_interrupt_disable();
  134. offset_sec = _current_tz_offset_sec;
  135. rt_hw_interrupt_enable(level);
  136. return offset_sec;
  137. }
  138. int8_t rt_tz_is_dst(void)
  139. {
  140. return 0U; /* TODO */
  141. }
  142. #endif /* RT_LIBC_USING_LIGHT_TZ_DST */
  143. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  144. {
  145. int i;
  146. int work;
  147. if(timep == RT_NULL || r == RT_NULL)
  148. {
  149. rt_set_errno(EFAULT);
  150. return RT_NULL;
  151. }
  152. rt_memset(r, RT_NULL, sizeof(struct tm));
  153. work = *timep % (24*60*60);
  154. r->tm_sec = work % 60;
  155. work /= 60;
  156. r->tm_min = work % 60;
  157. r->tm_hour = work / 60;
  158. work = (int)(*timep / (24*60*60));
  159. r->tm_wday = (4 + work) % 7;
  160. for (i = 1970;; ++i)
  161. {
  162. int k = __isleap(i) ? 366 : 365;
  163. if (work >= k)
  164. work -= k;
  165. else
  166. break;
  167. }
  168. r->tm_year = i - 1900;
  169. r->tm_yday = work;
  170. r->tm_mday = 1;
  171. if (__isleap(i) && (work > 58))
  172. {
  173. if (work == 59)
  174. r->tm_mday = 2; /* 29.2. */
  175. work -= 1;
  176. }
  177. for (i = 11; i && (__spm[i] > work); --i);
  178. r->tm_mon = i;
  179. r->tm_mday += work - __spm[i];
  180. #if defined(RT_LIBC_USING_LIGHT_TZ_DST)
  181. r->tm_isdst = rt_tz_is_dst();
  182. #else
  183. r->tm_isdst = 0U;
  184. #endif /* RT_LIBC_USING_LIGHT_TZ_DST */
  185. return r;
  186. }
  187. RTM_EXPORT(gmtime_r);
  188. struct tm* gmtime(const time_t* t)
  189. {
  190. static struct tm tmp;
  191. return gmtime_r(t, &tmp);
  192. }
  193. RTM_EXPORT(gmtime);
  194. struct tm* localtime_r(const time_t* t, struct tm* r)
  195. {
  196. time_t local_tz;
  197. #if defined(RT_LIBC_USING_LIGHT_TZ_DST)
  198. local_tz = *t + rt_tz_get();
  199. #else
  200. local_tz = *t + 0U;
  201. #endif /* RT_LIBC_USING_LIGHT_TZ_DST */
  202. return gmtime_r(&local_tz, r);
  203. }
  204. RTM_EXPORT(localtime_r);
  205. struct tm* localtime(const time_t* t)
  206. {
  207. static struct tm tmp;
  208. return localtime_r(t, &tmp);
  209. }
  210. RTM_EXPORT(localtime);
  211. time_t mktime(struct tm * const t)
  212. {
  213. time_t timestamp;
  214. timestamp = timegm(t);
  215. #if defined(RT_LIBC_USING_LIGHT_TZ_DST)
  216. timestamp = timestamp - rt_tz_get();
  217. #else
  218. timestamp = timestamp - 0U;
  219. #endif /* RT_LIBC_USING_LIGHT_TZ_DST */
  220. return timestamp;
  221. }
  222. RTM_EXPORT(mktime);
  223. char* asctime_r(const struct tm *t, char *buf)
  224. {
  225. if(t == RT_NULL || buf == RT_NULL)
  226. {
  227. rt_set_errno(EFAULT);
  228. return RT_NULL;
  229. }
  230. rt_memset(buf, RT_NULL, 26);
  231. /* Checking input validity */
  232. if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2))
  233. {
  234. LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
  235. *(int*) buf = *(int*) days;
  236. *(int*) (buf + 4) = *(int*) months;
  237. num2str(buf + 8, t->tm_mday);
  238. if (buf[8] == '0')
  239. buf[8] = ' ';
  240. buf[10] = ' ';
  241. num2str(buf + 11, t->tm_hour);
  242. buf[13] = ':';
  243. num2str(buf + 14, t->tm_min);
  244. buf[16] = ':';
  245. num2str(buf + 17, t->tm_sec);
  246. buf[19] = ' ';
  247. num2str(buf + 20, 2000 / 100);
  248. num2str(buf + 22, 2000 % 100);
  249. buf[24] = '\n';
  250. buf[25] = '\0';
  251. return buf;
  252. }
  253. /* "Wed Jun 30 21:49:08 1993\n" */
  254. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  255. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  256. num2str(buf + 8, t->tm_mday);
  257. if (buf[8] == '0')
  258. buf[8] = ' ';
  259. buf[10] = ' ';
  260. num2str(buf + 11, t->tm_hour);
  261. buf[13] = ':';
  262. num2str(buf + 14, t->tm_min);
  263. buf[16] = ':';
  264. num2str(buf + 17, t->tm_sec);
  265. buf[19] = ' ';
  266. num2str(buf + 20, (t->tm_year + 1900) / 100);
  267. num2str(buf + 22, (t->tm_year + 1900) % 100);
  268. buf[24] = '\n';
  269. buf[25] = '\0';
  270. return buf;
  271. }
  272. RTM_EXPORT(asctime_r);
  273. char *asctime(const struct tm *timeptr)
  274. {
  275. static char buf[26];
  276. return asctime_r(timeptr, buf);
  277. }
  278. RTM_EXPORT(asctime);
  279. char *ctime_r(const time_t * tim_p, char * result)
  280. {
  281. struct tm tm;
  282. return asctime_r(localtime_r(tim_p, &tm), result);
  283. }
  284. RTM_EXPORT(ctime_r);
  285. char *ctime(const time_t *tim_p)
  286. {
  287. return asctime(localtime(tim_p));
  288. }
  289. RTM_EXPORT(ctime);
  290. #ifndef __ICCARM__
  291. double difftime(time_t time1, time_t time2)
  292. {
  293. return (double)(time1 - time2);
  294. }
  295. #endif /* __ICCARM__ */
  296. RTM_EXPORT(difftime);
  297. RTM_EXPORT(strftime); /* inherent in the toolchain */
  298. /**
  299. * Returns the current time.
  300. *
  301. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  302. *
  303. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  304. * If timer is not a NULL pointer, the return value is also stored in timer.
  305. *
  306. */
  307. rt_weak time_t time(time_t *t)
  308. {
  309. time_t _t;
  310. if (_control_rtc(RT_DEVICE_CTRL_RTC_GET_TIME, &_t) != RT_EOK)
  311. {
  312. rt_set_errno(EFAULT);
  313. return (time_t)-1;
  314. }
  315. if (t)
  316. *t = _t;
  317. return _t;
  318. }
  319. RTM_EXPORT(time);
  320. rt_weak clock_t clock(void)
  321. {
  322. return rt_tick_get(); // TODO should return cpu usage time
  323. }
  324. RTM_EXPORT(clock);
  325. int stime(const time_t *t)
  326. {
  327. if ((t != RT_NULL) && (_control_rtc(RT_DEVICE_CTRL_RTC_SET_TIME, (void *)t) == RT_EOK))
  328. {
  329. return 0;
  330. }
  331. rt_set_errno(EFAULT);
  332. return -1;
  333. }
  334. RTM_EXPORT(stime);
  335. time_t timegm(struct tm * const t)
  336. {
  337. time_t day;
  338. time_t i;
  339. time_t years;
  340. if(t == RT_NULL)
  341. {
  342. rt_set_errno(EFAULT);
  343. return (time_t)-1;
  344. }
  345. years = (time_t)t->tm_year - 70;
  346. if (t->tm_sec > 60) /* seconds after the minute - [0, 60] including leap second */
  347. {
  348. t->tm_min += t->tm_sec / 60;
  349. t->tm_sec %= 60;
  350. }
  351. if (t->tm_min >= 60) /* minutes after the hour - [0, 59] */
  352. {
  353. t->tm_hour += t->tm_min / 60;
  354. t->tm_min %= 60;
  355. }
  356. if (t->tm_hour >= 24) /* hours since midnight - [0, 23] */
  357. {
  358. t->tm_mday += t->tm_hour / 24;
  359. t->tm_hour %= 24;
  360. }
  361. if (t->tm_mon >= 12) /* months since January - [0, 11] */
  362. {
  363. t->tm_year += t->tm_mon / 12;
  364. t->tm_mon %= 12;
  365. }
  366. while (t->tm_mday > __spm[1 + t->tm_mon])
  367. {
  368. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  369. {
  370. --t->tm_mday;
  371. }
  372. t->tm_mday -= __spm[t->tm_mon];
  373. ++t->tm_mon;
  374. if (t->tm_mon > 11)
  375. {
  376. t->tm_mon = 0;
  377. ++t->tm_year;
  378. }
  379. }
  380. if (t->tm_year < 70)
  381. {
  382. rt_set_errno(EINVAL);
  383. return (time_t) -1;
  384. }
  385. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  386. day = years * 365 + (years + 1) / 4;
  387. /* After 2100 we have to substract 3 leap years for every 400 years
  388. This is not intuitive. Most mktime implementations do not support
  389. dates after 2059, anyway, so we might leave this out for it's
  390. bloat. */
  391. if (years >= 131)
  392. {
  393. years -= 131;
  394. years /= 100;
  395. day -= (years >> 2) * 3 + 1;
  396. if ((years &= 3) == 3)
  397. years--;
  398. day -= years;
  399. }
  400. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  401. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  402. /* day is now the number of days since 'Jan 1 1970' */
  403. i = 7;
  404. t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */
  405. i = 24;
  406. day *= i;
  407. i = 60;
  408. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  409. }
  410. RTM_EXPORT(timegm);
  411. int gettimeofday(struct timeval *tv, struct timezone *tz)
  412. {
  413. /* The use of the timezone structure is obsolete;
  414. * the tz argument should normally be specified as NULL.
  415. * The tz_dsttime field has never been used under Linux.
  416. * Thus, the following is purely of historic interest.
  417. */
  418. if(tz != RT_NULL)
  419. {
  420. tz->tz_dsttime = DST_NONE;
  421. #if defined(RT_LIBC_USING_LIGHT_TZ_DST)
  422. tz->tz_minuteswest = -(rt_tz_get() / 60);
  423. #else
  424. tz->tz_minuteswest = 0;
  425. #endif /* RT_LIBC_USING_LIGHT_TZ_DST */
  426. }
  427. if (tv != RT_NULL)
  428. {
  429. tv->tv_sec = 0;
  430. tv->tv_usec = 0;
  431. if (_control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv) == RT_EOK)
  432. return 0;
  433. }
  434. rt_set_errno(EINVAL);
  435. return -1;
  436. }
  437. RTM_EXPORT(gettimeofday);
  438. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  439. {
  440. /* The use of the timezone structure is obsolete;
  441. * the tz argument should normally be specified as NULL.
  442. * The tz_dsttime field has never been used under Linux.
  443. * Thus, the following is purely of historic interest.
  444. */
  445. if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0)
  446. {
  447. if (_control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMEVAL, (void *)tv) == RT_EOK)
  448. return 0;
  449. }
  450. rt_set_errno(EINVAL);
  451. return -1;
  452. }
  453. RTM_EXPORT(settimeofday);
  454. #if defined(RT_USING_POSIX_DELAY) && defined(RT_USING_KTIME)
  455. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  456. {
  457. struct timespec old_ts = {0};
  458. struct timespec new_ts = {0};
  459. if (rqtp == RT_NULL)
  460. {
  461. rt_set_errno(EFAULT);
  462. return -1;
  463. }
  464. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  465. {
  466. rt_set_errno(EINVAL);
  467. return -1;
  468. }
  469. unsigned long ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  470. rt_ktime_boottime_get_ns(&old_ts);
  471. rt_ktime_hrtimer_ndelay(ns);
  472. if (rt_get_errno() == -RT_EINTR)
  473. {
  474. if (rmtp)
  475. {
  476. rt_ktime_boottime_get_ns(&new_ts);
  477. rmtp->tv_sec = 0;
  478. rmtp->tv_nsec =
  479. (old_ts.tv_nsec + ns) - ((new_ts.tv_sec - old_ts.tv_sec) * NANOSECOND_PER_SECOND + new_ts.tv_nsec);
  480. if (rmtp->tv_nsec > NANOSECOND_PER_SECOND)
  481. {
  482. rmtp->tv_nsec %= NANOSECOND_PER_SECOND;
  483. rmtp->tv_sec += rmtp->tv_nsec / NANOSECOND_PER_SECOND;
  484. }
  485. }
  486. rt_set_errno(EINTR);
  487. return -1;
  488. }
  489. return 0;
  490. }
  491. RTM_EXPORT(nanosleep);
  492. #endif /* RT_USING_POSIX_DELAY && RT_USING_KTIME */
  493. #if defined(RT_USING_POSIX_CLOCK) && defined(RT_USING_KTIME)
  494. int clock_getres(clockid_t clockid, struct timespec *res)
  495. {
  496. if (res == RT_NULL)
  497. {
  498. rt_set_errno(EFAULT);
  499. return -1;
  500. }
  501. switch (clockid)
  502. {
  503. case CLOCK_REALTIME: // use RTC
  504. case CLOCK_REALTIME_COARSE:
  505. return _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMERES, res);
  506. case CLOCK_MONOTONIC: // use cputimer
  507. case CLOCK_MONOTONIC_COARSE:
  508. case CLOCK_MONOTONIC_RAW:
  509. case CLOCK_BOOTTIME:
  510. case CLOCK_PROCESS_CPUTIME_ID:
  511. case CLOCK_THREAD_CPUTIME_ID:
  512. res->tv_sec = 0;
  513. res->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL);
  514. return 0;
  515. default:
  516. rt_set_errno(EINVAL);
  517. return -1;
  518. }
  519. }
  520. RTM_EXPORT(clock_getres);
  521. int clock_gettime(clockid_t clockid, struct timespec *tp)
  522. {
  523. if (tp == RT_NULL)
  524. {
  525. rt_set_errno(EFAULT);
  526. return -1;
  527. }
  528. switch (clockid)
  529. {
  530. case CLOCK_REALTIME: // use RTC
  531. case CLOCK_REALTIME_COARSE:
  532. return _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, tp);
  533. case CLOCK_MONOTONIC: // use boottime
  534. case CLOCK_MONOTONIC_COARSE:
  535. case CLOCK_MONOTONIC_RAW:
  536. case CLOCK_BOOTTIME:
  537. return rt_ktime_boottime_get_ns(tp);
  538. case CLOCK_PROCESS_CPUTIME_ID:
  539. case CLOCK_THREAD_CPUTIME_ID:
  540. return rt_ktime_boottime_get_ns(tp); // TODO not yet implemented
  541. default:
  542. tp->tv_sec = 0;
  543. tp->tv_nsec = 0;
  544. rt_set_errno(EINVAL);
  545. return -1;
  546. }
  547. }
  548. RTM_EXPORT(clock_gettime);
  549. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
  550. {
  551. struct timespec ts = {0};
  552. rt_err_t err = -RT_EINVAL;
  553. if (rqtp == RT_NULL)
  554. {
  555. rt_set_errno(EFAULT);
  556. return -1;
  557. }
  558. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  559. {
  560. rt_set_errno(EINVAL);
  561. return -1;
  562. }
  563. switch (clockid)
  564. {
  565. case CLOCK_REALTIME: // use RTC
  566. if (flags & TIMER_ABSTIME)
  567. err = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, &ts);
  568. break;
  569. case CLOCK_MONOTONIC: // use boottime
  570. case CLOCK_PROCESS_CPUTIME_ID:
  571. if (flags & TIMER_ABSTIME)
  572. err = rt_ktime_boottime_get_ns(&ts);
  573. break;
  574. default:
  575. rt_set_errno(EINVAL);
  576. return -1;
  577. }
  578. if (err != RT_EOK)
  579. return err;
  580. int64_t ns = rqtp->tv_nsec - ts.tv_nsec + (rqtp->tv_sec - ts.tv_sec) * NANOSECOND_PER_SECOND;
  581. if (ns <= 0)
  582. return 0;
  583. if (flags & TIMER_ABSTIME)
  584. {
  585. ts.tv_nsec = ns % NANOSECOND_PER_SECOND;
  586. ts.tv_sec = ns / NANOSECOND_PER_SECOND;
  587. return nanosleep(&ts, rmtp);
  588. }
  589. else
  590. {
  591. return nanosleep(rqtp, rmtp);
  592. }
  593. }
  594. RTM_EXPORT(clock_nanosleep);
  595. int clock_settime(clockid_t clockid, const struct timespec *tp)
  596. {
  597. if (tp == RT_NULL)
  598. {
  599. rt_set_errno(EFAULT);
  600. return -1;
  601. }
  602. if (tp->tv_sec < 0 || tp->tv_nsec < 0 || tp->tv_nsec >= NANOSECOND_PER_SECOND)
  603. {
  604. rt_set_errno(EINVAL);
  605. return -1;
  606. }
  607. switch (clockid)
  608. {
  609. case CLOCK_REALTIME:
  610. return _control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMESPEC, (void *)tp);
  611. case CLOCK_REALTIME_COARSE:
  612. case CLOCK_MONOTONIC:
  613. case CLOCK_MONOTONIC_COARSE:
  614. case CLOCK_MONOTONIC_RAW:
  615. case CLOCK_BOOTTIME:
  616. case CLOCK_PROCESS_CPUTIME_ID:
  617. case CLOCK_THREAD_CPUTIME_ID:
  618. rt_set_errno(EPERM);
  619. return -1;
  620. default:
  621. rt_set_errno(EINVAL);
  622. return -1;
  623. }
  624. }
  625. RTM_EXPORT(clock_settime);
  626. int rt_timespec_to_tick(const struct timespec *time)
  627. {
  628. int tick;
  629. int nsecond, second;
  630. struct timespec tp = {0};
  631. RT_ASSERT(time != RT_NULL);
  632. tick = RT_WAITING_FOREVER;
  633. if (time != NULL)
  634. {
  635. /* get current tp */
  636. clock_gettime(CLOCK_REALTIME, &tp);
  637. if ((time->tv_nsec - tp.tv_nsec) < 0)
  638. {
  639. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  640. second = time->tv_sec - tp.tv_sec - 1;
  641. }
  642. else
  643. {
  644. nsecond = time->tv_nsec - tp.tv_nsec;
  645. second = time->tv_sec - tp.tv_sec;
  646. }
  647. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  648. if (tick < 0) tick = 0;
  649. }
  650. return tick;
  651. }
  652. RTM_EXPORT(rt_timespec_to_tick);
  653. #endif /* RT_USING_POSIX_CLOCK && RT_USING_KTIME */
  654. #if defined(RT_USING_POSIX_TIMER) && defined(RT_USING_KTIME)
  655. #include <resource_id.h>
  656. #define ACTIVE 1
  657. #define NOT_ACTIVE 0
  658. struct timer_obj
  659. {
  660. struct rt_ktime_hrtimer hrtimer;
  661. void (*sigev_notify_function)(union sigval val);
  662. union sigval val;
  663. struct timespec interval; /* Reload value */
  664. struct timespec value; /* Reload value */
  665. unsigned long reload; /* Reload value in ms */
  666. rt_uint32_t status;
  667. int sigev_signo;
  668. clockid_t clockid;
  669. timer_t timer_id;
  670. #ifdef RT_USING_SMART
  671. pid_t pid;
  672. struct rt_work *work;
  673. rt_list_t lwp_node;
  674. #endif
  675. };
  676. #ifdef RT_USING_SMART
  677. struct lwp_timer_event_param
  678. {
  679. struct rt_work work;
  680. union
  681. {
  682. int tid;
  683. pid_t pid;
  684. };
  685. int signo;
  686. };
  687. static void _lwp_timer_event_from_tid(struct rt_work *work, void *param)
  688. {
  689. rt_err_t ret;
  690. struct lwp_timer_event_param *data = rt_container_of(work, struct lwp_timer_event_param, work);
  691. rt_thread_t thread;
  692. RT_ASSERT(data->tid);
  693. thread = lwp_tid_get_thread(data->tid);
  694. ret = lwp_thread_signal_kill(thread, data->signo, SI_TIMER, 0);
  695. if (ret)
  696. {
  697. LOG_W("%s: Do kill failed(tid %d) returned %d", __func__, data->tid, ret);
  698. }
  699. }
  700. static void _lwp_timer_event_from_pid(struct rt_work *work, void *param)
  701. {
  702. rt_err_t ret;
  703. struct lwp_timer_event_param *data = rt_container_of(work, struct lwp_timer_event_param, work);
  704. ret = lwp_signal_kill(lwp_from_pid(data->pid), data->signo, SI_TIMER, 0);
  705. if (ret)
  706. {
  707. LOG_W("%s: Do kill failed(pid %d) returned %d", __func__, data->pid, ret);
  708. }
  709. }
  710. int timer_list_free(rt_list_t *timer_list)
  711. {
  712. struct timer_obj *pos, *n;
  713. rt_list_for_each_entry_safe(pos, n, timer_list, lwp_node)
  714. {
  715. timer_delete(pos->timer_id);
  716. }
  717. return 0;
  718. }
  719. #endif /* RT_USING_SMART */
  720. static void rtthread_timer_wrapper(void *timerobj)
  721. {
  722. struct timer_obj *timer;
  723. timer = (struct timer_obj *)timerobj;
  724. if (timer->reload == 0U)
  725. {
  726. timer->status = NOT_ACTIVE;
  727. }
  728. timer->reload = ((timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) * RT_KTIME_RESMUL) /
  729. rt_ktime_cputimer_getres();
  730. if (timer->reload)
  731. {
  732. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  733. rt_ktime_hrtimer_start(&timer->hrtimer);
  734. }
  735. #ifdef RT_USING_SMART
  736. /* this field is named as tid in musl */
  737. int tid = *(int *)&timer->sigev_notify_function;
  738. struct lwp_timer_event_param *data = rt_container_of(timer->work, struct lwp_timer_event_param, work);
  739. data->signo = timer->sigev_signo;
  740. if (!tid)
  741. {
  742. data->pid = timer->pid;
  743. rt_work_init(timer->work, _lwp_timer_event_from_pid, 0);
  744. }
  745. else
  746. {
  747. data->tid = tid;
  748. rt_work_init(timer->work, _lwp_timer_event_from_tid, 0);
  749. }
  750. if (rt_work_submit(timer->work, 0))
  751. RT_ASSERT(0);
  752. #else
  753. if(timer->sigev_notify_function != RT_NULL)
  754. {
  755. (timer->sigev_notify_function)(timer->val);
  756. }
  757. #endif /* RT_USING_SMART */
  758. }
  759. #define TIMER_ID_MAX 50
  760. static struct timer_obj *_g_timerid[TIMER_ID_MAX];
  761. static void *timer_id[TIMER_ID_MAX];
  762. static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id);
  763. /**
  764. * @brief Create a per-process timer.
  765. *
  766. * This API does not accept SIGEV_THREAD as valid signal event notification
  767. * type.
  768. *
  769. * See IEEE 1003.1
  770. */
  771. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
  772. {
  773. static int num = 0;
  774. int _timerid = 0;
  775. struct timer_obj *timer;
  776. char timername[RT_NAME_MAX] = {0};
  777. if (evp == RT_NULL || timerid == RT_NULL)
  778. {
  779. rt_set_errno(EINVAL);
  780. return -1;
  781. }
  782. if (evp->sigev_notify == SIGEV_THREAD) // TODO need to implement
  783. {
  784. rt_set_errno(EINVAL);
  785. return -1;
  786. }
  787. switch (clockid)
  788. {
  789. case CLOCK_REALTIME:
  790. case CLOCK_REALTIME_ALARM:
  791. case CLOCK_MONOTONIC:
  792. case CLOCK_BOOTTIME:
  793. case CLOCK_BOOTTIME_ALARM:
  794. case CLOCK_PROCESS_CPUTIME_ID:
  795. case CLOCK_THREAD_CPUTIME_ID:
  796. break; // Only these ids are supported
  797. default:
  798. rt_set_errno(EINVAL);
  799. return -1;
  800. }
  801. timer = rt_malloc(sizeof(struct timer_obj));
  802. if(timer == RT_NULL)
  803. {
  804. rt_set_errno(ENOMEM);
  805. return -1;
  806. }
  807. rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
  808. num %= 100;
  809. timer->sigev_signo = evp->sigev_signo;
  810. #ifdef RT_USING_SMART
  811. struct rt_work *work;
  812. struct rt_lwp *lwp = lwp_self();
  813. struct lwp_timer_event_param *param;
  814. param = rt_malloc(sizeof(struct lwp_timer_event_param));
  815. work = &param->work;
  816. if (!work)
  817. {
  818. rt_set_errno(ENOMEM);
  819. return -1;
  820. }
  821. if (lwp)
  822. {
  823. timer->pid = lwp_self()->pid;
  824. rt_list_insert_after(&lwp->timer, &timer->lwp_node);
  825. }
  826. else
  827. {
  828. timer->pid = 0; /* pid 0 is never used */
  829. }
  830. timer->work = work;
  831. #endif /* RT_USING_SMART */
  832. timer->sigev_notify_function = evp->sigev_notify_function;
  833. timer->val = evp->sigev_value;
  834. timer->interval.tv_sec = 0;
  835. timer->interval.tv_nsec = 0;
  836. timer->reload = 0U;
  837. timer->status = NOT_ACTIVE;
  838. timer->clockid = clockid;
  839. rt_ktime_hrtimer_init(&timer->hrtimer, timername, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
  840. rtthread_timer_wrapper, timer);
  841. _timerid = resource_id_get(&id_timer);
  842. if (_timerid < 0)
  843. {
  844. LOG_E("_timerid overflow!");
  845. return -1; /* todo:memory leak */
  846. }
  847. _g_timerid[_timerid] = timer;
  848. timer->timer_id = (timer_t)(rt_ubase_t)_timerid;
  849. *timerid = (timer_t)(rt_ubase_t)_timerid;
  850. return 0;
  851. }
  852. RTM_EXPORT(timer_create);
  853. /**
  854. * @brief Delete a per-process timer.
  855. *
  856. * See IEEE 1003.1
  857. */
  858. int timer_delete(timer_t timerid)
  859. {
  860. struct timer_obj *timer;
  861. rt_ubase_t ktimerid;
  862. ktimerid = (rt_ubase_t)timerid;
  863. if (ktimerid < 0 || ktimerid >= TIMER_ID_MAX)
  864. {
  865. rt_set_errno(EINVAL);
  866. return -1;
  867. }
  868. if (_g_timerid[ktimerid] == NULL)
  869. {
  870. rt_set_errno(EINVAL);
  871. LOG_E("can not find timer!");
  872. return -1;
  873. }
  874. timer = _g_timerid[ktimerid];
  875. resource_id_put(&id_timer, ktimerid);
  876. if (timer == RT_NULL)
  877. {
  878. rt_set_errno(EINVAL);
  879. return -1;
  880. }
  881. if (timer->status == ACTIVE)
  882. {
  883. timer->status = NOT_ACTIVE;
  884. rt_ktime_hrtimer_stop(&timer->hrtimer);
  885. }
  886. rt_ktime_hrtimer_detach(&timer->hrtimer);
  887. #ifdef RT_USING_SMART
  888. if (timer->pid)
  889. rt_list_remove(&timer->lwp_node);
  890. rt_free(timer->work);
  891. #endif
  892. rt_free(timer);
  893. return 0;
  894. }
  895. RTM_EXPORT(timer_delete);
  896. /**
  897. *
  898. * Return the overrun count for the last timer expiration.
  899. * It is subefficient to create a new structure to get overrun count.
  900. **/
  901. int timer_getoverrun(timer_t timerid)
  902. {
  903. rt_set_errno(ENOSYS);
  904. return -1;
  905. }
  906. /**
  907. * @brief Get amount of time left for expiration on a per-process timer.
  908. *
  909. * See IEEE 1003.1
  910. */
  911. int timer_gettime(timer_t timerid, struct itimerspec *its)
  912. {
  913. struct timer_obj *timer;
  914. rt_uint32_t seconds, nanoseconds;
  915. timer = _g_timerid[(rt_ubase_t)timerid];
  916. if (timer == NULL)
  917. {
  918. rt_set_errno(EINVAL);
  919. return -1;
  920. }
  921. if (its == NULL)
  922. {
  923. rt_set_errno(EFAULT);
  924. return -1;
  925. }
  926. if (timer->status == ACTIVE)
  927. {
  928. unsigned long remain_cnt;
  929. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_cnt);
  930. nanoseconds = ((remain_cnt - rt_ktime_cputimer_getcnt()) * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
  931. seconds = nanoseconds / NANOSECOND_PER_SECOND;
  932. nanoseconds = nanoseconds % NANOSECOND_PER_SECOND;
  933. its->it_value.tv_sec = (rt_int32_t)seconds;
  934. its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
  935. }
  936. else
  937. {
  938. /* Timer is disarmed */
  939. its->it_value.tv_sec = 0;
  940. its->it_value.tv_nsec = 0;
  941. }
  942. /* The interval last set by timer_settime() */
  943. its->it_interval = timer->interval;
  944. return 0;
  945. }
  946. RTM_EXPORT(timer_gettime);
  947. /**
  948. * @brief Sets expiration time of per-process timer.
  949. *
  950. * See IEEE 1003.1
  951. */
  952. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  953. struct itimerspec *ovalue)
  954. {
  955. struct timespec ts = {0};
  956. rt_err_t err = RT_EOK;
  957. struct timer_obj *timer;
  958. timer = _g_timerid[(rt_ubase_t)timerid];
  959. if (timer == NULL ||
  960. value->it_interval.tv_nsec < 0 ||
  961. value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
  962. value->it_interval.tv_sec < 0 ||
  963. value->it_value.tv_nsec < 0 ||
  964. value->it_value.tv_nsec >= NANOSECOND_PER_SECOND ||
  965. value->it_value.tv_sec < 0)
  966. {
  967. rt_set_errno(EINVAL);
  968. return -1;
  969. }
  970. /* Save time to expire and old reload value. */
  971. if (ovalue != NULL)
  972. {
  973. timer_gettime(timerid, ovalue);
  974. }
  975. /* Stop the timer if the value is 0 */
  976. if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
  977. {
  978. if (timer->status == ACTIVE)
  979. {
  980. rt_ktime_hrtimer_stop(&timer->hrtimer);
  981. }
  982. timer->status = NOT_ACTIVE;
  983. return 0;
  984. }
  985. switch (timer->clockid)
  986. {
  987. case CLOCK_REALTIME:
  988. case CLOCK_REALTIME_ALARM:
  989. if (flags & TIMER_ABSTIME)
  990. err = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMESPEC, &ts);
  991. break;
  992. case CLOCK_MONOTONIC:
  993. case CLOCK_BOOTTIME:
  994. case CLOCK_BOOTTIME_ALARM:
  995. case CLOCK_PROCESS_CPUTIME_ID:
  996. case CLOCK_THREAD_CPUTIME_ID:
  997. if (flags & TIMER_ABSTIME)
  998. err = rt_ktime_boottime_get_ns(&ts);
  999. break;
  1000. default:
  1001. rt_set_errno(EINVAL);
  1002. return -1;
  1003. }
  1004. if (err != RT_EOK)
  1005. return err;
  1006. int64_t ns = value->it_value.tv_nsec - ts.tv_nsec + (value->it_value.tv_sec - ts.tv_sec) * NANOSECOND_PER_SECOND;
  1007. if (ns <= 0)
  1008. return 0;
  1009. unsigned long res = rt_ktime_cputimer_getres();
  1010. timer->reload = (ns * RT_KTIME_RESMUL) / res;
  1011. timer->interval.tv_sec = value->it_interval.tv_sec;
  1012. timer->interval.tv_nsec = value->it_interval.tv_nsec;
  1013. timer->value.tv_sec = value->it_value.tv_sec;
  1014. timer->value.tv_nsec = value->it_value.tv_nsec;
  1015. if (timer->status == ACTIVE)
  1016. {
  1017. rt_ktime_hrtimer_stop(&timer->hrtimer);
  1018. }
  1019. timer->status = ACTIVE;
  1020. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  1021. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  1022. else
  1023. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  1024. rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  1025. rt_ktime_hrtimer_start(&timer->hrtimer);
  1026. return 0;
  1027. }
  1028. RTM_EXPORT(timer_settime);
  1029. #endif /* RT_USING_POSIX_TIMER && RT_USING_KTIME */