⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg.c

📁 还是arm7操作源码
💻 C
📖 第 1 页 / 共 2 页
字号:

    }
    return p;
}

/******************************************************************************
Function    : STATUS msgFree (HMSG hmsg)
Params      : hmsg - the message.
            : 
            : 
            : 
Return      : error code.
Description : When a message is process by task, and it is not resend, the task
            : should call this function to free the message.
            : Or when a message is allocated but needn't be sent, the task
            : should call this function to free the message too.
******************************************************************************/
STATUS msgFree (HMSG hmsg)
{
    STATUS nret;

    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 ("msgFree(): invalid msg handle, hmsg = [%d], type [%d] handle [%d]!!!\n", hmsg, type, handle);
            nret = OS_FAIL;
        }
        else if (g_miniMsgCB[handle].state == OS_MSG_FREE)
        {
            OS_error ("msgFree(): msg handle [%d](type [%d] handle [%d]) is free, can't freed!!!\n", hmsg, type, handle);
            nret = OS_SUCCESS;
        }
        else
        {
            g_miniMsgCB[handle].state = OS_MSG_FREE;
            g_miniMsgCB[handle].times = 0;
            g_miniMsgCB[handle].owner = NULL_TASK;

            OS_ENTER_CRITICAL();
            q_add(g_miniMsgFreeQ, (HANDLE)handle, 0);
            OS_LEAVE_CRITICAL();
            nret = OS_SUCCESS;
        }
        break;
#endif

#if COMMON_MSG_NUM > 0
    case COMMON_MSG_PACK:
        if (handle >= COMMON_MSG_NUM)
        {
            OS_error ("msgFree(): invalid msg handle, hmsg = [%d], type [%d] handle [%d]!!!\n", hmsg, type, handle);
            nret = OS_FAIL;
        }
        else if (g_commMsgCB[handle].state == OS_MSG_FREE)
        {
            OS_error ("msgFree(): msg handle [%d](type [%d] handle [%d]) is free, can't freed!!!\n", hmsg, type, handle);
            nret = OS_SUCCESS;
        }
        else
        {
            g_commMsgCB[handle].state = OS_MSG_FREE;
            g_commMsgCB[handle].times = 0;
            g_commMsgCB[handle].owner = NULL_TASK;

            OS_ENTER_CRITICAL();
            q_add(g_commMsgFreeQ, (HANDLE)handle, 0);
            OS_LEAVE_CRITICAL();
            nret = OS_SUCCESS;
        }
        break;
#endif

#if HUGE_MSG_NUM > 0
    case HUGE_MSG_PACK:
        if (handle >= HUGE_MSG_NUM)
        {
            OS_error ("msgFree(): invalid msg handle, hmsg = [%d], type [%d] handle [%d]!!!\n", hmsg, type, handle);
            nret = OS_FAIL;
        }
        else if (g_hugeMsgCB[handle].state == OS_MSG_FREE)
        {
            OS_error ("msgFree(): msg handle [%d](type [%d] handle [%d]) is free, can't freed!!!\n", hmsg, type, handle);
            nret = OS_SUCCESS;
        }
        else
        {
            g_hugeMsgCB[handle].state = OS_MSG_FREE;
            g_hugeMsgCB[handle].times = 0;
            g_hugeMsgCB[handle].owner = NULL_TASK;

            OS_ENTER_CRITICAL();
            q_add(g_hugeMsgFreeQ, (HANDLE)handle, 0);
            OS_LEAVE_CRITICAL();
            nret = OS_SUCCESS;
        }
        break;
#endif
    default:
        OS_error ("msgFree(): invalid msg handle, hmsg = [%d], type [%d] handle [%d]!!!\n", hmsg, type, handle);
        nret = OS_FAIL;
        break;
    }
    return nret;
}

/******************************************************************************
Function    : STATUS msgSend(HMSG hmsg, HTASK task, U32 timeout)
Params      : hmsg - the msg.
            : task - the dest task (to receive the msg).
            : timeout - the waiting ticks if task is pending on sending msg.
            : 
Return      : OS_SUCCESS or OS_FAIL
Description : this function is same as msgQSend(), see msgQSend() for more.
            : 
******************************************************************************/
STATUS msgSend(HMSG hmsg, HTASK task, U32 timeout)
{
    return msgQSend(hmsg, task, timeout);
}

/******************************************************************************
Function    : void msgCheck()
Params      : N/A
            : 
            : 
            : 
Return      : N/A
Description : This function is execute periodly for message packet check. It
            : renew the unknown or error state messages and add them to
            : free queue.
******************************************************************************/
void msgCheck()
{
    HMSG hmsg;

#if MINI_MSG_NUM > 0
    for (hmsg = 0; hmsg < MINI_MSG_NUM; hmsg++)
    {
        if ((g_miniMsgCB[hmsg].state == OS_MSG_ALLOCATED)
             ||(g_miniMsgCB[hmsg].state == OS_MSG_RECEIVED))
        {
            g_miniMsgCB[hmsg].times++;

            /* if this msg has been allocated for a long time, free it forcely. */
            if (g_miniMsgCB[hmsg].times >= 2)
            {
                OS_error("msgCheck(): mini msg [0x%x] waiting for a long time, owner task [%d], state [%d]!!!\n",
                            hmsg + (MINI_MSG_PACK<<14), g_miniMsgCB[hmsg].owner, g_miniMsgCB[hmsg].state);
                msgDump(hmsg + (MINI_MSG_PACK<<14));

                g_miniMsgCB[hmsg].owner = NULL_TASK;
                g_miniMsgCB[hmsg].times = 0;
                g_miniMsgCB[hmsg].state = OS_MSG_FREE;

                q_add(g_miniMsgFreeQ, (HANDLE)hmsg, 0);
            }
        }
    }
#endif

#if COMMON_MSG_NUM > 0
    for (hmsg = 0; hmsg < COMMON_MSG_NUM; hmsg++)
    {
        if ((g_commMsgCB[hmsg].state == OS_MSG_ALLOCATED)
             ||(g_commMsgCB[hmsg].state == OS_MSG_RECEIVED))
        {
            g_commMsgCB[hmsg].times++;

            /* if this msg has been allocated for a long time, free it forcely. */
            if (g_commMsgCB[hmsg].times >= 2)
            {
                OS_error("msgCheck(): common msg [0x%x] waiting for a long time, owner task [%d], state [%d]!!!\n",
                            hmsg + (COMMON_MSG_PACK<<14), g_commMsgCB[hmsg].owner, g_commMsgCB[hmsg].state);
                msgDump(hmsg + (COMMON_MSG_PACK<<14));

                g_commMsgCB[hmsg].owner = NULL_TASK;
                g_commMsgCB[hmsg].times = 0;
                g_commMsgCB[hmsg].state = OS_MSG_FREE;

                q_add(g_commMsgFreeQ, (HANDLE)hmsg, 0);
            }
        }
    }
#endif

#if HUGE_MSG_NUM > 0
    for (hmsg = 0; hmsg < HUGE_MSG_NUM; hmsg++)
    {
        if ((g_hugeMsgCB[hmsg].state == OS_MSG_ALLOCATED)
             ||(g_hugeMsgCB[hmsg].state == OS_MSG_RECEIVED))
        {
            g_hugeMsgCB[hmsg].times++;

            /* if this msg has been allocated for a long time, free it forcely. */
            if (g_hugeMsgCB[hmsg].times >= 2)
            {
                OS_error("msgCheck(): mini msg [0x%x] waiting for a long time, owner task [%d], state [%d]!!!\n",
                            hmsg + (HUGE_MSG_PACK<<14), g_hugeMsgCB[hmsg].owner, g_hugeMsgCB[hmsg].state);
                msgDump(hmsg + (HUGE_MSG_PACK<<14));

                g_hugeMsgCB[hmsg].owner = NULL_TASK;
                g_hugeMsgCB[hmsg].times = 0;
                g_hugeMsgCB[hmsg].state = OS_MSG_FREE;

                q_add(g_hugeMsgFreeQ, (HANDLE)hmsg, 0);
            }
        }
    }
#endif

}

/******************************************************************************
Function    : void msgDump(HMSG hmsg)
Params      : hmsg - the msg.
            : 
            : 
            : 
Return      : N/A
Description : This function is used for dumping the message contents, if the
            : the msg is longer than DEFAULT_DUMP_MSG_LEN, default dumping 
            : DEFAULT_DUMP_MSG_LEN bytes, else dumping the whole msg body.
******************************************************************************/
void msgDump(HMSG hmsg)
{
    U16 type;
    HANDLE handle;

    U8 * pmsg;

    U32 i, offset;
    U32 dump_len;

    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 MINI_MSG_LEN < DEFAULT_DUMP_MSG_LEN
            dump_len = MINI_MSG_LEN;
#else
            dump_len = DEFAULT_DUMP_MSG_LEN;
#endif
        break;
#endif

#if COMMON_MSG_NUM > 0
    case COMMON_MSG_PACK:
#if COMMON_MSG_LEN < DEFAULT_DUMP_MSG_LEN
            dump_len = COMMON_MSG_LEN;
#else
            dump_len = DEFAULT_DUMP_MSG_LEN;
#endif
        break;
#endif

#if HUGE_MSG_NUM > 0
    case HUGE_MSG_PACK:
#if HUGE_MSG_LEN < DEFAULT_DUMP_MSG_LEN
            dump_len = HUGE_MSG_LEN;
#else
            dump_len = DEFAULT_DUMP_MSG_LEN;
#endif
        break;
#endif
    default:
        OS_error("msgDump(): msg handle [%d](type [%d] handle [%d]) is invalid!!!\n", hmsg, type, handle);
        dump_len = 0;
        break;
    }

    OS_printf("msg contents dump[hmsg = 0x%x]:", hmsg);
    pmsg = (U8 *)msgMap(hmsg);
    if (pmsg == NULL)
    {
        return;
    }

    for (i = 0, offset = 0; i < dump_len; i++,offset++)
    {
        if ((offset%16)==0)
        {
            OS_printf("\n0x%08x: ", offset);
        }
        else if ((offset%8)==0)
        {
            OS_printf("- ");
        }
        OS_printf("%02x ", pmsg[i]);
    }
    OS_printf("\n");
}

/******************************************************************************
Function    : STATUS setmsgState(HMSG hmsg, U16 state)
Params      : hmsg  - the msg packet
            : state - the msg packet's new state
            : 
            : 
Return      : OS_SUCCESS, OS_FAIL if hmsg is invlaid.
Description : change the msg packet's state.
            : 
******************************************************************************/
STATUS setmsgState(HMSG hmsg, U16 state)
{
    U16 type;
    HANDLE handle;

    if ((state != OS_MSG_FREE)
        &&(state != OS_MSG_ALLOCATED)
        &&(state != OS_MSG_SENT)
        &&(state != OS_MSG_RECEIVED))
    {
        OS_error("setMsgState(): msg state [%d] is error!!!\n", state);
        return OS_FAIL;
    }

    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:
        g_miniMsgCB[handle].state = state;
        break;
#endif


#if COMMON_MSG_NUM > 0
    case COMMON_MSG_PACK:
        g_commMsgCB[handle].state = state;
        break;
#endif

#if HUGE_MSG_NUM > 0
    case HUGE_MSG_PACK:
        g_hugeMsgCB[handle].state = state;
        break;
#endif
    default:
        break;
    }
    return OS_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -