bluetooth/btstack/secman/oobdata.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2008-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 // OOB Data Manager.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <bttypes.h>
       
    25 
       
    26 #include <bluetooth/pairingservershared.h>
       
    27 #include <bluetooth/hcicommandqueue.h>
       
    28 
       
    29 #include <bluetooth/hci/event.h>
       
    30 #include <bluetooth/hci/readlocaloobdatacommand.h>
       
    31 #include <bluetooth/hci/readlocaloobdatacompleteevent.h>
       
    32 
       
    33 #include "oobdata.h"
       
    34 #include "pairingserver.h"
       
    35 
       
    36 #include <bluetooth/logger.h>
       
    37 #ifdef __FLOG_ACTIVE
       
    38 _LIT8(KLogComponent, LOG_COMPONENT_PAIRING_SERVER);
       
    39 #endif
       
    40 
       
    41 #include "secman.h"
       
    42 
       
    43 //
       
    44 // COobDataManager
       
    45 //
       
    46 
       
    47 COobDataManager* COobDataManager::NewL(CBTSecMan& aSecMan)
       
    48 	{
       
    49 	LOG_STATIC_FUNC
       
    50 	COobDataManager* self = new(ELeave) COobDataManager(aSecMan);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53 	CleanupStack::Pop(self);
       
    54 	return self;
       
    55 	}
       
    56 
       
    57 COobDataManager::COobDataManager(CBTSecMan& aSecMan)
       
    58 	: CBase()
       
    59 	, iLocalOobState(ELocalOobInvalid)
       
    60 	, iLocalOobObservers(_FOFF(XOobLocalDataObserver, iLink))
       
    61 	, iSecMan(aSecMan)
       
    62 	{
       
    63 	LOG_FUNC
       
    64 	}
       
    65 
       
    66 void COobDataManager::SetHCICommandQueue(MHCICommandQueue& aCommandQueue)
       
    67 	{
       
    68 	LOG_FUNC
       
    69 	__ASSERT_DEBUG(!iCommandQueue, PANIC(KOobDataPanicCat, EOobDataPanicCommandQueueAlreadyProvided));
       
    70 	iCommandQueue = &aCommandQueue;
       
    71 	}
       
    72 
       
    73 void COobDataManager::ClearHCICommandQueue()
       
    74 	{
       
    75 	LOG_FUNC
       
    76 	if(iCommandQueue)
       
    77 		{
       
    78 		iCommandQueue->MhcqRemoveAllCommands(*this);
       
    79 		}
       
    80 	iCommandQueue = NULL;
       
    81 	}
       
    82 
       
    83 MHCICommandQueue& COobDataManager::CommandQueueL() const
       
    84 	{
       
    85 	LOG_FUNC
       
    86 	if(!iCommandQueue)
       
    87 		{
       
    88 		LEAVEL(KErrNotReady);
       
    89 		}
       
    90 	return *iCommandQueue;
       
    91 	}
       
    92 
       
    93 void COobDataManager::ConstructL()
       
    94 	{
       
    95 	LOG_FUNC
       
    96 	}
       
    97 
       
    98 COobDataManager::~COobDataManager()
       
    99 	{
       
   100 	LOG_FUNC
       
   101 	}
       
   102 
       
   103 void COobDataManager::ProvideRemoteOobDataL(const TOobData& aData)
       
   104 	{
       
   105 	LOG_FUNC
       
   106 	TInt ix = iOobDataFromPeers.Find(aData.Addr(), TOobData::IsForAddress);
       
   107 	if(ix == KErrNotFound)
       
   108 		{
       
   109 		iOobDataFromPeers.AppendL(aData);
       
   110 		}
       
   111 	else
       
   112 		{
       
   113 		__ASSERT_DEBUG(ix >= 0, PANIC(KOobDataPanicCat, EOobDataPanicErrorWhenFindingOobDataForProvision));
       
   114 		iOobDataFromPeers[ix] = aData;
       
   115 		}
       
   116 	}
       
   117 
       
   118 void COobDataManager::ClearRemoteOobData(const TBTDevAddr& aAddr)
       
   119 	{
       
   120 	LOG_FUNC
       
   121 	TInt ix = iOobDataFromPeers.Find(aAddr, TOobData::IsForAddress);
       
   122 	if(ix != KErrNotFound) // ignore if we cannot find an existing entry.
       
   123 		{
       
   124 		__ASSERT_DEBUG(ix >= 0, PANIC(KOobDataPanicCat, EOobDataPanicErrorWhenFindingOobDataForClear));
       
   125 		iOobDataFromPeers.Remove(ix);
       
   126 		iOobDataFromPeers.GranularCompress();
       
   127 		}
       
   128 #ifdef _DEBUG
       
   129 	// When in debug builds, check that there is no entry for the referenced device
       
   130 	// address once one has been removed (there should only be a single entry per
       
   131 	// remote device).
       
   132 	ix = iOobDataFromPeers.Find(aAddr, TOobData::IsForAddress);
       
   133 	__ASSERT_DEBUG(ix == KErrNotFound, PANIC(KOobDataPanicCat, EOobDataPanicMultipleOobEntriesForDevice));
       
   134 #endif // _DEBUG
       
   135 	}
       
   136 
       
   137 
       
   138 void COobDataManager::ReadLocalOobData(XOobLocalDataObserver& aObserver)
       
   139 	{
       
   140 	switch(iLocalOobState)
       
   141 		{
       
   142 		case ELocalOobValid:
       
   143 			// synchronous complete.
       
   144 			aObserver.XoldoLocalOobDataRetrieved(KErrNone, iOobHashC, iOobRandomizerR);
       
   145 			break;
       
   146 			
       
   147 		case ELocalOobUpdating:
       
   148 			// The update is outstanding and so the observer can be completed when the
       
   149 			// result comes in.
       
   150 			__ASSERT_DEBUG(!aObserver.iLink.IsQueued(), PANIC(KOobDataPanicCat, EOobDataPanicLocalOobObserverIsAlreadyQueuedDuringUpdate));
       
   151 			iLocalOobObservers.AddLast(aObserver);
       
   152 			break;
       
   153 			
       
   154 		case ELocalOobInvalid:
       
   155 			// If invalid then we should issue a request to retrieve the data.
       
   156 			__ASSERT_DEBUG(!aObserver.iLink.IsQueued(), PANIC(KOobDataPanicCat, EOobDataPanicLocalOobObserverIsAlreadyQueuedWhileInvalid));
       
   157 			iLocalOobObservers.AddLast(aObserver);
       
   158 			MakeLocalOobRequestToController();
       
   159 			break;
       
   160 		default:
       
   161 			__ASSERT_DEBUG(EFalse, PANIC(KOobDataPanicCat, EOobDataPanicUnknownState));
       
   162 			break;
       
   163 		}
       
   164 	}
       
   165 
       
   166 void COobDataManager::CancelReadLocalOobData(XOobLocalDataObserver& aObserver)
       
   167 	{
       
   168 	LOG_FUNC
       
   169 	// This requests doesn't actually cancel the retrieval of the local OOB data
       
   170 	// from the controller.  Instead it merely removes the observer from being called.
       
   171 	// back.
       
   172 	aObserver.iLink.Deque();
       
   173 	// Callback with to indicate the cancel. 
       
   174 	aObserver.XoldoLocalOobDataRetrieved(KErrCancel, iOobHashC, iOobRandomizerR);
       
   175 	}
       
   176 
       
   177 void COobDataManager::RefreshLocalOobData()
       
   178 	{
       
   179 	LOG_FUNC
       
   180 	// Don't do much, just set the state as invalid.  If there is an outstanding
       
   181 	// new set of OOB data then don't do anything (as we are being refreshed.)
       
   182 	if(iLocalOobState == ELocalOobValid)
       
   183 		{
       
   184 		iLocalOobState = ELocalOobInvalid;
       
   185 		}
       
   186 	}
       
   187 
       
   188 void COobDataManager::MakeLocalOobRequestToController()
       
   189 	{
       
   190 	LOG_FUNC
       
   191 	TRAPD(err, MakeLocalOobRequestToControllerL());
       
   192 	if(err != KErrNone)
       
   193 		{
       
   194 		CompleteQueuedLocalOobObservers(err);
       
   195 		}
       
   196 	}
       
   197 
       
   198 void COobDataManager::MakeLocalOobRequestToControllerL()
       
   199 	{
       
   200 	LOG_FUNC
       
   201 	if (!iSecMan.LocalSimplePairingMode())
       
   202 		{
       
   203 		User::Leave(KErrNotSupported);
       
   204 		}
       
   205 	CReadLocalOOBDataCommand* cmd = CReadLocalOOBDataCommand::NewL();
       
   206 	CleanupStack::PushL(cmd);
       
   207 	MHCICommandQueue& commandQueue = CommandQueueL();
       
   208 	CleanupStack::Pop(cmd);
       
   209 	commandQueue.MhcqAddCommandL(cmd, *this);
       
   210 	}
       
   211 
       
   212 
       
   213 void COobDataManager::MhcqcCommandEventReceived(const THCIEventBase& aEvent, const CHCICommandBase* __DEBUG_ONLY(aRelatedCommand))
       
   214 	{
       
   215 	LOG_FUNC
       
   216 	__ASSERT_DEBUG(aRelatedCommand && (aRelatedCommand->Opcode() == KReadLocalOOBDataOpcode),
       
   217 			PANIC(KOobDataPanicCat, EOobDataPanicUnexpectedHciCommandAssociated));
       
   218 	__ASSERT_DEBUG(aEvent.EventCode() == ECommandCompleteEvent,
       
   219 			PANIC(KOobDataPanicCat, EOobDataPanicUnexpectedHciEventReceived));
       
   220 	const THCICommandCompleteEvent& completeEvent = THCICommandCompleteEvent::Cast(aEvent);
       
   221 	__ASSERT_DEBUG(completeEvent.CommandOpcode() == KReadLocalOOBDataOpcode,
       
   222 			PANIC(KOobDataPanicCat, EOobDataPanicUnexpectedHciCommandCompleteEventReceived));
       
   223 	const TReadLocalOOBDataCompleteEvent& event = TReadLocalOOBDataCompleteEvent::Cast(completeEvent);
       
   224 	
       
   225 	iOobHashC = event.OOBDataC();
       
   226 	iOobRandomizerR = event.OOBDataR();
       
   227 	
       
   228 	iLocalOobState = ELocalOobValid;
       
   229 	CompleteQueuedLocalOobObservers(KErrNone);
       
   230 	}
       
   231 
       
   232 void COobDataManager::MhcqcCommandErrored(TInt aErrorCode, const CHCICommandBase* __DEBUG_ONLY(aCommand))
       
   233 	{
       
   234 	LOG_FUNC
       
   235 	__ASSERT_DEBUG(aCommand && (aCommand->Opcode() == KReadLocalOOBDataOpcode),
       
   236 			PANIC(KOobDataPanicCat, EOobDataPanicUnexpectedHciCommandAssociatedWithError));
       
   237 	iLocalOobState = ELocalOobInvalid;
       
   238 	CompleteQueuedLocalOobObservers(aErrorCode);
       
   239 	}
       
   240 
       
   241 void COobDataManager::CompleteQueuedLocalOobObservers(TInt aResult)
       
   242 	{
       
   243 	LOG_FUNC
       
   244 	TDblQueIter<XOobLocalDataObserver> iter(iLocalOobObservers);
       
   245 	while(XOobLocalDataObserver* obs = iter++)
       
   246 		{
       
   247 		obs->iLink.Deque();
       
   248 		obs->XoldoLocalOobDataRetrieved(aResult, iOobHashC, iOobRandomizerR);
       
   249 		}
       
   250 	}
       
   251 
       
   252 
       
   253 
       
   254 
       
   255 TBool COobDataManager::GetRemoteOobData(const TBTDevAddr& aAddr, TBluetoothSimplePairingHash& aHashC, TBluetoothSimplePairingRandomizer& aRandomizerR)
       
   256 	{
       
   257 	LOG_FUNC
       
   258 	TBool found = EFalse;
       
   259 	TInt ix = iOobDataFromPeers.Find(aAddr, TOobData::IsForAddress);
       
   260 	if(ix == KErrNotFound)
       
   261 		{
       
   262 		aHashC = KNullDesC8;
       
   263 		aRandomizerR = KNullDesC8;
       
   264 		}
       
   265 	else
       
   266 		{
       
   267 		__ASSERT_DEBUG(ix >= 0, PANIC(KOobDataPanicCat, EOobDataPanicErrorWhenFindingOobDataForGet));
       
   268 		found = ETrue;
       
   269 		aHashC = iOobDataFromPeers[ix].HashC();
       
   270 		aRandomizerR = iOobDataFromPeers[ix].RandomizerR();
       
   271 		// Now invalidate (remove) this OOB data; only allow retrieval once
       
   272 		iOobDataFromPeers.Remove(ix);
       
   273 		iOobDataFromPeers.GranularCompress();
       
   274 		}
       
   275 	return found;
       
   276 	}
       
   277 
       
   278 TBool COobDataManager::HasRemoteOobData(const TBTDevAddr& aAddr)
       
   279 	{
       
   280 	LOG_FUNC
       
   281 	TInt ix = iOobDataFromPeers.Find(aAddr, TOobData::IsForAddress);
       
   282 	__ASSERT_DEBUG((ix >= 0 || ix == KErrNotFound), 
       
   283 			PANIC(KOobDataPanicCat, EOobDataPanicErrorWhenFindingOobDataForHas));
       
   284 	return ix != KErrNotFound;
       
   285 	}
       
   286 
       
   287 
       
   288 //
       
   289 // TOobData
       
   290 //
       
   291 
       
   292 TOobData::TOobData(const TBTDevAddr& aAddr, const TBluetoothSimplePairingHash& aHashC, const TBluetoothSimplePairingRandomizer&  aRandomizerR)
       
   293 	: iAddr(aAddr)
       
   294 	, iHashC(aHashC)
       
   295 	, iRandomizerR(aRandomizerR)
       
   296 	{
       
   297 	LOG_FUNC
       
   298 	}
       
   299 	
       
   300 const TBTDevAddr& TOobData::Addr() const
       
   301 	{
       
   302 	LOG_FUNC
       
   303 	return iAddr;
       
   304 	}
       
   305 
       
   306 const TBluetoothSimplePairingHash& TOobData::HashC() const
       
   307 	{
       
   308 	LOG_FUNC
       
   309 	return iHashC;
       
   310 	}
       
   311 
       
   312 const TBluetoothSimplePairingRandomizer& TOobData::RandomizerR() const
       
   313 	{
       
   314 	LOG_FUNC
       
   315 	return iRandomizerR;
       
   316 	}
       
   317 
       
   318 TBool TOobData::IsForAddress(const TBTDevAddr* aAddr, const TOobData& aOobData)
       
   319 	{
       
   320 	LOG_STATIC_FUNC
       
   321 	return aAddr && *aAddr == aOobData.Addr();
       
   322 	}
       
   323