bluetoothmgmt/bluetoothclientlib/btlib/pairing.cpp
changeset 0 29b1cd4cb562
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 #include <bluetooth/pairing.h>
       
    17 
       
    18 #include <e32uid.h>
       
    19 #include <e32base.h>
       
    20 #include <bluetooth/pairingservershared.h>
       
    21 
       
    22 #include <bluetooth/logger.h>
       
    23 
       
    24 #ifdef __FLOG_ACTIVE
       
    25 _LIT8(KLogComponent, LOG_COMPONENT_PAIRING_SERVER);
       
    26 #endif
       
    27 
       
    28 //
       
    29 // RBluetoothPairingServer
       
    30 //
       
    31 
       
    32 EXPORT_C RBluetoothPairingServer::RBluetoothPairingServer()
       
    33 	{
       
    34 	LOG_FUNC
       
    35 	}
       
    36 
       
    37 /**
       
    38 @return The version of the pairing server the client library is compiled to operate with.
       
    39 */
       
    40 EXPORT_C TVersion RBluetoothPairingServer::Version() const
       
    41 	{
       
    42 	LOG_FUNC
       
    43 	return(TVersion(KPairingServerMajorVersionNumber, KPairingServerMinorVersionNumber, KPairingServerBuildVersionNumber));
       
    44 	}
       
    45 
       
    46 /**
       
    47 Connects to the Bluetooth Pairing Server.
       
    48 This must be successfully called before any other function can be used.
       
    49 @return KErrNone if successful, otherwise the error that occurred
       
    50 */
       
    51 EXPORT_C TInt RBluetoothPairingServer::Connect()
       
    52 	{
       
    53 	CONNECT_LOGGER
       
    54 	LOG_FUNC
       
    55 	__ASSERT_ALWAYS(!Handle(), PANIC(KPairingServerPanicCat, EPairingServerSessionAlreadyConnected));
       
    56 	// Don't start the Bluetooth Protocol Stack. It's either loaded and we'll use it, or else this will fail
       
    57 	return CreateSession(KPairingServerName, Version());
       
    58 	}
       
    59 
       
    60 /**
       
    61 Releases all resources associated with this handle to the pairing server.
       
    62 */
       
    63 EXPORT_C void RBluetoothPairingServer::Close()
       
    64 	{
       
    65 	LOG_FUNC
       
    66 	RSessionBase::Close();
       
    67 	CLOSE_LOGGER
       
    68 	}
       
    69 
       
    70 /**
       
    71 Returns the session for use when connecting subsessions.
       
    72 @internalComponent
       
    73 */
       
    74 RSessionBase& RBluetoothPairingServer::Session()
       
    75 	{
       
    76 	LOG_FUNC
       
    77 	return *static_cast<RSessionBase*>(this);
       
    78 	}
       
    79 
       
    80 
       
    81 //
       
    82 // RBluetoothOobData
       
    83 //
       
    84 
       
    85 EXPORT_C RBluetoothOobData::RBluetoothOobData()
       
    86 	{
       
    87 	LOG_FUNC
       
    88 	}
       
    89 
       
    90 /**
       
    91 Opens the OOB data session to the pairing server through the provided pairing
       
    92 server session.
       
    93 @param aPairingServer The pairing server session through which to establish the OOB data session.
       
    94 @return KErrNone on success, otherwise a Symbian OS error code.
       
    95 */
       
    96 EXPORT_C TInt RBluetoothOobData::Open(RBluetoothPairingServer& aPairingServer)
       
    97 	{
       
    98 	LOG_FUNC
       
    99 	__ASSERT_ALWAYS(aPairingServer.Session().Handle(), PANIC(KPairingServerPanicCat, EPairingServerSessionNotConnected));
       
   100 	__ASSERT_ALWAYS(!SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionAlreadyOpen));
       
   101 	return CreateSubSession(aPairingServer.Session(), EPairingServerCreateOobDataSubSession);
       
   102 	}
       
   103 
       
   104 /**
       
   105 Closes the OOB data session and releases all resources associated with it.
       
   106 Any outstanding asynchronous requests will be cancelled.
       
   107 */
       
   108 EXPORT_C void RBluetoothOobData::Close()
       
   109 	{
       
   110 	LOG_FUNC
       
   111 	RSubSessionBase::CloseSubSession(EPairingServerCloseSubSession);
       
   112 	}
       
   113 
       
   114 /**
       
   115 This invalidates the cached OOB data that has been retrieved from the local Bluetooth
       
   116 controller.  The next call to ReadLocalOobData will result in the new OOB data from the
       
   117 controller.
       
   118 @see RBluetoothOobData::ReadLocalOobData
       
   119 */
       
   120 EXPORT_C void RBluetoothOobData::RefreshLocalOobData()
       
   121 	{
       
   122 	LOG_FUNC
       
   123 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   124 	static_cast<void>(SendReceive(EPairingServerOobDataRefreshLocal));
       
   125 	}
       
   126 
       
   127 /**
       
   128 Upon completion the provided parameters for the simple pairing hash and randomizer will be populated
       
   129 with the current OOB data provided by the Bluetooth controller.  The data retrieved from the controller
       
   130 is cached by the Bluetooth stack, and so if new data is required the RefreshLocalOobData function should
       
   131 be called.
       
   132 @see RBluetoothOobData::RefreshLocalOobData
       
   133 @param aHash Upon completion this will hold the simple pairing hash C for the local Bluetooth controller.
       
   134 @param aRandomizer Upon completion this will hold the simple pairing randomizer R for the local Bluetooth controller.
       
   135 @param aStatus The TRequestStatus that will be completed when the operation has completed, its value will be
       
   136  KErrNone if successful, and a Symbian OS error code if there was a failure.  The aHash and aRandomizer values
       
   137  should be considered invalid if aStatus is not KErrNone.
       
   138 */
       
   139 EXPORT_C void RBluetoothOobData::ReadLocalOobData(TBluetoothSimplePairingHash& aHash, TBluetoothSimplePairingRandomizer& aRandomizer, TRequestStatus& aStatus)
       
   140 	{
       
   141 	LOG_FUNC
       
   142 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   143 	TIpcArgs args(&aHash, &aRandomizer);
       
   144 	SendReceive(EPairingServerOobDataReadLocal, args, aStatus);
       
   145 	}
       
   146 
       
   147 /**
       
   148 Aborts the retrieval of OOB data for the local Bluetooth controller.
       
   149 @see RBluetoothOobData::ReadLocalOobData
       
   150 */
       
   151 EXPORT_C void RBluetoothOobData::CancelReadLocalOobData()
       
   152 	{
       
   153 	LOG_FUNC
       
   154 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   155 	static_cast<void>(SendReceive(EPairingServerOobDataCancelReadLocal));
       
   156 	}
       
   157 
       
   158 /**
       
   159 This function is to allow the provision of OOB data retrieved from remote Bluetooth devices into
       
   160 the Bluetooth stack for use when pairing.  This version of the function is for when the device address, 
       
   161 hash and randomizer have already been parsed from an OOB data packet.
       
   162 The OOB data provided will replace any existing data that has previously been provided.
       
   163 @param aAddr The Bluetooth device address for the remote device whose OOB pairing data has been received.
       
   164 @param aHash The simple pairing hash C value for the remote device.
       
   165 @param aRandomizer The simple pairing randomizer R value for the remote device.
       
   166 @return KErrNone if successful, otherwise a Symbian OS error code.
       
   167 */
       
   168 EXPORT_C TInt RBluetoothOobData::ProvideRemoteOobData(const TBTDevAddr& aAddr, const TBluetoothSimplePairingHash& aHash, const TBluetoothSimplePairingRandomizer& aRandomizer)
       
   169 	{
       
   170 	LOG_FUNC
       
   171 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   172 	TPckgBuf<TBTDevAddr> addrBuf(aAddr);
       
   173 	TIpcArgs args(&addrBuf, &aHash, &aRandomizer);
       
   174 	return SendReceive(EPairingServerOobDataProvideRemoteParsed, args);
       
   175 	}
       
   176 
       
   177 /**
       
   178 This function is to allow the provision of OOB data retrieved from remote Bluetooth devices into
       
   179 the Bluetooth stack for use when pairing.  This version of the function is for when the device address 
       
   180 and hash have already been parsed from an OOB data packet. (The randomizer value is not available).
       
   181 The OOB data provided will replace any existing data that has previously been provided.
       
   182 @param aAddr The Bluetooth device address for the remote device whose OOB pairing data has been received.
       
   183 @param aHash The simple pairing hash C value for the remote device.
       
   184 @return KErrNone if successful, otherwise a Symbian OS error code.
       
   185 */
       
   186 EXPORT_C TInt RBluetoothOobData::ProvideRemoteOobData(const TBTDevAddr& aAddr, const TBluetoothSimplePairingHash& aHash)
       
   187 	{
       
   188 	LOG_FUNC
       
   189 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   190 	TPckgBuf<TBTDevAddr> addrBuf(aAddr);
       
   191 	TIpcArgs args(&addrBuf, &aHash);
       
   192 	return SendReceive(EPairingServerOobDataProvideRemoteParsed, args);
       
   193 	}
       
   194 
       
   195 /**
       
   196 This function is to allow the provision of OOB data retrieved from remote Bluetooth devices into
       
   197 the Bluetooth stack for use when pairing.  This version of the function is for when OOB data has been
       
   198 received from a remote device but has not been parsed by the manager of OOB data.
       
   199 The OOB data provided will replace any existing data that has previously been provided.
       
   200 @param aData The raw OOB data that may contain pairing data.
       
   201 @return KErrNone if successful, otherwise a Symbian OS error code.
       
   202 */
       
   203 EXPORT_C TInt RBluetoothOobData::ProvideRemoteOobData(const TDesC8& aData)
       
   204 	{
       
   205 	LOG_FUNC
       
   206 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   207 	TIpcArgs args(&aData);
       
   208 	return SendReceive(EPairingServerOobDataProvideRemoteRaw, args);
       
   209 	}
       
   210 
       
   211 /**
       
   212 When the OOB data from a remote device is no longer valid, this function should be called.  This will
       
   213 remove any OOB data associated with a particular remote device.
       
   214 @param aAddr The Bluetooth device address for the remote device whose OOB pairing data is no longer valid.
       
   215 */
       
   216 EXPORT_C void RBluetoothOobData::ClearRemoteOobData(const TBTDevAddr& aAddr)
       
   217 	{
       
   218 	LOG_FUNC
       
   219 	__ASSERT_ALWAYS(SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerOobDataSessionNotOpen));
       
   220 	TPckgBuf<TBTDevAddr> addrBuf(aAddr);
       
   221 	TIpcArgs args(&addrBuf);
       
   222 	static_cast<void>(SendReceive(EPairingServerOobDataClearRemote, args));
       
   223 	}
       
   224 
       
   225 
       
   226 //
       
   227 // RBluetoothDedicatedBondingInitiator
       
   228 //
       
   229 
       
   230 EXPORT_C RBluetoothDedicatedBondingInitiator::RBluetoothDedicatedBondingInitiator()
       
   231 	{
       
   232 	LOG_FUNC
       
   233 	}
       
   234 
       
   235 /**
       
   236 Initiates a dedicated bond with a remote Bluetooth device.
       
   237 
       
   238 Once this function is called, the RBluetoothDedicatedBondingInitiator::Close function must
       
   239 be called before RBluetoothDedicatedBondingInitiator::Start the instance can be called again.
       
   240 
       
   241 @param aPairingServer The pairing server session through which to initiate the request.
       
   242 @param aAddr The Bluetooth device address of the remote device to initiate a dedicated bond with.
       
   243 @param aStatus Completed when the dedicated bonding process has completed with a value of KErrNone
       
   244 				if successful, otherwise a Symbian OS error.
       
   245 */
       
   246 EXPORT_C void RBluetoothDedicatedBondingInitiator::Start(RBluetoothPairingServer& aPairingServer, const TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   247 	{
       
   248 	LOG_FUNC
       
   249 	__ASSERT_ALWAYS(aPairingServer.Session().Handle(), PANIC(KPairingServerPanicCat, EPairingServerSessionNotConnected));
       
   250 	__ASSERT_ALWAYS(!SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerDedicatedBondAlreadyIssued));
       
   251 	
       
   252 	// This function encapsulates a connection to the pairing server and a request for a dedicated bond.
       
   253 	TInt err = CreateSubSession(aPairingServer.Session(), EPairingServerCreateDedicatedBondingSubSession);
       
   254 	if(err != KErrNone)
       
   255 		{
       
   256 		// Error so locally complete TRequestStatus.
       
   257 		aStatus = KRequestPending;
       
   258 		TRequestStatus* status = &aStatus;
       
   259 		User::RequestComplete(status, err);
       
   260 		return;
       
   261 		}
       
   262 	
       
   263 	iDedicatedBondAddress() = aAddr;
       
   264 	TIpcArgs args(&iDedicatedBondAddress);
       
   265 	SendReceive(EPairingServerStartDedicatedBond, args, aStatus);
       
   266 	}
       
   267 
       
   268 /**
       
   269 Releases the resources associated with the dedicated bonding initiator.
       
   270 If the bonding is in progress, it will be cancelled.
       
   271 */
       
   272 EXPORT_C void RBluetoothDedicatedBondingInitiator::Close()
       
   273 	{
       
   274 	LOG_FUNC
       
   275 	RSubSessionBase::CloseSubSession(EPairingServerCloseSubSession);
       
   276 	}
       
   277 
       
   278 
       
   279 //
       
   280 // RBluetoothPairingResult
       
   281 //
       
   282 
       
   283 EXPORT_C RBluetoothPairingResult::RBluetoothPairingResult()
       
   284 	: iDeviceAddressBuffer(NULL, 0, 0), iRequest(NULL)
       
   285 	{
       
   286 	LOG_FUNC
       
   287 	}
       
   288 
       
   289 /**
       
   290 Opens the pairing result session to the pairing server through the provided pairing
       
   291 server session.
       
   292 The pairing result session is used to obtain results of simple pairing attempts 
       
   293 using the Numeric Comparison or Passkey Entry association models.
       
   294 @param aPairingServer The pairing server session through which to establish the pairing result session.
       
   295 @return KErrNone on success, otherwise a Symbian OS error code.
       
   296 */
       
   297 EXPORT_C TInt RBluetoothPairingResult::Open(RBluetoothPairingServer& aPairingServer)
       
   298 	{
       
   299 	LOG_FUNC
       
   300 	__ASSERT_ALWAYS(aPairingServer.Session().Handle(), PANIC(KPairingServerPanicCat, EPairingServerSessionNotConnected));
       
   301 	__ASSERT_ALWAYS(!SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerSimplePairingResultSessionAlreadyOpen));
       
   302 	return CreateSubSession(aPairingServer.Session(), EPairingServerCreateSimplePairingResultSubSession);
       
   303 	}
       
   304 
       
   305 /**
       
   306 Closes the pairing result session and releases all resources associated with it.
       
   307 Any outstanding asynchronous requests will be cancelled.
       
   308 */
       
   309 EXPORT_C void RBluetoothPairingResult::Close()
       
   310 	{
       
   311 	LOG_FUNC
       
   312 	RSubSessionBase::CloseSubSession(EPairingServerCloseSubSession);
       
   313 	}
       
   314 
       
   315 /**
       
   316 Upon successful completion the TBTDevAddr parameter will be populated with the Bluetooth device address
       
   317 of the remote device with which a pairing attempt has been made.
       
   318 Only a single request can be queued on an RBluetoothPairingResult instance.
       
   319 
       
   320 By the limitations of the Bluetooth specification, the result of PIN code entry based pairings will not
       
   321 be reported.
       
   322 
       
   323 @param aAddr Upon completion this will hold the Bluetooth device address for the remote device with 
       
   324  which pairing was attempted.
       
   325 @param aStatus The TRequestStatus that will be completed when the operation has completed, its value 
       
   326  will either be a HCI error code (in the Symbian error code space) representing the result of the
       
   327  pairing, or a Symbian OS system-wide error code if there was a failure.  The aAddr value should be
       
   328   considered invalid if aStatus is not in the HCI error code range.
       
   329 */
       
   330 EXPORT_C void RBluetoothPairingResult::SimplePairingResult(TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   331 	{
       
   332 	LOG_FUNC
       
   333 	__ASSERT_ALWAYS(!iRequest || *iRequest != KRequestPending, 
       
   334 		PANIC(KPairingServerPanicCat, EPairingServerSimplePairingResultRequestOutstanding));
       
   335 	iRequest = &aStatus;
       
   336 	iDeviceAddressBuffer.Set(TPckg<TBTDevAddr>(aAddr));
       
   337 	TIpcArgs args(&iDeviceAddressBuffer);
       
   338 	SendReceive(EPairingServerSimplePairingResult, args, aStatus);
       
   339 	}
       
   340 
       
   341 /**
       
   342 Cancels any outstanding request for a simple pairing result.
       
   343 */
       
   344 EXPORT_C void RBluetoothPairingResult::CancelSimplePairingResult()
       
   345 	{
       
   346 	LOG_FUNC
       
   347 	SendReceive(EPairingServerCancelSimplePairingResult);
       
   348 	}
       
   349 
       
   350 
       
   351 //
       
   352 // RBluetoothAuthenticationResult
       
   353 //
       
   354 
       
   355 EXPORT_C RBluetoothAuthenticationResult::RBluetoothAuthenticationResult()
       
   356 	: iDeviceAddressBuffer(NULL, 0, 0),iRequest(NULL)
       
   357 	{
       
   358 	LOG_FUNC
       
   359 	}
       
   360 
       
   361 /**
       
   362 Opens the authentication result session to the pairing server through the provided pairing
       
   363 server session.
       
   364 The authentication result session is used to obtain results of authentication complete events, 
       
   365 which are generated for both simple and legacy pairing attempts.
       
   366 @param aPairingServer The pairing server session through which to establish the authentication result session.
       
   367 @return KErrNone on success, otherwise a Symbian OS error code.
       
   368 */
       
   369 EXPORT_C TInt RBluetoothAuthenticationResult::Open(RBluetoothPairingServer& aPairingServer)
       
   370 	{
       
   371 	LOG_FUNC
       
   372 	__ASSERT_ALWAYS(aPairingServer.Session().Handle(), PANIC(KPairingServerPanicCat, EPairingServerSessionNotConnected));
       
   373 	__ASSERT_ALWAYS(!SubSessionHandle(), PANIC(KPairingServerPanicCat, EPairingServerAuthenticationResultSessionAlreadyOpen));
       
   374 	return CreateSubSession(aPairingServer.Session(), EPairingServerCreateAuthenticationResultSubSession);
       
   375 	}
       
   376 
       
   377 /**
       
   378 Closes the authentication result session and releases all resources associated with it.
       
   379 Any outstanding asynchronous requests will be cancelled.
       
   380 */
       
   381 EXPORT_C void RBluetoothAuthenticationResult::Close()
       
   382 	{
       
   383 	LOG_FUNC
       
   384 	RSubSessionBase::CloseSubSession(EPairingServerCloseSubSession);
       
   385 	}
       
   386 
       
   387 /**
       
   388 Upon successful completion the TBTDevAddr parameter will be populated with the Bluetooth device address
       
   389 of the remote device with which an authentication attempt has been made.
       
   390 Only a single request can be queued on an RBluetoothAuthenticationResult instance.
       
   391 
       
   392 By the limitations of the Bluetooth specification, the result of PIN code entry based pairings will not
       
   393 be reported.
       
   394 
       
   395 @param aAddr Upon completion this will hold the Bluetooth device address for the remote device with
       
   396  which authentication was attempted.
       
   397 @param aStatus The TRequestStatus that will be completed when the operation has completed, its value
       
   398  will either be a HCI error code (in the Symbian error code space) representing the result of the
       
   399  authentication, or a Symbian OS system-wide error code if there was a failure.  The aAddr value should be
       
   400   considered invalid if aStatus is not in the HCI error code range.
       
   401 */
       
   402 EXPORT_C void RBluetoothAuthenticationResult::AuthenticationResult(TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   403 	{
       
   404 	LOG_FUNC
       
   405 	__ASSERT_ALWAYS(!iRequest || *iRequest != KRequestPending,
       
   406 		PANIC(KPairingServerPanicCat, EPairingServerAuthenticationResultRequestOutstanding));
       
   407 	iRequest = &aStatus;
       
   408 	iDeviceAddressBuffer.Set(TPckg<TBTDevAddr>(aAddr));
       
   409 	TIpcArgs args(&iDeviceAddressBuffer);
       
   410 	SendReceive(EPairingServerAuthenticationResult, args, aStatus);
       
   411 	}
       
   412 
       
   413 /**
       
   414 Cancels any outstanding request for an authentication result.
       
   415 */
       
   416 EXPORT_C void RBluetoothAuthenticationResult::CancelAuthenticationResult()
       
   417 	{
       
   418 	LOG_FUNC
       
   419 	SendReceive(EPairingServerCancelAuthenticationResult);
       
   420 	}
       
   421 
       
   422 
       
   423