tc_comm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "tc_comm.h"
  2. #ifdef RT_USING_FINSH
  3. #include <finsh.h>
  4. #endif
  5. #ifdef RT_USING_TC
  6. #define TC_PRIORITY 25
  7. #define TC_STACK_SIZE 0x400
  8. static rt_uint8_t _tc_stat;
  9. static struct rt_semaphore _tc_sem;
  10. static struct rt_thread _tc_thread;
  11. static rt_uint8_t _tc_stack[TC_STACK_SIZE];
  12. static char _tc_prefix[64];
  13. static const char* _tc_current;
  14. static void (*_tc_cleanup)(void) = RT_NULL;
  15. static rt_uint32_t _tc_scale = 1;
  16. FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)
  17. void tc_thread_entry(void* parameter)
  18. {
  19. struct finsh_syscall* index;
  20. /* create tc semaphore */
  21. rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);
  22. while (_tc_stat & TC_STAT_RUNNING)
  23. {
  24. for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
  25. {
  26. /* search testcase */
  27. if (rt_strstr(index->name, _tc_prefix) == index->name)
  28. {
  29. long tick;
  30. _tc_current = index->name + 4;
  31. rt_kprintf("Run TestCase: %s\n", _tc_current);
  32. _tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
  33. tick = index->func();
  34. if (tick > 0)
  35. {
  36. rt_sem_take(&_tc_sem, tick * _tc_scale);
  37. if (_tc_cleanup != RT_NULL)
  38. {
  39. /* perform testcase cleanup */
  40. _tc_cleanup();
  41. _tc_cleanup = RT_NULL;
  42. }
  43. if (_tc_stat & TC_STAT_FAILED)
  44. rt_kprintf("TestCase[%s] failed\n", _tc_current);
  45. else
  46. rt_kprintf("TestCase[%s] passed\n", _tc_current);
  47. }
  48. else
  49. {
  50. if (_tc_cleanup != RT_NULL)
  51. {
  52. /* perform testcase cleanup */
  53. _tc_cleanup();
  54. _tc_cleanup = RT_NULL;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. rt_kprintf("RT-Thread TestCase Running Done!\n");
  61. /* detach tc semaphore */
  62. rt_sem_detach(&_tc_sem);
  63. }
  64. void tc_stop()
  65. {
  66. _tc_stat &= ~TC_STAT_RUNNING;
  67. rt_thread_delay(RT_TICK_PER_SECOND/2);
  68. if (_tc_thread.stat != RT_THREAD_INIT)
  69. {
  70. /* lock scheduler */
  71. rt_enter_critical();
  72. /* detach old tc thread */
  73. rt_thread_detach(&_tc_thread);
  74. rt_sem_detach(&_tc_sem);
  75. /* unlock scheduler */
  76. rt_exit_critical();
  77. }
  78. rt_thread_delay(RT_TICK_PER_SECOND/2);
  79. }
  80. FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);
  81. void tc_done(rt_uint8_t stat)
  82. {
  83. _tc_stat |= stat;
  84. _tc_stat &= ~TC_STAT_RUNNING;
  85. /* release semaphore */
  86. rt_sem_release(&_tc_sem);
  87. }
  88. void tc_stat(rt_uint8_t stat)
  89. {
  90. if (stat & TC_STAT_FAILED)
  91. {
  92. rt_kprintf("TestCases[%s] failed\n", _tc_current);
  93. }
  94. _tc_stat |= stat;
  95. }
  96. void tc_cleanup(void (*cleanup)())
  97. {
  98. _tc_cleanup = cleanup;
  99. }
  100. void tc_start(const char* tc_prefix)
  101. {
  102. rt_err_t result;
  103. /* tesecase prefix is null */
  104. if (tc_prefix == RT_NULL)
  105. {
  106. rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
  107. rt_kprintf("list_tc() can list all testcases.\n");
  108. return ;
  109. }
  110. /* init tc thread */
  111. if (_tc_stat & TC_STAT_RUNNING)
  112. {
  113. /* stop old tc thread */
  114. tc_stop();
  115. }
  116. rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
  117. rt_snprintf(_tc_prefix, sizeof(_tc_prefix), "_tc_%s", tc_prefix);
  118. result = rt_thread_init(&_tc_thread, "tc",
  119. tc_thread_entry, RT_NULL,
  120. &_tc_stack[0], sizeof(_tc_stack),
  121. TC_PRIORITY - 3, 5);
  122. /* set tc stat */
  123. _tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;
  124. if (result == RT_EOK)
  125. rt_thread_startup(&_tc_thread);
  126. }
  127. FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);
  128. void list_tc()
  129. {
  130. struct finsh_syscall* index;
  131. rt_kprintf("TestCases List:\n");
  132. for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
  133. {
  134. /* search testcase */
  135. if (rt_strstr(index->name, "_tc_") == index->name)
  136. {
  137. #ifdef FINSH_USING_DESCRIPTION
  138. rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
  139. #else
  140. rt_kprintf("%s\n", index->name + 4);
  141. #endif
  142. }
  143. }
  144. }
  145. FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
  146. #endif