kservice.c 32 KB

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