obex/obexprotocol/obexusbtransport/inc/usbconn.h
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 1997-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  @file
       
    18  @internalComponent 
       
    19 */
       
    20 
       
    21 
       
    22 #ifndef __USBCONN_H__
       
    23 #define __USBCONN_H__
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <usb.h>
       
    27 #include <d32usbc.h>
       
    28 #include <obex/transport/obexreaderbase.h>
       
    29 #include <obex/transport/obexwriterbase.h>
       
    30 #include <obex/transport/obexconnector.h>
       
    31 #include <obex/transport/mobextransportnotify.h>
       
    32 #include <obexconstants.h>
       
    33 #include <obexusbtransportinfo.h>
       
    34 #include "ObexUsbTransportController.h"
       
    35 
       
    36 //
       
    37 // LDD name
       
    38 //
       
    39 _LIT(KUsbLddName, "eusbc");
       
    40 
       
    41 // Transmit and receive endpoints
       
    42 // Note that these are the virtual endpoint numbers for the bulk interface and
       
    43 // should be put in the same order in the TUsbcEndpointInfo array in RegInterfacesL
       
    44 const TEndpointNumber KTransmitEndpoint = EEndpoint1;
       
    45 const TEndpointNumber KReceiveEndpoint  = EEndpoint2;
       
    46 
       
    47 // These are taken from section 6.5 of the WMCDC spec.
       
    48 const TUint KObexDescriptorLength = 18;
       
    49 const TUint KObexMinNumEndpoints = 3;
       
    50 const TUint KObexClassNumber = 0x02;
       
    51 const TUint KObexNumInterfaces = 2;
       
    52 const TUint KObexSubClassNumber = 0x0b;
       
    53 const TUint KObexProtocolNumber = 0x0;
       
    54 const TUint KObexTotalEndpoints = 2;
       
    55 
       
    56 const TUint KObexDataClass = 0x0A;
       
    57 const TUint KObexDataSubClass = 0;
       
    58 const TUint KObexAlt0 = 0;
       
    59 const TUint KObexAlt1 = 1;
       
    60 const TUint KObexFunctionalDescriptorLength = 5;
       
    61 const TUint KObexDefaultInterfaceDescriptorLength = 100; //Default value used prior to OS 9.2
       
    62 const TUint KObexInterfaceDescriptorBlockLength = 16;
       
    63 
       
    64 const TUint8 KCdcVersionNumber[2] = { 0x10, 0x01 }; // CDC v1.1
       
    65 const TUint8 KHeaderFunctionalDescriptor = 0x00;
       
    66 const TUint8 KObexFunctionalDescriptor = 0x15;
       
    67 const TUint8 KWmcdcVersionNumber[2] = { 0x00, 0x01 }; // WMCDC v1.0
       
    68 const TUint8 KUnionFunctionalDescriptor = 0x06;
       
    69 
       
    70 const TInt KMaxPacketTypeBulkFS = 64;
       
    71 const TInt KMaxPacketTypeBulkHS = 512;
       
    72 
       
    73 
       
    74 /**
       
    75 USB Active Writer class
       
    76 */
       
    77 NONSHARABLE_CLASS(CObexUsbActiveWriter) : public CObexWriterBase
       
    78 //------------------------------------------------------------------------------------
       
    79 	{
       
    80 public:
       
    81 	static CObexUsbActiveWriter* NewL(MObexTransportNotify& aOwner, RDevUsbcClient& aUsb, TObexConnectionInfo& aInfo, TInt aPacketSize);
       
    82 	~CObexUsbActiveWriter();
       
    83 
       
    84 private:
       
    85 	virtual void DoCancel();
       
    86 	virtual void DoTransfer();
       
    87 
       
    88 private:
       
    89 	CObexUsbActiveWriter(MObexTransportNotify& aOwner, RDevUsbcClient& aUsb, TObexConnectionInfo& aInfo, TInt aPacketSize);
       
    90 
       
    91 private:
       
    92 	RDevUsbcClient& iUsb;       // USB logical device driver
       
    93 	TInt iPacketSize;
       
    94 	};
       
    95 
       
    96 
       
    97 /**
       
    98 USB Active Reader class
       
    99 */
       
   100 NONSHARABLE_CLASS(CObexUsbActiveReader) : public CObexReaderBase
       
   101 //------------------------------------------------------------------------------------
       
   102     {
       
   103 public:
       
   104 	static CObexUsbActiveReader* NewL(MObexTransportNotify& aOwner, RDevUsbcClient& aUsb, TObexConnectionInfo& aInfo);
       
   105 	~CObexUsbActiveReader();
       
   106 
       
   107 private:
       
   108 	virtual void DoCancel();
       
   109 	virtual void DoTransfer();
       
   110 	virtual TInt GetMaxPacketSize();
       
   111 	virtual TInt GetInitialPacketSize();	
       
   112 private:
       
   113 	CObexUsbActiveReader(MObexTransportNotify& aOwner, RDevUsbcClient& aUsb, TObexConnectionInfo& aInfo);
       
   114 private:
       
   115 	RDevUsbcClient& iUsb;       // USB logical device driver
       
   116 	TBool iGotHeader;
       
   117     };
       
   118     
       
   119 
       
   120 /**
       
   121 Used by the USB underlying object to notify changes in the transport link.
       
   122 */
       
   123 NONSHARABLE_CLASS(MObexUsbConnector)
       
   124 //------------------------------------------------------------------------------------
       
   125 	{
       
   126 public:
       
   127 	/**
       
   128 	@internalComponent
       
   129 	Called in response to the USB transport coming up.
       
   130 	*/
       
   131 	virtual void TransportUp() =0;
       
   132 	
       
   133 	/**
       
   134 	@internalComponent
       
   135 	Called in response to the USB transport going down.
       
   136 	@param aErr An error value associated with the transport down event.
       
   137 	*/
       
   138 	virtual void TransportDown(TInt aErr = KErrDisconnected) =0;
       
   139 	
       
   140 	/**
       
   141 	@internalComponent
       
   142 	Called when an endpoint stall has been cleared.
       
   143 	*/
       
   144 	virtual void StallCleared() =0;
       
   145 	};
       
   146 
       
   147 
       
   148 /**
       
   149 Handles the underlying details of a USB connection.  Monitors the state of
       
   150 the transport and notifies owner as transport goes up and down.
       
   151 */
       
   152 NONSHARABLE_CLASS(CObexUsbHandler) : public CActive
       
   153 //------------------------------------------------------------------------------------
       
   154     {
       
   155 public:
       
   156 	virtual ~CObexUsbHandler();
       
   157 
       
   158 	static CObexUsbHandler* NewL(TObexUsbTransportInfo& aUsbTransportInfo,
       
   159 		MObexUsbConnector& aOwner, RDevUsbcClient& aUsb);
       
   160 
       
   161 	void Start();
       
   162 
       
   163 	// Implements CActive
       
   164 	void RunL();
       
   165 	TInt RunError(TInt /*aError*/);
       
   166 
       
   167 private:
       
   168 	CObexUsbHandler(TObexUsbTransportInfo& aUsbProtocolInfo, MObexUsbConnector& aOwner, RDevUsbcClient& aUsb);
       
   169 	void DoCancel();
       
   170 	void ConstructL();
       
   171 	void RegInterfacesL();
       
   172 	void AllocateDma();
       
   173 	static void CleanupReleaseInterface0(TAny* aInterface);
       
   174 
       
   175 private:
       
   176 	TObexUsbTransportInfo& iObexTransportInfo; // Note: Cannot assume this is valid outside of construction phases
       
   177 	MObexUsbConnector& iOwner;
       
   178 
       
   179 	RDevUsbcClient& iUsb;           // USB LDD used for Data Class interface
       
   180 	RDevUsbcClient iUsbComms;		// USB LDD used for Comms Class interface
       
   181 
       
   182 	TUint iUsbState;		    	// Used by RDevUsbcClient::AlternateDeviceStatusNotify()
       
   183 	TBool iTransportUp;             // TRUE when device is configured
       
   184 
       
   185 	// Interface performance configuration parameters
       
   186 	// (for extended USB Client Driver API)
       
   187 	TInt iBandwidthPriority;
       
   188 	TBool iRequestDmaOnOutEndpoint;
       
   189 	TBool iRequestDmaOnInEndpoint;
       
   190     };
       
   191 
       
   192 
       
   193 /**
       
   194 Class to stall then monitor an endpoint and notify owner when the halt state has been cleared.
       
   195 */
       
   196 NONSHARABLE_CLASS(CObexUsbStaller) : public CActive
       
   197 //------------------------------------------------------------------------------------
       
   198 	{
       
   199 public:
       
   200 	static CObexUsbStaller* NewL(RDevUsbcClient& aUsb, MObexUsbConnector& iOwner);
       
   201 	~CObexUsbStaller();
       
   202 	
       
   203 	void Start();
       
   204 	
       
   205 private:
       
   206 	CObexUsbStaller(RDevUsbcClient& aUsb, MObexUsbConnector& iOwner);
       
   207 	
       
   208 	virtual void DoCancel();
       
   209 	virtual TInt RunError(TInt /*aError*/);
       
   210 	virtual void RunL();
       
   211 		
       
   212 private:
       
   213 	RDevUsbcClient& iUsb;
       
   214 	MObexUsbConnector& iOwner;
       
   215 	TUint iEndpointStatus;
       
   216 	};
       
   217 
       
   218 
       
   219 /**
       
   220 Class to support connecting to other USB devices.
       
   221 Server mode is relatively straightforward as this corresponds
       
   222 exactly to how USB works from the device perspective.  Obex client
       
   223 support though requires some 'magic'.
       
   224 */
       
   225 NONSHARABLE_CLASS(CObexUsbConnector) : public CObexConnector, public MObexUsbConnector
       
   226 //------------------------------------------------------------------------------------
       
   227 	{
       
   228 public:
       
   229 	/**
       
   230 	This enum holds the three different states which the connector
       
   231 	can be in.  In DefaultClient mode, the connector is behaving as
       
   232 	a client but can be switched to server behaviour.  Once the mode
       
   233 	has been moved out of DefaultClient (to either Client or Server),
       
   234 	the mode is locked for the lifetime of the object.  Other
       
   235 	connectors can provide different behaviour for other CObexServer
       
   236 	or CObexClient objects.
       
   237 	*/
       
   238 	enum TObexUsbMode
       
   239 		{
       
   240 		EDefaultClient,
       
   241 		EClient,
       
   242 		EServer
       
   243 		};
       
   244 	static CObexUsbConnector* NewL(MObexTransportNotify& aOwner, TObexUsbTransportInfo& aUsbTransportInfo);	
       
   245 	CObexUsbConnector (MObexTransportNotify& aOwner, TObexUsbTransportInfo& aUsbTransportInfo);
       
   246 
       
   247 	~CObexUsbConnector();
       
   248 
       
   249 	virtual void ConstructL();
       
   250 
       
   251 	//Implement MObexUsbConnector interface
       
   252 	virtual void TransportUp();
       
   253 	virtual void TransportDown(TInt aErr = KErrDisconnected);
       
   254 	virtual void StallCleared();	
       
   255 
       
   256 	//Overrides for pure virtuals in CObexConnector
       
   257 	virtual void ConnectL();
       
   258 	virtual void CancelConnect();
       
   259 	virtual void AcceptL();
       
   260 	virtual void CancelAccept();
       
   261 	virtual TBool BringTransportDown();
       
   262 	virtual void SignalTransportError();
       
   263 	
       
   264 	//required to pass the RDevUsbcClient to the activer reader and writer
       
   265 	RDevUsbcClient* TransportObject();
       
   266 	TInt GetTransportPacketSize();
       
   267 
       
   268 private:
       
   269 	void OpenUsbL(TInt aUnit);	
       
   270 	static TInt LinkUp(TAny* aConn);
       
   271 	static TInt NoDevice(TAny* aConn);
       
   272 	void SignalUp();
       
   273 	void SignalDown(TInt aErr);
       
   274 	
       
   275 	void ResetTransportPacketSize();
       
   276 	void SetTransportPacketSize();
       
   277 
       
   278 // Data
       
   279 private:
       
   280 	RDevUsbcClient iUsb;	// USB logical device driver
       
   281 							// Note: This is the handle used for reads and writes.
       
   282 	TObexUsbTransportInfo& iObexTransportInfo;	// Reference to protocol info to pass on to CObexUsbHandler
       
   283 												// Note: Cannot assume this is valid outside of construction phases
       
   284 	TObexUsbMode iMode;
       
   285 	
       
   286 	CAsyncCallBack* iCallback;
       
   287 	TBool iTransportLinkAvailable;
       
   288 	TBool iConnectRequested;
       
   289 	TBool iWaitingForLink;
       
   290 	CObexUsbHandler* iHandler;
       
   291 	CObexUsbStaller* iStaller;
       
   292 	
       
   293 	TInt iPacketSize;
       
   294 	};
       
   295 
       
   296 
       
   297 #endif /* __USBCONN_H__ */