bluetooth/btstack/linkmgr/PhysicalLinkHelper.h
changeset 48 22de2e391156
parent 0 29b1cd4cb562
child 56 015fa7494bd2
equal deleted inserted replaced
35:14e279d2bbb7 48:22de2e391156
    20 #include <bt_sock.h>
    20 #include <bt_sock.h>
    21 #include "physicallinks.h"
    21 #include "physicallinks.h"
    22 #include "linkutil.h"
    22 #include "linkutil.h"
    23 #include <bluetooth/hci/hciutil.h> 
    23 #include <bluetooth/hci/hciutil.h> 
    24 
    24 
    25 // watchdog for first half of the SM including:
    25 // A struct to allow both single event and combination events to be passed for requesting
    26 // DisablingLPM, DisablingEncryption, RoleSwitch
    26 // notification.  This effectively moves the cast to a TInt to below the API rather than
    27 const TUint KTimeoutRoleSwitch =   3000000;  // 3 s; 
    27 // being required by the client.
       
    28 struct TNotifyEvent
       
    29 	{
       
    30 public:
       
    31 	TNotifyEvent(TBTPhysicalLinkStateNotifier aSingleEvent) : iEvent(aSingleEvent) {};
       
    32 	TNotifyEvent(TBTPhysicalLinkStateNotifierCombinations aComboEvent) : iEvent(aComboEvent) {};
    28 
    33 
    29 // watchdog for EnablingEncryption
    34 	TInt NotifyEvent() const {return iEvent;};
    30 const TUint KTimeoutOneCommand =   2000000;  // 2 s;
    35 private:
    31  
    36 	TInt iEvent;
    32 class TRoleSwitcherState;
    37 	};
    33 
    38 
    34 NONSHARABLE_CLASS(CRoleSwitcher) : public CTimer, public MSocketNotify
    39 
       
    40 NONSHARABLE_CLASS(CPhysicalLinkHelper) : public CBase, public MSocketNotify
    35 	{
    41 	{
    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:
    42 public:
    44 
    43 	~CPhysicalLinkHelper();
    45 	static CRoleSwitcher* NewL(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole);
       
    46 	~CRoleSwitcher();
       
    47 
    44 
    48 	// From MSocketNotify
    45 	// From MSocketNotify
    49 	void NewData(TUint aCount);
    46 	void NewData(TUint aCount);
    50 	void CanSend();
    47 	void CanSend();
    51 	void ConnectComplete();
    48 	void ConnectComplete();
    59 	void Disconnect(TDesC8& aDisconnectData);
    56 	void Disconnect(TDesC8& aDisconnectData);
    60 	void IoctlComplete(TDesC8 *aBuf);
    57 	void IoctlComplete(TDesC8 *aBuf);
    61 	void NoBearer(const TDesC8& /*aConnectionInf*/) {};
    58 	void NoBearer(const TDesC8& /*aConnectionInf*/) {};
    62 	void Bearer(const TDesC8& /*aConnectionInf*/) {};
    59 	void Bearer(const TDesC8& /*aConnectionInf*/) {};
    63 
    60 
    64 	void Start();
    61 	virtual void StartHelper() = 0;
    65 	void Finish();
    62 
    66 	inline const TBTDevAddr& BDAddr() const;
    63 	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 
    64 
    73 private:
    65 protected:
    74 	CRoleSwitcher(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole);
    66 	CPhysicalLinkHelper(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink);
    75 	void ConstructL(); 
    67 	void BaseConstructL();
    76 
    68 
    77 	void DisableLPM();
    69 	void DisableLPM();
    78 	void DisableEncryption();
       
    79 	void ChangeRole();
       
    80 	void EnableEncryption();
       
    81 	void EnableLPM();
       
    82 	void CancelIoctl();
       
    83 	
    70 	
    84 	void SaveEncryption();
    71 	void QueueTimer(TTimeIntervalMicroSeconds32 aTimerVal);
    85 	// CTimer
    72 	void RemoveTimer();
    86 	void RunL();
    73 	void NotifyBasebandEvent(TNotifyEvent aEvent);
    87 	TInt RunError(TInt aError);
    74 	void CancelNotify();
    88 	
    75 	
    89 	static TInt EventReceivedCallBack(TAny* aRoleSwitcher);
    76 	TBool IsEPRSupported() const;
       
    77 	
       
    78 	virtual void TimerExpired() = 0;
       
    79 	virtual void HandleError(TInt aError) = 0;
       
    80 	virtual void EventReceived(TBTBasebandEventNotification& aEvent) = 0;
       
    81 	
       
    82 private:
       
    83 	static TInt EventReceivedCallBack(TAny* aThis);
       
    84 	void DoEventReceivedCallBack();
       
    85 	
       
    86 	static TInt TimerExpiredCallBack(TAny* aThis);
       
    87 	void DoTimerExpiredCallBack();
       
    88 	
       
    89 protected:
       
    90 	CPhysicalLinksManager&			iLinkMgr;
       
    91 	CPhysicalLink&					iLink;
       
    92 	
       
    93 private:
    90 	// Async Callback to notify baseband event received.
    94 	// Async Callback to notify baseband event received.
    91 	CAsyncCallBack*               iEventReceivedCallBack;
    95 	CAsyncCallBack*					iEventReceivedCallBack;
    92 	TBTBasebandEventNotification    iBasebandEvent;
    96 	TBTBasebandEventNotification	iBasebandEvent;
    93 	TBool                      iIsEncrypted;
    97 	CBTProxySAP*					iBTProxySAP;  
    94 	TBool                      iIsActive;  // LinkMode
    98 	
    95 	TBool                     iAddedToLinkMgr;
    99 	TDeltaTimerEntry				iTimerEntry;
    96 	CPhysicalLinksManager&          iLinkMgr;
       
    97 	CPhysicalLink&                iLink;
       
    98 	TBTBasebandRole               iRole;  // Requested role   
       
    99 	CBTProxySAP*                iBTProxySAP;   
       
   100 	TRoleSwitcherState*            iState;
       
   101 	TBool                     iIsEncryptionDisabledForRoleSwitch;             
       
   102 	};
   100 	};
   103 
   101 
   104 //--------------------------------------------------
   102 inline const TBTDevAddr& CPhysicalLinkHelper::BDAddr() const
   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 	{
   103 	{
   243 	return iLink.BDAddr();   
   104 	return iLink.BDAddr();   
   244 	}
   105 	}
   245 
   106 
   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_
   107 #endif //PHYSICALLINKHELPER_H_