📄 pppmodem.c
字号:
*/STATUS modemDisconnectTest ( PFW_STACK_OBJ * pModemStack ) { MODEM_CONN_DESC modemConnDesc; if (pModemStack == NULL) { MODEM_LOG_MSG ("modemDisconnectTest: Invalid stack object \n", 0, 0, 0, 0, 0, 0); printf ("modemDisconnectTest: Invalid stack object \n"); return (ERROR); } modemConnDesc.modemDisconnCallback = (VOIDFUNCPTR) modemDisconnCallback; modemConnDesc.modemDisconnParm = (void *) pModemStack; if (modemDisconnect (pModemStack, &modemConnDesc) == ERROR) { MODEM_LOG_MSG ("failed to disconnect to modem \n", 0, 0, 0, 0, 0, 0); return (ERROR); } return (OK); }#endif /* MODEM_DEBUG *//***************************************************************************** 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 * modemConnParm ) { MODEM_LOG_MSG ("callback called with parm 0x%x\n", (int) modemConnParm, 0, 0, 0, 0, 0); if (pppConnectionOpen (modemConnParm, NULL, 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 ("callback called with parm 0x%x\n", (int) modemDisconnParm, 0, 0, 0, 0, 0); if (pppConnectionClose (modemDisconnParm) == ERROR) MODEM_LOG_MSG ("failed to close PPP connection\n", 0, 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 parameter <modemConnParm> 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; char response[128]; /* 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; } bzero (response, sizeof (response));#if 0 if ( (modemCommand (pObjState, pModemInterface, "ATO0\r", response, sizeof (response), "OK", pModemConnDesc->modemCmdTimeout)) == ERROR) { printf ("modemCommand: failed to send ATO0 modem\n"); return ERROR; }#endif 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->modemConnParm); } 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 * <modemDisconnParm> 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)); 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, "ATD%.*s\r", MODEM_MAX_STRLEN, phoneNumber); 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 ) { char response[32]; bzero (response, NELEMENTS (response)); taskDelay(sysClkRateGet()/2); /* 500ms delay */ if (modemCommand (pObjState, pModemInterface, "+++", response, sizeof(response), NULL, MODEM_TIMEOUT_CMD)!= OK) { return (ERROR); /* Modem should report "OK" */ }#if 0 if (modemCommand (pObjState, pModemInterface, "+++", NULL, 0, MODEM_INIT_OK, MODEM_TIMEOUT_CMD)!= OK) { return (ERROR); /* Modem should report "OK" */ } return (modemCommand (pObjState, pModemInterface, "ATH\r", response, sizeof(response), MODEM_INIT_OK, MODEM_TIMEOUT_CMD)); #endif bzero (response, NELEMENTS (response)); /* Hangup */ return (modemCommand (pObjState, pModemInterface, "ATH\r", response, sizeof(response), NULL, MODEM_TIMEOUT_CMD)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -