tc_comm.c 3.7 KB

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