tc_comm.c 3.5 KB

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