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

📄 mdm.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 3 页
字号:

/***************************************************************************
* FUNCTION
*
*       MDM_Change_Communication_Mode
*
* DESCRIPTION
*
*       This function changes the communication mode that the UART is operating
*       in.  There are two modes.  Terminal communication mode might be used
*       to log into a dailup router. Network mode is for exchanging IP packets
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       INT                 The mode of operation desired.
*
* OUTPUTS
*
*       STATUS              Indicates success or failure of the operation.
*
****************************************************************************/
STATUS MDM_Change_Communication_Mode(INT mode, CHAR *link_name)
{
    DV_DEVICE_ENTRY *dev_ptr;
    STATUS          ret_status;
    LINK_LAYER         *link_layer;
    URT_LAYER          *uart;

    /* Is the mode valid. */
    if( (mode != MDM_NETWORK_COMMUNICATION) &&
        (mode != MDM_TERMINAL_COMMUNICATION) )
        return (NU_INVALID_MODE);

    /* Find the device for this link */
    dev_ptr = DEV_Get_Dev_By_Name (link_name);

    /* Make sure the link was found. */
    if (dev_ptr)
    {
        /* Clear the input buffer for this device. */
        MDM_Purge_Input_Buffer(link_name);

        /* Switch the mode in the UART component as well. */
        link_layer = (LINK_LAYER *)dev_ptr->dev_ppp_layer;
        uart = &link_layer->uart;

        URT_Change_Communication_Mode(mode, uart);

        ret_status = NU_SUCCESS;
    }
    else
        ret_status = NU_INVALID_LINK;

    return (ret_status);

}   /* MDM_Change_Communication_Mode */

/***************************************************************************
* FUNCTION
*
*       MDM_Wait_For_Client
*
* DESCRIPTION
*
*       This function waits for a client to call. If only returns once a
*       successful modem connection is made.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       DV_DEVICE_ENTRY     *dev_ptr    Pointer to the device from which
*                                       to accept a PPP client.
*
* OUTPUTS
*
*       none
*
****************************************************************************/
STATUS MDM_Wait_For_Client (DV_DEVICE_ENTRY *dev_ptr)
{
    CHAR        mdm_string [81];
    CHAR        rx_char;
    INT         i, index, connected;

    LINK_LAYER *link_layer = (LINK_LAYER *)dev_ptr->dev_ppp_layer;
    MDM_LAYER *mdm = (MDM_LAYER*)link_layer->link;
    URT_LAYER  *uart = &link_layer->uart;
    STATUS      status = ~NU_SUCCESS;

#ifdef NU_DEBUG_PPP
    UINT32  baud_rate;

    _PRINT ("\nwaiting for client ");
#endif

    /* Reset the uart */
    URT_Reset(uart);

    /* Clear the hangup attempts since we are starting a new session. */
    mdm->hangup_attempts = 0;

    /* We are not currently connected. */
    connected = NU_FALSE;
    index     = 0;

    /* wait for modem */
    NU_Sleep (TICKS_PER_SECOND);

    /* Change to terminal mode so we can talk to the modem. */
    MDM_Change_Communication_Mode (MDM_TERMINAL_COMMUNICATION, dev_ptr->dev_net_if_name);

    /* Create the modem string to send to modem. */
    strcpy(mdm_string, MDM_ACCEPT_CALL);
    
    /* Add number of rings to the string. */
    i = strlen(mdm_string);
    NU_ITOA((int)mdm->num_rings, &mdm_string[i], 10);
    strcat(mdm_string, MDM_STRING_TERMINATOR);

    /* Tell the modem to accept a caller. */
    MDM_Control_String (mdm_string, dev_ptr->dev_net_if_name);

    /* Clear out the modem buffer. */
    MDM_Purge_Input_Buffer(dev_ptr->dev_net_if_name);

    /* Wait for a caller. */
    while ((!connected) && (link_layer->connection_status != NU_PPP_WAITING_ABORTED))
    {
        /* wait for modem */
        NU_Sleep (TICKS_PER_SECOND);

        while (MDM_Data_Ready(dev_ptr->dev_net_if_name))
        {
            /* Get the char that came in */
            if (MDM_Get_Char (&rx_char, dev_ptr->dev_net_if_name) == NU_SUCCESS)
            {
                if ((index + 1) >= 80)
                    index = 0;

                /* Put it in our buffer so we can look at it. */
                mdm_string [index++] = rx_char;

                /* Null the end. */
                mdm_string [index]   = 0;

                /* See if we got a connection. */
                if (strstr (mdm_string, "CONNECT"))
                {

#ifdef NU_DEBUG_PPP
                    /* Wait for the baud rate. */
                    NU_Sleep(TICKS_PER_SECOND / 18);

                    index = 0;

                    /* read in the baud rate */
                    while (MDM_Data_Ready(dev_ptr->dev_net_if_name) &&
                                        (index <= 80))
                    {
                        MDM_Get_Char (&rx_char, dev_ptr->dev_net_if_name);
                        mdm_string[index++] = rx_char;
                        mdm_string[index] = 0;
                    }

                    /* convert the baud rate to a number */
                    baud_rate = NU_ATOL(mdm_string);

                    _PRINT ("\n***MODEM OUTPUT***\n");
                    _PRINT (mdm_string);
                    _PRINT ("\n");
#endif

                    /* Set this flag to true so that we will fall out of
                       the loop and exit this function. */
                    connected = NU_TRUE;
                    status = NU_SUCCESS;
                }
            }
        }
    }

    if (link_layer->connection_status == NU_PPP_WAITING_ABORTED)
        status = NU_PPP_ATTEMPT_ABORTED;

    else if (status == NU_SUCCESS)

        /* Switch back to network packet mode. */
        MDM_Change_Communication_Mode (MDM_NETWORK_COMMUNICATION,
                                      dev_ptr->dev_net_if_name);

    return status;
} /* end MDM_Wait_For_Client */

/***************************************************************************
* FUNCTION
*
*       MDM_Put_Char
*
* DESCRIPTION
*
*       This function sends one byte to the UART.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       CHAR            c           The character to send
*       CHAR*           link_name   Pointer to the name of the link
*                                   to send the character to
*
* OUTPUTS
*
*       STATUS                      NU_SUCCESS if the character was sent.
*                                   Otherwise NU_INVALID_LINK is returned.
*
****************************************************************************/
STATUS MDM_Put_Char (CHAR c, CHAR *link_name)
{
    DV_DEVICE_ENTRY *dev_ptr;
    STATUS          ret_status;
    LINK_LAYER      *link_layer;
    URT_LAYER       *uart;


    /* Find the device for this link */
    dev_ptr = DEV_Get_Dev_By_Name (link_name);

    /* Make sure the link was found. */
    if (dev_ptr)
    {
        /* Get the uart layer for this device. */
        link_layer = (LINK_LAYER *)dev_ptr->dev_ppp_layer;
        uart = &link_layer->uart;

        /* Send the byte */
        URT_Put_Char (c, uart);

        /* Set the status */
        ret_status = NU_SUCCESS;
    }
    else
        /* Set the error status. */
        ret_status = NU_INVALID_LINK;

    return (ret_status);

} /* end MDM_Put_Char */


/***************************************************************************
* FUNCTION
*
*       MDM_Carrier
*
* DESCRIPTION
*
*       This function checks for the presence of a carrier from the UART.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       CHAR*       link_name   Pointer to the link for which to check
*                               for a carrier
*
* OUTPUTS
*
*       STATUS                  The state of the carrier or NU_INVALID_LINK
*
****************************************************************************/
STATUS MDM_Carrier (CHAR *link_name)
{
    DV_DEVICE_ENTRY *dev_ptr;
    STATUS          ret_status;
    LINK_LAYER *link_layer;
    URT_LAYER  *uart;


    /* Find the device for this link */
    dev_ptr = DEV_Get_Dev_By_Name (link_name);

    /* Make sure the link was found. */
    if (dev_ptr)
    {
        /* Get the uart layer for this device. */
        link_layer = (LINK_LAYER *)dev_ptr->dev_ppp_layer;
        uart = &link_layer->uart;

        /* Set the status */
        ret_status = URT_Carrier(uart);
    }
    else
        /* Set the error status. */
        ret_status = NU_INVALID_LINK;

    return (ret_status);

} /* end MDM_Carrier */

/***************************************************************************
* FUNCTION
*
*       MDM_Modem_Connected
*
* DESCRIPTION
*
*       This function checks for the presence of a modem by
*       querying the modem to respond to the AT command.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*     DV_DEVICE_ENTRY   *dev_ptr        Pointer to the device structure
*                                        for this device, ie the modem
*                                        to query.
* OUTPUTS
*
*       STATUS                          NU_TRUE or NU_FALSE
*
****************************************************************************/
STATUS MDM_Modem_Connected (DV_DEVICE_ENTRY *dev_ptr)
{
    CHAR     mdm_response[3];
    INT      mdm_index = 0, mdm_there = NU_FALSE;
    INT      sleep_loops;

    /* Change to terminal  mode */
    MDM_Change_Communication_Mode(MDM_TERMINAL_COMMUNICATION, dev_ptr->dev_net_if_name);

    /* Purge any characters that were already in the receive buffer. */
    MDM_Purge_Input_Buffer(dev_ptr->dev_net_if_name);

    /* Send the AT command to the modem. */
    MDM_Control_String(MDM_ATTENTION, dev_ptr->dev_net_if_name);

    /* We will only wait MDM_MAX_DETECTION_WAIT_LOOPS seconds for the modem to respond.
       Each loop we sleep for one second. */
    sleep_loops = MDM_MAX_DETECTION_WAIT_LOOPS;

    /* Loop until the modem responds or we timeout. */
    while ( (mdm_there == NU_FALSE) && (sleep_loops > 0) )
    {
        /* Wait for the response. */
        NU_Sleep (SCK_Ticks_Per_Second);

        /* Decrement the number of times to sleep. */
        --sleep_loops;

        /* We have already sent the AT command. So check the modem
           buffer for an OK response from the modem. */
        while ( (MDM_Data_Ready (dev_ptr->dev_net_if_name) == NU_TRUE)
                 && (mdm_there == NU_FALSE) )
        {
            /* Read in one char. */
            MDM_Get_Char (&mdm_response[mdm_index++],
                                dev_ptr->dev_net_if_name);

            /* Make sure the the OK does not get split between the last
               byte and the first byte. If we just placed a byte in the
               last position then move it to the first. */
            if (mdm_index > 2)
            {
                /* Move the byte. */
                mdm_response[0] = mdm_response[2];

                /* Set the index past the one we just moved. */
                mdm_index = 1;
            }

            /* Check to see if we got an OK back from the modem. */
            if (strstr (mdm_response, "OK"))

                /* The modem did return an OK. Set the flag to true. */
                mdm_there = NU_TRUE;
        }

    }

    return (mdm_there);

} /* end MDM_Modem_Connected */




/***************************************************************************
* FUNCTION
*
*       MDM_Rings_To_Answer
*
* DESCRIPTION
*
*       This function allows the user to change the number of rings the
*       modem will answer on.
*
* AUTHOR
*
*       Rick Gonchar
*
* INPUTS
*
*       CHAR  *link_name         name of the PPP device
*       UINT8 num_rings          number of rings (0-255)
*
* OUTPUTS
*
*       STATUS                   NU_SUCCESS or NU_INVALID_LINK
*
****************************************************************************/
STATUS MDM_Rings_To_Answer(CHAR *link_name, UINT8 num_rings)
{
    DV_DEVICE_ENTRY *dev_ptr;
    STATUS          ret_status = NU_SUCCESS;
    LINK_LAYER      *link_layer;
    MDM_LAYER       *mdm;

    /* Find the device for this link */
    dev_ptr = DEV_Get_Dev_By_Name (link_name);

    /* Make sure the link was found. */
    if (dev_ptr)
    {
        /* Get the address of the modem structure for this device. */
        link_layer = (LINK_LAYER *)dev_ptr->dev_ppp_layer;
        mdm = (MDM_LAYER*)link_layer->link;

        mdm->num_rings = num_rings;
    }
    else
        /* Set the error status. */
        ret_status = NU_INVALID_LINK;

    return (ret_status);

} /* end MDM_Rings_To_Answer */

⌨️ 快捷键说明

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