void * fonction_thread () { cpt=100; while (cpt--) { le_sem_Wait(GSemPtr); fprintf(stdout, "\n%d : thread '%s' has %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_1,le_sem_GetValue(GSemPtr)); CU_PASS("thread GSemPtr get"); le_sem_Wait(GSem2Ptr); fprintf(stdout, "\n%d : thread '%s' has %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_2,le_sem_GetValue(GSem2Ptr)); CU_PASS("thread GSem2Ptr get"); usleep(10000); le_sem_Post(GSem2Ptr); fprintf(stdout, "\n%d : thread '%s' release %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_2,le_sem_GetValue(GSem2Ptr)); CU_PASS("thread GSemPtr2 UnLocked"); le_sem_Post(GSemPtr); fprintf(stdout, "\n%d : thread '%s' release %s %d\n", cpt,le_thread_GetMyName(),SEM_NAME_1,le_sem_GetValue(GSemPtr)); CU_PASS("thread GSemPtr UnLocked"); } return NULL; }
//-------------------------------------------------------------------------------------------------- static void StartStressECall ( void ) { LE_INFO("Start StartStressECall"); LastECallState = LE_ECALL_STATE_DISCONNECTED; TestCount = 0; ThreadSemaphore = le_sem_Create("ThreadSem",0); LE_ASSERT(le_ecall_AddStateChangeHandler(MyECallEventHandler, NULL) != NULL); LE_ASSERT(le_ecall_SetPsapNumber(PsapNumber) == LE_OK); LE_ASSERT(le_ecall_SetMsdTxMode(LE_ECALL_TX_MODE_PUSH) == LE_OK); LE_ASSERT((MytECallRef=le_ecall_Create()) != NULL); LE_ASSERT(le_ecall_SetMsdPosition(MytECallRef, true, +48898064, +2218092, 0) == LE_OK); LE_ASSERT(le_ecall_SetMsdPassengersCount(MytECallRef, 3) == LE_OK); le_thread_Start(le_thread_Create("ECallLoopThread", ECallLoopThread, NULL)); // Start Test le_sem_Post(ThreadSemaphore); }
// A thread "main" function which creates a series of sems. static void* ThreadCreateSem ( void* context ) { char semNameBuffer[LIMIT_MAX_SEMAPHORE_NAME_BYTES] = {0}; LE_INFO("Thread [%s] has started. Waiting on a semaphore.", le_thread_GetMyName()); le_mutex_Lock(SemIndexMutexRef); snprintf(semNameBuffer, LIMIT_MAX_SEMAPHORE_NAME_BYTES, "[%s]Sem%ld", le_thread_GetMyName(), SemCreateIdx); le_sem_Ref_t sem = le_sem_Create(semNameBuffer, 0); SemRefArray[SemCreateIdx] = (SemRef_t){le_thread_GetCurrent(), sem}; SemCreateIdx++; le_mutex_Unlock(SemIndexMutexRef); LE_INFO("In thread [%s], about to wait sem", le_thread_GetMyName()); // notify the calling thread that this thread is about to wait on its sema. le_sem_Post(SemaRef); le_sem_Wait(sem); LE_INFO("In thread [%s], sema is posted", le_thread_GetMyName()); le_event_RunLoop(); return NULL; }
//-------------------------------------------------------------------------------------------------- static void HandlerFunc ( le_mdc_ProfileRef_t profileRef, le_mdc_ConState_t ConnectionStatus, void* contextPtr ) { ConnectionStateReceivedByHandler = ( ConnectionStatus == LE_MDC_CONNECTED ) ? true : false; ProfileRefReceivedByHandler = (le_mdc_ProfileRef_t) profileRef; if (ConnectionStatus == LE_MDC_DISCONNECTED) { switch (le_mdc_GetPDP(profileRef)) { case LE_MDC_PDP_IPV4V6: CheckDisconnectionReason(profileRef, LE_MDC_PDP_IPV4, true); CheckDisconnectionReason(profileRef, LE_MDC_PDP_IPV6, true); break; case LE_MDC_PDP_IPV4: CheckDisconnectionReason(profileRef, LE_MDC_PDP_IPV4, false); break; case LE_MDC_PDP_IPV6: CheckDisconnectionReason(profileRef, LE_MDC_PDP_IPV6, false); break; case LE_MDC_PDP_UNKNOWN: exit(EXIT_FAILURE); break; } } le_sem_Post(ThreadSemaphore); }
static void PostSemInThread ( int threadIdx ) { if (threadIdx >= ThreadNum) { LE_ERROR("thread index out of range."); return; } int i; for (i = 0; i < ThreadNum; i++) { if (SemRefArray[i].threadRef == ThreadRefArray[threadIdx]) { char threadNameBuffer[MAX_THREAD_NAME_SIZE]; le_thread_GetName(SemRefArray[i].threadRef, threadNameBuffer, MAX_THREAD_NAME_SIZE); LE_INFO("About to post the semaphore being waited in thread [%s]", threadNameBuffer); le_sem_Post(SemRefArray[i].semRef); break; } } if (i == ThreadNum) { LE_ERROR("Failed to post semaphore for the %dth thread.", threadIdx); } }
void testGetValue(void) { le_sem_Ref_t semPtr=NULL; semPtr = le_sem_Create( "SEMAPHORE-1", 10); CU_ASSERT_PTR_NOT_EQUAL(semPtr, NULL); le_sem_Post(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),11); le_sem_Post(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),12); le_sem_Post(semPtr); CU_ASSERT_EQUAL(le_sem_GetValue(semPtr),13); le_sem_Delete(semPtr); CU_PASS("Destruct semaphore\n"); }
//-------------------------------------------------------------------------------------------------- static void RemoveHandler ( void* param1Ptr, void* param2Ptr ) { le_mdc_RemoveSessionStateHandler(param1Ptr); le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void TestSimStateHandler ( le_sim_Id_t simId, ///< [IN] SIM identifier le_sim_States_t simState, ///< [IN] SIM state void* contextPtr ///< [IN] Context ) { switch (simState) { case LE_SIM_INSERTED: LE_INFO("-TEST- New state LE_SIM_INSERTED for SIM card.%d", simId); break; case LE_SIM_ABSENT: LE_INFO("-TEST- New state LE_SIM_ABSENT for SIM card.%d", simId); break; case LE_SIM_READY: LE_INFO("-TEST- New state LE_SIM_READY for SIM card.%d", simId); if (SimPowerCycleStarted) { SimPowerCycleStarted = false; le_sem_Post(SimPowerCycleSemaphore); } break; case LE_SIM_BLOCKED: LE_INFO("-TEST- New state LE_SIM_BLOCKED for SIM card.%d", simId); break; case LE_SIM_BUSY: LE_INFO("-TEST- New state LE_SIM_BUSY for SIM card.%d", simId); break; case LE_SIM_POWER_DOWN: LE_INFO("-TEST- New state LE_SIM_POWER_DOWN for SIM card.%d", simId); if (SimPowerCycleStarted) { le_sem_Post(SimPowerCycleSemaphore); } break; case LE_SIM_STATE_UNKNOWN: LE_INFO("-TEST- New state LE_SIM_STATE_UNKNOWN for SIM card.%d", simId); break; default: LE_INFO("-TEST- New state %d for SIM card.%d", simState, simId); break; } }
//-------------------------------------------------------------------------------------------------- void AtClientServer ( SharedData_t* sharedDataPtr ) { struct sockaddr_un addr; int epollFd; struct epoll_event event; char monitorName[MAX_LEN_MONITOR_NAME]; le_fdMonitor_Ref_t fdMonitorRef; LE_INFO("AtClientServer Started !!!"); ClientData.socketFd = socket(AF_UNIX, SOCK_STREAM, 0); LE_ASSERT(ClientData.socketFd != -1); epollFd = epoll_create1(0); LE_ASSERT(epollFd != -1); event.events = EPOLLIN | EPOLLRDHUP; event.data.fd = ClientData.socketFd; LE_ASSERT(epoll_ctl(epollFd, EPOLL_CTL_ADD, ClientData.socketFd, &event) == 0); memset(&addr, 0, sizeof(addr)); addr.sun_family= AF_UNIX; strncpy(addr.sun_path, sharedDataPtr->devPathPtr, sizeof(addr.sun_path)-1); LE_ASSERT(bind(ClientData.socketFd, (struct sockaddr*) &addr, sizeof(addr)) != -1); LE_ASSERT(listen(ClientData.socketFd, 1) != -1); le_sem_Post(sharedDataPtr->semRef); ClientData.connFd = accept(ClientData.socketFd, NULL, NULL); LE_ASSERT(ClientData.connFd != -1); le_sem_Post(sharedDataPtr->semRef); snprintf(monitorName, sizeof(monitorName), "Monitor-%d", ClientData.connFd); fdMonitorRef = le_fdMonitor_Create(monitorName, dup(ClientData.connFd), RxNewData, POLLIN | POLLPRI | POLLRDHUP); le_fdMonitor_SetContextPtr(fdMonitorRef, sharedDataPtr); }
//-------------------------------------------------------------------------------------------------- static void SimRefreshHandler ( le_sim_Id_t simId, le_sim_StkEvent_t stkEvent, void* contextPtr ) { LE_INFO("SIM refresh performed"); le_sem_Post(SimRefreshSemaphore); }
//-------------------------------------------------------------------------------------------------- static void SessionHandlerFunc ( le_mdc_ProfileRef_t profileRef, le_result_t result, void* contextPtr ) { LE_ASSERT(result == LE_OK); le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void AddStkHandler ( void* param1Ptr, void* param2Ptr ) { AppContext_t * appCtxPtr = (AppContext_t*) param1Ptr; // internal semaphore: le_sim internal variable SimToolkitHandlerCount must be correctly updated // before calling le_sim_AddSimToolkitEventHandler again le_sem_Wait(StkHandlerSem); appCtxPtr->stkHandler = le_sim_AddSimToolkitEventHandler(StkHandler, appCtxPtr); LE_ASSERT(appCtxPtr->stkHandler != NULL); le_sem_Post(StkHandlerSem); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void ThresholdEventHandler ( le_temp_ThresholdStatus_t event, void* contextPtr ) { le_temp_ThresholdStatus_t* expectedStatusPtr = contextPtr; LE_ASSERT( *expectedStatusPtr == event ); le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void* TempThread ( void* ctxPtr ) { pa_temp_Init(); le_temp_Init(); le_sem_Post(ThreadSemaphore); le_event_RunLoop(); }
//-------------------------------------------------------------------------------------------------- static void RemoveHandler ( void* param1Ptr, void* param2Ptr ) { AppContext_t * appCtxPtr = (AppContext_t*) param1Ptr; le_ecall_RemoveStateChangeHandler(appCtxPtr->ecallHandler); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void RemoveHandler ( void* param1Ptr, void* param2Ptr ) { AppContext_t * appCtxPtr = (AppContext_t*) param1Ptr; le_temp_RemoveThresholdEventHandler( NULL ); le_temp_RemoveThresholdEventHandler( appCtxPtr->eventHandler ); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void RemoveHandler ( void* param1Ptr, void* param2Ptr ) { AppContext_t * appCtxPtr = (AppContext_t*) param1Ptr; le_sim_RemoveNewStateHandler( appCtxPtr->statHandler ); le_sim_RemoveSimToolkitEventHandler( appCtxPtr->stkHandler ); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
// ------------------------------------------------------------------------------------------------- static void IncrementCounter(void) // ------------------------------------------------------------------------------------------------- { Lock(); Counter++; LE_TEST_INFO("Thread '%s' incremented counter to %u.", le_thread_GetMyName(), Counter); if (Counter == ExpectedCounterValue) { le_sem_Post(CounterSemRef); } Unlock(); }
//-------------------------------------------------------------------------------------------------- static void StkHandler ( le_sim_Id_t simId, le_sim_StkEvent_t stkEvent, void* contextPtr ) { AppContext_t * appCtxPtr = (AppContext_t*) contextPtr; LE_ASSERT(CurrentSimId == simId); LE_ASSERT(StkEvent == stkEvent); appCtxPtr->stkEvent = stkEvent; // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void* AppHandler ( void* ctxPtr ) { AppContext_t * appCtxPtr = (AppContext_t*) ctxPtr; LE_INFO("App id: %d", appCtxPtr->appId); // Subscribe to eCall state handler LE_ASSERT((appCtxPtr->ecallHandler = le_ecall_AddStateChangeHandler(MyECallEventHandler, ctxPtr)) != NULL); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); le_event_RunLoop(); }
//-------------------------------------------------------------------------------------------------- static void* AppHandler ( void* ctxPtr ) { AppContext_t * appCtxPtr = (AppContext_t*) ctxPtr; LE_DEBUG("App id: %d", appCtxPtr->appId); // Subscribe to SIM state handler appCtxPtr->statHandler = le_sim_AddNewStateHandler(SimStateHandler, ctxPtr); LE_ASSERT(appCtxPtr->statHandler != NULL); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); le_event_RunLoop(); }
//-------------------------------------------------------------------------------------------------- static void StartBridge ( void *param1Ptr, void *param2Ptr ) { // dup stdout FdAtClient = dup(2); le_atServer_BridgeRef_t bridgeRef = le_atServer_OpenBridge(FdAtClient); LE_ASSERT(bridgeRef != NULL); *(le_atServer_BridgeRef_t*) param1Ptr = bridgeRef; le_atServer_DeviceRef_t devRef = param2Ptr; LE_ASSERT_OK(le_atServer_AddDeviceToBridge(devRef, bridgeRef)); LE_ASSERT(le_atServer_AddDeviceToBridge(devRef, bridgeRef) == LE_BUSY); le_sem_Post(BridgeSemaphore); }
//-------------------------------------------------------------------------------------------------- static void HandlerFunc ( le_mdc_ProfileRef_t profileRef, le_mdc_ConState_t ConnectionStatus, void* contextPtr ) { ConnectionStateReceivedByHandler = ( ConnectionStatus == LE_MDC_CONNECTED ) ? true : false; ProfileRefReceivedByHandler = (le_mdc_ProfileRef_t) profileRef; if (ConnectionStatus == LE_MDC_DISCONNECTED) { LE_ASSERT( le_mdc_GetDisconnectionReason(profileRef) == LE_MDC_DISC_REGULAR_DEACTIVATION ); LE_ASSERT( le_mdc_GetPlatformSpecificDisconnectionCode(profileRef) == 2 ); } le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- void* ThreadTestHandler( void* ctxPtr ) { int i; for (i=0; i < NB_PROFILE; i++) { SessionStateHandler[i] = le_mdc_AddSessionStateHandler (ProfileRef[i], HandlerFunc, ProfileRef[i]); LE_ASSERT(SessionStateHandler[i] != NULL); } le_sem_Post(ThreadSemaphore); le_event_RunLoop(); }
// A thread function that attempts to lock all the mutexes in the mutex array passed in as context. static void* LockMutex ( void* context ) { MutexRefArray_t* mra = (MutexRefArray_t*)context; int i; for (i = 0; i < mra->size; i++) { le_mutex_Lock(mra->mutexRefArray[i]); } le_sem_Post(SemaRef); le_event_RunLoop(); return NULL; }
//-------------------------------------------------------------------------------------------------- static void SimStateHandler ( le_sim_Id_t simId, le_sim_States_t simState, void* contextPtr ) { AppContext_t * appCtxPtr = (AppContext_t*) contextPtr; LE_DEBUG("App id: %d", appCtxPtr->appId); LE_ASSERT(CurrentSimState == simState); LE_ASSERT(CurrentSimId == simId); appCtxPtr->simState = simState; appCtxPtr->simId = simId; // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); }
//-------------------------------------------------------------------------------------------------- static void RxNewData ( int fd, ///< [IN] File descriptor to read on short events ///< [IN] Event reported on fd ) { char buffer[READ_BYTES]; ssize_t count; SharedData_t* sharedDataPtr = le_fdMonitor_GetContextPtr(); le_sem_Post(sharedDataPtr->semRef); memset(buffer, 0, sizeof(buffer)); if (events & (POLLIN | POLLPRI)) { count = read(fd, buffer, READ_BYTES); if (count == -1) { LE_ERROR("read error: %s", strerror(errno)); return; } else if (count > 0) { if (strcmp(buffer, "AT+CREG?\r") == 0) { LE_INFO("Received AT command: %s", buffer); // Send the response of AT command write(fd, "\r\n\r\n+CREG: 0,1\r\n\r\n\r\nOK\r\n", 24); return; } else if (strcmp(buffer, "AT+CGSN\r") == 0) { LE_INFO("Received AT command: %s", buffer); // Send the response of AT command write(fd, "\r\n359377060033064\r\n\r\nOK\r\n", 25); return; } } } }
//-------------------------------------------------------------------------------------------------- static void* SimPowerCycleIndThread ( void* context ///< Context ) { le_sim_ConnectService(); SimPowerCycleHdlrRef = le_sim_AddNewStateHandler(TestSimStateHandler, NULL); LE_ASSERT(SimPowerCycleHdlrRef); //First sem post and the "simPowerCycleStarted" flag indicate this thread is running le_sem_Post(SimPowerCycleSemaphore); SimPowerCycleStarted = true; LE_INFO("SimPowerCycleIndThread started ..."); le_event_RunLoop(); return NULL; }
//-------------------------------------------------------------------------------------------------- static void* AppHandler ( void* ctxPtr ) { AppContext_t * appCtxPtr = (AppContext_t*) ctxPtr; LE_DEBUG("App id: %d", appCtxPtr->appId); // Subscribe to temperature event handler appCtxPtr->eventHandler = le_temp_AddThresholdEventHandler(NULL, &ExpectedStatus); LE_ASSERT(appCtxPtr->eventHandler == NULL); appCtxPtr->eventHandler=le_temp_AddThresholdEventHandler(ThresholdEventHandler,&ExpectedStatus); LE_ASSERT(appCtxPtr->eventHandler != NULL); // Semaphore is used to synchronize the task execution with the core test le_sem_Post(ThreadSemaphore); le_event_RunLoop(); }
// A thread "main" function which creates a series of mutexes. static void* ThreadCreateMutex ( void* context ) { long mutexCount = (long)context; char mutexNameBuffer[MAX_NAME_BYTES] = {0}; MutexRefArray_t mra; mra.size = mutexCount; mra.mutexRefArray = malloc(mutexCount * sizeof(le_mutex_Ref_t)); LE_INFO("Thread [%s] has started. Creating %ld mutexes.", le_thread_GetMyName(), mutexCount); le_mutex_Lock(MutexIndexMutexRef); long cnt = 0; while (cnt < mutexCount) { snprintf(mutexNameBuffer, MAX_NAME_BYTES, "[%s]Mutex%ld", le_thread_GetMyName(), MutexCreateIdx); mra.mutexRefArray[cnt] = le_mutex_CreateNonRecursive(mutexNameBuffer); le_mutex_Lock(mra.mutexRefArray[cnt]); MutexCreateIdx++; cnt++; } le_mutex_Unlock(MutexIndexMutexRef); // Save the list of mutex refs in the thread's local storage. (void) pthread_setspecific(TsdMutexRefKey, &mra); le_sem_Post(SemaRef); le_event_RunLoop(); return NULL; }