📄 wlansampleexappui.cpp
字号:
/* ====================================================================
* File: WLanSampleExAppUi.cpp
* Created: 01/19/07
* Author:
* Copyright (c): All rights reserved
* ==================================================================== */
#include <avkon.hrh>
#include <rconnmon.h>
#include <apaccesspointitem.h>
#include <apdatahandler.h>
#include <CommDbConnPref.h>
#include <apselect.h>
#include <wlancdbcols.h>
#include <wlanmgmtclient.h>
#include <in_sock.h>
#include "WLanSampleEx.pan"
#include "WLanSampleExAppUi.h"
#include "WLanSampleExAppView.h"
#include "WLanSampleEx.hrh"
// ConstructL is called by the application framework
void CWLanSampleExAppUi::ConstructL()
{
BaseConstructL(EAknEnableSkin);
iAppView = CWLanSampleExAppView::NewL(ClientRect());
AddToStackL(iAppView);
}
CWLanSampleExAppUi::CWLanSampleExAppUi()
{
// no implementation required
}
CWLanSampleExAppUi::~CWLanSampleExAppUi()
{
if (iAppView)
{
RemoveFromStack(iAppView);
delete iAppView;
iAppView = NULL;
}
iConnection.Close();
iServer.Close();
}
// handle any menu commands
void CWLanSampleExAppUi::HandleCommandL(TInt aCommand)
{
switch(aCommand)
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
case EWLanSampleExCommand1:
{
NetworkNameL();
}
break;
case EWLanSampleExCommand2:
{
ScanNetworksL();
}
break;
case EWLanSampleExCommand3:
{
CreateAccessPointL();
}
break;
case EWLanSampleExCommand4:
{
EstablishConnectionL();
}
break;
case EWLanSampleExCommand5:
{
GetMacAddressL();
}
break;
case EWLanSampleExCommand6:
{
GetIpAddressL();
}
break;
case EWLanSampleExCommand7:
{
GetCurrentSignalStrengthL();
}
break;
default:
Panic(EWLanSampleExBasicUi);
break;
}
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::CreateAccessPointL()
// Creates a sample wlan access point with security settings
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::CreateAccessPointL()
{
TInt errorCode;
TInt error;
CCommsDatabase* db = CCommsDatabase::NewL();
CleanupStack::PushL(db);
TInt err = db->BeginTransaction();
// Create a wlan AP and fill its mandatory fields.
CApAccessPointItem* wlanAp = CApAccessPointItem::NewLC();
CApAccessPointItem* wlanNewItem=CApAccessPointItem::NewLC();
wlanAp->SetNamesL(_L("wcdma"));
wlanAp->SetBearerTypeL(EApBearerTypeWLAN);
wlanAp->WriteTextL(EApWlanNetworkName, _L("L!nkSys@MdpNok!a")); // Give corrct Data as per the router configuration //L!nkSys@MdpNok!a
//Security mode should be 2 for WEP network
//1 for Open network
//4 for E802_1x
//8 for EWpa
//16 for EWpa2
wlanAp->WriteUint(EApWlanSecurityMode,2);
//Lan network mode should be 1 for infrastructure, 0 for adhoc
wlanAp->WriteUint(EApWlanNetworkMode,1);
// Store it into the iDb.
TBool val =ETrue;
TUint32 newalanid;
CApDataHandler* handler = CApDataHandler::NewLC(*db);
TUint32 apid;
apid = handler->CreateFromDataL(*wlanAp);
handler->UpdateAccessPointDataL(*wlanAp,val);
handler->AccessPointDataL(apid,*wlanNewItem);
wlanNewItem->ReadUint( EApIapServiceId, newalanid );
wlanNewItem->ReadTextL( EApWlanNetworkName, ibuff );
TBuf8<50> keyData;
keyData.Copy(_L("5F89AE0A3B9D5C1123F81F0A41")); // Give corrct Data as per the router configuration
CCommsDbTableView* wLanServiceTable = db->OpenViewMatchingUintLC(
TPtrC( WLAN_SERVICE ), TPtrC( WLAN_SERVICE_ID ), newalanid );
errorCode = wLanServiceTable->GotoFirstRecord();
if ( errorCode == KErrNone )
{
wLanServiceTable->UpdateRecord();
}
else
{
TUint32 dummyUid( KUidNone );
User::LeaveIfError( wLanServiceTable->InsertRecord( dummyUid ) );
// Save link to LAN service
wLanServiceTable->WriteUintL( TPtrC( WLAN_SERVICE_ID ), apid );
}
// Save first WEP key
TRAP(error,wLanServiceTable->WriteTextL( TPtrC( WLAN_WEP_KEY1 ),
keyData ));
// Save second WEP key
TRAP(error,wLanServiceTable->WriteTextL( TPtrC( WLAN_WEP_KEY2 ),
keyData ));
// Save third WEP key
TRAP(error,wLanServiceTable->WriteTextL( TPtrC( WLAN_WEP_KEY3 ),
keyData ));
// Save fourth WEP key
TRAP(error,wLanServiceTable->WriteTextL( TPtrC( WLAN_WEP_KEY4 ),
keyData ));
wLanServiceTable->PutRecordChanges();
err = db->CommitTransaction(); // // End a transaction. Call after `InsertRecord()` or `UpdateRecord()`.
CleanupStack::PopAndDestroy(5);
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::ScanNetworksL()
// Scans the available WLAN network and finds the security mode and
// the network mode and BSSID
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::ScanNetworksL()
{
TBuf<10> infoBuff;
TInt securityMode;
CWlanScanInfo* scanInfo=CWlanScanInfo::NewL();
CleanupStack::PushL(scanInfo);
CWlanMgmtClient* client=CWlanMgmtClient::NewL();
CleanupStack::PushL(client);
TInt i=1;
client->GetScanResults(*scanInfo);
for(scanInfo->First(); !scanInfo->IsDone(); scanInfo->Next() )
{
ibuff.Zero();
ibuff.AppendNum(i);
CEikonEnv::InfoWinL(_L("Network"),ibuff);
i++;
securityMode=scanInfo->SecurityMode();
if(securityMode==0)
{
CEikonEnv::InfoWinL(_L("SecurityMode"),_L("Open"));
}
else if(securityMode==1)
{
CEikonEnv::InfoWinL(_L("SecurityMode"),_L("WEP Mode"));
}
else if(securityMode==2)
{
CEikonEnv::InfoWinL(_L("SecurityMode"),_L("802d1x Mode"));
}
else if(securityMode==3)
{
CEikonEnv::InfoWinL(_L("SecurityMode"),_L("WPA"));
}
//In general if the least significant bit of the capbility field
//is 1 then the network is of "Infrastructure Type" or else if the
//capability field is 0 then the network is of "Ad-hoc type"
TUint16 capability=scanInfo->Capability();
TInt pad(1);
TInt lanType=capability & pad;
if(lanType==1)
CEikonEnv::InfoWinL(_L("Network mode"),_L("Infrastructure"));
else
CEikonEnv::InfoWinL(_L("Network mode"),_L("Adhoc"));
infoBuff.Zero();
TWlanBssid bssid;
scanInfo->Bssid( bssid );
ibuff.Zero();
ibuff.Copy(bssid);
CEikonEnv::InfoWinL(_L("BSSID"),ibuff);
}
CleanupStack::PopAndDestroy(2);
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::NetworkNameL()
// Gets the available WLAN network name and its strength
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::NetworkNameL()
{
TPckgBuf<TConnMonNetworkNames> networks;
RConnectionMonitor monitor;
monitor.ConnectL();
CleanupClosePushL(monitor);
TRequestStatus status;
monitor.GetPckgAttribute(EBearerIdWLAN, 0, KNetworkNames , networks, status); // EBearerIdWLAN KNetworkNames
User::WaitForRequest(status ) ;
User::LeaveIfError(status.Int());
TBuf<20> ibuff;
TInt count = networks().iCount;
ibuff.Zero();
ibuff.AppendNum(count);
CEikonEnv::InfoWinL(ibuff,_L("Total Network found"));
for(TInt i=0;i<count;i++)
{
TBuf8<32> ssid;
ssid.Copy( networks().iNetwork[i].iName );
ibuff.Zero();
ibuff.Copy(ssid);
CEikonEnv::InfoWinL(_L("Network Name"),ibuff);
TUint8 iSignal = networks().iNetwork[i].iSignalStrength;
ibuff.Zero();
ibuff.AppendNum(iSignal);
CEikonEnv::InfoWinL(_L("Network Signal"),ibuff);
}
CleanupStack::PopAndDestroy();
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::GetMacAddressL()
// Gets the MAC address of the WLAN device
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::GetMacAddressL()
{
// MAC address of the WLAN device
RSocketServ socketServ;
User::LeaveIfError(socketServ.Connect());
CleanupClosePushL(socketServ);
// Open a socket
RSocket socket;
User::LeaveIfError(socket.Open(socketServ,
KAfInet,
KSockStream,
KProtocolInetTcp));
CleanupClosePushL(socket);
// Start enumerating the interfaces
TPckgBuf<TSoInetInterfaceInfo> info;
socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
TBuf<32> macAddr;
while(socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info) == KErrNone)
{
if(info().iName.FindF(_L("Wlan")) == KErrNotFound)
{
continue;
}
macAddr.Zero();
for( TUint i = sizeof(SSockAddr) ; i < sizeof(SSockAddr) + 6 ; i++ )
{
if(i < (TUint)info().iHwAddr.Length())
{
macAddr.AppendFormat(_L("%02X:"), info().iHwAddr[i]);
}
}
if(macAddr.Length()) // remove trailing ':'
{
macAddr.Delete(macAddr.Length()-1, 1);
}
}
CEikonEnv::InfoWinL(_L("MAC address"),macAddr);
CleanupStack::PopAndDestroy(2);
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::GetIpAddressL()
// Gets the IP address of the WLAN iConnection. Works only if there is a wlan iConnection established
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::GetIpAddressL()
{
if(iConnState)
{
// ip address of the current iConnection call after establish a iConnection
// The last in the loop is the current IP address
// To be called after the iConnection is established or else it panics
RSocketServ socketServ;
socketServ.Connect();
CleanupClosePushL(socketServ);
RSocket sock;
User::LeaveIfError(sock.Open(socketServ, KAfInet, KSockStream, KProtocolInetTcp));
CleanupClosePushL(sock);
// Get the IAP id of the underlying interface of this RConnection
TUint32 iapId = 0;
User::LeaveIfError(iConnection.GetIntSetting(_L("IAP\\Id"), iapId));
// Get IP information from the socket
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
// To find out which interfaces are using our current IAP, we must
// enumerate and go through all of them and make a query by name
// for each.
User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone)
{
ifquery.iName = ifinfo.iName;
TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
if(err == KErrNone && ifquery.iZone[1] == iapId) // IAP ID is index 1 of iZone
{
// We have found an interface using the IAP we are interested in.
if(ifinfo.iAddress.Address() > 0)
{
// found a IPv4 address
TInetAddr aAddr = ifinfo.iAddress;
ibuff.Zero();
aAddr.Output(ibuff);
} // while
}
else if(err != KErrNone)
{
}
}
CEikonEnv::InfoWinL(ibuff,_L("IP address"));
CleanupStack::PopAndDestroy(2);
}
else
CEikonEnv::InfoWinL(_L("Establish connection first"),_L(""));
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::EstablishConnectionL()
// Establishes WLAN connection with the last wlan access point from the access point list
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::EstablishConnectionL()
{
// establishes iConnection with the last wlan access point.
// It also lists all the available access point
TUint32 accessPoint(0);
CCommsDatabase* commDB = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(commDB);
CApSelect * iSelect =CApSelect::NewLC(*commDB,KEApIspTypeAll,EApBearerTypeWLAN,KEApSortUidAscending);
TBuf<50> iName;
TBool move = iSelect->MoveToFirst();
TInt icount = 0;
while(icount<iSelect->Count())
{
if(move==TRUE)
{
iName =iSelect->Name();
accessPoint=iSelect->Uid();
}
move = iSelect->MoveNext();
icount=icount+1;
}
CEikonEnv::InfoWinL(_L("Using"),iName);
User::LeaveIfError(iServer.Connect(KESockDefaultMessageSlots));
User::LeaveIfError(iConnection.Open(iServer, KConnectionTypeDefault));
TCommDbConnPref prefs;
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
prefs.SetDirection(ECommDbConnectionDirectionOutgoing);
prefs.SetIapId(accessPoint);
User::LeaveIfError(iConnection.Start(prefs));
iConnState = ETrue;
CleanupStack::PopAndDestroy(2);
}
// ----------------------------------------------------------------------------
// CWLanSampleExAppUi::GetCurrentSignalStrengthL()
// Get the signal strength of the currently established iConnection
// To be called after the iConnection is established or else it panics
// ----------------------------------------------------------------------------
//
void CWLanSampleExAppUi::GetCurrentSignalStrengthL()
{
RConnectionMonitor monitor;
monitor.ConnectL();
CleanupClosePushL(monitor);
TRequestStatus status;
TInt32 iSignalStrength;
TUint iCount;
monitor.GetConnectionCount(iCount,status);
User::WaitForRequest(status ) ;
User::LeaveIfError(status.Int());
TBuf<20> ibuff;
TUint connectId;
TUint subId;
monitor.GetConnectionInfo(iCount,connectId,subId);
monitor.GetIntAttribute(connectId,0 , KSignalStrength , (TInt &)iSignalStrength, status); // EBearerIdWLAN KNetworkNames
User::WaitForRequest(status ) ;
ibuff.Zero();
ibuff.AppendNum(iSignalStrength);
CEikonEnv::InfoWinL(ibuff,_L("Signal1"));
User::LeaveIfError(status.Int());
CleanupStack::PopAndDestroy();
}
// End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -