kservice.c 31 KB

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