#include #include #include #include #include #if defined(TEST_TARGET_oscp) #include #include #include #include #else #include "init.h" #include "command.h" #include "unitt.h" #include "kern.h" #include "oscp.h" #include "kern.h" #endif /************************************************************************************/ /************************************* Unit Test ************************************/ /************************************************************************************/ // #define EXIT_TEST extern uint64_t unitt_clock(void); static int test_0(void) { for (int i = 0; i < 100; i++) { if (0) { #if defined (EXIT_TEST) exit(0); #endif return UNITT_E_FAIL; } } return UNITT_E_OK; } static void unitt_task(void) { static UNITT_TCASE rand_tests[] = { UNITT_TCASE(test_0), // UNITT_TCASE(test_1), // UNITT_TCASE(test_2), }; static UNITT suites[] = { { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock }, }; UNITT_EXE(suites); } /************************************************************************************/ /************************************* Base Test ************************************/ /************************************************************************************/ int level = 0; static unsigned int get_msec(void) { struct timeval mstime; unsigned int ms = 0; gettimeofday(&mstime, NULL); ms = mstime.tv_sec * 1000 + mstime.tv_usec / 1000; return ms; } static void task1(void) { oscp_handle(); } static void task2(void) { static int count = 0; count++; if (count > 628) count = 0; level = RESOLUTION / 2 * sin((double)count / 10) + RESOLUTION / 2; } static void test_base(void) { #if defined(TEST_TARGET_oscp) uint32_t count = 0; oscp_set_scale(O_SCALE_50MS); oscp_set_monitor(&level); while (1) { count++; if (count >= 25200000) count = 0; if (count % 5 == 0) task1(); if (count % 50 == 0) task2(); usleep(1000); } #else printf("create task %d\r\n", task_create(5, task1)); printf("create task %d\r\n", task_create(50, task2)); oscp_set_scale(O_SCALE_50MS); oscp_set_monitor(&level); #endif } /************************************************************************************/ /************************************* Command ************************************/ /************************************************************************************/ static void usage(void) { printf( "Usage: oscp [opt] [arg] ...\n" "\n" "options:\n" " -e Specifies the function to execute, the default is the test\n" " Test base function\n" " Unit test\n" " -h Print help\n" " -v Print version\n" " -u [] Unit test period, unit ms, the default is 1000ms\n" "\n" ); } static int test(int argc, char *argv[]) { char *execute = NULL; int ut_period = 1000; /* reset getopt */ command_opt_init(); while (1) { int opt = command_getopt(argc, argv, "e:hvu::"); if (opt == -1) break; switch (opt) { case 'u' : if (command_optarg) ut_period = atoi(command_optarg); break; case 'e' : execute = command_optarg; break; case 'v' : printf("oscp version %d.%d.%d\r\n", OSCP_V_MAJOR, OSCP_V_MINOR, OSCP_V_PATCH); return 0; case '?': printf("Unknown option `%c`\r\n", command_optopt); return -1; case 'h' : default: usage(); return 0; } } if (execute) { if (!strcmp(execute, "base")) { test_base(); } else if (!strcmp(execute, "ut")) { srand((unsigned int)time(NULL)); #if defined(TEST_TARGET_oscp) while (1) { unitt_task(); usleep(1000 * ut_period); } #else printf("create task %d\r\n", task_create(ut_period, unitt_task)); #endif } } else { test_base(); } return 0; } /************************************************************************************/ /************************************ Test entry ************************************/ /************************************************************************************/ #if defined(TEST_TARGET_oscp) int main(int argc, char *argv[]) { return test(argc, argv); } #else void test_oscp(void) { command_export("oscp", test); } init_export_app(test_oscp); #endif