📄 msg.c
字号:
/******************************************************************************
Copyright (c) 2006 by RockOS.
All rights reserved.
This software is supported by the Rock Software Workroom only.
Any bugs please contact the author with e-mail or QQ:
E-mail : baobaoba520@yahoo.com.cn
QQ : 59681888
*******************************************************************************
File name : msg.c
Description : message packet management for RockOS.
:
:
Auther : sunxinqiu
History :
2006-3-15 first release.
******************************************************************************/
#include "os.h"
/******************************************************************************
Global var : MSGCB g_miniMsgCB[];
: MSGCB g_commMsgCB[];
: MSGCB g_hugeMsgCB[];
Description : The message CBs for mini msg, common msg and huge msg.
******************************************************************************/
#if MINI_MSG_NUM > 0
MSGCB g_miniMsgCB[MINI_MSG_NUM];
#endif
#if COMMON_MSG_NUM > 0
MSGCB g_commMsgCB[COMMON_MSG_NUM];
#endif
#if HUGE_MSG_NUM > 0
MSGCB g_hugeMsgCB[COMMON_MSG_NUM];
#endif
/******************************************************************************
Global var : HQUEUE g_miniMsgFreeQ;
: HQUEUE g_commMsgFreeQ;
: HQUEUE g_hugeMsgFreeQ;
Description : The message CBs for mini msg, common msg and huge msg.
******************************************************************************/
#if MINI_MSG_NUM > 0
HQUEUE g_miniMsgFreeQ;
#endif
#if COMMON_MSG_NUM > 0
HQUEUE g_commMsgFreeQ;
#endif
#if HUGE_MSG_NUM > 0
HQUEUE g_hugeMsgFreeQ;
#endif
/******************************************************************************
Global var : HTIMER msgTimer;
Description : This timer is used for check message packet periodly.
:
******************************************************************************/
HTIMER msgTimer;
/******************************************************************************
Function : STATUS msg_init(void)
Params : N/A
:
:
:
Return : NO_ERROR or other error code.
Description : This function should only be called during system starting up.
:
******************************************************************************/
STATUS msg_init(void)
{
STATUS nret;
U32 size;
U8 * p;
HMSG hmsg;
if (g_OSRunning != OS_PHASE_INIT)
{
OS_error("msg_init(): this function can't be called after system in scheduling!!!\n");
return OS_FAIL;
}
#if MINI_MSG_NUM > 0
/* alloc memory for mini msg packet. */
size = MINI_MSG_NUM * MINI_MSG_LEN;
p = (U8 *)memAlloc(size, 0, "mini msg");
if (p == NULL)
{
OS_error ("msg_init(): alloc memory for mini msg failure, size = %d!!!\n", size);
return OS_FAIL;
}
/* create free mini msg queue. */
g_miniMsgFreeQ = q_create(OS_QUEUE_FIFO, MINI_MSG_NUM, NULL_TASK, "free mini msg");
if (g_miniMsgFreeQ == NULL_QUEUE)
{
OS_error ("msg_init(): create free mini msg queue fail!!!\n");
return OS_FAIL;
}
/* init msgcb and add all msg to free queue. */
for (hmsg = 0; hmsg < MINI_MSG_NUM; hmsg++)
{
memset (&g_miniMsgCB[hmsg], 0, sizeof(MSGCB));
g_miniMsgCB[hmsg].state = OS_MSG_FREE;
g_miniMsgCB[hmsg].owner = NULL_TASK;
g_miniMsgCB[hmsg].pBuffer = p + hmsg * MINI_MSG_LEN;
nret = q_add(g_miniMsgFreeQ, (HANDLE)hmsg, 0);
if (nret != OS_SUCCESS)
{
OS_error("msg_init(): create mini msg free queue fail, the MAX_BASIC_QELEMENT may be too few!!!\n");
return OS_FAIL;
}
}
#endif
#if COMMON_MSG_NUM > 0
/* alloc memory for common msg packet. */
size = COMMON_MSG_NUM * COMMON_MSG_LEN;
p = (U8 *)memAlloc(size, 0, "common msg");
if (p == NULL)
{
OS_error ("msg_init(): alloc memory for common msg failure, size = %d!!!\n", size);
return OS_FAIL;
}
/* create free common msg queue. */
g_commMsgFreeQ = q_create(OS_QUEUE_FIFO, COMMON_MSG_NUM, NULL_TASK, "free common msg");
if (g_commMsgFreeQ == NULL_QUEUE)
{
OS_error ("msg_init(): create free common msg queue fail!!!\n");
return OS_FAIL;
}
for (hmsg = 0; hmsg < COMMON_MSG_NUM; hmsg++)
{
memset (&g_commMsgCB[hmsg], 0, sizeof(MSGCB));
g_commMsgCB[hmsg].state = OS_MSG_FREE;
g_commMsgCB[hmsg].owner = NULL_TASK;
g_commMsgCB[hmsg].pBuffer = p + hmsg * COMMON_MSG_LEN;
nret = q_add(g_commMsgFreeQ, (HANDLE)hmsg, 0);
if (nret != OS_SUCCESS)
{
OS_error("msg_init(): create common msg free queue fail, the MAX_BASIC_QELEMENT may be too few!!!\n");
return OS_FAIL;
}
}
#endif
#if HUGE_MSG_NUM > 0
/* alloc memory for huge msg packet. */
size = HUGE_MSG_NUM * HUGE_MSG_LEN;
p = (U8 *)memAlloc(size, 0, "huge msg");
if (p == NULL)
{
OS_error ("msg_init(): alloc memory for huge msg failure, size = %d!!!\n", size);
return OS_FAIL;
}
/* create free huge msg queue. */
g_hugeMsgFreeQ = q_create(OS_QUEUE_FIFO, HUGE_MSG_NUM, NULL_TASK, "free huge msg");
if (g_hugeMsgFreeQ == NULL_QUEUE)
{
OS_error ("msg_init(): create free huge msg queue fail!!!\n");
return OS_FAIL;
}
for (hmsg = 0; hmsg < HUGE_MSG_NUM; hmsg++)
{
memset (&g_hugeMsgCB[hmsg], 0, sizeof(MSGCB));
g_hugeMsgCB[hmsg].state = OS_MSG_FREE;
g_hugeMsgCB[hmsg].owner = NULL_TASK;
g_hugeMsgCB[hmsg].pBuffer = p + hmsg * HUGE_MSG_LEN;
nret = q_add(g_hugeMsgFreeQ, (HANDLE)hmsg, 0);
if (nret != OS_SUCCESS)
{
OS_error("msg_init(): create huge msg free queue fail, the MAX_BASIC_QELEMENT may be too few!!!\n");
return OS_FAIL;
}
}
#endif
/* message check timer should be create here. */
return OS_SUCCESS;
}
/******************************************************************************
Function : HMSG msgAlloc(U16 type)
Params : type - mini, common or huge message.
:
:
:
Return : handle for msg packet.
Description : To use a msg, this function should be called first, and map the
: handle to get the address.
******************************************************************************/
HMSG msgAlloc(U16 type)
{
STATUS result;
HMSG hmsg;
hmsg = NULL_HANDLE;
switch (type)
{
#if MINI_MSG_NUM > 0
case MINI_MSG_PACK:
OS_ENTER_CRITICAL();
result = q_enum(g_miniMsgFreeQ, (HANDLE *)&hmsg);
OS_LEAVE_CRITICAL();
if (result == OS_SUCCESS)
{
g_miniMsgCB[hmsg].state = OS_MSG_ALLOCATED;
g_miniMsgCB[hmsg].owner = taskIdSelf();
g_miniMsgCB[hmsg].times = 0;
hmsg += MINI_MSG_PACK<<14;
}
else
{
OS_error("msgAlloc(): mini msg packet exhausted!!!\n");
}
break;
#endif
#if COMMON_MSG_NUM > 0
case COMMON_MSG_PACK:
OS_ENTER_CRITICAL();
result = q_enum(g_commMsgFreeQ, (HANDLE *)&hmsg);
OS_LEAVE_CRITICAL();
if (result == OS_SUCCESS)
{
g_commMsgCB[hmsg].state = OS_MSG_ALLOCATED;
g_commMsgCB[hmsg].times = 0;
g_commMsgCB[hmsg].owner = taskIdSelf();
hmsg += COMMON_MSG_PACK<<14;
}
else
{
OS_error("msgAlloc(): mini msg packet exhausted!!!\n");
}
break;
#endif
#if HUGE_MSG_NUM > 0
case HUGE_MSG_PACK:
OS_ENTER_CRITICAL();
result = q_enum(g_hugeMsgFreeQ, (HANDLE *)&hmsg);
OS_LEAVE_CRITICAL();
if (result == OS_SUCCESS)
{
g_hugeMsgCB[hmsg].state = OS_MSG_ALLOCATED;
g_hugeMsgCB[hmsg].times = 0;
g_hugeMsgCB[hmsg].owner = taskIdSelf();
hmsg += HUGE_MSG_PACK<<14;
}
else
{
OS_error("msgAlloc(): mini msg packet exhausted!!!\n");
}
break;
#endif
default:
hmsg = NULL_HANDLE;
}
return hmsg;
}
/******************************************************************************
Function : void * msgMap (HMSG hmsg)
Params : hmsg - the msg.
:
:
:
Return : the message buffer pointer.
Description : This function should be called after the msgAlloc().
:
******************************************************************************/
void * msgMap (HMSG hmsg)
{
void * p;
U16 type;
HANDLE handle;
type = (hmsg & (0x3<<14))>>14; /* msg type */
handle = hmsg & (~(0x3<<14)); /* the realy handle. */
switch (type)
{
#if MINI_MSG_NUM > 0
case MINI_MSG_PACK:
if (handle >= MINI_MSG_NUM)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is invalid!!!\n", hmsg, type, handle);
p = NULL;
}
else if (g_miniMsgCB[handle].state == OS_MSG_FREE)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is free, can't maped!!!\n", hmsg, type, handle);
p = NULL;
}
else
{
p = g_miniMsgCB[handle].pBuffer;
}
break;
#endif
#if COMMON_MSG_NUM > 0
case COMMON_MSG_PACK:
if (handle >= COMMON_MSG_NUM)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is invalid!!!\n", hmsg, type, handle);
p = NULL;
}
else if (g_commMsgCB[handle].state == OS_MSG_FREE)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is free, can't maped!!!\n", hmsg, type, handle);
p = NULL;
}
else
{
p = g_commMsgCB[handle].pBuffer;
}
break;
#endif
#if HUGE_MSG_NUM > 0
case HUGE_MSG_PACK:
if (handle >= HUGE_MSG_NUM)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is invalid!!!\n", hmsg, type, handle);
p = NULL;
}
else if (g_hugeMsgCB[handle].state == OS_MSG_FREE)
{
OS_error ("msgMap(): msg handle [%d](type [%d] handle [%d]) is free, can't maped!!!\n", hmsg, type, handle);
p = NULL;
}
else
{
p = g_hugeMsgCB[handle].pBuffer;
}
break;
#endif
default:
p = NULL;
OS_error("msgMap(): msg handle [%d](type [%d] handle [%d]) is invalid!!!\n", hmsg, type, handle);
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -