Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_rtos_test_utils.c Source File

pal_rtos_test_utils.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #include "pal.h"
00018 #include "pal_rtos_test_utils.h"
00019 
00020 #include "test_runners.h"
00021 #include "unity_fixture.h"
00022 
00023 #include <stdio.h>
00024 
00025 #define TRACE_GROUP "PAL"
00026 
00027 extern threadsArgument_t g_threadsArg;
00028 timerArgument_t timerArgs;
00029 
00030 void palThreadFunc1(void const *argument)
00031 {
00032     volatile palThreadID_t threadID;
00033     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00034 #ifdef MUTEX_UNITY_TEST
00035     palStatus_t status = PAL_SUCCESS;
00036     PAL_PRINTF("palThreadFunc1::before mutex\n");
00037     status = pal_osMutexWait(mutex1, 100);
00038     PAL_PRINTF("palThreadFunc1::after mutex: 0x%08x\n", status);
00039     PAL_PRINTF("palThreadFunc1::after mutex (expected): 0x%08x\n", PAL_ERR_RTOS_TIMEOUT );
00040     TEST_ASSERT_EQUAL_HEX(PAL_ERR_RTOS_TIMEOUT , status);
00041     return; // for Mutex scenario, this should end here
00042 #endif //MUTEX_UNITY_TEST
00043 
00044     tmp->arg1 = 10;
00045     threadID = pal_osThreadGetId();
00046     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);    
00047     PAL_PRINTF("palThreadFunc1::Thread ID is %"PRIuPTR " \n", threadID);    
00048 #ifdef MUTEX_UNITY_TEST
00049     status = pal_osMutexRelease(mutex1);
00050     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00051 #endif //MUTEX_UNITY_TEST
00052     PAL_PRINTF("palThreadFunc1::STAAAAM\n");
00053 }
00054 
00055 void palThreadFunc2(void const *argument)
00056 {
00057     volatile palThreadID_t threadID;
00058     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00059 #ifdef MUTEX_UNITY_TEST
00060     palStatus_t status = PAL_SUCCESS;
00061     PAL_PRINTF("palThreadFunc2::before mutex\n");
00062     status = pal_osMutexWait(mutex2, 300);
00063     PAL_PRINTF("palThreadFunc2::after mutex: 0x%08x\n", status);
00064     PAL_PRINTF("palThreadFunc2::after mutex (expected): 0x%08x\n", PAL_SUCCESS);
00065     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00066 #endif //MUTEX_UNITY_TEST
00067 
00068     tmp->arg2 = 20;
00069     threadID = pal_osThreadGetId();
00070     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);    
00071     PAL_PRINTF("palThreadFunc2::Thread ID is %"PRIuPTR "\n", threadID);
00072 #ifdef MUTEX_UNITY_TEST
00073     status = pal_osMutexRelease(mutex2);
00074     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00075 #endif //MUTEX_UNITY_TEST
00076     PAL_PRINTF("palThreadFunc2::STAAAAM\n");
00077 }
00078 
00079 void palThreadFunc3(void const *argument)
00080 {
00081     volatile palThreadID_t threadID;
00082     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00083 
00084 #ifdef SEMAPHORE_UNITY_TEST
00085     palStatus_t status = PAL_SUCCESS;
00086     uint32_t semaphoresAvailable = 10;
00087     status = pal_osSemaphoreWait(semaphore1, 200, &semaphoresAvailable);
00088     
00089     if (PAL_SUCCESS == status)
00090     {
00091         PAL_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
00092         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00093     }
00094     else if(PAL_ERR_RTOS_TIMEOUT  == status)
00095     {
00096         PAL_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
00097         PAL_PRINTF("palThreadFunc3::status: 0x%08x\n", status);
00098         PAL_PRINTF("palThreadFunc3::failed to get Semaphore as expected\n", status);
00099         TEST_ASSERT_EQUAL_HEX(PAL_ERR_RTOS_TIMEOUT , status);
00100         return;
00101     }
00102     pal_osDelay(6000);
00103 #endif //SEMAPHORE_UNITY_TEST
00104     tmp->arg3 = 30;
00105     threadID = pal_osThreadGetId();
00106     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);    
00107     PAL_PRINTF("palThreadFunc3::Thread ID is %"PRIuPTR "\n", threadID);
00108 
00109 #ifdef SEMAPHORE_UNITY_TEST
00110     status = pal_osSemaphoreRelease(semaphore1);
00111     PAL_PRINTF("palThreadFunc3::pal_osSemaphoreRelease res: 0x%08x\n", status);
00112     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00113 #endif //SEMAPHORE_UNITY_TEST
00114     PAL_PRINTF("palThreadFunc3::STAAAAM\n");
00115 }
00116 
00117 void palThreadFunc4(void const *argument)
00118 {
00119     volatile palThreadID_t threadID;
00120     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00121 #ifdef MUTEX_UNITY_TEST
00122     palStatus_t status = PAL_SUCCESS;
00123     PAL_PRINTF("palThreadFunc4::before mutex\n");
00124     status = pal_osMutexWait(mutex1, 200);
00125     PAL_PRINTF("palThreadFunc4::after mutex: 0x%08x\n", status);
00126     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00127     pal_osDelay(3500);  //wait 3.5 seconds to make sure that the next thread arrive to this point
00128 #endif //MUTEX_UNITY_TEST
00129 
00130     tmp->arg4 = 40;
00131     threadID = pal_osThreadGetId();
00132     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);    
00133     PAL_PRINTF("Thread ID is %"PRIuPTR "\n", threadID);
00134 
00135 #ifdef MUTEX_UNITY_TEST
00136     status = pal_osMutexRelease(mutex1);
00137     PAL_PRINTF("palThreadFunc4::after release mutex: 0x%08x\n", status);
00138     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00139 #endif //MUTEX_UNITY_TEST
00140     PAL_PRINTF("palThreadFunc4::STAAAAM\n");
00141 }
00142 
00143 void palThreadFunc5(void const *argument)
00144 {
00145     volatile palThreadID_t threadID;
00146     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00147 #ifdef MUTEX_UNITY_TEST
00148     palStatus_t status = PAL_SUCCESS;
00149     PAL_PRINTF("palThreadFunc5::before mutex\n");
00150     status = pal_osMutexWait(mutex1, 4500);
00151     PAL_PRINTF("palThreadFunc5::after mutex: 0x%08x\n", status);
00152     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00153 #endif //MUTEX_UNITY_TEST
00154     tmp->arg5 = 50;
00155     threadID = pal_osThreadGetId();
00156     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);    
00157     PAL_PRINTF("Thread ID is %"PRIuPTR "\n", threadID);
00158 #ifdef MUTEX_UNITY_TEST
00159     status = pal_osMutexRelease(mutex1);
00160     PAL_PRINTF("palThreadFunc5::after release mutex: 0x%08x\n", status);
00161     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00162 #endif //MUTEX_UNITY_TEST
00163     PAL_PRINTF("palThreadFunc5::STAAAAM\n");
00164 }
00165 
00166 void palThreadFunc6(void const *argument)
00167 {
00168     volatile palThreadID_t threadID;
00169     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00170 #ifdef SEMAPHORE_UNITY_TEST
00171     palStatus_t status = PAL_SUCCESS;
00172     uint32_t semaphoresAvailable = 10;
00173     status = pal_osSemaphoreWait(123456, 200, &semaphoresAvailable);  //MUST fail, since there is no semaphore with ID=3
00174     PAL_PRINTF("palThreadFunc6::semaphoresAvailable: %d\n", semaphoresAvailable);
00175     TEST_ASSERT_EQUAL_HEX(PAL_ERR_RTOS_PARAMETER , status);
00176     return;
00177 #endif //SEMAPHORE_UNITY_TEST
00178     tmp->arg6 = 60;
00179     threadID = pal_osThreadGetId();
00180     TEST_ASSERT_NOT_EQUAL(threadID, NULLPTR);
00181     PAL_PRINTF("Thread ID is %"PRIuPTR "\n", threadID);
00182 #ifdef SEMAPHORE_UNITY_TEST
00183     status = pal_osSemaphoreRelease(123456);
00184     PAL_PRINTF("palThreadFunc6::pal_osSemaphoreRelease res: 0x%08x\n", status);
00185     TEST_ASSERT_EQUAL_HEX(PAL_ERR_RTOS_PARAMETER , status);
00186 #endif //SEMAPHORE_UNITY_TEST
00187     PAL_PRINTF("palThreadFunc6::STAAAAM\n");
00188 }
00189 
00190 
00191 void palTimerFunc1(void const *argument)
00192 {
00193     g_timerArgs.ticksInFunc1 = pal_osKernelSysTick();
00194     PAL_PRINTF("ticks in palTimerFunc1: 0 - %" PRIu32 "\n", g_timerArgs.ticksInFunc1);
00195     PAL_PRINTF("Once Timer function was called\n");
00196 }
00197 
00198 void palTimerFunc2(void const *argument)
00199 {
00200     g_timerArgs.ticksInFunc2 = pal_osKernelSysTick();
00201     PAL_PRINTF("ticks in palTimerFunc2: 0 - %" PRIu32 "\n", g_timerArgs.ticksInFunc2);
00202     PAL_PRINTF("Periodic Timer function was called\n");
00203 }
00204 
00205 void palTimerFunc3(void const *argument)
00206 {
00207     static int counter =0;
00208     counter++;
00209 }
00210 
00211 void palTimerFunc4(void const *argument)
00212 { 
00213     static int counter =0;
00214     counter++;
00215     g_timerArgs.ticksInFunc1 = counter;
00216 }
00217 
00218 void palTimerFunc5(void const *argument) // function to count calls + wait alternatin short and long periods for timer drift test
00219 {
00220     static int counter = 0;
00221     counter++;
00222     g_timerArgs.ticksInFunc1 = counter;
00223 
00224 #ifdef __SXOS__
00225     // The sleep API really should not be used from timer interrupt, as it
00226     // a) will panic on OS side
00227     // b) harm the system as it is documented that callback should not spend more than 10ms in it
00228 #else
00229     if (counter % 2 == 0)
00230     {
00231         pal_osDelay(PAL_TIMER_TEST_TIME_TO_WAIT_MS_LONG);
00232     }
00233     else
00234     {
00235         pal_osDelay(PAL_TIMER_TEST_TIME_TO_WAIT_MS_SHORT);
00236     }
00237 #endif
00238 }
00239 
00240 void palTimerFunc6(void const *argument)
00241 {
00242     palTimerID_t *timerID = (palTimerID_t*)argument;
00243 
00244     pal_osTimerStop(*timerID);
00245     g_timerArgs.ticksInFunc1++;
00246 }
00247 
00248 void palTimerFunc7(void const *argument)
00249 {
00250     palTimerID_t *timerID = (palTimerID_t*)argument;
00251     g_timerArgs.ticksInFunc1++;
00252 
00253     if (g_timerArgs.ticksInFunc1 >= 10) {
00254         pal_osTimerStop(*timerID);
00255         g_timerArgs.ticksInFunc2 = pal_osKernelSysTick();
00256     }
00257 }
00258 
00259 void palThreadFuncWaitForEverTest(void const *argument)
00260 {
00261     pal_osDelay(PAL_TIME_TO_WAIT_MS/2);
00262     pal_osSemaphoreRelease(*((palSemaphoreID_t*)(argument)));
00263 }
00264 
00265 void palRunThreads()
00266 {
00267     palStatus_t status = PAL_SUCCESS;
00268     palThreadID_t threadID1 = NULLPTR;
00269     palThreadID_t threadID2 = NULLPTR;
00270     palThreadID_t threadID3 = NULLPTR;
00271     palThreadID_t threadID4 = NULLPTR;
00272     palThreadID_t threadID5 = NULLPTR;
00273     palThreadID_t threadID6 = NULLPTR;
00274 
00275     status = pal_osThreadCreateWithAlloc(palThreadFunc1, &g_threadsArg, PAL_osPriorityIdle, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID1);
00276     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00277 
00278     status = pal_osThreadCreateWithAlloc(palThreadFunc2, &g_threadsArg, PAL_osPriorityLow, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID2);
00279     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00280 
00281     status = pal_osThreadCreateWithAlloc(palThreadFunc3, &g_threadsArg, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID3);
00282     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00283 
00284     status = pal_osThreadCreateWithAlloc(palThreadFunc4, &g_threadsArg, PAL_osPriorityBelowNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID4);
00285     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00286 
00287     status = pal_osDelay(PAL_RTOS_THREAD_CLEANUP_TIMER_MILISEC * 2); // dealy to work around mbedOS timer issue (starting more than 6 timers at once will cause a hang)
00288     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00289 
00290     status = pal_osThreadCreateWithAlloc(palThreadFunc5, &g_threadsArg, PAL_osPriorityAboveNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID5);
00291     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00292 
00293     status = pal_osThreadCreateWithAlloc(palThreadFunc6, &g_threadsArg, PAL_osPriorityHigh, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID6);
00294     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00295 
00296     pal_osDelay(PAL_TIME_TO_WAIT_MS/5);
00297 
00298     status = pal_osThreadTerminate(&threadID1);
00299     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00300 
00301     status = pal_osThreadTerminate(&threadID2);
00302     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00303 
00304     status = pal_osThreadTerminate(&threadID3);
00305     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00306 
00307     status = pal_osThreadTerminate(&threadID4);
00308     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00309 
00310     status = pal_osThreadTerminate(&threadID5);
00311     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00312 
00313     status = pal_osThreadTerminate(&threadID6);
00314     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00315 }
00316 
00317 void RecursiveLockThread(void const *param)
00318 {
00319     size_t i = 0;
00320     palStatus_t status;
00321     palRecursiveMutexParam_t *actualParams = (palRecursiveMutexParam_t*)param;
00322     size_t countbeforeStart = 0;
00323     volatile palThreadID_t threadID = 10;
00324     
00325     status = pal_osSemaphoreWait(actualParams->sem, PAL_RTOS_WAIT_FOREVER, NULL);
00326     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00327 
00328     for (i = 0; i < 100; ++i)
00329     {
00330         status = pal_osMutexWait(actualParams->mtx, PAL_RTOS_WAIT_FOREVER);
00331         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00332         if (i == 0)
00333         {
00334             countbeforeStart = actualParams->count;
00335             TEST_ASSERT_EQUAL_HEX(NULLPTR, actualParams->activeThread);
00336             actualParams->activeThread = pal_osThreadGetId();
00337         }
00338         actualParams->count++;
00339         threadID = pal_osThreadGetId();
00340         TEST_ASSERT_NOT_EQUAL(NULLPTR, threadID);
00341         TEST_ASSERT_EQUAL(actualParams->activeThread, threadID);
00342         pal_osDelay(1);
00343     }
00344 
00345     threadID = 10;
00346     pal_osDelay(50);
00347     TEST_ASSERT_EQUAL(100, actualParams->count - countbeforeStart);
00348     for (i = 0; i < 100; ++i)
00349     {
00350         threadID = pal_osThreadGetId();
00351         TEST_ASSERT_NOT_EQUAL(NULLPTR, threadID);
00352         TEST_ASSERT_EQUAL(actualParams->activeThread, threadID);
00353         actualParams->count++;
00354         if (i == 99)
00355         {
00356             TEST_ASSERT_EQUAL(200, actualParams->count - countbeforeStart);
00357             actualParams->activeThread = NULLPTR;
00358         }
00359 
00360         status = pal_osMutexRelease(actualParams->mtx);
00361         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00362         pal_osDelay(1);
00363     }
00364   
00365     status = pal_osSemaphoreRelease(actualParams->sem);
00366     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00367 }
00368