obex/obexprotocol/obex/src/obexheader.cpp
changeset 57 f6055a57ae18
parent 0 d0791faffa3f
equal deleted inserted replaced
54:4dc88a4ac6f4 57:f6055a57ae18
       
     1 // Copyright (c) 2003-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  @internalComponent
       
    19 */
       
    20 
       
    21 #include <obexheaders.h>
       
    22 #include <obexconstants.h>
       
    23 #include "obexunderlyingheader.h"
       
    24 #include "OBEXUTIL.H"
       
    25 #include "logger.h"
       
    26 
       
    27 #define BAD_HEADER_ACTION IrOBEXUtil::Panic(EHeaderAsBadType)
       
    28 
       
    29 #if ( defined __FLOG_ACTIVE && defined __LOG_FUNCTIONS__ )
       
    30 _LIT8(KLogComponent, "OBEX");
       
    31 #endif
       
    32 
       
    33 /**
       
    34 Standard constructor
       
    35 
       
    36 @internalComponent
       
    37  */
       
    38 CRefCounted::CRefCounted()
       
    39 	{
       
    40 	iCount = 0;
       
    41 	}
       
    42 	
       
    43 /**
       
    44 Increments the object's reference count.
       
    45 
       
    46 @internalComponent
       
    47 */
       
    48 void CRefCounted::Open() const
       
    49 	{
       
    50 	iCount++;
       
    51 	}
       
    52 
       
    53 /**
       
    54 Decrements the object's reference count and deletes the object when count goes to zero.
       
    55 
       
    56 @internalComponent
       
    57 */
       
    58 void CRefCounted::Close() const
       
    59 	{
       
    60 	iCount--;
       
    61 	if (iCount == 0) 
       
    62 		{
       
    63 		delete this;
       
    64 		}
       
    65 	}
       
    66 
       
    67 /**
       
    68 @panic ObexFault ERefCountNonZero Panics if there are still references to this object or if the reference count is negative.
       
    69 
       
    70 @internalComponent
       
    71 */
       
    72 CRefCounted::~CRefCounted()
       
    73 	{
       
    74 	__ASSERT_ALWAYS(iCount == 0, IrOBEXUtil::Fault(ERefCountNonZero));
       
    75 	}
       
    76 
       
    77 EXPORT_C CObexHeader* CObexHeader::NewL()
       
    78 	{
       
    79 	LOG_LINE
       
    80 	LOG_STATIC_FUNC_ENTRY
       
    81 
       
    82 	CObexHeader* self = new(ELeave) CObexHeader();
       
    83 	CleanupStack::PushL(self);
       
    84 	self->ConstructL();
       
    85 	CleanupStack::Pop(self);
       
    86 	return self;
       
    87 	}
       
    88 
       
    89 /**
       
    90 Return a pointer to a copy of this object.  This must be used instead
       
    91 of merely copying the pointer to the initial CObexHeader object as
       
    92 reference counting is used to keep track of instances of the header.
       
    93 The underlying data is not cloned, so any change to the data pointed to
       
    94 by this pointer could change the underlying data.
       
    95 
       
    96 @publishedAll
       
    97 @released
       
    98 */
       
    99 EXPORT_C CObexHeader* CObexHeader::CopyL() const
       
   100 	{
       
   101 	LOG_LINE
       
   102 	LOG_FUNC
       
   103 
       
   104 	CObexHeader* copy = new(ELeave) CObexHeader(iHeader);
       
   105 	copy->iHeader->Open();
       
   106 	return copy;
       
   107 	}
       
   108 
       
   109 /**
       
   110 @internalComponent
       
   111 Standard constructor.
       
   112 
       
   113 @see CObexHeaderSet
       
   114 */
       
   115 CObexHeader::CObexHeader()
       
   116 	{
       
   117 	}
       
   118 
       
   119 /**
       
   120 Standard constructor, taking a pointer to an underlying object.
       
   121 
       
   122 @internalComponent
       
   123 */
       
   124 CObexHeader::CObexHeader(CObexUnderlyingHeader* aHeader)
       
   125 	: iHeader(aHeader)
       
   126 	{
       
   127 	}
       
   128 	
       
   129 /**
       
   130 Sets this object to use the same underlying header as the parameter.
       
   131 This performs essentially the same function as the CopyL() function,
       
   132 but does not allocate a new object.
       
   133 The underlying data is not cloned, so any change to the data pointed to
       
   134 by this pointer could change the underlying data.
       
   135 
       
   136 @publishedAll
       
   137 @released
       
   138 */
       
   139 EXPORT_C void CObexHeader::Set(CObexHeader* aHeader)
       
   140 	{
       
   141 	LOG_LINE
       
   142 	LOG_FUNC
       
   143 
       
   144 	Reset();
       
   145 	iHeader = aHeader->iHeader;
       
   146 	iHeader->Open();
       
   147 	}
       
   148 
       
   149 /**
       
   150 Resets the contents of this header, discarding the underlying data.
       
   151 This is only called by the last instance of the header.
       
   152 
       
   153 @publishedAll
       
   154 @released
       
   155 */
       
   156 EXPORT_C void CObexHeader::Reset()
       
   157 	{
       
   158 	LOG_LINE
       
   159 	LOG_FUNC
       
   160 
       
   161 	iHeader->Close();
       
   162 	iHeader = 0;
       
   163 	}
       
   164 
       
   165 /**
       
   166 Phase two constructor.
       
   167 Create the underlying header object.  This is reference counted
       
   168 so ownership passes to itself.
       
   169 
       
   170 @internalComponent
       
   171 */
       
   172 void CObexHeader::ConstructL()
       
   173 	{
       
   174 	iHeader = CObexUnderlyingHeader::NewL();
       
   175 	iHeader->Open();
       
   176 	}
       
   177 
       
   178 /**
       
   179 Standard destructor.
       
   180 
       
   181 @publishedAll
       
   182 @released
       
   183 */
       
   184 CObexHeader::~CObexHeader()
       
   185 	{
       
   186 	if (iHeader != 0)
       
   187 		{
       
   188 		iHeader->Close();
       
   189 		}
       
   190 	}
       
   191 
       
   192 /**
       
   193 Resets and destroys all header attributes.
       
   194 
       
   195 @publishedAll
       
   196 @released
       
   197 */
       
   198 EXPORT_C void CObexHeader::ResetContents()
       
   199 	{
       
   200 	LOG_LINE
       
   201 	LOG_FUNC
       
   202 
       
   203 	iHeader->ResetContents();
       
   204 	}
       
   205 
       
   206 /**
       
   207 Sets the attibutes of this header.
       
   208 
       
   209 @publishedAll
       
   210 @released
       
   211 */
       
   212 EXPORT_C void CObexHeader::SetAttributes(TUint16 aAttr)
       
   213 	{
       
   214 	LOG_LINE
       
   215 	LOG_FUNC
       
   216 
       
   217 	iHeader->SetAttributes(aAttr);
       
   218 	}
       
   219 
       
   220 /**
       
   221 Return the attibute set currently in force.
       
   222 
       
   223 @publishedAll
       
   224 @released
       
   225 */
       
   226 EXPORT_C TUint16 CObexHeader::Attributes() const
       
   227 	{
       
   228 	LOG_LINE
       
   229 	LOG_FUNC
       
   230 
       
   231 	return iHeader->Attributes();
       
   232 	}
       
   233 
       
   234 /**
       
   235 Return the type of this header.
       
   236 
       
   237 @publishedAll
       
   238 @released
       
   239 */
       
   240 EXPORT_C CObexHeader::THeaderType CObexHeader::Type() const
       
   241 
       
   242 	{
       
   243 	LOG_LINE
       
   244 	LOG_FUNC
       
   245 
       
   246 	return iHeader->Type();
       
   247 	}
       
   248 
       
   249 /**
       
   250 Returns the header identifier.
       
   251 
       
   252 @publishedAll
       
   253 @released
       
   254 */
       
   255 EXPORT_C TUint8 CObexHeader::HI() const
       
   256 	{
       
   257 	// Don't log this function as it's too verbose.
       
   258 	//LOG_LINE
       
   259 	//LOG_FUNC
       
   260 
       
   261 	return iHeader->HI();
       
   262 	}
       
   263 
       
   264 /**
       
   265 Returns this header as a byte value.
       
   266 @return The value of this header.
       
   267 @panic Obex EHeaderAsBadType Panics builds if header is of incorrect type.
       
   268 
       
   269 @publishedAll
       
   270 @released
       
   271 */
       
   272 EXPORT_C TUint8 CObexHeader::AsByte() const
       
   273 	{
       
   274 	LOG_LINE
       
   275 	LOG_FUNC
       
   276 
       
   277 	return iHeader->AsByte();
       
   278 	}
       
   279 
       
   280 /**
       
   281 Returns this header as a four-byte value.
       
   282 @return The value of this header.
       
   283 @panic Obex EHeaderAsBadType Panics if header is of incorrect type.
       
   284 
       
   285 @publishedAll
       
   286 @released
       
   287 */
       
   288 EXPORT_C TUint32 CObexHeader::AsFourByte() const
       
   289 	{
       
   290 	LOG_LINE
       
   291 	LOG_FUNC
       
   292 
       
   293 	return iHeader->AsFourByte();
       
   294 	}
       
   295 
       
   296 /**
       
   297 Returns this header as a byte sequence.
       
   298 @return The value of this header.
       
   299 @panic Obex EHeaderAsBadType Panics if header is of incorrect type.
       
   300 
       
   301 @publishedAll
       
   302 @released
       
   303 */
       
   304 EXPORT_C const TDesC8& CObexHeader::AsByteSeq() const
       
   305 	{
       
   306 	LOG_LINE
       
   307 	LOG_FUNC
       
   308 
       
   309 	return iHeader->AsByteSeq();
       
   310 	}
       
   311 
       
   312 /**
       
   313 Returns this header as an Unicode string.
       
   314 @return The value of this header.
       
   315 @panic Obex EHeaderAsBadType Panics in debug builds if header is of incorrect type.
       
   316 
       
   317 @publishedAll
       
   318 @released
       
   319 */
       
   320 EXPORT_C const TDesC16& CObexHeader::AsUnicode() const
       
   321 	{
       
   322 	LOG_LINE
       
   323 	LOG_FUNC
       
   324 
       
   325 	return iHeader->AsUnicode();
       
   326 	}
       
   327 
       
   328 /**
       
   329 Sets this header to represent a byte value.
       
   330 Forces header type to be a byte (which may therefore change the HI
       
   331 value).
       
   332 
       
   333 @publishedAll
       
   334 @released
       
   335 */
       
   336 EXPORT_C void CObexHeader::SetByte(const TUint8 aHI, const TUint8 aByte)
       
   337 	{
       
   338 	LOG_LINE
       
   339 	LOG_FUNC
       
   340 
       
   341 	iHeader->SetByte(aHI, aByte);
       
   342 	}
       
   343 	
       
   344 /**
       
   345 Sets this header to represent a four-byte value.
       
   346 Forces header type to be a four-byte (which may therefore change
       
   347 the HI value).
       
   348 
       
   349 @publishedAll
       
   350 @released
       
   351 */
       
   352 EXPORT_C void CObexHeader::SetFourByte(const TUint8 aHI, const TUint32 aFourByte)
       
   353 	{
       
   354 	LOG_LINE
       
   355 	LOG_FUNC
       
   356 
       
   357 	iHeader->SetFourByte(aHI, aFourByte);
       
   358 	}
       
   359 	
       
   360 /**
       
   361 Sets this header to represent a byte sequence.
       
   362 Forces header type to be a byte sequence (which may therefore change
       
   363 the HI value).
       
   364 
       
   365 @publishedAll
       
   366 @released
       
   367 */
       
   368 EXPORT_C void CObexHeader::SetByteSeqL(const TUint8 aHI, const TDesC8& aByteSeq)
       
   369 	{
       
   370 	LOG_LINE
       
   371 	LOG_FUNC
       
   372 
       
   373 	iHeader->SetByteSeqL(aHI, aByteSeq);
       
   374 	}
       
   375 
       
   376 /**
       
   377 Forces header type to be a byte (which may therefore change the HI
       
   378 value).
       
   379 
       
   380 @publishedAll
       
   381 @released
       
   382 */
       
   383 EXPORT_C void CObexHeader::SetUnicodeL(const TUint8 aHI, const TDesC16& aUnicode)
       
   384 	{
       
   385 	LOG_LINE
       
   386 	LOG_FUNC
       
   387 
       
   388 	iHeader->SetUnicodeL(aHI, aUnicode);
       
   389 	}
       
   390 
       
   391 /**
       
   392 @return The encoded size of this header in bytes, including the 
       
   393 HI byte and the HV body.
       
   394 
       
   395 @publishedAll
       
   396 @released
       
   397 */
       
   398 EXPORT_C TInt CObexHeader::EncodedSize() const
       
   399 	{
       
   400 	LOG_LINE
       
   401 	LOG_FUNC
       
   402 
       
   403 	return iHeader->EncodedSize();
       
   404 	}
       
   405 
       
   406 // ----------------------------------------------------------------------
       
   407 // Now the real code.  CObexHeader is just a facade, passing requests on
       
   408 // to an underlying reference counted object.  This is where the real
       
   409 // work goes on.
       
   410 // ----------------------------------------------------------------------
       
   411 
       
   412 /**
       
   413 @internalComponent
       
   414 NewL method
       
   415 */
       
   416 CObexUnderlyingHeader* CObexUnderlyingHeader::NewL()
       
   417 	{
       
   418 	CObexUnderlyingHeader* self = new(ELeave) CObexUnderlyingHeader();
       
   419 	return self;
       
   420 	}
       
   421 	
       
   422 /**
       
   423 @internalComponent
       
   424 Standard constructor.
       
   425 */
       
   426 CObexUnderlyingHeader::CObexUnderlyingHeader()
       
   427 	{
       
   428 	}
       
   429 
       
   430 /**
       
   431 @internalComponent
       
   432 Standard destructor.
       
   433 */
       
   434 CObexUnderlyingHeader::~CObexUnderlyingHeader()
       
   435 	{
       
   436 	ResetContents();
       
   437 	}
       
   438 
       
   439 /**
       
   440 @internalComponent
       
   441 Resets and destroys all header attributes.
       
   442 */
       
   443 void CObexUnderlyingHeader::ResetContents()
       
   444 	{
       
   445 	// If a byte sequence or Unicode string header, destroy
       
   446 	// the underlying HBufC.
       
   447 	switch (Type())
       
   448 		{
       
   449 		case CObexHeader::EByteSeq:
       
   450 			delete iHV.iByteSeq;
       
   451 			break;
       
   452 		
       
   453 		case CObexHeader::EUnicode:
       
   454 			delete iHV.iUnicode;
       
   455 			break;
       
   456 		
       
   457 		default:
       
   458 			// Not a pointer, nothing required
       
   459 			{}
       
   460 		}
       
   461 
       
   462 	iAttributes = 0;
       
   463 	iHI = 0;
       
   464 	// Resetting the fourbyte data, will explicitly reset all the data
       
   465 	// stored in the union (so no need to reset unicode, byte, byteseq)
       
   466 	iHV.iFourByte = 0;
       
   467 	}
       
   468 
       
   469 /**
       
   470 @internalComponent
       
   471 Sets the attibutes of this header.
       
   472 */
       
   473 void CObexUnderlyingHeader::SetAttributes(TUint16 aAttr)
       
   474 	{
       
   475 	iAttributes = aAttr;
       
   476 	}
       
   477 
       
   478 /**
       
   479 @internalComponent
       
   480 Return the attibute set currently in force.
       
   481 */
       
   482 TUint16 CObexUnderlyingHeader::Attributes() const
       
   483 	{
       
   484 	return iAttributes;
       
   485 	}
       
   486 
       
   487 /**
       
   488 @internalComponent
       
   489 Return the type of this header.
       
   490 */
       
   491 CObexHeader::THeaderType CObexUnderlyingHeader::Type() const
       
   492 	{
       
   493 	// Call the internal inline.  Just in case the header representation
       
   494 	// changes!
       
   495 	return static_cast<CObexHeader::THeaderType>(ObexHeaderType(iHI));
       
   496 	}
       
   497 
       
   498 /**
       
   499 @internalComponent
       
   500 Returns the header identifier.
       
   501 */
       
   502 TUint8 CObexUnderlyingHeader::HI() const
       
   503 	{
       
   504 	return iHI;
       
   505 	}
       
   506 
       
   507 /**
       
   508 @internalComponent
       
   509 Returns this header as a byte value.
       
   510 @return The value of this header.
       
   511 @leave KErrNotSupported (release builds only) if this header is of
       
   512 incorrect type.
       
   513 @panic Obex EHeaderAsBadType Panics in debug builds if header is of incorrect type.
       
   514 */
       
   515 TUint8 CObexUnderlyingHeader::AsByte() const
       
   516 	{
       
   517 	__ASSERT_ALWAYS((ObexHeaderType(iHI) == CObexHeader::EByte), BAD_HEADER_ACTION);
       
   518 	
       
   519 	return iHV.iByte;
       
   520 	}
       
   521 
       
   522 /**
       
   523 @internalComponent
       
   524 Returns this header as a four-byte value.
       
   525 @return The value of this header.
       
   526 @leave KErrNotSupported (release builds only) if this header is of
       
   527 incorrect type.
       
   528 @panic Obex EHeaderAsBadType Panics in debug builds if header is of incorrect type.
       
   529 */
       
   530 TUint32 CObexUnderlyingHeader::AsFourByte() const
       
   531 	{
       
   532 	__ASSERT_ALWAYS((ObexHeaderType(iHI) == CObexHeader::EFourByte), BAD_HEADER_ACTION);
       
   533 	
       
   534 	return iHV.iFourByte;
       
   535 	}
       
   536 
       
   537 /**
       
   538 @internalComponent
       
   539 Returns this header as a byte sequence.
       
   540 @return The value of this header.
       
   541 @leave KErrNotSupported (release builds only) if this header is of
       
   542 incorrect type.
       
   543 @panic Obex EHeaderAsBadType Panics in debug builds if header is of incorrect type.
       
   544 */
       
   545 const TDesC8& CObexUnderlyingHeader::AsByteSeq() const
       
   546 	{
       
   547 	__ASSERT_ALWAYS((ObexHeaderType(iHI) == CObexHeader::EByteSeq), BAD_HEADER_ACTION);
       
   548 	
       
   549 	if (iHV.iByteSeq)
       
   550 		return *(iHV.iByteSeq);
       
   551 	else
       
   552 		return KNullDesC8();
       
   553 	}
       
   554 
       
   555 /**
       
   556 @internalComponent
       
   557 Returns this header as an Unicode string.
       
   558 @return The value of this header.
       
   559 @leave KErrNotSupported (release builds only) if this header is of
       
   560 incorrect type.
       
   561 @panic Obex EHeaderAsBadType Panics in debug builds if header is of incorrect type.
       
   562 */
       
   563 const TDesC16& CObexUnderlyingHeader::AsUnicode() const
       
   564 	{
       
   565 	__ASSERT_ALWAYS((ObexHeaderType(iHI) == CObexHeader::EUnicode), BAD_HEADER_ACTION);
       
   566 	
       
   567 	if (iHV.iUnicode)
       
   568 		return *(iHV.iUnicode);
       
   569 	else
       
   570 		return KNullDesC16();
       
   571 	}
       
   572 
       
   573 /**
       
   574 @internalComponent
       
   575 Sets this header to represent a byte value.
       
   576 Forces header type to be a byte (which may therefore change the HI
       
   577 value).
       
   578 */
       
   579 void CObexUnderlyingHeader::SetByte(const TUint8 aHI, const TUint8 aByte)
       
   580 	{
       
   581 	__ASSERT_ALWAYS((ObexHeaderType(aHI) == CObexHeader::EByte), BAD_HEADER_ACTION);
       
   582 
       
   583 	if ((iHI & KObexHeaderTypeMask) != (aHI & KObexHeaderTypeMask))
       
   584 		ResetContents();
       
   585 	
       
   586 	iHI = aHI;
       
   587 	iHV.iByte = aByte;
       
   588 	}
       
   589 	
       
   590 /**
       
   591 @internalComponent
       
   592 Sets this header to represent a four-byte value.
       
   593 Forces header type to be a four-byte (which may therefore change
       
   594 the HI value).
       
   595 */
       
   596 void CObexUnderlyingHeader::SetFourByte(const TUint8 aHI, const TUint32 aFourByte)
       
   597 	{
       
   598 	__ASSERT_ALWAYS((ObexHeaderType(aHI) == CObexHeader::EFourByte), BAD_HEADER_ACTION);
       
   599 
       
   600 	if ((iHI & KObexHeaderTypeMask) != (aHI & KObexHeaderTypeMask))
       
   601 		ResetContents();
       
   602 
       
   603 	iHI = aHI;
       
   604 	iHV.iFourByte = aFourByte;
       
   605 	}
       
   606 	
       
   607 /**
       
   608 @internalComponent
       
   609 Sets this header to represent a byte sequence.
       
   610 Forces header type to be a byte sequence (which may therefore change
       
   611 the HI value).
       
   612 */
       
   613 void CObexUnderlyingHeader::SetByteSeqL(const TUint8 aHI, const TDesC8& aByteSeq)
       
   614 	{
       
   615 	__ASSERT_ALWAYS((ObexHeaderType(aHI) == CObexHeader::EByteSeq), BAD_HEADER_ACTION);
       
   616 
       
   617 	HBufC8* tmp = aByteSeq.AllocL();
       
   618 	if ((iHI & KObexHeaderTypeMask) != (aHI & KObexHeaderTypeMask))
       
   619 		ResetContents();
       
   620 
       
   621 	iHI = aHI;
       
   622 	delete iHV.iByteSeq;
       
   623 	iHV.iByteSeq = tmp;
       
   624 	}
       
   625 
       
   626 /**
       
   627 @internalComponent
       
   628 Sets this header to represent a byte value.
       
   629 Forces header type to be a byte (which may therefore change the HI
       
   630 value).
       
   631 */
       
   632 void CObexUnderlyingHeader::SetUnicodeL(const TUint8 aHI, const TDesC16& aUnicode)
       
   633 	{
       
   634 	__ASSERT_ALWAYS((ObexHeaderType(aHI) == CObexHeader::EUnicode), BAD_HEADER_ACTION);
       
   635 
       
   636 	HBufC16* tmp = aUnicode.AllocL();
       
   637 	if ((iHI & KObexHeaderTypeMask) != (aHI & KObexHeaderTypeMask))
       
   638 		ResetContents();
       
   639 
       
   640 	iHI = aHI;
       
   641 	delete iHV.iUnicode;
       
   642 	iHV.iUnicode = tmp;
       
   643 	}
       
   644 
       
   645 TInt CObexUnderlyingHeader::EncodedSize() const
       
   646 	{
       
   647 	const TInt KHIByteSize=1;
       
   648 	const TInt KLengthEncoding=2;
       
   649 	const TInt KOneByteHeaderEncodedSize=1+KHIByteSize;
       
   650 	const TInt KFourByteHeaderEncodedSize=4+KHIByteSize;
       
   651 
       
   652 	TInt encodedSize=0;
       
   653 
       
   654 	switch(Type())
       
   655 		{
       
   656 
       
   657 	case CObexHeader::EUnicode:
       
   658 		encodedSize=KHIByteSize+KLengthEncoding+AsUnicode().Size();
       
   659 		if(AsUnicode().Size()>0)
       
   660 			encodedSize+=2; //	Two extra byes for null terminator
       
   661 		break;
       
   662 		
       
   663 	case CObexHeader::EByteSeq:
       
   664 		encodedSize=KHIByteSize+KLengthEncoding+AsByteSeq().Size();
       
   665 		break;
       
   666 
       
   667 	case CObexHeader::EByte:
       
   668 		encodedSize=KOneByteHeaderEncodedSize;
       
   669 		break;
       
   670 
       
   671 	case CObexHeader::EFourByte:
       
   672 		encodedSize=KFourByteHeaderEncodedSize;
       
   673 		break;
       
   674 
       
   675 		}
       
   676 		
       
   677 	return encodedSize;
       
   678 	}