bluetooth/btstack/HCIShared/src/hcishared.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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @publishedPartner
       
    19 */
       
    20 
       
    21 #include <bluetooth/hci/hcishared.h>
       
    22 #include "btfeatures.h"
       
    23 
       
    24 
       
    25 enum TBTFeaturesPanic
       
    26 	{
       
    27 	ERangeTooLarge,
       
    28 	};
       
    29 
       
    30 void FeaturesPanic(TBTFeaturesPanic aPanic)
       
    31 	{
       
    32 	_LIT(KPanicText, "BTFeatures");
       
    33 	User::Panic(KPanicText, aPanic);
       
    34 	}
       
    35 
       
    36 void Panic(THCISharePanic aPanic)
       
    37 // Panic due to stack bug.
       
    38 	{
       
    39 	User::Panic(KHCISharedPanicName, aPanic);
       
    40 	}
       
    41 
       
    42 /** 
       
    43 Constructor
       
    44 Start with no requests to update the required configuration.
       
    45 */
       
    46 
       
    47 EXPORT_C TBTFeatures::TBTFeatures()
       
    48 	{
       
    49 	Init(0);
       
    50 	}
       
    51 
       
    52 /** 
       
    53 Constructor
       
    54 Start with request to update the required configuration
       
    55 
       
    56 @param aFeatures the required bluetooth features
       
    57 */
       
    58 
       
    59 EXPORT_C TBTFeatures::TBTFeatures(TUint64 aFeatures)
       
    60 	{
       
    61 	Init(aFeatures);
       
    62 	}
       
    63 
       
    64 
       
    65 void TBTFeatures::Init(TUint64 aFeatures)
       
    66 	{
       
    67 	const TInt KMaxBit = 7;
       
    68 	iFeatures.SetMax();
       
    69 	for (TInt pos = 0; pos < iFeatures.MaxSize(); pos++)
       
    70 		{
       
    71 		TUint8 byte = (pos <= KMaxBit) ? (aFeatures >> (pos * 8)) & 0xff : 0;
       
    72 		iFeatures[pos] = byte;
       
    73 		}
       
    74 	}
       
    75 
       
    76 TUint32 TBTFeatures::Extract32BitsOffset(TInt aStart, TInt aEnd, TInt aOffset) const
       
    77 	{
       
    78 	return Extract32Bits(aStart, aEnd) << aOffset;
       
    79 	}
       
    80 
       
    81 TUint32 TBTFeatures::Extract32Bits(TInt aStart, TInt aEnd) const
       
    82 	{
       
    83 	__ASSERT_DEBUG(aEnd - aStart <= 32, FeaturesPanic(ERangeTooLarge));
       
    84 	TUint32 result =0;
       
    85 	TInt outOffset = 0;
       
    86 	for (TInt bit = aStart; bit <= aEnd; bit++)
       
    87 		{
       
    88 		result |= (operator[](bit) ? 1<<outOffset : 0);
       
    89 		outOffset++;
       
    90 		}
       
    91 	
       
    92 	return result;
       
    93 	}
       
    94 
       
    95 
       
    96 /**
       
    97 Subscript operator.
       
    98 
       
    99 Return an individual bit from BT features
       
   100 
       
   101 @param aOffset Object to compare to this.
       
   102 @return ETrue if the individual bit is 1, otherwise EFalse returned
       
   103 */
       
   104 
       
   105 EXPORT_C TBool TBTFeatures::operator[](TInt aOffset) const
       
   106 	{
       
   107 	TInt byte = aOffset / 8;
       
   108 	TInt offset = aOffset - (byte * 8);
       
   109 	return TBool(iFeatures[byte] & (1<<offset));
       
   110 	}
       
   111 
       
   112 
       
   113 /** 
       
   114 Return current link mode set  
       
   115 
       
   116 @return current link mode set 
       
   117 */
       
   118 
       
   119 EXPORT_C TBTLinkModeSet TBTFeatures::LinkModes() const
       
   120 	{
       
   121 	return TBTLinkModeSet(Extract32Bits(ESupportedHoldModeBit, ESupportedParkModeBit));
       
   122 	}
       
   123 
       
   124 
       
   125 /** 
       
   126 Return current SCO packets type
       
   127 
       
   128 @return current SCO packets type 
       
   129 */
       
   130 EXPORT_C TBTSCOPackets TBTFeatures::SCOPackets() const
       
   131 	{
       
   132 	return TBTSCOPackets(Extract32Bits(ESupportedSCOLinkBit, ESupportedHV3PacketsBit) * EPacketsHV1);
       
   133 	}
       
   134 
       
   135 /** 
       
   136 Return current synchronous packet types (SCO and eSCO on v1.2).
       
   137 
       
   138 @return current synchronous packet types (SCO and eSCO on v1.2).
       
   139 */
       
   140 EXPORT_C TBTSyncPacketTypes TBTFeatures::SyncPackets() const
       
   141 	{
       
   142 	TBTSyncPacketTypes packets = Extract32Bits(ESupportedSCOLinkBit, ESupportedHV3PacketsBit) | Extract32BitsOffset(ESupportedExtendedSCOLinkBit, ESupportedEV5PacketsBit, 3);
       
   143 	return packets;
       
   144 	}
       
   145 
       
   146 /** 
       
   147 Return current supported mode
       
   148 
       
   149 @param aMode specified link modes to compare 
       
   150 @return ETrue if specified link modes are supported, otherwise EFalse returned
       
   151 */
       
   152 EXPORT_C TBool TBTFeatures::IsModeSupported(TBTLinkMode aMode) const
       
   153 	{
       
   154 	return LinkModes() & aMode;
       
   155 	}
       
   156 
       
   157 /** 
       
   158 Return whether role switch is supported
       
   159 
       
   160 @return ETrue if role switch is supported, otherwise EFalse returned
       
   161 */
       
   162 EXPORT_C TBool TBTFeatures::IsRoleSwitchSupported() const
       
   163 	{
       
   164 	return operator[](ESupportedSwitchBit);
       
   165 	}
       
   166 
       
   167 /** 
       
   168 Return whether encryption is supported
       
   169 
       
   170 @return ETrue if encryption is supported, otherwise EFalse returned
       
   171 */
       
   172 EXPORT_C TBool TBTFeatures::IsEncryptionSupported() const
       
   173 	{
       
   174 	return operator[](ESupportedEncryptionBit);
       
   175 	}
       
   176 
       
   177 /** 
       
   178 Return whether Extended Inquiry Response is supported
       
   179 
       
   180 @return ETrue if Extended Inquiry Response is supported, otherwise EFalse returned
       
   181 */
       
   182 EXPORT_C TBool TBTFeatures::IsExtendedInquiryResponseSupported() const
       
   183 	{
       
   184 	return operator[](ESupportedExtendedInquiryResponseBit);
       
   185 	}
       
   186 
       
   187 /** 
       
   188 Return whether Read signal strength indication with inquiry is supported
       
   189 
       
   190 @return ETrue if Read signal strength indication with inquiry is supported, otherwise EFalse returned
       
   191 */
       
   192 EXPORT_C TBool TBTFeatures::IsRssiWithInquiryResultsSupported() const
       
   193 	{
       
   194 	return operator[](ESupportedRSSIWithInquiryResultsBit);
       
   195 	}
       
   196 
       
   197 /** 
       
   198 Return whether Secure Simple Pairing is supported
       
   199 
       
   200 @return ETrue if Secure Simple Pairing is supported, otherwise EFalse returned
       
   201 */
       
   202 EXPORT_C TBool TBTFeatures::IsSecureSimplePairingSupported() const
       
   203 	{
       
   204 	return operator[](ESupportedSecureSimplePairingBit);
       
   205 	}
       
   206 
       
   207 /** 
       
   208 Return whether Encryption Pause and Resume is supported
       
   209 
       
   210 @return ETrue if Encryption Pause and Resume is supported, otherwise EFalse returned
       
   211 */
       
   212 EXPORT_C TBool TBTFeatures::IsEncryptionPauseResumeSupported() const
       
   213 	{
       
   214 	return operator[](ESupportedEncryptionPauseResumeBit);
       
   215 	}
       
   216 
       
   217 /** 
       
   218 Return whether SCO link is supported
       
   219 
       
   220 @return ETrue if SCO link is supported, otherwise EFalse returned
       
   221 */
       
   222 EXPORT_C TBool TBTFeatures::IsSCOLinkSupported() const
       
   223 	{
       
   224 	return operator[](ESupportedSCOLinkBit);
       
   225 	}
       
   226 
       
   227 /** 
       
   228 Return whether Extended SCO link is supported
       
   229 
       
   230 @return ETrue if Extended SCO link is supported, otherwise EFalse returned
       
   231 */
       
   232 
       
   233 EXPORT_C TBool TBTFeatures::IsExtendedSCOLinkSupported() const
       
   234 	{
       
   235 	return operator[](ESupportedExtendedSCOLinkBit);
       
   236 	}
       
   237 
       
   238 
       
   239 
       
   240 /** 
       
   241 Indicate whether a low power mode is allowed
       
   242 
       
   243 @param aMode Bluetooth SIG specified values for indicating link modes
       
   244 @return ETrue if the indicated low power modes are allowed, otherwise return EFalse
       
   245 */
       
   246 
       
   247 EXPORT_C TBool TLinkPolicy::IsModeAllowed(TBTLinkMode aMode) const
       
   248 	{
       
   249 	TBool allowed;
       
   250 
       
   251 	switch (aMode)
       
   252 		{
       
   253 		case EHoldMode:
       
   254 		case ESniffMode:
       
   255 		case EParkMode:
       
   256 			allowed = iLowPowerModePolicy & aMode;
       
   257 			break;
       
   258 
       
   259 		default:
       
   260 			Panic(EBTUnknownLowPowerMode);
       
   261 			allowed = EFalse;
       
   262 		}
       
   263 	
       
   264 	return allowed;
       
   265 	}
       
   266 
       
   267 /** 
       
   268 Indicate whether mode switch is allowed
       
   269 
       
   270 @return ETrue if mode switch is allowed, otherwise return EFalse
       
   271 */
       
   272 
       
   273 EXPORT_C TBool TLinkPolicy::IsSwitchAllowed() const
       
   274 	{
       
   275 	return iSwitchAllowed;
       
   276 	}
       
   277 
       
   278 /** 
       
   279 Set permission or prohibition to a specific low power mode
       
   280 
       
   281 @param aMode Bluetooth SIG specified values for indicating link modes
       
   282 @param aAllowed the permission or prohibition to a specific low power mode
       
   283 */
       
   284 
       
   285 EXPORT_C void TLinkPolicy::SetModeAllowed(TBTLinkMode aMode, TBool aAllowed)
       
   286 	{
       
   287 	switch (aMode)
       
   288 		{
       
   289 		case EHoldMode:
       
   290 		case ESniffMode:
       
   291 		case EParkMode:
       
   292 			aAllowed ? iLowPowerModePolicy |= aMode : iLowPowerModePolicy &= ~aMode;
       
   293 			break;
       
   294 
       
   295 		default:
       
   296 			Panic(EBTUnknownLowPowerMode);
       
   297 		}
       
   298 	}
       
   299 
       
   300 /** 
       
   301 Set permission to low power modes
       
   302 
       
   303 @param aModesAllowed the low power modes to be allowed or not
       
   304 */
       
   305 
       
   306 EXPORT_C void TLinkPolicy::SetModesAllowed(TUint8 aModesAllowed)
       
   307 	{
       
   308 	iLowPowerModePolicy = aModesAllowed;
       
   309 	}
       
   310 
       
   311 /** 
       
   312 Set permission to low power modes switch
       
   313 
       
   314 @param aAllowed the low power modes switch to be allowed or not
       
   315 */
       
   316 
       
   317 EXPORT_C void TLinkPolicy::SetSwitchAllowed(TBool aAllowed)
       
   318 	{
       
   319 	iSwitchAllowed = aAllowed;
       
   320 	}
       
   321 
       
   322 
       
   323 /** 
       
   324 Return current low power mode policy 
       
   325 
       
   326 @return current low power mode policy
       
   327 */
       
   328 
       
   329 EXPORT_C TUint8 TLinkPolicy::LowPowerModePolicy() const
       
   330 	{
       
   331 	return iLowPowerModePolicy;
       
   332 	}
       
   333 
       
   334 /** 
       
   335 Return current link policy  
       
   336 
       
   337 @return current link policy 
       
   338 */
       
   339 
       
   340 EXPORT_C TUint8 TLinkPolicy::LinkPolicy() const
       
   341 	{
       
   342 	return static_cast<TUint8>(iSwitchAllowed + (iLowPowerModePolicy << 1));
       
   343 	}
       
   344 
       
   345 
       
   346 /** 
       
   347 Constructor
       
   348 Start with default configuration
       
   349 */
       
   350 EXPORT_C TBTBasebandLinkState::TBTBasebandLinkState()
       
   351  :	iLinkState(ELinkDown), 
       
   352 	iAuthenticated(EFalse), 
       
   353 	iEncrypted(EFalse),
       
   354 	iMode(EActiveMode),
       
   355 	iRole(ERoleUnknown),
       
   356 	iPacketTypes(EAnyPacket),
       
   357 	iMaxSlots(5)
       
   358 	{
       
   359 	}
       
   360 
       
   361 /** 
       
   362 Set the link state
       
   363 
       
   364 @param aState link state values 
       
   365 */
       
   366 EXPORT_C void TBTBasebandLinkState::SetLinkState(TLinkState aState)		
       
   367 	{ 
       
   368  	iLinkState = aState; 
       
   369 	}
       
   370 
       
   371 /** 
       
   372 Set the authentication state
       
   373 
       
   374 @param aAuthenticated TBool value indicates whether authentication is allowed or not
       
   375 */
       
   376 EXPORT_C void TBTBasebandLinkState::SetAuthenticated(TBool aAuthenticated)
       
   377 	{ 
       
   378  	iAuthenticated = aAuthenticated;
       
   379 	}
       
   380 
       
   381 /** 
       
   382 Set the encryption state
       
   383 
       
   384 @param aEncrypted TBool value indicates whether encryption is allowed or not
       
   385 */
       
   386 EXPORT_C void TBTBasebandLinkState::SetEncrypted(TBool aEncrypted)
       
   387 	{ 
       
   388  	iEncrypted = aEncrypted;
       
   389 	}
       
   390 
       
   391 /** 
       
   392 Set the link mode
       
   393 
       
   394 @param aMode TBool value indicates the BT link mode
       
   395 */
       
   396 EXPORT_C void TBTBasebandLinkState::SetLinkMode(TBTLinkMode aMode)			
       
   397 	{ 
       
   398  	iMode = aMode;
       
   399 	}
       
   400 
       
   401 /** 
       
   402 Set the link role
       
   403 
       
   404 @param aRole Bluetooth SIG specified values for specification of (piconet) role
       
   405 */
       
   406 EXPORT_C void TBTBasebandLinkState::SetLinkRole(TBTBasebandRole aRole)		
       
   407 	{ 
       
   408  	iRole = aRole;
       
   409 	}
       
   410 
       
   411 /** 
       
   412 Set the packet types
       
   413 
       
   414 @param aPacketTypes Bluetooth SIG specified packet types
       
   415 */
       
   416 EXPORT_C void TBTBasebandLinkState::SetPacketTypes(TUint16 aPacketTypes)	
       
   417 	{ 
       
   418  	iPacketTypes = aPacketTypes;
       
   419 	}
       
   420 
       
   421 /** 
       
   422 Set the maximum types
       
   423 
       
   424 @param aMaxSlots LMP maximum slot
       
   425 */
       
   426 EXPORT_C void TBTBasebandLinkState::SetMaxSlots(TUint8 aMaxSlots)				
       
   427 	{ 
       
   428  	iMaxSlots = aMaxSlots;
       
   429 	}
       
   430 
       
   431 /** 
       
   432 Return current baseband link state 
       
   433 
       
   434 @return current baseband link state  
       
   435 */
       
   436 EXPORT_C TBTBasebandLinkState::TLinkState TBTBasebandLinkState::LinkState() const				
       
   437 	{ 
       
   438 	return iLinkState;
       
   439 	}
       
   440 
       
   441 /** 
       
   442 Return current authentication status 
       
   443 
       
   444 @return current authentication status  
       
   445 */
       
   446 EXPORT_C TBool TBTBasebandLinkState::Authenticated() const					
       
   447 	{ 
       
   448 	return iAuthenticated;
       
   449 	}
       
   450 
       
   451 /** 
       
   452 Return current encryption status 
       
   453 
       
   454 @return current encryption status  
       
   455 */
       
   456 EXPORT_C TBool TBTBasebandLinkState::Encrypted() const						
       
   457 	{ 
       
   458 	return iEncrypted;
       
   459 	}
       
   460 
       
   461 /** 
       
   462 Return current baseband link state 
       
   463 
       
   464 @return current baseband link state 
       
   465 */
       
   466 EXPORT_C TBTLinkMode TBTBasebandLinkState::LinkMode() const				
       
   467 	{ 
       
   468 	return iMode;
       
   469 	}
       
   470 
       
   471 /** 
       
   472 Return current baseband link role 
       
   473 
       
   474 @return current baseband link role 
       
   475 */
       
   476 EXPORT_C TBTBasebandRole TBTBasebandLinkState::LinkRole() const			
       
   477 	{ 
       
   478 	return iRole;
       
   479 	}
       
   480 
       
   481 /** 
       
   482 Return current baseband packet types 
       
   483 
       
   484 @return current baseband packet types 
       
   485 */
       
   486 EXPORT_C TUint16 TBTBasebandLinkState::PacketTypes() const					
       
   487 	{ 
       
   488 	return iPacketTypes;
       
   489 	}
       
   490 
       
   491 /** 
       
   492 Return current baseband max slots 
       
   493 
       
   494 @return current baseband max slots 
       
   495 */
       
   496 EXPORT_C TUint8 TBTBasebandLinkState::MaxSlots() const						
       
   497 	{ 
       
   498 	return iMaxSlots;
       
   499 	}