Auto select internet access point programmatically.We use RGenericAgent and CActive to form an engine based on observer pattern which is most used in symbian app development.
The following is extracted from my project.
Notes:It is rather simplified and only tested on UIQ platform.Use it at your own risk.
1.IPAEngine.hrh
#ifndef __IPAENGINE_HRH
#define __IPAENGINE_HRH
enum TIAPConnectEngineStates // Engine states
{
EIAPConnectStateNotConnected,
EIAPConnectStateConnecting,
EIAPConnectStateConnected,
EIAPConnectStateDisconnecting
};
#endif
2.IAPConnectStateObserver.h
#ifndef __IAPCONNECTSTATEOBSERVER_H
#define __IAPCONNECTSTATEOBSERVER_H
#include <e32std.h>
class MIAPConnectStateObserver
{
public:
// Notifies the changed state
virtual void ChangeStateL(TInt aState) = 0;
};
#endif // __IAPCONNECTSTATEOBSERVER_H
3.CIAPConnectEngine.h
#ifndef _CIAPCONNECTENGINE_H
#define _CIAPCONNECTENGINE_H
#include <e32base.h>
#include <nifman.h>
#include <AgentClient.h>
#include "IAPConnectStateObserver.h"
// FORWARD DECLARATIONS;
class RGenericAgent;
class CStoreableOverrideSettings;
// CONSTANTS
const TUint32 KUndefinedIAPid = 0x00;
class CIAPConnectEngine : public CActive
{
public:
static CIAPConnectEngine* NewL(MIAPConnectStateObserver* aObserver);
static CIAPConnectEngine* NewLC(MIAPConnectStateObserver* aObserver);
~CIAPConnectEngine();
void ConnectL();
void ReleaseL();
void TerminateConnectionL();
void SetConnectionPreferences(TInt aBearer, TBool aDialog, TUint32 aIapId=KUndefinedIAPid);
protected:
// from CActive
void RunL();
void DoCancel();
private:
// Constructor
CIAPConnectEngine(MIAPConnectStateObserver* aObserver);
void ConstructL();
TInt IsDisconnected(TUint32 &aIap);
private:
RGenericAgent iConnMgr;
TInt iState;
CCommsDbConnectionPrefTableView::TCommDbIapConnectionPref iPref;
MIAPConnectStateObserver* iObserver;
CStoreableOverrideSettings *cs;
};
#endif
4.CIAPConnectEngine.cpp
#include "CIAPConnectEngine.h"
#include "IAPConnectStateObserver.h"
#include <eikenv.h>
#include "IpaEngine.hrh"
const TUint32 KBearerSetUnknown = 1000;
CIAPConnectEngine* CIAPConnectEngine::NewL(MIAPConnectStateObserver* aObserver)
{
CIAPConnectEngine* self = NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
CIAPConnectEngine* CIAPConnectEngine::NewLC(MIAPConnectStateObserver* aObserver)
{
CIAPConnectEngine* self = new (ELeave) CIAPConnectEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CIAPConnectEngine::CIAPConnectEngine(MIAPConnectStateObserver* aObserver)
: CActive(CActive::EPriorityStandard),
iState(EIAPConnectStateNotConnected),
iObserver(aObserver)
{
SetConnectionPreferences(ECommDbBearerUnknown, ETrue);
}
CIAPConnectEngine::~CIAPConnectEngine()
{
delete cs;
Cancel();
iConnMgr.Close();
}
void CIAPConnectEngine::ConstructL()
{
cs = CStoreableOverrideSettings::NewL(CStoreableOverrideSettings::EParamListFull);
iConnMgr.Open();
CActiveScheduler::Add(this);
}
void CIAPConnectEngine::ConnectL()
{
iState=EIAPConnectStateConnecting,
iObserver->ChangeStateL(iState);
TUint32 iap;
if(!IsDisconnected(iap))
{
if(iap==iPref.iBearer.iIapId)
{
iState=EIAPConnectStateConnected;
iObserver->ChangeStateL(iState);
return;
}else
TerminateConnectionL();
}
if (!IsActive())
{
iStatus = KRequestPending;
cs->SetConnectionPreferenceOverride(iPref);
iConnMgr.StartOutgoing(*cs,iStatus);
SetActive();
}
}
void CIAPConnectEngine::RunL()
{
if(iStatus==KErrNone)
{
iState=EIAPConnectStateConnected;
iObserver->ChangeStateL(iState);
}
}
void CIAPConnectEngine::DoCancel()
{
iState = EIAPConnectStateNotConnected;
iObserver->ChangeStateL(iState);
}
void CIAPConnectEngine::ReleaseL()
{
TUint32 iap;//Not used
if (!IsDisconnected(iap))
{
Cancel();
iState = EIAPConnectStateNotConnected;
iObserver->ChangeStateL(iState);
}
}
void CIAPConnectEngine::TerminateConnectionL()
{
TUint32 iap;
if (!IsDisconnected(iap))
{
User::LeaveIfError(iConnMgr.Stop());
iState = EIAPConnectStateNotConnected;
iObserver->ChangeStateL(iState);
}
}
TInt CIAPConnectEngine::IsDisconnected(TUint32& aIap)
{
return iConnMgr.GetActiveIntSetting(TPtrC(IAP),TPtrC( COMMDB_ID),aIap);
}
void CIAPConnectEngine::SetConnectionPreferences(TInt aBearer,
TBool aDialog, TUint32 aIapId)
{
iPref.iRanking = 1;
iPref.iDirection = ECommDbConnectionDirectionOutgoing;
aDialog ? iPref.iDialogPref = ECommDbDialogPrefPrompt : iPref.iDialogPref = ECommDbDialogPrefDoNotPrompt;
CCommsDbConnectionPrefTableView::TCommDbIapBearer bearer;
bearer.iBearerSet = aBearer;
bearer.iIapId = aIapId;
iPref.iBearer = bearer;
}