kservice.c 35 KB

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