void PCCoreClear(ST_PacketConnection* pPacketConnection, Semaphore* pSemaphore, PCClearType type) { Event* pEvent1 = NULL; Event* pEvent2 = NULL; Event* pEvent3 = NULL; PcClearVarInd request; ProtocolEngine* pProtocolEngine = pPacketConnection->pProtocolEngine; #ifdef AMOS_DEBUG AMPrintf("noble:PCCoreClear <<<<< PCClearType = %d start...\n", type); #endif memset(&request, 0, sizeof(request)); //denx add for sync pProtocolEngine->pPCContext->semaphoreCreate(pSemaphore, 0, 2); request.pSemaphore = pSemaphore; EventCreate(pPacketConnection, &pEvent1, PC_CLEAR_VAR_IND, (void*)&request, sizeof(request)); EventCreate(pPacketConnection, &pEvent2, PC_CLEAR_VAR_IND, (void*)&request, sizeof(request)); PCPostMessage(pEvent1, pProtocolEngine->pPCCore->pReaderQueue); PCPostMessage(pEvent2, pProtocolEngine->pPCCore->pSenderQueue); PCContextGetInstance()->semaphoreWait(pSemaphore); PCContextGetInstance()->semaphoreWait(pSemaphore); PCContextGetInstance()->semaphoreDestory(pSemaphore); if(type == ePCClearPCHandle) { #ifdef AMOS_DEBUG AMPrintf("noble:PCCoreClear <<<<< ePCClearPCHandle...\n"); #endif pProtocolEngine->pPCContext->semaphoreCreate(pSemaphore, 0, 1); request.pSemaphore = pSemaphore; EventCreate(pPacketConnection, &pEvent3, PC_CLEAR_VAR_IND, (void*)&request, sizeof(request)); PCPostMessage(pEvent3, pProtocolEngine->pPCCore->pCallbackQueue); #ifdef AMOS_DEBUG AMPrintf("noble:PCCoreClear <<<<< ePCClearPCHandle before wait..\n"); #endif PCContextGetInstance()->semaphoreWait(pSemaphore); PCContextGetInstance()->semaphoreDestory(pSemaphore); } #ifdef AMOS_DEBUG AMPrintf("noble:PCCoreClear >>>>> end...\n"); #endif }
void ReaderExceedExceptionHandler(ProtocolEngine* pProtocolEngine, AMInt32 length) { PCRetCode retCode = eOK; Event* pExceptionEvent = NULL; PcExceedExceptionInd exceptionInd; PacketConnection* pTempPC = NULL; PacketConnectionMgr* pTempPCMgr = NULL; if(pProtocolEngine->pPCCore->coreStatus == eIMnetCoreStart) { //send a exception to up-layer. pExceptionEvent = NULL; exceptionInd.length = length; //denx: send this exception to all packet connection. pTempPCMgr = PCMgrGetInstance(); pTempPC = pTempPCMgr->pFirstPC; while(pTempPC) { retCode = EventCreate(pTempPC, &pExceptionEvent, PC_EXCEED_EXCEPTION_IND, (void*)&exceptionInd, sizeof(exceptionInd)); if(retCode == eOK) PCPostMessage(pExceptionEvent, pProtocolEngine->pPCCore->pCallbackQueue); else { #ifdef AMOS_DEBUG AMAssert(0); #endif } pTempPC = pTempPC->next; } } }
void LoopStop(Queue* pQueue, Semaphore* pSemaphore) { Event* pEvent = NULL; PcShutdownInd indShutdown; memset(&indShutdown, 0, sizeof(indShutdown)); //send out shutdown signal to close loop EventCreate(NULL, &pEvent, PC_SHUTDOWN_IND, (void*)&indShutdown, sizeof(indShutdown)); PCPostMessage(pEvent, pQueue); PCContextGetInstance()->semaphoreWait(pSemaphore); }
void SocketReaderCallback(AMInt32 reference, AMInt32 socket, SocketStatus status) { IMnet* pIMnet = (IMnet*)reference; Event* pEvent = NULL; ImSocketInd indication; PCRetCode retCode; memset(&indication, 0, sizeof(indication)); indication.indType = status; retCode = EventCreate(&pEvent, PC_SOCKET_IND, (void*)&indication, sizeof(indication)); if(retCode == eOK) { IMnetPostMessage(pIMnet, pEvent, IMnetReaderQueue); } }
PCRetCode SendNetworkConnectedMsg(PacketConnection* pPC, AMInt32 result) { PCRetCode retCode; Event* pEventTemp = NULL; PcCnfNetworkConfig confirm; pEventTemp = NULL; memset(&confirm, 0, sizeof(confirm)); confirm.result = result; retCode = EventCreate(pPC, &pEventTemp, PC_CNF_NETWORK_CONFIG, (void*)&confirm, sizeof(confirm)); if(retCode == eOK) PCPostMessage(pEventTemp, pPC->pProtocolEngine->pPCCore->pCallbackQueue); return retCode; }
void PacketExceptionHandler(PacketConnection* pPC, PCRetCode code, EventId id) { PCRetCode retCode = eOK; Event* pExceptionEvent = NULL; PcUnpackExceptionInd exceptionInd; //send a exception to up-layer. pExceptionEvent = NULL; exceptionInd.retCode = code; retCode = EventCreate(pPC, &pExceptionEvent, id, (void*)&exceptionInd, sizeof(exceptionInd)); if(retCode == eOK) PCPostMessage(pExceptionEvent, pPC->pProtocolEngine->pPCCore->pCallbackQueue); else { #ifdef AMOS_DEBUG AMAssert(0); #endif } }
void SenderSendExceptionHandler(PacketConnection* pPC, AMInt32 sd, EventId id) { PCRetCode retCode = eOK; Event* pExceptionEvent = NULL; PcSendExceptionInd exceptionInd; //send a exception to up-layer. pExceptionEvent = NULL; exceptionInd.sd = sd; exceptionInd.id = id; retCode = EventCreate(pPC, &pExceptionEvent, PC_SEND_EXCEPTION_IND, (void*)&exceptionInd, sizeof(exceptionInd)); if(retCode == eOK) PCPostMessage(pExceptionEvent, pPC->pProtocolEngine->pPCCore->pCallbackQueue); else { #ifdef AMOS_DEBUG AMAssert(0); #endif } }
/*----------------------------------------------------------------------------* * NAME * SchedInit * * DESCRIPTION * Prepare scheduler instance with identifier id. The scheduler instance * will run in its own thread with given priority and stack size. * The function returns the new scheduler instance. Priority and stack * size values are passed unchanged to the ThreadCreate() call of the * underlying Framework Extension API. * * Valid id range is 0 to _SCHED_MAX_SEGMENTS - 1 * * Valid priority and stack size values are determined be the Framework * Extensions API. * * RETURNS * The scheduler instance * *----------------------------------------------------------------------------*/ void *SchedInit(uint16 id, uint16 priority, uint32 stackSize) { uint16 i = 0; /* Sanity check */ if (id >= _SCHED_MAX_SEGMENTS) { // Panic(_TECH_FW, _PANIC_FW_UNEXPECTED_VALUE, "_SCHED_MAX_SEGMENTS exceeded"); } /* Alloc instance */ if (instance == NULL) { instance = (SchedulerInstanceType *) MemAlloc(sizeof(SchedulerInstanceType)); if (instance == NULL) { GENINFO(("%s, line=%d error memory\n", __FUNCTION__, __LINE__)); return (void*)NULL; } for (i = 0; i < _SCHED_MAX_SEGMENTS; i ++) { instance->thread[i].currentTask = _SCHED_TASK_ID; } MutexCreate(&instance->bgMutex); if (EventCreate(&instance->eventHandle)) { // Panic(_TECH_FW, _PANIC_FW_UNEXPECTED_VALUE, "Event creation failed"); } } instance->thread[id].inUse = TRUE; instance->threadIdVector |= 0x0001 << (uint16) id; /* Initial running state */ instance->thread[id].schedRunning = FALSE; /* Set thread priority */ instance->thread[id].priority = priority; /* Set thread stack size */ instance->thread[id].stackSize = stackSize; /* Start initialisation of a single thread */ instance->thread[id].init = TRUE; /* Store thread id */ instance->thread[id].id = (uint8) id; /* Collect the number of tasks for this thread */ instance->setupId = id; SchedTaskInit((void *) instance); /* Setup task structures for thread */ instance->thread[id].tasks = MemAlloc(instance->thread[id].numberOfTasks * sizeof(TaskDefinitionType)); if (instance->thread[id].tasks == NULL) { GENINFO(("%s, line=%d error memory\n", __FUNCTION__, __LINE__)); return (void*)NULL; } /* Run task setup once more to transfer the function pointers */ instance->thread[id].numberOfTasks = 0; instance->thread[id].init = FALSE; /* Prepare tasks */ SchedTaskInit((void *) instance); instance->setupId = _SCHED_TASK_ID; if (MutexCreate(&(instance->thread[id].qMutex)) != _RESULT_SUCCESS) { // Panic(_TECH_FW, _PANIC_FW_UNEXPECTED_VALUE, "Mutex creation failed"); } if (EventCreate(&(instance->thread[id].eventHandle))) { // Panic(_TECH_FW, _PANIC_FW_UNEXPECTED_VALUE, "Event creation failed"); } /* Prepare queues */ for (i = 0; i < instance->thread[id].numberOfTasks; i ++) { instance->thread[id].tasks[i].instanceDataPointer = NULL; instance->thread[id].tasks[i].messageQueueFirst = NULL; instance->thread[id].tasks[i].messageQueueLast = NULL; } return instance; }
AMUInt32 SenderRunner(void* lpParam) #endif #endif { Event* pEvent = NULL; AMInt32 isExit = 0; PcNetworkConfig* pConfig = NULL; ProtocolEngine* pProtocolEngine = ((PacketConnection*)lpParam)->pProtocolEngine; PacketConnection* pPacketConnection; AMInt32 iBarkTime = 0, iNowTime = 0, iMissCount = 0; PcHealthCheckConfig hcConfig = {0, NULL}; Event* pHealthCheckEvent = NULL; Event* pHealthCheckSelfEvent = NULL; PcClearVarInd* pClearVarInd = NULL; while(1) { //get Request EVENT from sender queue. PullQueue(pProtocolEngine->pPCCore->pSenderQueue, &pEvent, 1); if(pEvent != NULL) { pPacketConnection = (PacketConnection*)pEvent->hPCHandle; switch(pEvent->id) { case PC_SHUTDOWN_IND: //关闭当前模块 #ifdef AMOS_DEBUG AMPrintf("noble:exit sender thread....\n"); #endif isExit = 1; break; case PC_CLEAR_VAR_IND: #ifdef AMOS_DEBUG AMPrintf("noble:sender PC_CLEAR_VAR_IND....\n"); #endif //network need to close if(pPacketConnection && pPacketConnection->pNetwork && pPacketConnection->pNetwork->fd>0) PCContextGetInstance()->socketClose(&pPacketConnection->pNetwork->fd); pClearVarInd = (PcClearVarInd*)pEvent->pContent; pProtocolEngine->pPCContext->semaphoreSignal(pClearVarInd->pSemaphore); break; case PC_SET_HEALTH_CHECK_CONFIG: if(iBarkTime == 0) memcpy(&hcConfig, (PcHealthCheckConfig*)pEvent->pContent, sizeof(hcConfig)); if( IsPCNetworkReady(pProtocolEngine->pPCCore) > 0 && IsPCNeedHealthCheck(pProtocolEngine->pPCCore) > 0 ) { iNowTime = AMGetUTCTime(AMNULL); if( iBarkTime+hcConfig.second < iNowTime || iNowTime < iBarkTime) { if(hcConfig.pMaker != NULL) { hcConfig.pMaker(pEvent->hPCHandle, &pHealthCheckEvent, pPacketConnection->pReference); SenderPacket(pHealthCheckEvent); EventDestory(&pHealthCheckEvent); iBarkTime = iNowTime; } iMissCount = 0; } } //send PC_SET_HEALTH_CHECK_CONFIG to self. EventCreate(pEvent->hPCHandle, &pHealthCheckSelfEvent, PC_SET_HEALTH_CHECK_CONFIG, (void*)&hcConfig, sizeof(hcConfig)); PCPostMessage(pHealthCheckSelfEvent, pProtocolEngine->pPCCore->pSenderQueue); pProtocolEngine->pPCContext->sleep(HEALTH_CHECK_SLEEP_TIME); break; case PC_SET_NETWORK_CONFIG: //预处理网络连接 pConfig = (PcNetworkConfig*)pEvent->pContent; if(pConfig->nCount <= 1) { #ifdef AMOS_DEBUG AMPrintf("noble:sender socket create. ip = %s:%d\n", pConfig->ip, pConfig->port); #endif pProtocolEngine->pPCCore->socketFd = pProtocolEngine->pPCContext->socketCreate( pConfig->ip, pConfig->port #ifdef AMOS #ifdef SOCKET_ASYNC , (AMInt32)pIMnet, AMSocketIAPGet(), SocketConnectionCallback, SocketSenderCallback, SocketReaderCallback #endif #endif ); #ifdef AMOS_DEBUG AMPrintf("noble:sender socket create over %d \n", pProtocolEngine->pPCCore->socketFd); #endif } if(pPacketConnection->pNetwork != NULL) pPacketConnection->pNetwork->fd = pProtocolEngine->pPCCore->socketFd; if(pConfig->sync) { //tell pcmgr PacketConnectionOpen pPacketConnection->pProtocolEngine->pPCContext->semaphoreSignal(&(pPacketConnection->semaphore)); } else { #ifdef SOCKET_ASYNC if(pProtocolEngine->pPCCore->socketFd <= 0) #endif { SendNetworkConnectedMsg(pPacketConnection, pProtocolEngine->pPCCore->socketFd); } } break; default: SenderPacket(pEvent); break; } } //destory EVENT if(pEvent != NULL) EventDestory(&pEvent); if(isExit == 1) goto SenderEnd; } SenderEnd: pProtocolEngine->pPCContext->semaphoreSignal(&pProtocolEngine->pPCCore->pSenderLooper->semaphore); #ifdef AMOS return (AMPVoid)isExit; #else #ifdef WIN32 return isExit; #endif #endif }
PCRetCode ReaderRecvExceptionHandler(PacketConnection* pPacketConnection, AMInt32 desireLen, AMInt32 realLen) { PCRetCode retCode = eOK; Event* pExceptionEvent = NULL; PcRecvExceptionInd exceptionInd; #ifdef SOCKET_ASYNC AMInt32 indType = 0; #endif AMAssert(NULL != pPacketConnection && NULL != pPacketConnection->pProtocolEngine ); AMAssert(NULL != pPacketConnection->pProtocolEngine->pPCCore); //shutdown health check PCCoreSwitchHealthCheck(pPacketConnection->pProtocolEngine->pPCCore, 0); //close network PCContextGetInstance()->socketClose((AMUInt32*)&pPacketConnection->pProtocolEngine->pPCCore->socketFd); if(pPacketConnection->pProtocolEngine->pPCCore->coreStatus == eIMnetCoreStart) { //send a exception to up-layer. pExceptionEvent = NULL; exceptionInd.desireLength = desireLen; exceptionInd.realLength = realLen; #ifdef AMOS_DEBUG AMPrintf("noble: reader send PC_RECV_EXCEPTION_IND\n"); #endif retCode = EventCreate(pPacketConnection, &pExceptionEvent, PC_RECV_EXCEPTION_IND, (void*)&exceptionInd, sizeof(exceptionInd)); if(retCode == eOK) PCPostMessage(pExceptionEvent, pPacketConnection->pProtocolEngine->pPCCore->pCallbackQueue); else { #ifdef AMOS_DEBUG AMAssert(0); #endif return eJumpWhile; } } else { #ifdef AMOS_DEBUG AMPrintf("noble: reader do not send PC_RECV_EXCEPTION_IND\n"); #endif } #ifdef AMOS_DEBUG AMPrintf("noble: reader wait for ...\n"); #endif //等待上层重新建立连接,或者退出程序 while(IsPCNetworkReady(pPacketConnection->pProtocolEngine->pPCCore) <= 0) { if( ReaderEventHandler(pPacketConnection->pProtocolEngine, 0 #ifdef SOCKET_ASYNC , &indType #endif ) == eJumpWhile ) return eJumpWhile; PCContextGetInstance()->sleep(100); } #ifdef AMOS_DEBUG AMPrintf("noble: reader out wait for....\n"); #endif return eContinue; }