kservice.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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 dest 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 n 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 n is the string to be duplicated.
  514. *
  515. * @param base is support divide instructions value.
  516. *
  517. * @return the duplicated string pointer.
  518. */
  519. rt_inline int divide(long long *n, int base)
  520. {
  521. int res;
  522. /* optimized for processor which does not support divide instructions. */
  523. if (base == 10)
  524. {
  525. res = (int)(((unsigned long long)*n) % 10U);
  526. *n = (long long)(((unsigned long long)*n) / 10U);
  527. }
  528. else
  529. {
  530. res = (int)(((unsigned long long)*n) % 16U);
  531. *n = (long long)(((unsigned long long)*n) / 16U);
  532. }
  533. return res;
  534. }
  535. #else
  536. rt_inline int divide(long *n, int base)
  537. {
  538. int res;
  539. /* optimized for processor which does not support divide instructions. */
  540. if (base == 10)
  541. {
  542. res = (int)(((unsigned long)*n) % 10U);
  543. *n = (long)(((unsigned long)*n) / 10U);
  544. }
  545. else
  546. {
  547. res = (int)(((unsigned long)*n) % 16U);
  548. *n = (long)(((unsigned long)*n) / 16U);
  549. }
  550. return res;
  551. }
  552. #endif /* RT_PRINTF_LONGLONG */
  553. rt_inline int skip_atoi(const char **s)
  554. {
  555. register int i = 0;
  556. while (_ISDIGIT(**s))
  557. i = i * 10 + *((*s)++) - '0';
  558. return i;
  559. }
  560. #define ZEROPAD (1 << 0) /* pad with zero */
  561. #define SIGN (1 << 1) /* unsigned/signed long */
  562. #define PLUS (1 << 2) /* show plus */
  563. #define SPACE (1 << 3) /* space if plus */
  564. #define LEFT (1 << 4) /* left justified */
  565. #define SPECIAL (1 << 5) /* 0x */
  566. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  567. #ifdef RT_PRINTF_PRECISION
  568. static char *print_number(char *buf,
  569. char *end,
  570. #ifdef RT_PRINTF_LONGLONG
  571. long long num,
  572. #else
  573. long num,
  574. #endif /* RT_PRINTF_LONGLONG */
  575. int base,
  576. int s,
  577. int precision,
  578. int type)
  579. #else
  580. static char *print_number(char *buf,
  581. char *end,
  582. #ifdef RT_PRINTF_LONGLONG
  583. long long num,
  584. #else
  585. long num,
  586. #endif /* RT_PRINTF_LONGLONG */
  587. int base,
  588. int s,
  589. int type)
  590. #endif /* RT_PRINTF_PRECISION */
  591. {
  592. char c, sign;
  593. #ifdef RT_PRINTF_LONGLONG
  594. char tmp[32];
  595. #else
  596. char tmp[16];
  597. #endif /* RT_PRINTF_LONGLONG */
  598. int precision_bak = precision;
  599. const char *digits;
  600. static const char small_digits[] = "0123456789abcdef";
  601. static const char large_digits[] = "0123456789ABCDEF";
  602. register int i;
  603. register int size;
  604. size = s;
  605. digits = (type & LARGE) ? large_digits : small_digits;
  606. if (type & LEFT)
  607. type &= ~ZEROPAD;
  608. c = (type & ZEROPAD) ? '0' : ' ';
  609. /* get sign */
  610. sign = 0;
  611. if (type & SIGN)
  612. {
  613. if (num < 0)
  614. {
  615. sign = '-';
  616. num = -num;
  617. }
  618. else if (type & PLUS)
  619. sign = '+';
  620. else if (type & SPACE)
  621. sign = ' ';
  622. }
  623. #ifdef RT_PRINTF_SPECIAL
  624. if (type & SPECIAL)
  625. {
  626. if (base == 16)
  627. size -= 2;
  628. else if (base == 8)
  629. size--;
  630. }
  631. #endif /* RT_PRINTF_SPECIAL */
  632. i = 0;
  633. if (num == 0)
  634. tmp[i++] = '0';
  635. else
  636. {
  637. while (num != 0)
  638. tmp[i++] = digits[divide(&num, base)];
  639. }
  640. #ifdef RT_PRINTF_PRECISION
  641. if (i > precision)
  642. precision = i;
  643. size -= precision;
  644. #else
  645. size -= i;
  646. #endif /* RT_PRINTF_PRECISION */
  647. if (!(type & (ZEROPAD | LEFT)))
  648. {
  649. if ((sign) && (size > 0))
  650. size--;
  651. while (size-- > 0)
  652. {
  653. if (buf < end)
  654. *buf = ' ';
  655. ++ buf;
  656. }
  657. }
  658. if (sign)
  659. {
  660. if (buf < end)
  661. {
  662. *buf = sign;
  663. }
  664. -- size;
  665. ++ buf;
  666. }
  667. #ifdef RT_PRINTF_SPECIAL
  668. if (type & SPECIAL)
  669. {
  670. if (base == 8)
  671. {
  672. if (buf < end)
  673. *buf = '0';
  674. ++ buf;
  675. }
  676. else if (base == 16)
  677. {
  678. if (buf < end)
  679. *buf = '0';
  680. ++ buf;
  681. if (buf < end)
  682. {
  683. *buf = type & LARGE ? 'X' : 'x';
  684. }
  685. ++ buf;
  686. }
  687. }
  688. #endif /* RT_PRINTF_SPECIAL */
  689. /* no align to the left */
  690. if (!(type & LEFT))
  691. {
  692. while (size-- > 0)
  693. {
  694. if (buf < end)
  695. *buf = c;
  696. ++ buf;
  697. }
  698. }
  699. #ifdef RT_PRINTF_PRECISION
  700. while (i < precision--)
  701. {
  702. if (buf < end)
  703. *buf = '0';
  704. ++ buf;
  705. }
  706. #endif /* RT_PRINTF_PRECISION */
  707. /* put number in the temporary buffer */
  708. while (i-- > 0 && (precision_bak != 0))
  709. {
  710. if (buf < end)
  711. *buf = tmp[i];
  712. ++ buf;
  713. }
  714. while (size-- > 0)
  715. {
  716. if (buf < end)
  717. *buf = ' ';
  718. ++ buf;
  719. }
  720. return buf;
  721. }
  722. /**
  723. * This function will fill a formatted string to buffer.
  724. *
  725. * @param buf is the buffer to save formatted string.
  726. *
  727. * @param size is the size of buffer.
  728. *
  729. * @param fmt is the format parameters.
  730. *
  731. * @param args is a list of variable parameters.
  732. *
  733. * @return The number of characters actually written to buffer.
  734. */
  735. rt_int32_t rt_vsnprintf(char *buf,
  736. rt_size_t size,
  737. const char *fmt,
  738. va_list args)
  739. {
  740. #ifdef RT_PRINTF_LONGLONG
  741. unsigned long long num;
  742. #else
  743. rt_uint32_t num;
  744. #endif /* RT_PRINTF_LONGLONG */
  745. int i, len;
  746. char *str, *end, c;
  747. const char *s;
  748. rt_uint8_t base; /* the base of number */
  749. rt_uint8_t flags; /* flags to print number */
  750. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  751. rt_int32_t field_width; /* width of output field */
  752. #ifdef RT_PRINTF_PRECISION
  753. int precision; /* min. # of digits for integers and max for a string */
  754. #endif /* RT_PRINTF_PRECISION */
  755. str = buf;
  756. end = buf + size;
  757. /* Make sure end is always >= buf */
  758. if (end < buf)
  759. {
  760. end = ((char *) - 1);
  761. size = end - buf;
  762. }
  763. for (; *fmt ; ++fmt)
  764. {
  765. if (*fmt != '%')
  766. {
  767. if (str < end)
  768. *str = *fmt;
  769. ++ str;
  770. continue;
  771. }
  772. /* process flags */
  773. flags = 0;
  774. while (1)
  775. {
  776. /* skips the first '%' also */
  777. ++ fmt;
  778. if (*fmt == '-') flags |= LEFT;
  779. else if (*fmt == '+') flags |= PLUS;
  780. else if (*fmt == ' ') flags |= SPACE;
  781. else if (*fmt == '#') flags |= SPECIAL;
  782. else if (*fmt == '0') flags |= ZEROPAD;
  783. else break;
  784. }
  785. /* get field width */
  786. field_width = -1;
  787. if (_ISDIGIT(*fmt)) field_width = skip_atoi(&fmt);
  788. else if (*fmt == '*')
  789. {
  790. ++ fmt;
  791. /* it's the next argument */
  792. field_width = va_arg(args, int);
  793. if (field_width < 0)
  794. {
  795. field_width = -field_width;
  796. flags |= LEFT;
  797. }
  798. }
  799. #ifdef RT_PRINTF_PRECISION
  800. /* get the precision */
  801. precision = -1;
  802. if (*fmt == '.')
  803. {
  804. ++ fmt;
  805. if (_ISDIGIT(*fmt)) precision = skip_atoi(&fmt);
  806. else if (*fmt == '*')
  807. {
  808. ++ fmt;
  809. /* it's the next argument */
  810. precision = va_arg(args, int);
  811. }
  812. if (precision < 0) precision = 0;
  813. }
  814. #endif /* RT_PRINTF_PRECISION */
  815. /* get the conversion qualifier */
  816. qualifier = 0;
  817. #ifdef RT_PRINTF_LONGLONG
  818. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  819. #else
  820. if (*fmt == 'h' || *fmt == 'l')
  821. #endif /* RT_PRINTF_LONGLONG */
  822. {
  823. qualifier = *fmt;
  824. ++ fmt;
  825. #ifdef RT_PRINTF_LONGLONG
  826. if (qualifier == 'l' && *fmt == 'l')
  827. {
  828. qualifier = 'L';
  829. ++ fmt;
  830. }
  831. #endif /* RT_PRINTF_LONGLONG */
  832. }
  833. /* the default base */
  834. base = 10;
  835. switch (*fmt)
  836. {
  837. case 'c':
  838. if (!(flags & LEFT))
  839. {
  840. while (--field_width > 0)
  841. {
  842. if (str < end) *str = ' ';
  843. ++ str;
  844. }
  845. }
  846. /* get character */
  847. c = (rt_uint8_t)va_arg(args, int);
  848. if (str < end) *str = c;
  849. ++ str;
  850. /* put width */
  851. while (--field_width > 0)
  852. {
  853. if (str < end) *str = ' ';
  854. ++ str;
  855. }
  856. continue;
  857. case 's':
  858. s = va_arg(args, char *);
  859. if (!s) s = "(NULL)";
  860. for (len = 0; (len != field_width) && (s[len] != '\0'); len++);
  861. #ifdef RT_PRINTF_PRECISION
  862. if (precision > 0 && len > precision) len = precision;
  863. #endif /* RT_PRINTF_PRECISION */
  864. if (!(flags & LEFT))
  865. {
  866. while (len < field_width--)
  867. {
  868. if (str < end) *str = ' ';
  869. ++ str;
  870. }
  871. }
  872. for (i = 0; i < len; ++i)
  873. {
  874. if (str < end) *str = *s;
  875. ++ str;
  876. ++ s;
  877. }
  878. while (len < field_width--)
  879. {
  880. if (str < end) *str = ' ';
  881. ++ str;
  882. }
  883. continue;
  884. case 'p':
  885. if (field_width == -1)
  886. {
  887. field_width = sizeof(void *) << 1;
  888. flags |= ZEROPAD;
  889. }
  890. #ifdef RT_PRINTF_PRECISION
  891. str = print_number(str, end,
  892. (long)va_arg(args, void *),
  893. 16, field_width, precision, flags);
  894. #else
  895. str = print_number(str, end,
  896. (long)va_arg(args, void *),
  897. 16, field_width, flags);
  898. #endif /* RT_PRINTF_PRECISION */
  899. continue;
  900. case '%':
  901. if (str < end) *str = '%';
  902. ++ str;
  903. continue;
  904. /* integer number formats - set up the flags and "break" */
  905. case 'o':
  906. base = 8;
  907. break;
  908. case 'X':
  909. flags |= LARGE;
  910. case 'x':
  911. base = 16;
  912. break;
  913. case 'd':
  914. case 'i':
  915. flags |= SIGN;
  916. case 'u':
  917. break;
  918. default:
  919. if (str < end) *str = '%';
  920. ++ str;
  921. if (*fmt)
  922. {
  923. if (str < end) *str = *fmt;
  924. ++ str;
  925. }
  926. else
  927. {
  928. -- fmt;
  929. }
  930. continue;
  931. }
  932. #ifdef RT_PRINTF_LONGLONG
  933. if (qualifier == 'L') num = va_arg(args, long long);
  934. else if (qualifier == 'l')
  935. #else
  936. if (qualifier == 'l')
  937. #endif /* RT_PRINTF_LONGLONG */
  938. {
  939. num = va_arg(args, rt_uint32_t);
  940. if (flags & SIGN) num = (rt_int32_t)num;
  941. }
  942. else if (qualifier == 'h')
  943. {
  944. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  945. if (flags & SIGN) num = (rt_int16_t)num;
  946. }
  947. else
  948. {
  949. num = va_arg(args, rt_uint32_t);
  950. if (flags & SIGN) num = (rt_int32_t)num;
  951. }
  952. #ifdef RT_PRINTF_PRECISION
  953. str = print_number(str, end, num, base, field_width, precision, flags);
  954. #else
  955. str = print_number(str, end, num, base, field_width, flags);
  956. #endif /* RT_PRINTF_PRECISION */
  957. }
  958. if (size > 0)
  959. {
  960. if (str < end) *str = '\0';
  961. else
  962. {
  963. end[-1] = '\0';
  964. }
  965. }
  966. /* the trailing null byte doesn't count towards the total
  967. * ++str;
  968. */
  969. return str - buf;
  970. }
  971. RTM_EXPORT(rt_vsnprintf);
  972. /**
  973. * This function will fill a formatted string to buffer.
  974. *
  975. * @param buf is the buffer to save formatted string.
  976. *
  977. * @param size is the size of buffer.
  978. *
  979. * @param fmt is the format parameters.
  980. *
  981. * @return The number of characters actually written to buffer.
  982. */
  983. rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  984. {
  985. rt_int32_t n;
  986. va_list args;
  987. va_start(args, fmt);
  988. n = rt_vsnprintf(buf, size, fmt, args);
  989. va_end(args);
  990. return n;
  991. }
  992. RTM_EXPORT(rt_snprintf);
  993. /**
  994. * This function will fill a formatted string to buffer.
  995. *
  996. * @param buf is the buffer to save formatted string.
  997. *
  998. * @param format is the format parameters.
  999. *
  1000. * @param arg_ptr is a list of variable parameters.
  1001. *
  1002. * @return The number of characters actually written to buffer.
  1003. */
  1004. rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  1005. {
  1006. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  1007. }
  1008. RTM_EXPORT(rt_vsprintf);
  1009. /**
  1010. * This function will fill a formatted string to buffer
  1011. *
  1012. * @param buf the buffer to save formatted string.
  1013. *
  1014. * @param format is the format parameters.
  1015. *
  1016. * @return The number of characters actually written to buffer.
  1017. */
  1018. rt_int32_t rt_sprintf(char *buf, const char *format, ...)
  1019. {
  1020. rt_int32_t n;
  1021. va_list arg_ptr;
  1022. va_start(arg_ptr, format);
  1023. n = rt_vsprintf(buf, format, arg_ptr);
  1024. va_end(arg_ptr);
  1025. return n;
  1026. }
  1027. RTM_EXPORT(rt_sprintf);
  1028. #ifdef RT_USING_CONSOLE
  1029. #ifdef RT_USING_DEVICE
  1030. /**
  1031. * This function returns the device using in console.
  1032. *
  1033. * @return Returns the console device pointer or RT_NULL.
  1034. */
  1035. rt_device_t rt_console_get_device(void)
  1036. {
  1037. return _console_device;
  1038. }
  1039. RTM_EXPORT(rt_console_get_device);
  1040. /**
  1041. * This function will set a device as console device.
  1042. * After set a device to console, all output of rt_kprintf will be
  1043. * redirected to this new device.
  1044. *
  1045. * @param name is the name of new console device.
  1046. *
  1047. * @return the old console device handler on successful, or RT_NULL on failure.
  1048. */
  1049. rt_device_t rt_console_set_device(const char *name)
  1050. {
  1051. rt_device_t new_device, old_device;
  1052. /* save old device */
  1053. old_device = _console_device;
  1054. /* find new console device */
  1055. new_device = rt_device_find(name);
  1056. /* check whether it's a same device */
  1057. if (new_device == old_device) return RT_NULL;
  1058. if (new_device != RT_NULL)
  1059. {
  1060. if (_console_device != RT_NULL)
  1061. {
  1062. /* close old console device */
  1063. rt_device_close(_console_device);
  1064. }
  1065. /* set new console device */
  1066. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1067. _console_device = new_device;
  1068. }
  1069. return old_device;
  1070. }
  1071. RTM_EXPORT(rt_console_set_device);
  1072. #endif /* RT_USING_DEVICE */
  1073. RT_WEAK void rt_hw_console_output(const char *str)
  1074. {
  1075. /* empty console output */
  1076. }
  1077. RTM_EXPORT(rt_hw_console_output);
  1078. /**
  1079. * This function will put string to the console.
  1080. *
  1081. * @param str is the string output to the console.
  1082. */
  1083. void rt_kputs(const char *str)
  1084. {
  1085. if (!str) return;
  1086. #ifdef RT_USING_DEVICE
  1087. if (_console_device == RT_NULL)
  1088. {
  1089. rt_hw_console_output(str);
  1090. }
  1091. else
  1092. {
  1093. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1094. }
  1095. #else
  1096. rt_hw_console_output(str);
  1097. #endif /* RT_USING_DEVICE */
  1098. }
  1099. /**
  1100. * This function will print a formatted string on system console.
  1101. *
  1102. * @param fmt is the format parameters.
  1103. */
  1104. RT_WEAK void rt_kprintf(const char *fmt, ...)
  1105. {
  1106. va_list args;
  1107. rt_size_t length;
  1108. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1109. va_start(args, fmt);
  1110. /* the return value of vsnprintf is the number of bytes that would be
  1111. * written to buffer had if the size of the buffer been sufficiently
  1112. * large excluding the terminating null byte. If the output string
  1113. * would be larger than the rt_log_buf, we have to adjust the output
  1114. * length. */
  1115. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1116. if (length > RT_CONSOLEBUF_SIZE - 1)
  1117. length = RT_CONSOLEBUF_SIZE - 1;
  1118. #ifdef RT_USING_DEVICE
  1119. if (_console_device == RT_NULL)
  1120. {
  1121. rt_hw_console_output(rt_log_buf);
  1122. }
  1123. else
  1124. {
  1125. rt_device_write(_console_device, 0, rt_log_buf, length);
  1126. }
  1127. #else
  1128. rt_hw_console_output(rt_log_buf);
  1129. #endif /* RT_USING_DEVICE */
  1130. va_end(args);
  1131. }
  1132. RTM_EXPORT(rt_kprintf);
  1133. #endif /* RT_USING_CONSOLE */
  1134. #ifdef RT_USING_HEAP
  1135. /**
  1136. * This function allocates a memory block, which address is aligned to the
  1137. * specified alignment size.
  1138. *
  1139. * @param size is the allocated memory block size.
  1140. *
  1141. * @param align is the alignment size.
  1142. *
  1143. * @return The memory block address was returned successfully, otherwise it was
  1144. * returned empty RT_NULL.
  1145. */
  1146. RT_WEAK void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1147. {
  1148. void *ptr;
  1149. void *align_ptr;
  1150. int uintptr_size;
  1151. rt_size_t align_size;
  1152. /* sizeof pointer */
  1153. uintptr_size = sizeof(void*);
  1154. uintptr_size -= 1;
  1155. /* align the alignment size to uintptr size byte */
  1156. align = ((align + uintptr_size) & ~uintptr_size);
  1157. /* get total aligned size */
  1158. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1159. /* allocate memory block from heap */
  1160. ptr = rt_malloc(align_size);
  1161. if (ptr != RT_NULL)
  1162. {
  1163. /* the allocated memory block is aligned */
  1164. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1165. {
  1166. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1167. }
  1168. else
  1169. {
  1170. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1171. }
  1172. /* set the pointer before alignment pointer to the real pointer */
  1173. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1174. ptr = align_ptr;
  1175. }
  1176. return ptr;
  1177. }
  1178. RTM_EXPORT(rt_malloc_align);
  1179. /**
  1180. * This function release the memory block, which is allocated by
  1181. * rt_malloc_align function and address is aligned.
  1182. *
  1183. * @param ptr is the memory block pointer.
  1184. */
  1185. RT_WEAK void rt_free_align(void *ptr)
  1186. {
  1187. void *real_ptr;
  1188. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1189. rt_free(real_ptr);
  1190. }
  1191. RTM_EXPORT(rt_free_align);
  1192. #endif /* RT_USING_HEAP */
  1193. #ifndef RT_USING_CPU_FFS
  1194. const rt_uint8_t __lowest_bit_bitmap[] =
  1195. {
  1196. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1197. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1198. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1199. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1200. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1201. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1202. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1203. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1204. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1205. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1206. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1207. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1208. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1209. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1210. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1211. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1212. };
  1213. /**
  1214. * This function finds the first bit set (beginning with the least significant bit)
  1215. * in value and return the index of that bit.
  1216. *
  1217. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1218. * zero from any of these functions means that the argument was zero.
  1219. *
  1220. * @return Return the index of the first bit set. If value is 0, then this function
  1221. * shall return 0.
  1222. */
  1223. int __rt_ffs(int value)
  1224. {
  1225. if (value == 0) return 0;
  1226. if (value & 0xff)
  1227. return __lowest_bit_bitmap[value & 0xff] + 1;
  1228. if (value & 0xff00)
  1229. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1230. if (value & 0xff0000)
  1231. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1232. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1233. }
  1234. #endif /* RT_USING_CPU_FFS */
  1235. #ifdef RT_DEBUG
  1236. /* RT_ASSERT(EX)'s hook */
  1237. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1238. /**
  1239. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1240. *
  1241. * @param hook is the hook function.
  1242. */
  1243. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1244. {
  1245. rt_assert_hook = hook;
  1246. }
  1247. /**
  1248. * The RT_ASSERT function.
  1249. *
  1250. * @param ex_string is the assertion condition string.
  1251. *
  1252. * @param func is the function name when assertion.
  1253. *
  1254. * @param line is the file line number when assertion.
  1255. */
  1256. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1257. {
  1258. volatile char dummy = 0;
  1259. if (rt_assert_hook == RT_NULL)
  1260. {
  1261. #ifdef RT_USING_MODULE
  1262. if (dlmodule_self())
  1263. {
  1264. /* close assertion module */
  1265. dlmodule_exit(-1);
  1266. }
  1267. else
  1268. #endif /*RT_USING_MODULE*/
  1269. {
  1270. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1271. while (dummy == 0);
  1272. }
  1273. }
  1274. else
  1275. {
  1276. rt_assert_hook(ex_string, func, line);
  1277. }
  1278. }
  1279. RTM_EXPORT(rt_assert_handler);
  1280. #endif /* RT_DEBUG */
  1281. /**@}*/