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

📄 pppmodem.c

📁 这是全套的PPP协议的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
    return result;    }/***************************************************************************** modemDisconnectTest - test disconnection with a modem* * This routine tests disconnection with a modem by filling up all the relevant* fields in the MODEM_CONN_DESC structure, and then calling the modem * disconnection routine.** RETURNS: OK on success, ERROR otherwise* * NOMANUAL*/STATUS modemDisconnectTest    (    PFW_STACK_OBJ * pModemStack,    BOOL closeConnection    )    {    MODEM_CONN_DESC     modemConnDesc;    if (pModemStack == NULL)        {        MODEM_LOG_MSG ("ERROR: NULL stack\n",1,2,3,4,5,6);        return (ERROR);        }    if (closeConnection == TRUE)        {        if (pppConnectionClose (pModemStack) == ERROR)            MODEM_LOG_MSG ("failed to close PPP connection\n",                           1,2,3,4,5,6);        /*         * Wait 5 seconds for pppConnectionClose to do a graceful termination.         *         * Alternatively, if using the stacks created by the Project Facility         * demo configuration, instead of using this function, pppCom1Disconnect         * or pppCom2Disconnect can be used instead. These functions will call         * pppConnectionClose, and within the pppClosed callback, if the peer         * is MODEM, modemDisconnect will be called.         */        taskDelay (sysClkRateGet() * 5);        }    modemConnDesc.modemDisconnCallback = (VOIDFUNCPTR) modemDisconnCallback;    modemConnDesc.modemDisconnParm = (void *) pModemStack;    if (modemDisconnect (pModemStack,&modemConnDesc) == ERROR)        {        MODEM_DEBUG_PRINT (("failed to disconnect to modem \n"));        return (ERROR);        }    return (OK);    }/***************************************************************************** modemConnCallback - callback routine for a modem connection* * This routine is invoked when connection to a modem has been successfully* established.** RETURNS: OK on success, ERROR otherwise*/LOCAL void modemConnCallback    (    void * modemConnParm1,    void * modemConnParm2    )    {    PFW_STACK_OBJ * pStackObj = (PFW_STACK_OBJ *) modemConnParm1;    PPP_UPCALL_FUNCTIONS * pUpcalls = (PPP_UPCALL_FUNCTIONS *) modemConnParm2;    MODEM_LOG_MSG ("modemConnCallback called with parm 0x%x\n", (int) modemConnParm1,                    0, 0, 0, 0, 0);    if (pppConnectionOpen (pStackObj, pUpcalls, NULL) == ERROR)        MODEM_LOG_MSG ("failed to open PPP connection\n", 0, 0, 0, 0, 0, 0);    return;    }/***************************************************************************** modemDisconnCallback - callback routine for a modem disconnection* * This routine is invoked when a modem has been successfully disconnected.* * RETURNS: OK on success, ERROR otherwise*/LOCAL void modemDisconnCallback    (    void	* modemDisconnParm    )    {    MODEM_LOG_MSG ("modemDisconnCallback called with parm 0x%x\n", (int) modemDisconnParm,		    0, 0, 0, 0, 0);    return;    }/***************************************************************************** modemConnect - initialize connection with a modem* * This routine first retrieves the modem interface within the relevant * framework, then uses that interface to establish connection with a modem.* To do so, it opens the connection to the device, than initializes it,* and finally attempts to dial-up. At this point the interface is marked as * up (MODEM_UP), closed, and the modem is usable for PPP sessions. If the * connection callback in the MODEM_CONN_DESC descriptor is a valid pointer, * then that callback is invoked with the parameters specified* in the same descriptor.** RETURNS: OK on success, ERROR otherwise*/STATUS modemConnect    (    PFW_STACK_OBJ	* pStackObj,    MODEM_CONN_DESC	* pModemConnDesc    )    {    PFW_PLUGIN_OBJ_STATE * pObjState;    PPP_MODEM_INTERFACE * pModemInterface;    PFW_OBJ * pfw;    PFW_STACK_STATUS stackStatus;    UINT	modemInterfaceId = 0;    PFW_INTERFACE_STATE_PAIR interfaceState;     /* get to the modem interface and object state */    if ((pfwStackStatusGet(pStackObj, &stackStatus) != OK) ||        (stackStatus != PFW_STACK_READY))        {        MODEM_LOG_MSG ("PPP: Invalid OR Incomplete stackObj 0x%x\n", 		       (UINT32)pStackObj, 0, 0, 0, 0, 0);        return ERROR;        }     if ((pfw = pfwStackObjPfwGet(pStackObj)) == NULL)        {        MODEM_LOG_MSG ("PPP: Invalid stackObj 0x%x\n", 		       (UINT32)pStackObj, 0, 0, 0, 0, 0);        return ERROR;        }     if ((modemInterfaceId = pfwInterfaceIdGet (pfw, "PPP_MODEM_INTERFACE"))	== 0)        return ERROR;    if (pfwInterfaceObjAndStateGetViaStackObj (pStackObj, 					       modemInterfaceId,					       &interfaceState)	== ERROR)        return ERROR;    pObjState = interfaceState.state;    pModemInterface = (PPP_MODEM_INTERFACE *) interfaceState.interfaceObj;    if (pModemInterface->modemOpen != NULL)	if ((* (pModemInterface->modemOpen)) (pObjState) == ERROR)	    {	    MODEM_LOG_MSG ("modemConnect: failed to open modem\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }    MODEM_LOG_MSG ("modemConnect: about to initialize modem\n",		    0, 0, 0, 0, 0, 0);    if (modemInit (pObjState, pModemInterface, 		   pModemConnDesc->modemInitString,		   pModemConnDesc->modemCmdTimeout) == ERROR)	{	MODEM_LOG_MSG ("modemConnect: initialize failed\n", 0, 0, 0, 0, 0, 0);        return ERROR;	}    if (modemDial (pObjState, pModemInterface, 		   pModemConnDesc->modemNumber,		   pModemConnDesc->modemDialResponse,		   pModemConnDesc->modemDialTimeout) == ERROR)	{	MODEM_LOG_MSG ("modemConnect: dialup failed\n", 0, 0, 0, 0, 0, 0);        return ERROR;	}    if (pModemInterface->modemIoctl != NULL)	if ((* (pModemInterface->modemIoctl)) (pObjState, MODEM_SET_FLAGS, 					       MODEM_UP) == ERROR)	    {	    MODEM_LOG_MSG ("modemConnect: failed to bring themodem up\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }    if (pModemInterface->modemClose != NULL)	if ((* (pModemInterface->modemClose)) (pObjState) == ERROR)	    {	    MODEM_LOG_MSG ("modemConnect: failed to close modem\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }    if (pModemConnDesc->modemConnCallback != NULL)	{	(* pModemConnDesc->modemConnCallback)         (pModemConnDesc->modemConnParm1, pModemConnDesc->modemConnParm2);	}    return (OK);    }/***************************************************************************** modemDisconnect - disconnect from a modem* * This routine disconnects from a modem previously connected with * modemConnect ().** This routine first retrieves the modem interface within the relevant * framework, then uses that interface to disconnect from the modem.* To do so, it opens the connection to the device, than marks it as down,* and finally hangs it up. At this point the interface is unusable for PPP * sessions. If the disconnection callback in the MODEM_CONN_DESC descriptor * is a valid pointer, then that callback is invoked with the parameter * specified in the same descriptor.** RETURNS: OK on success, ERROR otherwise*/STATUS modemDisconnect    (    PFW_STACK_OBJ	* pStackObj,    MODEM_CONN_DESC	* pModemConnDesc    )    {    PFW_PLUGIN_OBJ_STATE * pObjState;    PPP_MODEM_INTERFACE * pModemInterface;    PFW_OBJ * pfw;    PFW_STACK_STATUS stackStatus;    UINT	modemInterfaceId = 0;    PFW_INTERFACE_STATE_PAIR interfaceState;    int		flags = 0;     /* get to the modem interface and object state */    if ((pfwStackStatusGet(pStackObj, &stackStatus) != OK) ||        (stackStatus != PFW_STACK_READY))        {        MODEM_LOG_MSG ("PPP: Invalid OR Incomplete stackObj 0x%x\n", 		       (UINT32)pStackObj, 0, 0, 0, 0, 0);        return ERROR;        }     if ((pfw = pfwStackObjPfwGet(pStackObj)) == NULL)        {        MODEM_LOG_MSG ("PPP: Invalid stackObj 0x%x\n", 		       (UINT32)pStackObj, 0, 0, 0, 0, 0);        return ERROR;        }     if ((modemInterfaceId = pfwInterfaceIdGet (pfw, "PPP_MODEM_INTERFACE"))	== 0)        return ERROR;    if (pfwInterfaceObjAndStateGetViaStackObj (pStackObj, 					       modemInterfaceId,					       &interfaceState)	== ERROR)        return ERROR;    pObjState = interfaceState.state;    pModemInterface = (PPP_MODEM_INTERFACE *) interfaceState.interfaceObj;    if (pModemInterface->modemOpen != NULL)	if ((* (pModemInterface->modemOpen)) (pObjState) == ERROR)	    {	    MODEM_LOG_MSG ("modemDisconnect: failed to open modem\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }    if (pModemInterface->modemIoctl != NULL)	{	if ((* (pModemInterface->modemIoctl)) (pObjState, MODEM_GET_FLAGS, 					       (int) &flags) == ERROR)	    return ERROR;	if ((* (pModemInterface->modemIoctl)) (pObjState, MODEM_SET_FLAGS, 					       (flags & ~MODEM_UP)) == ERROR)	    {	    MODEM_LOG_MSG ("modemDisconnect: failed to bring modem down\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }	}    if (modemHangup (pObjState, pModemInterface) == ERROR)	{	MODEM_LOG_MSG ("modemDisconnect: failed to hangup modem\n", 		       0, 0, 0, 0, 0, 0);        return ERROR;	}    if (pModemInterface->modemClose != NULL)	if ((* (pModemInterface->modemClose)) (pObjState) == ERROR)	    {	    MODEM_LOG_MSG ("modemDisconnect: failed to close modem\n",			    0, 0, 0, 0, 0, 0);	    return ERROR;	    }    if (pModemConnDesc->modemDisconnCallback != NULL)	{	(* pModemConnDesc->modemDisconnCallback) 			   (pModemConnDesc->modemDisconnParm);	}    return (OK);    }/***************************************************************************** modemInit - initialize connection with a modem* * This routine sends an initialization string to a modem, waits for correct * response. Optionally it sends user initialization string to the modem ** RETURNS: OK on success, ERROR otherwise*/LOCAL STATUS modemInit    (    PFW_PLUGIN_OBJ_STATE	* pObjState,    PPP_MODEM_INTERFACE		* pModemInterface,    char			* userInitString,    UINT			modemCmdTimeout    )    {    char	command[128];    char	response[128];    int		status;    bzero (response, NELEMENTS (response));    /* do an hardware initialization */    status = modemCommand (pObjState, 			   pModemInterface,			   MODEM_HARD_INIT"\r", 			   response, 			   sizeof(response), 			   MODEM_INIT_OK,			   modemCmdTimeout);    if (status != OK)	{	MODEM_LOG_MSG ("hw init FAILED, \n",			0, 0, 0, 0, 0, 0);	return (status);	}    if (userInitString == NULL || !userInitString [0])	{	return (OK);	}    sprintf(command, "%.*s\r", MODEM_MAX_STRLEN, userInitString);    bzero (response, NELEMENTS (response));    /* external modem needs a small delay between commands, if no debug messages */    taskDelay (60);    status = modemCommand (pObjState, 			   pModemInterface,			   command, 			   response, 			   sizeof(response), 			   MODEM_INIT_OK,			   modemCmdTimeout);    if (status != OK)	{	MODEM_LOG_MSG ("user init FAILED, \n",			0, 0, 0, 0, 0, 0);	return (status);	}    MODEM_DEBUG_PRINT (("command successful %s \n", command));     return (OK);    }	/***************************************************************************** modemDial - dial phone number using a modem* * This routine dials phone number using a modem, and waits up to * modemDialTimeout for a response.** RETURNS: OK on success, ERROR otherwise*/LOCAL STATUS modemDial    (    PFW_PLUGIN_OBJ_STATE	* pObjState,    PPP_MODEM_INTERFACE		* pModemInterface,    char			* phoneNumber,    char			* modemDialResponse,    UINT			modemDialTimeout    )    {    char command[128];    char response[128];    bzero (response, NELEMENTS (response));    sprintf(command, "ATDT%.*s\r", MODEM_MAX_STRLEN, phoneNumber);    /* external modem needs a small delay between commands, if no debug messages */    taskDelay (60);    if (modemCommand (pObjState, pModemInterface, command, 		      response, sizeof(response), 		      modemDialResponse, modemDialTimeout) != OK)	{	MODEM_LOG_MSG ("Dialup failed \n", 0,0,0,0,0,0);	return (ERROR);	}    return (OK);    }/***************************************************************************** modemHangup - hang-up a modem* * This routine hangs-up the connection to a modem.** RETURNS: OK on success, ERROR otherwise*/LOCAL STATUS modemHangup    (    PFW_PLUGIN_OBJ_STATE	* pObjState,    PPP_MODEM_INTERFACE		* pModemInterface    )    {    if (modemCommand (pObjState, pModemInterface, 		      "+++", NULL, 0, 		      NULL, MODEM_TIMEOUT_CMD)!= OK)	{	return (ERROR);		}    /* Hangup */    /* 1 sec delay needed between escape and command */    taskDelay(sysClkRateGet());    return (modemCommand (pObjState, pModemInterface, 			  "ATH0\r", NULL, 0, 			  NULL, MODEM_TIMEOUT_CMD));	    }

⌨️ 快捷键说明

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