Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

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 <stdio.h>
00018 #include "pal.h"
00019 #include "pal_rtos_test_utils.h"
00020 
00021 #include "pal_BSP.h"
00022 #include "unity_fixture.h"
00023 
00024 #include "pal.h"
00025 
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     if (counter % 2 == 0)
00224     {
00225         pal_osDelay(PAL_TIMER_TEST_TIME_TO_WAIT_MS_LONG);
00226     }
00227     else
00228     {
00229         pal_osDelay(PAL_TIMER_TEST_TIME_TO_WAIT_MS_SHORT);
00230     }
00231 }
00232 
00233 
00234 void palThreadFuncWaitForEverTest(void const *argument)
00235 {
00236     pal_osDelay(PAL_TIME_TO_WAIT_MS/2);
00237     pal_osSemaphoreRelease(*((palSemaphoreID_t*)(argument)));
00238 }
00239 
00240 void palRunThreads()
00241 {
00242     palStatus_t status = PAL_SUCCESS;
00243     palThreadID_t threadID1 = NULLPTR;
00244     palThreadID_t threadID2 = NULLPTR;
00245     palThreadID_t threadID3 = NULLPTR;
00246     palThreadID_t threadID4 = NULLPTR;
00247     palThreadID_t threadID5 = NULLPTR;
00248     palThreadID_t threadID6 = NULLPTR;
00249 
00250     status = pal_osThreadCreateWithAlloc(palThreadFunc1, &g_threadsArg, PAL_osPriorityIdle, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID1);
00251     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00252 
00253     status = pal_osThreadCreateWithAlloc(palThreadFunc2, &g_threadsArg, PAL_osPriorityLow, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID2);
00254     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00255 
00256     status = pal_osThreadCreateWithAlloc(palThreadFunc3, &g_threadsArg, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID3);
00257     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00258 
00259     status = pal_osThreadCreateWithAlloc(palThreadFunc4, &g_threadsArg, PAL_osPriorityBelowNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID4);
00260     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00261 
00262     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)
00263     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00264 
00265     status = pal_osThreadCreateWithAlloc(palThreadFunc5, &g_threadsArg, PAL_osPriorityAboveNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID5);
00266     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00267 
00268     status = pal_osThreadCreateWithAlloc(palThreadFunc6, &g_threadsArg, PAL_osPriorityHigh , PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID6);
00269     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00270 
00271     pal_osDelay(PAL_TIME_TO_WAIT_MS/5);
00272 
00273     status = pal_osThreadTerminate(&threadID1);
00274     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00275 
00276     status = pal_osThreadTerminate(&threadID2);
00277     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00278 
00279     status = pal_osThreadTerminate(&threadID3);
00280     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00281 
00282     status = pal_osThreadTerminate(&threadID4);
00283     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00284 
00285     status = pal_osThreadTerminate(&threadID5);
00286     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00287 
00288     status = pal_osThreadTerminate(&threadID6);
00289     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00290 }
00291 
00292 void RecursiveLockThread(void const *param)
00293 {
00294     size_t i = 0;
00295     palStatus_t status;
00296     palRecursiveMutexParam_t *actualParams = (palRecursiveMutexParam_t*)param;
00297     size_t countbeforeStart = 0;
00298     volatile palThreadID_t threadID = 10;
00299     
00300     status = pal_osSemaphoreWait(actualParams->sem, PAL_RTOS_WAIT_FOREVER, NULL);
00301     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00302 
00303     for (i = 0; i < 100; ++i)
00304     {
00305         status = pal_osMutexWait(actualParams->mtx, PAL_RTOS_WAIT_FOREVER);
00306         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00307         if (i == 0)
00308         {
00309             countbeforeStart = actualParams->count;
00310             TEST_ASSERT_EQUAL_HEX(NULLPTR, actualParams->activeThread);
00311             actualParams->activeThread = pal_osThreadGetId();
00312         }
00313         actualParams->count++;
00314         threadID = pal_osThreadGetId();
00315         TEST_ASSERT_NOT_EQUAL(NULLPTR, threadID);
00316         TEST_ASSERT_EQUAL(actualParams->activeThread, threadID);
00317         pal_osDelay(1);
00318     }
00319 
00320     threadID = 10;
00321     pal_osDelay(50);
00322     TEST_ASSERT_EQUAL(100, actualParams->count - countbeforeStart);
00323     for (i = 0; i < 100; ++i)
00324     {
00325         threadID = pal_osThreadGetId();
00326         TEST_ASSERT_NOT_EQUAL(NULLPTR, threadID);
00327         TEST_ASSERT_EQUAL(actualParams->activeThread, threadID);
00328         actualParams->count++;
00329         if (i == 99)
00330         {
00331             TEST_ASSERT_EQUAL(200, actualParams->count - countbeforeStart);
00332             actualParams->activeThread = NULLPTR;
00333         }
00334 
00335         status = pal_osMutexRelease(actualParams->mtx);
00336         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00337         pal_osDelay(1);
00338     }
00339   
00340     status = pal_osSemaphoreRelease(actualParams->sem);
00341     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00342 }
00343