obex/obexprotocol/obextransport/src/ObexReaderBase.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 <obex/transport/obexreaderbase.h>
       
    17 #include <obex/internal/obexpacket.h>
       
    18 #include <obex/transport/mobextransportnotify.h>
       
    19 #include "logger.h"
       
    20 
       
    21 #ifdef __FLOG_ACTIVE
       
    22 _LIT8(KLogComponent, "OBEXCT");
       
    23 #endif
       
    24 
       
    25 /**
       
    26 *Constructor
       
    27 */
       
    28 EXPORT_C CObexReaderBase::CObexReaderBase(TPriority aPriority, 
       
    29 										  MObexTransportNotify& aOwner, 
       
    30 										  TObexConnectionInfo& aInfo)
       
    31 	: CObexActiveRW (aPriority, aOwner, aInfo)
       
    32 	{
       
    33 	LOG_LINE
       
    34 	LOG_FUNC
       
    35 	}
       
    36 
       
    37 /**
       
    38 * Destructor
       
    39 */
       
    40 EXPORT_C CObexReaderBase::~CObexReaderBase()
       
    41 	{
       
    42 	LOG_LINE
       
    43 	LOG_FUNC
       
    44 
       
    45 	Cancel();
       
    46 	}
       
    47 
       
    48 /**
       
    49 This function is a place holder for future use. If the iFuture1 variable is 
       
    50 used it will need this function for any allocation required.  
       
    51 
       
    52 To prevent binary compatiblity breaks if the iFuture1 variable is used, this 
       
    53 function must be called from the 
       
    54 NewL of derived classes.  
       
    55 */
       
    56 EXPORT_C void CObexReaderBase::BaseConstructL()
       
    57 	{
       
    58 	LOG_LINE
       
    59 	LOG_FUNC
       
    60 	}
       
    61 
       
    62 /**
       
    63 Start transfer. Calls into CObexActiveRW, which eventually queues a read 
       
    64 
       
    65 @param aPacket The Obex packet to read into.
       
    66 */
       
    67 EXPORT_C void CObexReaderBase::StartTransfer (CObexPacket& aPacket)
       
    68 	{
       
    69 	LOG_LINE
       
    70 	LOG_FUNC
       
    71 
       
    72 	// Ensure that we don't try to queue two reads at once (this should probably
       
    73 	// never happen anyway).
       
    74 	if (IsActive())
       
    75 		{
       
    76 		FLOG(_L("\treturning because already active..."));
       
    77 		return;
       
    78 		}
       
    79 	iGotHeader = EFalse;
       
    80 	// Initial packet size indicates how much data we should ask for on the 
       
    81 	// first read
       
    82 	// Packet & stream based transports do the same thing here due to ESOCK 
       
    83 	// flexibility.
       
    84 	// See Remaining() and CompleteTransfer() for a clearer picture
       
    85 	iPacketSize = 0;
       
    86 	aPacket.Init(0);
       
    87 	NewRequest (aPacket);
       
    88 	}
       
    89 
       
    90 /**
       
    91 Check if the packet read is complete 
       
    92 @return ETrue if the read is complete.  EFalse otherwise.
       
    93 */
       
    94 EXPORT_C TBool CObexReaderBase::CompleteTransfer ()
       
    95 	{
       
    96 	LOG_LINE
       
    97 	LOG_FUNC
       
    98 
       
    99 	iTransferError = KErrNone;
       
   100 
       
   101 	// Can't check anything until we have at least the OBEX packet header
       
   102 	if (iCount >= KObexPacketHeaderSize)
       
   103 		{// iCount is the number of bytes read thus far
       
   104 		if (!iGotHeader)
       
   105 			{
       
   106 			iPacketSize = iPacket->PacketSize ();
       
   107 
       
   108 			// Check packet's claimed size is at least as big as the header just sent
       
   109 			if ( iPacketSize < KObexPacketHeaderSize )
       
   110 				{
       
   111 				iTransferError = KErrCommsFrame;
       
   112 				return ETrue;
       
   113 				}
       
   114 				TInt maxPacketSize = GetMaxPacketSize();
       
   115 				LOG1(_L8("\taMaxPacketSize = %d"), maxPacketSize);
       
   116 			if (iPacketSize > maxPacketSize)
       
   117 				{// The peer is sending us a packet thats too large for us
       
   118 				iTransferError = KErrCommsFrame;
       
   119 				return (ETrue);
       
   120 				}
       
   121 			iGotHeader = ETrue;
       
   122 			OnReadActivity();
       
   123 			}
       
   124 
       
   125 
       
   126 		if (iCount >= iPacketSize)
       
   127 			{// We've got the whole packet.
       
   128 			return (ETrue);
       
   129 			}
       
   130 		}
       
   131 	return (EFalse);
       
   132 		
       
   133 	}
       
   134 
       
   135 /** 
       
   136 	Called when read activity is detected. 
       
   137 	This method will certainly be called when read activity is 
       
   138 	first detected on an Obex operation, and it may be called 
       
   139 	from time to time thereafter until that Obex operation is
       
   140 	completed.
       
   141 */
       
   142 EXPORT_C void CObexReaderBase::OnReadActivity()
       
   143 	{
       
   144 	iOwner.SignalPacketProcessEvent(EObexReadActivityDetected);
       
   145 	}
       
   146 
       
   147 /** Performs any actions necessary on completion of a read.
       
   148 */
       
   149 EXPORT_C void CObexReaderBase::OnCompleteTransfer()
       
   150 	{
       
   151 	if(iTransferError)
       
   152 		{
       
   153 		iOwner.Error(iTransferError);
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		iOwner.Process(*iPacket);
       
   158 		}
       
   159 	}
       
   160 
       
   161 /*
       
   162 Returns the number of bytes remaining to be read based on the default packet 
       
   163 size if the packet size is unknown, otherwise based on the packet size
       
   164 
       
   165 @return TInt the number of bytes remaining to be read
       
   166 */
       
   167 EXPORT_C TInt CObexReaderBase::Remaining()
       
   168 	{
       
   169 	LOG_LINE
       
   170 	LOG_FUNC
       
   171 	
       
   172 
       
   173 	TInt remaining;
       
   174 	
       
   175 	if (iPacketSize == 0)
       
   176 		{	
       
   177 		// If we don't know the size of the packet yet, ask for as much as 
       
   178 		// an initial or default size (-iCount) specifies
       
   179 		TInt defaultPacketSize = GetInitialPacketSize();
       
   180 		LOG1(_L8("\taRemainingLimit = %d"), defaultPacketSize);
       
   181 		remaining = defaultPacketSize - iCount;
       
   182 		}
       
   183 	else
       
   184 		{
       
   185 		remaining = iPacketSize - iCount;
       
   186 		}
       
   187 	
       
   188 	if (remaining < 0)
       
   189 		{
       
   190 		remaining = 0;
       
   191 		}
       
   192 	
       
   193 	return remaining;
       
   194 	}
       
   195 
       
   196 /**
       
   197 This function returns the buffer size of the read packet.  
       
   198 
       
   199 @return TInt the size of the buffer held by the iPacket member variable
       
   200 */
       
   201 EXPORT_C TInt CObexReaderBase::GetObexPacketBufferSize()
       
   202 	{
       
   203 	return iPacket->BufferSize();
       
   204 	}
       
   205 
       
   206 /**
       
   207 This function returns the size of the obex packet's header
       
   208 
       
   209 @return TInt the size of the obex header
       
   210 */
       
   211 EXPORT_C TInt CObexReaderBase::GetObexPacketHeaderSize()
       
   212 	{
       
   213 	return KObexPacketHeaderSize;
       
   214 	}
       
   215 
       
   216 /**
       
   217 This function return the maximum limit the received data can be
       
   218 
       
   219 @return TInt the data limit of the obex packet
       
   220 */
       
   221 EXPORT_C TInt CObexReaderBase::GetObexPacketDataLimit()
       
   222 	{
       
   223 	return iPacket->DataLimit();
       
   224 	}
       
   225 		
       
   226 /**
       
   227 This function is part of the extension pattern and is implemented by all 
       
   228 derived instantiable classes that need to extend it's interface.
       
   229 
       
   230 By default this returns null. Any derived class that is required to extend its 
       
   231 interface and that of this base class returns its new interface in the form of 
       
   232 an M class, that it extends, if and only if  the corresponding TUid, aUid, is 
       
   233 received. Otherwise the function calls the base class implementation, 
       
   234 returning NULL.
       
   235 
       
   236 @return TAny* The M Class representing the extension to the interface 
       
   237 otherwise NULL
       
   238 @param aUid The uid associated with the M Class that is being implemented
       
   239 */
       
   240 EXPORT_C TAny* CObexReaderBase::GetInterface(TUid /*aUid*/)
       
   241 	{
       
   242 	return NULL;
       
   243 	}