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

📄 hdlc.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
                    uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

                    /* Store the byte. */
                    uart->tx_buffer[uart->tx_buffer_write++] = (PPP_HDLC_CONTROL_ESCAPE ^ PPP_HDLC_TRANSPARENCY);

                    /* Check for wrapping of the write pointer. */
                    uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif

                    break;

                /* Otherwise it is just a regular character. We will either send
                   it or encode it depending on the ACCM. */
                default:

#ifdef DEBUG_PKT_TRACE
                        if (debug_tx_index == PKT_TRACE_SIZE)
                            debug_tx_index = 0;

                        debug_tx_buf [debug_tx_index++] = *ppp_header_ptr;
#endif

                    /* Since the ACCM is only for the first 32 chars. see if
                       we even need to check it for this one. */
                    if (*ppp_header_ptr >= PPP_MAX_ACCM)
                    {
                       /* guess not so send it */
#ifdef PPP_POLLED_TX
                        URT_Put_Char(*ppp_header_ptr, uart);
#else
                        /* Store the byte. */
                        uart->tx_buffer[uart->tx_buffer_write++] = *ppp_header_ptr;

                        /* Check for wrapping of the write pointer. */
                        uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                    }
                    else

                        /* else check the character against the map */

                        if ((PPP_Two_To_Power (*ppp_header_ptr) & lcp->foreign_options.accm)
                            || lcp->state != OPENED)
                        {

                            /* It is in the map so send the two char code. */

#ifdef PPP_POLLED_TX
                            URT_Put_Char(PPP_HDLC_CONTROL_ESCAPE, uart);
                            URT_Put_Char((CHAR) (*ppp_header_ptr ^ PPP_HDLC_TRANSPARENCY), uart);
#else
                            /* Store the byte. */
                            uart->tx_buffer[uart->tx_buffer_write++] = PPP_HDLC_CONTROL_ESCAPE;

                            /* Check for wrapping of the write pointer. */
                            uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

                            /* Store the byte. */
                            uart->tx_buffer[uart->tx_buffer_write++] = (*ppp_header_ptr ^ PPP_HDLC_TRANSPARENCY);

                            /* Check for wrapping of the write pointer. */
                            uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                        }
                        else
                        {
                            /* It is not in the map so send it in the clear. */
#ifdef PPP_POLLED_TX
                            URT_Put_Char(*ppp_header_ptr, uart);
#else
                            /* Store the byte. */
                            uart->tx_buffer[uart->tx_buffer_write++] = *ppp_header_ptr;

                            /* Check for wrapping of the write pointer. */
                            uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif

                        }
            }

            /* Move to the next byte. */
            ppp_header_ptr++;
        }

        /* Move the buffer pointer to the next buffer. */
        tmp_buf_ptr = tmp_buf_ptr->next_buffer;

        /* If there are no more buffers then there is no more to send. */
        if (!tmp_buf_ptr)
        {
            more_to_send = NU_FALSE;
        }

    }


    /* Now set the length to two for the two bytes of the FCS that are appended
       to the end of each packet. */
    length = PPP_FCS_SIZE_BYTE2;

    /* Swap the FCS, remember that the FCS is put on LSB first. */

    /* Reuse the pkt_type variable. */
    pkt_type = frame_check;

    /* Get a pointer to the FCS. */
    ppp_header_ptr = (UINT8 *) &frame_check;

    /* Swap it. */
    ppp_header_ptr[0] = (UINT8) pkt_type;
    ppp_header_ptr[1] = (UINT8) (pkt_type >> 8);

    /* Put the FCS on the end of the packet. */
    while(length--)
    {
        switch((CHAR)*ppp_header_ptr)
        {
            /* If it's the same code as a FRAME character, then send the special
               two character code so that the receiver does not think we sent
               a FRAME.
            */
            case PPP_HDLC_FRAME:
#ifdef DEBUG_PKT_TRACE
                if (debug_tx_index == PKT_TRACE_SIZE)
                    debug_tx_index = 0;

                debug_tx_buf [debug_tx_index++] = (PPP_HDLC_FRAME);

#endif

#ifdef PPP_POLLED_TX
                URT_Put_Char(PPP_HDLC_CONTROL_ESCAPE, uart);
                URT_Put_Char(PPP_HDLC_FRAME ^ PPP_HDLC_TRANSPARENCY, uart);
#else
                /* Store the byte. */
                uart->tx_buffer[uart->tx_buffer_write++] = PPP_HDLC_CONTROL_ESCAPE;

                /* Check for wrapping of the write pointer. */
                uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

                /* Store the byte. */
                uart->tx_buffer[uart->tx_buffer_write++] = (PPP_HDLC_FRAME ^ PPP_HDLC_TRANSPARENCY);

                /* Check for wrapping of the write pointer. */
                uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                break;

            /* If it's the same code as an ESC character, then send the special
               two character code so that the receiver does not think we sent
               an ESC.
            */
            case PPP_HDLC_CONTROL_ESCAPE:
#ifdef DEBUG_PKT_TRACE
                if (debug_tx_index == PKT_TRACE_SIZE)
                    debug_tx_index = 0;

                debug_tx_buf [debug_tx_index++] = PPP_HDLC_CONTROL_ESCAPE;

#endif

#ifdef PPP_POLLED_TX
                URT_Put_Char(PPP_HDLC_CONTROL_ESCAPE, uart);
                URT_Put_Char(PPP_HDLC_CONTROL_ESCAPE ^ PPP_HDLC_TRANSPARENCY, uart);
#else
                /* Store the byte. */
                uart->tx_buffer[uart->tx_buffer_write++] = PPP_HDLC_CONTROL_ESCAPE;

                /* Check for wrapping of the write pointer. */
                uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

                /* Store the byte. */
                uart->tx_buffer[uart->tx_buffer_write++] = (PPP_HDLC_CONTROL_ESCAPE ^ PPP_HDLC_TRANSPARENCY);

                /* Check for wrapping of the write pointer. */
                uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                break;

            /* Otherwise it is just a regular character. We will either send
               it or encode it depending on the ACCM. */
            default:

#ifdef DEBUG_PKT_TRACE
                    if (debug_tx_index == PKT_TRACE_SIZE)
                        debug_tx_index = 0;

                    debug_tx_buf [debug_tx_index++] = *ppp_header_ptr;
#endif

                /* Since the ACCM is only for the first 32 chars. see if
                   we even need to check it for this one. */
                if (*ppp_header_ptr >= PPP_MAX_ACCM)
                {
                    /* guess not so send it */
#ifdef PPP_POLLED_TX
                    URT_Put_Char(*ppp_header_ptr, uart);
#else
                    /* Store the byte. */
                    uart->tx_buffer[uart->tx_buffer_write++] = *ppp_header_ptr;

                    /* Check for wrapping of the write pointer. */
                    uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                }
                else

                    /* else check the character against the map */

                    if ((PPP_Two_To_Power (*ppp_header_ptr) & lcp->foreign_options.accm)
                            || lcp->state != OPENED)
                    {

                        /* It is in the map so send the two char code. */
#ifdef PPP_POLLED_TX
                        URT_Put_Char(PPP_HDLC_CONTROL_ESCAPE, uart);
                        URT_Put_Char((CHAR) (*ppp_header_ptr ^ PPP_HDLC_TRANSPARENCY), uart);
#else
                        /* Store the byte. */
                        uart->tx_buffer[uart->tx_buffer_write++] = PPP_HDLC_CONTROL_ESCAPE;

                        /* Check for wrapping of the write pointer. */
                        uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

                        /* Store the byte. */
                        uart->tx_buffer[uart->tx_buffer_write++] = (*ppp_header_ptr ^ PPP_HDLC_TRANSPARENCY);

                        /* Check for wrapping of the write pointer. */
                        uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif
                    }
                    else
                    {
                        /* It is not in the map so send it in the clear. */
#ifdef PPP_POLLED_TX
                        URT_Put_Char(*ppp_header_ptr, uart);
#else
                        /* Store the byte. */
                        uart->tx_buffer[uart->tx_buffer_write++] = *ppp_header_ptr;

                        /* Check for wrapping of the write pointer. */
                        uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;
#endif

                    }
        }

        ppp_header_ptr++;
    }

    /* Terminate the packet. */
#ifdef PPP_POLLED_TX

    URT_Put_Char(PPP_HDLC_FRAME, uart);

#else

    /* Store the byte. */
    uart->tx_buffer[uart->tx_buffer_write++] = PPP_HDLC_FRAME;

    /* Check for wrapping of the write pointer. */
    uart->tx_buffer_write %= (UINT16) URT_TX_BUFFER_SIZE;

#endif

#ifndef PPP_POLLED_TX

    /* Send a FLAG character to flush out any characters the remote host might
       have received because of line noise and to mark the begining of this
       PPP HDLC frame. This will also start the TX interrupts which will finish
       sending the TX buffer. */

    URT_Put_Char (PPP_HDLC_FRAME, uart);

#endif

    return (NU_SUCCESS);

}  /* end PPP_Xmit routine */




/************************************************************************
* FUNCTION
*
*     PPP_RX_HISR_Entry
*
* DESCRIPTION
*
*    This is the entry function for the PPP HISR.  The HISR will check the
*    FCS of the incoming packet. If it passes, an event will be set to
*    notify the upper layer that a packet needs to be processed.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     none
*
* OUTPUTS
*
*     none
*
************************************************************************/
VOID HDLC_RX_HISR_Entry (VOID)
{
    NET_BUFFER      *buf_ptr, *work_buf;
    UINT16          bytes_left;
    UINT8           *buffer;
    UINT8           acclen = 0;
    UINT8           pfclen = 0;
    UINT16          hdrlen, paylen;

    /* Check the FCS to make sure this is a valid packet before we
      bother the upper layers with it. */
    if (HDLC_Compute_RX_FCS (PPP_INIT_FCS16,
        _ppp_rx_queue[_ppp_rx_queue_read]) != PPP_GOOD_FCS16)
    {
#ifdef NU_DEBUG_PPP
        _PRINT ("\ndiscard - invalid FCS ");
#endif

#if (INCLUDE_PPP_MIB == NU_TRUE)
        dev_ptr = _ppp_rx_queue[_ppp_rx_queue_read]->device;
        ppp = _ppp_rx_queue[_ppp_rx_queue_read]->device->link_layer;


        /* increment SNMP error counter fof bad checksums */
        SNMP_pppLinkStatusBadFCSs(dev_ptr->dev_index);

        /* checksum is wrong because the packet received could be bigger than
           the negotiated MRU and it has gotten truncated. Need to incremenet
           the snmp error counter */
        if (ppp->received > ppp->lcp.local_options.max_rx_unit)
            SNMP_pppLinkStatusPacketTooLongs(dev_ptr->dev_index);
#endif

        /* Bump the number of silent discards */
        ((LINK_LAYER *)_ppp_rx_queue[_ppp_rx_queue_read]->device->link_layer)
                        ->silent_discards++;

        /* Remove the temp buffer entry. */
        _ppp_rx_queue[_ppp_rx_queue_read] = NU_NULL;

    }
    else
    {
        /* Get a pointer to the data. */
        buffer = _ppp_rx_queue[_ppp_rx_queue_read]->buffer;

        /* Store the total size, removing the two bytes of the FCS. */

⌨️ 快捷键说明

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