Ram Gandikota / Mbed OS ABCD
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 (c) 2016 ARM Limited. All rights reserved.
00003 * SPDX-License-Identifier: Apache-2.0
00004 * Licensed under the Apache License, Version 2.0 (the License); you may
00005 * 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, WITHOUT
00012 * 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_rtos_test_utils.h"
00018 #include "pal_rtos.h"
00019 #include "unity_fixture.h"
00020 
00021 #include "pal.h"
00022 
00023 threadsArgument_t threadsArg;
00024 timerArgument_t timerArgs;
00025 
00026 void palThreadFunc1(void const *argument)
00027 {
00028     palThreadID_t threadID = 10;
00029     uint32_t* threadStorage = NULL;
00030     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00031 #ifdef MUTEX_UNITY_TEST
00032     palStatus_t status = PAL_SUCCESS;
00033     TEST_PRINTF("palThreadFunc1::before mutex\n");
00034     status = pal_osMutexWait(mutex1, 100);
00035     TEST_PRINTF("palThreadFunc1::after mutex: 0x%08x\n", status);
00036     TEST_PRINTF("palThreadFunc1::after mutex (expected): 0x%08x\n", PAL_ERR_RTOS_TIMEOUT);
00037     TEST_ASSERT_EQUAL(PAL_ERR_RTOS_TIMEOUT, status);
00038     return; // for Mutex scenario, this should end here
00039 #endif //MUTEX_UNITY_TEST
00040 
00041     tmp->arg1 = 10;
00042 
00043     threadID = pal_osThreadGetId();
00044     TEST_PRINTF("palThreadFunc1::Thread ID is %d\n", threadID);
00045 
00046     threadStorage = pal_osThreadGetLocalStore();
00047     if (threadStorage == g_threadStorage)
00048     {
00049         TEST_PRINTF("Thread storage updated as expected\n");    
00050     }
00051     TEST_ASSERT_EQUAL(threadStorage, g_threadStorage);
00052 #ifdef MUTEX_UNITY_TEST
00053     status = pal_osMutexRelease(mutex1);
00054     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00055 #endif //MUTEX_UNITY_TEST
00056     TEST_PRINTF("palThreadFunc1::STAAAAM\n");
00057 
00058 }
00059 
00060 void palThreadFunc2(void const *argument)
00061 {
00062 
00063     palThreadID_t threadID = 10;
00064     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00065 #ifdef MUTEX_UNITY_TEST
00066     palStatus_t status = PAL_SUCCESS;
00067     TEST_PRINTF("palThreadFunc2::before mutex\n");
00068     status = pal_osMutexWait(mutex2, 300);
00069     TEST_PRINTF("palThreadFunc2::after mutex: 0x%08x\n", status);
00070     TEST_PRINTF("palThreadFunc2::after mutex (expected): 0x%08x\n", PAL_SUCCESS);
00071     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00072 #endif //MUTEX_UNITY_TEST
00073 
00074     tmp->arg2 = 20;
00075 
00076     threadID = pal_osThreadGetId();
00077     TEST_PRINTF("palThreadFunc2::Thread ID is %d\n", threadID);
00078 #ifdef MUTEX_UNITY_TEST
00079     status = pal_osMutexRelease(mutex2);
00080     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00081 #endif //MUTEX_UNITY_TEST
00082     TEST_PRINTF("palThreadFunc2::STAAAAM\n");
00083 }
00084 
00085 void palThreadFunc3(void const *argument)
00086 {
00087 
00088     palThreadID_t threadID = 10;
00089     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00090 
00091 #ifdef SEMAPHORE_UNITY_TEST
00092     palStatus_t status = PAL_SUCCESS;
00093     uint32_t semaphoresAvailable = 10;
00094     status = pal_osSemaphoreWait(semaphore1, 200, &semaphoresAvailable);
00095     
00096     if (PAL_SUCCESS == status)
00097     {
00098         TEST_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
00099         TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00100     }
00101     else if(PAL_ERR_RTOS_TIMEOUT == status)
00102     {
00103         TEST_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
00104         TEST_PRINTF("palThreadFunc3::status: 0x%08x\n", status);
00105         TEST_PRINTF("palThreadFunc3::failed to get Semaphore as expected\n", status);
00106         TEST_ASSERT_EQUAL(PAL_ERR_RTOS_TIMEOUT, status);
00107         return;
00108     }
00109     pal_osDelay(6000);
00110 #endif //SEMAPHORE_UNITY_TEST
00111     tmp->arg3 = 30;
00112     threadID = pal_osThreadGetId();
00113     TEST_PRINTF("palThreadFunc3::Thread ID is %d\n", threadID);
00114 
00115 #ifdef SEMAPHORE_UNITY_TEST
00116     status = pal_osSemaphoreRelease(semaphore1);
00117     TEST_PRINTF("palThreadFunc3::pal_osSemaphoreRelease res: 0x%08x\n", status);
00118     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00119 #endif //SEMAPHORE_UNITY_TEST
00120     TEST_PRINTF("palThreadFunc3::STAAAAM\n");
00121 }
00122 
00123 void palThreadFunc4(void const *argument)
00124 {
00125     palThreadID_t threadID = 10;
00126     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00127 #ifdef MUTEX_UNITY_TEST
00128     palStatus_t status = PAL_SUCCESS;
00129     TEST_PRINTF("palThreadFunc4::before mutex\n");
00130     status = pal_osMutexWait(mutex1, 200);
00131     TEST_PRINTF("palThreadFunc4::after mutex: 0x%08x\n", status);
00132     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00133     pal_osDelay(3500);  //wait 3.5 seconds to make sure that the next thread arrive to this point
00134 #endif //MUTEX_UNITY_TEST
00135 
00136 
00137     tmp->arg4 = 40;
00138 
00139     threadID = pal_osThreadGetId();
00140     TEST_PRINTF("Thread ID is %d\n", threadID);
00141 
00142 
00143 
00144 #ifdef MUTEX_UNITY_TEST
00145     status = pal_osMutexRelease(mutex1);
00146     TEST_PRINTF("palThreadFunc4::after release mutex: 0x%08x\n", status);
00147     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00148 #endif //MUTEX_UNITY_TEST
00149     TEST_PRINTF("palThreadFunc4::STAAAAM\n");
00150 }
00151 
00152 void palThreadFunc5(void const *argument)
00153 {
00154     palThreadID_t threadID = 10;
00155     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00156 #ifdef MUTEX_UNITY_TEST
00157     palStatus_t status = PAL_SUCCESS;
00158     TEST_PRINTF("palThreadFunc5::before mutex\n");
00159     status = pal_osMutexWait(mutex1, 4500);
00160     TEST_PRINTF("palThreadFunc5::after mutex: 0x%08x\n", status);
00161     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00162 #endif //MUTEX_UNITY_TEST
00163     tmp->arg5 = 50;
00164 
00165     threadID = pal_osThreadGetId();
00166     TEST_PRINTF("Thread ID is %d\n", threadID);
00167 #ifdef MUTEX_UNITY_TEST
00168     status = pal_osMutexRelease(mutex1);
00169     TEST_PRINTF("palThreadFunc5::after release mutex: 0x%08x\n", status);
00170     TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00171 #endif //MUTEX_UNITY_TEST
00172     TEST_PRINTF("palThreadFunc5::STAAAAM\n");
00173 }
00174 
00175 void palThreadFunc6(void const *argument)
00176 {
00177     palThreadID_t threadID = 10;
00178     threadsArgument_t *tmp = (threadsArgument_t*)argument;
00179 #ifdef SEMAPHORE_UNITY_TEST
00180     palStatus_t status = PAL_SUCCESS;
00181     uint32_t semaphoresAvailable = 10;
00182     status = pal_osSemaphoreWait(123456, 200, &semaphoresAvailable);  //MUST fail, since there is no semaphore with ID=3
00183     TEST_PRINTF("palThreadFunc6::semaphoresAvailable: %d\n", semaphoresAvailable);
00184     TEST_ASSERT_EQUAL(PAL_ERR_RTOS_PARAMETER, status);
00185     return;
00186 #endif //SEMAPHORE_UNITY_TEST
00187     tmp->arg6 = 60;
00188 
00189     threadID = pal_osThreadGetId();
00190     TEST_PRINTF("Thread ID is %d\n", threadID);
00191 #ifdef SEMAPHORE_UNITY_TEST
00192     status = pal_osSemaphoreRelease(123456);
00193     TEST_PRINTF("palThreadFunc6::pal_osSemaphoreRelease res: 0x%08x\n", status);
00194     TEST_ASSERT_EQUAL(PAL_ERR_RTOS_PARAMETER, status);
00195 #endif //SEMAPHORE_UNITY_TEST
00196     TEST_PRINTF("palThreadFunc6::STAAAAM\n");
00197 }
00198 
00199 
00200 void palTimerFunc1(void const *argument)
00201 {
00202     g_timerArgs.ticksInFunc1 = pal_osKernelSysTick();
00203     TEST_PRINTF("ticks in palTimerFunc1: 0 - %d\n", g_timerArgs.ticksInFunc1);
00204     TEST_PRINTF("Once Timer function was called\n");
00205 }
00206 
00207 void palTimerFunc2(void const *argument)
00208 {
00209     g_timerArgs.ticksInFunc2 = pal_osKernelSysTick();
00210     TEST_PRINTF("ticks in palTimerFunc2: 0 - %d\n", g_timerArgs.ticksInFunc2);
00211     TEST_PRINTF("Periodic Timer function was called\n");    
00212 }
00213 
00214 void palThreadFuncCustom1(void const *argument)
00215 {
00216     TEST_PRINTF("palThreadFuncCustom1 was called\n");
00217 }
00218 
00219 void palThreadFuncCustom2(void const *argument)
00220 {
00221     TEST_PRINTF("palThreadFuncCustom2 was called\n");
00222 }
00223 
00224 void palThreadFuncCustom3(void const *argument)
00225 {
00226     TEST_PRINTF("palThreadFuncCustom3 was called\n");
00227 }
00228 
00229 void palThreadFuncCustom4(void const *argument)
00230 {
00231     TEST_PRINTF("palThreadFuncCustom4 was called\n");
00232 }
00233 
00234 void palRunThreads()
00235 {
00236   palStatus_t status = PAL_SUCCESS;
00237   palThreadID_t threadID1 = NULLPTR;
00238   palThreadID_t threadID2 = NULLPTR;
00239   palThreadID_t threadID3 = NULLPTR;
00240   palThreadID_t threadID4 = NULLPTR;
00241   palThreadID_t threadID5 = NULLPTR;
00242   palThreadID_t threadID6 = NULLPTR;
00243 
00244   uint32_t *stack1 = malloc(THREAD_STACK_SIZE);
00245   uint32_t *stack2 = malloc(THREAD_STACK_SIZE);
00246   uint32_t *stack3 = malloc(THREAD_STACK_SIZE);
00247   uint32_t *stack4 = malloc(THREAD_STACK_SIZE);
00248   uint32_t *stack5 = malloc(THREAD_STACK_SIZE);
00249   uint32_t *stack6 = malloc(THREAD_STACK_SIZE);
00250 
00251   status = pal_init(NULL);
00252   TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
00253 
00254   status = pal_osThreadCreate(palThreadFunc1, &g_threadsArg, PAL_osPriorityIdle, THREAD_STACK_SIZE, stack1, (palThreadLocalStore_t *)g_threadStorage, &threadID1);
00255   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00256 
00257   status = pal_osThreadCreate(palThreadFunc2, &g_threadsArg, PAL_osPriorityLow, THREAD_STACK_SIZE, stack2, NULL, &threadID2);
00258   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00259 
00260   status = pal_osThreadCreate(palThreadFunc3, &g_threadsArg, PAL_osPriorityNormal, THREAD_STACK_SIZE, stack3, NULL, &threadID3);
00261   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00262 
00263   status = pal_osThreadCreate(palThreadFunc4, &g_threadsArg, PAL_osPriorityBelowNormal, THREAD_STACK_SIZE, stack4, NULL, &threadID4);
00264   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00265 
00266   status = pal_osThreadCreate(palThreadFunc5, &g_threadsArg, PAL_osPriorityAboveNormal, THREAD_STACK_SIZE, stack5, NULL, &threadID5);
00267   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00268 
00269   status = pal_osThreadCreate(palThreadFunc6, &g_threadsArg, PAL_osPriorityHigh, THREAD_STACK_SIZE, stack6, NULL, &threadID6);
00270   TEST_ASSERT_EQUAL(PAL_SUCCESS, status); 
00271 
00272   
00273   free(stack1);
00274   free(stack2);
00275   free(stack3);
00276   free(stack4);
00277   free(stack5);
00278   free(stack6);
00279 }