kservice.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. cs++, ct++;
  396. return (*cs - *ct);
  397. }
  398. RTM_EXPORT(rt_strcmp);
  399. /**
  400. * The strnlen() function returns the number of characters in the
  401. * string pointed to by s, excluding the terminating null byte ('\0'),
  402. * but at most maxlen. In doing this, strnlen() looks only at the
  403. * first maxlen characters in the string pointed to by s and never
  404. * beyond s+maxlen.
  405. *
  406. * @param s the string
  407. * @param maxlen the max size
  408. * @return the length of string
  409. */
  410. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  411. {
  412. const char *sc;
  413. for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
  414. ;
  415. return sc - s;
  416. }
  417. RTM_EXPORT(rt_strnlen);
  418. /**
  419. * This function will return the length of a string, which terminate will
  420. * null character.
  421. *
  422. * @param s the string
  423. *
  424. * @return the length of string
  425. */
  426. rt_size_t rt_strlen(const char *s)
  427. {
  428. const char *sc;
  429. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  430. ;
  431. return sc - s;
  432. }
  433. RTM_EXPORT(rt_strlen);
  434. #ifdef RT_USING_HEAP
  435. /**
  436. * This function will duplicate a string.
  437. *
  438. * @param s the string to be duplicated
  439. *
  440. * @return the duplicated string pointer
  441. */
  442. char *rt_strdup(const char *s)
  443. {
  444. rt_size_t len = rt_strlen(s) + 1;
  445. char *tmp = (char *)rt_malloc(len);
  446. if (!tmp)
  447. return RT_NULL;
  448. rt_memcpy(tmp, s, len);
  449. return tmp;
  450. }
  451. RTM_EXPORT(rt_strdup);
  452. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  453. char *strdup(const char *s) __attribute__((alias("rt_strdup")));
  454. #endif
  455. #endif
  456. /**
  457. * This function will show the version of rt-thread rtos
  458. */
  459. void rt_show_version(void)
  460. {
  461. rt_kprintf("\n \\ | /\n");
  462. rt_kprintf("- RT - Thread Operating System\n");
  463. rt_kprintf(" / | \\ %d.%d.%d build %s\n",
  464. RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__);
  465. rt_kprintf(" 2006 - 2020 Copyright by rt-thread team\n");
  466. }
  467. RTM_EXPORT(rt_show_version);
  468. /* private function */
  469. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  470. #ifdef RT_PRINTF_LONGLONG
  471. rt_inline int divide(long long *n, int base)
  472. {
  473. int res;
  474. /* optimized for processor which does not support divide instructions. */
  475. if (base == 10)
  476. {
  477. res = (int)(((unsigned long long)*n) % 10U);
  478. *n = (long long)(((unsigned long long)*n) / 10U);
  479. }
  480. else
  481. {
  482. res = (int)(((unsigned long long)*n) % 16U);
  483. *n = (long long)(((unsigned long long)*n) / 16U);
  484. }
  485. return res;
  486. }
  487. #else
  488. rt_inline int divide(long *n, int base)
  489. {
  490. int res;
  491. /* optimized for processor which does not support divide instructions. */
  492. if (base == 10)
  493. {
  494. res = (int)(((unsigned long)*n) % 10U);
  495. *n = (long)(((unsigned long)*n) / 10U);
  496. }
  497. else
  498. {
  499. res = (int)(((unsigned long)*n) % 16U);
  500. *n = (long)(((unsigned long)*n) / 16U);
  501. }
  502. return res;
  503. }
  504. #endif
  505. rt_inline int skip_atoi(const char **s)
  506. {
  507. register int i = 0;
  508. while (isdigit(**s))
  509. i = i * 10 + *((*s)++) - '0';
  510. return i;
  511. }
  512. #define ZEROPAD (1 << 0) /* pad with zero */
  513. #define SIGN (1 << 1) /* unsigned/signed long */
  514. #define PLUS (1 << 2) /* show plus */
  515. #define SPACE (1 << 3) /* space if plus */
  516. #define LEFT (1 << 4) /* left justified */
  517. #define SPECIAL (1 << 5) /* 0x */
  518. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  519. #ifdef RT_PRINTF_PRECISION
  520. static char *print_number(char *buf,
  521. char *end,
  522. #ifdef RT_PRINTF_LONGLONG
  523. long long num,
  524. #else
  525. long num,
  526. #endif
  527. int base,
  528. int s,
  529. int precision,
  530. int type)
  531. #else
  532. static char *print_number(char *buf,
  533. char *end,
  534. #ifdef RT_PRINTF_LONGLONG
  535. long long num,
  536. #else
  537. long num,
  538. #endif
  539. int base,
  540. int s,
  541. int type)
  542. #endif
  543. {
  544. char c, sign;
  545. #ifdef RT_PRINTF_LONGLONG
  546. char tmp[32];
  547. #else
  548. char tmp[16];
  549. #endif
  550. int precision_bak = precision;
  551. const char *digits;
  552. static const char small_digits[] = "0123456789abcdef";
  553. static const char large_digits[] = "0123456789ABCDEF";
  554. register int i;
  555. register int size;
  556. size = s;
  557. digits = (type & LARGE) ? large_digits : small_digits;
  558. if (type & LEFT)
  559. type &= ~ZEROPAD;
  560. c = (type & ZEROPAD) ? '0' : ' ';
  561. /* get sign */
  562. sign = 0;
  563. if (type & SIGN)
  564. {
  565. if (num < 0)
  566. {
  567. sign = '-';
  568. num = -num;
  569. }
  570. else if (type & PLUS)
  571. sign = '+';
  572. else if (type & SPACE)
  573. sign = ' ';
  574. }
  575. #ifdef RT_PRINTF_SPECIAL
  576. if (type & SPECIAL)
  577. {
  578. if (base == 16)
  579. size -= 2;
  580. else if (base == 8)
  581. size--;
  582. }
  583. #endif
  584. i = 0;
  585. if (num == 0)
  586. tmp[i++] = '0';
  587. else
  588. {
  589. while (num != 0)
  590. tmp[i++] = digits[divide(&num, base)];
  591. }
  592. #ifdef RT_PRINTF_PRECISION
  593. if (i > precision)
  594. precision = i;
  595. size -= precision;
  596. #else
  597. size -= i;
  598. #endif
  599. if (!(type & (ZEROPAD | LEFT)))
  600. {
  601. if ((sign) && (size > 0))
  602. size--;
  603. while (size-- > 0)
  604. {
  605. if (buf < end)
  606. *buf = ' ';
  607. ++ buf;
  608. }
  609. }
  610. if (sign)
  611. {
  612. if (buf < end)
  613. {
  614. *buf = sign;
  615. }
  616. -- size;
  617. ++ buf;
  618. }
  619. #ifdef RT_PRINTF_SPECIAL
  620. if (type & SPECIAL)
  621. {
  622. if (base == 8)
  623. {
  624. if (buf < end)
  625. *buf = '0';
  626. ++ buf;
  627. }
  628. else if (base == 16)
  629. {
  630. if (buf < end)
  631. *buf = '0';
  632. ++ buf;
  633. if (buf < end)
  634. {
  635. *buf = type & LARGE ? 'X' : 'x';
  636. }
  637. ++ buf;
  638. }
  639. }
  640. #endif
  641. /* no align to the left */
  642. if (!(type & LEFT))
  643. {
  644. while (size-- > 0)
  645. {
  646. if (buf < end)
  647. *buf = c;
  648. ++ buf;
  649. }
  650. }
  651. #ifdef RT_PRINTF_PRECISION
  652. while (i < precision--)
  653. {
  654. if (buf < end)
  655. *buf = '0';
  656. ++ buf;
  657. }
  658. #endif
  659. /* put number in the temporary buffer */
  660. while (i-- > 0 && (precision_bak != 0))
  661. {
  662. if (buf < end)
  663. *buf = tmp[i];
  664. ++ buf;
  665. }
  666. while (size-- > 0)
  667. {
  668. if (buf < end)
  669. *buf = ' ';
  670. ++ buf;
  671. }
  672. return buf;
  673. }
  674. rt_int32_t rt_vsnprintf(char *buf,
  675. rt_size_t size,
  676. const char *fmt,
  677. va_list args)
  678. {
  679. #ifdef RT_PRINTF_LONGLONG
  680. unsigned long long num;
  681. #else
  682. rt_uint32_t num;
  683. #endif
  684. int i, len;
  685. char *str, *end, c;
  686. const char *s;
  687. rt_uint8_t base; /* the base of number */
  688. rt_uint8_t flags; /* flags to print number */
  689. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  690. rt_int32_t field_width; /* width of output field */
  691. #ifdef RT_PRINTF_PRECISION
  692. int precision; /* min. # of digits for integers and max for a string */
  693. #endif
  694. str = buf;
  695. end = buf + size;
  696. /* Make sure end is always >= buf */
  697. if (end < buf)
  698. {
  699. end = ((char *) - 1);
  700. size = end - buf;
  701. }
  702. for (; *fmt ; ++fmt)
  703. {
  704. if (*fmt != '%')
  705. {
  706. if (str < end)
  707. *str = *fmt;
  708. ++ str;
  709. continue;
  710. }
  711. /* process flags */
  712. flags = 0;
  713. while (1)
  714. {
  715. /* skips the first '%' also */
  716. ++ fmt;
  717. if (*fmt == '-') flags |= LEFT;
  718. else if (*fmt == '+') flags |= PLUS;
  719. else if (*fmt == ' ') flags |= SPACE;
  720. else if (*fmt == '#') flags |= SPECIAL;
  721. else if (*fmt == '0') flags |= ZEROPAD;
  722. else break;
  723. }
  724. /* get field width */
  725. field_width = -1;
  726. if (isdigit(*fmt)) field_width = skip_atoi(&fmt);
  727. else if (*fmt == '*')
  728. {
  729. ++ fmt;
  730. /* it's the next argument */
  731. field_width = va_arg(args, int);
  732. if (field_width < 0)
  733. {
  734. field_width = -field_width;
  735. flags |= LEFT;
  736. }
  737. }
  738. #ifdef RT_PRINTF_PRECISION
  739. /* get the precision */
  740. precision = -1;
  741. if (*fmt == '.')
  742. {
  743. ++ fmt;
  744. if (isdigit(*fmt)) precision = skip_atoi(&fmt);
  745. else if (*fmt == '*')
  746. {
  747. ++ fmt;
  748. /* it's the next argument */
  749. precision = va_arg(args, int);
  750. }
  751. if (precision < 0) precision = 0;
  752. }
  753. #endif
  754. /* get the conversion qualifier */
  755. qualifier = 0;
  756. #ifdef RT_PRINTF_LONGLONG
  757. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  758. #else
  759. if (*fmt == 'h' || *fmt == 'l')
  760. #endif
  761. {
  762. qualifier = *fmt;
  763. ++ fmt;
  764. #ifdef RT_PRINTF_LONGLONG
  765. if (qualifier == 'l' && *fmt == 'l')
  766. {
  767. qualifier = 'L';
  768. ++ fmt;
  769. }
  770. #endif
  771. }
  772. /* the default base */
  773. base = 10;
  774. switch (*fmt)
  775. {
  776. case 'c':
  777. if (!(flags & LEFT))
  778. {
  779. while (--field_width > 0)
  780. {
  781. if (str < end) *str = ' ';
  782. ++ str;
  783. }
  784. }
  785. /* get character */
  786. c = (rt_uint8_t)va_arg(args, int);
  787. if (str < end) *str = c;
  788. ++ str;
  789. /* put width */
  790. while (--field_width > 0)
  791. {
  792. if (str < end) *str = ' ';
  793. ++ str;
  794. }
  795. continue;
  796. case 's':
  797. s = va_arg(args, char *);
  798. if (!s) s = "(NULL)";
  799. len = rt_strlen(s);
  800. #ifdef RT_PRINTF_PRECISION
  801. if (precision > 0 && len > precision) len = precision;
  802. #endif
  803. if (!(flags & LEFT))
  804. {
  805. while (len < field_width--)
  806. {
  807. if (str < end) *str = ' ';
  808. ++ str;
  809. }
  810. }
  811. for (i = 0; i < len; ++i)
  812. {
  813. if (str < end) *str = *s;
  814. ++ str;
  815. ++ s;
  816. }
  817. while (len < field_width--)
  818. {
  819. if (str < end) *str = ' ';
  820. ++ str;
  821. }
  822. continue;
  823. case 'p':
  824. if (field_width == -1)
  825. {
  826. field_width = sizeof(void *) << 1;
  827. flags |= ZEROPAD;
  828. }
  829. #ifdef RT_PRINTF_PRECISION
  830. str = print_number(str, end,
  831. (long)va_arg(args, void *),
  832. 16, field_width, precision, flags);
  833. #else
  834. str = print_number(str, end,
  835. (long)va_arg(args, void *),
  836. 16, field_width, flags);
  837. #endif
  838. continue;
  839. case '%':
  840. if (str < end) *str = '%';
  841. ++ str;
  842. continue;
  843. /* integer number formats - set up the flags and "break" */
  844. case 'o':
  845. base = 8;
  846. break;
  847. case 'X':
  848. flags |= LARGE;
  849. case 'x':
  850. base = 16;
  851. break;
  852. case 'd':
  853. case 'i':
  854. flags |= SIGN;
  855. case 'u':
  856. break;
  857. default:
  858. if (str < end) *str = '%';
  859. ++ str;
  860. if (*fmt)
  861. {
  862. if (str < end) *str = *fmt;
  863. ++ str;
  864. }
  865. else
  866. {
  867. -- fmt;
  868. }
  869. continue;
  870. }
  871. #ifdef RT_PRINTF_LONGLONG
  872. if (qualifier == 'L') num = va_arg(args, long long);
  873. else if (qualifier == 'l')
  874. #else
  875. if (qualifier == 'l')
  876. #endif
  877. {
  878. num = va_arg(args, rt_uint32_t);
  879. if (flags & SIGN) num = (rt_int32_t)num;
  880. }
  881. else if (qualifier == 'h')
  882. {
  883. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  884. if (flags & SIGN) num = (rt_int16_t)num;
  885. }
  886. else
  887. {
  888. num = va_arg(args, rt_uint32_t);
  889. if (flags & SIGN) num = (rt_int32_t)num;
  890. }
  891. #ifdef RT_PRINTF_PRECISION
  892. str = print_number(str, end, num, base, field_width, precision, flags);
  893. #else
  894. str = print_number(str, end, num, base, field_width, flags);
  895. #endif
  896. }
  897. if (size > 0)
  898. {
  899. if (str < end) *str = '\0';
  900. else
  901. {
  902. end[-1] = '\0';
  903. }
  904. }
  905. /* the trailing null byte doesn't count towards the total
  906. * ++str;
  907. */
  908. return str - buf;
  909. }
  910. RTM_EXPORT(rt_vsnprintf);
  911. /**
  912. * This function will fill a formatted string to buffer
  913. *
  914. * @param buf the buffer to save formatted string
  915. * @param size the size of buffer
  916. * @param fmt the format
  917. */
  918. rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  919. {
  920. rt_int32_t n;
  921. va_list args;
  922. va_start(args, fmt);
  923. n = rt_vsnprintf(buf, size, fmt, args);
  924. va_end(args);
  925. return n;
  926. }
  927. RTM_EXPORT(rt_snprintf);
  928. /**
  929. * This function will fill a formatted string to buffer
  930. *
  931. * @param buf the buffer to save formatted string
  932. * @param arg_ptr the arg_ptr
  933. * @param format the format
  934. */
  935. rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  936. {
  937. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  938. }
  939. RTM_EXPORT(rt_vsprintf);
  940. /**
  941. * This function will fill a formatted string to buffer
  942. *
  943. * @param buf the buffer to save formatted string
  944. * @param format the format
  945. */
  946. rt_int32_t rt_sprintf(char *buf, const char *format, ...)
  947. {
  948. rt_int32_t n;
  949. va_list arg_ptr;
  950. va_start(arg_ptr, format);
  951. n = rt_vsprintf(buf, format, arg_ptr);
  952. va_end(arg_ptr);
  953. return n;
  954. }
  955. RTM_EXPORT(rt_sprintf);
  956. #ifdef RT_USING_CONSOLE
  957. #ifdef RT_USING_DEVICE
  958. /**
  959. * This function returns the device using in console.
  960. *
  961. * @return the device using in console or RT_NULL
  962. */
  963. rt_device_t rt_console_get_device(void)
  964. {
  965. return _console_device;
  966. }
  967. RTM_EXPORT(rt_console_get_device);
  968. /**
  969. * This function will set a device as console device.
  970. * After set a device to console, all output of rt_kprintf will be
  971. * redirected to this new device.
  972. *
  973. * @param name the name of new console device
  974. *
  975. * @return the old console device handler
  976. */
  977. rt_device_t rt_console_set_device(const char *name)
  978. {
  979. rt_device_t new_device, old_device;
  980. /* save old device */
  981. old_device = _console_device;
  982. /* find new console device */
  983. new_device = rt_device_find(name);
  984. if (new_device != RT_NULL)
  985. {
  986. if (_console_device != RT_NULL)
  987. {
  988. /* close old console device */
  989. rt_device_close(_console_device);
  990. }
  991. /* set new console device */
  992. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  993. _console_device = new_device;
  994. }
  995. return old_device;
  996. }
  997. RTM_EXPORT(rt_console_set_device);
  998. #endif
  999. RT_WEAK void rt_hw_console_output(const char *str)
  1000. {
  1001. /* empty console output */
  1002. }
  1003. RTM_EXPORT(rt_hw_console_output);
  1004. /**
  1005. * This function will put string to the console.
  1006. *
  1007. * @param str the string output to the console.
  1008. */
  1009. void rt_kputs(const char *str)
  1010. {
  1011. if (!str) return;
  1012. #ifdef RT_USING_DEVICE
  1013. if (_console_device == RT_NULL)
  1014. {
  1015. rt_hw_console_output(str);
  1016. }
  1017. else
  1018. {
  1019. rt_uint16_t old_flag = _console_device->open_flag;
  1020. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  1021. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1022. _console_device->open_flag = old_flag;
  1023. }
  1024. #else
  1025. rt_hw_console_output(str);
  1026. #endif
  1027. }
  1028. /**
  1029. * This function will print a formatted string on system console
  1030. *
  1031. * @param fmt the format
  1032. */
  1033. void rt_kprintf(const char *fmt, ...)
  1034. {
  1035. va_list args;
  1036. rt_size_t length;
  1037. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1038. va_start(args, fmt);
  1039. /* the return value of vsnprintf is the number of bytes that would be
  1040. * written to buffer had if the size of the buffer been sufficiently
  1041. * large excluding the terminating null byte. If the output string
  1042. * would be larger than the rt_log_buf, we have to adjust the output
  1043. * length. */
  1044. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1045. if (length > RT_CONSOLEBUF_SIZE - 1)
  1046. length = RT_CONSOLEBUF_SIZE - 1;
  1047. #ifdef RT_USING_DEVICE
  1048. if (_console_device == RT_NULL)
  1049. {
  1050. rt_hw_console_output(rt_log_buf);
  1051. }
  1052. else
  1053. {
  1054. rt_uint16_t old_flag = _console_device->open_flag;
  1055. _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;
  1056. rt_device_write(_console_device, 0, rt_log_buf, length);
  1057. _console_device->open_flag = old_flag;
  1058. }
  1059. #else
  1060. rt_hw_console_output(rt_log_buf);
  1061. #endif
  1062. va_end(args);
  1063. }
  1064. RTM_EXPORT(rt_kprintf);
  1065. #endif
  1066. #ifdef RT_USING_HEAP
  1067. /**
  1068. * This function allocates a memory block, which address is aligned to the
  1069. * specified alignment size.
  1070. *
  1071. * @param size the allocated memory block size
  1072. * @param align the alignment size
  1073. *
  1074. * @return the allocated memory block on successful, otherwise returns RT_NULL
  1075. */
  1076. void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1077. {
  1078. void *ptr;
  1079. void *align_ptr;
  1080. int uintptr_size;
  1081. rt_size_t align_size;
  1082. /* sizeof pointer */
  1083. uintptr_size = sizeof(void*);
  1084. uintptr_size -= 1;
  1085. /* align the alignment size to uintptr size byte */
  1086. align = ((align + uintptr_size) & ~uintptr_size);
  1087. /* get total aligned size */
  1088. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1089. /* allocate memory block from heap */
  1090. ptr = rt_malloc(align_size);
  1091. if (ptr != RT_NULL)
  1092. {
  1093. /* the allocated memory block is aligned */
  1094. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1095. {
  1096. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1097. }
  1098. else
  1099. {
  1100. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1101. }
  1102. /* set the pointer before alignment pointer to the real pointer */
  1103. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1104. ptr = align_ptr;
  1105. }
  1106. return ptr;
  1107. }
  1108. RTM_EXPORT(rt_malloc_align);
  1109. /**
  1110. * This function release the memory block, which is allocated by
  1111. * rt_malloc_align function and address is aligned.
  1112. *
  1113. * @param ptr the memory block pointer
  1114. */
  1115. void rt_free_align(void *ptr)
  1116. {
  1117. void *real_ptr;
  1118. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1119. rt_free(real_ptr);
  1120. }
  1121. RTM_EXPORT(rt_free_align);
  1122. #endif
  1123. #ifndef RT_USING_CPU_FFS
  1124. const rt_uint8_t __lowest_bit_bitmap[] =
  1125. {
  1126. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1127. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1128. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1129. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1130. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1131. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1132. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1133. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1134. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1135. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1136. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1137. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1138. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1139. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1140. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1141. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1142. };
  1143. /**
  1144. * This function finds the first bit set (beginning with the least significant bit)
  1145. * in value and return the index of that bit.
  1146. *
  1147. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1148. * zero from any of these functions means that the argument was zero.
  1149. *
  1150. * @return return the index of the first bit set. If value is 0, then this function
  1151. * shall return 0.
  1152. */
  1153. int __rt_ffs(int value)
  1154. {
  1155. if (value == 0) return 0;
  1156. if (value & 0xff)
  1157. return __lowest_bit_bitmap[value & 0xff] + 1;
  1158. if (value & 0xff00)
  1159. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1160. if (value & 0xff0000)
  1161. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1162. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1163. }
  1164. #endif
  1165. #ifdef RT_DEBUG
  1166. /* RT_ASSERT(EX)'s hook */
  1167. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1168. /**
  1169. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1170. *
  1171. * @param hook the hook function
  1172. */
  1173. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1174. {
  1175. rt_assert_hook = hook;
  1176. }
  1177. /**
  1178. * The RT_ASSERT function.
  1179. *
  1180. * @param ex the assertion condition string
  1181. * @param func the function name when assertion.
  1182. * @param line the file line number when assertion.
  1183. */
  1184. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1185. {
  1186. volatile char dummy = 0;
  1187. if (rt_assert_hook == RT_NULL)
  1188. {
  1189. #ifdef RT_USING_MODULE
  1190. if (dlmodule_self())
  1191. {
  1192. /* close assertion module */
  1193. dlmodule_exit(-1);
  1194. }
  1195. else
  1196. #endif
  1197. {
  1198. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1199. while (dummy == 0);
  1200. }
  1201. }
  1202. else
  1203. {
  1204. rt_assert_hook(ex_string, func, line);
  1205. }
  1206. }
  1207. RTM_EXPORT(rt_assert_handler);
  1208. #endif /* RT_DEBUG */
  1209. #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)
  1210. #include <sys/types.h>
  1211. void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));
  1212. void *memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset")));
  1213. void *memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove")));
  1214. int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp")));
  1215. size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen")));
  1216. char *strstr(const char *s1, const char *s2) __attribute__((weak, alias("rt_strstr")));
  1217. int strcasecmp(const char *a, const char *b) __attribute__((weak, alias("rt_strcasecmp")));
  1218. char *strncpy(char *dest, const char *src, size_t n) __attribute__((weak, alias("rt_strncpy")));
  1219. int strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, alias("rt_strncmp")));
  1220. #ifdef RT_USING_HEAP
  1221. char *strdup(const char *s) __attribute__((weak, alias("rt_strdup")));
  1222. #endif
  1223. int sprintf(char *buf, const char *format, ...) __attribute__((weak, alias("rt_sprintf")));
  1224. int snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias("rt_snprintf")));
  1225. int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf")));
  1226. #endif
  1227. /**@}*/