tc_comm.c 3.7 KB

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