os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/cmassstoragefilesystem.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/cmassstoragefilesystem.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,433 @@
     1.4 +// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// CMassStorageFileSystem implementation.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +
    1.28 +#include <e32def.h>
    1.29 +#include <f32ver.h>
    1.30 +#include <f32fsys.h>
    1.31 +
    1.32 +#include "usbmsshared.h"
    1.33 +#include "mstypes.h"
    1.34 +#include "msctypes.h"
    1.35 +#include "mserverprotocol.h"
    1.36 +#include "cusbmassstoragecontroller.h"
    1.37 +#include "cmassstoragefilesystem.h"
    1.38 +#include "cmassstoragemountcb.h"
    1.39 +#include "debug.h"
    1.40 +#include "msdebug.h"
    1.41 +#include "massstorage.h"
    1.42 +
    1.43 +_LIT(KMsFsyName, "MassStorageFileSystem");
    1.44 +_LIT(KMsThreadName, "MassStorageThread");
    1.45 +_LIT(KMsDeadThreadName, "MassStorageDeadThread");
    1.46 +_LIT(KMsFsysSemName, "MassStorageSemaphore");
    1.47 +LOCAL_D const TInt KMsFsyMajorVersionNumber=1;
    1.48 +LOCAL_D const TInt KMsFsyMinorVersionNumber=0;
    1.49 +
    1.50 +CMassStorageFileSystem::CMassStorageFileSystem()
    1.51 +	{
    1.52 +    __MSFNLOG
    1.53 +	}
    1.54 +
    1.55 +CMassStorageFileSystem::~CMassStorageFileSystem()
    1.56 +	{
    1.57 +    __MSFNLOG
    1.58 +	//Kill the controller thread if it exists
    1.59 +	delete iMassStorageController;
    1.60 +	iMediaChangedStatusList.Close();
    1.61 +	RThread thread;
    1.62 +	TInt err = thread.Open(KMsThreadName);
    1.63 +	if (err == KErrNone)
    1.64 +		{
    1.65 +		thread.Kill(1); //Parameter is irrelevant
    1.66 +		}
    1.67 +	thread.Close();
    1.68 +	iDriveMap.Close();
    1.69 +	}
    1.70 +
    1.71 +CMassStorageFileSystem* CMassStorageFileSystem::NewL()
    1.72 +	{
    1.73 +    __MSFNSLOG
    1.74 +	CMassStorageFileSystem*  self = new (ELeave) CMassStorageFileSystem();
    1.75 +	CleanupStack::PushL(self);
    1.76 +	self->ConstructL();
    1.77 +	CleanupStack::Pop(self);
    1.78 +	return self;
    1.79 +	}
    1.80 +
    1.81 +void CMassStorageFileSystem::ConstructL()
    1.82 +	{
    1.83 +    __MSFNLOG
    1.84 +	}
    1.85 +
    1.86 +/**
    1.87 +Set the file system version and name
    1.88 +
    1.89 +@return Any of the standard Symbian error codes.
    1.90 +*/
    1.91 +TInt CMassStorageFileSystem::Install()
    1.92 +	{
    1.93 +    __MSFNLOG
    1.94 +	iVersion=TVersion(KMsFsyMajorVersionNumber, KMsFsyMinorVersionNumber, KF32BuildVersionNumber);
    1.95 +	TInt err = SetName(&KMsFsyName);
    1.96 +	return err;
    1.97 +	}
    1.98 +
    1.99 +TInt CMassStorageFileSystem::Remove()
   1.100 +	{
   1.101 +    __MSFNLOG
   1.102 +	TInt err = KErrNone;
   1.103 +	if (iInstalled)
   1.104 +		{
   1.105 +		// Try connecting to the server to send a shutdown message.
   1.106 +		// - If the class controller has a session in use, this will return KErrInUse
   1.107 +		RUsbMassStorage usbMs;
   1.108 +		err = usbMs.Connect();
   1.109 +		if(err == KErrNone)
   1.110 +			{
   1.111 +			err = usbMs.Shutdown();
   1.112 +			usbMs.Close();
   1.113 +
   1.114 +			if(err == KErrNone)
   1.115 +				{
   1.116 +				User::WaitForRequest(iThreadStat);
   1.117 +				err = iThreadStat.Int();
   1.118 +				}
   1.119 +			else
   1.120 +				{
   1.121 +				__PRINT1(_L("CMassStorageFileSystem::Remove Shutdown Error %d\n"),err);
   1.122 +				}
   1.123 +			}
   1.124 +		else
   1.125 +			{
   1.126 +			__PRINT1(_L("CMassStorageFileSystem::Remove Connect Error %d\n"),err);
   1.127 +			}
   1.128 +		}
   1.129 +	return(err);
   1.130 +	}
   1.131 +
   1.132 +/**
   1.133 +Creates a new Mass Storage mount object.
   1.134 +
   1.135 +@return A new CMassStorageMountCB
   1.136 +@leave KErrNotReady if the Mass Storage controller is not running.
   1.137 +*/
   1.138 +CMountCB* CMassStorageFileSystem::NewMountL() const
   1.139 +	{
   1.140 +    __MSFNSLOG
   1.141 +	if (!iRunning)
   1.142 +		{
   1.143 +		User::Leave(KErrNotReady);
   1.144 +		}
   1.145 +	return CMassStorageMountCB::NewL(iDriveMap);
   1.146 +	}
   1.147 +
   1.148 +/**
   1.149 +Sets the media attributes and type in the aInfo parameter to those of the specified drive.
   1.150 +
   1.151 +@param anInfo TDriveInfo object to store the drive information.
   1.152 +@param aDriveNumber The number of the drive to get the information from.
   1.153 +*/
   1.154 +void CMassStorageFileSystem::DriveInfo(TDriveInfo& aInfo, TInt aDriveNumber) const
   1.155 +	{
   1.156 +    __MSFNSLOG
   1.157 +	TLocalDriveCapsV2Buf caps;
   1.158 +	if (!IsValidLocalDriveMapping(aDriveNumber))
   1.159 +		{
   1.160 +		return;
   1.161 +		}
   1.162 +	GetLocalDrive(aDriveNumber).Caps(caps);
   1.163 +	aInfo.iMediaAtt=caps().iMediaAtt;
   1.164 +    aInfo.iType = ::EMediaNotPresent;  // Media is not available to the file system
   1.165 +	aInfo.iDriveAtt=caps().iDriveAtt;
   1.166 +	}
   1.167 +
   1.168 +/**
   1.169 +Returns a reference to the Mass Storage controller.
   1.170 +
   1.171 +@return Reference to the Mass Storage controller.
   1.172 +*/
   1.173 +CUsbMassStorageController& CMassStorageFileSystem::Controller()
   1.174 +	{
   1.175 +    __MSFNLOG
   1.176 +	return *iMassStorageController;
   1.177 +	}
   1.178 +
   1.179 +/**
   1.180 +Fill iMsDrives with a mapping of lun->drive number for supported mass storage drives
   1.181 +
   1.182 +*/
   1.183 +TInt CMassStorageFileSystem::EnumerateMsDrivesL()
   1.184 +	{
   1.185 +    __MSFNLOG
   1.186 +	iDriveMap.Reset();
   1.187 +	TInt driveCount = 0;
   1.188 +
   1.189 +	TLocalDriveCapsV2Buf caps;
   1.190 +
   1.191 +	for (TInt i = EDriveC; i < KMaxDrives; i++)
   1.192 +		{
   1.193 +		caps.FillZ();
   1.194 +
   1.195 +		if (IsValidLocalDriveMapping(i))
   1.196 +			{
   1.197 +			TInt err = GetLocalDrive(i).Caps(caps);
   1.198 +			TInt locDrvNum = DriveNumberToLocalDriveNumber(i);
   1.199 +			__PRINT2(_L("Caps: err=%d, att=%d\n"), err, caps().iDriveAtt);
   1.200 +
   1.201 +			TBool isRemovable = err==KErrNotReady || (caps().iDriveAtt & KDriveAttRemovable);
   1.202 +			__PRINT2(_L("EnumerateMsDrives: Drive %c: is %sremovable\n"),
   1.203 +							'A'+i-EDriveA,
   1.204 +							isRemovable?_S(""):_S("NOT "));
   1.205 +
   1.206 +			if (isRemovable)
   1.207 +				{
   1.208 +				//
   1.209 +				// STF: Connect to the local drive here.  This gives us the media changed flag, and
   1.210 +				//		our own TBusLocalDrive object for use by the proxy drive and controller.
   1.211 +                //
   1.212 +
   1.213 +                TMediaChangedStatus mediaChanged;
   1.214 +                iMediaChangedStatusList.AppendL(mediaChanged);
   1.215 +
   1.216 +                TBool& mediaChangedRef = iMediaChangedStatusList[driveCount].iMediaChanged;
   1.217 +				TInt err = iMediaChangedStatusList[driveCount].iLocalDrive.Connect(locDrvNum, mediaChangedRef);
   1.218 +				if (err == KErrNone)
   1.219 +					{
   1.220 +					iDriveMap.Append(static_cast <TDriveNumber>(i));
   1.221 +					}
   1.222 +				driveCount++;
   1.223 +				}
   1.224 +			}
   1.225 +		}
   1.226 +
   1.227 +	__PRINT1(_L("EnumerateMsDrives Out, %d MS drives found\n"), driveCount);
   1.228 +	return driveCount;
   1.229 +	}
   1.230 +
   1.231 +TBool CMassStorageFileSystem::IsExtensionSupported() const
   1.232 +	{
   1.233 +    __MSFNSLOG
   1.234 +	return ETrue;
   1.235 +	}
   1.236 +
   1.237 +/**
   1.238 +Creates a TrapCleanup and ActiveScheduler and initializes the Mass Storage controller.
   1.239 +Start the ActiveScheduler.
   1.240 +
   1.241 +@return Any of the standard Symbian error codes.
   1.242 +*/
   1.243 +TInt CMassStorageFileSystem::InitThread()
   1.244 +	{
   1.245 +    __MSFNLOG
   1.246 +
   1.247 +	//Give the thread a name so we can kill it later
   1.248 +	User::RenameThread(KMsThreadName);
   1.249 +
   1.250 +	CTrapCleanup* cleanup = CTrapCleanup::New();
   1.251 +	if (cleanup == NULL)
   1.252 +		{
   1.253 +		return KErrNoMemory;
   1.254 +		}
   1.255 +
   1.256 +	TRAPD(err, InitThreadL());
   1.257 +
   1.258 +	delete cleanup;
   1.259 +
   1.260 +	__PRINT1(_L("CMassStorageFileSystem::InitThread Out, error=%d\n"), err);
   1.261 +	return err;
   1.262 +	}
   1.263 +
   1.264 +TInt CMassStorageFileSystem::InitThreadL()
   1.265 +	{
   1.266 +    __MSFNLOG
   1.267 +	RSemaphore gSemThreadReady;
   1.268 +	gSemThreadReady.OpenGlobal(KMsFsysSemName);
   1.269 +
   1.270 +	// Determine which drives are available for Mass Storage.
   1.271 +	// (this also creates a local TBusLocalDrive for use by the drive controller)
   1.272 +	EnumerateMsDrivesL();
   1.273 +
   1.274 +	CActiveScheduler* sched = new (ELeave) CActiveScheduler;
   1.275 +	if (sched == NULL)
   1.276 +		{
   1.277 +		gSemThreadReady.Signal();
   1.278 +		User::Leave(KErrNoMemory);
   1.279 +		}
   1.280 +	CleanupStack::PushL(sched);
   1.281 +	CActiveScheduler::Install(sched);
   1.282 +
   1.283 +	iMassStorageController = CUsbMassStorageController::NewL();
   1.284 +	if (iMassStorageController == NULL)
   1.285 +		{
   1.286 +		gSemThreadReady.Signal();
   1.287 +		User::Leave(KErrNoMemory);
   1.288 +		}
   1.289 +
   1.290 +	__PRINT(_L("CMassStorageFileSystem::InitThread: Creating Mass Storage Controller\n"));
   1.291 +	TRAPD(err, iMassStorageController->CreateL(iDriveMap));
   1.292 +	if (err != KErrNone)
   1.293 +		{
   1.294 +		gSemThreadReady.Signal();
   1.295 +		CActiveScheduler::Install(NULL);
   1.296 +		User::Leave(err);
   1.297 +		}
   1.298 +
   1.299 +	CleanupStack::Pop(sched);
   1.300 +
   1.301 +	iRunning = ETrue;
   1.302 +	gSemThreadReady.Signal();
   1.303 +	gSemThreadReady.Close();
   1.304 +	CActiveScheduler::Start();
   1.305 +
   1.306 +//========= stop thread ================
   1.307 +	delete iMassStorageController;
   1.308 +	iMassStorageController = NULL;
   1.309 +
   1.310 +	for (TInt i=0; i < iMediaChangedStatusList.Count(); i++)
   1.311 +		{
   1.312 +        iMediaChangedStatusList[i].iLocalDrive.Disconnect();
   1.313 +		}
   1.314 +	iMediaChangedStatusList.Reset();
   1.315 +
   1.316 +	delete sched;
   1.317 +	iRunning = EFalse;
   1.318 +	return KErrNone;
   1.319 +	}
   1.320 +
   1.321 +/**
   1.322 +Not supported in Mass Storage file system.
   1.323 +
   1.324 +@leave KErrNotReady
   1.325 +*/
   1.326 +CFileCB* CMassStorageFileSystem::NewFileL() const
   1.327 +	{
   1.328 +    __MSFNSLOG
   1.329 +	User::Leave(KErrNotReady);
   1.330 +	return NULL;
   1.331 +	}
   1.332 +
   1.333 +/**
   1.334 +Not supported in Mass Storage file system.
   1.335 +
   1.336 +@leave KErrNotReady
   1.337 +*/
   1.338 +CDirCB* CMassStorageFileSystem::NewDirL() const
   1.339 +	{
   1.340 +    __MSFNSLOG
   1.341 +	User::Leave(KErrNotReady);
   1.342 +	return NULL;
   1.343 +	}
   1.344 +
   1.345 +/**
   1.346 +Not supported in Mass Storage file system.
   1.347 +
   1.348 +@leave KErrNotReady
   1.349 +*/
   1.350 +CFormatCB* CMassStorageFileSystem::NewFormatL() const
   1.351 +	{
   1.352 +    __MSFNSLOG
   1.353 +	User::Leave(KErrNotReady);
   1.354 +	return NULL;
   1.355 +	}
   1.356 +
   1.357 +/**
   1.358 +Not supported in Mass Storage file system.
   1.359 +
   1.360 +@return KErrNotSupported
   1.361 +*/
   1.362 +TInt CMassStorageFileSystem::DefaultPath(TDes& /*aPath*/) const
   1.363 +	{
   1.364 +    __MSFNSLOG
   1.365 +	return KErrNotSupported;
   1.366 +	}
   1.367 +
   1.368 +/**
   1.369 +Not supported in Mass Storage file system.
   1.370 +
   1.371 +@return KErrNotSupported
   1.372 +*/
   1.373 +TInt CMassStorageFileSystem::DriveList(TDriveList& /*aList*/) const
   1.374 +	{
   1.375 +    __MSFNSLOG
   1.376 +	return KErrNotSupported;
   1.377 +	}
   1.378 +
   1.379 +/**
   1.380 +Thread entry point.
   1.381 +*/
   1.382 +LOCAL_C TInt MsInitThreadFn(TAny* aPtr)
   1.383 +	{
   1.384 +	User::SetCritical(User::ESystemCritical);
   1.385 +	((CMassStorageFileSystem*)aPtr)->InitThread();
   1.386 +	//Rename the thread so we can create a new one with the same original name later
   1.387 +	User::RenameThread(KMsDeadThreadName);
   1.388 +	return KErrNone;
   1.389 +	}
   1.390 +
   1.391 +/**
   1.392 +Standard entry point for file systems.
   1.393 +Creates a new file system object and starts a new thread for the Mass Storage controller.
   1.394 +*/
   1.395 +extern "C" EXPORT_C CFileSystem* CreateFileSystem()
   1.396 +	{
   1.397 +	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem In\n"));
   1.398 +	RSemaphore gSemThreadReady;
   1.399 +	TInt err = gSemThreadReady.CreateGlobal(KMsFsysSemName, 0);
   1.400 +	if (err != KErrNone)
   1.401 +		{
   1.402 +		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Semaphore Error %d\n"),err);
   1.403 +		return NULL;
   1.404 +		}
   1.405 +
   1.406 +	CFileSystem* msFsys = NULL;
   1.407 +	TRAP(err,  msFsys = CMassStorageFileSystem::NewL());
   1.408 +	if (err != KErrNone)
   1.409 +		{
   1.410 +		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out MSFS Error %d\n"),err);
   1.411 +		gSemThreadReady.Close();
   1.412 +		return NULL;
   1.413 +		}
   1.414 +
   1.415 +	RThread msThread;
   1.416 +	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem: Creating Mass Storage thread\n"));
   1.417 +	err = msThread.Create(KMsThreadName, MsInitThreadFn, KDefaultStackSize, NULL, msFsys);
   1.418 +	if (err != KErrNone)
   1.419 +		{
   1.420 +		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Thread Error %d\n"),err);
   1.421 +		gSemThreadReady.Close();
   1.422 +		return msFsys;
   1.423 +		}
   1.424 +	((CMassStorageFileSystem*)msFsys)->iInstalled=ETrue;
   1.425 +
   1.426 +
   1.427 +	msThread.Logon(((CMassStorageFileSystem*)msFsys)->iThreadStat);
   1.428 +	msThread.Resume();
   1.429 +	gSemThreadReady.Wait();
   1.430 +	gSemThreadReady.Close();
   1.431 +	msThread.Close();
   1.432 +
   1.433 +	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem Out Clean\n"));
   1.434 +
   1.435 +	return msFsys;
   1.436 +	}