mtptransports/mtpptpiptransport/ptpipplugin/inc/cptpipconnection.h
changeset 0 d0791faffa3f
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 
       
    21 #ifndef CPTPIPCONNECTION_H_
       
    22 #define CPTPIPCONNECTION_H_
       
    23 
       
    24 #include <mtp/tmtptyperequest.h>
       
    25 #include <mtp/tmtptypeevent.h>
       
    26 #include <mtp/mtpdataproviderapitypes.h>
       
    27 #include <mtp/tmtptypenull.h>
       
    28 
       
    29 
       
    30 #include "mmtpconnectionmgr.h"
       
    31 #include "mmtpconnectionprotocol.h"
       
    32 
       
    33 
       
    34 #include "mmtptransportconnection.h"
       
    35 #include "ptpipdatatypes.h"
       
    36 #include "cptpipgenericcontainer.h"
       
    37 #include "cptpipdatacontainer.h"
       
    38 #include "tptpiprequestpayload.h"
       
    39 #include "tptpipresponsepayload.h"
       
    40 #include "tptpipstartdatapayload.h"
       
    41 #include "tptpipinitevtack.h"
       
    42 
       
    43 #include "mtpdebug.h"
       
    44 #include "ptpipprotocolconstants.h"
       
    45 
       
    46 
       
    47 
       
    48 class CPTPIPCommandHandler;
       
    49 class CPTPIPEventHandler;
       
    50 class MMTPConnectionProtocol;
       
    51 class MMTPConnectionMgr;
       
    52 
       
    53 /**
       
    54 Implements the MTP USB device class connection protocol and transport layer 
       
    55 interface.
       
    56 
       
    57 This is the class implementing the main API functions exposed to the framework layer
       
    58 for exchange of data with the transport layer.
       
    59 
       
    60 It also has the communication protocol which exposes the SPI / observer functions 
       
    61 exposed by the framework layer , which is used by the transport to communicate with 
       
    62 the framework. 
       
    63 
       
    64 It implements a state machine which keeps track of whether we are sending data , 
       
    65 expecting response etc. 
       
    66 
       
    67 The error handling strategy is as follows:
       
    68 If unexpected data is received on the event channel , we close the connection.
       
    69 If unexpected data is received on the command channel the we close the connection.
       
    70 If the internal state is invalid, then a panic is raised. 
       
    71 Any errors received, are passed up to the MTP fw, except for TCP disconnection
       
    72 which is converted to KErrAbort. 
       
    73 
       
    74 @internalComponent
       
    75 */
       
    76 class CPTPIPConnection : public CActive,
       
    77 						 public MMTPTransportConnection
       
    78 {
       
    79 public :
       
    80 
       
    81 	enum TConnectionState
       
    82 		{
       
    83 		EIdle                   = 0x00000000,
       
    84 		EInitialising           = 0x00000001,
       
    85 		EInitialisationComplete = 0x00000002,
       
    86 		EStartListening         = 0x00000003,
       
    87 		EDataSendInProgress     = 0x00000004,
       
    88 		EDataSendFinished       = 0x00000007,
       
    89 		ECancelled				= 0x00000008
       
    90 		};
       
    91 
       
    92 	
       
    93 	enum TCancelState
       
    94 		{
       
    95 		ECancelNotReceived        = 0x00000000,
       
    96 		
       
    97 		ECancelCmdReceived        = 0x10000001,
       
    98 		ECancelCmdHandleInProgress= 0x10000002,
       
    99 		ECancelCmdHandled         = 0x10000003,
       
   100 		
       
   101 		ECancelEvtReceived        = 0x20000001,
       
   102 		ECancelEvtHandled         = 0x20000003,
       
   103 		
       
   104 		ECancelChannel            = 0xF0000000
       
   105 		};
       
   106 
       
   107 
       
   108 	static CPTPIPConnection* NewL(MMTPConnectionMgr& aConnectionMgr);
       
   109 	~CPTPIPConnection();
       
   110 	
       
   111 
       
   112 public : // from MMTPTransportConnection
       
   113 	virtual void BindL(MMTPConnectionProtocol& aProtocol) ;
       
   114 	virtual MMTPConnectionProtocol& BoundProtocolLayer(); 
       
   115 	virtual void CloseConnection() ;
       
   116 	virtual void ReceiveDataL(MMTPType& aData, const TMTPTypeRequest& aRequest) ;
       
   117 	virtual void ReceiveDataCancelL(const TMTPTypeRequest& aRequest) ;
       
   118 	virtual void SendDataL(const MMTPType& aData, const TMTPTypeRequest& aRequest) ; 
       
   119 	virtual void SendDataCancelL(const TMTPTypeRequest& aRequest) ;
       
   120 	virtual void SendEventL(const TMTPTypeEvent& aEvent) ;
       
   121 	virtual void SendResponseL(const TMTPTypeResponse& aResponse, const TMTPTypeRequest& aRequest) ;
       
   122 	virtual void TransactionCompleteL(const TMTPTypeRequest& aRequest) ;
       
   123 	virtual void Unbind(MMTPConnectionProtocol& aProtocol) ;
       
   124 	virtual TAny* GetExtendedInterface(TUid aInterfaceUid) ; 
       
   125 	virtual TUint GetImplementationUid();
       
   126 	
       
   127 public : //from CActive
       
   128 	void DoCancel();
       
   129 	void RunL();
       
   130 	TInt RunError(TInt aError);
       
   131 	
       
   132 public : // Other functions
       
   133 	void GetCancelPayload();
       
   134 	void CompleteSelf(TInt aCompletionCode);
       
   135 	void SetConnectionState(TConnectionState);
       
   136 	
       
   137 	// Receiving functions:
       
   138 	void ReceiveCommandChannelCompleteL(TInt aError, MMTPType& aSource);
       
   139 	
       
   140 	void ReceiveEventCompleteL(TInt aError, MMTPType& aSource);
       
   141 	void ReceiveCommandCompleteL(TInt aError);
       
   142 	
       
   143 	void ReceiveCommandDataL(MMTPType& aData) ;
       
   144 	void ReceiveCommandDataCompleteL(TInt aError);
       
   145 	
       
   146 	
       
   147 
       
   148 	// Sending funtions
       
   149 	void SendCommandChannelCompleteL(TInt aError, const MMTPType& aSource);
       
   150 	
       
   151 	void SendResponseCompleteL(TInt aError, MMTPType& aSource);
       
   152 	void SendEventCompleteL(TInt aError, const MMTPType& aSource);
       
   153 
       
   154 	void SendStartDataPacketL();
       
   155 	void SendDataPacketL();
       
   156 	void SendCommandCompleteL(TInt aError);
       
   157 	void SendCommandDataCompleteL(TInt aError);
       
   158 
       
   159 	
       
   160 	CPTPIPGenericContainer* CommandContainer();
       
   161 	CPTPIPGenericContainer* EventContainer();
       
   162 	CPTPIPDataContainer* DataContainer();
       
   163 	TUint32 ValidateAndSetCommandPayloadL();
       
   164 	TUint32 ValidateAndSetEventPayloadL();
       
   165 	TUint32 ValidateDataPacketL();
       
   166 	TMTPTransactionPhase TransactionPhase() const;
       
   167 	void HandleError(TInt aError);
       
   168 	void SetDataTypeInDataContainerL(TPTPIPPacketTypeCode aType);
       
   169 	TBool ConnectionOpen() const;
       
   170 
       
   171 private:
       
   172 	CPTPIPConnection(MMTPConnectionMgr& aConnectionMgr);
       
   173 	void ConstructL();	
       
   174 
       
   175 	void TransferSocketsL();
       
   176 
       
   177 	void InitiateCommandRequestPhaseL();
       
   178 	void InitiateEventRequestPhaseL();
       
   179 	
       
   180 	void SendInitAckL();
       
   181 	
       
   182 	void DoSendDataL();
       
   183 	void DoReceiveDataL();
       
   184 	
       
   185 	// Cancel handling functions
       
   186 	void HandleEventCancelL();
       
   187 	void HandleCommandCancelL(TUint32 aTransId);
       
   188 	void HandleCommandCancelCompleteL();
       
   189 	void SendCancelToFrameworkL(TUint32 aTransId);
       
   190 	void SendCancelResponseL(TUint32 aTransId);
       
   191 	void HandleCancelDuringSendL();
       
   192 	
       
   193 	void SetTransactionPhase(TMTPTransactionPhase);
       
   194 	TBool ValidateTransactionPhase(TMTPTransactionPhase aExpectedTransactionState);
       
   195 	TBool HandleTCPError(TInt& aError);
       
   196 	void StopConnection();
       
   197 	
       
   198 	void SetNULLPacketL();
       
   199 	
       
   200 private: // Owned
       
   201 
       
   202 	/**
       
   203 	The current state of the MTP transaction, ( request, response, data phase) 
       
   204 	*/
       
   205 	TMTPTransactionPhase        iTransactionState;
       
   206 	
       
   207 	/**
       
   208 	Current state of the Connection, ( whether its initialisting, transferring data, listening)
       
   209 	*/
       
   210 	TInt                        iState;
       
   211 	
       
   212 	/**
       
   213 	Flag, which is turned on when the cancel operation is received on the command channel. 
       
   214 	*/
       
   215 	TCancelState                iCancelOnCommandState;
       
   216 	
       
   217 	/**
       
   218 	Flag, which is turned on when the cancel operation is received on the event channel. 
       
   219 	*/
       
   220 	TCancelState                iCancelOnEventState;
       
   221 	
       
   222 	/**
       
   223 	During the sending of data this keeps track of the total data to send
       
   224 	*/
       
   225 	TUint64                     iTotalDataLen;
       
   226 
       
   227 	/**
       
   228 	Command handler to send the data via the command socket.
       
   229 	*/
       
   230 	CPTPIPCommandHandler*       iCommandHandler;
       
   231 	
       
   232 	/**
       
   233 	EventHandler to send the data via the event socket.
       
   234 	*/
       
   235 	CPTPIPEventHandler*         iEventHandler;
       
   236 	
       
   237 	// Command Container & payloads //
       
   238 	/**
       
   239 	Command Container, its payload can be request, response, start data or cancel. 
       
   240 	*/
       
   241 	CPTPIPGenericContainer*     iPTPIPCommandContainer;
       
   242 	
       
   243 	/**
       
   244 	Command Request Paramenter Payload
       
   245 	4 - data phase
       
   246 	2 - op code
       
   247 	4 - tran id
       
   248 	20 - 5 params
       
   249 	*/
       
   250 	TPTPIPTypeRequestPayload    iPTPIPRequestPayload;
       
   251 	
       
   252 	/**
       
   253 	CommandData Response Parameter Payload
       
   254 	2 - res code
       
   255 	4 tran id
       
   256 	20 - 5 params
       
   257 	*/
       
   258 	TPTPIPTypeResponsePayload   iPTPIPResponsePayload;
       
   259 	
       
   260 	/**
       
   261 	StartData parameter payload
       
   262 	4 - tran id
       
   263 	8 - total length
       
   264 	*/
       
   265 	TPTPIPTypeStartDataPayload  iPTPIPStartDataPayload;
       
   266 	
       
   267 	/**
       
   268 	Cancel data Parameter payload
       
   269 	4 trans id
       
   270 	*/
       
   271 	TMTPTypeInt32               iPTPIPCommandCancelPayload;
       
   272 	
       
   273 	
       
   274 	// Event Container & payloads //
       
   275 	/**
       
   276 	Event Container, its payload can be event, cancel or probe (ie no payload) 
       
   277 	*/
       
   278 	CPTPIPGenericContainer*     iPTPIPEventContainer;
       
   279 	
       
   280 	/**
       
   281 	Event Payload 
       
   282 	2 - res code
       
   283 	4 tran id
       
   284 	20 - 5 params
       
   285 	*/
       
   286 	TPTPIPTypeResponsePayload   iPTPIPEventPayload;
       
   287 
       
   288 	/**
       
   289 	Event Payload 
       
   290 	*/
       
   291 	TMTPTypeUint32              iPTPIPEventCancelPayload;
       
   292 
       
   293 	// Data Container ( payload comes from mtp f/w) //
       
   294 	/**
       
   295 	4 - tran id
       
   296 	? - payload, given by the framework. 
       
   297 	*/
       
   298 	CPTPIPDataContainer*        iPTPIPDataContainer;
       
   299 
       
   300 	/** 
       
   301     The current active MTP SessionID, set while sending response to open session.
       
   302     */
       
   303     TUint32                     iMTPSessionId;
       
   304     
       
   305     /**
       
   306     The active MTP operation request dataset buffer.
       
   307     */
       
   308     TMTPTypeRequest             iMTPRequest;
       
   309     
       
   310     /**
       
   311     The MTP event dataset buffer.
       
   312     */
       
   313     TMTPTypeEvent               iMTPEvent;
       
   314 	
       
   315 	/**
       
   316     The total amount of data expected, this is filled from the ptpip start data packet. 
       
   317     */
       
   318     TUint64	                    iTotalRecvData;
       
   319 	
       
   320 	/**
       
   321     The data receved so far. 
       
   322     */
       
   323     TUint64	                    iRecvData;
       
   324     
       
   325 	/**
       
   326 	Data sink for consuming data during error recovery from
       
   327 	a failed ItoR transaction.
       
   328 	*/
       
   329 	TMTPTypeNull                iNull;
       
   330 	
       
   331 	/**
       
   332 	Buffer for reading discarded data into.
       
   333 	*/
       
   334 	RBuf8                       iNullBuffer;
       
   335 	
       
   336     /**
       
   337     FLOGGER debug trace member variable.
       
   338     */
       
   339     __FLOG_DECLARATION_MEMBER;    
       
   340 	
       
   341 private: // Not Owned
       
   342 
       
   343 	/** 
       
   344     The MTP connection manager.
       
   345     */
       
   346     MMTPConnectionMgr*			iConnectionMgr;
       
   347 
       
   348 	/** 
       
   349     The MTP connection protocol layer binding. 
       
   350     This is the SPI/ observer - transport uses it to communicate to the MTP framework.
       
   351     */
       
   352     MMTPConnectionProtocol*		iProtocolLayer;
       
   353 
       
   354 };
       
   355 
       
   356 
       
   357 #endif /*CPTPIPCONNECTION_H_*/