kservice.c 33 KB

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