userlibandfileserver/fileserver/smassstorage/inc/drivemanager.h
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2004-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 the License "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 // CDriveManager and CMassStorageDrive classes for USB Mass Storage.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #ifndef __DRIVEMANAGER_H__
       
    24 #define __DRIVEMANAGER_H__
       
    25 
       
    26 #include <e32base.h>			// C Class Definitions, Cleanup Stack 
       
    27 #include <e32def.h>				// T Type  Definitions
       
    28 #include <f32fsys.h>
       
    29 #include <e32property.h>
       
    30 #include "usbmsshared.h"		// KUsbMsMaxDrives
       
    31 #include "drivepublisher.h"
       
    32 
       
    33 // Forward declarations
       
    34 class CDriveManager;
       
    35 
       
    36 
       
    37 /**
       
    38 @internalTechnology
       
    39 
       
    40 Along with CDriveManager, this provides an interface between the generic SCSI
       
    41 protocol component and the target drive unit.  The CMassStorageDrive class is 
       
    42 instantiated by the drive manager, and contains a pointer to the associated 
       
    43 CProxyDrive that was registered by the Mass Storage File System.
       
    44 */
       
    45 class CMassStorageDrive : public CBase
       
    46 	{
       
    47 public:
       
    48 	/**
       
    49 	The Drive Mount State Machine.
       
    50 	*/
       
    51 	enum TMountState
       
    52 		{
       
    53 		/**
       
    54 		Unmounted
       
    55 		*/
       
    56 		EDisconnected,
       
    57 		/**
       
    58 		Not mounted, but SCSI started
       
    59 		*/
       
    60 		EConnecting,
       
    61 		/**
       
    62 		Mounted
       
    63 		*/
       
    64 		EConnected,
       
    65 		/**
       
    66 		Not unmounted, but SCSI stopped
       
    67 		*/
       
    68 		EDisconnecting
       
    69 		};
       
    70 
       
    71 	/**
       
    72 	The Drive Media State Machine.
       
    73 	*/
       
    74 	enum TDriveState
       
    75 		{
       
    76 		/**
       
    77 		The media is present and ready for access.
       
    78 		*/
       
    79 		EIdle,
       
    80 		/**
       
    81 		The media is currently being accessed by Mass Storage. 
       
    82 		*/
       
    83 		EActive,
       
    84 		/**
       
    85 		The media is present but is password-protected.
       
    86 		*/
       
    87 		ELocked,
       
    88 		/**
       
    89 		The media is not physically present.
       
    90 		*/
       
    91 		EMediaNotPresent,
       
    92 		/**
       
    93 		No drive.
       
    94 		*/
       
    95 		EErrDisMounted
       
    96 		};
       
    97 
       
    98 	CMassStorageDrive(RCriticalSection& aCritSec,
       
    99 					  RDriveStateChangedPublisher& aDriveStateChangedPublisher);
       
   100 
       
   101 	~CMassStorageDrive();
       
   102 
       
   103 	TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia = ETrue);
       
   104 	TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia = ETrue);
       
   105 	TInt Caps(TLocalDriveCapsV4& aCaps);
       
   106 	inline TMountState MountState() const;
       
   107 	TDriveState DriveState() const;
       
   108 	TDriveState CheckDriveState();
       
   109 #ifndef USB_TRANSFER_PUBLISHER
       
   110 	inline TUint KBytesRead() const;
       
   111 	inline TUint KBytesWritten() const;
       
   112 #endif
       
   113 	inline TInt SetMountDisconnected();
       
   114 	inline TInt SetMountConnecting();
       
   115 	inline TInt SetMountDisconnecting();
       
   116 	inline TInt SetMountConnected();
       
   117 	TInt SetMountConnected(CProxyDrive& aProxyDrive, TBool& aMediaChanged);
       
   118 	TInt SetCritical(TBool aCritical);
       
   119 	TBool IsMediaChanged(TBool aReset=EFalse);
       
   120 	TBool IsWholeMediaAccess();
       
   121 
       
   122 #ifndef USBMSDRIVE_TEST
       
   123  private:
       
   124 #else
       
   125  public:
       
   126 #endif
       
   127 	// Forward declaration
       
   128 	struct CLocalDriveRef;
       
   129 
       
   130 	TInt HandleCriticalError();
       
   131 	TInt ClearCriticalError();
       
   132 	TInt DoCaps(TLocalDriveCapsV4& aCaps);
       
   133 	void SetDriveState(TDriveState aNewState);
       
   134 	TInt SetMountState(TMountState aNewState, CLocalDriveRef* aLocalDrive=NULL);
       
   135 	CProxyDrive& SafeProxyDrive() const;
       
   136 	
       
   137 	/**
       
   138 	A Critical Section, shared by all instances of CMassStorageDrive, used to ensure 
       
   139 	that iMountState and iProxyDrive are changed atomically.
       
   140 	*/
       
   141 	RCriticalSection& iCritSec;
       
   142 	/**
       
   143 	The Drive Mount state machine
       
   144 	*/
       
   145 	TMountState iMountState;
       
   146 	
       
   147 #ifndef USB_TRANSFER_PUBLISHER
       
   148 	/**
       
   149 	Cumulative bytes read
       
   150 	*/
       
   151 	TInt64 iBytesRead;
       
   152 	/**
       
   153 	Cumulative bytes written
       
   154 	*/
       
   155 	TInt64 iBytesWritten;
       
   156 #endif
       
   157 
       
   158 	/**
       
   159 	When Connected, references to CProxyDrive and TBusLocalDrive's Media Changed flag.
       
   160 	*/
       
   161 	CLocalDriveRef* iLocalDrive;
       
   162 	/**
       
   163 	Publisher for media errors.
       
   164 	*/
       
   165 	RDriveMediaErrorPublisher iDriveMediaErrorPublisher;
       
   166 	/**
       
   167 	Reference to publisher for tracking drive state changes.
       
   168 	*/
       
   169 	RDriveStateChangedPublisher& iDriveStateChangedPublisher;
       
   170 	/**
       
   171 	Indicates whether whole media access is permitted. 
       
   172 	*/
       
   173 	TBool iWholeMediaAccess;
       
   174 	};
       
   175 
       
   176 
       
   177 /**
       
   178 @internalTechnology
       
   179 
       
   180 Along with CMassStorageDrive, this provides an interface between the generic SCSI
       
   181 protocol component and the target drive unit.  This package is responsible for 
       
   182 maintaining the list of registered drives.  The owner of the controller registers 
       
   183 each drive it wishes to make available to USB Mass Storage along with an 
       
   184 associated Logical Drive Unit identifier.  The SCSI protocol contains a reference 
       
   185 to the drive manager in order to route the incoming request to a drive.  
       
   186 */
       
   187 class CDriveManager : public CBase
       
   188 	{
       
   189 public:
       
   190 	/**
       
   191 	The Logical Drive Unit Identifiers (LUN) must be in the range 0..7 due to the
       
   192 	fact that the status for all drives is encoded into one 32-bit word.
       
   193 	*/
       
   194 	enum { KAllLuns = 0xff };
       
   195 
       
   196 	static CDriveManager* NewL(TRefDriveMap aDriveMap);
       
   197 	~CDriveManager();
       
   198 
       
   199 	TInt RegisterDrive(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TUint aLun);
       
   200 	TInt DeregisterDrive(TUint aLun);
       
   201 	CMassStorageDrive* Drive(TUint aLun, TInt& aError) const;
       
   202 	TInt Connect(TUint aLun);
       
   203 	TInt Disconnect(TUint aLun);
       
   204 	TBool IsMediaChanged(TUint aLun, TBool aReset=EFalse);
       
   205 	TInt SetCritical(TUint aLun, TBool aCritical);
       
   206 
       
   207 private:
       
   208 	// private default constructor to ensure that NewL is used
       
   209 	CDriveManager(const RArray<TInt>& aDriveMap);
       
   210 	void ConstructL();
       
   211 
       
   212 public:
       
   213 	/**
       
   214 	The array of drives.  The index into the array is a LUN.
       
   215 	*/
       
   216 	TFixedArray<CMassStorageDrive*,KUsbMsMaxDrives> iDrives;
       
   217 
       
   218 private:
       
   219 	/**
       
   220 	For converting LUN to Drive Number.
       
   221 	*/
       
   222 	const RArray<TInt>& iDriveMap;
       
   223 	/**
       
   224 	A resource owned by DriveManager but used by the Drive objects.
       
   225 	*/
       
   226 	RCriticalSection iDriveCritSec;
       
   227 
       
   228 	/**
       
   229 	Publisher for tracking drive state changes.
       
   230 	*/
       
   231 	RDriveStateChangedPublisher* iDriveStateChangedPublisher;
       
   232 	};
       
   233 
       
   234 #include "drivemanager.inl"
       
   235 
       
   236 #endif //__DRIVEMANAGER_H__