kservice.c 33 KB

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