kservice.c 22 KB

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