Auto select internet access point(Symbian/UIQ)

來源:互聯網
上載者:User

    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;

    }

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.