kservice.c 32 KB

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