osp.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:9k
- #include <osp.h>
- #include <task.h>
- #include <message.h>
- #include <ostime.h>
- #include <string.h>
- #include <stddefs.h>
- #include "stcommon.h"
- /*
- #ifdef ST_OS21
- #define MS_TO_TICKS(X) ((X)*(time_ticks_per_sec()/1000))
- #else
- #define MS_TO_TICKS(X) ((X)*(ST_GetClocksPerSecond()/1000))
- #endif
- */
- static OSP_TaskInfo *taskLink = NULL;
- static T_OSP_QueueInfo *queueLink = NULL;
- static void KD_InserTaskNode(OSP_TaskInfo *node)
- {
- if(node != NULL)
- {
- node->pNext = taskLink;
- taskLink = node;
- }
- }
- static OSP_TaskInfo* KD_RemoveTaskNode(task_t *taskId)
- {
- OSP_TaskInfo *cur, *prv;
- cur = taskLink;
- prv = NULL;
-
- while((cur != NULL) && (cur->pTask != taskId))
- {
- prv = cur;
- cur = cur->pNext;
- }
- if(cur != NULL)
- {
- if(cur == taskLink)
- {
- taskLink = taskLink->pNext;
- cur->pNext = NULL;
- }
- else
- {
- prv->pNext = cur->pNext;
- cur->pNext = NULL;
- }
- }
-
- return cur;
- }
- //modified by whale 07/06/03
- //static void InserQueueNode(T_OSP_QueueInfo *node)
- void InserQueueNode(T_OSP_QueueInfo *node)
- {
- if(node != NULL)
- {
- node->pNext = queueLink;
- queueLink = node;
- }
- }
- static T_OSP_QueueInfo* RemoveQueueNode(message_queue_t *queueId)
- {
- T_OSP_QueueInfo *cur, *prv;
- cur = queueLink;
- prv = NULL;
-
- while((cur != NULL) && (cur->pQueueId != queueId))
- {
- prv = cur;
- cur = cur->pNext;
- }
- if(cur != NULL)
- {
- if(cur == queueLink)
- {
- queueLink = queueLink->pNext;
- cur->pNext = NULL;
- }
- else
- {
- prv->pNext = cur->pNext;
- cur->pNext = NULL;
- }
- }
-
- return cur;
- }
- KB_OSPRet KB_OSPInit(void)
- {
- return Ret_OK;
- }
- KB_OSPRet KB_OSPTaskInit
- (
- const char* pName,
- UINT32 dStackSize,
- void (*entryPoint)(void*),
- INT32 dPriority,
- void *pPara,
- UINT32 *pTaskId
- )
- {
- OSP_TaskInfo *pTaskInfo;
- int Error;
- pTaskInfo = (OSP_TaskInfo*)KB_OSPMalloc(sizeof(OSP_TaskInfo));
- if(pTaskInfo == NULL)
- {
- return Ret_Fail;
- }
- pTaskInfo->pTaskStack = (UINT8 *)KB_OSPMalloc(dStackSize);
- pTaskInfo->pTaskDesc = (tdesc_t *)KB_OSPMalloc(sizeof(tdesc_t));
- pTaskInfo->pTask = (task_t *)KB_OSPMalloc(sizeof(task_t));
- if( (pTaskInfo->pTaskStack == NULL) || (pTaskInfo->pTaskDesc == NULL)
- || (pTaskInfo->pTask == NULL) )
- {
- KB_OSPFree((void*)pTaskInfo->pTaskStack);
- KB_OSPFree((void*)pTaskInfo->pTaskDesc);
- KB_OSPFree((void*)pTaskInfo->pTask);
- KB_OSPFree((void*)pTaskInfo);
- return Ret_Fail;
- }
-
- Error = task_init((void (*)(void *))entryPoint, (void *)pPara,
- (void *)pTaskInfo->pTaskStack, (size_t)dStackSize,
- pTaskInfo->pTask, pTaskInfo->pTaskDesc,
- (int)(dPriority * OS20_PRIORITY_LEVELS / 256),
- pName, 0);
- if(Error != 0)
- {
- KB_OSPFree((void*)pTaskInfo->pTaskStack);
- KB_OSPFree((void*)pTaskInfo->pTaskDesc);
- KB_OSPFree((void*)pTaskInfo->pTask);
- KB_OSPFree((void*)pTaskInfo);
- return Ret_Fail;
- }
- *pTaskId = (UINT32)pTaskInfo->pTask;
- KD_InserTaskNode(pTaskInfo);
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPTaskSetPri(UINT32 dTaskId, INT32 dPriority)
- {
- task_priority_set((task_t*)dTaskId, (int)(dPriority * OS20_PRIORITY_LEVELS / 256));
- return Ret_OK;
- }
- UINT32 KB_OSPTaskGetID(void)
- {
- return (UINT32)task_id();
- }
- KB_OSPRet KB_OSPTaskDelay(UINT32 dMilliSeconds)
- {
- task_delay((clock_t)MS_TO_TICKS(dMilliSeconds));
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPTaskSus(UINT32 dTaskId)
- {
- if(task_suspend((task_t*)dTaskId)==0)
- return Ret_OK;
- else
- return Ret_Fail;
- }
- KB_OSPRet KB_OSPTaskRes(UINT32 dTaskId)
- {
- if(task_resume((task_t*)dTaskId) == 0)
- return Ret_OK;
- else
- return Ret_Fail;
- }
- KB_OSPRet KB_OSPTaskDel(UINT32 dTaskId)
- {
- OSP_TaskInfo *pTaskInfo;
-
- task_t* task = (task_t*)dTaskId;
- pTaskInfo = KD_RemoveTaskNode(task);
-
- task_kill(task, 0, 0);
-
- task_wait(&task, 1, TIMEOUT_IMMEDIATE);
-
- task_delete(task);
- if(pTaskInfo != NULL)
- {
- KB_OSPFree((void*)pTaskInfo->pTaskStack);
- KB_OSPFree((void*)pTaskInfo->pTaskDesc);
- KB_OSPFree((void*)pTaskInfo->pTask);
- KB_OSPFree((void*)pTaskInfo);
- }
- return Ret_OK;
- }
- KB_OSPRet KB_OSPQueInit
- (
- const char* pName,
- UINT32 dMaxMessages,
- UINT32 *pQueueId
- )
- {
- T_OSP_QueueInfo *pQueueInfo;
- if((pQueueId == NULL) || (dMaxMessages == 0))
- return Ret_ParErr;
-
- pQueueInfo = (T_OSP_QueueInfo*)KB_OSPMalloc(sizeof(T_OSP_QueueInfo));
- if(pQueueInfo == NULL)
- {
- return Ret_Fail;
- }
-
- pQueueInfo->pQueueId = (message_queue_t*)KB_OSPMalloc(sizeof(message_queue_t));
- pQueueInfo->pQueueBuf = (void*)KB_OSPMalloc(MESSAGE_MEMSIZE_QUEUE(sizeof(KB_OSPMsgNode), dMaxMessages));
- if( (pQueueInfo->pQueueId == NULL)
- || (pQueueInfo->pQueueBuf == NULL) )
- {
- KB_OSPFree((void*)pQueueInfo->pQueueId);
- KB_OSPFree((void*)pQueueInfo->pQueueBuf);
- KB_OSPFree((void*)pQueueInfo);
- return Ret_Fail;
- }
- message_init_queue_timeout(pQueueInfo->pQueueId, pQueueInfo->pQueueBuf, sizeof(KB_OSPMsgNode),
- (unsigned int)dMaxMessages);
-
- *pQueueId = (UINT32)pQueueInfo->pQueueId;
- InserQueueNode(pQueueInfo);
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPQueDel(UINT32 nQueID)
- {
- T_OSP_QueueInfo *pQueueInfo;
-
- pQueueInfo = RemoveQueueNode((message_queue_t*)nQueID);
- message_delete_queue((message_queue_t*)nQueID);
-
- if(pQueueInfo != NULL)
- {
- /* 回收已分配的内存 */
- KB_OSPFree((void*)pQueueInfo->pQueueId);
- KB_OSPFree((void*)pQueueInfo->pQueueBuf);
- KB_OSPFree((void*)pQueueInfo);
- }
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPMsgSend
- (
- UINT32 nQueID,
- KB_OSPMsgNode *pMsgNode
- )
- {
- KB_OSPMsgNode * PtMessage;
- if(pMsgNode == NULL) return Ret_ParErr;
- PtMessage = (KB_OSPMsgNode *)message_claim_timeout((message_queue_t*)nQueID, (clock_t*)TIMEOUT_IMMEDIATE);
- if(PtMessage == NULL) return Ret_Fail;
-
- memcpy(PtMessage, pMsgNode, sizeof(KB_OSPMsgNode));
- message_send((message_queue_t*)nQueID, (void*)PtMessage);
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPMsgGet
- (
- UINT32 nQueID,
- KB_OSPWaitMode nWaitMode,
- UINT32 nTime,
- KB_OSPMsgNode *pMsgNode
- )
- {
- INT32 dReturnType;
- void* pMessageReceived;
- clock_t *dWaitTime;
- clock_t time;
-
- if(pMsgNode == NULL) return Ret_ParErr;
-
- if(nWaitMode == KD_NoWait)
- {
- dWaitTime = (clock_t*)TIMEOUT_IMMEDIATE;
- }
- else if(nWaitMode == KB_Wait)
- {
- if(nTime == 0)
- {
- dWaitTime = (clock_t*)TIMEOUT_INFINITY;
-
- }
- else
- {
- time = time_plus(time_now(), (clock_t)MS_TO_TICKS(nTime));
- dWaitTime = &time;
- }
- }
- else
- {
- return Ret_ParErr;
- }
- pMessageReceived = message_receive_timeout((message_queue_t*)nQueID, dWaitTime);
- if(pMessageReceived != NULL)
- {
- memcpy(pMsgNode, pMessageReceived, sizeof(KB_OSPMsgNode));
- message_release((message_queue_t*)nQueID, pMessageReceived);
- dReturnType = Ret_OK;
- }
- else
- {
- dReturnType = Ret_Fail;
- }
-
- return dReturnType;
- }
- KB_OSPRet KB_OSPSemInit
- (
- const char* pName,
- UINT32 nSemMax,
- KB_OSPQueMode nQueMode,
- UINT32 *pSemID
- )
- {
- semaphore_t *pSemaphoreCreated;
- if( (nQueMode != J_OSP_WAIT_FIFO)
- && (nQueMode != J_OSP_WAIT_PRIO) )
- {
- return Ret_ParErr;
- }
-
- pSemaphoreCreated = KB_OSPMalloc(sizeof(semaphore_t));
-
- if(pSemaphoreCreated == NULL) return Ret_Fail;
-
- if(nQueMode == J_OSP_WAIT_FIFO)
- {
- semaphore_init_fifo_timeout(pSemaphoreCreated, (int)nSemMax);
- }
- else if(nQueMode == J_OSP_WAIT_PRIO)
- {
- semaphore_init_priority_timeout(pSemaphoreCreated, (int)nSemMax);
- }
- *pSemID = (UINT32)pSemaphoreCreated;
-
- return Ret_OK;
- }
- KB_OSPRet KB_OSPSemDel(UINT32 nSemId)
- {
- semaphore_delete((semaphore_t*)nSemId);
- KB_OSPFree((void *)nSemId);
- return Ret_OK;
- }
- KB_OSPRet KB_OSPSemGet
- (
- UINT32 nSemId,
- KB_OSPWaitMode nWaitMode,
- UINT32 nTime
- )
- {
- INT32 dReturnType;
- clock_t *dWaitTime;
- clock_t time;
-
- if(nWaitMode == KD_NoWait)
- {
- dWaitTime = (clock_t*)TIMEOUT_IMMEDIATE;
- }
- else if(nWaitMode == KB_Wait)
- {
- if(nTime == 0)
- {
- dWaitTime = (clock_t*)TIMEOUT_INFINITY;
- }
- else
- {
- time = time_plus(time_now(), (clock_t)MS_TO_TICKS(nTime));
- dWaitTime = &time;
- }
- }
- else
- {
- return Ret_ParErr;
- }
-
- if(0 == semaphore_wait_timeout((semaphore_t*)nSemId, dWaitTime))
- {
- dReturnType = Ret_OK;
- }
- else
- {
- dReturnType = Ret_Timeout;
- }
-
- return dReturnType;
- }
- KB_OSPRet KB_OSPSemSet(UINT32 nSemId)
- {
- semaphore_signal((semaphore_t*)nSemId);
- return Ret_OK;
- }
- extern partition_t *SystemPartition;
- extern void SRCHK_RecordMemoryStatus(void);
- extern void SRCHK_ShowMemoryStatus(void);
- void* KB_OSPMalloc(UINT32 dSize)
- {
- void*p=NULL;
- //add byshriek
- if(dSize<=0)
- return NULL;
- //shriek end
-
- p = memory_allocate(SystemPartition,(size_t)dSize);
- if(p==NULL)
- printf("nmemory_allocate null size[%ld]",dSize);
-
- return p;
- }
- KB_OSPRet KB_OSPFree(void *pFree)
- {
- if(pFree == NULL) return Ret_Fail;
- memory_deallocate(SystemPartition, pFree);
- return Ret_OK;
- }
- /* EOF */