kservice.c 35 KB

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