obex/obexprotocol/obextransport/src/obexpacket.cpp
changeset 57 f6055a57ae18
parent 0 d0791faffa3f
equal deleted inserted replaced
54:4dc88a4ac6f4 57:f6055a57ae18
       
     1 // Copyright (c) 2005-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 <obex/internal/obexpacket.h>
       
    18 #include <obex/internal/obexinternalconstants.h>
       
    19 #include <obex/internal/obexdata.h>
       
    20 #include "logger.h"
       
    21 #include "obextransportfaults.h"
       
    22 
       
    23 #ifdef __FLOG_ACTIVE
       
    24 _LIT8(KLogComponent, "OBEXCT");
       
    25 #endif
       
    26 
       
    27 _LIT(KPanicCat, "ObexPacket");
       
    28 CObexPacket::CObexPacket (TUint16 aBufferSize, TUint16 aDataLimit) : iBufferSize ( aBufferSize ), iDataLimit ( aDataLimit )
       
    29 	{
       
    30 	LOG_FUNC
       
    31  	}
       
    32 
       
    33 /**
       
    34 Destructor
       
    35 */
       
    36 EXPORT_C CObexPacket::~CObexPacket ()
       
    37 	{
       
    38 	LOG_LINE
       
    39 	LOG_FUNC
       
    40 
       
    41 	delete[] iBuffer;
       
    42 	}
       
    43 
       
    44 /**
       
    45 */
       
    46 EXPORT_C CObexPacket* CObexPacket::NewL (TUint16 aBufferSize, TUint16 aDataLimit)
       
    47 	{
       
    48 	LOG_LINE
       
    49 	LOG_STATIC_FUNC_ENTRY
       
    50 	LOG2(_L8("\taBufferSize = %d, aDataLimit = %d"), aBufferSize, aDataLimit);
       
    51 
       
    52 	// Data limit can't be larger than the buffer
       
    53 	__ASSERT_ALWAYS ( aBufferSize >= aDataLimit, PANIC(KPanicCat, EDataLimitLargerThanBuffer) );
       
    54 
       
    55 	CObexPacket* self = new (ELeave) CObexPacket ( aBufferSize, aDataLimit );
       
    56 	CleanupStack::PushL (self);
       
    57 	self->ConstructL ();
       
    58 	CleanupStack::Pop (self);
       
    59 	return (self);
       
    60 	}
       
    61 
       
    62 void CObexPacket::ConstructL ()
       
    63 	{
       
    64 //	FTRACE(FPrint(_L("CObexPacket::ConstructL buffer %d data limit %d"), iBufferSize, iDataLimit));
       
    65 
       
    66 	iBuffer = new (ELeave) TUint8 [ iBufferSize ];
       
    67 	Init ( 0 );
       
    68 	}
       
    69 /**
       
    70 Set up as a fresh packet with the given opcode.
       
    71 @param aOpcode a opcode
       
    72 */
       
    73 EXPORT_C void CObexPacket::Init (TObexOpcode aOpcode)
       
    74 	{
       
    75 	LOG_LINE
       
    76 	LOG_FUNC
       
    77 
       
    78 	SetOpcode (aOpcode);
       
    79 	SetPacketSize (KObexPacketHeaderSize);
       
    80 	iInsertPoint = Payload ();
       
    81 	iExtractPoint = Payload ();
       
    82 	iNotificationEvents = 0;
       
    83 	}
       
    84 	
       
    85 /**
       
    86 Returns the packet opcode, with the final bit cleared (regardless of its actual value) 
       
    87 @return TObexOpcode
       
    88 */
       
    89 EXPORT_C TObexOpcode CObexPacket::Opcode () const
       
    90 	{
       
    91 	LOG_LINE
       
    92 	LOG_FUNC
       
    93 
       
    94 	return (STATIC_CAST(TObexOpcode, iBuffer[0] & ~KObexPacketFinalBit));
       
    95 	}
       
    96 
       
    97 /**
       
    98 Set the packet opcode to the passed value -- final bit will get through too.
       
    99 @param aOpcode an Opcode
       
   100 */
       
   101 EXPORT_C void CObexPacket::SetOpcode (TObexOpcode aOpcode)
       
   102 	{
       
   103 	LOG_LINE
       
   104 	LOG_FUNC
       
   105 
       
   106 	iBuffer[0] = aOpcode;
       
   107 	}
       
   108 
       
   109 /**
       
   110 True if the packet's final bit is set 
       
   111 @return TBool true if this is the final packet
       
   112 */
       
   113 EXPORT_C TBool CObexPacket::IsFinal () const
       
   114 	{
       
   115 	LOG_LINE
       
   116 
       
   117 	const TBool isFinal = (iBuffer[0] & KObexPacketFinalBit);
       
   118 
       
   119 	LOG1(_L8("CObexPacket::IsFinal returning %d"), isFinal);
       
   120 
       
   121 	return isFinal;
       
   122 	}
       
   123 
       
   124 /**
       
   125 If aFinal == ETrue (default), the final bit is set, otherwise it is cleared.
       
   126 @param aFinal whether or not to set or clear the final bit
       
   127 */
       
   128 EXPORT_C void CObexPacket::SetFinal (TBool aFinal)
       
   129 	{
       
   130 	LOG_LINE
       
   131 	LOG_FUNC
       
   132 
       
   133 	iBuffer[0] = STATIC_CAST(TObexOpcode, aFinal ? (iBuffer[0] | KObexPacketFinalBit) : (iBuffer[0] & ~KObexPacketFinalBit));
       
   134 	}
       
   135 
       
   136 /**
       
   137 Returns the total size of the current packet.
       
   138 @return TUint16 total size of the packet
       
   139 */
       
   140 EXPORT_C TUint16 CObexPacket::PacketSize () const
       
   141 	{
       
   142 	LOG_LINE
       
   143 	LOG_FUNC
       
   144 
       
   145 	return (BigEndian::Get16 (&iBuffer[1]));
       
   146 	}
       
   147 
       
   148 /**
       
   149 Sets the crruent packet's size.
       
   150 @param aSize a packet size
       
   151 */
       
   152 EXPORT_C void CObexPacket::SetPacketSize (TUint16 aSize)
       
   153 	{
       
   154 	LOG_LINE
       
   155 	LOG_FUNC
       
   156 
       
   157 	BigEndian::Put16 (&iBuffer[1], aSize);
       
   158 	}
       
   159 
       
   160 /**
       
   161 Sets the data limit of the buffer, ensuring it's larger than minimum possible but not larger than the buffer
       
   162 Parameter passed in is the requested new data limit
       
   163 Returns size that was set
       
   164 @param aRequestedSize a Requested size for the data limit
       
   165 @return TUint16  the data limit size
       
   166 */
       
   167 EXPORT_C TUint16 CObexPacket::SetLegalDataLimit (TUint16 aRequestedSize)
       
   168 	{
       
   169 	LOG_LINE
       
   170 	LOG_FUNC
       
   171 
       
   172 //	FTRACE(FPrint(_L("CObexPacket::SetLegalDataLimit requested size %d"), aRequestedSize));
       
   173 
       
   174 	aRequestedSize = Max ( KObexPacketMinSize, aRequestedSize );
       
   175 	aRequestedSize = Min ( iBufferSize, aRequestedSize );
       
   176 
       
   177 //	FTRACE(FPrint(_L("CObexPacket::SetLegalDataLimit set size %d"), aRequestedSize));
       
   178 	iDataLimit = aRequestedSize;
       
   179 	return iDataLimit;
       
   180 	}
       
   181 	
       
   182 /**	
       
   183 return the data limit
       
   184 @return TUint16 the data limit
       
   185 */
       
   186 EXPORT_C TUint16 CObexPacket::DataLimit () const	
       
   187 	{ 
       
   188 	LOG_LINE
       
   189 	LOG_FUNC
       
   190 
       
   191 	return iDataLimit; 
       
   192 	}
       
   193 	
       
   194 /**
       
   195 return the buffer size	
       
   196 @return TUint16 the buffer size
       
   197 */
       
   198 EXPORT_C TUint16 CObexPacket::BufferSize() const
       
   199   	{
       
   200 	LOG_LINE
       
   201 	LOG_FUNC
       
   202 
       
   203    	return iBufferSize; 
       
   204   	}
       
   205   
       
   206 /**
       
   207 Insert the passed packet data object at iInsertPoint 
       
   208 @param aHeader OBEX header to insert
       
   209 @return True if the number of bytes is not zero and there is enough space to insert data
       
   210 */
       
   211 EXPORT_C TBool CObexPacket::InsertData (const TObexData& aHeader)
       
   212 	{
       
   213 	LOG_LINE
       
   214 	LOG_FUNC
       
   215 
       
   216 	if (aHeader.TotalSize () > RemainingInsertSpace ()) return EFalse;
       
   217 	TUint16 bytesInserted = aHeader.WriteOut (iInsertPoint, RemainingInsertSpace ());
       
   218 	iInsertPoint += bytesInserted;
       
   219 	SetPacketSize (STATIC_CAST(TUint16, PacketSize () + bytesInserted));
       
   220 	return (bytesInserted != 0);
       
   221 	}
       
   222 
       
   223 /**
       
   224 Extract into the passed data object from iExtractPoint 
       
   225 @param aHeader Container for extracted data
       
   226 @return True if bytes read is not zero
       
   227 */
       
   228 EXPORT_C TBool CObexPacket::ExtractData (TObexData& aHeader)
       
   229 	{
       
   230 	LOG_LINE
       
   231 	LOG_FUNC
       
   232 
       
   233 	TUint16 bytesRead = aHeader.ParseIn (iExtractPoint, RemainingExtractSpace ());
       
   234 	iExtractPoint += bytesRead;
       
   235 	
       
   236 	// If no bytes have been read two possible conditions have occured
       
   237 	// 1. There is no remaining extract space in the packet (i.e. at the end)
       
   238 	// 2. There has been an error when parsing.
       
   239 	// In either case iExtractPoint will not move and aHeader has not really
       
   240 	// changed its state (iHI, iHV, iHVSize and iHVRep will change but get
       
   241 	// reset on the next ParseIn.  So if no bytes are read then from the API we
       
   242 	// assume that we have finished processing the packet, and so can reset the
       
   243 	// extract point, in case it needs to be parsed again.
       
   244 	if (bytesRead == 0)
       
   245 		{
       
   246 		iExtractPoint = Payload();
       
   247 		}
       
   248 	
       
   249 	return (bytesRead != 0);
       
   250 	}
       
   251 
       
   252 /**
       
   253 Returns the number of bytes of unused space in the obex packet (on writes)
       
   254 @return Remaining insert space
       
   255 */
       
   256 EXPORT_C TUint16 CObexPacket::RemainingInsertSpace () const
       
   257 	{
       
   258 	LOG_LINE
       
   259 	LOG_FUNC
       
   260 
       
   261 	__ASSERT_DEBUG (iInsertPoint >= Payload () && iInsertPoint <= &iBuffer[iDataLimit], PANIC(KPanicCat, EPacketOverrun));
       
   262 	return (TUint16)((&iBuffer[iDataLimit]) - iInsertPoint);
       
   263 	}
       
   264 
       
   265 /**
       
   266 Returns the number of bytes of unread data in the obex packet (on reads) 
       
   267 @return Remaining extract space
       
   268 */
       
   269 EXPORT_C TUint16 CObexPacket::RemainingExtractSpace () const
       
   270 	{
       
   271 	LOG_LINE
       
   272 	LOG_FUNC
       
   273 
       
   274 	__ASSERT_DEBUG (iExtractPoint >= Payload () && iExtractPoint <= &iBuffer[PacketSize ()], PANIC (KPanicCat, EPacketOverrun));
       
   275 	return (TUint16)((&iBuffer[PacketSize ()] - iExtractPoint));
       
   276 	}
       
   277 
       
   278 /** Adds events that should be notified.
       
   279 
       
   280 Add events into the packet process events mask.  When this packet
       
   281 is processed this mask will be checked and notifications will be
       
   282 issued only if the appropriate bits are set.
       
   283 
       
   284 @param aEvents The additional events to notify on.
       
   285 */
       
   286 EXPORT_C void CObexPacket::AddPacketProcessEvents(TObexPacketProcessEvents aEvents)
       
   287 	{
       
   288 	LOG_LINE
       
   289 	LOG_FUNC
       
   290 	iNotificationEvents |= aEvents;
       
   291 	}
       
   292 	
       
   293 /** Removes events from packet notification.
       
   294 
       
   295 Remove events from the packet process events mask.  When this packet
       
   296 is processed this mask will be checked and notifications will be
       
   297 issued only if the appropriate bits are set.
       
   298 
       
   299 @param aEvents The events to cease notification of.
       
   300 */
       
   301 EXPORT_C void CObexPacket::RemovePacketProcessEvents(TObexPacketProcessEvents aEvents)
       
   302 	{
       
   303 	LOG_LINE
       
   304 	LOG_FUNC
       
   305 	iNotificationEvents &= ~aEvents;
       
   306 	}
       
   307 
       
   308 /** Gets events that will be notified.
       
   309 
       
   310 Retrieves the packet process events mask.  When this packet
       
   311 is processed this mask will be checked and notifications will be
       
   312 issued only if the appropriate bits are set.
       
   313 
       
   314 @return The events that will be notified.
       
   315 */
       
   316 EXPORT_C TObexPacketProcessEvents CObexPacket::PacketProcessNotificationEvents() const
       
   317 	{
       
   318 	LOG_LINE
       
   319 	LOG_FUNC
       
   320 	return iNotificationEvents;
       
   321 	}
       
   322 
       
   323 /**
       
   324 Logs the size of the packet. 
       
   325 If __LOG_PACKET_DUMP__ is defined (obex/common/logger.h) then also logs the 
       
   326 packet's contents.
       
   327 */
       
   328 EXPORT_C void CObexPacket::Dump () const
       
   329 	{
       
   330 	LOG1(_L8("PacketSize = %d, Packet Dump:"), PacketSize ());
       
   331 #ifdef __LOG_PACKET_DUMP__
       
   332 	LOGHEXRAW(iBuffer, iBufferSize);
       
   333 #endif
       
   334 	}