kservice.c 22 KB

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