probe_tcpip.c

来自「嵌入式的tcpip协议栈」· C语言 代码 · 共 299 行

C
299
字号
/*
*********************************************************************************************************
*                                      uC/Probe Communication
*
*                           (c) Copyright 2007; Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*               Knowledge of the source code may NOT be used to develop a similar product.
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                              uC/Probe
*
*                                      Communication: TCP-IP
*
* Filename      : probe_tcpip.c
* Version       : V1.40
* Programmer(s) : FBJ
* Note(s)       : (1) The abbreviations RX and TX refer to communication from the target's perspective.
*
*                 (2) The abbreviations RD and WR refer to reading data from the target memory and
*                     writing data to the target memory, respectively.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/

#define   PROBE_TCPIP_GLOBALS
#include  <probe_com.h>
#include  <probe_tcpip.h>
#include  <net.h>

#if (PROBE_COM_METHOD_TCPIP > 0)

/*
*********************************************************************************************************
*                                           LOCAL CONSTANTS
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                          LOCAL DATA TYPES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                            LOCAL TABLES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                       LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/


static  NET_SOCK_ID          ProbeTCPIP_ConnSockID;                     /* Socket ID of the connected or accepted socket    */

static  CPU_CHAR             ProbeTCPIP_TxBuf[PROBE_TCPIP_TX_BUF_SIZE]; /* Buffer used to send UDP packets                  */
static  CPU_CHAR             ProbeTCPIP_RxBuf[PROBE_TCPIP_RX_BUF_SIZE]; /* Buffer used to recv UDP packets                  */

static  struct  sockaddr_in  ProbeTCPIP_RemoteSockAddr;                 /* Structure containing info about the remote host  */
static  CPU_INT32U           ProbeTCPIP_RemoteLen;                      /* Size of the structure                            */


/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  CPU_INT16U  ProbeTCPIP_ParseRxPkt(CPU_INT16S  rx_len);
static  void        ProbeTCPIP_ServerInit(void);
static  CPU_INT16S  ProbeTCPIP_RxPkt     (void);
static  CPU_INT16S  ProbeTCPIP_TxStart   (CPU_INT16S  tx_len);


/*
*********************************************************************************************************
*                                     LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*********************************************************************************************************
**                                          Global Functions
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            ProbeTCPIP_Init()
*
* Description : Initializes the uC/Probe TCP-IP communication module.
*
* Argument(s) :  none.
*
* Return(s)   :  none.
*********************************************************************************************************
*/

void  ProbeTCPIP_Init (void)
{
    ProbeTCPIP_OS_Init();
}


/*
*********************************************************************************************************
*                                              ProbeTCPIP_Task()
*
* Description : The server task for the uC/Probe TCP-IP communication module.
*
* Argument(s) : p_arg       Argument passed to 'ProbeTCPIP_Task()' by 'ProbeTCPIP_OS_Task()'.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  ProbeTCPIP_Task (void *p_arg)
{
    CPU_INT16S  len;


    (void)p_arg;

    ProbeTCPIP_ServerInit();

    while(DEF_TRUE)  {

        len = ProbeTCPIP_RxPkt();

        if (len != NET_BSD_ERR_DFLT) {

            len = ProbeTCPIP_ParseRxPkt(len);                           /* Parse packet and formulate a response                */

            if (len > 0) {
                ProbeTCPIP_TxStart(len);
            }
        }
    }
}


/*
*********************************************************************************************************
*********************************************************************************************************
**                                          Local Functions
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                         ProbeTCPIP_ServerInit()
*
* Description : Initializes the UDP server.
*
* Argument(s) : none.
*
* Return(s)   : The socket identifier or (if an error occurred) -1.
*********************************************************************************************************
*/

static  void    ProbeTCPIP_ServerInit  (void)
{
    CPU_INT32S           bind_status;
    NET_ERR              err;
    struct  sockaddr_in  sock_addr;


    ProbeTCPIP_ConnSockID     = socket(AF_INET,
                                       SOCK_DGRAM,
                                       IPPROTO_UDP);

                                                                    /* Bind a local address so the client can send to us.   */
    Mem_Set((void     *)&sock_addr,
            (CPU_INT08U)0,
            (CPU_SIZE_T)NET_SOCK_ADDR_SIZE);

    sock_addr.sin_family      = AF_INET;
    sock_addr.sin_port        = htons(PROBE_TCPIP_PORT);
    sock_addr.sin_addr.s_addr = htonl(NetIP_AddrThisHost);

    bind_status               = bind((int              )ProbeTCPIP_ConnSockID,
                                     (struct sockaddr *)&sock_addr,
                                     (int              )NET_SOCK_ADDR_SIZE);

    if (bind_status != NET_SOCK_BSD_ERR_NONE) {
        close((int)ProbeTCPIP_ConnSockID);                          /* Could not bind to the port.                          */
    }

    NetSock_CfgTimeoutRxQ_Set(ProbeTCPIP_ConnSockID, NET_TMR_TIME_INFINITE, &err);

    ProbeTCPIP_RemoteLen      = sizeof(ProbeTCPIP_RemoteSockAddr);

}

/*
*********************************************************************************************************
*                                         ProbeTCPIP_RxPkt()
*
* Description : Receives the data from uC/Probe via a UDP socket.
*
* Argument(s) : none.
*
* Return(s)   : The length of the received packet or (if an error occurred) -1.
*********************************************************************************************************
*/

static  CPU_INT16S  ProbeTCPIP_RxPkt  (void)
{
    CPU_INT16S  len;


    len = recvfrom((int              )ProbeTCPIP_ConnSockID,
                   (void            *)ProbeTCPIP_RxBuf,
                   (int              )PROBE_TCPIP_RX_BUF_SIZE,
                   (int              )NET_SOCK_FLAG_NONE,
                   (struct sockaddr *)&ProbeTCPIP_RemoteSockAddr,
                   (int             *)&ProbeTCPIP_RemoteLen);

    return (len);
}


/*
*********************************************************************************************************
*                                       ProbeTCPIP_TxStart()
*
* Description : Sends a reply packet to uC/Probe.
*
* Argument(s) : tx_len       Length of the transmit packet.
*
* Return(s)   : The length of the received packet or (if an error occurred) -1.
*********************************************************************************************************
*/

static  CPU_INT16S  ProbeTCPIP_TxStart (CPU_INT16S  tx_len)
{
    CPU_INT16S  len;


    len = sendto((int              )ProbeTCPIP_ConnSockID,
                 (void            *)ProbeTCPIP_TxBuf,
                 (int              )tx_len,
                 (int              )NET_SOCK_FLAG_NONE,
                 (struct sockaddr *)&ProbeTCPIP_RemoteSockAddr,
                 (int              )ProbeTCPIP_RemoteLen);

    return (len);
}


/*
*********************************************************************************************************
*                                         ProbeTCPIP_ParseRxPkt()
*
* Description : Parses received packet & forms respose packet.
*
* Argument(s) : rx_len      Length of the received packet.
*
* Return(S)   : The number of bytes in the data segment of the packet to transmit in response.
*********************************************************************************************************
*/

static  CPU_INT16U  ProbeTCPIP_ParseRxPkt (CPU_INT16S  rx_len)
{
    CPU_INT16U  len;


    len = ProbeCom_ParseRxPkt((void     *)ProbeTCPIP_RxBuf,
                              (void     *)ProbeTCPIP_TxBuf,
                              (CPU_INT16U)rx_len,
                              (CPU_INT16U)PROBE_TCPIP_TX_BUF_SIZE);

    return (len);
}

#endif

⌨️ 快捷键说明

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