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

📄 probe_com.c

📁 uCOS-II V2.84 LM3S6965 TCPIP Demo
💻 C
📖 第 1 页 / 共 5 页
字号:
*
* Returns    : The number of bytes in the data segment of the packet to transmit in response.
*********************************************************************************************************
*/

CPU_INT16U  ProbeCom_ParseRxPkt (void *rx_pkt, void *tx_pkt, CPU_INT16U rx_pkt_sz, CPU_INT16U  tx_buf_sz)
{
    CPU_INT16U   tx_buf_wr;
    CPU_INT16U   format;
    CPU_INT08U  *rx_buf;
    CPU_INT08U  *tx_buf;


    rx_buf  = (CPU_INT08U *)rx_pkt;
    tx_buf  = (CPU_INT08U *)tx_pkt;
    format  = (rx_buf[1] << 8) + rx_buf[0];
    rx_buf += 2;

#if (PROBE_COM_STAT_EN == DEF_ENABLED)
    ProbeCom_RxPktCtr++;
    ProbeCom_TxPktCtr++;
#endif

    switch (format) {
        case PROBE_COM_FMT_RX_QUERY:
             tx_buf_wr = ProbeCom_CmdQuery(     rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;

        case PROBE_COM_FMT_RX_SIMPLE_RD:
             tx_buf_wr = ProbeCom_CmdSimpleRd(  rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;

#if (PROBE_COM_SUPPORT_WR == DEF_TRUE)
        case PROBE_COM_FMT_RX_SIMPLE_WR:
             tx_buf_wr = ProbeCom_CmdSimpleWr(  rx_buf, tx_buf, rx_pkt_sz, tx_buf_sz);
             break;
#endif

        case PROBE_COM_FMT_RX_MULTIPLE_RD:
             tx_buf_wr = ProbeCom_CmdMultipleRd(rx_buf, tx_buf,  rx_pkt_sz, tx_buf_sz);
             break;

#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
        case PROBE_COM_FMT_RX_STR_GET:
             tx_buf_wr = ProbeCom_CmdStrGet(    rx_buf, tx_buf,  rx_pkt_sz, tx_buf_sz);
             break;
#endif

        default:
             tx_buf_wr = ProbeCom_CmdError(tx_buf, PROBE_COM_STATUS_UNKNOWN_REQUEST);
             break;
    }

    return (tx_buf_wr);
}


/*
*********************************************************************************************************
*                                    Queue String for Transmission
*
* Description: This routine is called to append a string in the string buffer.
*
* Argument(s): s          is a pointer to the string to send.
*
*              dly        allows the calling task to delay for a certain number of milliseconds until
*                         the entire string has been queued in the buffer.  If this value is zero, then
*                         the function will return after queueing in the buffer the portion that fits
*                         immediately.
*
* Returns    : DEF_TRUE   if the entire string was queued in the buffer.
*              DEF_FALSE  if the entire string could not be queued in the buffer.
*
* Note(s)    : (1) The string buffer is implemented as a circular buffer.  This function is one of two
*                  points of access for this buffer, the other being in the task or ISR which forms the .
*                  tx packets.  Only this function should modify the global current write index
*                  (ProbeComStrBufWrIx); only the task or ISR which forms the packets should modify the
*                  global current read index (ProbeComStrBufRdIx).
*
*              (2) The global current write index (ProbeComStrBufWrIx) is the index of the next location
*                  in the buffer to write.  The global current read index (ProbeComStrBufRdIx) is the
*                  index of the next location in the buffer to read.
*
*              (3) The string buffer, an array of PROBE_COM_STR_BUF_SIZE bytes, can only hold
*                  (PROBE_COM_STR_BUF_SIZE - 1) bytes so that the condition
*
*                                        ProbeComStrBufWrIx == ProbeComStrBufRdIx
*
*                  will be true if and only if the buffer is empty.  Consequently, this function
*                  always leaves an empty space in the buffer.
*
*              (4) If called from an ISR, dly MUST be 0.
*********************************************************************************************************
*/

#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
CPU_BOOLEAN  ProbeCom_TxStr (CPU_CHAR *s, CPU_INT16U dly)
{
    CPU_BOOLEAN  ret;
    CPU_INT32U   len;
    CPU_INT32U   wr_ix;
    CPU_INT32U   rd_ix;
    CPU_INT32U   wr_ix_n;

    CPU_INT32U   nbytes_free;
    CPU_INT32U   nbytes_wr;


    if (dly == 0) {
        ret = ProbeCom_OS_Pend(DEF_FALSE);
    } else {
        ret = ProbeCom_OS_Pend(DEF_TRUE);
    }

    if (ret == DEF_FALSE) {
        return (DEF_FALSE);
    }

    len = (CPU_INT32U)Str_Len(s);                                       /* Determine length of the string (without NULL byte*/

    while (DEF_TRUE) {
        if (len == 0) {                                                 /* If entire string has been placed in buffer       */
            ProbeCom_OS_Post();
            return (DEF_TRUE);                                          /* ... Return DEF_TRUE to indicate success.         */
        }

        rd_ix = ProbeComStrBufRdIx;
        wr_ix = ProbeComStrBufWrIx;

        if (rd_ix > wr_ix) {                                            /* If rd_ix > wr_ix, store string into              */
                                                                        /*           buffer locations [wr_ix, rd_ix - 1)    */
            nbytes_free = rd_ix - wr_ix - 1;

        } else {
            if (rd_ix == 0) {                                           /* If rd_ix <= wr_ix && rd_ix == 0, store string at */
                                                                        /*           buffer locations [wr_ix, end_ix - 1)   */
                nbytes_free = PROBE_COM_STR_BUF_SIZE - wr_ix - 1;

            } else {                                                    /* If rd_ix <= wr_ix && rd_ix != 0, store string at */
                                                                        /*           buffer locations [wr_ix, end_ix)       */

                nbytes_free = PROBE_COM_STR_BUF_SIZE - wr_ix;
            }
        }

        if (nbytes_free == 0) {                                         /* If the buffer is full                            */
            if (dly == 0) {                                             /* (a) Return if dly = 0                            */
                ProbeCom_OS_Post();
                return (DEF_FALSE);
            } else {                                                    /* (b) Call OS function to delay and continue       */
                ProbeCom_OS_Dly(dly);
                continue;
            }
        }

        if (nbytes_free > len) {                                        /* If string is shorter than free space             */
            nbytes_wr = len;
        } else {
            nbytes_wr = nbytes_free;
        }

        wr_ix_n = wr_ix + nbytes_wr;                                    /* Assign write index after write                   */

        if (wr_ix_n == PROBE_COM_STR_BUF_SIZE) {                        /* Wrap buffer index around                         */
            wr_ix_n = 0;
        }

        Mem_Copy(&ProbeComStrBuf[wr_ix], s, nbytes_wr);                 /* Copy string to buffer                            */

        ProbeComStrBufWrIx  = wr_ix_n;                                  /* Assign new global write index                    */
        s                  += nbytes_wr;                                /* Increase string pointer                          */
        len                -= nbytes_wr;                                /* Decrease string length                           */
    }
}
#endif


/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                  Static String-Handling Function
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                    Check if a String Is Ready
*
* Description: This routine is called to check if a string is ready for transmission.
*
* Argument(s): None
*
* Returns    : DEF_TRUE   if a  string is in the buffer for transmission.
*              DEF_FALSE  if no string is in the buffer for transmission.
*
* Note(s)    : (1) See Notes for ProbeCom_TxStr().
*********************************************************************************************************
*/

#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)
static  CPU_BOOLEAN  ProbeCom_StrRdy (void)
{
    CPU_BOOLEAN  rdy;
    CPU_INT32U   wr_ix;
    CPU_INT32U   rd_ix;


    wr_ix = ProbeComStrBufWrIx;
    rd_ix = ProbeComStrBufRdIx;

    if (wr_ix == rd_ix) {
        rdy = DEF_FALSE;
    } else {
        rdy = DEF_TRUE;
    }

    return (rdy);
}
#endif


/*
*********************************************************************************************************
*********************************************************************************************************
**                                  Static Packet-Handling Functions
*********************************************************************************************************
*********************************************************************************************************
*/

static  CPU_INT08U  ProbeCom_PktModifier (void)
{
#if (PROBE_COM_SUPPORT_STR == DEF_TRUE)

    if (ProbeCom_StrRdy() == DEF_TRUE) {
        return (PROBE_COM_MODIFIER_STR_HAVE);
    } else {
        return (PROBE_COM_MODIFIER_NONE);
    }

#else

    return (PROBE_COM_MODIFIER_NONE);

#endif
}


/*
*********************************************************************************************************
*                                       Send an FMT_ERROR Command

⌨️ 快捷键说明

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