mtptransports/mtpptpiptransport/ptpipplugin/src/cptpipconnection.cpp
changeset 0 d0791faffa3f
child 1 f8e15b44d440
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     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  @internalComponent
       
    18 */
       
    19 
       
    20 // System includes
       
    21 #include "e32base.h"
       
    22 #include "ptpippanic.h"
       
    23 #include "e32property.h" 
       
    24 
       
    25 // MTP includes
       
    26 #include <mtp/mtpprotocolconstants.h>
       
    27 #include <mtp/tmtptyperesponse.h>
       
    28 
       
    29 // Plugin includes
       
    30 #include "cptpipcommandhandler.h"
       
    31 #include "cptpipeventhandler.h"
       
    32 #include "cptpipconnection.h"
       
    33 #include "ptpipsocketpublish.h" 
       
    34 
       
    35 // File type constants.
       
    36 const TInt KMTPNullChunkSize(0x00020000); // 100KB
       
    37 const TUint32 KPTPIPDataHeaderSize = 12;  // Size of len, type and tran id. 
       
    38 __FLOG_STMT(_LIT8(KComponent,"PTPIPConnection");)
       
    39 #define UNUSED_VAR(a) (a) = (a)
       
    40 
       
    41 /**
       
    42  PTPIP device class connection factory method.
       
    43  @param aConnectionMgr The MTP connection manager interface.
       
    44  @return A pointer to an PTPIP device class connection. Ownership IS transfered.
       
    45  @leave One of the system wide error codes, if a processing failure occurs.
       
    46  */
       
    47 CPTPIPConnection* CPTPIPConnection::NewL(MMTPConnectionMgr& aConnectionMgr )
       
    48 	{
       
    49 	CPTPIPConnection* self = new(ELeave) CPTPIPConnection(aConnectionMgr);
       
    50 	CleanupStack::PushL (self );
       
    51 	self->ConstructL ( );
       
    52 	CleanupStack::Pop (self );
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 /**
       
    57  Destructor
       
    58  */
       
    59 CPTPIPConnection::~CPTPIPConnection( )
       
    60 	{
       
    61 	__FLOG(_L8("Destructor - Entry"));
       
    62 	StopConnection ( );
       
    63 
       
    64 	// Delete all the handlers which will close the sockets.
       
    65 	delete iCommandHandler;
       
    66 	delete iEventHandler;
       
    67 
       
    68 	delete iPTPIPCommandContainer;
       
    69 	delete iPTPIPDataContainer;
       
    70 	delete iPTPIPEventContainer;
       
    71 	
       
    72 	iNullBuffer.Close();
       
    73 
       
    74 	__FLOG(_L8("Destructor - Exit"));
       
    75 	__FLOG_CLOSE;
       
    76 	}
       
    77 
       
    78 /**
       
    79  Second phase constructor
       
    80  @leave One of the system wide error codes, if a processing failure occurs.
       
    81  */
       
    82 void CPTPIPConnection::ConstructL( )
       
    83 	{
       
    84 	__FLOG_OPEN(KMTPSubsystem, KComponent);
       
    85 	__FLOG(_L8("ConstructL - Entry"));
       
    86 
       
    87 	// Construct the Command and event handlers
       
    88 	iCommandHandler = CPTPIPCommandHandler::NewL (*this );
       
    89 	iEventHandler = CPTPIPEventHandler::NewL (*this );
       
    90 
       
    91 	iPTPIPCommandContainer = CPTPIPGenericContainer::NewL ( );
       
    92 	iPTPIPDataContainer = CPTPIPDataContainer::NewL ( );
       
    93 	iPTPIPEventContainer = CPTPIPGenericContainer::NewL ( );
       
    94 
       
    95 	// Transfer the sockets
       
    96 	TransferSocketsL();
       
    97 
       
    98 	SetConnectionState (EInitialising );
       
    99 	CompleteSelf (KErrNone );
       
   100 
       
   101 	__FLOG(_L8("ConstructL - Exit"));
       
   102 	}
       
   103 
       
   104 /**
       
   105  Constructor
       
   106  */
       
   107 CPTPIPConnection::CPTPIPConnection(MMTPConnectionMgr& aConnectionMgr ) :
       
   108 									CActive(EPriorityStandard), 
       
   109 									iConnectionMgr(&aConnectionMgr)
       
   110 	{
       
   111 	CActiveScheduler::Add (this );
       
   112 	}
       
   113 
       
   114 //
       
   115 // MMTPTransportConnection functions
       
   116 //
       
   117 
       
   118 /**
       
   119  Binds the protocol layer. 
       
   120  @param aProtocol - The connectionProtocol provides the SPI or the observer
       
   121  for the communication to happen from the transport to the framework. 
       
   122  */
       
   123 void CPTPIPConnection::BindL(MMTPConnectionProtocol& aProtocol )
       
   124 	{
       
   125 	__FLOG(_L8("BindL - Entry"));
       
   126 	iProtocolLayer = &aProtocol;
       
   127 	__FLOG(_L8("BindL - Exit"));
       
   128 	}
       
   129 
       
   130 /**
       
   131  Returns the protocol layer.
       
   132  @return The SPI or the observer protocol layer
       
   133  */
       
   134 MMTPConnectionProtocol& CPTPIPConnection::BoundProtocolLayer( )
       
   135 	{
       
   136 	__FLOG(_L8("BoundProtocolLayer - Entry"));
       
   137 	__ASSERT_ALWAYS(iProtocolLayer, Panic(EPTPIPBadState));
       
   138 	__FLOG(_L8("BoundProtocolLayer - Exit"));
       
   139 	return *iProtocolLayer;
       
   140 	}
       
   141 
       
   142 /**
       
   143  Close the connection, stop all data transfer activity and and wait for the host 
       
   144  to issue a Device Reset Request.
       
   145  */
       
   146 void CPTPIPConnection::CloseConnection( )
       
   147 	{
       
   148 	__FLOG(_L8("CloseConnection - Entry"));
       
   149 	StopConnection ( );
       
   150 	__FLOG(_L8("CloseConnection - Exit"));
       
   151 	}
       
   152 
       
   153 /**
       
   154  A transaction is one set of request, data transfer and response phases. 
       
   155  The fw calls this to mark the end of an MTP transaction. 
       
   156  */
       
   157 void CPTPIPConnection::TransactionCompleteL(const TMTPTypeRequest& /*aRequest*/)
       
   158 	{
       
   159 	__FLOG(_L8("TransactionCompleteL - Entry"));
       
   160 	__FLOG(_L8("******** Transaction Complete **************"));
       
   161 	SetTransactionPhase (EIdlePhase );
       
   162 
       
   163 	// Clear the cancel flag.
       
   164 	iCancelOnCommandState = ECancelNotReceived;
       
   165 	iCancelOnEventState = ECancelNotReceived;
       
   166 
       
   167 	// Again start listening for the command request. 
       
   168 	InitiateCommandRequestPhaseL( );
       
   169 
       
   170 	__FLOG(_L8("TransactionCompleteL - Exit"));
       
   171 	}
       
   172 
       
   173 /**
       
   174  Called by the fw to indicate that protocol layer is not using this transport anymore 
       
   175  and so we will not have any callback/SPI funtions to the fw any more
       
   176  */
       
   177 void CPTPIPConnection::Unbind(MMTPConnectionProtocol& /*aProtocol*/)
       
   178 	{
       
   179 	__FLOG(_L8("Unbind - Entry"));
       
   180 	__ASSERT_DEBUG(iProtocolLayer, Panic(EPTPIPBadState));
       
   181 	// Protocol will no longer be bound to the transport
       
   182 	iProtocolLayer = NULL;
       
   183 	__FLOG(_L8("Unbind - Exit"));
       
   184 	}
       
   185 
       
   186 /**
       
   187  Not used
       
   188  */
       
   189 TAny* CPTPIPConnection::GetExtendedInterface(TUid /*aInterfaceUid*/)
       
   190 	{
       
   191 	return NULL;
       
   192 	}
       
   193 
       
   194 /*
       
   195  * return PTPIP transport implementation UID
       
   196  */
       
   197 TUint CPTPIPConnection::GetImplementationUid()
       
   198     {
       
   199     return KMTPPTPIPTransportImplementationUid;
       
   200     }
       
   201 
       
   202 //
       
   203 // Active Object functions
       
   204 //
       
   205 
       
   206 /**
       
   207  Used to cancel the outstanding requests, and reset internal states as appropriate. 
       
   208  */
       
   209 void CPTPIPConnection::DoCancel( )
       
   210 	{
       
   211 	__FLOG(_L8("DoCancel - Entry"));
       
   212 
       
   213 	iCommandHandler->Cancel( );
       
   214 	iEventHandler->Cancel( );
       
   215 
       
   216 	SetConnectionState(ECancelled );
       
   217 
       
   218 	__FLOG(_L8("DoCancel - Exit"));
       
   219 	}
       
   220 
       
   221 /**
       
   222 The connection behaves as an active object during the PTPIP connection establishment phase.
       
   223 Subsequently, it behaves as a normal object whose functions are invoked by the MTP framework
       
   224 or by the SocketHandler on sending/ receiving data. 
       
   225 
       
   226 Thus the runl is not hit after the initial connection establishment. 
       
   227  */
       
   228 void CPTPIPConnection::RunL( )
       
   229 	{
       
   230 	__FLOG(_L8("RunL - Entry"));
       
   231 	__FLOG_VA((_L8("Current State is %d, and status is %d"), iState, iStatus.Int()));
       
   232 
       
   233 	if(iStatus != KErrNone )
       
   234 		{
       
   235 		CloseConnection( );
       
   236 		}
       
   237 	else 
       
   238 		{
       
   239 
       
   240 		switch(iState )
       
   241 		{
       
   242 		// ConstructL is complete, 
       
   243 		// the PTPIP connection establishment will be complete after the init ack is sent.
       
   244 		case EInitialising:
       
   245 			SendInitAckL( );
       
   246 			break;
       
   247 
       
   248 			// Now the PTPIP connection has been established. 
       
   249 		case EInitialisationComplete:
       
   250 			SetConnectionState(EStartListening );
       
   251 			CompleteSelf(KErrNone );
       
   252 			break;
       
   253 
       
   254 			// Start listeing for requests on the 2 channels
       
   255 		case EStartListening:
       
   256 			InitiateEventRequestPhaseL( );
       
   257 			InitiateCommandRequestPhaseL( );
       
   258 			break;
       
   259 
       
   260 		default:
       
   261 			__FLOG(_L8("PTPIP ERROR: Invalid  connection state"));
       
   262 			Panic(EPTPIPBadState );
       
   263 			break;
       
   264 		}
       
   265 		}
       
   266 
       
   267 	__FLOG(_L8("RunL - Exit"));
       
   268 	}
       
   269 
       
   270 /**
       
   271  Called when an error occurs in the RunL 
       
   272  */
       
   273 #ifdef __FLOG_ACTIVE
       
   274 TInt CPTPIPConnection::RunError(TInt aError )
       
   275 #else
       
   276 TInt CPTPIPConnection::RunError(TInt /*aError*/ )
       
   277 #endif
       
   278 	{
       
   279 	__FLOG(_L8("RunError - Entry"));
       
   280 	__FLOG_VA((_L8("PTPIP ERROR: Error received is %d"), aError));
       
   281 
       
   282 	// Cancel all the outstanding requests.
       
   283 	Cancel( );
       
   284 
       
   285 	// Stop the connection, if necessary.
       
   286 	StopConnection( );
       
   287 
       
   288 	__FLOG(_L8("RunError - Exit"));
       
   289 	return KErrNone;
       
   290 	}
       
   291 
       
   292 //
       
   293 // Receive data functions
       
   294 //
       
   295 
       
   296 /**
       
   297  Called internally to recevive the commands over the command channel. 
       
   298  This will invoke the command handler 
       
   299  to listen on the socket and get the data into the buffers passed. 
       
   300  */
       
   301 void CPTPIPConnection::InitiateCommandRequestPhaseL( )
       
   302 	{
       
   303 	__FLOG(_L8("InitiateCommandRequestPhaseL - Entry"));
       
   304 	__FLOG(_L8("******** Phase 1 - Request **************"));
       
   305 	// Set current state to request phase
       
   306 	SetTransactionPhase(ERequestPhase );
       
   307 
       
   308 	// The PTPIP data buffer is a member of connection.  
       
   309 	// Since we are expecting a request now, set the payload to request type
       
   310 	iPTPIPRequestPayload.Reset( );
       
   311 	iPTPIPCommandContainer->SetPayloadL(&iPTPIPRequestPayload );
       
   312 
       
   313 	// Call the CommandHandler to get the request in the container
       
   314 	iCommandHandler->ReceiveCommandRequestL(*iPTPIPCommandContainer );
       
   315 
       
   316 	__FLOG(_L8("InitiateCommandRequestPhaseL - Exit"));
       
   317 	}
       
   318 
       
   319 /** 
       
   320  Called to indicate completion of receive of data started by InitiateCommandRequestPhaseL.
       
   321  There is no point in reporting the error up to the f/w since no transaction has started.
       
   322  USB does nothing at this stage. In PTP , the connection is closed.
       
   323  @param aError - The error if any, received from socket.
       
   324  */
       
   325 void CPTPIPConnection::ReceiveCommandCompleteL(TInt aError )
       
   326 	{
       
   327 	__FLOG(_L8("ReceiveCommandCompleteL - Entry"));
       
   328 
       
   329 	if(KErrNone != aError )
       
   330 		{
       
   331 		__FLOG_VA((_L8("PTPIP Error: Received error=%d in request phase, closing  connection"), aError));
       
   332 		CloseConnection( );
       
   333 		}
       
   334 	else if(ValidateTransactionPhase(ERequestPhase ) )
       
   335 		{
       
   336 		// Request block received.
       
   337 		TPTPIPTypeRequestPayload* pRequest = static_cast<TPTPIPTypeRequestPayload*>(iPTPIPCommandContainer->Payload());
       
   338 
       
   339 		TUint16 op(pRequest->Uint16(TPTPIPTypeRequestPayload::EOpCode ));
       
   340 		TUint32	tran(pRequest->Uint32(TPTPIPTypeRequestPayload::ETransactionId ));
       
   341 		TUint32 sessionId = KMTPSessionNone;
       
   342 		__FLOG_VA((_L8("Command block received with op = 0x%04X ,transId = %d"), op, tran));
       
   343 
       
   344 		// Reset the iMTPRequest.
       
   345 		iMTPRequest.Reset( );
       
   346 
       
   347 		// Setup the MTP request dataset buffer. Set Operation Code and TransactionID
       
   348 		iMTPRequest.SetUint16(TMTPTypeRequest::ERequestOperationCode, op );
       
   349 		iMTPRequest.SetUint32(TMTPTypeRequest::ERequestTransactionID, tran );
       
   350 
       
   351 		// Set SessionID.
       
   352 		if(op == EMTPOpCodeOpenSession )
       
   353 			{
       
   354 			__FLOG(_L8("Processing OpenSession request"));
       
   355 			}
       
   356 		else if(op == EMTPOpCodeCloseSession || op == EMTPOpCodeResetDevice )
       
   357 			{
       
   358 			__FLOG(_L8("Processing CloseSession or the ResetDevice request"));
       
   359 			// Force CloseSession requests to be processed outside an active session. 
       
   360 			// ResetDevice currently behaves the same way as CloseSession. 
       
   361 			iMTPRequest.SetUint32(TMTPTypeRequest::ERequestParameter1,
       
   362 					iMTPSessionId );
       
   363 			}
       
   364 		else
       
   365 			{
       
   366 			sessionId = iMTPSessionId;
       
   367 			__FLOG_VA((_L8("Processing general request on session %d"), sessionId));
       
   368 			}
       
   369 		
       
   370 		iMTPRequest.SetUint32(TMTPTypeRequest::ERequestSessionID,sessionId );
       
   371 
       
   372 		// Set Parameter 1 .. Parameter 5.
       
   373 		pRequest->CopyOut(iMTPRequest, TMTPTypeRequest::ERequestParameter1,	TMTPTypeRequest::ERequestParameter5 );
       
   374 		pRequest->Reset( );
       
   375 
       
   376 		// Notify the protocol layer.
       
   377 		
       
   378 		BoundProtocolLayer().ReceivedRequestL(iMTPRequest );
       
   379 
       
   380 		}
       
   381 	__FLOG(_L8("ReceiveCommandCompleteL - Exit"));
       
   382 	}
       
   383 
       
   384 /**
       
   385  Called to get data over the command channel,( in turn its called by f/ws ReceiveData)
       
   386  @param aData - The buffer provided by the F/w in which to receive data.
       
   387  */
       
   388 void CPTPIPConnection::ReceiveCommandDataL(MMTPType& aData )
       
   389 	{
       
   390 	__FLOG(_L8("ReceiveCommandDataL - Entry"));
       
   391 
       
   392 	iRecvData = 0;
       
   393 	iTotalRecvData = 0;
       
   394 	
       
   395 	// First we will receive a PTPIP start data packet which is stored in the commandContainer
       
   396 	iPTPIPCommandContainer->SetPayloadL(&iPTPIPStartDataPayload );
       
   397 
       
   398 	// The mtp buffer is passed from the framework and will be passed on as the payload.
       
   399 	// in the next ptpip data packet.
       
   400 	iPTPIPDataContainer->SetPayloadL(&aData );
       
   401 	iCommandHandler->ReceiveCommandDataL(*iPTPIPCommandContainer );
       
   402 
       
   403 	__FLOG(_L8("ReceiveCommandDataL - Exit"));
       
   404 	}
       
   405 
       
   406 /**
       
   407  Called to indicate completion of receive of data started by ReceiveCommandDataL.
       
   408  Any errors received are sent back up to the framework. 
       
   409  */
       
   410 void CPTPIPConnection::ReceiveCommandDataCompleteL(TInt aError )
       
   411 	{
       
   412 	__FLOG(_L8("ReceiveCommandDataCompleteL - Entry"));
       
   413 	if(ValidateTransactionPhase(EDataIToRPhase ) )
       
   414 		{
       
   415 		// Data block received, notify the protocol layer.
       
   416 		iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType, 0 );
       
   417 		BoundProtocolLayer().ReceiveDataCompleteL(aError, *iPTPIPDataContainer->Payload(), iMTPRequest );
       
   418 		}
       
   419 	__FLOG(_L8("ReceiveCommandDataCompleteL - Exit"));
       
   420 	}
       
   421 
       
   422 /**
       
   423  Called by the command handler to indicate completion of receive on the command channel.
       
   424  Now check whether it was commands or data completion and call the appropriate function. 
       
   425  */
       
   426 
       
   427 void CPTPIPConnection::ReceiveCommandChannelCompleteL(TInt aError, MMTPType& /*aSource*/)
       
   428 	{
       
   429 	__FLOG(_L8("ReceiveCommandChannelCompleteL - Entry"));
       
   430 	__FLOG(_L8("******** Receiving 1 ptpip packet on command/data channel complete **************"));
       
   431 	HandleTCPError(aError );
       
   432 	TUint32	typeCommand = iPTPIPCommandContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   433 	TUint32	typeData = iPTPIPDataContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   434 	__FLOG_VA((_L8("type on the command buffer is %d and type on the data buffer is %d"), typeCommand, typeData ) );
       
   435 
       
   436 
       
   437 	switch (typeCommand)
       
   438 	{
       
   439 	case EPTPIPPacketTypeOperationRequest:
       
   440 		ReceiveCommandCompleteL(aError );
       
   441 		break;
       
   442 		
       
   443 	case EPTPIPPacketTypeStartData:
       
   444 		if(aError != KErrNone )
       
   445 			{
       
   446 			ReceiveCommandCompleteL(aError );
       
   447 			}
       
   448 		else
       
   449 			{
       
   450 			// Save the total data expected. 
       
   451 			iTotalRecvData =(static_cast<TPTPIPTypeStartDataPayload*>(iPTPIPCommandContainer->Payload()))->Uint64(TPTPIPTypeStartDataPayload::ETotalSize );
       
   452 			__FLOG_VA((_L8("Total data to receive in data phase is %ld"), iTotalRecvData));						
       
   453 			
       
   454 			//reset the command container 
       
   455 			iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketType, 0 );
       
   456 			iCommandHandler->ReceiveCommandDataL(*iPTPIPDataContainer );
       
   457 			}
       
   458 		break;
       
   459 		
       
   460 	case EPTPIPPacketTypeCancel:
       
   461 		{
       
   462 		TMTPTypeInt32* pTransId;
       
   463 		pTransId = static_cast<TMTPTypeInt32*>(iPTPIPCommandContainer->Payload());
       
   464 		iCancelOnCommandState = ECancelCmdReceived;
       
   465 		HandleCommandCancelL(pTransId->Value());
       
   466 		}
       
   467 		break;
       
   468 		
       
   469 		
       
   470 	default:
       
   471 		// No known types came on the command container, now check the data container.
       
   472 		switch (typeData)
       
   473 		{
       
   474 		case EPTPIPPacketTypeData:
       
   475 			// One PTPIP packet has been received. We will now continue getting the next PTPIP packets. 
       
   476 			iRecvData += iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::EPacketLength );
       
   477 			iRecvData -= KPTPIPDataHeaderSize; // The header size is not included in the total size sent. 
       
   478 			
       
   479 			//If more data is expected,then set the flag to use the current offset
       
   480 			//in the chunk
       
   481 			if (iRecvData < iTotalRecvData)
       
   482 			{
       
   483 			iCommandHandler->iUseOffset = ETrue;	
       
   484 				
       
   485 			}
       
   486 			
       
   487 			
       
   488 			__FLOG_VA((_L8("Data received so far in data phase is %ld"), iRecvData));
       
   489 			if(iRecvData <= iTotalRecvData )
       
   490 				{
       
   491 				iCommandHandler->ReceiveCommandDataL(*iPTPIPDataContainer );
       
   492 				}
       
   493 			else
       
   494 				{
       
   495 				__FLOG_VA((_L8("PTPIP ERROR: The data received so far= %ld is more than expected data = %ld "), iRecvData, iTotalRecvData));
       
   496 				CloseConnection( );
       
   497 				}			
       
   498 			break;
       
   499 		
       
   500 		case EPTPIPPacketTypeEndData:
       
   501 			iRecvData += iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::EPacketLength );
       
   502 			iRecvData -= KPTPIPDataHeaderSize; // The header size is not included in the total size sent. 
       
   503 			
       
   504 			iCommandHandler->iUseOffset = EFalse;
       
   505 			
       
   506 			__FLOG_VA((_L8("Data received so far in data phase is %ld"), iRecvData));
       
   507 			if(iTotalRecvData == iRecvData )
       
   508 				{
       
   509 				ReceiveCommandDataCompleteL(aError );
       
   510 				}
       
   511 			else
       
   512 				{
       
   513 				__FLOG_VA((_L8("PTPIP ERROR: The data received so far= %ld is not equal to expected data = %ld "), iRecvData, iTotalRecvData));
       
   514 				CloseConnection( );
       
   515 				}
       
   516 			break;
       
   517 		
       
   518 		case EPTPIPPacketTypeCancel:
       
   519 			{
       
   520 			TUint32 transId = iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::ETransactionId);
       
   521 			iCancelOnCommandState = ECancelCmdReceived;
       
   522 			iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType, 0 );
       
   523 			HandleCommandCancelL(transId);
       
   524 			}
       
   525 			break;
       
   526 		
       
   527 		default:
       
   528 			__FLOG_VA((_L8("PTPIP ERROR: Unexpected type received,  data container = %d, command container =%d "), typeData, typeCommand));
       
   529 			CloseConnection( );
       
   530 			break;
       
   531 		} // switch data
       
   532 	} // switch command
       
   533 
       
   534 	__FLOG(_L8("ReceiveCommandChannelCompleteL - Exit"));
       
   535 	}
       
   536 
       
   537 /**
       
   538  Called by MTP fw to get data from the transport.
       
   539  */
       
   540 void CPTPIPConnection::ReceiveDataL(MMTPType& aData, const TMTPTypeRequest& /*aRequest*/)
       
   541 	{
       
   542 	__FLOG(_L8("ReceiveDataL - Entry"));
       
   543 	__FLOG(_L8("******** Phase 2 - Data I to R **************"));
       
   544 	SetTransactionPhase(EDataIToRPhase );
       
   545 	ReceiveCommandDataL(aData );
       
   546 	__FLOG(_L8("ReceiveDataL - Exit"));
       
   547 	}
       
   548 
       
   549 /**
       
   550  Called by MTP fw to cancel the receiving data.
       
   551  */
       
   552 void CPTPIPConnection::ReceiveDataCancelL(const TMTPTypeRequest& /*aRequest*/)
       
   553 	{
       
   554 	__FLOG(_L8("ReceiveDataCancelL - Entry"));
       
   555 
       
   556 	iCommandHandler->CancelReceiveL(KErrCancel );
       
   557 	__FLOG(_L8("ReceiveDataCancelL - Exit"));
       
   558 	}
       
   559 
       
   560 /**
       
   561  This will invoke the event handler to listen on the socket and get the events 
       
   562  into the buffers passed. 
       
   563  */
       
   564 void CPTPIPConnection::InitiateEventRequestPhaseL( )
       
   565 	{
       
   566 	__FLOG(_L8("InitiateEventRequestPhaseL - Entry"));
       
   567 
       
   568 	// Initialise the PTP buffers to get the data. 
       
   569 	iPTPIPEventPayload.Reset( );
       
   570 	iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketType, NULL );
       
   571 	iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketLength,	NULL );
       
   572 	iPTPIPEventContainer->SetPayloadL(&iPTPIPEventPayload );
       
   573 
       
   574 	// Call the EventHandler
       
   575 	iEventHandler->ReceiveEventL(*iPTPIPEventContainer );
       
   576 	__FLOG(_L8("InitiateEventRequestPhaseL - Exit"));
       
   577 	}
       
   578 
       
   579 /**
       
   580  Signals the completion of receving an event. 
       
   581  */
       
   582 void CPTPIPConnection::ReceiveEventCompleteL(TInt aError, MMTPType& /*aSource*/)
       
   583 	{
       
   584 	__FLOG(_L8("ReceiveEventCompleteL - Entry"));
       
   585 
       
   586 	TUint32	type = iPTPIPEventContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   587 	__FLOG_VA((_L8("Error value is %d and type is %d"), aError, type));
       
   588 
       
   589 	if(KErrNone != aError )
       
   590 		{
       
   591 		__FLOG_VA((_L8("PTPIP Error: Received error=%d in request phase, closing  connection"), aError));
       
   592 		CloseConnection( );
       
   593 		}
       
   594 	else
       
   595 		{
       
   596 		// For a probe request, we just send a probe response and don't notify the MTP f/w. 
       
   597 		if( type == EPTPIPPacketTypeProbeRequest )
       
   598 			{
       
   599 			__FLOG(_L8("Received a probe request, sending back a probe response"));
       
   600 			// Send the response, 
       
   601 			iPTPIPEventContainer->SetPayloadL(NULL );
       
   602 			iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketLength, iPTPIPEventContainer->Size( ) );
       
   603 			iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketType, EPTPIPPacketTypeProbeResponse );
       
   604 
       
   605 			// Call the EventHandler
       
   606 			iEventHandler->SendEventL(*iPTPIPEventContainer );
       
   607 			}
       
   608 		else if(type == EPTPIPPacketTypeCancel )
       
   609 			{			
       
   610 			iCancelOnEventState = ECancelCmdReceived;
       
   611 			HandleEventCancelL(); 
       
   612 			}
       
   613 		else if(type == EPTPIPPacketTypeEvent )
       
   614 			{
       
   615 			// Request block received.
       
   616 			TPTPIPTypeResponsePayload* pEvent = static_cast<TPTPIPTypeResponsePayload*>(iPTPIPEventContainer->Payload());
       
   617 			TUint16	op(pEvent->Uint16(TPTPIPTypeResponsePayload::EResponseCode ));
       
   618 			__FLOG_VA((_L8("Event block 0x%04X received"), op));
       
   619 
       
   620 			// Reset the iMTPRequest.
       
   621 			iMTPEvent.Reset( );
       
   622 
       
   623 			// Setup the MTP request dataset buffer. Set Operation Code and TransactionID
       
   624 			iMTPEvent.SetUint16(TMTPTypeEvent::EEventCode, op );
       
   625 			iMTPEvent.SetUint32(TMTPTypeEvent::EEventSessionID,	iMTPSessionId );
       
   626 			iMTPEvent.SetUint32(TMTPTypeEvent::EEventTransactionID,	pEvent->Uint32(TPTPIPTypeResponsePayload::ETransactionId ) );
       
   627 
       
   628 			// Set Parameter 1 .. Parameter 3.
       
   629 			pEvent->CopyOut(iMTPRequest,TMTPTypeResponse::EResponseParameter1, TMTPTypeResponse::EResponseParameter3 );
       
   630 			pEvent->Reset( );
       
   631 
       
   632 			BoundProtocolLayer().ReceivedEventL(iMTPEvent );
       
   633 			InitiateEventRequestPhaseL( );
       
   634 			}
       
   635 		// If unexpected data is received , its ignored in the release mode. 
       
   636 		else
       
   637 			{
       
   638 			__FLOG(_L8("PTPIP ERROR : Unknown event type received, ignoring it."));
       
   639 			__ASSERT_DEBUG(type, Panic(EPTPIPBadState));
       
   640 			}
       
   641 		}
       
   642 	__FLOG(_L8("ReceiveEventCompleteL - Exit"));
       
   643 	}
       
   644 
       
   645 //
       
   646 // Send data functions
       
   647 //
       
   648 /**
       
   649  Called by the MTP f/w to send the response to the initiator.
       
   650  Also called from this connection itself to send the cancel response. 
       
   651  */
       
   652 void CPTPIPConnection::SendResponseL(const TMTPTypeResponse& aResponse,	const TMTPTypeRequest& aRequest )
       
   653 	{
       
   654 	__FLOG(_L8("SendResponseL - Entry"));
       
   655 
       
   656 	// Update the transaction state.
       
   657 	SetTransactionPhase(EResponsePhase );
       
   658 	__FLOG(_L8("******** Phase 3 - Response **************"));
       
   659 	
       
   660 	if(iCancelOnCommandState  )
       
   661 		{
       
   662 		__FLOG(_L8("Cancel has been received from initiator, so send own response"));
       
   663 		
       
   664 		SendCancelResponseL(aRequest.Uint32(TMTPTypeRequest::ERequestTransactionID ));
       
   665 		}
       
   666 	else
       
   667 		{
       
   668 		TUint16	opCode(aRequest.Uint16(TMTPTypeRequest::ERequestOperationCode ));
       
   669 		TUint16 rspCode(aResponse.Uint16(TMTPTypeResponse::EResponseCode ));
       
   670 		__FLOG_VA((_L8("ResponseCode = 0x%04X, Operation Code = 0x%04X"), rspCode, opCode));
       
   671 
       
   672 		if((opCode == EMTPOpCodeOpenSession) &&(rspCode == EMTPRespCodeOK) )
       
   673 			{
       
   674 			// An session has been opened. Record the active SessionID.
       
   675 			iMTPSessionId = aRequest.Uint32(TMTPTypeRequest::ERequestParameter1 );
       
   676 			__FLOG_VA((_L8("Processing OpenSession response, SessionID = %d"), iMTPSessionId));
       
   677 			}
       
   678 		else if(((opCode == EMTPOpCodeCloseSession) ||
       
   679 				(opCode == EMTPOpCodeResetDevice))&&(rspCode == EMTPRespCodeOK) )
       
   680 			{
       
   681 			// An session has been closed. Clear the active SessionID.        
       
   682 			__FLOG_VA((_L8("Processing CloseSession or ResetDevice response, SessionID = %d"), iMTPSessionId));
       
   683 			iMTPSessionId = KMTPSessionNone;
       
   684 			}
       
   685 
       
   686 		//Setup the parameter block payload dataset. Note that since this is a 
       
   687 		//variable length dataset, it must first be reset.
       
   688 
       
   689 		iPTPIPResponsePayload.Reset( );
       
   690 		TUint numberOfNullParam = TMTPTypeResponse::EResponseParameter5 - TMTPTypeResponse::EResponseParameter1 + 1;
       
   691 
       
   692 		iPTPIPResponsePayload.CopyIn(aResponse,	
       
   693 		                             TMTPTypeResponse::EResponseParameter1, TMTPTypeResponse::EResponseParameter5, 
       
   694 		                             ETrue,
       
   695 		                             numberOfNullParam);
       
   696 		iPTPIPResponsePayload.SetUint16(TPTPIPTypeResponsePayload::EResponseCode, rspCode );
       
   697 		iPTPIPResponsePayload.SetUint32(TPTPIPTypeResponsePayload::ETransactionId, aRequest.Uint32(TMTPTypeRequest::ERequestTransactionID ) );
       
   698 
       
   699 		// Setup the command container.
       
   700 		iPTPIPCommandContainer->SetPayloadL(const_cast<TPTPIPTypeResponsePayload*>(&iPTPIPResponsePayload) );
       
   701 		iPTPIPCommandContainer->SetUint32L(	CPTPIPGenericContainer::EPacketLength, static_cast<TUint32>(iPTPIPCommandContainer->Size()) );
       
   702 		iPTPIPCommandContainer->SetUint32L(	CPTPIPGenericContainer::EPacketType, EPTPIPPacketTypeOperationResponse );
       
   703 
       
   704 		// Initiate the command send sequence.
       
   705 		__FLOG_VA((_L8("Sending response 0x%04X(%d bytes)"),
       
   706 						iPTPIPResponsePayload.Uint16(TPTPIPTypeResponsePayload::EResponseCode),
       
   707 						iPTPIPCommandContainer->Uint32L(CPTPIPGenericContainer::EPacketLength)));
       
   708 		iCommandHandler->SendCommandL(*iPTPIPCommandContainer );
       
   709 		}
       
   710 
       
   711 	__FLOG(_L8("SendResponseL - Exit"));
       
   712 	}
       
   713 
       
   714 /**
       
   715  Send response complete
       
   716  */
       
   717 void CPTPIPConnection::SendCommandCompleteL(TInt aError )
       
   718 	{
       
   719 
       
   720 	__FLOG(_L8("SendCommandCompleteL - Entry"));
       
   721 
       
   722 	if(ValidateTransactionPhase(EResponsePhase ) )
       
   723 		{
       
   724 		BoundProtocolLayer().SendResponseCompleteL( aError,
       
   725 		                                            *static_cast<TMTPTypeResponse*>(iPTPIPCommandContainer->Payload()), 
       
   726 		                                            iMTPRequest );
       
   727 		}
       
   728 	__FLOG(_L8("SendCommandCompleteL - Exit"));
       
   729 	}
       
   730 
       
   731 /**
       
   732  Send data complete
       
   733  */
       
   734 void CPTPIPConnection::SendCommandDataCompleteL(TInt aError )
       
   735 	{
       
   736 	__FLOG(_L8("SendCommandDataCompleteL - Entry"));
       
   737 
       
   738 	if(ValidateTransactionPhase(EDataRToIPhase ) )
       
   739 		{
       
   740 		BoundProtocolLayer().SendDataCompleteL(aError, *iPTPIPDataContainer->Payload(), iMTPRequest );
       
   741 		}
       
   742 	SetConnectionState(EDataSendFinished);
       
   743 	iPTPIPDataContainer->SetPayloadL(NULL );
       
   744 
       
   745 	__FLOG(_L8("SendCommandDataCompleteL - Exit"));
       
   746 	}
       
   747 
       
   748 /**
       
   749  Called by the command handler to indicate completion of send on the command channel.
       
   750  Now check whether it was commands or data completion and call the appropriate function. 
       
   751  */
       
   752 void CPTPIPConnection::SendCommandChannelCompleteL(TInt aError, const MMTPType& /*aSource*/)
       
   753 	{
       
   754 	__FLOG(_L8("SendCommandChannelCompleteL - Entry"));
       
   755 
       
   756 	// Now see whether we have completed getting data or commands, and call the appropriate function.
       
   757 	TUint typeCommand = iPTPIPCommandContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   758 	TUint typeData = iPTPIPDataContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   759 	__FLOG_VA((_L8("type on the command buffer is %d and type on the data buffer is %d"), typeCommand, typeData ) );
       
   760 	
       
   761 	
       
   762 	// if we have received a cancel on the event channel then terminate the current sending
       
   763 	// and handle the receiving and sending of cancel on the channel.
       
   764 	if (iCancelOnEventState && !iCancelOnCommandState)
       
   765 		{
       
   766 		HandleCancelDuringSendL(); 	
       
   767 		}
       
   768 	
       
   769 	// A command has been sent
       
   770 	else if((EPTPIPPacketTypeOperationResponse == typeCommand) &&(0 == typeData) )
       
   771 		{
       
   772 		// If this response was a cancel, then we don't inform the framework, as it was internally generated
       
   773 		if(iCancelOnCommandState )
       
   774 			{
       
   775 			iCancelOnCommandState = ECancelCmdHandled;
       
   776 			SendCommandCompleteL(aError );
       
   777 			HandleCommandCancelCompleteL( );
       
   778 			}
       
   779 		// Tell the framework that a command has been received. 
       
   780 		else
       
   781 			{
       
   782 			iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketType, 0 );
       
   783 			SendCommandCompleteL(aError );
       
   784 			}
       
   785 		}
       
   786 
       
   787 	// Tell the connection that data has been sent.
       
   788 	else if((EPTPIPPacketTypeEndData == typeData) &&(0 == typeCommand ) )
       
   789 			{
       
   790 			iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType, 0 );
       
   791 			SendCommandDataCompleteL(aError );
       
   792 			}
       
   793 
       
   794 	// We sent the start data packet, and we now have to send the actual data.  
       
   795 	else if((EPTPIPPacketTypeStartData == typeCommand) &&(EDataSendInProgress == iState ) )
       
   796 			{
       
   797 			iPTPIPCommandContainer->SetUint32L(	CPTPIPGenericContainer::EPacketType, 0 );
       
   798 			SendDataPacketL( );
       
   799 			}
       
   800 
       
   801 	// Any other type indicates a programming error, and a panic is raised. 
       
   802 	else
       
   803 		{
       
   804 		__FLOG_VA((_L8("PTPIP ERROR: Unexpected type in sent data, type = = %d, command =%d "), typeData, typeCommand));
       
   805 		Panic(EPTPIPBadState );
       
   806 		}
       
   807 
       
   808 	__FLOG(_L8("SendCommandChannelCompleteL - Exit"));
       
   809 	}
       
   810 
       
   811 /**
       
   812  Called by the MTP fw to send the data by the transport via the sockets
       
   813  */
       
   814 void CPTPIPConnection::SendDataL(const MMTPType& aData,	const TMTPTypeRequest& aRequest )
       
   815 	{
       
   816 	__FLOG(_L8("SendDataL - Entry"));
       
   817 
       
   818 	__FLOG(_L8("******** Phase 2 - Data R to I **************"));
       
   819 	SetTransactionPhase(EDataRToIPhase );
       
   820 	SetConnectionState(EDataSendInProgress );
       
   821 
       
   822 	// Save the actual data in the dataContainer
       
   823 	iPTPIPDataContainer->SetPayloadL(const_cast<MMTPType*>(&aData) );
       
   824 	iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketLength,	iPTPIPDataContainer->Size( ) );
       
   825 
       
   826 	iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType,EPTPIPPacketTypeEndData );
       
   827 	iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::ETransactionId,aRequest.Uint32(TMTPTypeRequest::ERequestTransactionID ) );
       
   828 
       
   829 	// Create the start data ptpip packet. 
       
   830 	iPTPIPStartDataPayload.Reset( );
       
   831 	iPTPIPStartDataPayload.SetUint32(TPTPIPTypeStartDataPayload::ETransactionId, aRequest.Uint32(TMTPTypeRequest::ERequestTransactionID ) );
       
   832 	iPTPIPStartDataPayload.SetUint64(TPTPIPTypeStartDataPayload::ETotalSize, aData.Size( ) );
       
   833 
       
   834 	iPTPIPCommandContainer->SetPayloadL(&iPTPIPStartDataPayload );
       
   835 	iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketLength, iPTPIPCommandContainer->Size( ) );
       
   836 	iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketType,	EPTPIPPacketTypeStartData );
       
   837 
       
   838 	// First send the start data packet, once this is complete, it will invoke the 
       
   839 	// SendCommandChannelCompleteL, where we will check the state and send the 
       
   840 	// actual data in the next packet, which has been saved in the dataContainer. 
       
   841 	SendStartDataPacketL( );
       
   842 
       
   843 	__FLOG(_L8("SendDataL - Exit"));
       
   844 	}
       
   845 
       
   846 /**
       
   847  The data will be sent in 2 ptpip operations. 
       
   848  first the start data ptpip packet will be sent. This has the totalk size and transaction. 
       
   849  next the actual data packet will be sent, with the end data ptp ip header.
       
   850  */
       
   851 void CPTPIPConnection::SendStartDataPacketL( )
       
   852 	{
       
   853 	__FLOG(_L8("SendStartDataPacketL - Entry"));
       
   854 
       
   855 	SetConnectionState(EDataSendInProgress );
       
   856 	iCommandHandler->SendCommandL(*iPTPIPCommandContainer );
       
   857 	__FLOG(_L8("SendStartDataPacketL - Exit"));
       
   858 	}
       
   859 
       
   860 /**
       
   861  Send the actual data, which has come from the MTP framework 
       
   862  */
       
   863 void CPTPIPConnection::SendDataPacketL( )
       
   864 	{
       
   865 	__FLOG(_L8("SendDataPacketL - Entry"));
       
   866 	
       
   867 	MMTPType* payLoad = iPTPIPDataContainer->Payload();
       
   868 	
       
   869 	TPtr8 headerChunk(NULL, 0);
       
   870 	TBool hasTransportHeader = payLoad->ReserveTransportHeader(KPTPIPDataHeaderSize, headerChunk);
       
   871 	if (hasTransportHeader)
       
   872 	    {
       
   873         const TInt KLengthOffset = 0;
       
   874         const TInt KTypeOffset = 4;
       
   875         const TInt KXIDOffset = 8;
       
   876         TUint32 pkgLength = iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::EPacketLength);
       
   877         TUint32 pkgType = iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::EPacketType);
       
   878         TUint32 transId = iPTPIPDataContainer->Uint32L(CPTPIPDataContainer::ETransactionId);
       
   879         
       
   880         memcpy(&(headerChunk[KLengthOffset]), &pkgLength, sizeof(TUint32));
       
   881         memcpy(&(headerChunk[KTypeOffset]), &pkgType, sizeof(TUint32));
       
   882         memcpy(&(headerChunk[KXIDOffset]), &transId, sizeof(TUint32));
       
   883 
       
   884         SetConnectionState(EDataSendInProgress );
       
   885         iCommandHandler->SendCommandDataL(*payLoad, transId);
       
   886 	    }
       
   887 	else
       
   888 	    {
       
   889 	    
       
   890 	    SetConnectionState(EDataSendInProgress );
       
   891 	    iCommandHandler->SendCommandDataL(*iPTPIPDataContainer,	iPTPIPDataContainer->Uint32L(TMTPTypeRequest::ERequestTransactionID ) );
       
   892 	    }
       
   893 
       
   894 	__FLOG(_L8("SendDataPacketL - Exit"));
       
   895 	}
       
   896 
       
   897 /**
       
   898  Called by the fw to cancel the sending of data
       
   899  */
       
   900 void CPTPIPConnection::SendDataCancelL(const TMTPTypeRequest& /*aRequest*/)
       
   901 	{
       
   902 	__FLOG(_L8("SendDataCancelL - Entry"));
       
   903 	iCommandHandler->CancelSendL(KErrCancel );
       
   904 	__FLOG(_L8("SendDataCancelL - Exit"));
       
   905 	}
       
   906 
       
   907 /**
       
   908  Called by the fw to send an event. 
       
   909  */
       
   910 void CPTPIPConnection::SendEventL(const TMTPTypeEvent& aEvent )
       
   911 	{
       
   912 	__FLOG(_L8("SendEventL - Entry"));
       
   913 
       
   914     // Reset the event.
       
   915     iMTPEvent.Reset(); 
       
   916     MMTPType::CopyL(aEvent, iMTPEvent);
       
   917     
       
   918 	TUint16 opCode(aEvent.Uint16(TMTPTypeEvent::EEventCode ));
       
   919 	TUint32 tran(aEvent.Uint32(TMTPTypeEvent::EEventTransactionID ));
       
   920 	__FLOG_VA((_L8(" Sending event with Operation Code = 0x%04X and tran id = %d"), opCode, tran ));
       
   921 
       
   922 	TBool isNullParamValid = EFalse;
       
   923 	TUint numberOfNullParam = 0;
       
   924 
       
   925 	iPTPIPEventPayload.CopyIn(aEvent, 
       
   926 	                          TMTPTypeResponse::EResponseParameter1,TMTPTypeResponse::EResponseParameter3, 
       
   927 	                          isNullParamValid,
       
   928 	                          numberOfNullParam );
       
   929 	
       
   930 	iPTPIPEventPayload.SetUint16(TPTPIPTypeResponsePayload::EResponseCode, opCode );
       
   931 	iPTPIPEventPayload.SetUint32(TPTPIPTypeResponsePayload::ETransactionId,	tran );
       
   932 
       
   933 	// Setup the bulk container.
       
   934 	iPTPIPEventContainer->SetPayloadL(const_cast<TPTPIPTypeResponsePayload*>(&iPTPIPEventPayload) );
       
   935 	iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketLength,	static_cast<TUint32>(iPTPIPEventContainer->Size()) );
       
   936 	iPTPIPEventContainer->SetUint32L(CPTPIPGenericContainer::EPacketType, EPTPIPPacketTypeEvent );
       
   937 
       
   938 	// Initiate the event send sequence.
       
   939 	__FLOG_VA((_L8("Sending response 0x%04X(%d bytes)"),
       
   940 					iPTPIPEventPayload.Uint16(TPTPIPTypeResponsePayload::EResponseCode),
       
   941 					iPTPIPEventContainer->Uint32L(CPTPIPGenericContainer::EPacketLength)));
       
   942 
       
   943 	iEventHandler->SendEventL(*iPTPIPEventContainer );
       
   944 	__FLOG(_L8("SendEventL - Exit"));
       
   945 	}
       
   946 
       
   947 /**
       
   948  Marks the completion of the asynchronous send event. 
       
   949  */
       
   950 void CPTPIPConnection::SendEventCompleteL(TInt aError, const MMTPType& /*aSource*/)
       
   951 	{
       
   952 	__FLOG(_L8("SendEventCompleteL - Entry"));
       
   953 	TUint type = iPTPIPEventContainer->Uint32L(CPTPIPGenericContainer::EPacketType );
       
   954 
       
   955 	// Notify the fw that event was sent. 
       
   956 	if(type == EPTPIPPacketTypeEvent )
       
   957 		{
       
   958 		// Notify the fw
       
   959 		BoundProtocolLayer().SendEventCompleteL(aError, iMTPEvent );
       
   960 		}
       
   961 
       
   962 #ifdef _DEBUG
       
   963 	//In case we sent a probe response, we dont' need to notify the fw.
       
   964 	else
       
   965 		if(type == EPTPIPPacketTypeProbeResponse )
       
   966 			{
       
   967 			__FLOG(_L8("Probe response was sent successfully"));
       
   968 			}
       
   969 		else
       
   970 			{
       
   971 			// If unexpected data was sent , it is ignored in the release mode. 
       
   972 			__FLOG(_L8("PTPIP ERROR: An invalid send event completion signalled"));
       
   973 			__ASSERT_DEBUG(type, Panic(EPTPIPBadState));
       
   974 			}
       
   975 #endif
       
   976 	
       
   977 	// Restart listening for events
       
   978 	InitiateEventRequestPhaseL( );
       
   979 	__FLOG(_L8("SendEventCompleteL - Exit"));
       
   980 	}
       
   981 
       
   982 //
       
   983 // Cancel handling functions
       
   984 //
       
   985 
       
   986 /**
       
   987  Handle the cancel on the event channel. This can come before 
       
   988  or after the cancel on the command channle, and it can also come 
       
   989  in any of the MTP transaction states ( request, response, data)
       
   990  */
       
   991 void CPTPIPConnection::HandleEventCancelL( )
       
   992 	{
       
   993 	__FLOG(_L8("HandleEventCancelL - Entry"));
       
   994 	__FLOG_VA((_L8("iCancelOnCommandState = 0x%04X, and  iCancelOnEventState = 0x%04X"), iCancelOnCommandState, iCancelOnEventState));
       
   995 
       
   996 	// Check whether the cancel has already been received on the command channel. 
       
   997 	// If so then we can simply ignore this on the event channel. 
       
   998 	switch(iCancelOnCommandState )
       
   999 		{
       
  1000 		case ECancelCmdHandled:
       
  1001 			// Cancel has already been received and handled on the command channel
       
  1002 			// ignore the cancel on event channel and reset the state to none, 
       
  1003 			// and start listening for the next transaction.
       
  1004 			iCancelOnCommandState = ECancelNotReceived;
       
  1005 			iCancelOnEventState = ECancelNotReceived;
       
  1006 			InitiateEventRequestPhaseL();
       
  1007 			break;
       
  1008 
       
  1009 		case ECancelCmdReceived:
       
  1010 		case ECancelCmdHandleInProgress:
       
  1011 			// cancel has already been received on the command channel and is being 
       
  1012 			// handled. Ignore the cancel on event channel. 
       
  1013 			iCancelOnEventState = ECancelEvtHandled;
       
  1014 			break;
       
  1015 
       
  1016 		case ECancelNotReceived:
       
  1017 			// cancel on command has not yet been received. depending on the current 
       
  1018 			// mtp transaction state, handle the cancel. 
       
  1019 
       
  1020 			switch(iTransactionState )
       
  1021 				{
       
  1022 				case EDataIToRPhase:
       
  1023 					SetNULLPacketL();
       
  1024 					iCancelOnEventState = ECancelEvtHandled;
       
  1025 					break;
       
  1026 
       
  1027 				case EDataRToIPhase:
       
  1028 					// Set the commandHandler's cancel flag on. 
       
  1029 					// It will complete sending the current PTPIP packet and then handle.
       
  1030 					// Once a PTPIP packet has been sent , the sendCommandChannel complete will 
       
  1031 					// be invoked, and the cancel will be checked and handled. 
       
  1032 					iCancelOnEventState = ECancelEvtHandled;
       
  1033 					iCommandHandler->SetCancel();
       
  1034 					break;
       
  1035 
       
  1036 				case EResponsePhase:
       
  1037 				case ERequestPhase:
       
  1038 				default:
       
  1039 					__FLOG(_L8(" Cancel received on event channel during a non data phase, ignoring, as this will be handled when its received on command channel."));
       
  1040 					iCancelOnEventState = ECancelEvtHandled;
       
  1041 					break;
       
  1042 				}// end of switch for transaction phase.
       
  1043 
       
  1044 			break;
       
  1045 		default:
       
  1046 			break;
       
  1047 		}
       
  1048 	__FLOG(_L8("HandleEventCancelL - Exit"));
       
  1049 	}
       
  1050 
       
  1051 /** 
       
  1052  Handle the cancel on the command channel. This can come before 
       
  1053  or after the cancel on the event channel, and it can also come 
       
  1054  in any of the MTP transaction states ( request, response, data)
       
  1055  */
       
  1056 void CPTPIPConnection::HandleCommandCancelL(TUint32 aTransId )
       
  1057 	{
       
  1058 	__FLOG(_L8("HandleCommandCancelL - Entry"));
       
  1059 
       
  1060 	switch(iTransactionState )
       
  1061 		{
       
  1062 		case ERequestPhase:
       
  1063 			__FLOG(_L8(" Cancel received during the request phase before the request packet, ignoring."));
       
  1064 			iCancelOnCommandState = ECancelCmdHandled;
       
  1065 			if (iCancelOnEventState == ECancelNotReceived)
       
  1066 				{
       
  1067 				// Wait for it to be received on event
       
  1068 				__FLOG(_L8("Awaiting cancel on the event channel."));
       
  1069 				}
       
  1070 			else
       
  1071 				{
       
  1072 				HandleCommandCancelCompleteL();
       
  1073 				}			
       
  1074 			InitiateCommandRequestPhaseL();
       
  1075 			break;
       
  1076 			
       
  1077 		case EDataRToIPhase:
       
  1078 			iCancelOnCommandState = ECancelCmdHandleInProgress;
       
  1079 			SendCancelToFrameworkL(aTransId );
       
  1080 			SendCommandDataCompleteL(KErrCancel);
       
  1081 			break;
       
  1082 			
       
  1083 		case EDataIToRPhase:
       
  1084 			iCancelOnCommandState = ECancelCmdHandleInProgress;
       
  1085 			SendCancelToFrameworkL(aTransId );
       
  1086 			ReceiveCommandDataCompleteL(KErrCancel);
       
  1087 			break;
       
  1088 			
       
  1089 		case EResponsePhase:
       
  1090 			iCancelOnCommandState = ECancelCmdHandled;
       
  1091 			if (iCancelOnEventState == ECancelNotReceived)
       
  1092 				{
       
  1093 				// Wait for it to be received on event
       
  1094 				__FLOG(_L8("Awaiting cancel on the event channel."));
       
  1095 				}
       
  1096 			else
       
  1097 				{
       
  1098 				HandleCommandCancelCompleteL();
       
  1099 				}
       
  1100 			
       
  1101 			break;
       
  1102 		}// switch
       
  1103 
       
  1104 	__FLOG(_L8("HandleCommandCancelL - Exit"));
       
  1105 	}
       
  1106 	
       
  1107 	
       
  1108 void CPTPIPConnection::HandleCancelDuringSendL()
       
  1109 	{
       
  1110 	__FLOG(_L8("HandleCancelDuringSendL - Entry"));	
       
  1111 	iCommandHandler->Cancel( );
       
  1112 	// Now start listening for the cancel on command channel.
       
  1113 	iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType, 0 );
       
  1114 	iPTPIPCommandContainer->SetUint32L(	CPTPIPGenericContainer::EPacketType, 0 );
       
  1115 	iPTPIPCommandCancelPayload.Set(0 );
       
  1116 	iPTPIPCommandContainer->SetPayloadL(&iPTPIPCommandCancelPayload );
       
  1117 	iCommandHandler->ReceiveCommandRequestL(*iPTPIPCommandContainer );	
       
  1118 	__FLOG(_L8("HandleCancelDuringSendL - Exit"));
       
  1119 	}
       
  1120 
       
  1121 /**
       
  1122  Called when the reponse to the cancel has been sent to the initiator
       
  1123  */
       
  1124 void CPTPIPConnection::HandleCommandCancelCompleteL( )
       
  1125 	{
       
  1126 	__FLOG(_L8("HandleCommandCancelCompleteL - Entry"));	
       
  1127 	//now cancel handling is complete.
       
  1128 
       
  1129 	if((ECancelCmdHandled == iCancelOnCommandState) &&(ECancelEvtHandled == iCancelOnEventState) )
       
  1130 		{
       
  1131 		__FLOG(_L8("Completed handling cancel on both channels. "));
       
  1132 		// Cancel has already been received and handled on the command channel
       
  1133 		// ignore the cancel on event channel and reset the state to none, 
       
  1134 		// and start listening for the next transaction.
       
  1135 		iCancelOnCommandState = ECancelNotReceived;
       
  1136 		iCancelOnEventState = ECancelNotReceived;
       
  1137 		// The transaction has been cancelled, now start listening again for next transaction.
       
  1138 		InitiateEventRequestPhaseL();
       
  1139 		}
       
  1140 	// if the cancel has not been received yet on event, we wait for it. 
       
  1141 	else if(ECancelEvtHandled != iCancelOnEventState )
       
  1142 		{
       
  1143 		__FLOG(_L8("Waiting for the cancel on the event channel. "));
       
  1144 		}
       
  1145 	__FLOG(_L8("HandleCommandCancelCompleteL - Exit"));
       
  1146 	}
       
  1147 
       
  1148 /**
       
  1149  Inform the MTP framework that a cancel has been received. 
       
  1150  */
       
  1151 void CPTPIPConnection::SendCancelToFrameworkL(TUint32 aTransId )
       
  1152 	{
       
  1153 	__FLOG(_L8("SendCancelToFramework - Entry"));
       
  1154 
       
  1155 	// Setup the MTP request dataset buffer. Set Operation Code and TransactionID
       
  1156 	iMTPEvent.Reset( );
       
  1157 	iMTPEvent.SetUint16(TMTPTypeEvent::EEventCode,	EMTPEventCodeCancelTransaction );
       
  1158 	iMTPEvent.SetUint32(TMTPTypeEvent::EEventSessionID, iMTPSessionId );
       
  1159 	iMTPEvent.SetUint32(TMTPTypeEvent::EEventTransactionID, aTransId );
       
  1160 
       
  1161 	BoundProtocolLayer().ReceivedEventL(iMTPEvent );
       
  1162 	__FLOG(_L8("SendCancelToFramework - Exit"));
       
  1163 	}
       
  1164 
       
  1165 /**
       
  1166  Send the response to the cancel event. 
       
  1167  */
       
  1168 void CPTPIPConnection::SendCancelResponseL(TUint32 aTransId )
       
  1169 	{
       
  1170 	__FLOG(_L8("SendCancelResponse - Entry"));
       
  1171 	iPTPIPResponsePayload.Reset( );
       
  1172 	iPTPIPResponsePayload.SetUint16(TPTPIPTypeResponsePayload::EResponseCode, EMTPRespCodeTransactionCancelled );
       
  1173 	iPTPIPResponsePayload.SetUint32(TPTPIPTypeResponsePayload::ETransactionId, aTransId );
       
  1174 
       
  1175 	// Setup the command container.
       
  1176 	iPTPIPCommandContainer->SetPayloadL(const_cast<TPTPIPTypeResponsePayload*>(&iPTPIPResponsePayload) );
       
  1177 	iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketLength, static_cast<TUint32>(iPTPIPCommandContainer->Size()) );
       
  1178 	iPTPIPCommandContainer->SetUint32L(CPTPIPGenericContainer::EPacketType,	EPTPIPPacketTypeOperationResponse );
       
  1179 
       
  1180 	// Initiate the command send sequence.
       
  1181 	__FLOG_VA((_L8("Sending response 0x%04X(%d bytes)"),
       
  1182 					iPTPIPResponsePayload.Uint16(TPTPIPTypeResponsePayload::EResponseCode),
       
  1183 					iPTPIPCommandContainer->Uint32L(CPTPIPGenericContainer::EPacketLength)));
       
  1184 	iCommandHandler->SendCommandL(*iPTPIPCommandContainer );
       
  1185 	__FLOG(_L8("SendCancelResponse - Exit"));
       
  1186 	}
       
  1187 
       
  1188 /**
       
  1189  * If the cancel packet is received on the event channel first
       
  1190  * and we are in the DataItoR phase, we have to continue reading and ignoreing the 
       
  1191  * data packets sent by the initiator, into dummy buffers until the cancel packet is received. 
       
  1192  */
       
  1193 void CPTPIPConnection::SetNULLPacketL()
       
  1194 	{
       
  1195 	__FLOG(_L8("SetNULLPacketL - Entry"));
       
  1196     // Setup the bulk container and initiate the bulk data receive sequence.
       
  1197     iNullBuffer.Close();
       
  1198     iNullBuffer.CreateL(KMTPNullChunkSize);
       
  1199     iNullBuffer.SetLength(KMTPNullChunkSize);
       
  1200     iNull.SetBuffer(iNullBuffer);
       
  1201 	iPTPIPDataContainer->SetPayloadL(&iNull);
       
  1202 	__FLOG(_L8("SetNULLPacketL - Exit"));
       
  1203 	}
       
  1204 
       
  1205 //
       
  1206 // Getters , Setters and other helper functions
       
  1207 //
       
  1208 
       
  1209 /**
       
  1210  This function will transfer the two command and event sockets from the Controller
       
  1211  and indicate the successful transfer to the PTP controller
       
  1212  @leave - In case the Publish and Subscribe mechanism gives any errors while getting the 
       
  1213  property names, then a leave occurs. 
       
  1214  Also in case opening or transferring the socket fails, a leave is generated. 
       
  1215  */
       
  1216 void CPTPIPConnection::TransferSocketsL( )
       
  1217 	{
       
  1218 
       
  1219 	__FLOG(_L8("TransferSocketsL - Entry"));
       
  1220 
       
  1221 	TName evtsockname, cmdsockname;
       
  1222 	TUid propertyUid=iConnectionMgr->ClientSId();
       
  1223 	User::LeaveIfError(RProperty::Get(propertyUid, ECommandSocketName,	cmdsockname ));
       
  1224 	User::LeaveIfError(RProperty::Get(propertyUid, EEventSocketName, evtsockname ));
       
  1225 
       
  1226 	RSocketServ serversocket;
       
  1227 	TInt err=serversocket.Connect( );
       
  1228 	__FLOG_VA((_L8("Connected to socketServer with %d code"), err) );
       
  1229 	
       
  1230 	if (KErrNone == err)
       
  1231 		{			
       
  1232 		User::LeaveIfError(iCommandHandler->Socket().Open(serversocket ));
       
  1233 		User::LeaveIfError(iEventHandler->Socket().Open(serversocket ));
       
  1234 	
       
  1235 		User::LeaveIfError(err=iCommandHandler->Socket().Transfer(serversocket, cmdsockname ));
       
  1236 		User::LeaveIfError(err=iEventHandler->Socket().Transfer(serversocket, evtsockname ));
       
  1237 		}
       
  1238 	
       
  1239 	iCommandHandler->SetSocketOptions();
       
  1240 	iEventHandler->SetSocketOptions();
       
  1241 
       
  1242 	__FLOG(_L8("TransferSocketsL - Exit"));
       
  1243 	}
       
  1244 
       
  1245 
       
  1246 /**
       
  1247  Connection establishment has 4 steps, the first 3 are completed by the controller process:
       
  1248 
       
  1249  1. Initiator connects to command socket, sends the init command request
       
  1250  2. Responder replies with the init command ack
       
  1251  3. Initiator connects to the command socket, sends the init event request
       
  1252 
       
  1253  4. Responder replies with the init event ack or init event fail. 
       
  1254 
       
  1255  The last step of sending the init ack is done by the transport plugin from 
       
  1256  the mtp process. This is done after the the framework has loaded, the sockets have
       
  1257  been transferred to this process and the transport is up. 
       
  1258  */
       
  1259 void CPTPIPConnection::SendInitAckL( )
       
  1260 	{
       
  1261 
       
  1262 	__FLOG(_L8("SendInitAckL - Entry"));
       
  1263 
       
  1264 	iPTPIPEventContainer->SetPayloadL(NULL );
       
  1265 	iPTPIPEventContainer->SetUint32L(TPTPIPInitEvtAck::ELength,	iPTPIPEventContainer->Size( ) );
       
  1266 	iPTPIPEventContainer->SetUint32L(TPTPIPInitEvtAck::EType, EPTPIPPacketTypeEventAck );
       
  1267 
       
  1268 	// Send the packet
       
  1269 	iEventHandler->SendInitAck(iPTPIPEventContainer );
       
  1270 
       
  1271 	__FLOG(_L8("SendInitAckL - Exit"));
       
  1272 	}
       
  1273 
       
  1274 /**
       
  1275  Stop the connection. 
       
  1276 
       
  1277  First cancel the command and socket handlers which are controlled by it
       
  1278  and complete any data send of receive commands with error code of abort. 
       
  1279 
       
  1280  Also inform the fw that connection is closed. 
       
  1281 
       
  1282  */
       
  1283 void CPTPIPConnection::StopConnection( )
       
  1284 	{
       
  1285 	__FLOG(_L8("StopConnection - Entry"));
       
  1286 
       
  1287 	if(ConnectionOpen( ) )
       
  1288 		{
       
  1289 		__FLOG(_L8("Stopping socket handlers"));
       
  1290 		iEventHandler->Cancel( );
       
  1291 		iCommandHandler->Cancel( );
       
  1292 		if(iTransactionState == EDataIToRPhase )
       
  1293 			{
       
  1294 			__FLOG(_L8("Aborting active I to R data phase"));
       
  1295 			TRAPD(err, BoundProtocolLayer().ReceiveDataCompleteL(KErrAbort, *iPTPIPDataContainer->Payload(), iMTPRequest));
       
  1296 			UNUSED_VAR(err);
       
  1297 			}
       
  1298 		else
       
  1299 			if(iTransactionState == EDataRToIPhase )
       
  1300 				{
       
  1301 				__FLOG(_L8("Aborting active R to I data phase"));
       
  1302 				TRAPD(err, BoundProtocolLayer().SendDataCompleteL(KErrAbort, *iPTPIPDataContainer->Payload(), iMTPRequest))	;
       
  1303 				UNUSED_VAR(err);
       
  1304 				}
       
  1305 
       
  1306 		__FLOG(_L8("Notifying protocol layer connection closed"));
       
  1307 		iConnectionMgr->ConnectionClosed(*this );
       
  1308 		SetTransactionPhase(EUndefined );
       
  1309 		SetConnectionState(EIdle );
       
  1310 		}
       
  1311 
       
  1312 	__FLOG(_L8("StopConnection - Exit"));
       
  1313 	}
       
  1314 
       
  1315 /**
       
  1316  * Invoked by the SocketHandler when there is an error.
       
  1317  */
       
  1318 #ifdef __FLOG_ACTIVE
       
  1319 void CPTPIPConnection::HandleError(TInt aError)
       
  1320 #else
       
  1321 void CPTPIPConnection::HandleError(TInt /*aError*/)
       
  1322 #endif
       
  1323 	{
       
  1324 	__FLOG_VA((_L8("SocketHandler received an error=%d, stopping connection.)"),aError));
       
  1325 	StopConnection();
       
  1326 	}
       
  1327 
       
  1328 /**
       
  1329  Used to trigger the RunL, by first setting itself to active and 
       
  1330  then simulating a fake asynchronous service provider which will complete us 
       
  1331  with a completion code.
       
  1332  */
       
  1333 void CPTPIPConnection::CompleteSelf(TInt aCompletionCode )
       
  1334 	{
       
  1335 	// Setting ourselves active to wait to be done by ASP.
       
  1336 	SetActive( );
       
  1337 
       
  1338 	// Simulating a fake ASP which completes us.
       
  1339 	TRequestStatus* stat = &iStatus;
       
  1340 	User::RequestComplete(stat, aCompletionCode );
       
  1341 	}
       
  1342 
       
  1343 /**
       
  1344  Setter for transaction phase(request, dataItoR, dataRtoI, or response)
       
  1345  */
       
  1346 void CPTPIPConnection::SetTransactionPhase(TMTPTransactionPhase aPhase )
       
  1347 	{
       
  1348 	__FLOG(_L8("SetTransactionPhase - Entry"));
       
  1349 	iTransactionState = aPhase;
       
  1350 	__FLOG_VA((_L8("Transaction Phase set to 0x%08X"), iTransactionState));
       
  1351 	__FLOG(_L8("SetTransactionPhase - Exit"));
       
  1352 	}
       
  1353 
       
  1354 /**
       
  1355  Setter for connection state,( initialising, send data, etc)
       
  1356  */
       
  1357 void CPTPIPConnection::SetConnectionState(TConnectionState aState )
       
  1358 	{
       
  1359 	__FLOG(_L8("SetConnectionState - Entry"));
       
  1360 	iState = aState;
       
  1361 	__FLOG_VA((_L8("Connection state set to 0x%08X"), iState));
       
  1362 	__FLOG(_L8("SetConnectionState - Exit"));
       
  1363 	}
       
  1364 
       
  1365 /**
       
  1366  Getter 
       
  1367  */
       
  1368 TBool CPTPIPConnection::ConnectionOpen( ) const
       
  1369 	{
       
  1370 	return((iState >= EInitialising) && (iState <= EDataSendFinished));
       
  1371 	}
       
  1372 
       
  1373 /**
       
  1374  Getter for the command container, 
       
  1375  */
       
  1376 CPTPIPGenericContainer* CPTPIPConnection::CommandContainer( )
       
  1377 	{
       
  1378 	return iPTPIPCommandContainer;
       
  1379 	}
       
  1380 
       
  1381 /**
       
  1382  Getter for the event container
       
  1383  */
       
  1384 CPTPIPGenericContainer* CPTPIPConnection::EventContainer( )
       
  1385 	{
       
  1386 	return iPTPIPEventContainer;
       
  1387 	}
       
  1388 
       
  1389 /**
       
  1390  Getter for the data container
       
  1391  */
       
  1392 CPTPIPDataContainer* CPTPIPConnection::DataContainer( )
       
  1393 	{
       
  1394 	return iPTPIPDataContainer;
       
  1395 	}
       
  1396 
       
  1397 /**
       
  1398  Getter for the transaction phase: request, dataItoR, dataRtoI or response
       
  1399  */
       
  1400 TMTPTransactionPhase CPTPIPConnection::TransactionPhase( ) const
       
  1401 	{
       
  1402 	return iTransactionState;
       
  1403 	}
       
  1404 
       
  1405 /**
       
  1406  Takes the 4 bytes from the chunk(iReceiveChunkData) and return 
       
  1407  whether the type is a valid request, cancel or probe packet. 
       
  1408 
       
  1409  @return: The container type, in case of an unknown type, 
       
  1410  the value 0 ( undefined) is returned
       
  1411 
       
  1412  */
       
  1413 TUint32 CPTPIPConnection::ValidateAndSetCommandPayloadL( )
       
  1414 	{
       
  1415 	__FLOG(_L8("ValidateAndSetCommandPayload - Entry"));
       
  1416 
       
  1417 	TUint32 containerType = CommandContainer()->Uint32L(CPTPIPGenericContainer::EPacketType );
       
  1418 	
       
  1419 	__FLOG_VA((_L8("PTP packet type  = %d, adjust payload accordingly"), containerType));
       
  1420 
       
  1421 	switch(containerType )
       
  1422 		{
       
  1423 		case EPTPIPPacketTypeOperationRequest:
       
  1424 			if (!ValidateTransactionPhase(ERequestPhase ))
       
  1425 				{
       
  1426 				__FLOG(_L8("PTPIP ERROR: Request data unexpected in this phase, setting type to undefined"));
       
  1427 				containerType = EPTPIPPacketTypeUndefined;
       
  1428 				}
       
  1429 			// Nothing to do , the payload is already set.  In case this is unexpected, 
       
  1430 			//then the validate function will close the connection. 
       
  1431 			break;
       
  1432 
       
  1433 		case EPTPIPPacketTypeStartData:
       
  1434 			if (!ValidateTransactionPhase(EDataIToRPhase ))
       
  1435 				{
       
  1436 				__FLOG(_L8("PTPIP ERROR: Start data unexpected in this phase, setting type to undefined"));
       
  1437 				containerType = EPTPIPPacketTypeUndefined;
       
  1438 				}
       
  1439 			// Nothing to do , the payload is already set.  In case this is unexpected, 
       
  1440 			//then the validate function will close the connection. 
       
  1441 			break;
       
  1442 
       
  1443 		case EPTPIPPacketTypeCancel:
       
  1444 			// This can come on the command channel either during the data phase or 
       
  1445 			// during the command phase. 
       
  1446 			// In data phase, no payload is needed on the data container. 
       
  1447 			if (EDataIToRPhase == iTransactionState)
       
  1448 				{
       
  1449 				DataContainer()->SetPayloadL(NULL);
       
  1450 				}
       
  1451 			else 
       
  1452 				{
       
  1453 				CommandContainer()->SetPayloadL(&iPTPIPCommandCancelPayload );
       
  1454 				}
       
  1455 			break;
       
  1456 			
       
  1457 		case EPTPIPPacketTypeOperationResponse:
       
  1458 			__FLOG(_L8("PTPIP ERROR: Response not expected from the initiator, setting type to undefined"));
       
  1459 			containerType = EPTPIPPacketTypeUndefined;			
       
  1460 			// As per the protocol, the initiator cannot send a response, 
       
  1461 			// only the responder( here device)  will create a response, 
       
  1462 			// if this is recieved it is an erro
       
  1463 			break;
       
  1464 			
       
  1465 
       
  1466 		default:
       
  1467 			__FLOG_VA((_L8("PTPIP ERROR: Invalid packet type received %d )"), containerType));
       
  1468 			containerType = EPTPIPPacketTypeUndefined;
       
  1469 			break;
       
  1470 		}
       
  1471 
       
  1472 	__FLOG(_L8("ValidateAndSetCommandPayload - Exit"));
       
  1473 	return containerType;
       
  1474 	}
       
  1475 
       
  1476 /**
       
  1477  Takes the 4 bytes from the chunk(iReceiveChunkData) and return 
       
  1478  whether the type is a valid event packet. 
       
  1479 
       
  1480  @return: The container type, in case of an unknown type, 
       
  1481  the value 0 ( undefined) is returned
       
  1482 
       
  1483  */
       
  1484 TUint32 CPTPIPConnection::ValidateDataPacketL( )
       
  1485 	{
       
  1486 	__FLOG(_L8("ValidateDataPacketL - Entry"));
       
  1487 
       
  1488 	TUint32 containerType = DataContainer()->Uint32L(CPTPIPDataContainer::EPacketType );
       
  1489 	__FLOG_VA((_L8("PTP data packet type  = %d, "), containerType));
       
  1490 
       
  1491 	switch(containerType )
       
  1492 		{
       
  1493 		case EPTPIPPacketTypeData:
       
  1494 		case EPTPIPPacketTypeEndData:
       
  1495 			if (!ValidateTransactionPhase(EDataIToRPhase ))
       
  1496 				{
       
  1497 				__FLOG(_L8("PTPIP ERROR: Receiving data unexpected in this phase, setting type to undefined"));
       
  1498 				containerType = EPTPIPPacketTypeUndefined;
       
  1499 				}
       
  1500 			break;
       
  1501 
       
  1502 		default:
       
  1503 			__FLOG_VA((_L8("PTPIP ERROR: Unexpected or Invalid packet type received while expecting data packet%d )"), containerType));
       
  1504 			containerType = EPTPIPPacketTypeUndefined;
       
  1505 			break;
       
  1506 		}
       
  1507 
       
  1508 	__FLOG(_L8("ValidateDataPacket - Exit"));
       
  1509 	return containerType;
       
  1510 	}
       
  1511 
       
  1512 /**
       
  1513  Takes the 4 bytes from the chunk(iReceiveChunkData) and return 
       
  1514  whether the type is a valid event packet. 
       
  1515 
       
  1516  @return: The container type, in case of an unknown type, 
       
  1517  the value 0 ( undefined) is returned
       
  1518 
       
  1519  */
       
  1520 TUint32 CPTPIPConnection::ValidateAndSetEventPayloadL( )
       
  1521 	{
       
  1522 	__FLOG(_L8("ValidateAndSetEventPayload - Entry"));
       
  1523 
       
  1524 	TUint32 containerType = EventContainer()->Uint32L(CPTPIPGenericContainer::EPacketType );
       
  1525 	__FLOG_VA((_L8("PTP event packet type  = %d, adjust payload accordingly"), containerType));
       
  1526 
       
  1527 	switch(containerType )
       
  1528 		{
       
  1529 		case EPTPIPPacketTypeProbeRequest:
       
  1530 			EventContainer()->SetPayloadL(NULL );
       
  1531 			break;
       
  1532 
       
  1533 		case EPTPIPPacketTypeCancel:
       
  1534 			EventContainer()->SetPayloadL(&iPTPIPEventCancelPayload );
       
  1535 			break;
       
  1536 
       
  1537 		case EPTPIPPacketTypeEvent:
       
  1538 			EventContainer()->SetPayloadL(&iPTPIPEventPayload );
       
  1539 			break;
       
  1540 
       
  1541 		default:
       
  1542 			__FLOG_VA((_L8("PTPIP ERROR: Invalid packet type received %d )"), containerType));
       
  1543 			containerType = EPTPIPPacketTypeUndefined;
       
  1544 			break;
       
  1545 		}
       
  1546 
       
  1547 	__FLOG(_L8("ValidateAndSetEventPayload - Exit"));
       
  1548 	return containerType;
       
  1549 	}
       
  1550 
       
  1551 /**
       
  1552  Processes bulk transfer request transaction state checking. If the transaction 
       
  1553  state is invalid, then the connection is shutdown.
       
  1554  @return ETrue if the control request completion status was abnormal, otherwise
       
  1555  EFalse.
       
  1556  */
       
  1557 TBool CPTPIPConnection::ValidateTransactionPhase(
       
  1558 		TMTPTransactionPhase aExpectedTransactionState )
       
  1559 	{
       
  1560 	__FLOG(_L8("ValidateTransactionPhase - Entry"));
       
  1561 	__FLOG_VA((_L8("transaction state = %d"), iTransactionState));
       
  1562 	TBool valid(iTransactionState == aExpectedTransactionState);
       
  1563 	if(!valid )
       
  1564 		{
       
  1565 		// Invalid transaction state, close the connection.
       
  1566 		__FLOG_VA((_L8("PTPIP ERROR: invalid transaction state, current = %d, expected = %d"), iTransactionState, aExpectedTransactionState));
       
  1567 		CloseConnection( );
       
  1568 		}
       
  1569 	__FLOG(_L8("ValidateTransactionPhase - Exit"));
       
  1570 	return valid;
       
  1571 	}
       
  1572 
       
  1573 /**
       
  1574  Convert the TCP errors, the disconnect should be reported as an abort, 
       
  1575  since that is what the MTP frameword expects.
       
  1576  */
       
  1577 TBool CPTPIPConnection::HandleTCPError(TInt& aError )
       
  1578 	{
       
  1579 	__FLOG(_L8("TCPErrorHandled - Entry"));
       
  1580 	TInt ret(EFalse);
       
  1581 	if(aError == KErrDisconnected || aError == KErrEof)
       
  1582 		{
       
  1583 		aError = KErrAbort;
       
  1584 		CloseConnection( );
       
  1585 		ret = ETrue;
       
  1586 		}
       
  1587 	__FLOG(_L8("TCPErrorHandled - Exit"));
       
  1588 	return ret;
       
  1589 	}
       
  1590 
       
  1591 void CPTPIPConnection::SetDataTypeInDataContainerL(TPTPIPPacketTypeCode aType )
       
  1592 	{
       
  1593 	iPTPIPDataContainer->SetUint32L(CPTPIPDataContainer::EPacketType, aType );
       
  1594 	}
       
  1595 
       
  1596 
       
  1597