?? wcegprs.cpp
字號:
// ----------------------------------------------------------------------------// Copyright 2006-2007, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description:// WindowsCE specific controls for GPRS connectivity// Notes:// - This section is for the WindowsCE/Mobile implementation.// - I'm still experimenting with the method used to invoke the GPRS connection// facility of the WindowsCE platform. If you see something that should be// changed here, please let me know.// - Note: even though the following appears to be gprs-centric, there is // nothing here that mandates that the connection necessarily be GPRS. [It// could be any other 'connection' name defined by PROP_COMM_CONNECTION].// - Modem may need initialization string:// IE. AT+CGDCONT=1,"IP","isp.cingular"// AT&W// ---// Change History:// 2007/01/28 Martin D. Flynn// -Initial release// 2007/03/11 Martin D. Flynn// -Increased MODEM_SHORT_RESET_TIMEOUT to 6 hours // -Increased MODEM_LONG_RESET_TIMEOUT to 36 hours // ----------------------------------------------------------------------------#include "stdafx.h" // TARGET_WINCE#include "custom/defaults.h"#if defined(TARGET_WINCE)// ----------------------------------------------------------------------------#include <stdlib.h>#include <string.h>#include <ctype.h>#include <winsock2.h>#include <ras.h>#include <raserror.h>#include <pm.h>#include "custom/log.h"#include "custom/startup.h"#include "custom/wince/wceos.h"#include "custom/wince/wcegprs.h"#include "tools/stdtypes.h"#include "tools/strtools.h"#include "tools/bintools.h"#include "tools/utctools.h"#include "tools/threads.h" // for 'threadSleepMS'#include "tools/comport.h"#include "tools/sockets.h"#include "base/props.h"#include "base/propman.h"#include "base/packet.h"// ----------------------------------------------------------------------------#if !defined(MODEM_SHORT_RESET_TIMEOUT)# define MODEM_SHORT_RESET_TIMEOUT HOUR_SECONDS(6) // ERROR_PORT_NOT_AVAILABLE errors#endif#if !defined(MODEM_LONG_RESET_TIMEOUT)# define MODEM_LONG_RESET_TIMEOUT HOUR_SECONDS(36) // any non-connectivity reason#endif#if !defined(MODEM_CONTINUE_TIMEOUT)# define MODEM_CONTINUE_TIMEOUT MINUTE_SECONDS(15)#endif#define DEFAULT_ENTRY_NAME "GPRS"// ----------------------------------------------------------------------------static TimerSec_t _wceResetTimerLong = 0L;static TimerSec_t _wceResetTimerShort = 0L;// ----------------------------------------------------------------------------/* WinCE initialization */static utBool _didInit = utFalse;void wceGprsInitialize(){ /* already initialized */ if (_didInit) { return; } _didInit = utTrue; /* clear errors */ _wceResetTimerLong = 0L; _wceResetTimerShort = 0L;}// ----------------------------------------------------------------------------/* hard reset the modem */utBool wceGprsResetModem(){ logWARNING(LOGSRC,"TODO: Reset GPRS modem ..."); // TODO: Change this to only reset the modem/phone return utFalse; // false, until this is properly implemented}// ----------------------------------------------------------------------------/* get connection matching PROP_COMM_CONNECTION */HRASCONN wceGprsGetConnection(const char *entryName){ /* default entry name */ if (!entryName || !*entryName) { entryName = propGetString(PROP_COMM_CONNECTION, DEFAULT_ENTRY_NAME); } /* wide-char entryName */ TCHAR wEntryName[RAS_MaxEntryName + 1]; strWideCopy(wEntryName, RAS_MaxEntryName, entryName, -1); /* enumerate connections */ RASCONN rasConn[10]; // should be more than enough memset(rasConn, 0, sizeof(rasConn)); rasConn[0].dwSize = sizeof(rasConn[0]); DWORD rasSize = sizeof(rasConn), rasCnt = 0L; DWORD err = RasEnumConnections(rasConn, &rasSize, &rasCnt); if (!err) { DWORD i; for (i = 0L; i < rasCnt; i++) { if (_wcsicmp(wEntryName, rasConn[i].szEntryName) == 0) { // Just because we've found a connection, doesn't means we're actually still // connected. The phone can be 'off' (on PDA phones where the 'phone' can be // powered separately) and we can still find an existing connection. return rasConn[i].hrasconn; } } } /* not found */ //logINFO(LOGSRC,"No existing connection: %ls", wEntryName); return NULL; }/* list existing entry names */static void wceGprsListEntryNames(){ RASENTRYNAME rasEntryName[10]; // <-- should be more than enough memset(&rasEntryName, 0, sizeof(rasEntryName)); rasEntryName[0].dwSize = sizeof(rasEntryName[0]); DWORD rasEntryNameSize = sizeof(rasEntryName); DWORD rasEntryCount = 0; WSASetLastError(0); DWORD entErr = RasEnumEntries(NULL, NULL, rasEntryName, &rasEntryNameSize, &rasEntryCount); if (entErr == ERROR_SUCCESS) { int i; logINFO(LOGSRC,"Entry names:"); for (i = 0; i < (int)rasEntryCount; i++) { logINFO(LOGSRC," %d) %ls", (i+1), rasEntryName[i].szEntryName); } } else { logINFO(LOGSRC,"Unable to enumerate entry names [%d]", WSAGetLastError()); }}// ----------------------------------------------------------------------------/* check to see if we have a connection that matches PROP_COMM_CONNECTION */utBool wceGprsIsConnected(HRASCONN rasConn){ /* check for open connection */ if (rasConn != NULL) { RASCONNSTATUS rasConnStat; memset(&rasConnStat, 0, sizeof(rasConnStat)); rasConnStat.dwSize = sizeof(rasConnStat); DWORD err = RasGetConnectStatus(rasConn, &rasConnStat); if (err) { // Errors encountered (see 'raserror.h'/'winerror.h' for a complete list) // 6 ERROR_INVALID_HANDLE if (err != ERROR_INVALID_HANDLE) { logWARNING(LOGSRC,"Error retrieving connect status [%ld]", (Int32)err); } return utFalse; } else if (rasConnStat.rasconnstate == RASCS_Connected) { return utTrue; } else { return utFalse; } } /* no existing connection */ return utFalse;}// ----------------------------------------------------------------------------/* disconnect the connection that we created */static int _wceIsDisconnecting = 0;utBool wceGprsDisconnect(HRASCONN rasConn){ utBool rtn = utTrue; if (rasConn != NULL) { if (_wceIsDisconnecting > 0) { // already disconnecting? logWARNING(LOGSRC,"******* Disconnect already in-process !!!"); rtn = utFalse; } else { // We've seen this section lock up! _wceIsDisconnecting++; DWORD err = RasHangUp(rasConn); threadSleepMS(4000L); // make sure that the port has time to close if (err != SUCCESS) { logWARNING(LOGSRC,"******* Disconnect Failed !!!"); rtn = utFalse; } else { // wait here until we get disconnect confirmation, or timeout RASCONNSTATUS rsc; memset(&rsc, 0, sizeof(rsc)); rsc.dwSize = sizeof(rsc); long timeout = 6000L; // maximum wait time long interval = 2000L; // sleep interval for (;timeout > 0L; timeout -= interval) { if (RasGetConnectStatus(rasConn,&rsc) == ERROR_INVALID_HANDLE) { logINFO(LOGSRC,"Disconnected"); break; } threadSleepMS(interval); } if (timeout <= 0L) { logWARNING(LOGSRC,"******* Disconnect Timeout !!!"); rtn = utFalse; } else { rtn = utTrue; } } _wceIsDisconnecting--; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -