bluetooth/btstack/linkmgr/PhysicalLinkHelper.h
changeset 0 29b1cd4cb562
child 45 99439b07e980
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef PHYSICALLINKHELPER_H_
       
    17 #define PHYSICALLINKHELPER_H_
       
    18 
       
    19 #include "debug.h"
       
    20 #include <bt_sock.h>
       
    21 #include "physicallinks.h"
       
    22 #include "linkutil.h"
       
    23 #include <bluetooth/hci/hciutil.h> 
       
    24 
       
    25 // watchdog for first half of the SM including:
       
    26 // DisablingLPM, DisablingEncryption, RoleSwitch
       
    27 const TUint KTimeoutRoleSwitch =   3000000;  // 3 s; 
       
    28 
       
    29 // watchdog for EnablingEncryption
       
    30 const TUint KTimeoutOneCommand =   2000000;  // 2 s;
       
    31  
       
    32 class TRoleSwitcherState;
       
    33 
       
    34 NONSHARABLE_CLASS(CRoleSwitcher) : public CTimer, public MSocketNotify
       
    35 	{
       
    36 	friend class TRoleSwitcherState;
       
    37 	friend class TRSStateDisablingLPM;
       
    38 	friend class TRSStateDisablingEncryption;
       
    39 	friend class TRSStateChangingRole;
       
    40 	friend class TRSStateChangingRoleWithEPR;
       
    41 	friend class TRSStateEnablingEncryption;
       
    42 
       
    43 public:
       
    44 
       
    45 	static CRoleSwitcher* NewL(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole);
       
    46 	~CRoleSwitcher();
       
    47 
       
    48 	// From MSocketNotify
       
    49 	void NewData(TUint aCount);
       
    50 	void CanSend();
       
    51 	void ConnectComplete();
       
    52 	void ConnectComplete(const TDesC8& aConnectData);
       
    53 	void ConnectComplete(CServProviderBase& aSSP);
       
    54 	void ConnectComplete(CServProviderBase& aSSP,const TDesC8& aConnectData);
       
    55 	void CanClose(TDelete aDelete=EDelete);
       
    56 	void CanClose(const TDesC8& aDisconnectData,TDelete aDelete=EDelete);
       
    57 	void Error(TInt aError,TUint aOperationMask=EErrorAllOperations);
       
    58 	void Disconnect();
       
    59 	void Disconnect(TDesC8& aDisconnectData);
       
    60 	void IoctlComplete(TDesC8 *aBuf);
       
    61 	void NoBearer(const TDesC8& /*aConnectionInf*/) {};
       
    62 	void Bearer(const TDesC8& /*aConnectionInf*/) {};
       
    63 
       
    64 	void Start();
       
    65 	void Finish();
       
    66 	inline const TBTDevAddr& BDAddr() const;
       
    67 	inline TBTBasebandRole RequestedRole() const;
       
    68 	inline TBool IsEncryptionDisabledForRoleSwitch() const;
       
    69 	TBool IsEPRSupported() const;
       
    70 	void LogRoleSwitchSuccessful() const;
       
    71 	TSglQueLink   iQLink;
       
    72 
       
    73 private:
       
    74 	CRoleSwitcher(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole);
       
    75 	void ConstructL(); 
       
    76 
       
    77 	void DisableLPM();
       
    78 	void DisableEncryption();
       
    79 	void ChangeRole();
       
    80 	void EnableEncryption();
       
    81 	void EnableLPM();
       
    82 	void CancelIoctl();
       
    83 	
       
    84 	void SaveEncryption();
       
    85 	// CTimer
       
    86 	void RunL();
       
    87 	TInt RunError(TInt aError);
       
    88 	
       
    89 	static TInt EventReceivedCallBack(TAny* aRoleSwitcher);
       
    90 	// Async Callback to notify baseband event received.
       
    91 	CAsyncCallBack*               iEventReceivedCallBack;
       
    92 	TBTBasebandEventNotification    iBasebandEvent;
       
    93 	TBool                      iIsEncrypted;
       
    94 	TBool                      iIsActive;  // LinkMode
       
    95 	TBool                     iAddedToLinkMgr;
       
    96 	CPhysicalLinksManager&          iLinkMgr;
       
    97 	CPhysicalLink&                iLink;
       
    98 	TBTBasebandRole               iRole;  // Requested role   
       
    99 	CBTProxySAP*                iBTProxySAP;   
       
   100 	TRoleSwitcherState*            iState;
       
   101 	TBool                     iIsEncryptionDisabledForRoleSwitch;             
       
   102 	};
       
   103 
       
   104 //--------------------------------------------------
       
   105 // STATE FACTORY
       
   106 //--------------------------------------------------
       
   107 
       
   108 /**
       
   109    Factory for the RoleSwitcher states
       
   110 
       
   111    The states are flyweight classes
       
   112 **/
       
   113 NONSHARABLE_CLASS(CRoleSwitcherStateFactory) : public CBase
       
   114 	{
       
   115 public:
       
   116 	static CRoleSwitcherStateFactory* NewL();
       
   117 
       
   118 	enum TRoleSwitcherStates
       
   119 		{
       
   120 		EIdle,
       
   121 		EDisablingLPM,
       
   122 		EDisablingEncryption,
       
   123 		EChangingRole,
       
   124 		EChangingRoleWithEPR,
       
   125 		EEnablingEncryption,
       
   126 	// *** keep next one last ***
       
   127 		ERoleSwitcherMaxState,
       
   128 		};
       
   129 	
       
   130 	TRoleSwitcherState& GetState(TRoleSwitcherStates aState);
       
   131 	TInt StateIndex(const TRoleSwitcherState* aState) const;
       
   132 	
       
   133 private:
       
   134 	CRoleSwitcherStateFactory();
       
   135 	void ConstructL();
       
   136 	TFixedArray<TRoleSwitcherState*, ERoleSwitcherMaxState> iStates;
       
   137 	};
       
   138 
       
   139 
       
   140 
       
   141 //--------------------------------------------------
       
   142 // STATES, base 
       
   143 //--------------------------------------------------
       
   144 
       
   145 NONSHARABLE_CLASS(TRoleSwitcherState)
       
   146 	{
       
   147 public:
       
   148 	TRoleSwitcherState(CRoleSwitcherStateFactory& aFactory);
       
   149 	
       
   150 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   151 	virtual void Exit(CRoleSwitcher& aContext) const;
       
   152 
       
   153 	virtual void Start(CRoleSwitcher& aContext) const;
       
   154 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   155 	virtual void Error(CRoleSwitcher& aContext, TInt aErr) const;
       
   156 	virtual void TimerExpired(CRoleSwitcher& aContext) const;
       
   157 
       
   158 protected:
       
   159 	// Exits old state, sets the new state, and enters it.
       
   160 	void ChangeState(CRoleSwitcher& aContext, CRoleSwitcherStateFactory::TRoleSwitcherStates aState) const;
       
   161 	void PanicInState(TLinkPanic aPanic) const;
       
   162 	
       
   163 protected:
       
   164 	CRoleSwitcherStateFactory& iFactory;
       
   165 #ifdef __FLOG_ACTIVE
       
   166 	TBuf<48> iName;
       
   167 #endif
       
   168 	};
       
   169 
       
   170 
       
   171 //--------------------------------------------------
       
   172 // STATES 
       
   173 //--------------------------------------------------
       
   174 
       
   175 NONSHARABLE_CLASS(TRSStateIdle) : public TRoleSwitcherState
       
   176 	{
       
   177 public:
       
   178 	TRSStateIdle(CRoleSwitcherStateFactory& aFactory);
       
   179 
       
   180 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   181 	virtual void Start(CRoleSwitcher& aContext) const;
       
   182 	};
       
   183 
       
   184 
       
   185 NONSHARABLE_CLASS(TRSStateDisablingLPM) : public TRoleSwitcherState
       
   186 	{
       
   187 public:
       
   188 	TRSStateDisablingLPM(CRoleSwitcherStateFactory& aFactory);
       
   189 
       
   190 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   191 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   192 	};
       
   193 
       
   194 NONSHARABLE_CLASS(TRSStateDisablingEncryption) : public TRoleSwitcherState
       
   195 	{
       
   196 public:
       
   197 	TRSStateDisablingEncryption(CRoleSwitcherStateFactory& aFactory);
       
   198 
       
   199 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   200 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   201 	virtual void TimerExpired(CRoleSwitcher& aContext) const;
       
   202 	};
       
   203 
       
   204 NONSHARABLE_CLASS(TRSStateChangingRole) : public TRoleSwitcherState
       
   205 	{
       
   206 public:
       
   207 	TRSStateChangingRole(CRoleSwitcherStateFactory& aFactory);
       
   208 
       
   209 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   210 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   211 	virtual void TimerExpired(CRoleSwitcher& aContext) const;
       
   212 	};
       
   213 
       
   214 NONSHARABLE_CLASS(TRSStateChangingRoleWithEPR) : public TRoleSwitcherState
       
   215 	{
       
   216 public:
       
   217 	TRSStateChangingRoleWithEPR(CRoleSwitcherStateFactory& aFactory);
       
   218 
       
   219 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   220 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   221 	virtual void TimerExpired(CRoleSwitcher& aContext) const;
       
   222 	};
       
   223 
       
   224 NONSHARABLE_CLASS(TRSStateEnablingEncryption) : public TRoleSwitcherState
       
   225 	{
       
   226 public:
       
   227 	TRSStateEnablingEncryption(CRoleSwitcherStateFactory& aFactory);
       
   228 
       
   229 	virtual void Enter(CRoleSwitcher& aContext) const;
       
   230 	virtual void Exit(CRoleSwitcher& aContext) const;   
       
   231 	virtual void EventReceived(CRoleSwitcher& aContext) const;
       
   232 	virtual void TimerExpired(CRoleSwitcher& aContext) const;
       
   233 	};
       
   234 
       
   235 #ifdef __FLOG_ACTIVE
       
   236 #define STATENAME(x)  iName=_L(x)
       
   237 #else
       
   238 #define STATENAME(x)
       
   239 #endif
       
   240 
       
   241 inline const TBTDevAddr& CRoleSwitcher::BDAddr() const
       
   242 	{
       
   243 	return iLink.BDAddr();   
       
   244 	}
       
   245 
       
   246 inline TBTBasebandRole CRoleSwitcher::RequestedRole() const
       
   247 	{
       
   248 	return iRole;
       
   249 	}
       
   250 
       
   251 inline TBool CRoleSwitcher::IsEncryptionDisabledForRoleSwitch() const
       
   252 	{
       
   253 	return iIsEncryptionDisabledForRoleSwitch;
       
   254 	}
       
   255 
       
   256 #endif //PHYSICALLINKHELPER_H_