os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/cmassstoragefilesystem.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// CMassStorageFileSystem implementation.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file
sl@0
    22
 @internalTechnology
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include <e32def.h>
sl@0
    26
#include <f32ver.h>
sl@0
    27
#include <f32fsys.h>
sl@0
    28
sl@0
    29
#include "usbmsshared.h"
sl@0
    30
#include "mstypes.h"
sl@0
    31
#include "msctypes.h"
sl@0
    32
#include "mserverprotocol.h"
sl@0
    33
#include "cusbmassstoragecontroller.h"
sl@0
    34
#include "cmassstoragefilesystem.h"
sl@0
    35
#include "cmassstoragemountcb.h"
sl@0
    36
#include "debug.h"
sl@0
    37
#include "msdebug.h"
sl@0
    38
#include "massstorage.h"
sl@0
    39
sl@0
    40
_LIT(KMsFsyName, "MassStorageFileSystem");
sl@0
    41
_LIT(KMsThreadName, "MassStorageThread");
sl@0
    42
_LIT(KMsDeadThreadName, "MassStorageDeadThread");
sl@0
    43
_LIT(KMsFsysSemName, "MassStorageSemaphore");
sl@0
    44
LOCAL_D const TInt KMsFsyMajorVersionNumber=1;
sl@0
    45
LOCAL_D const TInt KMsFsyMinorVersionNumber=0;
sl@0
    46
sl@0
    47
CMassStorageFileSystem::CMassStorageFileSystem()
sl@0
    48
	{
sl@0
    49
    __MSFNLOG
sl@0
    50
	}
sl@0
    51
sl@0
    52
CMassStorageFileSystem::~CMassStorageFileSystem()
sl@0
    53
	{
sl@0
    54
    __MSFNLOG
sl@0
    55
	//Kill the controller thread if it exists
sl@0
    56
	delete iMassStorageController;
sl@0
    57
	iMediaChangedStatusList.Close();
sl@0
    58
	RThread thread;
sl@0
    59
	TInt err = thread.Open(KMsThreadName);
sl@0
    60
	if (err == KErrNone)
sl@0
    61
		{
sl@0
    62
		thread.Kill(1); //Parameter is irrelevant
sl@0
    63
		}
sl@0
    64
	thread.Close();
sl@0
    65
	iDriveMap.Close();
sl@0
    66
	}
sl@0
    67
sl@0
    68
CMassStorageFileSystem* CMassStorageFileSystem::NewL()
sl@0
    69
	{
sl@0
    70
    __MSFNSLOG
sl@0
    71
	CMassStorageFileSystem*  self = new (ELeave) CMassStorageFileSystem();
sl@0
    72
	CleanupStack::PushL(self);
sl@0
    73
	self->ConstructL();
sl@0
    74
	CleanupStack::Pop(self);
sl@0
    75
	return self;
sl@0
    76
	}
sl@0
    77
sl@0
    78
void CMassStorageFileSystem::ConstructL()
sl@0
    79
	{
sl@0
    80
    __MSFNLOG
sl@0
    81
	}
sl@0
    82
sl@0
    83
/**
sl@0
    84
Set the file system version and name
sl@0
    85
sl@0
    86
@return Any of the standard Symbian error codes.
sl@0
    87
*/
sl@0
    88
TInt CMassStorageFileSystem::Install()
sl@0
    89
	{
sl@0
    90
    __MSFNLOG
sl@0
    91
	iVersion=TVersion(KMsFsyMajorVersionNumber, KMsFsyMinorVersionNumber, KF32BuildVersionNumber);
sl@0
    92
	TInt err = SetName(&KMsFsyName);
sl@0
    93
	return err;
sl@0
    94
	}
sl@0
    95
sl@0
    96
TInt CMassStorageFileSystem::Remove()
sl@0
    97
	{
sl@0
    98
    __MSFNLOG
sl@0
    99
	TInt err = KErrNone;
sl@0
   100
	if (iInstalled)
sl@0
   101
		{
sl@0
   102
		// Try connecting to the server to send a shutdown message.
sl@0
   103
		// - If the class controller has a session in use, this will return KErrInUse
sl@0
   104
		RUsbMassStorage usbMs;
sl@0
   105
		err = usbMs.Connect();
sl@0
   106
		if(err == KErrNone)
sl@0
   107
			{
sl@0
   108
			err = usbMs.Shutdown();
sl@0
   109
			usbMs.Close();
sl@0
   110
sl@0
   111
			if(err == KErrNone)
sl@0
   112
				{
sl@0
   113
				User::WaitForRequest(iThreadStat);
sl@0
   114
				err = iThreadStat.Int();
sl@0
   115
				}
sl@0
   116
			else
sl@0
   117
				{
sl@0
   118
				__PRINT1(_L("CMassStorageFileSystem::Remove Shutdown Error %d\n"),err);
sl@0
   119
				}
sl@0
   120
			}
sl@0
   121
		else
sl@0
   122
			{
sl@0
   123
			__PRINT1(_L("CMassStorageFileSystem::Remove Connect Error %d\n"),err);
sl@0
   124
			}
sl@0
   125
		}
sl@0
   126
	return(err);
sl@0
   127
	}
sl@0
   128
sl@0
   129
/**
sl@0
   130
Creates a new Mass Storage mount object.
sl@0
   131
sl@0
   132
@return A new CMassStorageMountCB
sl@0
   133
@leave KErrNotReady if the Mass Storage controller is not running.
sl@0
   134
*/
sl@0
   135
CMountCB* CMassStorageFileSystem::NewMountL() const
sl@0
   136
	{
sl@0
   137
    __MSFNSLOG
sl@0
   138
	if (!iRunning)
sl@0
   139
		{
sl@0
   140
		User::Leave(KErrNotReady);
sl@0
   141
		}
sl@0
   142
	return CMassStorageMountCB::NewL(iDriveMap);
sl@0
   143
	}
sl@0
   144
sl@0
   145
/**
sl@0
   146
Sets the media attributes and type in the aInfo parameter to those of the specified drive.
sl@0
   147
sl@0
   148
@param anInfo TDriveInfo object to store the drive information.
sl@0
   149
@param aDriveNumber The number of the drive to get the information from.
sl@0
   150
*/
sl@0
   151
void CMassStorageFileSystem::DriveInfo(TDriveInfo& aInfo, TInt aDriveNumber) const
sl@0
   152
	{
sl@0
   153
    __MSFNSLOG
sl@0
   154
	TLocalDriveCapsV2Buf caps;
sl@0
   155
	if (!IsValidLocalDriveMapping(aDriveNumber))
sl@0
   156
		{
sl@0
   157
		return;
sl@0
   158
		}
sl@0
   159
	GetLocalDrive(aDriveNumber).Caps(caps);
sl@0
   160
	aInfo.iMediaAtt=caps().iMediaAtt;
sl@0
   161
    aInfo.iType = ::EMediaNotPresent;  // Media is not available to the file system
sl@0
   162
	aInfo.iDriveAtt=caps().iDriveAtt;
sl@0
   163
	}
sl@0
   164
sl@0
   165
/**
sl@0
   166
Returns a reference to the Mass Storage controller.
sl@0
   167
sl@0
   168
@return Reference to the Mass Storage controller.
sl@0
   169
*/
sl@0
   170
CUsbMassStorageController& CMassStorageFileSystem::Controller()
sl@0
   171
	{
sl@0
   172
    __MSFNLOG
sl@0
   173
	return *iMassStorageController;
sl@0
   174
	}
sl@0
   175
sl@0
   176
/**
sl@0
   177
Fill iMsDrives with a mapping of lun->drive number for supported mass storage drives
sl@0
   178
sl@0
   179
*/
sl@0
   180
TInt CMassStorageFileSystem::EnumerateMsDrivesL()
sl@0
   181
	{
sl@0
   182
    __MSFNLOG
sl@0
   183
	iDriveMap.Reset();
sl@0
   184
	TInt driveCount = 0;
sl@0
   185
sl@0
   186
	TLocalDriveCapsV2Buf caps;
sl@0
   187
sl@0
   188
	for (TInt i = EDriveC; i < KMaxDrives; i++)
sl@0
   189
		{
sl@0
   190
		caps.FillZ();
sl@0
   191
sl@0
   192
		if (IsValidLocalDriveMapping(i))
sl@0
   193
			{
sl@0
   194
			TInt err = GetLocalDrive(i).Caps(caps);
sl@0
   195
			TInt locDrvNum = DriveNumberToLocalDriveNumber(i);
sl@0
   196
			__PRINT2(_L("Caps: err=%d, att=%d\n"), err, caps().iDriveAtt);
sl@0
   197
sl@0
   198
			TBool isRemovable = err==KErrNotReady || (caps().iDriveAtt & KDriveAttRemovable);
sl@0
   199
			__PRINT2(_L("EnumerateMsDrives: Drive %c: is %sremovable\n"),
sl@0
   200
							'A'+i-EDriveA,
sl@0
   201
							isRemovable?_S(""):_S("NOT "));
sl@0
   202
sl@0
   203
			if (isRemovable)
sl@0
   204
				{
sl@0
   205
				//
sl@0
   206
				// STF: Connect to the local drive here.  This gives us the media changed flag, and
sl@0
   207
				//		our own TBusLocalDrive object for use by the proxy drive and controller.
sl@0
   208
                //
sl@0
   209
sl@0
   210
                TMediaChangedStatus mediaChanged;
sl@0
   211
                iMediaChangedStatusList.AppendL(mediaChanged);
sl@0
   212
sl@0
   213
                TBool& mediaChangedRef = iMediaChangedStatusList[driveCount].iMediaChanged;
sl@0
   214
				TInt err = iMediaChangedStatusList[driveCount].iLocalDrive.Connect(locDrvNum, mediaChangedRef);
sl@0
   215
				if (err == KErrNone)
sl@0
   216
					{
sl@0
   217
					iDriveMap.Append(static_cast <TDriveNumber>(i));
sl@0
   218
					}
sl@0
   219
				driveCount++;
sl@0
   220
				}
sl@0
   221
			}
sl@0
   222
		}
sl@0
   223
sl@0
   224
	__PRINT1(_L("EnumerateMsDrives Out, %d MS drives found\n"), driveCount);
sl@0
   225
	return driveCount;
sl@0
   226
	}
sl@0
   227
sl@0
   228
TBool CMassStorageFileSystem::IsExtensionSupported() const
sl@0
   229
	{
sl@0
   230
    __MSFNSLOG
sl@0
   231
	return ETrue;
sl@0
   232
	}
sl@0
   233
sl@0
   234
/**
sl@0
   235
Creates a TrapCleanup and ActiveScheduler and initializes the Mass Storage controller.
sl@0
   236
Start the ActiveScheduler.
sl@0
   237
sl@0
   238
@return Any of the standard Symbian error codes.
sl@0
   239
*/
sl@0
   240
TInt CMassStorageFileSystem::InitThread()
sl@0
   241
	{
sl@0
   242
    __MSFNLOG
sl@0
   243
sl@0
   244
	//Give the thread a name so we can kill it later
sl@0
   245
	User::RenameThread(KMsThreadName);
sl@0
   246
sl@0
   247
	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0
   248
	if (cleanup == NULL)
sl@0
   249
		{
sl@0
   250
		return KErrNoMemory;
sl@0
   251
		}
sl@0
   252
sl@0
   253
	TRAPD(err, InitThreadL());
sl@0
   254
sl@0
   255
	delete cleanup;
sl@0
   256
sl@0
   257
	__PRINT1(_L("CMassStorageFileSystem::InitThread Out, error=%d\n"), err);
sl@0
   258
	return err;
sl@0
   259
	}
sl@0
   260
sl@0
   261
TInt CMassStorageFileSystem::InitThreadL()
sl@0
   262
	{
sl@0
   263
    __MSFNLOG
sl@0
   264
	RSemaphore gSemThreadReady;
sl@0
   265
	gSemThreadReady.OpenGlobal(KMsFsysSemName);
sl@0
   266
sl@0
   267
	// Determine which drives are available for Mass Storage.
sl@0
   268
	// (this also creates a local TBusLocalDrive for use by the drive controller)
sl@0
   269
	EnumerateMsDrivesL();
sl@0
   270
sl@0
   271
	CActiveScheduler* sched = new (ELeave) CActiveScheduler;
sl@0
   272
	if (sched == NULL)
sl@0
   273
		{
sl@0
   274
		gSemThreadReady.Signal();
sl@0
   275
		User::Leave(KErrNoMemory);
sl@0
   276
		}
sl@0
   277
	CleanupStack::PushL(sched);
sl@0
   278
	CActiveScheduler::Install(sched);
sl@0
   279
sl@0
   280
	iMassStorageController = CUsbMassStorageController::NewL();
sl@0
   281
	if (iMassStorageController == NULL)
sl@0
   282
		{
sl@0
   283
		gSemThreadReady.Signal();
sl@0
   284
		User::Leave(KErrNoMemory);
sl@0
   285
		}
sl@0
   286
sl@0
   287
	__PRINT(_L("CMassStorageFileSystem::InitThread: Creating Mass Storage Controller\n"));
sl@0
   288
	TRAPD(err, iMassStorageController->CreateL(iDriveMap));
sl@0
   289
	if (err != KErrNone)
sl@0
   290
		{
sl@0
   291
		gSemThreadReady.Signal();
sl@0
   292
		CActiveScheduler::Install(NULL);
sl@0
   293
		User::Leave(err);
sl@0
   294
		}
sl@0
   295
sl@0
   296
	CleanupStack::Pop(sched);
sl@0
   297
sl@0
   298
	iRunning = ETrue;
sl@0
   299
	gSemThreadReady.Signal();
sl@0
   300
	gSemThreadReady.Close();
sl@0
   301
	CActiveScheduler::Start();
sl@0
   302
sl@0
   303
//========= stop thread ================
sl@0
   304
	delete iMassStorageController;
sl@0
   305
	iMassStorageController = NULL;
sl@0
   306
sl@0
   307
	for (TInt i=0; i < iMediaChangedStatusList.Count(); i++)
sl@0
   308
		{
sl@0
   309
        iMediaChangedStatusList[i].iLocalDrive.Disconnect();
sl@0
   310
		}
sl@0
   311
	iMediaChangedStatusList.Reset();
sl@0
   312
sl@0
   313
	delete sched;
sl@0
   314
	iRunning = EFalse;
sl@0
   315
	return KErrNone;
sl@0
   316
	}
sl@0
   317
sl@0
   318
/**
sl@0
   319
Not supported in Mass Storage file system.
sl@0
   320
sl@0
   321
@leave KErrNotReady
sl@0
   322
*/
sl@0
   323
CFileCB* CMassStorageFileSystem::NewFileL() const
sl@0
   324
	{
sl@0
   325
    __MSFNSLOG
sl@0
   326
	User::Leave(KErrNotReady);
sl@0
   327
	return NULL;
sl@0
   328
	}
sl@0
   329
sl@0
   330
/**
sl@0
   331
Not supported in Mass Storage file system.
sl@0
   332
sl@0
   333
@leave KErrNotReady
sl@0
   334
*/
sl@0
   335
CDirCB* CMassStorageFileSystem::NewDirL() const
sl@0
   336
	{
sl@0
   337
    __MSFNSLOG
sl@0
   338
	User::Leave(KErrNotReady);
sl@0
   339
	return NULL;
sl@0
   340
	}
sl@0
   341
sl@0
   342
/**
sl@0
   343
Not supported in Mass Storage file system.
sl@0
   344
sl@0
   345
@leave KErrNotReady
sl@0
   346
*/
sl@0
   347
CFormatCB* CMassStorageFileSystem::NewFormatL() const
sl@0
   348
	{
sl@0
   349
    __MSFNSLOG
sl@0
   350
	User::Leave(KErrNotReady);
sl@0
   351
	return NULL;
sl@0
   352
	}
sl@0
   353
sl@0
   354
/**
sl@0
   355
Not supported in Mass Storage file system.
sl@0
   356
sl@0
   357
@return KErrNotSupported
sl@0
   358
*/
sl@0
   359
TInt CMassStorageFileSystem::DefaultPath(TDes& /*aPath*/) const
sl@0
   360
	{
sl@0
   361
    __MSFNSLOG
sl@0
   362
	return KErrNotSupported;
sl@0
   363
	}
sl@0
   364
sl@0
   365
/**
sl@0
   366
Not supported in Mass Storage file system.
sl@0
   367
sl@0
   368
@return KErrNotSupported
sl@0
   369
*/
sl@0
   370
TInt CMassStorageFileSystem::DriveList(TDriveList& /*aList*/) const
sl@0
   371
	{
sl@0
   372
    __MSFNSLOG
sl@0
   373
	return KErrNotSupported;
sl@0
   374
	}
sl@0
   375
sl@0
   376
/**
sl@0
   377
Thread entry point.
sl@0
   378
*/
sl@0
   379
LOCAL_C TInt MsInitThreadFn(TAny* aPtr)
sl@0
   380
	{
sl@0
   381
	User::SetCritical(User::ESystemCritical);
sl@0
   382
	((CMassStorageFileSystem*)aPtr)->InitThread();
sl@0
   383
	//Rename the thread so we can create a new one with the same original name later
sl@0
   384
	User::RenameThread(KMsDeadThreadName);
sl@0
   385
	return KErrNone;
sl@0
   386
	}
sl@0
   387
sl@0
   388
/**
sl@0
   389
Standard entry point for file systems.
sl@0
   390
Creates a new file system object and starts a new thread for the Mass Storage controller.
sl@0
   391
*/
sl@0
   392
extern "C" EXPORT_C CFileSystem* CreateFileSystem()
sl@0
   393
	{
sl@0
   394
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem In\n"));
sl@0
   395
	RSemaphore gSemThreadReady;
sl@0
   396
	TInt err = gSemThreadReady.CreateGlobal(KMsFsysSemName, 0);
sl@0
   397
	if (err != KErrNone)
sl@0
   398
		{
sl@0
   399
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Semaphore Error %d\n"),err);
sl@0
   400
		return NULL;
sl@0
   401
		}
sl@0
   402
sl@0
   403
	CFileSystem* msFsys = NULL;
sl@0
   404
	TRAP(err,  msFsys = CMassStorageFileSystem::NewL());
sl@0
   405
	if (err != KErrNone)
sl@0
   406
		{
sl@0
   407
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out MSFS Error %d\n"),err);
sl@0
   408
		gSemThreadReady.Close();
sl@0
   409
		return NULL;
sl@0
   410
		}
sl@0
   411
sl@0
   412
	RThread msThread;
sl@0
   413
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem: Creating Mass Storage thread\n"));
sl@0
   414
	err = msThread.Create(KMsThreadName, MsInitThreadFn, KDefaultStackSize, NULL, msFsys);
sl@0
   415
	if (err != KErrNone)
sl@0
   416
		{
sl@0
   417
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Thread Error %d\n"),err);
sl@0
   418
		gSemThreadReady.Close();
sl@0
   419
		return msFsys;
sl@0
   420
		}
sl@0
   421
	((CMassStorageFileSystem*)msFsys)->iInstalled=ETrue;
sl@0
   422
sl@0
   423
sl@0
   424
	msThread.Logon(((CMassStorageFileSystem*)msFsys)->iThreadStat);
sl@0
   425
	msThread.Resume();
sl@0
   426
	gSemThreadReady.Wait();
sl@0
   427
	gSemThreadReady.Close();
sl@0
   428
	msThread.Close();
sl@0
   429
sl@0
   430
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem Out Clean\n"));
sl@0
   431
sl@0
   432
	return msFsys;
sl@0
   433
	}