kservice.c 32 KB

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