1
0

kservice.c 33 KB

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