bluetooth/btstack/linkmgr/PhysicalLinkHelper.cpp
branchRCL_3
changeset 25 99439b07e980
parent 0 29b1cd4cb562
equal deleted inserted replaced
24:e9b924a62a66 25:99439b07e980
    20 #include "physicallinksmanager.h"
    20 #include "physicallinksmanager.h"
    21 #include "AclDataQController.h"
    21 #include "AclDataQController.h"
    22 #include "ProxySAP.h"
    22 #include "ProxySAP.h"
    23 #include "linkmgr.h"
    23 #include "linkmgr.h"
    24 
    24 
       
    25 
    25 #ifdef __FLOG_ACTIVE
    26 #ifdef __FLOG_ACTIVE
    26 _LIT8(KLogComponent, LOG_COMPONENT_LINKMGR);
    27 _LIT8(KLogComponent, LOG_COMPONENT_LINKMGR);
    27 #endif
    28 #endif
    28 
    29 
    29 CRoleSwitcher::CRoleSwitcher(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole)
    30 CPhysicalLinkHelper::CPhysicalLinkHelper(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink)
    30 	: CTimer(CActive::EPriorityStandard)
    31 	: iLinkMgr(aLinkMgr)
    31 	, iLinkMgr(aLinkMgr)
       
    32 	, iLink(aLink)
    32 	, iLink(aLink)
    33 	, iRole(aRole)
    33 	{
    34 	, iIsEncryptionDisabledForRoleSwitch(EFalse)
    34 	LOG_FUNC
    35 	{
    35 	}
    36 	LOG_FUNC
    36 	
    37 	iState = &iLinkMgr.RoleSwitcherStateFactory().GetState(CRoleSwitcherStateFactory::EIdle);
    37 void CPhysicalLinkHelper::BaseConstructL()
    38 	}
       
    39 
       
    40 CRoleSwitcher* CRoleSwitcher::NewL(CPhysicalLinksManager& aLinkMgr, CPhysicalLink& aLink, TBTBasebandRole aRole)
       
    41 	{
       
    42 	LOG_STATIC_FUNC
       
    43 	CRoleSwitcher* self = new(ELeave) CRoleSwitcher(aLinkMgr, aLink, aRole);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 	
       
    50 void CRoleSwitcher::ConstructL()
       
    51 	{
    38 	{
    52 	LOG_FUNC
    39 	LOG_FUNC
    53 	
    40 	
    54 	// create Proxy telling it the possible PHY
    41 	// create Proxy telling it the possible PHY
    55 	iBTProxySAP = CBTProxySAP::NewL(iLinkMgr, &iLink);
    42 	iBTProxySAP = CBTProxySAP::NewL(iLinkMgr, &iLink);
    56 	iBTProxySAP->SetNotify(this);
    43 	iBTProxySAP->SetNotify(this);
    57 		
    44 	
    58 	TCallBack cb(EventReceivedCallBack, this);
    45 	TCallBack ecb(EventReceivedCallBack, this);
    59 	iEventReceivedCallBack = new (ELeave)CAsyncCallBack(cb, EActiveHighPriority);
    46 	iEventReceivedCallBack = new (ELeave)CAsyncCallBack(ecb, EActiveHighPriority);
    60 
    47 	}
    61 	CTimer::ConstructL();
    48 	
    62 	CActiveScheduler::Add(this);
    49 CPhysicalLinkHelper::~CPhysicalLinkHelper()
    63 
    50 	{
    64 	// add ourselves to the list in LinkMgr, LinkMgr will kick off the role change state machine 	
    51 	LOG_FUNC
    65 	iLinkMgr.AddRoleSwitcher(*this);
    52 	
    66 	iAddedToLinkMgr = ETrue;
    53 	RemoveTimer();
    67 	}
       
    68 	
       
    69 CRoleSwitcher::~CRoleSwitcher()
       
    70 	{
       
    71 	LOG_FUNC
       
    72 	if (iAddedToLinkMgr)
       
    73 		{
       
    74 		iLinkMgr.RemoveRoleSwitcher(*this);
       
    75 		}
       
    76 		
       
    77 	Cancel(); // watchdog timer
       
    78 	delete iBTProxySAP;
    54 	delete iBTProxySAP;
       
    55 	
    79 	if (iEventReceivedCallBack)
    56 	if (iEventReceivedCallBack)
    80 		{
    57 		{
    81 		iEventReceivedCallBack->Cancel();
    58 		iEventReceivedCallBack->Cancel();
    82 		delete iEventReceivedCallBack;
    59 		delete iEventReceivedCallBack;
    83 		}
    60 		}
    84 	}
    61 	}
    85 
    62 
    86 void CRoleSwitcher::DisableLPM()
    63 void CPhysicalLinkHelper::DisableLPM()
    87    	{
    64    	{
    88 	LOG_FUNC
    65 	LOG_FUNC
    89 	TPckgBuf<TInt> optionBuf;	
    66 	TPckgBuf<TInt> optionBuf;	
    90    	iBTProxySAP->SAPSetOption(KSolBtLMProxy, EBBRequestPreventAllLowPowerModes, optionBuf);
    67    	iBTProxySAP->SAPSetOption(KSolBtLMProxy, EBBRequestPreventAllLowPowerModes, optionBuf);
    91    	}
    68    	}
    92 
    69 
    93 void CRoleSwitcher::EnableLPM()
    70 TBool CPhysicalLinkHelper::IsEPRSupported() const
    94    	{
       
    95 	LOG_FUNC
       
    96 	TPckgBuf<TInt> optionBuf;	
       
    97    	iBTProxySAP->SAPSetOption(KSolBtLMProxy, EBBRequestAllowAllLowPowerModes, optionBuf);
       
    98    	}
       
    99 
       
   100 void CRoleSwitcher::DisableEncryption()
       
   101   	{
       
   102 	LOG_FUNC
       
   103  	// data traffic suspended
       
   104 	iLinkMgr.LinkManagerProtocol().ACLController().SetParked(iLink.Handle(), ETrue);
       
   105    	TBTBasebandEvent event(ENotifyEncryptionChangeOff);
       
   106    	iBTProxySAP->Ioctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl, &event);
       
   107 	iLinkMgr.Encrypt(EFalse, iLink);
       
   108 
       
   109 	// set flag here, it's too late when we receive the event as AccessReqester
       
   110 	// might receive the baseband notification earlier then the flag is set!	
       
   111 	iIsEncryptionDisabledForRoleSwitch = ETrue;
       
   112   	}
       
   113 
       
   114 void CRoleSwitcher::EnableEncryption()
       
   115   	{
       
   116 	LOG_FUNC  	
       
   117    	TBTBasebandEvent event(ENotifyEncryptionChangeOn);
       
   118    	iBTProxySAP->Ioctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl, &event);
       
   119 	iLinkMgr.Encrypt(ETrue, iLink);	
       
   120 	// data traffic is enabled in IoctlComplete
       
   121   	}
       
   122    	
       
   123 void CRoleSwitcher::ChangeRole()
       
   124 	{
       
   125 	LOG_FUNC
       
   126    	TBTBasebandEvent event(ENotifyAnyRole);
       
   127    	iBTProxySAP->Ioctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl, &event);
       
   128 	iLinkMgr.ChangeRole(iRole, iLink);
       
   129 	}
       
   130 
       
   131 void CRoleSwitcher::CancelIoctl()
       
   132 	{
       
   133 	LOG_FUNC
       
   134 	iBTProxySAP->CancelIoctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl);
       
   135 	}
       
   136 	
       
   137 // Timer
       
   138 void CRoleSwitcher::RunL()
       
   139 	{
       
   140 	LOG_FUNC
       
   141 	iState->TimerExpired(*this);
       
   142 	}
       
   143 	
       
   144 TInt CRoleSwitcher::RunError(TInt aError)
       
   145 	{
       
   146 	LOG_FUNC
       
   147 	iState->Error(*this, aError);
       
   148 	return KErrNone;
       
   149 	}
       
   150    	   	
       
   151 // From MSocketNotify
       
   152 void CRoleSwitcher::NewData(TUint /*aCount*/)
       
   153 	{
       
   154 	LOG_FUNC
       
   155 	
       
   156 	}
       
   157 	
       
   158 void CRoleSwitcher::CanSend()
       
   159 	{
       
   160 	LOG_FUNC
       
   161 	
       
   162 	}
       
   163 	
       
   164 void CRoleSwitcher::ConnectComplete()
       
   165 	{
       
   166 	LOG_FUNC
       
   167 	
       
   168 	}
       
   169 	
       
   170 void CRoleSwitcher::ConnectComplete(const TDesC8& /*aConnectData*/)
       
   171 	{
       
   172 	LOG_FUNC
       
   173 	
       
   174 	}
       
   175 	
       
   176 void CRoleSwitcher::ConnectComplete(CServProviderBase& /*aSSP*/)
       
   177 	{
       
   178 	LOG_FUNC
       
   179 	
       
   180 	}
       
   181 	
       
   182 void CRoleSwitcher::ConnectComplete(CServProviderBase& /*aSSP*/,const TDesC8& /*aConnectData*/)
       
   183 	{
       
   184 	LOG_FUNC
       
   185 	
       
   186 	}
       
   187 	
       
   188 void CRoleSwitcher::CanClose(TDelete /*aDelete*/)
       
   189 	{
       
   190 	LOG_FUNC
       
   191 	
       
   192 	}
       
   193 
       
   194 void CRoleSwitcher::CanClose(const TDesC8& /*aDisconnectData*/,TDelete /*aDelete*/)
       
   195 	{
       
   196 	LOG_FUNC
       
   197 	
       
   198 	}
       
   199 	
       
   200 void CRoleSwitcher::Error(TInt /*aError*/,TUint /*aOperationMask*/)
       
   201 	{
       
   202 	LOG_FUNC
       
   203 	
       
   204 	}
       
   205 	
       
   206 void CRoleSwitcher::Disconnect(void)
       
   207 	{
       
   208 	LOG_FUNC
       
   209 	iState->Error(*this, KErrDisconnected);
       
   210 	}
       
   211 
       
   212 void CRoleSwitcher::Disconnect(TDesC8& /*aDisconnectData*/)
       
   213 	{
       
   214 	LOG_FUNC
       
   215 	iState->Error(*this, KErrDisconnected);
       
   216 	}
       
   217 
       
   218 void CRoleSwitcher::Start()
       
   219 	{
       
   220 	LOG_FUNC
       
   221 	iState->Start(*this);
       
   222 	}
       
   223 
       
   224 void CRoleSwitcher::Finish()
       
   225 	{
       
   226 	LOG_FUNC
       
   227 	// async call to delete this class
       
   228 	iLink.AsyncDeleteRoleSwitcher();
       
   229 	}
       
   230 		
       
   231 void CRoleSwitcher::SaveEncryption()
       
   232 	{
       
   233 	LOG_FUNC
       
   234 	iIsEncrypted = iLink.Encrypted();
       
   235 	}
       
   236 	
       
   237 TBool CRoleSwitcher::IsEPRSupported() const
       
   238 	{
    71 	{
   239 	LOG_FUNC
    72 	LOG_FUNC
   240 	// For Lisbon (Bluetooth 2.1), if EPR is supported both locally and remotely,
    73 	// For Lisbon (Bluetooth 2.1), if EPR is supported both locally and remotely,
   241 	// then the controllers will disable/enable encryption automatically for us,
    74 	// then the controllers will disable/enable encryption automatically for us,
   242 	// so skip some states.
    75 	// so skip some states.
   243 	return iLink.IsEncryptionPauseResumeSupported();
    76 	return iLink.IsEncryptionPauseResumeSupported();
   244 	}
    77 	}
   245 
    78 
   246 void CRoleSwitcher::LogRoleSwitchSuccessful() const
    79 void CPhysicalLinkHelper::NotifyBasebandEvent(TNotifyEvent aEvent)
   247 	{
    80 	{
   248 	LOG_FUNC
    81 	LOG_FUNC
   249 	TInt eventType;
    82 	TBTBasebandEvent event(aEvent.NotifyEvent());
   250 	eventType = (iRole == EMaster ? ENotifyMaster :ENotifySlave);
    83 	iBTProxySAP->Ioctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl, &event);
   251 	
    84 	}
   252 	if (iBasebandEvent.EventType()==eventType &&
    85 
   253 	    iBasebandEvent.ErrorCode()==KErrNone)
    86 void CPhysicalLinkHelper::CancelNotify()
   254 		{
    87 	{
   255 		LOG(_L("CRoleSwitcher RoleSwitch OK"));	
    88 	LOG_FUNC
   256 		}
    89 	iBTProxySAP->CancelIoctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl);
   257 	else 
    90 	}
   258 		{
    91 	
   259 		LOG(_L("CRoleSwitcher RoleSwitch failed"));
    92 void CPhysicalLinkHelper::QueueTimer(TTimeIntervalMicroSeconds32 aTimerVal)
   260 		}
    93 	{
   261 	}
    94 	LOG_FUNC
   262 
    95 
   263 void CRoleSwitcher::IoctlComplete(TDesC8 *aBuf)
    96 	TCallBack cb(TimerExpiredCallBack, this);
       
    97 	iTimerEntry.Set(cb);
       
    98 	BTSocketTimer::Queue(aTimerVal, iTimerEntry);
       
    99 	}
       
   100 
       
   101 
       
   102 void CPhysicalLinkHelper::RemoveTimer()
       
   103 	{
       
   104 	LOG_FUNC
       
   105 
       
   106 	BTSocketTimer::Remove(iTimerEntry);
       
   107 	}
       
   108    	   	
       
   109 // From MSocketNotify
       
   110 void CPhysicalLinkHelper::NewData(TUint /*aCount*/)
       
   111 	{
       
   112 	LOG_FUNC
       
   113 	
       
   114 	}
       
   115 	
       
   116 void CPhysicalLinkHelper::CanSend()
       
   117 	{
       
   118 	LOG_FUNC
       
   119 	
       
   120 	}
       
   121 	
       
   122 void CPhysicalLinkHelper::ConnectComplete()
       
   123 	{
       
   124 	LOG_FUNC
       
   125 	
       
   126 	}
       
   127 	
       
   128 void CPhysicalLinkHelper::ConnectComplete(const TDesC8& /*aConnectData*/)
       
   129 	{
       
   130 	LOG_FUNC
       
   131 	
       
   132 	}
       
   133 	
       
   134 void CPhysicalLinkHelper::ConnectComplete(CServProviderBase& /*aSSP*/)
       
   135 	{
       
   136 	LOG_FUNC
       
   137 	
       
   138 	}
       
   139 	
       
   140 void CPhysicalLinkHelper::ConnectComplete(CServProviderBase& /*aSSP*/,const TDesC8& /*aConnectData*/)
       
   141 	{
       
   142 	LOG_FUNC
       
   143 	
       
   144 	}
       
   145 	
       
   146 void CPhysicalLinkHelper::CanClose(TDelete /*aDelete*/)
       
   147 	{
       
   148 	LOG_FUNC
       
   149 	
       
   150 	}
       
   151 
       
   152 void CPhysicalLinkHelper::CanClose(const TDesC8& /*aDisconnectData*/,TDelete /*aDelete*/)
       
   153 	{
       
   154 	LOG_FUNC
       
   155 	
       
   156 	}
       
   157 	
       
   158 void CPhysicalLinkHelper::Error(TInt /*aError*/,TUint /*aOperationMask*/)
       
   159 	{
       
   160 	LOG_FUNC
       
   161 	
       
   162 	}
       
   163 	
       
   164 void CPhysicalLinkHelper::Disconnect(void)
       
   165 	{
       
   166 	LOG_FUNC
       
   167 	Error(KErrDisconnected);
       
   168 	}
       
   169 
       
   170 void CPhysicalLinkHelper::Disconnect(TDesC8& /*aDisconnectData*/)
       
   171 	{
       
   172 	LOG_FUNC
       
   173 	Error(KErrDisconnected);
       
   174 	}
       
   175 
       
   176 void CPhysicalLinkHelper::IoctlComplete(TDesC8 *aBuf)
   264 	{
   177 	{
   265 	LOG_FUNC
   178 	LOG_FUNC
   266 	const TBTBasebandEventNotification* event = reinterpret_cast<const TBTBasebandEventNotification*>(aBuf->Ptr());
   179 	const TBTBasebandEventNotification* event = reinterpret_cast<const TBTBasebandEventNotification*>(aBuf->Ptr());
   267 	iBasebandEvent = *event;
   180 	iBasebandEvent = *event;
   268 	iEventReceivedCallBack->CallBack();
   181 	iEventReceivedCallBack->CallBack();
   269 	}
   182 	}
   270 	
   183 
   271 /*static*/ TInt CRoleSwitcher::EventReceivedCallBack(TAny* aRoleSwitcher)
   184 /*static*/ TInt CPhysicalLinkHelper::EventReceivedCallBack(TAny* aThis)
   272 	{
   185 	{
   273 	LOG_STATIC_FUNC
   186 	LOG_STATIC_FUNC
   274 	CRoleSwitcher* roleSwitcher = static_cast<CRoleSwitcher*>(aRoleSwitcher);
   187 	CPhysicalLinkHelper* helper = static_cast<CPhysicalLinkHelper*>(aThis);
   275 	roleSwitcher->iState->EventReceived(*roleSwitcher);
   188 	helper->DoEventReceivedCallBack();
   276 	return EFalse;
   189 	return EFalse;
   277 	}
   190 	}
   278 		
   191 
   279 
   192 void CPhysicalLinkHelper::DoEventReceivedCallBack()
   280 //----------------------------------------------------------------------------------
   193 	{
   281 // STATE FACTORY
   194 	LOG_FUNC
   282 //----------------------------------------------------------------------------------
   195 	EventReceived(iBasebandEvent);
   283 
   196 	}
   284 CRoleSwitcherStateFactory* CRoleSwitcherStateFactory::NewL()
   197 
       
   198 /*static*/ TInt CPhysicalLinkHelper::TimerExpiredCallBack(TAny* aThis)
   285 	{
   199 	{
   286 	LOG_STATIC_FUNC
   200 	LOG_STATIC_FUNC
   287 	CRoleSwitcherStateFactory* ret=new (ELeave) CRoleSwitcherStateFactory();
   201 	CPhysicalLinkHelper* helper = static_cast<CPhysicalLinkHelper*>(aThis);
   288 	CleanupStack::PushL(ret);
   202 	helper->DoTimerExpiredCallBack();
   289 	ret->ConstructL();
   203 	return EFalse;
   290 	CleanupStack::Pop(ret);
   204 	}
   291 	return ret;
   205 
   292 	}
   206 void CPhysicalLinkHelper::DoTimerExpiredCallBack()
   293 
   207 	{
   294 void CRoleSwitcherStateFactory::ConstructL()
   208 	LOG_FUNC
   295 	{
   209 	TimerExpired();
   296 	LOG_FUNC	
   210 	}
   297 	iStates[EIdle]					=new (ELeave) TRSStateIdle(*this);
   211 
   298 	iStates[EDisablingLPM]			=new (ELeave) TRSStateDisablingLPM(*this);
   212 	
   299 	iStates[EDisablingEncryption]	=new (ELeave) TRSStateDisablingEncryption(*this);
   213 
   300 	iStates[EChangingRole]			=new (ELeave) TRSStateChangingRole(*this);
   214 
   301 	iStates[EChangingRoleWithEPR]	=new (ELeave) TRSStateChangingRoleWithEPR(*this);
   215 
   302 	iStates[EEnablingEncryption]	=new (ELeave) TRSStateEnablingEncryption(*this);
   216 
   303 	}
   217 
   304 
   218 
   305 CRoleSwitcherStateFactory::CRoleSwitcherStateFactory()
   219 
   306 	{
       
   307 	LOG_FUNC
       
   308 	iStates.DeleteAll();
       
   309 	}
       
   310 
       
   311 TRoleSwitcherState& CRoleSwitcherStateFactory::GetState(CRoleSwitcherStateFactory::TRoleSwitcherStates aState)
       
   312 	{
       
   313 	LOG_FUNC
       
   314 	__ASSERT_DEBUG(iStates[aState],  Panic(ERoleSwitcherInvalidState));
       
   315 	return *iStates[aState];
       
   316 	}
       
   317 
       
   318 TInt CRoleSwitcherStateFactory::StateIndex(const TRoleSwitcherState* aState) const
       
   319 	{
       
   320 	LOG_FUNC
       
   321 	TInt state;
       
   322 	for (state = 0; state < ERoleSwitcherMaxState; state++)
       
   323 		{
       
   324 		if (iStates[state] == aState)
       
   325 			{
       
   326 			return state;
       
   327 			}
       
   328 		}
       
   329 	
       
   330 	return KUnknownState;
       
   331 	}
       
   332 
       
   333 
       
   334 //----------------------------------------------------------------------------------
       
   335 // STATES
       
   336 //----------------------------------------------------------------------------------
       
   337 
       
   338 TRoleSwitcherState::TRoleSwitcherState(CRoleSwitcherStateFactory& aFactory)
       
   339 : iFactory(aFactory)
       
   340 	{
       
   341 	LOG_FUNC
       
   342 	}
       
   343 
       
   344 void TRoleSwitcherState::PanicInState(TLinkPanic aPanic) const
       
   345 	{
       
   346 	LOG_FUNC
       
   347 	Panic(aPanic, iFactory.StateIndex(this));
       
   348 	}
       
   349 
       
   350 void TRoleSwitcherState::ChangeState(CRoleSwitcher& aContext, CRoleSwitcherStateFactory::TRoleSwitcherStates aState) const
       
   351 	{
       
   352 	LOG_FUNC
       
   353 	
       
   354 	aContext.iState->Exit(aContext);
       
   355 
       
   356 #ifdef __FLOG_ACTIVE
       
   357 	TRoleSwitcherState* state=&iFactory.GetState(aState);
       
   358 	LOG2(_L("RoleSwitcher: State %S -> %S"), &aContext.iState->iName, &state->iName);
       
   359 #endif //__FLOG_ACTIVE
       
   360 	aContext.iState=&iFactory.GetState(aState);
       
   361 
       
   362 	aContext.iState->Enter(aContext);
       
   363 	}
       
   364 
       
   365 void TRoleSwitcherState::Enter(CRoleSwitcher& /*aContext*/) const
       
   366 	{
       
   367 	LOG_FUNC
       
   368 	// do nothing
       
   369 	}
       
   370 
       
   371 void TRoleSwitcherState::Exit(CRoleSwitcher& /*aContext*/) const
       
   372 	{
       
   373 	LOG_FUNC
       
   374 	// do nothing
       
   375 	}
       
   376 
       
   377 void TRoleSwitcherState::Start(CRoleSwitcher& /*aContext*/) const
       
   378 	{
       
   379 	LOG_FUNC
       
   380 	PanicInState(ERoleSwitcherStateMachineInvalidEvent);
       
   381 	}
       
   382 
       
   383 void TRoleSwitcherState::Error(CRoleSwitcher& aContext, TInt /*aErr*/) const
       
   384 	{
       
   385 	LOG_FUNC
       
   386 	aContext.CancelIoctl();
       
   387 	aContext.Cancel();
       
   388 	ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   389 	}
       
   390 
       
   391 void TRoleSwitcherState::EventReceived(CRoleSwitcher& /*aContext*/) const
       
   392 	{
       
   393 	LOG_FUNC
       
   394 	// do nothing
       
   395 	}
       
   396 
       
   397 void TRoleSwitcherState::TimerExpired(CRoleSwitcher& aContext) const
       
   398 	{
       
   399 	LOG_FUNC
       
   400 	ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   401 	}
       
   402 
       
   403 //----------------------------------------------------------------------------------
       
   404 
       
   405 TRSStateIdle::TRSStateIdle(CRoleSwitcherStateFactory& aFactory)
       
   406 : TRoleSwitcherState(aFactory)
       
   407 	{
       
   408 	LOG_FUNC
       
   409 	STATENAME("TRSStateIdle");
       
   410 	}
       
   411 
       
   412 void TRSStateIdle::Start(CRoleSwitcher& aContext) const
       
   413 	{
       
   414 	LOG_FUNC
       
   415 	aContext.After(KTimeoutRoleSwitch);	// watchdog timer
       
   416 	ChangeState(aContext, CRoleSwitcherStateFactory::EDisablingLPM);
       
   417 	}	
       
   418 
       
   419 void TRSStateIdle::Enter(CRoleSwitcher& aContext) const
       
   420 	{
       
   421 	LOG_FUNC
       
   422 	aContext.Finish();
       
   423 	}	
       
   424 
       
   425 //----------------------------------------------------------------------------------
       
   426 
       
   427 TRSStateDisablingLPM::TRSStateDisablingLPM(CRoleSwitcherStateFactory& aFactory)
       
   428 : TRoleSwitcherState(aFactory)
       
   429 	{
       
   430 	LOG_FUNC
       
   431 	STATENAME("TRSStateDisablingLPM");
       
   432 	}
       
   433 
       
   434 void TRSStateDisablingLPM::Enter(CRoleSwitcher& aContext) const
       
   435 	{
       
   436 	LOG_FUNC
       
   437 	// DisableLPM even if link is active to prevent possible LPM requests during encryption disabling
       
   438 
       
   439 	if (aContext.iLink.LinkMode() == EActiveMode)
       
   440 		{
       
   441 		aContext.DisableLPM();
       
   442 		if (aContext.IsEPRSupported())
       
   443 			{
       
   444 			ChangeState(aContext, CRoleSwitcherStateFactory::EChangingRoleWithEPR);
       
   445 			}
       
   446 		else
       
   447 			{
       
   448 			ChangeState(aContext, CRoleSwitcherStateFactory::EDisablingEncryption);
       
   449 			}
       
   450 		// don't wait for notification
       
   451 		}
       
   452 	else
       
   453 		{
       
   454 		TBTBasebandEvent event(ENotifyActiveMode);
       
   455 		aContext.iBTProxySAP->Ioctl(KSolBtLMProxy, KLMBasebandEventOneShotNotificationIoctl, &event);
       
   456 		aContext.DisableLPM();
       
   457 		}
       
   458 	}
       
   459 
       
   460 void TRSStateDisablingLPM::EventReceived(CRoleSwitcher& aContext) const
       
   461 	{
       
   462 	LOG_FUNC
       
   463 	if (aContext.iBasebandEvent.EventType()==ENotifyActiveMode &&
       
   464 		aContext.iBasebandEvent.ErrorCode()==KErrNone)
       
   465 		{
       
   466 		if (aContext.IsEPRSupported())
       
   467 			{
       
   468 			ChangeState(aContext, CRoleSwitcherStateFactory::EChangingRoleWithEPR);
       
   469 			}
       
   470 		else
       
   471 			{
       
   472 			ChangeState(aContext, CRoleSwitcherStateFactory::EDisablingEncryption);
       
   473 			}
       
   474 		}
       
   475 	else 
       
   476 		{
       
   477 		LOG(_L("CRoleSwitcher RoleSwitch failed in DisableLPM"));
       
   478 		// we can quit SM, don't need to rewind
       
   479 		ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   480 		}
       
   481 	}
       
   482 
       
   483 //----------------------------------------------------------------------------------
       
   484 TRSStateDisablingEncryption::TRSStateDisablingEncryption(CRoleSwitcherStateFactory& aFactory)
       
   485 : TRoleSwitcherState(aFactory)
       
   486 	{
       
   487 	LOG_FUNC
       
   488 	STATENAME("TRSStateDisablingEncryption");
       
   489 	}
       
   490 
       
   491 void TRSStateDisablingEncryption::Enter(CRoleSwitcher& aContext) const
       
   492 	{
       
   493 	LOG_FUNC
       
   494 	aContext.SaveEncryption();
       
   495 	if (aContext.iIsEncrypted)
       
   496 		{
       
   497 		aContext.DisableEncryption();
       
   498 		}
       
   499 	else
       
   500 		{
       
   501 		ChangeState(aContext, CRoleSwitcherStateFactory::EChangingRole);
       
   502 		}
       
   503 	}
       
   504 
       
   505 void TRSStateDisablingEncryption::EventReceived(CRoleSwitcher& aContext) const
       
   506 	{
       
   507 	LOG_FUNC
       
   508 	if (aContext.iBasebandEvent.EventType()==ENotifyEncryptionChangeOff &&
       
   509 	    aContext.iBasebandEvent.ErrorCode()==KErrNone)
       
   510 		{
       
   511 		ChangeState(aContext, CRoleSwitcherStateFactory::EChangingRole);
       
   512 		}
       
   513 	else 
       
   514 		{
       
   515 		LOG(_L("CRoleSwitcher RoleSwitch failed in DisableEncryption"));
       
   516 		// before quiting SM , try to enable LPM
       
   517 		aContext.EnableLPM();
       
   518 		ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   519 		}
       
   520 	}
       
   521 
       
   522 void TRSStateDisablingEncryption::TimerExpired(CRoleSwitcher& aContext) const
       
   523 	{
       
   524 	LOG_FUNC
       
   525 	aContext.CancelIoctl();
       
   526 	ChangeState(aContext, CRoleSwitcherStateFactory::EEnablingEncryption);
       
   527 	}
       
   528 
       
   529 //----------------------------------------------------------------------------------
       
   530 TRSStateChangingRole::TRSStateChangingRole(CRoleSwitcherStateFactory& aFactory)
       
   531 : TRoleSwitcherState(aFactory)
       
   532 	{
       
   533 	LOG_FUNC
       
   534 	STATENAME("TRSStateChangingRole");
       
   535 	}
       
   536 
       
   537 void TRSStateChangingRole::Enter(CRoleSwitcher& aContext) const
       
   538 	{
       
   539 	LOG_FUNC
       
   540 	aContext.ChangeRole();
       
   541 	}
       
   542 
       
   543 void TRSStateChangingRole::EventReceived(CRoleSwitcher& aContext) const
       
   544 	{
       
   545 	LOG_FUNC
       
   546 	aContext.Cancel();	// cancel watchdog timer
       
   547 
       
   548 	FTRACE(aContext.LogRoleSwitchSuccessful());
       
   549 	
       
   550 
       
   551 	ChangeState(aContext, CRoleSwitcherStateFactory::EEnablingEncryption);
       
   552 	}
       
   553 
       
   554 void TRSStateChangingRole::TimerExpired(CRoleSwitcher& aContext) const
       
   555 	{
       
   556 	LOG_FUNC
       
   557 	aContext.CancelIoctl();
       
   558 	ChangeState(aContext, CRoleSwitcherStateFactory::EEnablingEncryption);
       
   559 	}
       
   560 
       
   561 //----------------------------------------------------------------------------------
       
   562 TRSStateChangingRoleWithEPR::TRSStateChangingRoleWithEPR(CRoleSwitcherStateFactory& aFactory)
       
   563 : TRoleSwitcherState(aFactory)
       
   564 	{
       
   565 	LOG_FUNC
       
   566 	STATENAME("TRSStateChangingRoleWithEPR");
       
   567 	}
       
   568 
       
   569 void TRSStateChangingRoleWithEPR::Enter(CRoleSwitcher& aContext) const
       
   570 	{
       
   571 	LOG_FUNC
       
   572 	aContext.ChangeRole();
       
   573 	}
       
   574 
       
   575 void TRSStateChangingRoleWithEPR::EventReceived(CRoleSwitcher& aContext) const
       
   576 	{
       
   577 	LOG_FUNC
       
   578 	aContext.Cancel();	// cancel watchdog timer
       
   579 
       
   580 	FTRACE(aContext.LogRoleSwitchSuccessful());
       
   581 		
       
   582 	ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   583 	}
       
   584 
       
   585 void TRSStateChangingRoleWithEPR::TimerExpired(CRoleSwitcher& aContext) const
       
   586 	{
       
   587 	LOG_FUNC
       
   588 	aContext.CancelIoctl();
       
   589 	ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   590 	}
       
   591 
       
   592 //----------------------------------------------------------------------------------
       
   593 TRSStateEnablingEncryption::TRSStateEnablingEncryption(CRoleSwitcherStateFactory& aFactory)
       
   594 : TRoleSwitcherState(aFactory)
       
   595 	{
       
   596 	LOG_FUNC
       
   597 	STATENAME("TRSStateEnablingEncryption");
       
   598 	}
       
   599 
       
   600 void TRSStateEnablingEncryption::Enter(CRoleSwitcher& aContext) const
       
   601 	{
       
   602 	LOG_FUNC
       
   603 	if (aContext.iIsEncrypted)
       
   604 		{
       
   605 		aContext.After(KTimeoutOneCommand);
       
   606 		aContext.EnableEncryption();
       
   607 		}
       
   608 	else
       
   609 		{
       
   610 		aContext.EnableLPM();
       
   611 		ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   612 		}
       
   613 	}
       
   614 	
       
   615 void TRSStateEnablingEncryption::Exit(CRoleSwitcher& aContext) const
       
   616 	{
       
   617 	LOG_FUNC
       
   618 	if (aContext.iIsEncrypted)
       
   619 		{
       
   620 		// enable data traffic
       
   621 		aContext.iLinkMgr.LinkManagerProtocol().ACLController().SetParked(aContext.iLink.Handle(), EFalse);
       
   622 		}
       
   623 	}
       
   624 
       
   625 void TRSStateEnablingEncryption::EventReceived(CRoleSwitcher& aContext) const
       
   626 	{
       
   627 	LOG_FUNC
       
   628 	aContext.Cancel(); // watchdog timer
       
   629 	if (aContext.iBasebandEvent.EventType()==ENotifyEncryptionChangeOn &&
       
   630 		aContext.iBasebandEvent.ErrorCode()==KErrNone)
       
   631 		{
       
   632 		aContext.EnableLPM();		
       
   633 		ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   634 		aContext.iIsEncryptionDisabledForRoleSwitch = EFalse;
       
   635 		}
       
   636 	else 
       
   637 		{
       
   638 		LOG(_L("CRoleSwitcher SetEncryption failed, disconnect link"));
       
   639 		if (aContext.iLink.Terminate(ERemoteUserEndedConnection) != KErrNone) 
       
   640 			{
       
   641 			LOG(_L("CRoleSwitcher OOM"));
       
   642 			}
       
   643 		ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   644 		}
       
   645 	}
       
   646 
       
   647 void TRSStateEnablingEncryption::TimerExpired(CRoleSwitcher& aContext) const
       
   648 	{
       
   649 	LOG_FUNC
       
   650 	LOG(_L("CRoleSwitcher Timeout in EncryptionEnable, disconnect"));
       
   651 	aContext.CancelIoctl();			
       
   652 	if (aContext.iLink.Terminate(ERemoteUserEndedConnection) != KErrNone)
       
   653 			{
       
   654 			LOG(_L("CRoleSwitcher OOM"));
       
   655 			}
       
   656 	ChangeState(aContext, CRoleSwitcherStateFactory::EIdle);
       
   657 	}
       
   658 
       
   659 
       
   660