bluetooth/btstack/common/RegistryHelpers.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // RegistryHelpers.CPP
       
     2 
       
     3 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 // All rights reserved.
       
     5 // This component and the accompanying materials are made available
       
     6 // under the terms of "Eclipse Public License v1.0"
       
     7 // which accompanies this distribution, and is available
       
     8 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 //
       
    10 // Initial Contributors:
       
    11 // Nokia Corporation - initial contribution.
       
    12 //
       
    13 // Contributors:
       
    14 //
       
    15 // Description:
       
    16 // Small classes to assist stack in using Registry
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "RegistryHelpers.h"
       
    21 #include "hostresolver.h"
       
    22 #include "linkmgr.h"
       
    23 #include "linkutil.h"
       
    24 #include "debug.h"
       
    25 #include <comms-infras/commsdebugutility.h>
       
    26 
       
    27 __FLOG_STMT(_LIT8(KSubsys, "btstack");)
       
    28 __FLOG_STMT(_LIT8(KComponent, "btregistryhelpers");)
       
    29 
       
    30 //
       
    31 // Classes apertaining to MBTRegistryTaskNotifier //
       
    32 //
       
    33 
       
    34 CBTRegistryHelperBase::CBTRegistryHelperBase(MBTRegistryTaskNotifier& aParent, CRegistrySession& aRegSess)
       
    35 	: CActive(EPriorityHigh)
       
    36 	, iRegSess(aRegSess)
       
    37 	, iParent(&aParent)
       
    38 	{
       
    39 	// high for servicing the protocol quickly
       
    40 	CActiveScheduler::Add(this);
       
    41 	}
       
    42 
       
    43 CBTRegistryHelperBase::~CBTRegistryHelperBase()
       
    44 	{
       
    45 	__FLOG_STATIC(KSubsys, KComponent, _L8("sec\tRegistryHelper destructing"));
       
    46 	Cancel();
       
    47 	iRegistry.Close();
       
    48 	iRegSess.Close();
       
    49 	}
       
    50 
       
    51 void CBTRegistryHelperBase::ConstructL()
       
    52 	{
       
    53 	iRegSess.Open(); // Open the resource (as we will close on delete).
       
    54 	User::LeaveIfError(iRegistry.Open(iRegSess.RegServ()));
       
    55 	}
       
    56 
       
    57 void CBTRegistryHelperBase::DoCancel()
       
    58 	{
       
    59 	iRegistry.CancelRequest(iStatus);
       
    60 	}
       
    61 
       
    62 void CBTRegistryHelperBase::RunL()
       
    63 	{
       
    64 	// Store the value from iStatus in case another active request is started. 
       
    65 	TInt rValue = iStatus.Int();
       
    66 	
       
    67 	// Check that the registry access is complete.
       
    68 	if(IsRegistryTaskCompleteL())
       
    69 		{
       
    70 		// Is the parent of this object still available to be notified
       
    71 		// of completion.
       
    72 		if(iParent)
       
    73 			{
       
    74 			Notify(rValue);
       
    75 			}
       
    76 		else
       
    77 			{
       
    78 			delete this;
       
    79 			}
       
    80 		}
       
    81 	}
       
    82 
       
    83 TInt CBTRegistryHelperBase::RunError(TInt aError)
       
    84 	{
       
    85 	if(iParent)
       
    86 		{
       
    87 		Notify(aError);
       
    88 		}
       
    89 	else
       
    90 		{
       
    91 		delete this;
       
    92 		}
       
    93 	return KErrNone;
       
    94 	}
       
    95 
       
    96 TBool CBTRegistryHelperBase::IsRegistryTaskCompleteL()
       
    97 	{
       
    98 	// Default implementation.
       
    99 	return ETrue;
       
   100 	}
       
   101 	
       
   102 void CBTRegistryHelperBase::Notify(TInt aError)
       
   103 	{
       
   104 	// Default implementation.
       
   105 	__FLOG_STATIC2(KSubsys, KComponent, _L8("CBTRegistryHelperBase::Notify(%d), this 0x%08x"), aError, this);
       
   106 	iParent->RegistryTaskComplete(this, aError);
       
   107 	}
       
   108 
       
   109 void CBTRegistryHelperBase::DetachParent()
       
   110 	{
       
   111 	__FLOG_STATIC3(KSubsys, KComponent, _L8("CBTRegistryHelperBase::DetachParent(), this 0x%08x, Current Parent 0x%08x, IsActive() %d")
       
   112 			       ,this, iParent, IsActive());
       
   113 	
       
   114 	// If the helper is not currently active then this object must be deleted.
       
   115 	if(!IsActive())
       
   116 		{
       
   117 		delete this;
       
   118 		}
       
   119 	else
       
   120 		{
       
   121 		iParent = NULL;
       
   122 		}
       
   123 	}
       
   124 	
       
   125 CBTDeviceGetter::CBTDeviceGetter(MBTRegistryTaskNotifier& aParent, CRegistrySession& aRegSess)
       
   126 : CBTRegistryHelperBase(aParent, aRegSess)
       
   127 	{
       
   128 	}
       
   129 
       
   130 CBTDeviceGetter* CBTDeviceGetter::NewL(CRegistrySession& aRegSess,
       
   131 									  MBTRegistryTaskNotifier& aParent)
       
   132 	{
       
   133 	CBTDeviceGetter* self = new(ELeave) CBTDeviceGetter(aParent, aRegSess);
       
   134 	CleanupStack::PushL(self);
       
   135 	self->ConstructL();
       
   136 	CleanupStack::Pop(self);
       
   137 	return self;
       
   138 	}
       
   139 
       
   140 void CBTDeviceGetter::Start(const TBTDevAddr& aAddr)
       
   141 	{
       
   142 	__FLOG_STATIC1(KSubsys, KComponent, _L8("Registry helper (Getter 0x%08x) started..."), this);
       
   143 	iDevice.SetAddress(aAddr);
       
   144 	iRegistry.GetDevice(iDevice, iStatus);
       
   145 	SetActive();
       
   146 	}
       
   147 
       
   148 void CBTDeviceGetter::Notify(TInt aError)
       
   149 	{
       
   150 	iParent->RegistryTaskComplete(this, iDevice, aError);
       
   151 	}
       
   152 
       
   153 CBTDeviceModifier* CBTDeviceModifier::NewL(CRegistrySession& aRegSess, MBTRegistryTaskNotifier& aParent, CBTInquiryMgr& aInquiryMgr)
       
   154 	{
       
   155 	CBTDeviceModifier* self = new(ELeave) CBTDeviceModifier(aParent, aInquiryMgr, aRegSess);
       
   156 	CleanupStack::PushL(self);
       
   157 	self->ConstructL();
       
   158 	CleanupStack::Pop(self);
       
   159 	return self;
       
   160 	}
       
   161 
       
   162 CBTDeviceModifier::CBTDeviceModifier(MBTRegistryTaskNotifier& aParent, CBTInquiryMgr& aInquiryMgr, CRegistrySession& aRegSess)
       
   163 : CBTRegistryHelperBase(aParent, aRegSess), iInquiryMgr(aInquiryMgr), iRegistryUpdateStatus(ENoChange)
       
   164 	{
       
   165 	}
       
   166 
       
   167 void CBTDeviceModifier::Start(const TBTNamelessDevice& aDevice, TBool aCanAddDevice)
       
   168 	{
       
   169 	__FLOG_STATIC1(KSubsys, KComponent, _L8("Registry helper (Modifier 0x%08x) started..."), this);
       
   170 
       
   171 	// Store the arguments and check if the device specified by the
       
   172 	// aDevice argument is currently present in the registry.
       
   173 	iCanAddDevice = aCanAddDevice;
       
   174 	iDevice = aDevice;
       
   175 
       
   176 	iRegistrySearch.Reset();
       
   177 	iRegistrySearch.FindAddress(iDevice.Address());
       
   178 	
       
   179 	// Check the registry for this address.  If a record exists the iStatus
       
   180 	// in the RunL will be 1.
       
   181 	iRegistry.CreateView(iRegistrySearch, iStatus);
       
   182 	iState = EFindingDevice;
       
   183 	SetActive();
       
   184 	}
       
   185 
       
   186 CBTDeviceModifier::~CBTDeviceModifier()
       
   187 	{
       
   188 	Cancel();
       
   189 	delete iDeviceWithName;
       
   190 	}
       
   191 
       
   192 TBool CBTDeviceModifier::IsRegistryTaskCompleteL()
       
   193 	{
       
   194 	switch(iState)
       
   195 		{
       
   196 		case EFindingDevice:
       
   197 			{
       
   198 			// A view has been created based on the address of the new device.
       
   199 			// If the iStatus value is 1 then the device has been found.
       
   200 			// Note.  The value can never be greater than 1.  The server will
       
   201 			// PANIC if this condition were to occur therefore the code here does
       
   202 			// not need to check this condition.
       
   203 			if(iStatus.Int() > 0)
       
   204 				{
       
   205 				// The device is in the registry, so it can be modified
       
   206 				iRegistry.ModifyDevice(iDevice, iStatus);
       
   207 				iState = EModifyingDevice;
       
   208 				iCanAddDevice = EFalse;		// reinforce this, as its needed in the subsequent state
       
   209 				SetActive();
       
   210 				}
       
   211 			else if (iStatus.Int()==KErrNotFound)
       
   212 				{
       
   213 				// The device could not be found.  Check if its valid to
       
   214 				// add the device.
       
   215 				if(iCanAddDevice)
       
   216 					{
       
   217 					// The device should be added into the registry
       
   218 					delete iDeviceWithName;
       
   219 					iDeviceWithName = NULL;
       
   220 					iDeviceWithName = CBTDevice::NewL(iDevice); 
       
   221 					iRegistry.AddDeviceL(*iDeviceWithName, iStatus); 
       
   222 					iState = EAddingDevice;
       
   223 					SetActive();
       
   224 					}
       
   225 				// ELSE Nothing more to be done - the modification will be lost.
       
   226 				}
       
   227 			}
       
   228 			break;				
       
   229 
       
   230 		case EAddingDevice:
       
   231 		case EModifyingDevice:
       
   232 			{
       
   233   			if (iStatus.Int() == KErrNone)
       
   234 	  			{
       
   235 				if (iCanAddDevice)
       
   236 					{
       
   237 					// This informs the Phy that an a reg action has occurred
       
   238 					iRegistryUpdateStatus = EAddition;
       
   239 					}
       
   240 				else
       
   241 					{
       
   242 					iRegistryUpdateStatus = EModification;
       
   243 					}
       
   244 	  			}
       
   245   			else
       
   246 	  			{
       
   247 	  			__FLOG_STATIC1(KSubsys, KComponent, _L8("CBTDeviceModifier::IsRegistryTaskCompleteL() - registry update problem (TRequestStatus %d)"), iStatus.Int());
       
   248 	  			}
       
   249 	
       
   250  			// now try to put the device name in if we have one	
       
   251  			const TDesC8* name = iInquiryMgr.DeviceNameFromCache(iDevice.Address());
       
   252  			if(name && (name->Length()>0))
       
   253 				{
       
   254  				iRegistry.ModifyBluetoothDeviceNameL(iDevice.Address(), *name, iStatus); 
       
   255  				iState = EModifyingName;
       
   256  				SetActive();
       
   257 				}
       
   258 			}
       
   259 			break;
       
   260 
       
   261 		case EModifyingName:
       
   262 		default:
       
   263 			break;
       
   264 		};
       
   265 		
       
   266 	// If more modifications are required to the registry this AO
       
   267 	// will be active.  i.e., if this AO is active then the registry
       
   268 	// task is NOT complete.
       
   269 	return (!IsActive());
       
   270 	}
       
   271 
       
   272 void CBTDeviceModifier::Notify(TInt aError)
       
   273 	{
       
   274 	// Inform the Phy whether the registry has been modified
       
   275 	iParent->RegistryTaskComplete(this, iRegistryUpdateStatus, aError);
       
   276 	}
       
   277 
       
   278 CBTDeviceNameChanger::CBTDeviceNameChanger(MBTRegistryTaskNotifier& aParent,CRegistrySession& aRegSess)
       
   279 : CBTRegistryHelperBase(aParent, aRegSess)
       
   280 	{
       
   281 	}
       
   282 
       
   283 CBTDeviceNameChanger* CBTDeviceNameChanger::NewL(CRegistrySession& aRegSess,
       
   284 									  MBTRegistryTaskNotifier& aParent)
       
   285 	{
       
   286 	CBTDeviceNameChanger* self = new(ELeave) CBTDeviceNameChanger(aParent, aRegSess);
       
   287 	CleanupStack::PushL(self);
       
   288 	self->ConstructL();
       
   289 	CleanupStack::Pop(self);
       
   290 	return self;
       
   291 	}
       
   292 
       
   293 void CBTDeviceNameChanger::Start(const TBTDevAddr& aAddr, const TBTDeviceName8& aName)
       
   294 	{
       
   295 	__FLOG_STATIC1(KSubsys, KComponent, _L8("Registry helper (NameChanger 0x%08x) started..."), this);
       
   296 	iAddr = aAddr;
       
   297 	iName = aName;
       
   298 	TRAPD(err, iRegistry.ModifyBluetoothDeviceNameL(iAddr, iName, iStatus));
       
   299 	SetActive();
       
   300 	if(err != KErrNone)
       
   301 		{
       
   302 		TRequestStatus* status = &iStatus;
       
   303 		User::RequestComplete(status, err);
       
   304 		}
       
   305 	}
       
   306 
       
   307 CBTLinkKeyDeleter::CBTLinkKeyDeleter(MBTRegistryTaskNotifier& aParent, CRegistrySession& aRegSess)
       
   308 : CBTRegistryHelperBase(aParent, aRegSess)
       
   309 	{
       
   310 	}
       
   311 
       
   312 void CBTLinkKeyDeleter::Start(const TBTDevAddr& aDeviceAddr)
       
   313 	{
       
   314 	__FLOG_STATIC1(KSubsys, KComponent, _L8("Registry helper (Linkkey deleter 0x%08x) started..."), this);
       
   315 	iRegistry.UnpairDevice(aDeviceAddr, iStatus);
       
   316 	SetActive();
       
   317 	}
       
   318 
       
   319 CBTLinkKeyDeleter* CBTLinkKeyDeleter::NewL(CRegistrySession& aRegSess,
       
   320 										  MBTRegistryTaskNotifier& aParent)
       
   321 	{
       
   322 	CBTLinkKeyDeleter* self = new(ELeave) CBTLinkKeyDeleter(aParent, aRegSess);
       
   323 	CleanupStack::PushL(self);
       
   324 	self->ConstructL();
       
   325 	CleanupStack::Pop(self);
       
   326 	return self;
       
   327 	}
       
   328 
       
   329 
       
   330 
       
   331 
       
   332 //
       
   333 // Classes apertaining to MBtPairedDeviceNotifier //
       
   334 //
       
   335 
       
   336 
       
   337 // ----------------------------------------------------------
       
   338 //	Active Paired Device Getter (from Registry)
       
   339 // ----------------------------------------------------------
       
   340 
       
   341 CBTPairedBDAddrGetter::CBTPairedBDAddrGetter(MBTPairedDeviceNotifier& aNotifier, RBTRegServ& aRegServ)
       
   342 	: CActive(EPriorityStandard)
       
   343 	, iNotifier(aNotifier)
       
   344 	, iRegistryServer(aRegServ)
       
   345 	{
       
   346 	CActiveScheduler::Add(this);
       
   347 	}
       
   348 
       
   349 CBTPairedBDAddrGetter::~CBTPairedBDAddrGetter()
       
   350 	{
       
   351 	Cancel();
       
   352 	delete iResponseHandler;
       
   353 	iView.Close();
       
   354 	}
       
   355 	
       
   356 CBTPairedBDAddrGetter* CBTPairedBDAddrGetter::NewL(MBTPairedDeviceNotifier& aNotifier, RBTRegServ& aRegServ)
       
   357 	{
       
   358 	CBTPairedBDAddrGetter* self = new (ELeave) CBTPairedBDAddrGetter(aNotifier, aRegServ);
       
   359 	CleanupStack::PushL(self);
       
   360 	self->ConstructL();
       
   361 	CleanupStack::Pop(self);
       
   362 	return self;
       
   363 	}
       
   364 	
       
   365 void CBTPairedBDAddrGetter::ConstructL()
       
   366 	{
       
   367 	User::LeaveIfError(iView.Open(iRegistryServer));
       
   368 	}
       
   369 	
       
   370 void CBTPairedBDAddrGetter::Retrieve()
       
   371 	{
       
   372 	if(!IsActive())
       
   373 		{
       
   374 		// keep copy as we go async
       
   375 		iRegistrySearch.Reset();
       
   376 		iRegistrySearch.FindBonded();
       
   377 		iView.CreateView(iRegistrySearch, iStatus);
       
   378 		iState = ECreatingView;
       
   379 		SetActive();
       
   380 		}
       
   381 	}
       
   382 
       
   383 void CBTPairedBDAddrGetter::RunL()
       
   384 	{
       
   385 	// check for error
       
   386 	TInt result = iStatus.Int();
       
   387 	if (result < 0)
       
   388 		{
       
   389 		User::Leave(result);
       
   390 		}
       
   391 		
       
   392 	switch (iState)
       
   393 		{
       
   394 		case ECreatingView:
       
   395 			{
       
   396 			if (result == 0)
       
   397 				{
       
   398 				User::Leave(KErrNotFound); // let RunError handle this
       
   399 				}
       
   400 			else
       
   401 				{
       
   402 				delete iResponseHandler; //just in case not freed
       
   403 				iResponseHandler = NULL;
       
   404 				iResponseHandler = CBTRegistryResponse::NewL(iView);
       
   405 				iResponseHandler->Start(iStatus);
       
   406 				SetActive();
       
   407 				iState = EUsingResponse;
       
   408 				}
       
   409 			break;
       
   410 			}
       
   411 			
       
   412 		case EUsingResponse:
       
   413 			{
       
   414 			RBTDeviceArray& array(iResponseHandler->Results());
       
   415 			iNotifier.MbpdnAddPairedDevices(array);
       
   416 			iState = EReady;
       
   417 			break;
       
   418 			}
       
   419 
       
   420 		case EReady:
       
   421 		default:
       
   422 			{
       
   423 			__ASSERT_DEBUG(EFalse, Panic(EPairedBDAddrGetterUnexpectedState));
       
   424 			break;
       
   425 			}
       
   426 		}
       
   427 	}
       
   428 	
       
   429 	
       
   430 TInt CBTPairedBDAddrGetter::RunError(TInt aError)
       
   431 	{
       
   432 	iNotifier.MbpdnErrorInGettingPairedDevices(aError);
       
   433 	iState = EReady;
       
   434 	return KErrNone;
       
   435 	}
       
   436 	
       
   437 
       
   438 void CBTPairedBDAddrGetter::DoCancel()
       
   439 	{
       
   440 	// Cancelling registry search.
       
   441 	iView.CancelRequest(iStatus);
       
   442 	// Cancelling response retrieval.
       
   443 	delete iResponseHandler;
       
   444 	iResponseHandler = NULL;
       
   445 	// Resetting state
       
   446 	iState = EReady;
       
   447 	}
       
   448 
       
   449