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

📄 net_conn.c

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

    NetConn_ErrInvalidTypeCtr            =  0;
#endif
#endif
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                       NetConn_CfgAccessedTh()
*
* Description : Configure network connection accessed-promotion threshold.
*
* Argument(s) : nbr_access      Desired number of accesses before network connection is promoted.
*
* Return(s)   : DEF_OK, network connection promotion threshold configured.
*
* Caller(s)   : Net_InitDflt(),
*               Application.
*
*               This function is a network protocol suite application interface (API) function & MAY be 
*               called by application function(s).
*
* Note(s)     : none.
*********************************************************************************************************
*/

CPU_BOOLEAN  NetConn_CfgAccessedTh (CPU_INT16U  nbr_access)
{
#if (CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL)
    CPU_SR  cpu_sr;
#endif


#if (NET_CONN_ACCESSED_TH_MIN > DEF_INT_16U_MIN_VAL)
    if (nbr_access < NET_CONN_ACCESSED_TH_MIN) {
        nbr_access = NET_CONN_ACCESSED_TH_MIN;
    }
#endif
#if (NET_CONN_ACCESSED_TH_MAX < DEF_INT_16U_MAX_VAL)
    if (nbr_access > NET_CONN_ACCESSED_TH_MAX) {
        nbr_access = NET_CONN_ACCESSED_TH_MAX;
    }
#endif

    CPU_CRITICAL_ENTER();
    NetConn_AccessedTh_nbr = nbr_access;
    CPU_CRITICAL_EXIT();

    return (DEF_OK);
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                            NetConn_Get()
*
* Description : (1) Allocate & initialize a network connection :
*
*                   (a) Get      a network connection
*                   (b) Validate   network connection
*                   (c) Initialize network connection
*                   (d) Update     network connection pool statistics
*                   (e) Return network connection handle identifier
*                         OR
*                       Null identifier & error code, on failure
*
*               (2) The network connection pool is implemented as a stack :
*
*                   (a) 'NetConn_PoolPtr' points to the head of the network connection pool.
*
*                   (b) Connections' 'NextConnPtr's link each connection to form the connection pool stack.
*
*                   (c) Connections are inserted & removed at the head of        the connection pool stack.
*
*
*                                     Connections are
*                                    inserted & removed
*                                        at the head
*                                      (see Note #2c)
*
*                                             |               NextConnPtr
*                                             |             (see Note #2b)
*                                             v                    |
*                                                                  |
*                                          -------       -------   v   -------       -------
*                     Connection Pool ---->|     |------>|     |------>|     |------>|     |
*                         Pointer          |     |       |     |       |     |       |     |
*                                          |     |       |     |       |     |       |     |
*                      (see Note #2a)      -------       -------       -------       -------
*
*                                          |                                               |
*                                          |<--------- Pool of Free Connections ---------->|
*                                          |                (see Note #2)                  |
*
*
* Argument(s) : perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_CONN_ERR_NONE               Network connection successfully allocated &
*                                                                   initialized.
*                               NET_CONN_ERR_NONE_AVAIL         NO available network connections to allocate.
*                               NET_CONN_ERR_INVALID_FAMILY     Invalid connection family.
*                               NET_CONN_ERR_INVALID_TYPE       Invalid connection type.
*                               NET_CONN_ERR_INVALID_LIST_IX    Invalid connection list index.
*
* Return(s)   : Connection handle identifier, if NO errors.
*
*               NET_CONN_ID_NONE,             otherwise.
*
* Caller(s)   : NetSock_BindHandler(),
*               NetTCP_RxPktConnHandlerListen().
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : (3) (a) Network connection pool is accessed by 'NetConn_PoolPtr' during execution of
*
*                       (1) NetConn_Init()
*                       (2) NetConn_Get()
*                       (3) NetConn_FreeHandler()
*
*                   (b) Since the primary tasks of the network protocol suite are prevented from running 
*                       concurrently (see 'net.h  Note #2'), it is NOT necessary to protect the shared 
*                       resources of the network connection pool since no asynchronous access from other 
*                       network tasks is possible.
*********************************************************************************************************
*/
/*$PAGE*/
NET_CONN_ID  NetConn_Get (NET_CONN_FAMILY    family,
                          NET_CONN_LIST_IX   protocol_ix,
                          NET_ERR           *perr)
{
#if ((NET_CTR_CFG_ERR_EN      == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))
    CPU_SR        cpu_sr;
#endif
    NET_CONN     *pconn;
    NET_CONN_ID   conn_id;
    NET_ERR       stat_err;


                                                                /* ---------------- VALIDATE CONN ARGS ---------------- */
    switch (family) {
#if (NET_CONN_CFG_FAMILY == NET_CONN_FAMILY_IP_V4_SOCK)
        case NET_CONN_FAMILY_IP_V4_SOCK:
             switch (protocol_ix) {
                 case NET_CONN_LIST_IX_IP_V4_SOCK_UDP:
#ifdef  NET_TCP_MODULE_PRESENT
                 case NET_CONN_LIST_IX_IP_V4_SOCK_TCP:
#endif
                      break;


                 default:
                      NET_CTR_ERR_INC(NetConn_ErrInvalidListIxCtr);
                     *perr =  NET_CONN_ERR_INVALID_LIST_IX;
                      return (NET_CONN_ID_NONE);                /* Prevent 'break NOT reachable' compiler warning.      */
             }
             break;
#endif

        case NET_CONN_FAMILY_NONE:
        default:
             NET_CTR_ERR_INC(NetConn_ErrInvalidFamilyCtr);
            *perr =  NET_CONN_ERR_INVALID_FAMILY;
             return (NET_CONN_ID_NONE);                         /* Prevent 'break NOT reachable' compiler warning.      */
    }

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    if (protocol_ix >= NET_CONN_CFG_PROTOCOL_MAX) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidListIxCtr);
       *perr =  NET_CONN_ERR_INVALID_LIST_IX;
        return (NET_CONN_ID_NONE);
    }
#endif


                                                                /* ------------------- GET NET CONN ------------------- */
    if (NetConn_PoolPtr != (NET_CONN *)0) {                     /* If net conn pool NOT empty, get net conn from pool.  */
        pconn            = (NET_CONN *)NetConn_PoolPtr;
        NetConn_PoolPtr  = (NET_CONN *)pconn->NextConnPtr;

    } else {                                                    /* If none avail, rtn err.                              */
        NET_CTR_ERR_INC(NetConn_ErrNoneAvailCtr);
       *perr =  NET_CONN_ERR_NONE_AVAIL;
        return (NET_CONN_ID_NONE);
    }

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* ---------------- VALIDATE NET CONN ----------------- */
    if (pconn->Type != NET_CONN_TYPE_CONN) {
        NetConn_Discard(pconn);
        NET_CTR_ERR_INC(NetConn_ErrInvalidTypeCtr);
       *perr =  NET_CONN_ERR_INVALID_TYPE;
        return (NET_CONN_ID_NONE);
    }
#endif

                                                                /* ------------------ INIT NET CONN ------------------- */
    NetConn_Clr(pconn);
    DEF_BIT_SET(pconn->Flags, NET_CONN_FLAG_USED);              /* Set net conn as used.                                */
    pconn->Family         = family;
    pconn->ProtocolListIx = protocol_ix;

                                                                /* ------------ UPDATE NET CONN POOL STATS ------------ */
    NetStat_PoolEntryUsedInc(&NetConn_PoolStat, &stat_err);

                                                                /* ----------------- RTN NET CONN ID ------------------ */
    conn_id = pconn->ID;
   *perr    = NET_CONN_ERR_NONE;

    return (conn_id);
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                           NetConn_Free()
*
* Description : Free a network connection.
*
*               (1) Network connection free ONLY frees but does NOT close any connections.
*
*
* Argument(s) : conn_id     Handle identifier of network connection to free.
*
* Return(s)   : none.
*
* Caller(s)   : various.
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : (2) #### To prevent freeing a network connection already freed via previous network
*                   connection free, NetConn_Free() checks the connection's 'USED' flag BEFORE freeing
*                   the network connection.
*
*                   This prevention is only best-effort since any invalid duplicate network connection 
*                   frees MAY be asynchronous to potentially valid network connection gets.  Thus the 
*                   invalid network connection free(s) MAY corrupt the network connection's valid
*                   operation(s).
*
*                   However, since the primary tasks of the network protocol suite are prevented from
*                   running concurrently (see 'net.h  Note #2'), it is NOT necessary to protect network
*                   connection resources from possible corruption since no asynchronous access from
*                   other network tasks is possible.
*********************************************************************************************************
*/

void  NetConn_Free (NET_CONN_ID  conn_id)
{
#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)
    CPU_BOOLEAN   used;
#endif
    NET_CONN     *pconn;

                                                                /* ---------------- VALIDATE NET CONN ----------------- */
    if (conn_id == NET_CONN_ID_NONE) {
        return;
    }

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    if (conn_id < NET_CONN_ID_MIN) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
    if (conn_id > NET_CONN_ID_MAX) {
        NET_CTR_ERR_INC(NetConn_ErrInvalidConnCtr);
        return;
    }
#endif

    pconn = &NetConn_Tbl[conn_id];

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
                                                                /* -------------- VALIDATE NET CONN USED -------------- */
    used  =  DEF_BIT_IS_SET(pconn->Flags, NET_CONN_FLAG_USED);
    if (used != DEF_YES) {                                      /* If net conn NOT used, ...                            */
        NET_CTR_ERR_INC(NetConn_ErrNotUsedCtr);
        return;                                                 /* ... rtn but do NOT free (see Note #2).               */
    }
#endif

                                                                /* ------------------ FREE NET CONN ------------------- */
    NetConn_FreeHandler(pconn);
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                       NetConn_CloseFromApp()
*
* Description : (1) Close a network connection from application layer :
*

⌨️ 快捷键说明

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