tc_comm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. *
  8. */
  9. #include "tc_comm.h"
  10. #ifdef RT_USING_FINSH
  11. #include <finsh.h>
  12. #endif
  13. #ifdef RT_USING_TC
  14. #define TC_PRIORITY 25
  15. #define TC_STACK_SIZE 0x400
  16. static rt_uint8_t _tc_stat;
  17. static struct rt_semaphore _tc_sem;
  18. static struct rt_thread _tc_thread;
  19. static rt_uint8_t _tc_stack[TC_STACK_SIZE];
  20. static char _tc_prefix[64];
  21. static const char* _tc_current;
  22. static void (*_tc_cleanup)(void) = RT_NULL;
  23. static rt_uint32_t _tc_scale = 1;
  24. FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)
  25. static rt_uint32_t _tc_loop;
  26. void tc_thread_entry(void* parameter)
  27. {
  28. unsigned int fail_count = 0;
  29. struct finsh_syscall* index;
  30. /* create tc semaphore */
  31. rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_PRIO);
  32. do {
  33. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  34. {
  35. /* search testcase */
  36. if (rt_strstr(index->name, _tc_prefix) == index->name)
  37. {
  38. long tick;
  39. _tc_current = index->name + 4;
  40. rt_kprintf("Run TestCase: %s\n", _tc_current);
  41. _tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
  42. tick = index->func();
  43. if (tick > 0)
  44. {
  45. /* Make sure we are going to be blocked. */
  46. rt_sem_control(&_tc_sem, RT_IPC_CMD_RESET, 0);
  47. rt_sem_take(&_tc_sem, tick * _tc_scale);
  48. }
  49. if (_tc_cleanup != RT_NULL)
  50. {
  51. /* perform testcase cleanup */
  52. _tc_cleanup();
  53. _tc_cleanup = RT_NULL;
  54. }
  55. if (_tc_stat & TC_STAT_RUNNING)
  56. {
  57. rt_kprintf("TestCase[%s] exit with stat TC_STAT_RUNNING."
  58. " Please fix the TC.\n",
  59. _tc_current);
  60. /* If the TC forgot to clear the flag, we do it. */
  61. _tc_stat &= ~TC_STAT_RUNNING;
  62. }
  63. if (_tc_stat & TC_STAT_FAILED)
  64. {
  65. rt_kprintf("TestCase[%s] failed\n", _tc_current);
  66. fail_count++;
  67. }
  68. else
  69. {
  70. rt_kprintf("TestCase[%s] passed\n", _tc_current);
  71. }
  72. }
  73. }
  74. } while (_tc_loop);
  75. rt_kprintf("RT-Thread TestCase Running Done!\n");
  76. if (fail_count)
  77. {
  78. rt_kprintf("%d tests failed\n", fail_count);
  79. }
  80. else
  81. {
  82. rt_kprintf("All tests passed\n");
  83. }
  84. /* detach tc semaphore */
  85. rt_sem_detach(&_tc_sem);
  86. }
  87. void tc_stop()
  88. {
  89. _tc_loop = 0;
  90. rt_thread_delay(10 * RT_TICK_PER_SECOND);
  91. if (_tc_thread.stat != RT_THREAD_INIT)
  92. {
  93. /* lock scheduler */
  94. rt_enter_critical();
  95. /* detach old tc thread */
  96. rt_thread_detach(&_tc_thread);
  97. rt_sem_detach(&_tc_sem);
  98. /* unlock scheduler */
  99. rt_exit_critical();
  100. }
  101. rt_thread_delay(RT_TICK_PER_SECOND/2);
  102. }
  103. FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);
  104. void tc_done(rt_uint8_t stat)
  105. {
  106. _tc_stat |= stat;
  107. _tc_stat &= ~TC_STAT_RUNNING;
  108. /* release semaphore */
  109. rt_sem_release(&_tc_sem);
  110. }
  111. void tc_stat(rt_uint8_t stat)
  112. {
  113. if (stat & TC_STAT_FAILED)
  114. {
  115. rt_kprintf("TestCases[%s] failed\n", _tc_current);
  116. }
  117. _tc_stat |= stat;
  118. }
  119. void tc_cleanup(void (*cleanup)())
  120. {
  121. _tc_cleanup = cleanup;
  122. }
  123. void tc_start(const char* tc_prefix)
  124. {
  125. rt_err_t result;
  126. /* tesecase prefix is null */
  127. if (tc_prefix == RT_NULL)
  128. {
  129. rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
  130. rt_kprintf("list_tc() can list all testcases.\n");
  131. return ;
  132. }
  133. /* init tc thread */
  134. if (_tc_stat & TC_STAT_RUNNING)
  135. {
  136. /* stop old tc thread */
  137. tc_stop();
  138. }
  139. rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
  140. rt_snprintf(_tc_prefix, sizeof(_tc_prefix), "_tc_%s", tc_prefix);
  141. result = rt_thread_init(&_tc_thread, "tc",
  142. tc_thread_entry, RT_NULL,
  143. &_tc_stack[0], sizeof(_tc_stack),
  144. TC_PRIORITY - 3, 5);
  145. /* set tc stat */
  146. _tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;
  147. if (result == RT_EOK)
  148. rt_thread_startup(&_tc_thread);
  149. }
  150. FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);
  151. void tc_loop(const char *tc_prefix)
  152. {
  153. _tc_loop = 1;
  154. tc_start(tc_prefix);
  155. }
  156. FINSH_FUNCTION_EXPORT(tc_loop, start testcase with testcase prefix or name in loop mode);
  157. void list_tc()
  158. {
  159. struct finsh_syscall* index;
  160. rt_kprintf("TestCases List:\n");
  161. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  162. {
  163. /* search testcase */
  164. if (rt_strstr(index->name, "_tc_") == index->name)
  165. {
  166. #ifdef FINSH_USING_DESCRIPTION
  167. rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
  168. #else
  169. rt_kprintf("%s\n", index->name + 4);
  170. #endif
  171. }
  172. }
  173. }
  174. FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
  175. #endif