分类: C/C++
2008-04-02 13:15:33
北京理工大学 20981 陈罡
最近比较忙,没有时间更新blog了,忙里偷闲跑出来冒个泡泡贴点东西。
前段时间贴了关于s60 2nd平台创建cmwap接入点的方法,很多朋友问我如何创建
基于cmnet的接入点,其实基本上是完全一样的,所不同的是需要把proxy设置相关
的地方给去掉即可(我们都是中国移动的“好孩子”)。
_LIT(KCmnetIapName, "CMNET For XXXXXX") ;
TUint32 CSocketsEngine::CreateCmnetIap()
{
#ifdef __WINS__
return -1 ;
#else
TCommDbOpeningMethod OpeningMethod;
TInt error;
// gprs id
TUint32 GPRSId;
// open comm db
CCommsDatabase* commsDb = CCommsDatabase::NewL(EDatabaseTypeIAP,OpeningMethod);
CleanupStack::PushL(commsDb);
// open OUTGOING_GPRS table
CCommsDbTableView* commsView = commsDb->OpenTableLC(TPtrC(OUTGOING_GPRS));
//insert a new record which id is GPRSId
TRAP(error,commsView->InsertRecord(GPRSId));
if (error!=KErrNone) return -1;
commsView->WriteTextL(TPtrC(COMMDB_NAME),KCmnetIapName);
commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), ETrue);
commsView->WriteBoolL(TPtrC(GPRS_IP_ADDR_FROM_SERVER), ETrue);
commsView->WriteBoolL(TPtrC(GPRS_IP_DNS_ADDR_FROM_SERVER), ETrue);
commsView->WriteTextL(TPtrC(GPRS_IP_GATEWAY), _L("0.0.0.0"));
commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_NAME), _L(""));
commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_PASS),_L(""));
commsView->WriteTextL(TPtrC(GPRS_APN), _L("cmnet"));
commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), EFalse);
commsView->WriteTextL(TPtrC(GPRS_IF_NETWORKS), _L("ip"));
commsView->WriteBoolL(TPtrC(GPRS_HEADER_COMPRESSION), EFalse);
commsView->WriteBoolL(TPtrC(GPRS_DATA_COMPRESSION), EFalse);
commsView->WriteUintL(TPtrC(GPRS_REQ_PRECEDENCE), 0);
commsView->WriteUintL(TPtrC(GPRS_REQ_DELAY), 0);
commsView->WriteUintL(TPtrC(GPRS_REQ_RELIABILITY), 0);
commsView->WriteUintL(TPtrC(GPRS_REQ_PEAK_THROUGHPUT), 0);
commsView->WriteUintL(TPtrC(GPRS_REQ_MEAN_THROUGHPUT), 0);
commsView->WriteUintL(TPtrC(GPRS_MIN_PRECEDENCE), 0);
commsView->WriteUintL(TPtrC(GPRS_MIN_DELAY), 0);
commsView->WriteUintL(TPtrC(GPRS_MIN_RELIABILITY), 0);
commsView->WriteUintL(TPtrC(GPRS_MIN_PEAK_THROUGHPUT), 0);
commsView->WriteUintL(TPtrC(GPRS_MIN_MEAN_THROUGHPUT), 0);
commsView->WriteBoolL(TPtrC(GPRS_ANONYMOUS_ACCESS), EFalse);
commsView->WriteBoolL(TPtrC(GPRS_USE_EDGE), EFalse);
commsView->WriteBoolL(TPtrC(GPRS_ENABLE_LCP_EXTENSIONS), EFalse);
commsView->WriteBoolL(TPtrC(GPRS_DISABLE_PLAIN_TEXT_AUTH), EFalse);
commsView->WriteUintL(TPtrC(GPRS_AP_TYPE), 0);
commsView->WriteUintL(TPtrC(GPRS_QOS_WARNING_TIMEOUT), -1);
commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
commsView->WriteTextL(TPtrC(GPRS_PDP_ADDRESS), _L(""));
commsView->WriteTextL(TPtrC(GPRS_IF_PARAMS), _L(""));
commsView->WriteUintL(TPtrC(GPRS_IF_AUTH_RETRIES), 0);
commsView->WriteTextL(TPtrC(GPRS_IP_NETMASK), _L(""));
commsView->WriteTextL(TPtrC(GPRS_IP_ADDR),_L("0.0.0.0"));
commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER1),_L("0.0.0.0"));
commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER2),_L("0.0.0.0"));
// tell the view , record changed
// before new added record, we must call this function
TRAP(error,commsView->PutRecordChanges());
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// create NETWORK table record
TUint32 networkId;
commsView = commsDb->OpenTableLC(TPtrC(NETWORK));
TRAP(error,commsView->InsertRecord(networkId));
if (error!=KErrNone) return -1;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
TRAP(error,commsView->PutRecordChanges(EFalse, EFalse));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// find Mobile LOCATION id
TInt result;
TUint32 locationId;
TUint32 mobileLocationId;
commsView = commsDb->OpenTableLC(TPtrC(LOCATION));
result = commsView->GotoFirstRecord();
TBuf<128> locationName;
while (result == KErrNone) {
commsView->ReadTextL(TPtrC(COMMDB_NAME), locationName);
commsView->ReadUintL(TPtrC(LOCATION_MOBILE), locationId);
if (locationName.Match(_L("Mobile"))!= KErrNotFound)
mobileLocationId = locationId;
result = commsView->GotoNextRecord();
}
CleanupStack::PopAndDestroy(commsView);
// create IAP
TUint32 iapId;
commsView = commsDb->OpenTableLC(TPtrC(IAP));
TRAP(error,commsView->InsertRecord(iapId));
if (error!=KErrNone) return -1;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
commsView->WriteTextL(TPtrC(IAP_SERVICE_TYPE), TPtrC(OUTGOING_GPRS));
commsView->WriteUintL(TPtrC(IAP_SERVICE), GPRSId);
commsView->WriteUintL(TPtrC(IAP_NETWORK_WEIGHTING), 0);
commsView->WriteUintL(TPtrC(IAP_NETWORK), networkId);
commsView->WriteUintL(TPtrC(IAP_BEARER), 2);
commsView->WriteTextL(TPtrC(IAP_BEARER_TYPE), TPtrC(MODEM_BEARER));
commsView->WriteUintL(TPtrC(IAP_LOCATION), mobileLocationId);
TRAP(error,commsView->PutRecordChanges());
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// create cmnet ap
TUint32 wapId;
commsView = commsDb->OpenTableLC(TPtrC(WAP_ACCESS_POINT));
TRAP(error,commsView->InsertRecord(wapId));
if (error!=KErrNone) return 2048;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
commsView->WriteTextL(TPtrC(WAP_CURRENT_BEARER), TPtrC(WAP_IP_BEARER));
TRAP(error,commsView->PutRecordChanges());
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// create WAP_IP_BEARER
TUint32 wapIPId;
commsView = commsDb->OpenTableLC(TPtrC(WAP_IP_BEARER));
TRAP(error,commsView->InsertRecord(wapIPId));
if (error!=KErrNone) return -1;
commsView->WriteUintL(TPtrC(WAP_ACCESS_POINT_ID), wapId);
commsView->WriteTextL(TPtrC(WAP_GATEWAY_ADDRESS), _L("0.0.0.0"));
commsView->WriteUintL(TPtrC(WAP_WSP_OPTION),EWapWspOptionConnectionOriented);
commsView->WriteBoolL(TPtrC(WAP_SECURITY), EFalse);
commsView->WriteUintL(TPtrC(WAP_IAP),iapId);
commsView->WriteUintL(TPtrC(WAP_PROXY_PORT), 0);
commsView->WriteTextL(TPtrC(WAP_PROXY_LOGIN_NAME), _L(""));
commsView->WriteTextL(TPtrC(WAP_PROXY_LOGIN_PASS), _L(""));
TRAP(error,commsView->PutRecordChanges(EFalse, EFalse));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
CStoreableOverrideSettings* overrides = CStoreableOverrideSettings::NewL(CStoreableOverrideSettings::EParamListFull);
CleanupStack::PushL(overrides);
CCommsDbConnectionPrefTableView::TCommDbIapConnectionPref pref;
pref.iRanking = 1;
pref.iDirection = ECommDbConnectionDirectionOutgoing;
pref.iDialogPref = ECommDbDialogPrefDoNotPrompt;
pref.iBearer.iBearerSet = ECommDbBearerGPRS;
pref.iBearer.iIapId=iapId;
TRAP(error,overrides->SetConnectionPreferenceOverride(pref));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(overrides);
CleanupStack::PopAndDestroy(commsDb);
return iapId ;
#endif
}
void CSocketsEngine::ConnectSmoothCMNET()
{
#ifdef __WINS__
User::LeaveIfError(iSocketServ.Connect()) ;
#else
TBuf<20> iap_name_buf ;
TBuf<20> cmnet_str ;
TUint32 iap_id = -1 ; // set invalid iap first
cmnet_str.Append(KCmnetIapName) ;
// prepare conn
User::LeaveIfError(iSocketServ.Connect());
User::LeaveIfError(iConn.Open(iSocketServ)) ;
// visit commdb for iap
CCommsDatabase* const comm_db = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(comm_db);
// visit the table of comm_db
CCommsDbTableView *iap_table_view = comm_db->OpenTableLC(TPtrC(IAP));
// switch pointer to the first iap record
if(iap_table_view->GotoFirstRecord() == KErrNone) {
// search comm_db for cmnet iap
do {
// get the iap name from comm_db
iap_name_buf.SetLength(0) ;
iap_table_view->ReadTextL(TPtrC(COMMDB_NAME), iap_name_buf);
// test the cmnet iap name
if((iap_name_buf.Find(cmnet_str) != KErrNotFound)) {
iap_table_view->ReadUintL(TPtrC(COMMDB_ID), iap_id) ;
break ;
}
} while (KErrNone == iap_table_view->GotoNextRecord()) ;
}
CleanupStack::PopAndDestroy(2, comm_db); // iap_table_view, comm_db
// make sure the iap is valid
if(iap_id == -1) iap_id = CreateCmnetIap() ;
// use the iap, construct preference
TCommDbConnPref conn_pref;
conn_pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt) ;
conn_pref.SetDirection(ECommDbConnectionDirectionOutgoing) ;
conn_pref.SetBearerSet(ECommDbBearerGPRS) ;
conn_pref.SetIapId(iap_id) ;
// perform the smooth connection
iConn.Start(conn_pref);
#endif
}
使用的时候只需要在CSocketEngine::ConstructL()中加入这个即可。
void CSocketsEngine::ConstructL(TInt down_type)
{
ChangeStatus(ENotConnected);
// Start a timer
iTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);
CActiveScheduler::Add(this);
// Open channel to Socket Server
if(down_type == HTTP_DOWN_CMNET)
ConnectSmoothCMNET() ;
else
ConnectSmoothCMWAP() ;
// Create socket read and write active objects
iSocketsReader = CSocketsReader::NewL(*this, iSocket);
iSocketsWriter = CSocketsWriter::NewL(*this, iSocket);
}
这些都是在6670真机上测试过的代码,应该不会有问题。
希望可以帮助到对此还有困扰的朋友,这个是断点续传的测试安装包,有兴趣的朋友可以
实验一下:
|