tc_comm.c 4.8 KB
Newer Older
B
Bernard Xiong 已提交
1 2 3 4 5 6
#include "tc_comm.h"
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif

#ifdef RT_USING_TC
G
Grissiom 已提交
7 8
#define TC_PRIORITY        25
#define TC_STACK_SIZE    0x400
B
Bernard Xiong 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22

static rt_uint8_t _tc_stat;
static struct rt_semaphore _tc_sem;
static struct rt_thread _tc_thread;
static rt_uint8_t _tc_stack[TC_STACK_SIZE];
static char _tc_prefix[64];
static const char* _tc_current;
static void (*_tc_cleanup)(void) = RT_NULL;

static rt_uint32_t _tc_scale = 1;
FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)

void tc_thread_entry(void* parameter)
{
G
Grissiom 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    unsigned int fail_count = 0;
    struct finsh_syscall* index;

    /* create tc semaphore */
    rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);

    while (_tc_stat & TC_STAT_RUNNING)
    {
        for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
        {
            /* search testcase */
            if (rt_strstr(index->name, _tc_prefix) == index->name)
            {
                long tick;

                _tc_current = index->name + 4;
                rt_kprintf("Run TestCase: %s\n", _tc_current);
                _tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
G
Grissiom 已提交
41 42 43 44 45 46 47
                tick = index->func();
                if (tick > 0)
                {
                    /* Make sure we are going to be blocked. */
                    rt_sem_control(&_tc_sem, RT_IPC_CMD_RESET, 0);
                    rt_sem_take(&_tc_sem, tick * _tc_scale);
                }
B
Bernard Xiong 已提交
48

G
Grissiom 已提交
49 50 51 52 53 54
                if (_tc_cleanup != RT_NULL)
                {
                    /* perform testcase cleanup */
                    _tc_cleanup();
                    _tc_cleanup = RT_NULL;
                }
55

56 57 58 59 60 61 62
                if (_tc_stat & TC_STAT_RUNNING)
                {
                    rt_kprintf("TestCase[%s] exit with stat TC_STAT_RUNNING."
                               " Please fix the TC.\n",
                               _tc_current);
                    /* If the TC forgot to clear the flag, we do it. */
                    _tc_stat &= ~TC_STAT_RUNNING;
G
Grissiom 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                }

                if (_tc_stat & TC_STAT_FAILED)
                {
                    rt_kprintf("TestCase[%s] failed\n", _tc_current);
                    fail_count++;
                }
                else
                {
                    rt_kprintf("TestCase[%s] passed\n", _tc_current);
                }
            }
        }
    }

    rt_kprintf("RT-Thread TestCase Running Done!\n");
    if (fail_count)
    {
        rt_kprintf("%d tests failed\n", fail_count);
    }
    else
    {
        rt_kprintf("All tests passed\n");
    }
    /* detach tc semaphore */
    rt_sem_detach(&_tc_sem);
B
Bernard Xiong 已提交
89 90 91 92
}

void tc_stop()
{
G
Grissiom 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    _tc_stat &= ~TC_STAT_RUNNING;

    rt_thread_delay(RT_TICK_PER_SECOND/2);
    if (_tc_thread.stat != RT_THREAD_INIT)
    {
        /* lock scheduler */
        rt_enter_critical();

        /* detach old tc thread */
        rt_thread_detach(&_tc_thread);
        rt_sem_detach(&_tc_sem);

        /* unlock scheduler */
        rt_exit_critical();
    }
    rt_thread_delay(RT_TICK_PER_SECOND/2);
B
Bernard Xiong 已提交
109 110 111 112 113
}
FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);

void tc_done(rt_uint8_t stat)
{
G
Grissiom 已提交
114 115
    _tc_stat |= stat;
    _tc_stat &= ~TC_STAT_RUNNING;
B
Bernard Xiong 已提交
116

G
Grissiom 已提交
117 118
    /* release semaphore */
    rt_sem_release(&_tc_sem);
B
Bernard Xiong 已提交
119 120 121 122
}

void tc_stat(rt_uint8_t stat)
{
G
Grissiom 已提交
123 124 125 126 127
    if (stat & TC_STAT_FAILED)
    {
        rt_kprintf("TestCases[%s] failed\n", _tc_current);
    }
    _tc_stat |= stat;
B
Bernard Xiong 已提交
128 129 130 131
}

void tc_cleanup(void (*cleanup)())
{
G
Grissiom 已提交
132
    _tc_cleanup = cleanup;
B
Bernard Xiong 已提交
133 134 135 136
}

void tc_start(const char* tc_prefix)
{
G
Grissiom 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    rt_err_t result;

    /* tesecase prefix is null */
    if (tc_prefix == RT_NULL)
    {
        rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
        rt_kprintf("list_tc() can list all testcases.\n");
        return ;
    }

    /* init tc thread */
    if (_tc_stat & TC_STAT_RUNNING)
    {
        /* stop old tc thread */
        tc_stop();
    }

    rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
    rt_snprintf(_tc_prefix, sizeof(_tc_prefix), "_tc_%s", tc_prefix);

    result = rt_thread_init(&_tc_thread, "tc",
        tc_thread_entry, RT_NULL,
        &_tc_stack[0], sizeof(_tc_stack),
        TC_PRIORITY - 3, 5);

    /* set tc stat */
    _tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;

    if (result == RT_EOK)
        rt_thread_startup(&_tc_thread);
B
Bernard Xiong 已提交
167 168 169 170 171
}
FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);

void list_tc()
{
G
Grissiom 已提交
172 173 174 175 176 177 178 179
    struct finsh_syscall* index;

    rt_kprintf("TestCases List:\n");
    for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
    {
        /* search testcase */
        if (rt_strstr(index->name, "_tc_") == index->name)
        {
B
Bernard Xiong 已提交
180
#ifdef FINSH_USING_DESCRIPTION
G
Grissiom 已提交
181
            rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
B
Bernard Xiong 已提交
182
#else
G
Grissiom 已提交
183
            rt_kprintf("%s\n", index->name + 4);
B
Bernard Xiong 已提交
184
#endif
G
Grissiom 已提交
185 186
        }
    }
B
Bernard Xiong 已提交
187 188 189 190
}
FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
#endif