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

📄 mdm.c

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

    NU_Sleep(ticks);

} /* MDM_Delay */

/***************************************************************************
* FUNCTION
*
*       MDM_Dial
*
* DESCRIPTION
*
*       This function commands the modem to dial a phone number.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*        CHAR           *number             Pointer to a string that
*                                           contains the number to dial.
*
* OUTPUTS
*
*       STATUS                              NU_SUCCESS is returned if
*                                           connection to remote machine
*                                           is made, else a negtive value
*                                           is returned.
*
****************************************************************************/
STATUS MDM_Dial(CHAR *number, DV_DEVICE_ENTRY *dev_ptr)
{
    CHAR        dial_string[MDM_RESPONSE_SIZE],
                response[MDM_RESPONSE_SIZE],
                *baud_start;
    STATUS      status;
    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;

#ifdef NU_DEBUG_PPP
    UINT32      baud_rate;
#endif

    if (number == NU_NULL)
        return(NU_INVALID_POINTER);

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

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

    /* Set the status of the PPP connection attempt. */
    link_layer->connection_status = NU_PPP_DIALING;

    /* Before trying to dial check for the presence of the modem. */
    if ( (MDM_Modem_Connected (dev_ptr) == NU_TRUE) )
    {
        /* 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);

        strcpy(dial_string, mdm->dial_prefix);
        strcpy(&dial_string[strlen(dial_string)], number);
        strcpy(&dial_string[strlen (dial_string)], "^M");

        /* dial the number */
        MDM_Control_String(dial_string, dev_ptr->dev_net_if_name);

        /* The modem will return the string with a CR on the end, 0xD is
           the ASCII code for a CR. */
        strcpy(dial_string, mdm->dial_prefix);
        strcpy(&dial_string[strlen(dial_string)], number);
        dial_string [strlen(dial_string) + 1] = 0;
        dial_string [strlen(dial_string)] = 0xD;

        MDM_Get_Modem_String(response, dial_string, dev_ptr,
                             MDM_RESPONSE_SIZE);   /* get a response */

        /* get a pointer to the connect string */
        baud_start = (CHAR *) strstr(response, "CONNECT");

        /* connection was made */
        if (baud_start)
        {

#ifdef NU_DEBUG_PPP

            /* read the baud rate for printing purposes */
            baud_rate = NU_ATOL(baud_start + 8);

            _PRINT (baud_start);
            _PRINT ("\n");
#endif

            /* Set the status to success since the modem has connected. */
            status = NU_SUCCESS;

            /* We must wait a little to let the server begin its session. */
            NU_Sleep(TICKS_PER_SECOND * 2);

        }
        else
            if (strstr(response, "NO CARRIER") != NULL)
                status = NU_NO_CARRIER;
            else
                if (strstr(response, "BUSY") != NULL)
                    status = NU_BUSY;
                else
                    status = NU_NO_CONNECT;
    }
    else
        status = NU_NO_MODEM;

    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);

} /* MDM_Dial */

/***************************************************************************
* FUNCTION
*
*       MDM_Get_Modem_String
*
* DESCRIPTION
*
*       This function retrieves the responses from the modem after the modem
*       has been issued a command.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       CHAR            *response           Pointer to a location to store
*                                           the modems response.
*       CHAR            *dial_string        Pointer to the string that was
*                                           sent to the modem.
*
* OUTPUTS
*
*       CHAR*                               Pointer to a location to store
*                                           the modems response.
*
****************************************************************************/
CHAR *MDM_Get_Modem_String(CHAR *response, CHAR *dial_string,
                           DV_DEVICE_ENTRY *dev_ptr, UINT8 rsp_size)
{
    CHAR            c;
    INT32           time;
    INT8            got_dial_string;

    *response = 0;
    got_dial_string = NU_FALSE;

    time = NU_Retrieve_Clock();

    time += 50 * TICKS_PER_SECOND;

    /* Timeout if a response is not received within the specified period of
       time.  Note the test below accounts for the wrapping of the clock. */
    while ((time - (INT32)NU_Retrieve_Clock()) > 0)
    {
        /* get a character from the port if one's there */
        if (MDM_Data_Ready(dev_ptr->dev_net_if_name))
        {
            /* Retrieve the next character. */
            MDM_Get_Char(&c, dev_ptr->dev_net_if_name);

            switch (c)
            {
                case 0xD:                /* CR - return the result string */

                    /* The first CR is from the dial string. If we got it
                       already then return the received string.  */
                    if ((got_dial_string) && (*response))
                        return (response);
                    else

                        /* Otherwise set the flag stating that we got
                           the CR from dialing. */
                        got_dial_string = NU_TRUE;

                default:
                    if (c != 10)
                    {   /* add char to end of string */
                        response[strlen(response) + 1] = 0;
                        response[strlen(response)] = (CHAR)c;

                        /* ignore RINGING and the dial string */
                        if ( !strcmp(response, "RINGING") ||
                            !NCL_Stricmp(response, dial_string) )
                        {
                            *response = 0;
                        }
                        else
                            /* check for a full response buffer and return
                               if it is full */
                            if ((strlen (response)) >= ((UINT8)(rsp_size - 1)))
                                return (response);
                    }
            }
        }
    }

    return (response);

} /* MDM_Get_Modem_String */

/***************************************************************************
* FUNCTION
*
*       MDM_Hangup
*
* DESCRIPTION
*
*       This function does exactly what you would think, hangsup the modem.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       LINK_LAYER      *link_ptr       Pointer to the PPP link to hangup
*       UINT8           wait_for_modem  Should the routine wait for the
*                                       modem to respond
*
* OUTPUTS
*
*       none
*
****************************************************************************/
STATUS MDM_Hangup(LINK_LAYER *link_ptr, UINT8 wait_for_modem)
{
    CHAR     mdm_response[3];
    INT      mdm_index = 0;
    INT      mdm_hangup = NU_FALSE;
    MDM_LAYER *mdm = (MDM_LAYER*)link_ptr->link;

    /* First things first. Mark the device as not up. This will stop
       IP packets from trying to be transmitted. */
    link_ptr->dev_ptr->dev_flags &= ~DV_UP;

    /* Check the transq. If there is anything on we will wait until
       it has all be sent before hanging up the modem. Be sure to
       handle both cases, wait for the modem and don't wait for the
       modem. */
    while (wait_for_modem && link_ptr->dev_ptr->dev_transq.head)
        NU_Sleep (TICKS_PER_SECOND);

    /* Set the status of the PPP connection attempt. */
    link_ptr->connection_status = NU_PPP_HANGING_UP;

    if (!wait_for_modem && link_ptr->dev_ptr->dev_transq.head)

        /* Set an event to check the status of the modem. We do not
           want to wait in this routine for the modem to hangup. */
        UTL_Timerset (MDM_HANGUP, (UNSIGNED) link_ptr->dev_ptr,
                                                TICKS_PER_SECOND, NU_NULL);

    else
    {

        /* Is this the first time we have tried to hangup this device and
           we do not want to wait on the modem. */
        if ((mdm->hangup_attempts == 0) && !wait_for_modem)
        {
            /* Switch to terminal mode so that we can communicate with
               the modem. */
            MDM_Change_Communication_Mode (MDM_TERMINAL_COMMUNICATION,
                                        link_ptr->dev_ptr->dev_net_if_name);

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

            /* Bump the number of hangup attempts. */
            mdm->hangup_attempts++;

            /* Set an event to check the status of the modem. We do not
               want to wait in this routine for the modem to hangup. */
            UTL_Timerset (MDM_HANGUP, (UNSIGNED) link_ptr->dev_ptr,
                                                TICKS_PER_SECOND, NU_NULL);
        }
        else
        {
            do
            {

                /* Bump the number of hangup attempts. */
                mdm->hangup_attempts++;

                /* If we are waiting for the modem then send the hangup command
                   here. */
                if (wait_for_modem)
                {
                    /* Switch to terminal mode so that we can communicate with
                       the modem. */
                    MDM_Change_Communication_Mode (MDM_TERMINAL_COMMUNICATION,
                                    link_ptr->dev_ptr->dev_net_if_name);

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

                    /* Wait for a second for the modem to hangup */
                    NU_Sleep (TICKS_PER_SECOND);

                }

                /* We have already sent the hangup command. So check the modem
                   buffer for an OK response from the modem. */
                while ((!mdm_hangup) &&
                        (MDM_Data_Ready (link_ptr->dev_ptr->dev_net_if_name)))
                {
                    /* Read in one char. */
                    MDM_Get_Char (&mdm_response[mdm_index++],
                                        link_ptr->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 hangup flag to true. */
                        mdm_hangup = NU_TRUE;
                }

            } while ((wait_for_modem && !mdm_hangup) &&
                            (mdm->hangup_attempts <= MDM_MAX_HANGUP_ATTEMPTS));

            /* If the modem did not hangup and we are not out of attempts
               then we need to try again. */
            if ((!mdm_hangup) &&
                (mdm->hangup_attempts <= MDM_MAX_HANGUP_ATTEMPTS))
            {
                /* Send the AT hangup command to the modem again. */
                MDM_Control_String(MDM_HANGUP_STRING,
                                    link_ptr->dev_ptr->dev_net_if_name);

                /* Set an event to check the status of the modem. */
                UTL_Timerset (MDM_HANGUP, (UNSIGNED) link_ptr->dev_ptr,
                                                TICKS_PER_SECOND, NU_NULL);
            }
            else if (!wait_for_modem)
            {
                /* Change to the next state after the modem has hungup.
                   This is the only spot where the other layers and the
                   modem layer are mixed. NOTE that at this point the
                   echo counter is not being used and was previously set
                   by the original call to this function. */
                link_ptr->lcp.state = link_ptr->lcp.echo_counter;

#if (INCLUDE_PPP_MIB == NU_TRUE)

                if(link_ptr->lcp.state == OPENED)
                    SNMP_ifOperStatus(link_ptr->dev_ptr->dev_index, RFC1213_IF_UP);
                else
                    SNMP_ifOperStatus(link_ptr->dev_ptr->dev_index, RFC1213_IF_DOWN);
#endif
            }

        }
    }
    return NU_SUCCESS;
}

/***************************************************************************
* FUNCTION
*
*       MDM_Purge_Input_Buffer
*
* DESCRIPTION
*
*       This function resets the receive buffer, purging all characters that
*       still remain in the buffer.
*
* AUTHOR
*
*       Uriah T. Pollock
*
* INPUTS
*
*       CHAR        *link_name      Pointer to the link for which to clear
*                                   the buffer
*
* OUTPUTS
*
*       none
*
****************************************************************************/
STATUS MDM_Purge_Input_Buffer(CHAR *link_name)
{
    DV_DEVICE_ENTRY *dev_ptr;
    INT             prev_value;
    STATUS          ret_status;
    LINK_LAYER      *link_layer;
    MDM_LAYER       *mdm;

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

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

        prev_value = NU_Control_Interrupts(NU_DISABLE_INTERRUPTS);

        /* Reset the read and write pointers. */
        mdm->recv_buffer.mdm_read =
            mdm->recv_buffer.mdm_write =
            mdm->recv_buffer.mdm_head;

        /* Update the buffer status to empty. */
        mdm->recv_buffer.mdm_buffer_status = MDM_BUFFER_EMPTY;

        NU_Control_Interrupts(prev_value);

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

    return (ret_status);

}   /* MDM_Purge_Input_Buffer */

⌨️ 快捷键说明

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