📄 pppmodem.c
字号:
/* pppModem.c - modem support for PPP framework */ /* Copyright 1999 Wind River Systems, Inc. */ #include "copyright_wrs.h" /*modification history--------------------01f,07apr03,ijm updated for PPP 2.001e,28may02,rvr fixed build warnings 01d,29sep00,sj merging with TOR2_0-WINDNET_PPP-CUM_PATCH_201c,31jul00,adb Merging with wrs.tor2_0.windNet_ppp.cum_patch01b,31may00,cn corrected documentation.01a,19apr00,cn created.*/ /*DESCRIPTIONThis component implements a generic library for a dial-up modem. It enables PPP sessions over modem dial-up line. To do so, it exploits the interfacePPP_MODEM_INTERFACE implemented by the module pppSioAdapter.EXTERNAL INTERFACEThe external routines provided by this library are modemConnect () andmodemDisconnect ()..CS STATUS modemConnect (PFW_STACK_OBJ * pStackObj, MODEM_CONN_DESC * pModemConnDesc);.CE.CS STATUS modemDisconnect (PFW_STACK_OBJ * pStackObj, MODEM_CONN_DESC * pModemConnDesc);.CE INTERNALThis library contains conditional compilation switch MODEM_DEBUGIf defined, adds debug output routines.INCLUDE FILES: SEE ALSO*/ /* includes */ #include "vxWorks.h"#include "logLib.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ctype.h"#include "memLib.h"#include "semLib.h"#include "sysLib.h"#include "intLib.h"#include "stdio.h" #include "string.h"#include "ioLib.h" #include "sysLib.h"#include "tickLib.h"#include "taskLib.h" #include "net/mbuf.h"#include "netBufLib.h"#include "pfw/pfw.h"#include "pfw/pfwStack.h"#include "pfw/pfwLayer.h"#include "pfw/pfwEvent.h"#include "pfw/pfwInterface.h"#include "pfw/pfwMemory.h"#include "pfw/pfwComponent.h"#include "ppp/pppInterfaces.h"#include "ppp/interfaces/pppModemInterface.h"/* * US Robotics default init string: * &F1 load hardware flow control configuration * S0=0 disable auto answer mode * S7=90 wait at most 90 seconds for a carrie * E1 echo is ON * V1 display verbal result codes * &H1 hardware flow control (CTS) * &I0 disable software flow control * &R2 hardware flow control (RTS) */#define USR_SPORTSTER_DEF_INIT_STRING "AT&F1S0=0S7=90E1V1&H1&I0&R2"#define MODEM_HARD_INIT "AT&F1"#define MODEM_INIT_OK "OK"#define MODEM_CONNECT "CONNECT"#define MODEM_MAX_STRLEN 50#define MODEM_TIMEOUT_INIT 5#define MODEM_TIMEOUT_CMD 5#define MODEM_TIMEOUT_DIAL 30#define MODEM_MAX_RESPONSES 5 #define MODEM_DEBUG#ifdef MODEM_DEBUGUINT32 modemDbg = 0; #define MODEM_LOG_MSG(p1, p2, p3, p4, p5, p6, p7) \ if (modemDbg) \ logMsg ((p1), (p2), (p3), (p4), (p5), (p6), (p7)) #define MODEM_DEBUG_PRINT(p1) \ printf p1#else /* MODEM_DEBUG */ #define MODEM_LOG_MSG(p1, p2, p3, p4, p5, p6, p7) #define MODEM_DEBUG_PRINT(p1)#endif /* MODEM_DEBUG *//* globals */#ifdef MODEM_DEBUGSTATUS modemConnectTest (char * initString, char * modemNumber, char * modemDialResponse, PFW_STACK_OBJ * pModemStack, PPP_UPCALL_FUNCTIONS * pUpcalls);STATUS modemDisconnectTest (PFW_STACK_OBJ * pModemStack, BOOL closeConnection);#endif /* MODEM_DEBUG */typedef struct modemConnDesc { UINT modemDialTimeout; UINT modemCmdTimeout; char * modemInitString; char * modemNumber; char * modemDialResponse; void (* modemConnCallback) (void *, void *); void * modemConnParm1; void * modemConnParm2; void (* modemDisconnCallback) (void *); void * modemDisconnParm; } MODEM_CONN_DESC; STATUS modemConnect (PFW_STACK_OBJ * pStackObj, MODEM_CONN_DESC * pModemConnDesc); STATUS modemDisconnect (PFW_STACK_OBJ * pStackObj, MODEM_CONN_DESC * pModemConnDesc); /* locals */LOCAL STATUS modemInit (PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * userInitString, UINT modemCmdTimeout); LOCAL STATUS modemDial (PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * phoneNumber, char * modemDialResponse, UINT modemDialTimeout); LOCAL STATUS modemHangup (PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface); LOCAL STATUS modemCommand (PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * cmd, char * rsp, UINT rspBufLen, char * goodRsp, UINT timeout);LOCAL STATUS modemStringRead (PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * rsp, UINT rspBufLen, UINT timeout);LOCAL void modemConnCallback (void * modemConnParm1, void * modemConnParm2);LOCAL void modemDisconnCallback (void * modemDisconnParm);/***************************************************************************** modemStringRead - read a string from a modem** This routine reads a string from a modem. Strings here has a specialized* meaning, in that they are terminated with special control characters* like <carriage return>, etc.** RETURNS: OK on success, ERROR otherwise*/LOCAL STATUS modemStringRead ( PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * rsp, UINT rspBufLen, UINT timeout ) { UCHAR readChar; UINT readCharCount = 0; UINT timerStart = 0; UINT clockRate = 0; UINT currentSysClkTick; UINT timeoutTicks = 0; UINT elapsedTicks = 0; int secondsLeft; timerStart = tickGet(); clockRate = sysClkRateGet(); timeoutTicks = timeout * clockRate; while (TRUE) { static BOOL crCharReceived = FALSE; /* check against the timeout */ currentSysClkTick = tickGet (); if (currentSysClkTick >= timerStart) elapsedTicks = currentSysClkTick - timerStart; else /* handle tick wraparound */ elapsedTicks = ULONG_MAX - timerStart + currentSysClkTick; if (elapsedTicks >= timeoutTicks) { MODEM_DEBUG_PRINT (("modem: timeout waiting for response\n")); return (ERROR); } /* read the response */ secondsLeft = (int) ((timeoutTicks - elapsedTicks) / clockRate); if (pModemInterface->modemRead != NULL) if (pModemInterface->modemRead (pObjState, (char *)&readChar, 1, secondsLeft) == ERROR) { MODEM_DEBUG_PRINT (("modem error %x reading response \n", errno)); return (ERROR); } if ((readChar < ' ') && (readChar != '\r') && (readChar != '\n')) { MODEM_LOG_MSG ("modem: ignoring control character %x \n", (int) readChar, 0, 0, 0, 0, 0); continue; } if (readChar == '\r') { MODEM_LOG_MSG ("read 0x%x byte\n", (int) readChar, 0, 0, 0, 0, 0); crCharReceived = TRUE; continue; } if ((readChar == '\n') && crCharReceived) { MODEM_LOG_MSG ("read 0x%x byte\n", (int) readChar, 0, 0, 0, 0, 0); MODEM_DEBUG_PRINT (("response = %s \n", rsp)); crCharReceived = FALSE; /* null-terminate the response */ rsp [readCharCount] = '\0'; return (OK); } /* store read character */ rsp [readCharCount++] = readChar; /* check on the buffer lenght */ if (readCharCount == (rspBufLen - 1)) { rsp [readCharCount] = '\0'; return (OK); } MODEM_LOG_MSG ("read 0x%x byte\n", readChar, 0, 0, 0, 0, 0); } } /***************************************************************************** modemCommand - issue a command to a modem* * This routine issues a command to a modem and waits up to a timeout in seconds* for a response of up to rspBufLen characters. It finally compares the* response with the expected one.** RETURNS: OK on success, ERROR otherwise*/LOCAL STATUS modemCommand ( PFW_PLUGIN_OBJ_STATE * pObjState, PPP_MODEM_INTERFACE * pModemInterface, char * cmd, char * rsp, UINT rspBufLen, char * goodRsp, UINT timeout ) { BOOL echoResponse = TRUE; int i = 0; if (cmd!=NULL) { /* flush any responses to previous commands or other data */ if (pModemInterface->modemIoctl != NULL) if ((* pModemInterface->modemIoctl) (pObjState, MODEM_FLUSH, 0) == ERROR) return (ERROR); MODEM_DEBUG_PRINT (("modem: command = %s\n", cmd)); if (pModemInterface->modemWrite != NULL) if (pModemInterface->modemWrite (pObjState, cmd, strlen (cmd)) != OK) { MODEM_LOG_MSG ("modem: error %d sending command\n", errno, 0, 0, 0, 0, 0); return (ERROR); } } /* do we need a response? */ if (rsp==NULL || !rspBufLen) { return (OK); } while (echoResponse) { if (modemStringRead (pObjState, pModemInterface, rsp, rspBufLen, timeout) == ERROR) return (ERROR); MODEM_DEBUG_PRINT (("modem response %s to command %s \n", rsp, cmd)); /* is echo on? */ if (cmd!=NULL && !strncmp (cmd, rsp, strlen (cmd)-1)) { MODEM_LOG_MSG ("modem: ignoring echoed command\n", 0, 0, 0, 0, 0, 0); *rsp = '\0'; continue; } if (goodRsp != NULL) if (strncmp (rsp, goodRsp, strlen (goodRsp)) != 0) /* !OK */ { if (++i > MODEM_MAX_RESPONSES) { MODEM_DEBUG_PRINT (("modem: ERROR %d bad responses received\n", MODEM_MAX_RESPONSES)); return ERROR; } else { MODEM_LOG_MSG ("modem: ignoring response: \n", 0, 0, 0, 0, 0, 0); continue; } } MODEM_DEBUG_PRINT (("modem: good response = %s\n", rsp)); echoResponse = FALSE; } return (OK); }/***************************************************************************** modemConnectTest - test connection with a modem* * This routine tests connection with a modem by filling up all the relevant* fields in the MODEM_CONN_DESC structure, and then calling the modem * connection routine.** The parameter <initString> may be specified to the initialization string* most suitable for the modem being used. If this parameter is NULL, a default* initialization string for a USRobotics Sportster modem is used.* The parameter <modemNumber> should point to a string including the number* the user wishes to call. There is no default value for it.* The parameter <modemDialResponse> should point at a string representing the* expected return code for a successful dial-up connection. A default * connection string is provided, should this parameter be invalid.** RETURNS: OK on success, ERROR otherwise* * NOMANUAL*/STATUS modemConnectTest ( char * initString, char * modemNumber, char * modemDialResponse, PFW_STACK_OBJ * pModemStack, PPP_UPCALL_FUNCTIONS * pUpcalls ) { MODEM_CONN_DESC modemConnDesc; STATUS result; if (pModemStack == NULL) { MODEM_DEBUG_PRINT (("ERROR: NULL stack\n")); return (ERROR); } if (modemNumber == NULL || modemNumber [0] == 0) { MODEM_DEBUG_PRINT (("ERROR: NULL telephone number\n")); return (ERROR); } modemConnDesc.modemDialTimeout = MODEM_TIMEOUT_DIAL; modemConnDesc.modemCmdTimeout = MODEM_TIMEOUT_CMD; modemConnDesc.modemNumber = malloc (100); modemConnDesc.modemInitString = malloc (100); modemConnDesc.modemDialResponse = malloc (100); if (initString != NULL && initString [0] != 0) strcpy (modemConnDesc.modemInitString, initString); else strcpy (modemConnDesc.modemInitString, USR_SPORTSTER_DEF_INIT_STRING); if (modemNumber != NULL && modemNumber [0] != 0) strcpy (modemConnDesc.modemNumber, modemNumber); else return (ERROR); if (modemDialResponse != NULL && modemDialResponse [0] != 0) strcpy (modemConnDesc.modemDialResponse, modemDialResponse); else strcpy (modemConnDesc.modemDialResponse, MODEM_CONNECT); modemConnDesc.modemConnCallback = (VOIDFUNCPTR) modemConnCallback; modemConnDesc.modemDisconnCallback = (VOIDFUNCPTR) modemDisconnCallback; modemConnDesc.modemConnParm1 = (void *) pModemStack; modemConnDesc.modemConnParm2 = (void *) pUpcalls; modemConnDesc.modemDisconnParm = (void *) pModemStack; result = modemConnect (pModemStack, &modemConnDesc); free (modemConnDesc.modemNumber); free (modemConnDesc.modemInitString); free (modemConnDesc.modemDialResponse); if (result == ERROR) MODEM_DEBUG_PRINT (("failed to connect to modem \n"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -