bluetoothmgmt/bluetoothclientlib/btlib/uuid.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2000-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 <es_sock.h>
       
    17 #include <bttypes.h>
       
    18 #include "btsocketpanic.h"
       
    19 
       
    20 
       
    21 static const TUint8 KBluetoothBaseUUID[] =
       
    22 	{
       
    23 	/*Replaced by short UUID: 0x00, 0x00, 0x00, 0x00*/
       
    24 	0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
       
    25 	0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
       
    26 	};
       
    27 
       
    28 static const TUint8 KSymbianBaseUUID[] =
       
    29 	{
       
    30 	/*Replaced by TUid: 0x00, 0x00, 0x00, 0x00*/
       
    31 	0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
       
    32 	0x00, 0x00, 0x1B, 0x20, 0xC7, 0xAE
       
    33 	};
       
    34 
       
    35 /**
       
    36 Default constructor.
       
    37 Sets the UUID to all zeros.
       
    38 **/
       
    39 EXPORT_C TUUID::TUUID()
       
    40 	{
       
    41 	iUUID.Reset();
       
    42 	}
       
    43 
       
    44 /**
       
    45 Construct UUID from TUint32.
       
    46 The Bluetooth base UUID will be appended to the value passed.
       
    47 Use this form of constructor for building 16 or 32 bit short-form
       
    48 UUIDs.
       
    49 @param aLong is a Big-endian format TUint32 containing the UUID.
       
    50 **/
       
    51 EXPORT_C TUUID::TUUID(TUint32 aLong)
       
    52 	{
       
    53 	BigEndian::Put32(iUUID.Begin(), aLong);
       
    54 	Mem::Copy(iUUID.Begin()+4, KBluetoothBaseUUID, sizeof(KBluetoothBaseUUID));
       
    55 	}
       
    56 
       
    57 /**
       
    58 128 bit UUID constructor.
       
    59 Use this constructor to build full length 128 bit UUIDs.
       
    60 @param	aHH Highest order word (bits 96 - 127) in its Big-endian format
       
    61 @param	aHL Second highest order word (bits 64 - 95) in its Big-endian format
       
    62 @param	aLH Secong lowset order word (bits 32 - 63) in its Big-endian format
       
    63 @param	aLL Low order word (bits 0 - 31) in its Big-endian format
       
    64 **/
       
    65 EXPORT_C TUUID::TUUID(TUint32 aHH, TUint32 aHL, TUint32 aLH, TUint32 aLL)
       
    66 	{
       
    67 	BigEndian::Put32(&iUUID[0], aHH);
       
    68 	BigEndian::Put32(&iUUID[4], aHL);
       
    69 	BigEndian::Put32(&iUUID[8], aLH);
       
    70 	BigEndian::Put32(&iUUID[12],aLL);
       
    71 	}
       
    72 
       
    73 /**
       
    74 Set Bluetooth UUID from an EPOC UUID.
       
    75 Uses a well known reserved range. This defines a one-to-one
       
    76 mapping between all EPOC UIDs and and a range of Bluetooth UUID.
       
    77 
       
    78 Base UUID used is 00000000-0000-1000-8000-00001B20C7AE; the first four
       
    79 bytes are replaced by the 32 bit Sybmian OS Uid, in big-endian form.
       
    80 
       
    81 @param aUid	EPOC UID to set this Bluetooth UUID from
       
    82 **/
       
    83 EXPORT_C TUUID::TUUID(const TUid& aUid)
       
    84 	{
       
    85 	BigEndian::Put32(iUUID.Begin(), aUid.iUid);
       
    86 	Mem::Copy(iUUID.Begin()+4, KSymbianBaseUUID, sizeof(KSymbianBaseUUID));
       
    87 	}
       
    88 
       
    89 /**
       
    90 Set the UUID from a binary descriptor.
       
    91 Can accept a 2, 4, or 16 byte descriptor.
       
    92 Any other value will cause a leave with @c KErrArgument.
       
    93 If a 2 or 4 byte descriptor is set, the rest of the UUID will be filled
       
    94 with the Bluetooth well-known base UUID.
       
    95 
       
    96 @param aDes The binary descriptor. Note that the descriptor is interpretted to be in Big-endian format,
       
    97 i.e. index 0 holds the most significant byte.
       
    98 @leave This method will leave if an error occurs.
       
    99 **/
       
   100 EXPORT_C void TUUID::SetL(const TDesC8& aDes)
       
   101 	{
       
   102 	TPtr8 dst(iUUID.Begin(), KSdpUUIDMaxLength);
       
   103 	switch(aDes.Length())
       
   104 		{
       
   105 	// only 2 bytes given - so fill first two terms in iUUUD array with zeros
       
   106 	// then fall through to fill next two terms with contents of aDes
       
   107 	// then last 12 terms with default extension
       
   108 	case 2:
       
   109 		dst.FillZ(2); //fill first two bytes of 'dst' with zeros
       
   110 	// if fall through to here see comment above 'case 2'
       
   111 	// otherwise: 4 bytes given - so fill first four terms in iUUUD array with contents of aDes
       
   112 	// then last 12 terms with default extension
       
   113 	case 4:
       
   114 		dst.Append(aDes);
       
   115 		dst.Append(KBluetoothBaseUUID, sizeof(KBluetoothBaseUUID));
       
   116 		break;
       
   117 	// all 16 bytes given - copy them all to the iUUID array.
       
   118 	case 16:
       
   119 		dst.Copy(aDes);
       
   120 		break;
       
   121 	default:
       
   122 		User::Leave(KErrArgument);
       
   123 		}
       
   124 	}
       
   125 
       
   126 /**
       
   127 Returns the full 128 bit version of the UUID.
       
   128 @return The long form version of the UUID.
       
   129 **/
       
   130 EXPORT_C const TPtrC8 TUUID::LongForm() const
       
   131 	{
       
   132 	return TPtrC8(iUUID.Begin(), KSdpUUIDMaxLength);
       
   133 	}
       
   134 
       
   135 /**
       
   136 Get the smallest version the UUID will compress down to.
       
   137 Might be 2, 4, or 16 bytes.
       
   138 If 2 or 4 bytes, the ommitted bytes are implicity those
       
   139 from the Bluetooth base UUID.
       
   140 @return TPtrC8 pointing to 2, 4, or 16 bytes of significant address.
       
   141 **/
       
   142 EXPORT_C const TPtrC8 TUUID::ShortestForm() const
       
   143 	{
       
   144 	TPtrC8 uuid=LongForm();
       
   145 	
       
   146 	TInt minSize = MinimumSize();
       
   147 	
       
   148 	switch (minSize)
       
   149 		{
       
   150 		case 16:
       
   151 			// 128 bit
       
   152 			return uuid;
       
   153 		
       
   154 		case 4:
       
   155 			// 32 bit
       
   156 			return uuid.Left(4);
       
   157 		}
       
   158 	
       
   159 	// 16 bit version
       
   160 	return uuid.Mid(2,2);
       
   161 	}
       
   162 
       
   163 // HACKME used to hack the size of the TUUID returned, see AttrValue
       
   164 EXPORT_C const TPtrC8 TUUID::Des() const
       
   165 	{
       
   166 	return ShortestForm();
       
   167 	}
       
   168 
       
   169 
       
   170 /**
       
   171  * Return the UUID with a specified length.
       
   172  *
       
   173  * If 2 or 4 bytes, the ommitted bytes are implicity those from the Bluetooth
       
   174  * base UUID.
       
   175  *
       
   176  * @param aLength The required length (2, 4 or 16 bytes).
       
   177  * @return TPtrC8 pointing to 2, 4, or 16 bytes of significant UUID data.
       
   178  * @leave KErrArgument The UUID length requested is invalid.
       
   179  * @leave KErrTotalLossOfPrecision The UUID minimum size is less than the requested size.
       
   180  * @deprecated use TUUID::SpecifiedLengthL() that implements the right size comparison logic
       
   181  */
       
   182 EXPORT_C const TPtrC8 TUUID::FixedLengthL(TInt aLength) const
       
   183 	{
       
   184 	TPtrC8 uuid=LongForm();
       
   185 	
       
   186 	TInt minSize = MinimumSize();
       
   187 	if (minSize < aLength)
       
   188 		User::Leave(KErrTotalLossOfPrecision);
       
   189 	
       
   190 	switch (aLength)
       
   191 		{
       
   192 		case 2:
       
   193 			return uuid.Mid(2,2);
       
   194 		
       
   195 		case 4:
       
   196 			return uuid.Left(4);
       
   197 
       
   198 		case 16:
       
   199 			// Will always be sufficient to represent this UUID.
       
   200 			// Rather than returning directly, break out of switch
       
   201 			// to convince compiler that function will always return a value.
       
   202 			break;
       
   203 
       
   204 		default:
       
   205 			User::Leave(KErrArgument);
       
   206 		}
       
   207 	
       
   208 	return uuid;
       
   209 	}
       
   210 
       
   211 
       
   212 /**
       
   213  * Returns the minimum size of this UUID.
       
   214  */
       
   215 EXPORT_C TInt TUUID::MinimumSize() const
       
   216 	{
       
   217 	TPtrC8 uuid=LongForm();
       
   218 
       
   219 	TInt minSize = 16;
       
   220 	if (uuid.Mid(4) == TPtrC8(KBluetoothBaseUUID, sizeof(KBluetoothBaseUUID)))
       
   221 		minSize = 4;
       
   222 	if ((minSize == 4) && (BigEndian::Get16(uuid.Ptr()) == 0))
       
   223 		minSize = 2;
       
   224 	
       
   225 	return minSize;
       
   226 	}
       
   227 	/**
       
   228 Comparison operator.
       
   229 @param aUUID Object to compare to this.
       
   230 @return ETrue is aUUID is the same as this, EFalse if not.
       
   231 */
       
   232 EXPORT_C TBool TUUID::operator==(const TUUID& aUUID) const
       
   233 	{
       
   234 	return Mem::Compare(iUUID.Begin(), KSdpUUIDMaxLength, aUUID.iUUID.Begin(), KSdpUUIDMaxLength)==0;
       
   235 	}
       
   236 
       
   237 /**
       
   238 Inequality operator.
       
   239 @param aUUID Object to compare to this.
       
   240 @return EFalse is aUUID is the same as this, ETrue if not.
       
   241 */
       
   242 EXPORT_C TBool TUUID::operator!=(const TUUID& aUUID) const
       
   243 	{
       
   244 	return !(*this==aUUID);
       
   245 	}
       
   246 
       
   247 /**
       
   248 Access a single element of the UUID.
       
   249 @param aIndex The index of the element to access.
       
   250 @return The element.  The reference remains valid as long as this object is in scope.
       
   251 */
       
   252 EXPORT_C const TUint8 &TUUID::operator[](TInt aIndex) const
       
   253 	{
       
   254 	return iUUID[aIndex];
       
   255 	}
       
   256 
       
   257 /**
       
   258 Access a single element of the UUID.
       
   259 @param aIndex The index of the element to access.
       
   260 @return The element.  The reference remains valid as long as this object is in scope.
       
   261 */
       
   262 EXPORT_C TUint8 &TUUID::operator[](TInt aIndex)
       
   263 	{
       
   264 	return iUUID[aIndex];
       
   265 	}
       
   266 
       
   267 /**
       
   268  * Return the UUID with a specified length.
       
   269  *
       
   270  * Does not allow compressing a UUID to less than its shortest form.
       
   271  * If 2 or 4 bytes, the ommitted bytes are implicity those from the Bluetooth
       
   272  * base UUID.
       
   273  *
       
   274  * @param aLength The required length (2, 4 or 16 bytes).
       
   275  * @return TPtrC8 pointing to 2, 4, or 16 bytes of significant UUID data.
       
   276  * @leave KErrArgument The UUID length requested is invalid.
       
   277  * @leave KErrTotalLossOfPrecision The UUID cannot be compressed to the requested length.
       
   278  */
       
   279 EXPORT_C const TPtrC8 TUUID::SpecifiedLengthL(TInt aLength) const
       
   280 	{
       
   281 	TPtrC8 uuid=LongForm();
       
   282 	
       
   283 	TInt minSize = MinimumSize();
       
   284 	if (aLength < minSize)
       
   285 		User::Leave(KErrTotalLossOfPrecision);
       
   286 	
       
   287 	switch (aLength)
       
   288 		{
       
   289 		case 2:
       
   290 			return uuid.Mid(2,2);
       
   291 		
       
   292 		case 4:
       
   293 			return uuid.Left(4);
       
   294 
       
   295 		case 16:
       
   296 			// Will always be sufficient to represent this UUID.
       
   297 			// Rather than returning directly, break out of switch
       
   298 			// to convince compiler that function will always return a value.
       
   299 			break;
       
   300 
       
   301 		default:
       
   302 			User::Leave(KErrArgument);
       
   303 		}
       
   304 	
       
   305 	return uuid;
       
   306 	}
       
   307 
       
   308 /**
       
   309 Set the UUID from a binary descriptor containing Little-endian format UUID.
       
   310 Can accept a 2, 4, or 16 byte descriptor.
       
   311 Any other value will cause a leave with @c KErrArgument.
       
   312 If a 2 or 4 byte descriptor is set, the rest of the UUID will be filled
       
   313 with the Bluetooth well-known base UUID.
       
   314 
       
   315 @param aDes The binary descriptor. Note that the descriptor is interpretted to be in Little-endian format,
       
   316 i.e. index 0 holds the most significant byte.
       
   317 @leave This method will leave if an error occurs.
       
   318 **/
       
   319 EXPORT_C void TUUID::SetFromLittleEndianL(const TDesC8& aDes)
       
   320 	{
       
   321 	TBuf8<KSdpUUIDMaxLength> buf;
       
   322 	TInt length = aDes.Length();
       
   323 	__ASSERT_ALWAYS(length <= KSdpUUIDMaxLength, User::Leave(KErrArgument));
       
   324 	for(TInt i = length - 1; i >=0 ; i--)
       
   325 		{
       
   326 		buf.Append(aDes[i]);
       
   327 		}
       
   328 	SetL(buf);
       
   329 	}
       
   330 
       
   331 // ------ RUUIDContainer --------------------------------------------------------------------------
       
   332 
       
   333 
       
   334 EXPORT_C void RUUIDContainer::Close()
       
   335 	{
       
   336 	iUUIDs.Close();
       
   337 	}
       
   338 
       
   339 EXPORT_C void RUUIDContainer::Reset()
       
   340 	{
       
   341 	iUUIDs.Reset();
       
   342 	iUUIDs.Compress();
       
   343 	}
       
   344 
       
   345 EXPORT_C TInt RUUIDContainer::Count() const
       
   346 	{
       
   347 	return iUUIDs.Count();
       
   348 	}
       
   349 
       
   350 /**
       
   351 Check if a particular TUUID is present in the RUUIDContainer
       
   352 @param aUuid the TUUID to be looked up in the container class
       
   353 @return ETrue if it is present otherwise EFalse
       
   354 */
       
   355 EXPORT_C TBool RUUIDContainer::IsPresent(const TUUID& aUuid) const
       
   356 	{
       
   357 	TBool ret = EFalse;
       
   358 	TUint count = iUUIDs.Count();
       
   359 	for (TUint i = 0; i < count; i++)
       
   360 		{
       
   361 		if (aUuid == iUUIDs[i])
       
   362 			{
       
   363 			ret = ETrue;
       
   364 			break;
       
   365 			}
       
   366 		}
       
   367 	return ret;
       
   368 	}
       
   369 
       
   370 /**
       
   371 Add an TUUID into RUUIDContainer
       
   372 @param aUuid the TUUID to be added into the container class
       
   373 @return a system wide error code.
       
   374 */
       
   375 EXPORT_C TInt RUUIDContainer::Add(const TUUID& aUuid)
       
   376 	{
       
   377 	if (!IsPresent(aUuid))
       
   378 		{
       
   379 		return iUUIDs.Append(aUuid);
       
   380 		}
       
   381 	else
       
   382 		{
       
   383 		return KErrAlreadyExists;
       
   384 		}
       
   385 	}
       
   386 
       
   387 /**
       
   388 Access a single UUID.
       
   389 @param aIndex The index of the UUID to access.
       
   390 @return The UUID.  The reference remains valid as long as this object is in scope.
       
   391 */
       
   392 EXPORT_C const TUUID &RUUIDContainer::At(TInt aIndex) const
       
   393 	{
       
   394 	return iUUIDs[aIndex];
       
   395 	}
       
   396 
       
   397 /**
       
   398 Access a single UUID.
       
   399 @param aIndex The index of the UUID to access.
       
   400 @return The UUID.  The reference remains valid as long as this object is in scope.
       
   401 */
       
   402 EXPORT_C const TUUID &RUUIDContainer::operator[](TInt aIndex) const
       
   403 	{
       
   404 	return iUUIDs[aIndex];
       
   405 	}
       
   406 
       
   407 /**
       
   408 Access a single UUID.
       
   409 @param aIndex The index of the UUID to access.
       
   410 @return The UUID.  The reference remains valid as long as this object is in scope.
       
   411 */
       
   412 EXPORT_C TUUID &RUUIDContainer::operator[](TInt aIndex)
       
   413 	{
       
   414 	return iUUIDs[aIndex];
       
   415 	}
       
   416 
       
   417 EXPORT_C void RExtendedInquiryResponseUUIDContainer::Close()
       
   418 	{
       
   419 	iUUIDs.Close();
       
   420 	}
       
   421 
       
   422 /**
       
   423 Retrieve the UUID container class.
       
   424 @return RUUIDContainer, the UUID container class, which contains a set of UUIDs.
       
   425 */
       
   426 EXPORT_C RUUIDContainer& RExtendedInquiryResponseUUIDContainer::UUIDs()
       
   427 	{
       
   428 	return iUUIDs;
       
   429 	}
       
   430 
       
   431 /**
       
   432 Check if the Extended Inquiry Response has a complete set of UUIDs.
       
   433 @param aType a specifc type of UUID. It indicates which type of UUID the check will be carried upon.
       
   434 This could be 16 bit, 32 bit or 128 bit TUUID.
       
   435 @return the result of the check, ETrue means it's a complete set of UUIDs, otherwise EFalse.
       
   436 */
       
   437 EXPORT_C TBool RExtendedInquiryResponseUUIDContainer::GetCompleteness(TUUIDType aType) const
       
   438 	{
       
   439 	// Check the bit for this UUID type. If set,
       
   440 	// data is partial, else data is complete
       
   441 	TInt completeness = iCompleteness & aType;
       
   442 	if (completeness > 0)
       
   443 		{
       
   444 		return EFalse;
       
   445 		}
       
   446 	return ETrue;
       
   447 	}
       
   448 
       
   449 /**
       
   450 Set the completeness for a set of specific type UUID (16, 32 or 128 bit UUID) in Extended Inquiry Response.
       
   451 @param aType a specifc type of UUID. This could be 16 bit, 32 bit or 128 bit TUUID.
       
   452 @param aIsComplete ETrue means it's a complete set of UUIDs, otherwise EFalse.
       
   453 */
       
   454 EXPORT_C void RExtendedInquiryResponseUUIDContainer::SetCompleteness(TUUIDType aType, TBool aIsComplete)
       
   455 	{
       
   456 	TInt setcomp = 0;
       
   457 	if (aIsComplete == EFalse)
       
   458 		{
       
   459 		setcomp = aType;
       
   460 		}
       
   461 	// Set or clear the bit for this UUID type.
       
   462 	// If set, data is partial, else data is complete
       
   463 	iCompleteness &= ~(aType);
       
   464 	iCompleteness ^= setcomp;
       
   465 	}
       
   466 
       
   467 //EOF
       
   468