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

📄 net_buf.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 5 页
字号:
        pbuf_list = pbuf_list_next;
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                           NetBuf_DataRd()
*
* Description : (1) Read data from network buffer's DATA area :
*
*                   (a) Validate data read index & size
*                   (b) Read     data from buffer
*
*
* Argument(s) : pbuf        Pointer to a network buffer.
*
*               ix          Index into buffer's DATA area.
*
*               len         Number of octets to read (see Note #2).
*
*               pdest       Pointer to destination to read data into (see Note #3).
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_BUF_ERR_NONE                Read from DATA area successful.
*                               NET_BUF_ERR_NULL_PTR            Argument 'pbuf'/'pdest' passed a NULL pointer.
*                               NET_BUF_ERR_INVALID_TYPE        Argument 'pbuf's TYPE is invalid or unknown.
*                               NET_BUF_ERR_INVALID_IX          Invalid index  (outside buffer's DATA area).
*                               NET_BUF_ERR_INVALID_LEN         Invalid length (outside buffer's DATA area).
*
* Return(s)   : none.
*
* Caller(s)   : various.
*
*               This function is an INTERNAL network protocol suite function & SHOULD NOT be called by 
*               application function(s).
*
* Note(s)     : (2) Data read of 0 octets allowed.
*
*               (3) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
*               (4) 'ix' & 'len' argument check NOT required unless 'NET_BUF_SIZE's native data type 
*                   'CPU_INT16U' is incorrectly configured as a signed integer in 'cpu.h'.
*
*               (5) Buffer 'Size' is NOT re-validated; validated in NetBuf_Get().
*********************************************************************************************************
*/
/*$PAGE*/
void  NetBuf_DataRd (NET_BUF       *pbuf,
                     NET_BUF_SIZE   ix,
                     NET_BUF_SIZE   len,
                     CPU_INT08U    *pdest,
                     NET_ERR       *perr)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)                    && \
     (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD    == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR         cpu_sr;
#endif
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    NET_BUF_HDR   *pbuf_hdr;
    NET_BUF_SIZE   len_data;
#endif
    CPU_INT08U    *p_data;


#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* ------------------- VALIDATE PTR ------------------- */
    if (pbuf == (NET_BUF *)0) {
        NET_CTR_ERR_INC(NetBuf_ErrNullPtrCtr);
       *perr = NET_BUF_ERR_NULL_PTR;
        return;
    }
                                                                /* ------------------- VALIDATE TYPE ------------------ */
    pbuf_hdr = &pbuf->Hdr;
    switch (pbuf_hdr->Type) {
#if (NET_BUF_CFG_NBR_SMALL > 0)
        case NET_BUF_TYPE_SMALL:
#endif
#if (NET_BUF_CFG_NBR_LARGE > 0)
        case NET_BUF_TYPE_LARGE:
#endif
             break;


        case NET_BUF_TYPE_NONE:
        default:
             NET_CTR_ERR_INC(NetBuf_ErrInvalidTypeCtr);
            *perr = NET_BUF_ERR_INVALID_TYPE;
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */
    }
#endif

/*$PAGE*/
                                                                /* ----------------- VALIDATE IX/SIZE ----------------- */
#if 0                                                           /* See Note #4.                                         */
    if (ix < 0) {                                               /* If req'd ix  < 0,    rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrIxCtr);
       *perr = NET_BUF_ERR_INVALID_IX;
        return;
    }

    if (len < 0) {                                              /* If req'd len < 0,    rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrLenCtr);
       *perr = NET_BUF_ERR_INVALID_LEN;
        return;
    }
#endif
    if (len == 0) {                                             /* If req'd len = 0,    rtn null rd (see Note #2).      */
       *perr = NET_BUF_ERR_NONE;
        return;
    }


#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* ---------------- VALIDATE DEST PTR ----------------- */
    if (pdest == (CPU_INT08U *)0) {
        NET_CTR_ERR_INC(NetBuf_ErrNullPtrCtr);
       *perr = NET_BUF_ERR_NULL_PTR;
        return;
    }

                                                                /* ----------------- VALIDATE IX/SIZE ----------------- */
    if (ix >= pbuf_hdr->Size) {                                 /* If req'd ix  > size, rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrIxCtr);
       *perr = NET_BUF_ERR_INVALID_IX;
        return;
    }

    len_data = ix + len;
    if (len_data > pbuf_hdr->Size) {                            /* If req'd len > size, rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrLenCtr);
       *perr = NET_BUF_ERR_INVALID_LEN;
        return;
    }
#endif
                                                                /* ------------------- RD BUF DATA -------------------- */
                                                                /* Req'd ix & len within  buf DATA area, ...            */
    p_data = &pbuf->Data[ix];                                   /* ... set ptr to ix into buf DATA area, ...            */
    Mem_Copy((void     *)pdest,                                 /* ... & copy len nbr DATA buf octets to dest.          */
             (void     *)p_data,
             (CPU_SIZE_T)len);

   *perr = NET_BUF_ERR_NONE;
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                           NetBuf_DataWr()
*
* Description : (1) Write data into network buffer's DATA area :
*
*                   (a) Validate data write index & size
*                   (b) Write    data into buffer
*
*
* Argument(s) : pbuf        Pointer to a network buffer.
*
*               ix          Index into buffer's DATA area.
*
*               len         Number of octets to write (see Note #2).
*
*               psrc        Pointer to data to write.
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_BUF_ERR_NONE                Write to DATA area successful.
*                               NET_BUF_ERR_NULL_PTR            Argument 'pbuf'/'psrc' passed a NULL pointer.
*                               NET_BUF_ERR_INVALID_TYPE        Argument 'pbuf's TYPE is invalid or unknown.
*                               NET_BUF_ERR_INVALID_IX          Invalid index  (outside buffer's DATA area).
*                               NET_BUF_ERR_INVALID_LEN         Invalid length (outside buffer's DATA area).
*
* Return(s)   : none.
*
* Caller(s)   : various.
*
*               This function is an INTERNAL network protocol suite function & SHOULD NOT be called by 
*               application function(s).
*
* Note(s)     : (2) Data write of 0 octets allowed.
*
*               (3) 'ix' & 'len' argument check NOT required unless 'NET_BUF_SIZE's native data type 
*                   'CPU_INT16U' is incorrectly configured as a signed integer in 'cpu.h'.
*
*               (4) Buffer 'Size' is NOT re-validated; validated in NetBuf_Get().
*********************************************************************************************************
*/
/*$PAGE*/
void  NetBuf_DataWr (NET_BUF       *pbuf,
                     NET_BUF_SIZE   ix,
                     NET_BUF_SIZE   len,
                     CPU_INT08U    *psrc,
                     NET_ERR       *perr)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)                    && \
     (NET_CTR_CFG_ERR_EN         == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD    == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR         cpu_sr;
#endif
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    NET_BUF_HDR   *pbuf_hdr;
    NET_BUF_SIZE   len_data;
#endif
    CPU_INT08U    *p_data;


#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* ------------------- VALIDATE PTR ------------------- */
    if (pbuf == (NET_BUF *)0) {
        NET_CTR_ERR_INC(NetBuf_ErrNullPtrCtr);
       *perr = NET_BUF_ERR_NULL_PTR;
        return;
    }
                                                                /* ------------------- VALIDATE TYPE ------------------ */
    pbuf_hdr = &pbuf->Hdr;
    switch (pbuf_hdr->Type) {
#if (NET_BUF_CFG_NBR_SMALL > 0)
        case NET_BUF_TYPE_SMALL:
#endif
#if (NET_BUF_CFG_NBR_LARGE > 0)
        case NET_BUF_TYPE_LARGE:
#endif
             break;


        case NET_BUF_TYPE_NONE:
        default:
             NET_CTR_ERR_INC(NetBuf_ErrInvalidTypeCtr);
            *perr = NET_BUF_ERR_INVALID_TYPE;
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */
    }
#endif

/*$PAGE*/
                                                                /* ----------------- VALIDATE IX/SIZE ----------------- */
#if 0                                                           /* See Note #3.                                         */
    if (ix < 0) {                                               /* If req'd ix  < 0,    rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrIxCtr);
       *perr = NET_BUF_ERR_INVALID_IX;
        return;
    }

    if (len < 0) {                                              /* If req'd len < 0,    rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrLenCtr);
       *perr = NET_BUF_ERR_INVALID_LEN;
        return;
    }
#endif
    if (len == 0) {                                             /* If req'd len = 0,    rtn null wr (see Note #2).      */
       *perr = NET_BUF_ERR_NONE;
        return;
    }


#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* ----------------- VALIDATE SRC PTR ----------------- */
    if (psrc == (CPU_INT08U *)0) {
        NET_CTR_ERR_INC(NetBuf_ErrNullPtrCtr);
       *perr = NET_BUF_ERR_NULL_PTR;
        return;
    }

                                                                /* ----------------- VALIDATE IX/SIZE ----------------- */
    if (ix >= pbuf_hdr->Size) {                                 /* If req'd ix  > size, rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrIxCtr);
       *perr = NET_BUF_ERR_INVALID_IX;
        return;
    }

    len_data = ix + len;
    if (len_data > pbuf_hdr->Size) {                            /* If req'd len > size, rtn err.                        */
        NET_CTR_ERR_INC(NetBuf_ErrLenCtr);
       *perr = NET_BUF_ERR_INVALID_LEN;
        return;
    }
#endif
 

⌨️ 快捷键说明

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