kservice.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-16 Bernard the first version
  9. * 2006-05-25 Bernard rewrite vsprintf
  10. * 2006-08-10 Bernard add rt_show_version
  11. * 2010-03-17 Bernard remove rt_strlcpy function
  12. * fix gcc compiling issue.
  13. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  14. * 2012-07-18 Arda add the alignment display for signed integer
  15. * 2012-11-23 Bernard fix IAR compiler error.
  16. * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
  17. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.
  18. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.
  19. * 2015-07-06 Bernard Add rt_assert_handler routine.
  20. */
  21. #include <rtthread.h>
  22. #include <rthw.h>
  23. #include "string.h"
  24. #ifdef RT_USING_MODULE
  25. #include <dlmodule.h>
  26. #endif
  27. /* use precision */
  28. #define RT_PRINTF_PRECISION
  29. /**
  30. * @addtogroup KernelService
  31. */
  32. /**@{*/
  33. /* global errno in RT-Thread */
  34. static volatile int __rt_errno;
  35. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  36. static rt_device_t _console_device = RT_NULL;
  37. #endif
  38. /*
  39. * This function will get errno
  40. *
  41. * @return errno
  42. */
  43. rt_err_t rt_get_errno(void)
  44. {
  45. rt_thread_t tid;
  46. if (rt_interrupt_get_nest() != 0)
  47. {
  48. /* it's in interrupt context */
  49. return __rt_errno;
  50. }
  51. tid = rt_thread_self();
  52. if (tid == RT_NULL)
  53. return __rt_errno;
  54. return tid->error;
  55. }
  56. RTM_EXPORT(rt_get_errno);
  57. /*
  58. * This function will set errno
  59. *
  60. * @param error the errno shall be set
  61. */
  62. void rt_set_errno(rt_err_t error)
  63. {
  64. rt_thread_t tid;
  65. if (rt_interrupt_get_nest() != 0)
  66. {
  67. /* it's in interrupt context */
  68. __rt_errno = error;
  69. return;
  70. }
  71. tid = rt_thread_self();
  72. if (tid == RT_NULL)
  73. {
  74. __rt_errno = error;
  75. return;
  76. }
  77. tid->error = error;
  78. }
  79. RTM_EXPORT(rt_set_errno);
  80. /**
  81. * This function returns errno.
  82. *
  83. * @return the errno in the system
  84. */
  85. int *_rt_errno(void)
  86. {
  87. rt_thread_t tid;
  88. if (rt_interrupt_get_nest() != 0)
  89. return (int *)&__rt_errno;
  90. tid = rt_thread_self();
  91. if (tid != RT_NULL)
  92. return (int *) & (tid->error);
  93. return (int *)&__rt_errno;
  94. }
  95. RTM_EXPORT(_rt_errno);
  96. /**
  97. * This function will set the content of memory to specified value
  98. *
  99. * @param s the address of source memory
  100. * @param c the value shall be set in content
  101. * @param count the copied length
  102. *
  103. * @return the address of source memory
  104. */
  105. void *rt_memset(void *s, int c, rt_ubase_t count)
  106. {
  107. #ifdef RT_USING_TINY_SIZE
  108. char *xs = (char *)s;
  109. while (count--)
  110. *xs++ = c;
  111. return s;
  112. #else
  113. #define LBLOCKSIZE (sizeof(long))
  114. #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
  115. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  116. unsigned int i;
  117. char *m = (char *)s;
  118. unsigned long buffer;
  119. unsigned long *aligned_addr;
  120. unsigned int d = c & 0xff; /* To avoid sign extension, copy C to an
  121. unsigned variable. */
  122. if (!TOO_SMALL(count) && !UNALIGNED(s))
  123. {
  124. /* If we get this far, we know that n is large and m is word-aligned. */
  125. aligned_addr = (unsigned long *)s;
  126. /* Store D into each char sized location in BUFFER so that
  127. * we can set large blocks quickly.
  128. */
  129. if (LBLOCKSIZE == 4)
  130. {
  131. buffer = (d << 8) | d;
  132. buffer |= (buffer << 16);
  133. }
  134. else
  135. {
  136. buffer = 0;
  137. for (i = 0; i < LBLOCKSIZE; i ++)
  138. buffer = (buffer << 8) | d;
  139. }
  140. while (count >= LBLOCKSIZE * 4)
  141. {
  142. *aligned_addr++ = buffer;
  143. *aligned_addr++ = buffer;
  144. *aligned_addr++ = buffer;
  145. *aligned_addr++ = buffer;
  146. count -= 4 * LBLOCKSIZE;
  147. }
  148. while (count >= LBLOCKSIZE)
  149. {
  150. *aligned_addr++ = buffer;
  151. count -= LBLOCKSIZE;
  152. }
  153. /* Pick up the remainder with a bytewise loop. */
  154. m = (char *)aligned_addr;
  155. }
  156. while (count--)
  157. {
  158. *m++ = (char)d;
  159. }
  160. return s;
  161. #undef LBLOCKSIZE
  162. #undef UNALIGNED
  163. #undef TOO_SMALL
  164. #endif
  165. }
  166. RTM_EXPORT(rt_memset);
  167. /**
  168. * This function will copy memory content from source address to destination
  169. * address.
  170. *
  171. * @param dst the address of destination memory
  172. * @param src the address of source memory
  173. * @param count the copied length
  174. *
  175. * @return the address of destination memory
  176. */
  177. void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
  178. {
  179. #ifdef RT_USING_TINY_SIZE
  180. char *tmp = (char *)dst, *s = (char *)src;
  181. rt_ubase_t len;
  182. if (tmp <= s || tmp > (s + count))
  183. {
  184. while (count--)
  185. *tmp ++ = *s ++;
  186. }
  187. else
  188. {
  189. for (len = count; len > 0; len --)
  190. tmp[len - 1] = s[len - 1];
  191. }
  192. return dst;
  193. #else
  194. #define UNALIGNED(X, Y) \
  195. (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
  196. #define BIGBLOCKSIZE (sizeof (long) << 2)
  197. #define LITTLEBLOCKSIZE (sizeof (long))
  198. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  199. char *dst_ptr = (char *)dst;
  200. char *src_ptr = (char *)src;
  201. long *aligned_dst;
  202. long *aligned_src;
  203. int len = count;
  204. /* If the size is small, or either SRC or DST is unaligned,
  205. then punt into the byte copy loop. This should be rare. */
  206. if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
  207. {
  208. aligned_dst = (long *)dst_ptr;
  209. aligned_src = (long *)src_ptr;
  210. /* Copy 4X long words at a time if possible. */
  211. while (len >= BIGBLOCKSIZE)
  212. {
  213. *aligned_dst++ = *aligned_src++;
  214. *aligned_dst++ = *aligned_src++;
  215. *aligned_dst++ = *aligned_src++;
  216. *aligned_dst++ = *aligned_src++;
  217. len -= BIGBLOCKSIZE;
  218. }
  219. /* Copy one long word at a time if possible. */
  220. while (len >= LITTLEBLOCKSIZE)
  221. {
  222. *aligned_dst++ = *aligned_src++;
  223. len -= LITTLEBLOCKSIZE;
  224. }
  225. /* Pick up any residual with a byte copier. */
  226. dst_ptr = (char *)aligned_dst;
  227. src_ptr = (char *)aligned_src;
  228. }
  229. while (len--)
  230. *dst_ptr++ = *src_ptr++;
  231. return dst;
  232. #undef UNALIGNED
  233. #undef BIGBLOCKSIZE
  234. #undef LITTLEBLOCKSIZE
  235. #undef TOO_SMALL
  236. #endif
  237. }
  238. RTM_EXPORT(rt_memcpy);
  239. /**
  240. * This function will move memory content from source address to destination
  241. * address.
  242. *
  243. * @param dest the address of destination memory
  244. * @param src the address of source memory
  245. * @param n the copied length
  246. *
  247. * @return the address of destination memory
  248. */
  249. void *rt_memmove(void *dest, const void *src, rt_ubase_t n)
  250. {
  251. char *tmp = (char *)dest, *s = (char *)src;
  252. if (s < tmp && tmp < s + n)
  253. {
  254. tmp += n;
  255. s += n;
  256. while (n--)
  257. *(--tmp) = *(--s);
  258. }
  259. else
  260. {
  261. while (n--)
  262. *tmp++ = *s++;
  263. }
  264. return dest;
  265. }
  266. RTM_EXPORT(rt_memmove);
  267. /**
  268. * This function will compare two areas of memory
  269. *
  270. * @param cs one area of memory
  271. * @param ct another area of memory
  272. * @param count the size of the area
  273. *
  274. * @return the result
  275. */
  276. rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count)
  277. {
  278. const unsigned char *su1, *su2;
  279. int res = 0;
  280. for (su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)
  281. if ((res = *su1 - *su2) != 0)
  282. break;
  283. return res;
  284. }
  285. RTM_EXPORT(rt_memcmp);
  286. /**
  287. * This function will return the first occurrence of a string.
  288. *
  289. * @param s1 the source string
  290. * @param s2 the find string
  291. *
  292. * @return the first occurrence of a s2 in s1, or RT_NULL if no found.
  293. */
  294. char *rt_strstr(const char *s1, const char *s2)
  295. {
  296. int l1, l2;
  297. l2 = rt_strlen(s2);
  298. if (!l2)
  299. return (char *)s1;
  300. l1 = rt_strlen(s1);
  301. while (l1 >= l2)
  302. {
  303. l1 --;
  304. if (!rt_memcmp(s1, s2, l2))
  305. return (char *)s1;
  306. s1 ++;
  307. }
  308. return RT_NULL;
  309. }
  310. RTM_EXPORT(rt_strstr);
  311. /**
  312. * This function will compare two strings while ignoring differences in case
  313. *
  314. * @param a the string to be compared
  315. * @param b the string to be compared
  316. *
  317. * @return the result
  318. */
  319. rt_uint32_t rt_strcasecmp(const char *a, const char *b)
  320. {
  321. int ca, cb;
  322. do
  323. {
  324. ca = *a++ & 0xff;
  325. cb = *b++ & 0xff;
  326. if (ca >= 'A' && ca <= 'Z')
  327. ca += 'a' - 'A';
  328. if (cb >= 'A' && cb <= 'Z')
  329. cb += 'a' - 'A';
  330. }
  331. while (ca == cb && ca != '\0');
  332. return ca - cb;
  333. }
  334. RTM_EXPORT(rt_strcasecmp);
  335. /**
  336. * This function will copy string no more than n bytes.
  337. *
  338. * @param dst the string to copy
  339. * @param src the string to be copied
  340. * @param n the maximum copied length
  341. *
  342. * @return the result
  343. */
  344. char *rt_strncpy(char *dst, const char *src, rt_ubase_t n)
  345. {
  346. if (n != 0)
  347. {
  348. char *d = dst;
  349. const char *s = src;
  350. do
  351. {
  352. if ((*d++ = *s++) == 0)
  353. {
  354. /* NUL pad the remaining n-1 bytes */
  355. while (--n != 0)
  356. *d++ = 0;
  357. break;
  358. }
  359. } while (--n != 0);
  360. }
  361. return (dst);
  362. }
  363. RTM_EXPORT(rt_strncpy);
  364. /**
  365. * This function will compare two strings with specified maximum length
  366. *
  367. * @param cs the string to be compared
  368. * @param ct the string to be compared
  369. * @param count the maximum compare length
  370. *
  371. * @return the result
  372. */
  373. rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count)
  374. {
  375. register signed char __res = 0;
  376. while (count)
  377. {
  378. if ((__res = *cs - *ct++) != 0 || !*cs++)
  379. break;
  380. count --;
  381. }
  382. return __res;
  383. }
  384. RTM_EXPORT(rt_strncmp);
  385. /**
  386. * This function will compare two strings without specified length
  387. *
  388. * @param cs the string to be compared
  389. * @param ct the string to be compared
  390. *
  391. * @return the result
  392. */
  393. rt_int32_t rt_strcmp(const char *cs, const char *ct)
  394. {
  395. while (*cs && *cs == *ct)
  396. cs++, ct++;
  397. return (*cs - *ct);
  398. }
  399. RTM_EXPORT(rt_strcmp);
  400. /**
  401. * The strnlen() function returns the number of characters in the
  402. * string pointed to by s, excluding the terminating null byte ('\0'),
  403. * but at most maxlen. In doing this, strnlen() looks only at the
  404. * first maxlen characters in the string pointed to by s and never
  405. * beyond s+maxlen.
  406. *
  407. * @param s the string
  408. * @param maxlen the max size
  409. * @return the length of string
  410. */
  411. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  412. {
  413. const char *sc;
  414. for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
  415. ;
  416. return sc - s;
  417. }
  418. RTM_EXPORT(rt_strnlen);
  419. /**
  420. * This function will return the length of a string, which terminate will
  421. * null character.
  422. *
  423. * @param s the string
  424. *
  425. * @return the length of string
  426. */
  427. rt_size_t rt_strlen(const char *s)
  428. {
  429. const char *sc;
  430. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  431. ;
  432. return sc - s;
  433. }
  434. RTM_EXPORT(rt_strlen);
  435. #ifdef RT_USING_HEAP
  436. /**
  437. * This function will duplicate a string.
  438. *
  439. * @param s the string to be duplicated
  440. *
  441. * @return the duplicated string pointer
  442. */
  443. char *rt_strdup(const char *s)
  444. {
  445. rt_size_t len = rt_strlen(s) + 1;
  446. char *tmp = (char *)rt_malloc(len);
  447. if (!tmp)
  448. return RT_NULL;
  449. rt_memcpy(tmp, s, len);
  450. return tmp;
  451. }
  452. RTM_EXPORT(rt_strdup);
  453. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  454. char *strdup(const char *s) __attribute__((alias("rt_strdup")));
  455. #endif
  456. #endif
  457. /**
  458. * This function will show the version of rt-thread rtos
  459. */
  460. void rt_show_version(void)
  461. {
  462. rt_kprintf("\n \\ | /\n");
  463. rt_kprintf("- RT - Thread Operating System\n");
  464. rt_kprintf(" / | \\ %d.%d.%d build %s\n",
  465. RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__);
  466. rt_kprintf(" 2006 - 2019 Copyright by rt-thread team\n");
  467. }
  468. RTM_EXPORT(rt_show_version);
  469. /* private function */
  470. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  471. #ifdef RT_PRINTF_LONGLONG
  472. rt_inline int divide(long long *n, int base)
  473. {
  474. int res;
  475. /* optimized for processor which does not support divide instructions. */
  476. if (base == 10)
  477. {
  478. res = (int)(((unsigned long long)*n) % 10U);
  479. *n = (long long)(((unsigned long long)*n) / 10U);
  480. }
  481. else
  482. {
  483. res = (int)(((unsigned long long)*n) % 16U);
  484. *n = (long long)(((unsigned long long)*n) / 16U);
  485. }
  486. return res;
  487. }
  488. #else
  489. rt_inline int divide(long *n, int base)
  490. {
  491. int res;
  492. /* optimized for processor which does not support divide instructions. */
  493. if (base == 10)
  494. {
  495. res = (int)(((unsigned long)*n) % 10U);
  496. *n = (long)(((unsigned long)*n) / 10U);
  497. }
  498. else
  499. {
  500. res = (int)(((unsigned long)*n) % 16U);
  501. *n = (long)(((unsigned long)*n) / 16U);
  502. }
  503. return res;
  504. }
  505. #endif
  506. rt_inline int skip_atoi(const char **s)
  507. {
  508. register int i = 0;
  509. while (isdigit(**s))
  510. i = i * 10 + *((*s)++) - '0';
  511. return i;
  512. }
  513. #define ZEROPAD (1 << 0) /* pad with zero */
  514. #define SIGN (1 << 1) /* unsigned/signed long */
  515. #define PLUS (1 << 2) /* show plus */
  516. #define SPACE (1 << 3) /* space if plus */
  517. #define LEFT (1 << 4) /* left justified */
  518. #define SPECIAL (1 << 5) /* 0x */
  519. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  520. #ifdef RT_PRINTF_PRECISION
  521. static char *print_number(char *buf,
  522. char *end,
  523. #ifdef RT_PRINTF_LONGLONG
  524. long long num,
  525. #else
  526. long num,
  527. #endif
  528. int base,
  529. int s,
  530. int precision,
  531. int type)
  532. #else
  533. static char *print_number(char *buf,
  534. char *end,
  535. #ifdef RT_PRINTF_LONGLONG
  536. long long num,
  537. #else
  538. long num,
  539. #endif
  540. int base,
  541. int s,
  542. int type)
  543. #endif
  544. {
  545. char c, sign;
  546. #ifdef RT_PRINTF_LONGLONG
  547. char tmp[32];
  548. #else
  549. char tmp[16];
  550. #endif
  551. int precision_bak = precision;
  552. const char *digits;
  553. static const char small_digits[] = "0123456789abcdef";
  554. static const char large_digits[] = "0123456789ABCDEF";
  555. register int i;
  556. register int size;
  557. size = s;
  558. digits = (type & LARGE) ? large_digits : small_digits;
  559. if (type & LEFT)
  560. type &= ~ZEROPAD;
  561. c = (type & ZEROPAD) ? '0' : ' ';
  562. /* get sign */
  563. sign = 0;
  564. if (type & SIGN)
  565. {
  566. if (num < 0)
  567. {
  568. sign = '-';
  569. num = -num;
  570. }
  571. else if (type & PLUS)
  572. sign = '+';
  573. else if (type & SPACE)
  574. sign = ' ';
  575. }
  576. #ifdef RT_PRINTF_SPECIAL
  577. if (type & SPECIAL)
  578. {
  579. if (base == 16)
  580. size -= 2;
  581. else if (base == 8)
  582. size--;
  583. }
  584. #endif
  585. i = 0;
  586. if (num == 0)
  587. tmp[i++] = '0';
  588. else
  589. {
  590. while (num != 0)
  591. tmp[i++] = digits[divide(&num, base)];
  592. }
  593. #ifdef RT_PRINTF_PRECISION
  594. if (i > precision)
  595. precision = i;
  596. size -= precision;
  597. #else
  598. size -= i;
  599. #endif
  600. if (!(type & (ZEROPAD | LEFT)))
  601. {
  602. if ((sign) && (size > 0))
  603. size--;
  604. while (size-- > 0)
  605. {
  606. if (buf < end)
  607. *buf = ' ';
  608. ++ buf;
  609. }
  610. }
  611. if (sign)
  612. {
  613. if (buf < end)
  614. {
  615. *buf = sign;
  616. }
  617. -- size;
  618. ++ buf;
  619. }
  620. #ifdef RT_PRINTF_SPECIAL
  621. if (type & SPECIAL)
  622. {
  623. if (base == 8)
  624. {
  625. if (buf < end)
  626. *buf = '0';
  627. ++ buf;
  628. }
  629. else if (base == 16)
  630. {
  631. if (buf < end)
  632. *buf = '0';
  633. ++ buf;
  634. if (buf < end)
  635. {
  636. *buf = type & LARGE ? 'X' : 'x';
  637. }
  638. ++ buf;
  639. }
  640. }
  641. #endif
  642. /* no align to the left */
  643. if (!(type & LEFT))
  644. {
  645. while (size-- > 0)
  646. {
  647. if (buf < end)
  648. *buf = c;
  649. ++ buf;
  650. }
  651. }
  652. #ifdef RT_PRINTF_PRECISION
  653. while (i < precision--)
  654. {
  655. if (buf < end)
  656. *buf = '0';
  657. ++ buf;
  658. }
  659. #endif
  660. /* put number in the temporary buffer */
  661. while (i-- > 0 && (precision_bak != 0))
  662. {
  663. if (buf < end)
  664. *buf = tmp[i];
  665. ++ buf;
  666. }
  667. while (size-- > 0)
  668. {
  669. if (buf < end)
  670. *buf = ' ';
  671. ++ buf;
  672. }
  673. return buf;
  674. }
  675. rt_int32_t rt_vsnprintf(char *buf,
  676. rt_size_t size,
  677. const char *fmt,
  678. va_list args)
  679. {
  680. #ifdef RT_PRINTF_LONGLONG
  681. unsigned long long num;
  682. #else
  683. rt_uint32_t num;
  684. #endif
  685. int i, len;
  686. char *str, *end, c;
  687. const char *s;
  688. rt_uint8_t base; /* the base of number */
  689. rt_uint8_t flags; /* flags to print number */
  690. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  691. rt_int32_t field_width; /* width of output field */
  692. #ifdef RT_PRINTF_PRECISION
  693. int precision; /* min. # of digits for integers and max for a string */
  694. #endif
  695. str = buf;
  696. end = buf + size;
  697. /* Make sure end is always >= buf */
  698. if (end < buf)
  699. {
  700. end = ((char *) - 1);
  701. size = end - buf;
  702. }
  703. for (; *fmt ; ++fmt)
  704. {
  705. if (*fmt != '%')
  706. {
  707. if (str < end)
  708. *str = *fmt;
  709. ++ str;
  710. continue;
  711. }
  712. /* process flags */
  713. flags = 0;
  714. while (1)
  715. {
  716. /* skips the first '%' also */
  717. ++ fmt;
  718. if (*fmt == '-') flags |= LEFT;
  719. else if (*fmt == '+') flags |= PLUS;
  720. else if (*fmt == ' ') flags |= SPACE;
  721. else if (*fmt == '#') flags |= SPECIAL;
  722. else if (*fmt == '0') flags |= ZEROPAD;
  723. else break;
  724. }
  725. /* get field width */
  726. field_width = -1;
  727. if (isdigit(*fmt)) field_width = skip_atoi(&fmt);
  728. else if (*fmt == '*')
  729. {
  730. ++ fmt;
  731. /* it's the next argument */
  732. field_width = va_arg(args, int);
  733. if (field_width < 0)
  734. {
  735. field_width = -field_width;
  736. flags |= LEFT;
  737. }
  738. }
  739. #ifdef RT_PRINTF_PRECISION
  740. /* get the precision */
  741. precision = -1;
  742. if (*fmt == '.')
  743. {
  744. ++ fmt;
  745. if (isdigit(*fmt)) precision = skip_atoi(&fmt);
  746. else if (*fmt == '*')
  747. {
  748. ++ fmt;
  749. /* it's the next argument */
  750. precision = va_arg(args, int);
  751. }
  752. if (precision < 0) precision = 0;
  753. }
  754. #endif
  755. /* get the conversion qualifier */
  756. qualifier = 0;
  757. #ifdef RT_PRINTF_LONGLONG
  758. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  759. #else
  760. if (*fmt == 'h' || *fmt == 'l')
  761. #endif
  762. {
  763. qualifier = *fmt;
  764. ++ fmt;
  765. #ifdef RT_PRINTF_LONGLONG
  766. if (qualifier == 'l' && *fmt == 'l')
  767. {
  768. qualifier = 'L';
  769. ++ fmt;
  770. }
  771. #endif
  772. }
  773. /* the default base */
  774. base = 10;
  775. switch (*fmt)
  776. {
  777. case 'c':
  778. if (!(flags & LEFT))
  779. {
  780. while (--field_width > 0)
  781. {
  782. if (str < end) *str = ' ';
  783. ++ str;
  784. }
  785. }
  786. /* get character */
  787. c = (rt_uint8_t)va_arg(args, int);
  788. if (str < end) *str = c;
  789. ++ str;
  790. /* put width */
  791. while (--field_width > 0)
  792. {
  793. if (str < end) *str = ' ';
  794. ++ str;
  795. }
  796. continue;
  797. case 's':
  798. s = va_arg(args, char *);
  799. if (!s) s = "(NULL)";
  800. len = rt_strlen(s);
  801. #ifdef RT_PRINTF_PRECISION
  802. if (precision > 0 && len > precision) len = precision;
  803. #endif
  804. if (!(flags & LEFT))
  805. {
  806. while (len < field_width--)
  807. {
  808. if (str < end) *str = ' ';
  809. ++ str;
  810. }
  811. }
  812. for (i = 0; i < len; ++i)
  813. {
  814. if (str < end) *str = *s;
  815. ++ str;
  816. ++ s;
  817. }
  818. while (len < field_width--)
  819. {
  820. if (str < end) *str = ' ';
  821. ++ str;
  822. }
  823. continue;
  824. case 'p':
  825. if (field_width == -1)
  826. {
  827. field_width = sizeof(void *) << 1;
  828. flags |= ZEROPAD;
  829. }
  830. #ifdef RT_PRINTF_PRECISION
  831. str = print_number(str, end,
  832. (long)va_arg(args, void *),
  833. 16, field_width, precision, flags);
  834. #else
  835. str = print_number(str, end,
  836. (long)va_arg(args, void *),
  837. 16, field_width, flags);
  838. #endif
  839. continue;
  840. case '%':
  841. if (str < end) *str = '%';
  842. ++ str;
  843. continue;
  844. /* integer number formats - set up the flags and "break" */
  845. case 'o':
  846. base = 8;
  847. break;
  848. case 'X':
  849. flags |= LARGE;
  850. case 'x':
  851. base = 16;
  852. break;
  853. case 'd':
  854. case 'i':
  855. flags |= SIGN;
  856. case 'u':
  857. break;
  858. default:
  859. if (str < end) *str = '%';
  860. ++ str;
  861. if (*fmt)
  862. {
  863. if (str < end) *str = *fmt;
  864. ++ str;
  865. }
  866. else
  867. {
  868. -- fmt;
  869. }
  870. continue;
  871. }
  872. #ifdef RT_PRINTF_LONGLONG
  873. if (qualifier == 'L') num = va_arg(args, long long);
  874. else if (qualifier == 'l')
  875. #else
  876. if (qualifier == 'l')
  877. #endif
  878. {
  879. num = va_arg(args, rt_uint32_t);
  880. if (flags & SIGN) num = (rt_int32_t)num;
  881. }
  882. else if (qualifier == 'h')
  883. {
  884. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  885. if (flags & SIGN) num = (rt_int16_t)num;
  886. }
  887. else
  888. {
  889. num = va_arg(args, rt_uint32_t);
  890. if (flags & SIGN) num = (rt_int32_t)num;
  891. }
  892. #ifdef RT_PRINTF_PRECISION
  893. str = print_number(str, end, num, base, field_width, precision, flags);
  894. #else
  895. str = print_number(str, end, num, base, field_width, flags);
  896. #endif
  897. }
  898. if (size > 0)
  899. {
  900. if (str < end) *str = '\0';
  901. else
  902. {
  903. end[-1] = '\0';
  904. }
  905. }
  906. /* the trailing null byte doesn't count towards the total
  907. * ++str;
  908. */
  909. return str - buf;
  910. }
  911. RTM_EXPORT(rt_vsnprintf);
  912. /**
  913. * This function will fill a formatted string to buffer
  914. *
  915. * @param buf the buffer to save formatted string
  916. * @param size the size of buffer
  917. * @param fmt the format
  918. */
  919. rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  920. {
  921. rt_int32_t n;
  922. va_list args;
  923. va_start(args, fmt);
  924. n = rt_vsnprintf(buf, size, fmt, args);
  925. va_end(args);
  926. return n;
  927. }
  928. RTM_EXPORT(rt_snprintf);
  929. /**
  930. * This function will fill a formatted string to buffer
  931. *
  932. * @param buf the buffer to save formatted string
  933. * @param arg_ptr the arg_ptr
  934. * @param format the format
  935. */
  936. rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  937. {
  938. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  939. }
  940. RTM_EXPORT(rt_vsprintf);
  941. /**
  942. * This function will fill a formatted string to buffer
  943. *
  944. * @param buf the buffer to save formatted string
  945. * @param format the format
  946. */
  947. rt_int32_t rt_sprintf(char *buf, const char *format, ...)
  948. {
  949. rt_int32_t n;
  950. va_list arg_ptr;
  951. va_start(arg_ptr, format);
  952. n = rt_vsprintf(buf, format, arg_ptr);
  953. va_end(arg_ptr);
  954. return n;
  955. }
  956. RTM_EXPORT(rt_sprintf);
  957. #ifdef RT_USING_CONSOLE
  958. #ifdef RT_USING_DEVICE
  959. /**
  960. * This function returns the device using in console.
  961. *
  962. * @return the device using in console or RT_NULL
  963. */
  964. rt_device_t rt_console_get_device(void)
  965. {
  966. return _console_device;
  967. }
  968. RTM_EXPORT(rt_console_get_device);
  969. /**
  970. * This function will set a device as console device.
  971. * After set a device to console, all output of rt_kprintf will be
  972. * redirected to this new device.
  973. *
  974. * @param name the name of new console device
  975. *
  976. * @return the old console device handler
  977. */
  978. rt_device_t rt_console_set_device(const char *name)
  979. {
  980. rt_device_t new_device, old_device;
  981. /* save old device */
  982. old_device = _console_device;
  983. /* find new console device */
  984. new_device = rt_device_find(name);
  985. if (new_device != RT_NULL)
  986. {
  987. if (_console_device != RT_NULL)
  988. {
  989. /* close old console device */
  990. rt_device_close(_console_device);
  991. }
  992. /* set new console device */
  993. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  994. _console_device = new_device;
  995. }
  996. return old_device;
  997. }
  998. RTM_EXPORT(rt_console_set_device);
  999. #endif
  1000. RT_WEAK void rt_hw_console_output(const char *str)
  1001. {
  1002. /* empty console output */
  1003. }
  1004. RTM_EXPORT(rt_hw_console_output);
  1005. /**
  1006. * This function will put string to the console.
  1007. *
  1008. * @param str the string output to the console.
  1009. */
  1010. void rt_kputs(const char *str)
  1011. {
  1012. if (!str) return;
  1013. #ifdef RT_USING_DEVICE
  1014. if (_console_device == RT_NULL)
  1015. {
  1016. rt_hw_console_output(str);
  1017. }
  1018. else
  1019. {
  1020. rt_uint16_t old_flag = _console_device->open_flag;
  1021. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  1022. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1023. _console_device->open_flag = old_flag;
  1024. }
  1025. #else
  1026. rt_hw_console_output(str);
  1027. #endif
  1028. }
  1029. /**
  1030. * This function will print a formatted string on system console
  1031. *
  1032. * @param fmt the format
  1033. */
  1034. void rt_kprintf(const char *fmt, ...)
  1035. {
  1036. va_list args;
  1037. rt_size_t length;
  1038. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1039. rt_memset(rt_log_buf,'\0',sizeof(rt_log_buf));
  1040. va_start(args, fmt);
  1041. /* the return value of vsnprintf is the number of bytes that would be
  1042. * written to buffer had if the size of the buffer been sufficiently
  1043. * large excluding the terminating null byte. If the output string
  1044. * would be larger than the rt_log_buf, we have to adjust the output
  1045. * length. */
  1046. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1047. // if(device_status.dbgflag==1&&serial!=RT_NULL)
  1048. // {
  1049. // HMI_WRITE("debug.print.txt=%s",rt_log_buf);
  1050. // }
  1051. if (length > RT_CONSOLEBUF_SIZE - 1)
  1052. length = RT_CONSOLEBUF_SIZE - 1;
  1053. #ifdef RT_USING_DEVICE
  1054. if (_console_device == RT_NULL)
  1055. {
  1056. rt_hw_console_output(rt_log_buf);
  1057. }
  1058. else
  1059. {
  1060. rt_uint16_t old_flag = _console_device->open_flag;
  1061. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  1062. rt_device_write(_console_device, 0, rt_log_buf, length);
  1063. _console_device->open_flag = old_flag;
  1064. }
  1065. #else
  1066. rt_hw_console_output(rt_log_buf);
  1067. #endif
  1068. va_end(args);
  1069. }
  1070. RTM_EXPORT(rt_kprintf);
  1071. #endif
  1072. #ifdef RT_USING_HEAP
  1073. /**
  1074. * This function allocates a memory block, which address is aligned to the
  1075. * specified alignment size.
  1076. *
  1077. * @param size the allocated memory block size
  1078. * @param align the alignment size
  1079. *
  1080. * @return the allocated memory block on successful, otherwise returns RT_NULL
  1081. */
  1082. void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1083. {
  1084. void *ptr;
  1085. void *align_ptr;
  1086. int uintptr_size;
  1087. rt_size_t align_size;
  1088. /* sizeof pointer */
  1089. uintptr_size = sizeof(void*);
  1090. uintptr_size -= 1;
  1091. /* align the alignment size to uintptr size byte */
  1092. align = ((align + uintptr_size) & ~uintptr_size);
  1093. /* get total aligned size */
  1094. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1095. /* allocate memory block from heap */
  1096. ptr = rt_malloc(align_size);
  1097. if (ptr != RT_NULL)
  1098. {
  1099. /* the allocated memory block is aligned */
  1100. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1101. {
  1102. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1103. }
  1104. else
  1105. {
  1106. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1107. }
  1108. /* set the pointer before alignment pointer to the real pointer */
  1109. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1110. ptr = align_ptr;
  1111. }
  1112. return ptr;
  1113. }
  1114. RTM_EXPORT(rt_malloc_align);
  1115. /**
  1116. * This function release the memory block, which is allocated by
  1117. * rt_malloc_align function and address is aligned.
  1118. *
  1119. * @param ptr the memory block pointer
  1120. */
  1121. void rt_free_align(void *ptr)
  1122. {
  1123. void *real_ptr;
  1124. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1125. rt_free(real_ptr);
  1126. }
  1127. RTM_EXPORT(rt_free_align);
  1128. #endif
  1129. #ifndef RT_USING_CPU_FFS
  1130. const rt_uint8_t __lowest_bit_bitmap[] =
  1131. {
  1132. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1133. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1134. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1135. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1136. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1137. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1138. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1139. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1140. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1141. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1142. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1143. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1144. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1145. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1146. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1147. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1148. };
  1149. /**
  1150. * This function finds the first bit set (beginning with the least significant bit)
  1151. * in value and return the index of that bit.
  1152. *
  1153. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1154. * zero from any of these functions means that the argument was zero.
  1155. *
  1156. * @return return the index of the first bit set. If value is 0, then this function
  1157. * shall return 0.
  1158. */
  1159. int __rt_ffs(int value)
  1160. {
  1161. if (value == 0) return 0;
  1162. if (value & 0xff)
  1163. return __lowest_bit_bitmap[value & 0xff] + 1;
  1164. if (value & 0xff00)
  1165. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1166. if (value & 0xff0000)
  1167. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1168. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1169. }
  1170. #endif
  1171. #ifdef RT_DEBUG
  1172. /* RT_ASSERT(EX)'s hook */
  1173. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1174. /**
  1175. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1176. *
  1177. * @param hook the hook function
  1178. */
  1179. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1180. {
  1181. rt_assert_hook = hook;
  1182. }
  1183. /**
  1184. * The RT_ASSERT function.
  1185. *
  1186. * @param ex the assertion condition string
  1187. * @param func the function name when assertion.
  1188. * @param line the file line number when assertion.
  1189. */
  1190. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1191. {
  1192. volatile char dummy = 0;
  1193. if (rt_assert_hook == RT_NULL)
  1194. {
  1195. #ifdef RT_USING_MODULE
  1196. if (dlmodule_self())
  1197. {
  1198. /* close assertion module */
  1199. dlmodule_exit(-1);
  1200. }
  1201. else
  1202. #endif
  1203. {
  1204. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1205. while (dummy == 0);
  1206. }
  1207. }
  1208. else
  1209. {
  1210. rt_assert_hook(ex_string, func, line);
  1211. }
  1212. }
  1213. RTM_EXPORT(rt_assert_handler);
  1214. #endif /* RT_DEBUG */
  1215. #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)
  1216. #include <sys/types.h>
  1217. void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));
  1218. void *memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset")));
  1219. void *memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove")));
  1220. int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp")));
  1221. size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen")));
  1222. char *strstr(const char *s1, const char *s2) __attribute__((weak, alias("rt_strstr")));
  1223. int strcasecmp(const char *a, const char *b) __attribute__((weak, alias("rt_strcasecmp")));
  1224. char *strncpy(char *dest, const char *src, size_t n) __attribute__((weak, alias("rt_strncpy")));
  1225. int strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, alias("rt_strncmp")));
  1226. #ifdef RT_USING_HEAP
  1227. char *strdup(const char *s) __attribute__((weak, alias("rt_strdup")));
  1228. #endif
  1229. int sprintf(char *buf, const char *format, ...) __attribute__((weak, alias("rt_sprintf")));
  1230. int snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias("rt_snprintf")));
  1231. int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf")));
  1232. #endif
  1233. /**@}*/