usbclasses/usbphoneasmodem/classimplementation/mscfileserver/inc/bulkonlytransport.h
changeset 35 9d8b04ca6939
child 63 ef2686f7597e
equal deleted inserted replaced
34:7858bc6ead78 35:9d8b04ca6939
       
     1 // Copyright (c) 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: Class declaration for CBulkOnlyTransport.
       
    14 // 
       
    15 // 
       
    16 
       
    17 #ifndef BULKONLYTRANSPORT_H
       
    18 #define BULKONLYTRANSPORT_H
       
    19 
       
    20 #include <e32std.h> 
       
    21 
       
    22 #include <d32usbc.h>
       
    23 #include "protocol.h"  
       
    24 #include "mscfilecontroller.h"
       
    25 
       
    26 static const TInt KCbwLength = 31;
       
    27 
       
    28 // for control endpoint 
       
    29 static const TUint KRequestHdrSize = 8;
       
    30 
       
    31 
       
    32 /** size of buffer for command padding */
       
    33 static const TUint KBOTMaxBufSize 		= 1024; 
       
    34 
       
    35 /**
       
    36 Represent Endpoint0 request
       
    37 */
       
    38 class TUsbRequestHdr
       
    39 	{
       
    40 public:
       
    41 	enum TEp0Request
       
    42 		{
       
    43 		EReqGetMaxLun = 0xFE,
       
    44 		EReqReset	  = 0xFF
       
    45 		};
       
    46 public:
       
    47 	TInt Decode(const TDesC8& aBuffer);
       
    48 	TBool IsDataResponseRequired() const;
       
    49 
       
    50 public:
       
    51 	TUint8	iRequestType;
       
    52 	TEp0Request	iRequest;
       
    53 	TUint16 iValue;
       
    54 	TUint16 iIndex;
       
    55 	TUint16 iLength;
       
    56 
       
    57 
       
    58 	};
       
    59 
       
    60 
       
    61 //---------------------------------------
       
    62 class CBulkOnlyTransport;  //forward declaration
       
    63 
       
    64 
       
    65 
       
    66 //
       
    67 // --- class CActiveDeviceStateNotifier -----------------------------
       
    68 //
       
    69 
       
    70 class CActiveDeviceStateNotifier : public CActive
       
    71 	{
       
    72 public:
       
    73 	// Construction
       
    74 	static CActiveDeviceStateNotifier* NewL(CBulkOnlyTransport& aParent);
       
    75 
       
    76 	// Destruction
       
    77 	~CActiveDeviceStateNotifier();
       
    78 
       
    79 	void Activate();
       
    80 
       
    81 private:
       
    82 	// Construction
       
    83 	CActiveDeviceStateNotifier(CBulkOnlyTransport& aParent);
       
    84 	void ConstructL();
       
    85 
       
    86 	// Cancel request.
       
    87 	// Defined as pure virtual by CActive;
       
    88 	// implementation provided by this class.
       
    89 	virtual void DoCancel();
       
    90 
       
    91 	// Service completed request.
       
    92 	// Defined as pure virtual by CActive;
       
    93 	// implementation provided by this class,
       
    94 	virtual void RunL();
       
    95 
       
    96 private:
       
    97 	CBulkOnlyTransport& iParent;
       
    98 	TUint iDeviceState;
       
    99 	TUint iOldDeviceState;
       
   100 	
       
   101 	};
       
   102 //=====================
       
   103 
       
   104 
       
   105 
       
   106 
       
   107 /**
       
   108 Represent session with control endpoint (Ep0).
       
   109 handles the control interface, and responds to the class specific commands (RESET and GET_MAX_LUN).  
       
   110 */
       
   111 class CControlInterface : public CActive
       
   112 	{
       
   113 
       
   114 public:
       
   115 	enum TControlState
       
   116 		{
       
   117 		ENone,
       
   118 		EReadEp0Data,
       
   119 		ESendMaxLun
       
   120 
       
   121 		};
       
   122 
       
   123 public:
       
   124 	
       
   125 	static CControlInterface* NewL(CBulkOnlyTransport& aParent);
       
   126 	
       
   127 	~CControlInterface();
       
   128 	TInt Start();
       
   129 	void Stop();
       
   130 	virtual void RunL();
       
   131 	virtual void DoCancel();
       
   132 			
       
   133 
       
   134 private:
       
   135 	CControlInterface(CBulkOnlyTransport& aParent);
       
   136 	void ConstructL();
       
   137 	TInt ReadEp0Data();
       
   138 	void DecodeEp0Data();
       
   139 
       
   140 private:
       
   141 	/** Buffer for request data*/
       
   142 	TBuf8<KRequestHdrSize> iData;
       
   143 	TUsbRequestHdr iRequestHeader;
       
   144 	/** reference to the  CBulkOnlyTransport*/
       
   145 	CBulkOnlyTransport& iParent;
       
   146 	/** represent carrent state for state mashine */
       
   147 	TControlState iCurrentState;
       
   148 	};
       
   149 
       
   150 //----------------------
       
   151 /** handles the data transport and communications with the SCSI protocol */
       
   152 class CBulkOnlyTransport : public CActive, public MTransportBase
       
   153 	{
       
   154 public:
       
   155 	enum TCswStatus
       
   156 		{
       
   157 		ECommandPassed 	= 0,
       
   158 		ECommandFailed	= 1,
       
   159 		EPhaseError		= 2
       
   160 		};
       
   161 	
       
   162 	enum TTransportState
       
   163 		{
       
   164 		ENone,
       
   165 		EWaitForCBW,
       
   166 		ESendingCSW,
       
   167 		EWritingData,
       
   168 		EReadingData,
       
   169         EPermErr
       
   170 		};
       
   171 public:
       
   172 	static CBulkOnlyTransport* NewL(TInt aNumDrives,CMscFileController& aController);
       
   173 
       
   174 	~CBulkOnlyTransport();
       
   175 	void SetupReadData(TPtr8& aData);	
       
   176 	void SetupWriteData(TPtrC8& aData);
       
   177 	TInt Start();
       
   178 	TInt Stop();
       
   179 	void RegisterProtocol(MProtocolBase& aProtocol);
       
   180 	TInt BytesAvailable();
       
   181 
       
   182 	CMscFileController& Controller();
       
   183 	TInt MaxLun();
       
   184 	RDevUsbcClient& Ldd();
       
   185 	TInt HwStart(TBool aDiscard = EFalse);
       
   186 	TInt HwStop();
       
   187 	TInt HwSuspend();
       
   188 	TInt HwResume();
       
   189 	
       
   190 	virtual void RunL();
       
   191 	virtual void DoCancel();
       
   192 
       
   193 private:
       
   194 	CBulkOnlyTransport(TInt aNumDrives,CMscFileController& aController);	
       
   195 	void ConstructL();
       
   196 	TInt SetupConfigurationDescriptor(TBool aUnset = EFalse);
       
   197 	TInt SetupInterfaceDescriptors();	
       
   198 	void ReadCBW();
       
   199 	void DecodeCBW();
       
   200 	TBool CheckCBW();
       
   201 	void SetPermError();
       
   202 	void SendCSW(TUint aTag, TUint aDataResidue, TCswStatus aStatus);
       
   203 	void WriteData(TUint aLength, TBool aZlpRequired = EFalse);
       
   204 	void ReadData(TUint aLength);
       
   205 	void StallEndpointAndWaitForClear(TEndpointNumber aEndpoint);
       
   206 	void ReadAndDiscardData(TInt aBytes);
       
   207 	void Activate(TInt aReason);
       
   208     void PrepareReadFromHost(TUint aHostDataLength);
       
   209     void PrepareWriteToHost(TUint aHostDataLength);
       
   210 	
       
   211 private:
       
   212 	/** maximun logic unit number supported (started from 0) */
       
   213 	TInt iMaxLun;
       
   214 	
       
   215 	CMscFileController& iController;
       
   216 	
       
   217 	CControlInterface* iControlInterface;
       
   218 	MProtocolBase* iProtocol;
       
   219 	RDevUsbcClient iLdd;
       
   220 	TTransportState  iCurrentState;
       
   221 	
       
   222 	/** buffer for Command Block Wrapper */
       
   223 	TBuf8 <KCbwLength+1> iCbwBuf;
       
   224 	
       
   225 	/** Pointer to read buffer (bufer itself provided by protocol) */
       
   226 	TPtr8 iReadBuf; 
       
   227 	
       
   228 	/** Pointer to write buffer (bufer itself provided by protocol) */
       
   229 	TPtrC8 iWriteBuf;
       
   230 
       
   231 	/** Shows how many data was not sent/reseived */
       
   232 	TUint32 iDataResidue;
       
   233 	TUint32 iCbwTag;
       
   234 	TCswStatus iCmdStatus;
       
   235 	
       
   236 	/** Indicate if SCSI protocol has data to sent */
       
   237 	TBool iWriteSetUp;
       
   238 	
       
   239 	/** Indicate if SCSI protocol expected additional data */
       
   240 	TBool iReadSetUp;
       
   241 	
       
   242 	/** Indicate whether SCSI protocol started or not */
       
   243 	
       
   244 	TBool iStarted;
       
   245 	
       
   246 	/** internal buffer for padding */
       
   247 	TBuf8<KBOTMaxBufSize> iBuf;
       
   248 	
       
   249 	TBool iStallAllowed;
       
   250 	
       
   251 	CActiveDeviceStateNotifier* iDeviceStateNotifier;
       
   252 	TBool iInterfaceConfigured;
       
   253 	};
       
   254 	
       
   255 #endif // BULKONLYTRANSPORT_H