bthci/hci2implementations/hctls/bcsp/src/hctlbcspcontrollermanager.cpp
changeset 0 29b1cd4cb562
child 8 2b6718f05bdb
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2007-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "hctlbcspcontrollermanager.h"
       
    22 
       
    23 #include "debug.h"
       
    24 #include "hctlbcsp.h"
       
    25 #include "bcsputils.h"
       
    26 
       
    27 #include <bluetooth/hci/controllerstateobserver.h>
       
    28 #include <bluetooth/hci/hctluartpowermanager.h>
       
    29 
       
    30 // BlueCore Commands - Must be 18 bytes in length, payload is treated in 16bit units.
       
    31 //                            |Command |Length |SeqNum |BC Cmd |             Payload                   |
       
    32 //                            | SetReq |9 units|  N/a  | Reset | Pad                                   |
       
    33 _LIT8(KBcCmdColdResetCommand, "\x02\x00\x09\x00\x00\x00\x01\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
       
    34 //                            | SetReq |9 units|  N/a  | Halt  | Pad                                   |
       
    35 _LIT8(KBcCmdColdHaltCommand,  "\x02\x00\x09\x00\x00\x00\x03\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
       
    36 
       
    37 
       
    38 CHCTLBcspControllerManager* CHCTLBcspControllerManager::NewL(CHCTLBcsp& aHCTLBcsp, RBusDevComm& aPort,
       
    39 		CHCTLUartBase::TPowerControlDetectionMode aPwrCtrlMode)
       
    40     {
       
    41 	LOG_STATIC_FUNC
       
    42 
       
    43     CHCTLBcspControllerManager* self = new(ELeave) CHCTLBcspControllerManager(aHCTLBcsp, aPort, aPwrCtrlMode);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop(self);
       
    47     return self;
       
    48     }
       
    49 
       
    50 CHCTLBcspControllerManager::~CHCTLBcspControllerManager()
       
    51     {
       
    52 	LOG_FUNC
       
    53 
       
    54  	delete iPowerDownCallback;
       
    55 	delete iReadyToResetControllerCallback;
       
    56 	delete iUartPowerManager;
       
    57 	}
       
    58 
       
    59 CHCTLBcspControllerManager::CHCTLBcspControllerManager(CHCTLBcsp& aHCTLBcsp, RBusDevComm& aPort,
       
    60 		CHCTLUartBase::TPowerControlDetectionMode aPwrCtrlMode)
       
    61  :	iPort(aPort),
       
    62  	iHCTLBcsp(aHCTLBcsp), 
       
    63  	iControllerManagerState(EIdle),
       
    64  	iLastPowerRequest(EBTOn),
       
    65  	iCurrentPowerState(EBTOn),
       
    66 	iCurrentTask(ENoTask),
       
    67 	iPwrCtrlMode(aPwrCtrlMode)
       
    68 	{
       
    69 	LOG_FUNC
       
    70     }
       
    71 
       
    72 void CHCTLBcspControllerManager::ConstructL()
       
    73 	{
       
    74 	LOG_FUNC
       
    75 
       
    76 	iColdResetCommand = KBcCmdColdResetCommand();
       
    77 	iColdHaltCommand = KBcCmdColdHaltCommand();
       
    78 	
       
    79 	TCallBack cbHandlePowerDown(HandlePowerDown, this);
       
    80 	iPowerDownCallback = new(ELeave) CAsyncCallBack(cbHandlePowerDown, CActive::EPriorityStandard);
       
    81 	
       
    82 	TCallBack cbHandleReadyToResetController(HandleReadyToResetController, this);
       
    83 	iReadyToResetControllerCallback = new(ELeave) CAsyncCallBack(cbHandleReadyToResetController, CActive::EPriorityStandard);
       
    84 
       
    85 	// if the ini file is configured to use CTS line to control the 
       
    86 	// device's power on/off control than we instantiate iUartPowerManager.
       
    87 	// otherwise it remains NULL (as implicitly set by CBase base class).
       
    88 	// So, in the destructor, we can delete it directly. 
       
    89 	if (iPwrCtrlMode == CHCTLUartBase::EPwrCtrlCTSTimedLow)
       
    90 		{
       
    91 		iUartPowerManager = CHCTLUartPowerManager::NewL(*this, iPort);
       
    92 		iUartPowerManager->Start();
       
    93 		}
       
    94 	}
       
    95 
       
    96 
       
    97 void CHCTLBcspControllerManager::Start()
       
    98 /*
       
    99 For future use - called when HCTL is started
       
   100 */
       
   101 	{
       
   102 	LOG_FUNC
       
   103 	}
       
   104 
       
   105 void CHCTLBcspControllerManager::MhupoPowerChange(TInt aError)
       
   106 	{
       
   107 	LOG_FUNC
       
   108 
       
   109 	if (aError == KErrNone)
       
   110 		{
       
   111 		// if there is a task underway then the observer will be notified elsewhere
       
   112 		if (iCurrentTask == ENoTask)
       
   113 			{
       
   114 			// unsolicited power change			
       
   115 			iCurrentPowerState = (iCurrentPowerState == EBTOn) ? EBTOff : EBTOn;
       
   116 			LOG1(_L8("Unsolicited power change detected, current power state: %d"), iCurrentPowerState);
       
   117 
       
   118 			if(iObserver)
       
   119 				{
       
   120 				iObserver->McsoProcessPowerChange(KErrNone, MControllerStateObserver::EBTFatalChange, iCurrentPowerState);
       
   121 				}
       
   122 			}
       
   123 		}
       
   124 	else
       
   125 		{
       
   126 		// If we get an error from the uart power manager then just log
       
   127 		// error but do not attempt to restart.
       
   128 		LOG1(_L8("Error detecting power change: %d"), aError);
       
   129 		}
       
   130 	}
       
   131 
       
   132 TInt CHCTLBcspControllerManager::MhpiGetPower(TBTPowerState& aState)
       
   133 	{
       
   134 	if(iCurrentTask != ENoTask)
       
   135 		//Information will not be reliable
       
   136 		{
       
   137 		return KErrInUse;
       
   138 		}
       
   139 
       
   140 	aState = iCurrentPowerState;
       
   141 	return KErrNone;
       
   142 	}
       
   143 	
       
   144 TInt CHCTLBcspControllerManager::MhpiSetPower(TBTPowerState aState)
       
   145 /*
       
   146 To be called from outside the class only, for example in response to user request
       
   147 */
       
   148 	{
       
   149 	TInt rerr = KErrNone;
       
   150 	
       
   151 	switch(iCurrentTask)
       
   152 		{
       
   153 		case EPowerUp:
       
   154 		case EPowerDown:
       
   155 			//If SetPower is called whilst performing a power request error the BT client. 
       
   156 			//There should only be one!
       
   157 			rerr = KErrInUse;
       
   158 			break;
       
   159 			
       
   160 		case EControllerReset:
       
   161 			//Just postpone power request. A power down will be handled when
       
   162 			//the next link establshment comes in.
       
   163 			iLastPowerRequest = aState;
       
   164 			rerr = KErrInUse;
       
   165 			break;
       
   166 			
       
   167 		case ENoTask:
       
   168 			iLastPowerRequest = aState;
       
   169 			if(iCurrentPowerState == iLastPowerRequest)
       
   170 				{
       
   171 				//Nothing to do - need to return an error to complete synchronously
       
   172 				//(Do not tell stack.)
       
   173 				rerr = KErrAlreadyExists;
       
   174 				}
       
   175 			else
       
   176 				{
       
   177 				rerr = DoSetPower(aState);
       
   178 				}
       
   179 			break;
       
   180 			
       
   181 		default:
       
   182 			PANIC(KBcspPanicCat, EInvalidCurrentTask);
       
   183 		};
       
   184 	
       
   185 	return rerr;	
       
   186 	}
       
   187 	
       
   188 TInt CHCTLBcspControllerManager::DoSetPower(TBTPowerState aState)
       
   189 	{
       
   190 	switch(aState)
       
   191 		{
       
   192 		case EBTOff:
       
   193 			{
       
   194 			iHCTLBcsp.WriteBcCmd(iColdHaltCommand); //this should not produce a response
       
   195 			iPowerDownCallback->CallBack();	//allows asynch callback to BTClient.
       
   196 			iCurrentTask = EPowerDown;
       
   197 			iControllerManagerState = EWaiting;
       
   198 			iHCTLBcsp.Choke();
       
   199 			}
       
   200 			break;
       
   201 
       
   202 		case EBTOn:
       
   203 			{
       
   204 			iHCTLBcsp.UnChoke();
       
   205 			iHCTLBcsp.ResetMuzzled();  // Reset the muzzled parameter of the BT host
       
   206 			iHCTLBcsp.WriteBcCmd(iColdResetCommand);
       
   207 			iCurrentTask = EPowerUp;
       
   208 			iControllerManagerState = EResetHardware;
       
   209 			}
       
   210 			break;
       
   211 
       
   212 		default:
       
   213 			PANIC(KBcspPanicCat, EUnexpectedPowerState);
       
   214 			break;
       
   215 		};
       
   216 		
       
   217 	return KErrNone;
       
   218 	}
       
   219 
       
   220 void CHCTLBcspControllerManager::HardReset()
       
   221 	{
       
   222 	// Only perform reset if the power is on and the controller is
       
   223 	// not performing another action.
       
   224 	if(iLastPowerRequest == EBTOn && iCurrentTask == ENoTask)
       
   225 		{
       
   226 		/*
       
   227 		This implementation assumes a dedicated controller reset will be used.
       
   228 		However we provide an example of how a power cycle reset might be implemented.
       
   229 		*/
       
   230 		iReadyToResetControllerCallback->CallBack();//call back will call reset on controller
       
   231 		iCurrentTask = EControllerReset;
       
   232 		iControllerManagerState = EWaiting;
       
   233 		}
       
   234 	}
       
   235 
       
   236 /*
       
   237 	Called when BCSP state reaches 'garrulous'. 
       
   238 	Controller Manager may not want BCSP to inform the outside world, 
       
   239 	for example, if it wishes to do a controller reset, and 
       
   240 	so wishes to manage the controller itself and keep the outside world at bay.
       
   241 */
       
   242 TBool CHCTLBcspControllerManager::BcspLinkEstablished()
       
   243 	{
       
   244 	if(iLastPowerRequest == EBTOff)
       
   245 		/*
       
   246 		Should only get here if a power off request occurred whilst 
       
   247 		awaiting this link establishment - in which case abandon
       
   248 		whatever we were doing and switch power off.
       
   249 		*/
       
   250 		{
       
   251 		DoSetPower(EBTOff);
       
   252 		return EFalse;
       
   253 		}
       
   254 
       
   255 	TBool doTellStack = ETrue;
       
   256 
       
   257 	switch(iCurrentTask)
       
   258 		{
       
   259 		case ENoTask:
       
   260 			break; //not to do with us
       
   261 		case EPowerUp:
       
   262 			{
       
   263 			doTellStack = DoBcspLinkEstablishedForPowerUp();
       
   264 			}
       
   265 			break;
       
   266 		case EControllerReset:
       
   267 			{
       
   268 			doTellStack = DoBcspLinkEstablishedForControllerReset();
       
   269 			}
       
   270 			break;
       
   271 		case EPowerDown:
       
   272 		default:
       
   273 			{
       
   274 	    	__ASSERT_DEBUG(EFalse, PANIC(KBcspPanicCat, EInvalidCurrentTask));
       
   275 			}
       
   276 		}
       
   277 		
       
   278 	return doTellStack;
       
   279 	}
       
   280 
       
   281 /**
       
   282 	Called when the controller starts trying to establish a BCSP link whilst we
       
   283 	think a link is established. This state is intentional if the controller manager
       
   284 	has called a 'ColdReset' command.
       
   285 */
       
   286 TBool CHCTLBcspControllerManager::ExpectedControllerReset()
       
   287 	{
       
   288 	return (iControllerManagerState == EResetHardware);
       
   289 	}
       
   290 
       
   291 TBool CHCTLBcspControllerManager::PowerOffRequested()
       
   292 	{
       
   293 	return (iLastPowerRequest == EBTOff);
       
   294 	}
       
   295 
       
   296 void CHCTLBcspControllerManager::ProcessBcCmdEvent(const TDesC8& /*aEvent*/)
       
   297 	{
       
   298 	//unlikely to happen - but just drop if it does
       
   299 	//maybe useful later if more BCCMDs (BlueCore VSCs) are used.
       
   300 	}
       
   301 	
       
   302 TBool CHCTLBcspControllerManager::DoBcspLinkEstablishedForPowerUp()
       
   303 	{
       
   304 	__ASSERT_DEBUG(iControllerManagerState == EResetHardware, PANIC(KBcspPanicCat, EUnexpectedControllerMgrState));
       
   305 	if(iControllerManagerState == EResetHardware)
       
   306 		{
       
   307 		if(iObserver)
       
   308 			{
       
   309 			iObserver->McsoProcessPowerChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTOn);
       
   310 			}
       
   311 		iCurrentPowerState = EBTOn; 
       
   312 		iCurrentTask = ENoTask;
       
   313 		iControllerManagerState = EIdle; //controller reset finished
       
   314 		}
       
   315 	return ETrue;
       
   316 	}
       
   317 	
       
   318 TBool CHCTLBcspControllerManager::DoBcspLinkEstablishedForControllerReset()
       
   319 	{
       
   320 	TBool doTellStack = ETrue;
       
   321 	
       
   322 	switch(iControllerManagerState)
       
   323 		{
       
   324 		case EResetBCSP:
       
   325 			{
       
   326 			iHCTLBcsp.WriteBcCmd(iColdResetCommand); //should result in callback to CHCTLBcsp::HandlePeerReset
       
   327 			iControllerManagerState = EResetHardware;
       
   328 			doTellStack = EFalse;
       
   329 			}
       
   330 			break;
       
   331 		case EResetHardware:
       
   332 			{
       
   333 			if(iObserver)
       
   334 				{
       
   335 				iObserver->McsoProcessHardResetPhaseChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTResetComplete);
       
   336 				}
       
   337 			iCurrentTask = ENoTask;
       
   338 			iControllerManagerState = EIdle; //controller reset finished
       
   339 			}
       
   340 			break;
       
   341 		case EIdle:
       
   342 			break;
       
   343 
       
   344 		default:
       
   345 			{
       
   346 			PANIC(KBcspPanicCat, EUnexpectedControllerMgrState);
       
   347 			}
       
   348 		}
       
   349 
       
   350 	return doTellStack;
       
   351 	}
       
   352 
       
   353 /*static*/TInt CHCTLBcspControllerManager::HandlePowerDown(TAny* aThis)
       
   354 	{
       
   355 	LOG_STATIC_FUNC
       
   356 	
       
   357 	reinterpret_cast<CHCTLBcspControllerManager*>(aThis)->DoHandlePowerDown();
       
   358 	return KErrNone;
       
   359 	}
       
   360 
       
   361 void CHCTLBcspControllerManager::DoHandlePowerDown()
       
   362 	{
       
   363 	LOG_FUNC
       
   364 	
       
   365 	if(iObserver)
       
   366 		{
       
   367 		iObserver->McsoProcessPowerChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTOff);
       
   368 		}
       
   369 	iCurrentPowerState = EBTOff;
       
   370 	iCurrentTask = ENoTask;
       
   371 	iControllerManagerState = EIdle;
       
   372 	}
       
   373 
       
   374 /*static*/TInt CHCTLBcspControllerManager::HandleReadyToResetController(TAny* aThis)
       
   375 	{
       
   376 	LOG_STATIC_FUNC
       
   377 	
       
   378 	reinterpret_cast<CHCTLBcspControllerManager*>(aThis)->DoHandleReadyToResetController();
       
   379 	return KErrNone;
       
   380 	}
       
   381 
       
   382 void CHCTLBcspControllerManager::DoHandleReadyToResetController()
       
   383 	{
       
   384 	LOG_FUNC
       
   385 	
       
   386 	// asynchronous call to tell stack hard reset has started
       
   387 	LOG(_L8("HCTLUART: ***Hard Reset Started***"));
       
   388 	if(iObserver)
       
   389 		{
       
   390 		iObserver->McsoProcessHardResetPhaseChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTResetStarted);
       
   391 		}
       
   392 	iHCTLBcsp.Reset(); //causes BCSP to re-establish BCSP link (causing call back to CHCLTBcsp::Unchoke())
       
   393 	iControllerManagerState = EResetBCSP;
       
   394 	}
       
   395 
       
   396 void CHCTLBcspControllerManager::McroControllerResetComplete()
       
   397 	{
       
   398 	LOG_FUNC
       
   399 	
       
   400 	if(iObserver)
       
   401 		{
       
   402 		iObserver->McsoProcessHardResetPhaseChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTResetComplete);
       
   403 		}
       
   404 	iCurrentTask = ENoTask;
       
   405 	iControllerManagerState = EIdle;
       
   406 	}
       
   407 
       
   408 void CHCTLBcspControllerManager::McpooPowerOnComplete()
       
   409 	{
       
   410 	if(iObserver)
       
   411 		{
       
   412 		iObserver->McsoProcessPowerChange(KErrNone, MControllerStateObserver::EBTFatalChange, EBTOn);
       
   413 		}
       
   414 	iCurrentTask = ENoTask;
       
   415 	iControllerManagerState = EIdle;
       
   416 	}
       
   417 
       
   418 /**
       
   419  Setter for the observer
       
   420  @param aObserver A event observer the power man can use to notify power changes
       
   421  */
       
   422 void CHCTLBcspControllerManager::SetObserver(MControllerStateObserver& aObserver)
       
   423 	{
       
   424 	iObserver = &aObserver;
       
   425 	}