os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestclient/usbtestclient.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
// USB Mass Storage Application - also used as an improvised boot loader mechanism
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
*/
sl@0
    23
sl@0
    24
#include "usbtestclient.h"
sl@0
    25
sl@0
    26
#include <e32std.h>
sl@0
    27
#include <e32svr.h>
sl@0
    28
#include <e32cons.h>
sl@0
    29
sl@0
    30
#include <usbmsshared.h>
sl@0
    31
sl@0
    32
#include <d32usbc.h>
sl@0
    33
#include <d32otgdi.h>
sl@0
    34
sl@0
    35
#include <nkern/nk_trace.h>
sl@0
    36
#include <hal.h>
sl@0
    37
sl@0
    38
#include "rusbmassstorage.h"
sl@0
    39
sl@0
    40
enum
sl@0
    41
	{
sl@0
    42
	EUsbDeviceStateUndefined  = EUsbcDeviceStateUndefined,
sl@0
    43
	EUsbDeviceStateConfigured = EUsbcDeviceStateConfigured,
sl@0
    44
	};
sl@0
    45
sl@0
    46
static CConsoleBase* console = NULL;
sl@0
    47
static RFs fs;
sl@0
    48
static TInt selectedDriveIndex = 0;
sl@0
    49
static TBuf<0x40> mountList;
sl@0
    50
sl@0
    51
static TFixedArray<TBool, KMaxDrives>                   msfsMountedList;  ///< 'true' entry corresponds to the drive with mounted MSFS.FSY
sl@0
    52
static TFixedArray<CFileSystemDescriptor*, KMaxDrives>  unmountedFsList;  ///< every non-NULL entry corresponds to the unmounted original FS for the drive
sl@0
    53
sl@0
    54
_LIT(KMsFsy, "USBTESTMSCLIENT.FSY");
sl@0
    55
_LIT(KMsFs, "MassStorageFileSystem");
sl@0
    56
_LIT(KOk,"OK");
sl@0
    57
_LIT(KError,"Error");
sl@0
    58
_LIT(KBytesTransferredFmt, "%c:%d/%d ");
sl@0
    59
_LIT(KErrFmt, "Error: %d\r");
sl@0
    60
sl@0
    61
_LIT(KTxtApp,"USB TEST CLIENT");
sl@0
    62
_LIT(KDefPwd,"123");
sl@0
    63
sl@0
    64
//-- if defined, some useful information will be printed out via RDebug interface
sl@0
    65
//#define LOGGING_ENABLED
sl@0
    66
sl@0
    67
//-----------------------------------------------------------------------------
sl@0
    68
/**
sl@0
    69
    prints a line to the console and copies it to the debug log if LOGGING_ENABLED
sl@0
    70
*/
sl@0
    71
void LogPrint(TRefByValue<const TDesC> aFmt,...)
sl@0
    72
    {
sl@0
    73
    VA_LIST list;
sl@0
    74
    VA_START(list, aFmt);
sl@0
    75
sl@0
    76
    TBuf<0x100> buf;
sl@0
    77
    buf.FormatList(aFmt, list); //-- ignore overflows
sl@0
    78
sl@0
    79
    if(console)
sl@0
    80
        console->Write(buf);
sl@0
    81
sl@0
    82
#ifdef LOGGING_ENABLED
sl@0
    83
    //-- print out the line via RDebug::Print
sl@0
    84
    const TInt bufLen = buf.Length();
sl@0
    85
    if(bufLen >0 && buf[bufLen-1] == '\n')
sl@0
    86
        {
sl@0
    87
        buf.Insert(bufLen-1, _L("\r"));
sl@0
    88
        }
sl@0
    89
    else
sl@0
    90
        {
sl@0
    91
        buf.Append(_L("\r\n"));
sl@0
    92
        }
sl@0
    93
sl@0
    94
    RDebug::RawPrint(buf);
sl@0
    95
#endif
sl@0
    96
    }
sl@0
    97
sl@0
    98
//-----------------------------------------------------------------------------
sl@0
    99
/**
sl@0
   100
    prints a line to the debug log if LOGGING_ENABLED
sl@0
   101
*/
sl@0
   102
void Log(TRefByValue<const TDesC> aFmt,...)
sl@0
   103
    {
sl@0
   104
#ifdef LOGGING_ENABLED
sl@0
   105
sl@0
   106
    VA_LIST list;
sl@0
   107
    VA_START(list, aFmt);
sl@0
   108
sl@0
   109
    TBuf<0x100> buf;
sl@0
   110
    buf.FormatList(aFmt, list); //-- ignore overflows
sl@0
   111
sl@0
   112
    //-- print out the line via RDebug::Print
sl@0
   113
    const TInt bufLen = buf.Length();
sl@0
   114
    if(bufLen >0 && buf[bufLen-1] == '\n')
sl@0
   115
        {
sl@0
   116
        buf.Insert(bufLen-1, _L("\r"));
sl@0
   117
        }
sl@0
   118
sl@0
   119
    RDebug::RawPrint(buf);
sl@0
   120
#else
sl@0
   121
    (void)aFmt;
sl@0
   122
#endif
sl@0
   123
    }
sl@0
   124
sl@0
   125
sl@0
   126
//-----------------------------------------------------------------------------
sl@0
   127
sl@0
   128
static void Clear(int row, int count=1)
sl@0
   129
	{
sl@0
   130
	_LIT(KBlank,"                                        ");
sl@0
   131
	for(TInt i=0; i<count; i++)
sl@0
   132
		{
sl@0
   133
		console->SetPos(0,row+i);
sl@0
   134
		console->Printf(KBlank);
sl@0
   135
		}
sl@0
   136
	console->SetPos(0,row);
sl@0
   137
	}
sl@0
   138
sl@0
   139
sl@0
   140
static void ShowDriveSelection()
sl@0
   141
	{
sl@0
   142
	console->SetPos(0,15);
sl@0
   143
	if(PropertyHandlers::allDrivesStatus.Length()/2 > selectedDriveIndex)
sl@0
   144
		{
sl@0
   145
		LogPrint(_L("Selected Drive: %c"), 'A' + PropertyHandlers::allDrivesStatus[selectedDriveIndex*2]);
sl@0
   146
		}
sl@0
   147
	else
sl@0
   148
		{
sl@0
   149
		LogPrint(_L("Selected Drive: (none)"));
sl@0
   150
		}
sl@0
   151
	}
sl@0
   152
sl@0
   153
sl@0
   154
sl@0
   155
class CPeriodUpdate : public CActive
sl@0
   156
	{
sl@0
   157
public:
sl@0
   158
	static CPeriodUpdate* NewLC();
sl@0
   159
private:
sl@0
   160
	CPeriodUpdate();
sl@0
   161
	void ConstructL();
sl@0
   162
	~CPeriodUpdate();
sl@0
   163
	void RunL();
sl@0
   164
	void DoCancel();
sl@0
   165
sl@0
   166
	RTimer iTimer;
sl@0
   167
	TUint iUpTime;
sl@0
   168
	};
sl@0
   169
sl@0
   170
CPeriodUpdate* CPeriodUpdate::NewLC()
sl@0
   171
	{
sl@0
   172
	CPeriodUpdate* me=new(ELeave) CPeriodUpdate();
sl@0
   173
	CleanupStack::PushL(me);
sl@0
   174
	me->ConstructL();
sl@0
   175
	return me;
sl@0
   176
	}
sl@0
   177
sl@0
   178
CPeriodUpdate::CPeriodUpdate()
sl@0
   179
	: CActive(0), iUpTime(0)
sl@0
   180
	{}
sl@0
   181
sl@0
   182
void CPeriodUpdate::ConstructL()
sl@0
   183
	{
sl@0
   184
	CActiveScheduler::Add(this);
sl@0
   185
	iTimer.CreateLocal();
sl@0
   186
	RunL();
sl@0
   187
	}
sl@0
   188
sl@0
   189
CPeriodUpdate::~CPeriodUpdate()
sl@0
   190
	{
sl@0
   191
	Cancel();
sl@0
   192
	}
sl@0
   193
sl@0
   194
void CPeriodUpdate::DoCancel()
sl@0
   195
	{
sl@0
   196
	}
sl@0
   197
sl@0
   198
void CPeriodUpdate::RunL()
sl@0
   199
	{
sl@0
   200
	SetActive();
sl@0
   201
	// Print RAM usage & up time
sl@0
   202
sl@0
   203
	iUpTime++;
sl@0
   204
	TUint totmins=(iUpTime/60);
sl@0
   205
	TUint tothrs=(totmins/60);
sl@0
   206
	TInt mem=0;
sl@0
   207
	if (HAL::Get(HALData::EMemoryRAMFree, mem)==KErrNone)
sl@0
   208
		{
sl@0
   209
		console->SetPos(0,22);
sl@0
   210
		console->Printf(_L("mem (bytes) : %d\n"), mem);
sl@0
   211
		console->Printf(_L("up time     : %dh:%dm:%ds\n"),
sl@0
   212
					tothrs, totmins%60, iUpTime%60);
sl@0
   213
		}
sl@0
   214
	iTimer.After(iStatus, 1000000);
sl@0
   215
	}
sl@0
   216
sl@0
   217
//-----------------------------------------------------------------------------
sl@0
   218
/**
sl@0
   219
    Dismounts the originally mounted FS and optional primary extension from the drive and stores
sl@0
   220
    this information in the FS descriptor
sl@0
   221
sl@0
   222
    @return on success returns a pointer to the instantinated FS descriptor
sl@0
   223
*/
sl@0
   224
static CFileSystemDescriptor* DoDismountOrginalFS(RFs& aFs, TInt aDrive)
sl@0
   225
    {
sl@0
   226
    TInt        nRes;
sl@0
   227
    TBuf<128>   fsName;
sl@0
   228
    TBuf<128>   primaryExtName;
sl@0
   229
    TBool       bDrvSync = EFalse;
sl@0
   230
sl@0
   231
    Log(_L("# DoDismountOrginalFS drv:%d\n"), aDrive);
sl@0
   232
sl@0
   233
    //-- 1. get file system name
sl@0
   234
    nRes = aFs.FileSystemName(fsName, aDrive);
sl@0
   235
    if(nRes != KErrNone)
sl@0
   236
        {//-- probably no file system installed at all
sl@0
   237
        return NULL;
sl@0
   238
        }
sl@0
   239
sl@0
   240
    //-- 2. find out if the drive sync/async
sl@0
   241
    TPckgBuf<TBool> drvSyncBuf;
sl@0
   242
    nRes = aFs.QueryVolumeInfoExt(aDrive, EIsDriveSync, drvSyncBuf);
sl@0
   243
    if(nRes == KErrNone)
sl@0
   244
        {
sl@0
   245
        bDrvSync = drvSyncBuf();
sl@0
   246
        }
sl@0
   247
sl@0
   248
    //-- 3. find out primary extension name if it is present; we will need to add it againt when mounting the FS
sl@0
   249
    //-- other extensions (non-primary) are not supported yet
sl@0
   250
    nRes = aFs.ExtensionName(primaryExtName, aDrive, 0);
sl@0
   251
    if(nRes != KErrNone)
sl@0
   252
        {
sl@0
   253
        primaryExtName.SetLength(0);
sl@0
   254
        }
sl@0
   255
sl@0
   256
    //-- 3.1 check if the drive has non-primary extensions, fail in this case, because this FS can't be mounted back normally
sl@0
   257
    nRes = aFs.ExtensionName(primaryExtName, aDrive, 1);
sl@0
   258
    if(nRes == KErrNone)
sl@0
   259
        {
sl@0
   260
        LogPrint(_L("Non-primary extensions are not supported!\n"));
sl@0
   261
        return NULL;
sl@0
   262
        }
sl@0
   263
sl@0
   264
    Log(_L("# DoDismountOrginalFS FS:%S, Prim ext:%S, synch:%d\n"), &fsName, &primaryExtName, bDrvSync);
sl@0
   265
sl@0
   266
    //-- create FS descriptor and dismount the FS
sl@0
   267
    CFileSystemDescriptor* pFsDesc = NULL;
sl@0
   268
sl@0
   269
    TRAP(nRes, pFsDesc = CFileSystemDescriptor::NewL(fsName, primaryExtName, bDrvSync));
sl@0
   270
    if(nRes != KErrNone)
sl@0
   271
        return NULL; //-- OOM ?
sl@0
   272
sl@0
   273
    nRes = aFs.DismountFileSystem(fsName, aDrive);
sl@0
   274
    if(nRes != KErrNone)
sl@0
   275
        {
sl@0
   276
        delete pFsDesc;
sl@0
   277
        pFsDesc = NULL;
sl@0
   278
        Log(_L("# DoDismountOrginalFS Dismounting Err:%d\n"), nRes);
sl@0
   279
        }
sl@0
   280
sl@0
   281
    return pFsDesc;
sl@0
   282
}
sl@0
   283
sl@0
   284
//-----------------------------------------------------------------------------
sl@0
   285
/**
sl@0
   286
    Tries to restore the original FS on the drive using the FS descriptor provided
sl@0
   287
    @return standard error code.
sl@0
   288
*/
sl@0
   289
static TInt DoRestoreFS(RFs& aFs, TInt aDrive, CFileSystemDescriptor* apFsDesc)
sl@0
   290
    {
sl@0
   291
    TInt nRes;
sl@0
   292
sl@0
   293
    Log(_L("# DoRestoreFS drv:%d\n"), aDrive);
sl@0
   294
sl@0
   295
    //-- 1. check that there is no FS installed
sl@0
   296
        {
sl@0
   297
        TBuf<128>   fsName;
sl@0
   298
        nRes = aFs.FileSystemName(fsName, aDrive);
sl@0
   299
        if(nRes == KErrNone)
sl@0
   300
            {//-- probably no file system installed at all
sl@0
   301
            Log(_L("# This drive already has FS intalled:%S \n"), &fsName);
sl@0
   302
            return KErrAlreadyExists;
sl@0
   303
            }
sl@0
   304
        }
sl@0
   305
sl@0
   306
    TPtrC ptrN  (apFsDesc->FsName());
sl@0
   307
    TPtrC ptrExt(apFsDesc->PrimaryExtName());
sl@0
   308
    Log(_L("# Mounting FS:%S, Prim ext:%S, synch:%d\n"), &ptrN, &ptrExt, apFsDesc->DriveIsSynch());
sl@0
   309
sl@0
   310
    if(ptrExt.Length() >0)
sl@0
   311
        {//-- there is a primary extension to be mounted
sl@0
   312
        nRes = aFs.AddExtension(ptrExt);
sl@0
   313
        if(nRes != KErrNone && nRes != KErrAlreadyExists)
sl@0
   314
            {
sl@0
   315
            return nRes;
sl@0
   316
            }
sl@0
   317
sl@0
   318
        nRes = aFs.MountFileSystem(ptrN, ptrExt, aDrive, apFsDesc->DriveIsSynch());
sl@0
   319
        }
sl@0
   320
    else
sl@0
   321
        {
sl@0
   322
        nRes = aFs.MountFileSystem(ptrN, aDrive, apFsDesc->DriveIsSynch());
sl@0
   323
        }
sl@0
   324
sl@0
   325
    if(nRes != KErrNone)
sl@0
   326
        {
sl@0
   327
        Log(_L("# Mount failed! code:%d\n"),nRes);
sl@0
   328
        }
sl@0
   329
sl@0
   330
    return nRes;
sl@0
   331
    }
sl@0
   332
sl@0
   333
sl@0
   334
//-----------------------------------------------------------------------------
sl@0
   335
/**
sl@0
   336
    Dismount the original FS from the drive and mount MsFS instead
sl@0
   337
*/
sl@0
   338
static void MountMsFs(TInt driveNumber)
sl@0
   339
	{
sl@0
   340
	TInt x = console->WhereX();
sl@0
   341
	TInt y = console->WhereY();
sl@0
   342
sl@0
   343
    //-- 1. try dismounting the original FS
sl@0
   344
    CFileSystemDescriptor* fsDesc = DoDismountOrginalFS(fs, driveNumber);
sl@0
   345
    unmountedFsList[driveNumber] = fsDesc;
sl@0
   346
sl@0
   347
    console->SetPos(0, 10);
sl@0
   348
sl@0
   349
    if(fsDesc)
sl@0
   350
        {
sl@0
   351
        TPtrC ptrN(fsDesc->FsName());
sl@0
   352
        LogPrint(_L("drv:%d FS:%S Dismounted OK"),driveNumber, &ptrN);
sl@0
   353
        }
sl@0
   354
    else
sl@0
   355
        {
sl@0
   356
        LogPrint(_L("drv:%d Dismount FS Failed!"),driveNumber);
sl@0
   357
        }
sl@0
   358
sl@0
   359
    console->ClearToEndOfLine();
sl@0
   360
sl@0
   361
    //-- 2. try to mount the "MSFS"
sl@0
   362
    TInt error;
sl@0
   363
    error = fs.MountFileSystem(KMsFs, driveNumber);
sl@0
   364
	console->SetPos(0, 11);
sl@0
   365
	LogPrint(_L("MSFS Mount:   %S (%d)"), (error?&KError:&KOk), error);
sl@0
   366
	console->ClearToEndOfLine();
sl@0
   367
sl@0
   368
	if (!error)
sl@0
   369
		msfsMountedList[driveNumber] = ETrue;
sl@0
   370
sl@0
   371
	// restore console position
sl@0
   372
	console->SetPos(x,y);
sl@0
   373
	}
sl@0
   374
sl@0
   375
//-----------------------------------------------------------------------------
sl@0
   376
/**
sl@0
   377
    Dismount MsFS and mount the original FS
sl@0
   378
*/
sl@0
   379
static TInt RestoreMount(TInt driveNumber)
sl@0
   380
	{
sl@0
   381
	TInt err = KErrNone;
sl@0
   382
sl@0
   383
	TInt x = console->WhereX();
sl@0
   384
	TInt y = console->WhereY();
sl@0
   385
sl@0
   386
    //-- 1. try dismounting the "MSFS"
sl@0
   387
	if (msfsMountedList[driveNumber])
sl@0
   388
		{
sl@0
   389
		err = fs.DismountFileSystem(KMsFs, driveNumber);
sl@0
   390
		console->SetPos(0, 11);
sl@0
   391
		LogPrint(_L("MSFS Dismount:%S (%d)"), (err?&KError:&KOk), err);
sl@0
   392
		console->ClearToEndOfLine();
sl@0
   393
		if (err)
sl@0
   394
			return err;
sl@0
   395
sl@0
   396
		msfsMountedList[driveNumber] = EFalse;
sl@0
   397
        }
sl@0
   398
sl@0
   399
    //-- 2. try to mount the original FS back
sl@0
   400
    CFileSystemDescriptor* fsDesc = unmountedFsList[driveNumber];
sl@0
   401
    if(fsDesc)
sl@0
   402
        {
sl@0
   403
        err = DoRestoreFS(fs, driveNumber, fsDesc);
sl@0
   404
sl@0
   405
        TPtrC ptrN(fsDesc->FsName());
sl@0
   406
        console->SetPos(0, 10);
sl@0
   407
        LogPrint(_L("%S Mount:    %S (%d)"), &ptrN, (err?&KError:&KOk), err);
sl@0
   408
        console->ClearToEndOfLine();
sl@0
   409
sl@0
   410
        delete fsDesc;
sl@0
   411
        unmountedFsList[driveNumber] = NULL;
sl@0
   412
        }
sl@0
   413
sl@0
   414
sl@0
   415
    // restore console position
sl@0
   416
	console->SetPos(x,y);
sl@0
   417
	return err;
sl@0
   418
	}
sl@0
   419
sl@0
   420
//////////////////////////////////////////////////////////////////////////////
sl@0
   421
//
sl@0
   422
// CPropertyWatch
sl@0
   423
// An active object that tracks changes to the KUsbMsDriveState properties
sl@0
   424
//
sl@0
   425
//////////////////////////////////////////////////////////////////////////////
sl@0
   426
sl@0
   427
CPropertyWatch* CPropertyWatch::NewLC(TUsbMsDriveState_Subkey aSubkey, PropertyHandlers::THandler aHandler)
sl@0
   428
	{
sl@0
   429
	CPropertyWatch* me=new(ELeave) CPropertyWatch(aHandler);
sl@0
   430
	CleanupStack::PushL(me);
sl@0
   431
	me->ConstructL(aSubkey);
sl@0
   432
	return me;
sl@0
   433
	}
sl@0
   434
sl@0
   435
CPropertyWatch::CPropertyWatch(PropertyHandlers::THandler aHandler)
sl@0
   436
	: CActive(0), iHandler(aHandler)
sl@0
   437
	{}
sl@0
   438
sl@0
   439
void CPropertyWatch::ConstructL(TUsbMsDriveState_Subkey aSubkey)
sl@0
   440
	{
sl@0
   441
	User::LeaveIfError(iProperty.Attach(KUsbMsDriveState_Category, aSubkey));
sl@0
   442
	CActiveScheduler::Add(this);
sl@0
   443
	// initial subscription and process current property value
sl@0
   444
	RunL();
sl@0
   445
	}
sl@0
   446
sl@0
   447
CPropertyWatch::~CPropertyWatch()
sl@0
   448
	{
sl@0
   449
	Cancel();
sl@0
   450
	iProperty.Close();
sl@0
   451
	}
sl@0
   452
sl@0
   453
void CPropertyWatch::DoCancel()
sl@0
   454
	{
sl@0
   455
	iProperty.Cancel();
sl@0
   456
	}
sl@0
   457
sl@0
   458
void CPropertyWatch::RunL()
sl@0
   459
	{
sl@0
   460
	// resubscribe before processing new value to prevent missing updates
sl@0
   461
	iProperty.Subscribe(iStatus);
sl@0
   462
	SetActive();
sl@0
   463
sl@0
   464
	iHandler(iProperty);
sl@0
   465
	}
sl@0
   466
sl@0
   467
//////////////////////////////////////////////////////////////////////////////
sl@0
   468
//
sl@0
   469
// CUsbWatch
sl@0
   470
//
sl@0
   471
//////////////////////////////////////////////////////////////////////////////
sl@0
   472
sl@0
   473
CUsbWatch* CUsbWatch::NewLC(RUsb& aUsb)
sl@0
   474
	{
sl@0
   475
	CUsbWatch* me=new(ELeave) CUsbWatch(aUsb);
sl@0
   476
	CleanupStack::PushL(me);
sl@0
   477
	me->ConstructL();
sl@0
   478
	return me;
sl@0
   479
	}
sl@0
   480
sl@0
   481
CUsbWatch::CUsbWatch(RUsb& aUsb)
sl@0
   482
	:
sl@0
   483
	CActive(0),
sl@0
   484
	iUsb(aUsb),
sl@0
   485
	iUsbDeviceState(EUsbDeviceStateUndefined),
sl@0
   486
	iWasConfigured(EFalse)
sl@0
   487
	{}
sl@0
   488
sl@0
   489
void CUsbWatch::ConstructL()
sl@0
   490
	{
sl@0
   491
	CActiveScheduler::Add(this);
sl@0
   492
	RunL();
sl@0
   493
	}
sl@0
   494
sl@0
   495
CUsbWatch::~CUsbWatch()
sl@0
   496
	{
sl@0
   497
	Cancel();
sl@0
   498
//	iUsb.DeviceStateNotificationCancel();
sl@0
   499
	iUsb.AlternateDeviceStatusNotifyCancel();
sl@0
   500
	}
sl@0
   501
sl@0
   502
void CUsbWatch::DoCancel()
sl@0
   503
	{
sl@0
   504
//	iUsb.DeviceStateNotificationCancel();
sl@0
   505
	iUsb.AlternateDeviceStatusNotifyCancel();
sl@0
   506
	}
sl@0
   507
sl@0
   508
static TBool IsDriveConnected(TInt driveStatusIndex)
sl@0
   509
	{
sl@0
   510
	TInt driveStatus = PropertyHandlers::allDrivesStatus[2*driveStatusIndex+1];
sl@0
   511
	return driveStatus >= EUsbMsDriveState_Connected ? ETrue : EFalse;
sl@0
   512
	}
sl@0
   513
sl@0
   514
static TChar DriveNumberToLetter(TInt driveNumber)
sl@0
   515
	{
sl@0
   516
	TChar driveLetter = '?';
sl@0
   517
	fs.DriveToChar(driveNumber, driveLetter);
sl@0
   518
	return driveLetter;
sl@0
   519
	}
sl@0
   520
sl@0
   521
static TBool IsDriveInMountList(TUint driveLetter)
sl@0
   522
	{
sl@0
   523
	TUint16 driveLetter16 = static_cast<TUint16>(driveLetter);
sl@0
   524
	return(!mountList.Length() || KErrNotFound != mountList.Find(&driveLetter16, 1));
sl@0
   525
	}
sl@0
   526
sl@0
   527
void CUsbWatch::RunL()
sl@0
   528
	{
sl@0
   529
//	RDebug::Print(_L(">> CUsbWatch[%d] %d"), iUsbDeviceState, iWasConfigured);
sl@0
   530
sl@0
   531
//	const TUint stateMask = 0xFF;
sl@0
   532
//	iUsb.DeviceStateNotification(stateMask, iUsbDeviceState, iStatus);
sl@0
   533
	iUsb.AlternateDeviceStatusNotify(iStatus, iUsbDeviceState);
sl@0
   534
	SetActive();
sl@0
   535
sl@0
   536
	//RDebug::Print(_L("CUsbWatch DeviceStateNotification: iUsbDeviceState=%d"), iUsbDeviceState);
sl@0
   537
sl@0
   538
	// If the cable is disconnected, unmount all the connected drives.
sl@0
   539
	if(iWasConfigured && iUsbDeviceState == EUsbDeviceStateUndefined)
sl@0
   540
		{
sl@0
   541
		for(TInt i=0; i<PropertyHandlers::allDrivesStatus.Length()/2; i++)
sl@0
   542
			{
sl@0
   543
			if(IsDriveConnected(i))
sl@0
   544
				{
sl@0
   545
				//RDebug::Print(_L("CUsbWatch calling RestoreMount"));
sl@0
   546
				RestoreMount(PropertyHandlers::allDrivesStatus[2*i]);
sl@0
   547
				}
sl@0
   548
			}
sl@0
   549
		iWasConfigured = EFalse;
sl@0
   550
		}
sl@0
   551
sl@0
   552
    // If cable is connected, mount all drives in the auto-mount list. This is
sl@0
   553
    // done for performance, since if this is not done here, mounting will
sl@0
   554
    // happen later after each drive enters the Connecting state.
sl@0
   555
	if (iUsbDeviceState == EUsbDeviceStateConfigured)
sl@0
   556
		{
sl@0
   557
		for (TInt i=0; i<PropertyHandlers::allDrivesStatus.Length()/2; i++)
sl@0
   558
			{
sl@0
   559
			TInt driveNumber = PropertyHandlers::allDrivesStatus[2*i];
sl@0
   560
			if (!IsDriveConnected(i) && IsDriveInMountList(DriveNumberToLetter(driveNumber)))
sl@0
   561
				{
sl@0
   562
				//RDebug::Print(_L("CUsbWatch calling MountMsFs"));
sl@0
   563
				MountMsFs(driveNumber);
sl@0
   564
				}
sl@0
   565
			}
sl@0
   566
		iWasConfigured = ETrue;
sl@0
   567
		}
sl@0
   568
	}
sl@0
   569
sl@0
   570
//////////////////////////////////////////////////////////////////////////////
sl@0
   571
//
sl@0
   572
// PropertyHandlers
sl@0
   573
//
sl@0
   574
//////////////////////////////////////////////////////////////////////////////
sl@0
   575
sl@0
   576
TBuf8<16> PropertyHandlers::allDrivesStatus;
sl@0
   577
TUsbMsBytesTransferred PropertyHandlers::iKBytesRead;
sl@0
   578
TUsbMsBytesTransferred PropertyHandlers::iKBytesWritten;
sl@0
   579
TInt PropertyHandlers::iMediaError;
sl@0
   580
sl@0
   581
void PropertyHandlers::Read(RProperty& aProperty)
sl@0
   582
	{
sl@0
   583
	Transferred(aProperty, iKBytesRead);
sl@0
   584
	}
sl@0
   585
sl@0
   586
void PropertyHandlers::Written(RProperty& aProperty)
sl@0
   587
	{
sl@0
   588
	Transferred(aProperty, iKBytesWritten);
sl@0
   589
	}
sl@0
   590
sl@0
   591
void PropertyHandlers::Transferred(RProperty& aProperty, TUsbMsBytesTransferred& aReadOrWritten)
sl@0
   592
	{
sl@0
   593
	console->SetPos(0,1);
sl@0
   594
	console->Printf(_L("KB R/W:  "));
sl@0
   595
	TInt err = aProperty.Get(aReadOrWritten);
sl@0
   596
	if(err == KErrNone)
sl@0
   597
		{
sl@0
   598
		for(TInt i = 0; i < allDrivesStatus.Length()/2; i++)
sl@0
   599
			{
sl@0
   600
			console->Printf(KBytesTransferredFmt,
sl@0
   601
				(char)DriveNumberToLetter(allDrivesStatus[2*i]), iKBytesRead[i], iKBytesWritten[i]);
sl@0
   602
			}
sl@0
   603
		console->ClearToEndOfLine();
sl@0
   604
		}
sl@0
   605
	else
sl@0
   606
		{
sl@0
   607
		console->Printf(KErrFmt, err);
sl@0
   608
		}
sl@0
   609
	}
sl@0
   610
sl@0
   611
void PropertyHandlers::DriveStatus(RProperty& aProperty)
sl@0
   612
	{
sl@0
   613
//	RDebug::Print(_L(">> PropertyHandlers::DriveStatus"));
sl@0
   614
	TInt err = aProperty.Get(allDrivesStatus);
sl@0
   615
	console->SetPos(0,0);
sl@0
   616
	if(err == KErrNone)
sl@0
   617
		{
sl@0
   618
		console->Printf(_L("Status:  "));
sl@0
   619
		for(TInt i = 0; i < allDrivesStatus.Length()/2; i++)
sl@0
   620
			{
sl@0
   621
			TInt driveNumber = allDrivesStatus[2*i];
sl@0
   622
			TInt driveStatus = allDrivesStatus[2*i+1];
sl@0
   623
			TChar driveLetter = DriveNumberToLetter(driveNumber);
sl@0
   624
sl@0
   625
//			RDebug::Print(_L("%c:%d   "), (char)driveLetter, driveStatus);
sl@0
   626
sl@0
   627
			switch(driveStatus)
sl@0
   628
				{
sl@0
   629
				case EUsbMsDriveState_Disconnected:
sl@0
   630
					{
sl@0
   631
					LogPrint(_L("%c:%d:Disconnected "), (char)driveLetter, driveStatus);
sl@0
   632
					break;
sl@0
   633
					}
sl@0
   634
				case EUsbMsDriveState_Connecting:
sl@0
   635
					{
sl@0
   636
					LogPrint(_L("%c:%d:Connecting   "), (char)driveLetter, driveStatus);
sl@0
   637
					break;
sl@0
   638
					}
sl@0
   639
				case EUsbMsDriveState_Connected:
sl@0
   640
					{
sl@0
   641
					LogPrint(_L("%c:%d:Connected    "), (char)driveLetter, driveStatus);
sl@0
   642
					break;
sl@0
   643
					}
sl@0
   644
				case EUsbMsDriveState_Disconnecting:
sl@0
   645
					{
sl@0
   646
					LogPrint(_L("%c:%d:Disconnecting"), (char)driveLetter, driveStatus);
sl@0
   647
					break;
sl@0
   648
					}
sl@0
   649
				case EUsbMsDriveState_Active:
sl@0
   650
					{
sl@0
   651
					LogPrint(_L("%c:%d:Active       "), (char)driveLetter, driveStatus);
sl@0
   652
					break;
sl@0
   653
					}
sl@0
   654
				case EUsbMsDriveState_Locked:
sl@0
   655
					{
sl@0
   656
					LogPrint(_L("%c:%d:Locked       "), (char)driveLetter, driveStatus);
sl@0
   657
					break;
sl@0
   658
					}
sl@0
   659
				case EUsbMsDriveState_MediaNotPresent:
sl@0
   660
					{
sl@0
   661
					LogPrint(_L("%c:%d:Not Present  "), (char)driveLetter, driveStatus);
sl@0
   662
					break;
sl@0
   663
					}
sl@0
   664
				case EUsbMsDriveState_Removed:
sl@0
   665
					{
sl@0
   666
					LogPrint(_L("%c:%d:Removed      "), (char)driveLetter, driveStatus);
sl@0
   667
					break;
sl@0
   668
					}
sl@0
   669
				case EUsbMsDriveState_Error:
sl@0
   670
					{
sl@0
   671
					LogPrint(_L("%c:%d:Error        "), (char)driveLetter, driveStatus);
sl@0
   672
					break;
sl@0
   673
					}
sl@0
   674
				default :
sl@0
   675
					{
sl@0
   676
					LogPrint(_L("%c:%d:Unknown      "), (char)driveLetter, driveStatus);
sl@0
   677
					break;
sl@0
   678
					}
sl@0
   679
				}
sl@0
   680
sl@0
   681
			if(IsDriveInMountList(driveLetter))
sl@0
   682
				{
sl@0
   683
				if (driveStatus == EUsbMsDriveState_Connecting)
sl@0
   684
					{
sl@0
   685
					MountMsFs(driveNumber);
sl@0
   686
					}
sl@0
   687
				else if (driveStatus == EUsbMsDriveState_Disconnected)
sl@0
   688
					{
sl@0
   689
					RestoreMount(driveNumber);
sl@0
   690
					}
sl@0
   691
				else
sl@0
   692
					{
sl@0
   693
					//RDebug::Print(_L("PropertyHandlers::DriveStatus: nothing to do"));
sl@0
   694
					}
sl@0
   695
				}
sl@0
   696
			else
sl@0
   697
				{
sl@0
   698
				//RDebug::Print(_L("PropertyHandlers::DriveStatus: %c: is not in mountList\n"), driveLetter);
sl@0
   699
				}
sl@0
   700
			}
sl@0
   701
		}
sl@0
   702
	else
sl@0
   703
		{
sl@0
   704
		LogPrint(KErrFmt, err);
sl@0
   705
		}
sl@0
   706
sl@0
   707
	//RDebug::Print(_L("<< PropertyHandlers::DriveStatus"));
sl@0
   708
	}
sl@0
   709
sl@0
   710
void PropertyHandlers::MediaError(RProperty& aProperty)
sl@0
   711
	{
sl@0
   712
	TInt err = aProperty.Get(iMediaError);
sl@0
   713
	if (err != KErrNone)
sl@0
   714
		{
sl@0
   715
		// RDebug::Printf("RProperty::Get returned %d", err);
sl@0
   716
		return;
sl@0
   717
		}
sl@0
   718
sl@0
   719
	//RDebug::Printf("PropertyHandlers::MediaError %x", iMediaError);
sl@0
   720
sl@0
   721
	TInt x = console->WhereX();
sl@0
   722
	TInt y = console->WhereY();
sl@0
   723
	Clear(27,1);
sl@0
   724
	LogPrint(_L("Media Error %x"), iMediaError);
sl@0
   725
	// restore console position
sl@0
   726
	console->SetPos(x,y);
sl@0
   727
	}
sl@0
   728
sl@0
   729
//////////////////////////////////////////////////////////////////////////////
sl@0
   730
//
sl@0
   731
// CMessageKeyProcessor
sl@0
   732
//
sl@0
   733
//////////////////////////////////////////////////////////////////////////////
sl@0
   734
CMessageKeyProcessor::CMessageKeyProcessor(CConsoleBase* aConsole)
sl@0
   735
	: CActive(CActive::EPriorityUserInput), iConsole(aConsole)
sl@0
   736
	{
sl@0
   737
	}
sl@0
   738
sl@0
   739
CMessageKeyProcessor* CMessageKeyProcessor::NewLC(CConsoleBase* aConsole)
sl@0
   740
	{
sl@0
   741
	CMessageKeyProcessor* self=new (ELeave) CMessageKeyProcessor(aConsole);
sl@0
   742
	CleanupStack::PushL(self);
sl@0
   743
	self->ConstructL();
sl@0
   744
	return self;
sl@0
   745
	}
sl@0
   746
sl@0
   747
CMessageKeyProcessor* CMessageKeyProcessor::NewL(CConsoleBase* aConsole)
sl@0
   748
	{
sl@0
   749
	CMessageKeyProcessor* self = NewLC(aConsole);
sl@0
   750
	CleanupStack::Pop();
sl@0
   751
	return self;
sl@0
   752
	}
sl@0
   753
sl@0
   754
void CMessageKeyProcessor::ConstructL()
sl@0
   755
	{
sl@0
   756
	// Add to active scheduler
sl@0
   757
	CActiveScheduler::Add(this);
sl@0
   758
	RequestCharacter();
sl@0
   759
	}
sl@0
   760
sl@0
   761
void CMessageKeyProcessor::MakePassword(TMediaPassword &aPassword)
sl@0
   762
	{
sl@0
   763
	//  Create password with same format as eshell and S60
sl@0
   764
	TBuf<3> password(KDefPwd);
sl@0
   765
sl@0
   766
	// fill aPassword with contents of password, not converting to ASCII
sl@0
   767
	const TInt byteLen = password.Length() * 2;
sl@0
   768
	aPassword.Copy(reinterpret_cast<const TUint8 *>(password.Ptr()), byteLen);
sl@0
   769
	}
sl@0
   770
sl@0
   771
CMessageKeyProcessor::~CMessageKeyProcessor()
sl@0
   772
	{
sl@0
   773
	// Make sure we're cancelled
sl@0
   774
	Cancel();
sl@0
   775
	}
sl@0
   776
sl@0
   777
void  CMessageKeyProcessor::DoCancel()
sl@0
   778
	{
sl@0
   779
	iConsole->ReadCancel();
sl@0
   780
	}
sl@0
   781
sl@0
   782
void  CMessageKeyProcessor::RunL()
sl@0
   783
	{
sl@0
   784
	  // Handle completed request
sl@0
   785
	ProcessKeyPress(TChar(iConsole->KeyCode()));
sl@0
   786
	}
sl@0
   787
sl@0
   788
void CMessageKeyProcessor::RequestCharacter()
sl@0
   789
	{
sl@0
   790
	  // A request is issued to the CConsoleBase to accept a
sl@0
   791
	  // character from the keyboard.
sl@0
   792
	iConsole->Read(iStatus);
sl@0
   793
	SetActive();
sl@0
   794
	}
sl@0
   795
sl@0
   796
void CMessageKeyProcessor::ProcessKeyPress(TChar aChar)
sl@0
   797
	{
sl@0
   798
sl@0
   799
	TInt error = KErrNone;
sl@0
   800
sl@0
   801
    aChar.UpperCase();
sl@0
   802
	switch(aChar)
sl@0
   803
		{
sl@0
   804
		case 'Q':
sl@0
   805
		case EKeyEscape:
sl@0
   806
			{
sl@0
   807
			TInt err = KErrNone;
sl@0
   808
			for(TInt j=0; j<KMaxDrives; j++)
sl@0
   809
				{
sl@0
   810
				err = RestoreMount(j);
sl@0
   811
sl@0
   812
				if (err)
sl@0
   813
					{
sl@0
   814
					// Mount is busy/locked and can not be restored.
sl@0
   815
					break;
sl@0
   816
					}
sl@0
   817
sl@0
   818
				}
sl@0
   819
sl@0
   820
			if (err == KErrNone)
sl@0
   821
				{
sl@0
   822
				CActiveScheduler::Stop();
sl@0
   823
				return;
sl@0
   824
				}
sl@0
   825
sl@0
   826
			}
sl@0
   827
			break;
sl@0
   828
sl@0
   829
#if defined(_DEBUG)
sl@0
   830
		case 'T':
sl@0
   831
			iTraceEnable = !iTraceEnable;
sl@0
   832
			if (iTraceEnable)	// 0x44008401
sl@0
   833
				User::SetDebugMask(KHARDWARE|KDLL|KSCRATCH|KPOWER|KMEMTRACE);
sl@0
   834
			else
sl@0
   835
				User::SetDebugMask(0);
sl@0
   836
			break;
sl@0
   837
#endif
sl@0
   838
sl@0
   839
		case 'D':
sl@0
   840
			if(++selectedDriveIndex >= PropertyHandlers::allDrivesStatus.Length()/2)
sl@0
   841
				{
sl@0
   842
				selectedDriveIndex = 0;
sl@0
   843
				}
sl@0
   844
			ShowDriveSelection();
sl@0
   845
			break;
sl@0
   846
sl@0
   847
		case 'M':
sl@0
   848
			if(PropertyHandlers::allDrivesStatus.Length())
sl@0
   849
				{
sl@0
   850
				MountMsFs(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2]);
sl@0
   851
				}
sl@0
   852
			break;
sl@0
   853
sl@0
   854
		case 'U':
sl@0
   855
			if(PropertyHandlers::allDrivesStatus.Length())
sl@0
   856
				{
sl@0
   857
				RestoreMount(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2]);
sl@0
   858
				}
sl@0
   859
			break;
sl@0
   860
sl@0
   861
		case 'L':
sl@0
   862
			{
sl@0
   863
			// lock unprotected drive
sl@0
   864
			TMediaPassword password;
sl@0
   865
			MakePassword(password);
sl@0
   866
sl@0
   867
			_LIT(KEmpty, "");
sl@0
   868
			TMediaPassword nul;
sl@0
   869
			nul.Copy(KEmpty);
sl@0
   870
			error = fs.LockDrive(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2],
sl@0
   871
                                 nul, password, ETrue);
sl@0
   872
			console->SetPos(0,9);
sl@0
   873
			LogPrint(_L("LockDrive %S (%d)"), (error?&KError:&KOk), error);
sl@0
   874
			break;
sl@0
   875
			}
sl@0
   876
sl@0
   877
		case 'I':
sl@0
   878
            {
sl@0
   879
            // lock password protected drive
sl@0
   880
            TMediaPassword password;
sl@0
   881
            MakePassword(password);
sl@0
   882
            error = fs.LockDrive(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2],
sl@0
   883
                                 password, password, ETrue);
sl@0
   884
            console->SetPos(0,9);
sl@0
   885
            LogPrint(_L("LockDrive %S (%d)"), (error?&KError:&KOk), error);
sl@0
   886
            break;
sl@0
   887
            }
sl@0
   888
sl@0
   889
        case 'N':
sl@0
   890
            {
sl@0
   891
            TMediaPassword password;
sl@0
   892
            MakePassword(password);
sl@0
   893
            error = fs.UnlockDrive(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2],
sl@0
   894
                                   password, ETrue);
sl@0
   895
            Clear(9);
sl@0
   896
            LogPrint(_L("UnlockDrive %S (%d)"), (error?&KError:&KOk), error);
sl@0
   897
            }
sl@0
   898
			break;
sl@0
   899
sl@0
   900
        case 'C':
sl@0
   901
            {
sl@0
   902
            TMediaPassword password;
sl@0
   903
            MakePassword(password);
sl@0
   904
            error = fs.ClearPassword(PropertyHandlers::allDrivesStatus[selectedDriveIndex*2],
sl@0
   905
                                     password);
sl@0
   906
            Clear(9);
sl@0
   907
            LogPrint(_L("ClearPassword %S (%d)"), (error?&KError:&KOk), error);
sl@0
   908
            }
sl@0
   909
			break;
sl@0
   910
		default:
sl@0
   911
			break;
sl@0
   912
		}
sl@0
   913
	RequestCharacter();
sl@0
   914
	}
sl@0
   915
sl@0
   916
sl@0
   917
//////////////////////////////////////////////////////////////////////////////
sl@0
   918
//
sl@0
   919
// Application entry point
sl@0
   920
//
sl@0
   921
//////////////////////////////////////////////////////////////////////////////
sl@0
   922
static void RunAppL()
sl@0
   923
	{
sl@0
   924
sl@0
   925
    TInt error = KErrUnknown;
sl@0
   926
sl@0
   927
	//RDebug::Print(_L("USBMSAPP: Creating console\n"));
sl@0
   928
	console = Console::NewL(KTxtApp,TSize(KConsFullScreen,KConsFullScreen));
sl@0
   929
	CleanupStack::PushL(console);
sl@0
   930
sl@0
   931
	console->SetPos(0,2);
sl@0
   932
	console->Printf(_L("========================================"));
sl@0
   933
sl@0
   934
	// Command line: list of drive letters to auto-mount (all if not specified)
sl@0
   935
	User::CommandLine(mountList);
sl@0
   936
	mountList.UpperCase();
sl@0
   937
sl@0
   938
	CActiveScheduler* sched = new(ELeave) CActiveScheduler;
sl@0
   939
	CleanupStack::PushL(sched);
sl@0
   940
	CActiveScheduler::Install(sched);
sl@0
   941
sl@0
   942
	fs.Connect();
sl@0
   943
	CleanupClosePushL(fs);
sl@0
   944
sl@0
   945
	_LIT(KMountAllDefault,"(all)");
sl@0
   946
	console->SetPos(0,3);
sl@0
   947
	LogPrint(_L("Drives to auto-mount: %S"), (mountList.Length() ? &mountList : &KMountAllDefault));
sl@0
   948
sl@0
   949
	// Add MS file system
sl@0
   950
	error = fs.AddFileSystem(KMsFsy);
sl@0
   951
	if(error != KErrNone && error != KErrAlreadyExists)
sl@0
   952
		{
sl@0
   953
		//RDebug::Print(_L("AddFileSystem failed, err=%d\n"), error);
sl@0
   954
		User::Leave(error);
sl@0
   955
		}
sl@0
   956
	console->SetPos(0,4);
sl@0
   957
	LogPrint(_L("MSFS file system:\tAdded OK\n"));
sl@0
   958
sl@0
   959
	RUsb usb;
sl@0
   960
sl@0
   961
	// Load the logical device
sl@0
   962
	_LIT(KDriverFileName,"EUSBC.LDD");
sl@0
   963
	error = User::LoadLogicalDevice(KDriverFileName);
sl@0
   964
	if (error != KErrAlreadyExists)
sl@0
   965
		User::LeaveIfError(error);
sl@0
   966
sl@0
   967
	error = usb.Open(0);
sl@0
   968
	User::LeaveIfError(error);
sl@0
   969
sl@0
   970
	_LIT(KOtgdiLddFilename, "otgdi");
sl@0
   971
	// Check for OTG support
sl@0
   972
	TBuf8<KUsbDescSize_Otg> otg_desc;
sl@0
   973
	error = usb.GetOtgDescriptor(otg_desc);
sl@0
   974
	if (!(error == KErrNotSupported || error == KErrNone))
sl@0
   975
		{
sl@0
   976
		LogPrint(_L("Error %d while fetching OTG descriptor"), error);
sl@0
   977
		User::Leave(-1);
sl@0
   978
		return;
sl@0
   979
		}
sl@0
   980
sl@0
   981
	// On an OTG device we have to start the OTG driver, otherwise the Client
sl@0
   982
	// stack will remain disabled forever.
sl@0
   983
	if (error == KErrNotSupported)
sl@0
   984
	{
sl@0
   985
		CleanupClosePushL(usb);
sl@0
   986
		User::Leave(-1);
sl@0
   987
	}
sl@0
   988
sl@0
   989
	error = User::LoadLogicalDevice(KOtgdiLddFilename);
sl@0
   990
	if (error != KErrNone)
sl@0
   991
		{
sl@0
   992
		LogPrint(_L("Error %d on loading OTG LDD"), error);
sl@0
   993
		User::Leave(-1);
sl@0
   994
		return;
sl@0
   995
		}
sl@0
   996
sl@0
   997
	RUsbOtgDriver iOtgPort;
sl@0
   998
sl@0
   999
	error = iOtgPort.Open();
sl@0
  1000
	if (error != KErrNone)
sl@0
  1001
		{
sl@0
  1002
		LogPrint(_L("Error %d on opening OTG port"), error);
sl@0
  1003
		User::Leave(-1);
sl@0
  1004
		return;
sl@0
  1005
		}
sl@0
  1006
	error = iOtgPort.StartStacks();
sl@0
  1007
	if (error != KErrNone)
sl@0
  1008
		{
sl@0
  1009
		LogPrint(_L("Error %d on starting USB stack"), error);
sl@0
  1010
		User::Leave(-1);
sl@0
  1011
		return;
sl@0
  1012
		}
sl@0
  1013
sl@0
  1014
	CleanupClosePushL(usb);
sl@0
  1015
sl@0
  1016
//		RDebug::Print(_L("USBMSAPP: Create active objects\n"));
sl@0
  1017
	CMessageKeyProcessor::NewLC(console);
sl@0
  1018
	CPropertyWatch::NewLC(EUsbMsDriveState_KBytesRead, PropertyHandlers::Read);
sl@0
  1019
	CPropertyWatch::NewLC(EUsbMsDriveState_KBytesWritten, PropertyHandlers::Written);
sl@0
  1020
	CPropertyWatch::NewLC(EUsbMsDriveState_DriveStatus, PropertyHandlers::DriveStatus);
sl@0
  1021
	CPropertyWatch::NewLC(EUsbMsDriveState_MediaError, PropertyHandlers::MediaError);
sl@0
  1022
	CUsbWatch::NewLC(usb);
sl@0
  1023
	CPeriodUpdate::NewLC();
sl@0
  1024
sl@0
  1025
	RUsbMassStorage UsbMs;
sl@0
  1026
	TBuf<8>  t_vendorId(_L("vendor"));
sl@0
  1027
	TBuf<16> t_productId(_L("product"));
sl@0
  1028
	TBuf<4>  t_productRev(_L("1.00"));
sl@0
  1029
sl@0
  1030
	TMassStorageConfig msConfig;
sl@0
  1031
	msConfig.iVendorId.Copy(t_vendorId);
sl@0
  1032
	msConfig.iProductId.Copy(t_productId);
sl@0
  1033
	msConfig.iProductRev.Copy(t_productRev);
sl@0
  1034
sl@0
  1035
//   	console->Printf(_L("Connect to Mass Storage"));
sl@0
  1036
	error = UsbMs.Connect();
sl@0
  1037
	User::LeaveIfError(error);
sl@0
  1038
sl@0
  1039
//   	console->Printf(_L("Start Mass Storage"));
sl@0
  1040
	error = UsbMs.Start(msConfig);
sl@0
  1041
	User::LeaveIfError(error);
sl@0
  1042
sl@0
  1043
	TBuf8<KUsbDescSize_Device> deviceDescriptor;
sl@0
  1044
	error = usb.GetDeviceDescriptor(deviceDescriptor);
sl@0
  1045
	User::LeaveIfError(error);
sl@0
  1046
sl@0
  1047
	const TInt KUsbSpecOffset = 2;
sl@0
  1048
	const TInt KUsbDeviceClassOffset = 4;
sl@0
  1049
	const TInt KUsbVendorIdOffset = 8;
sl@0
  1050
	const TInt KUsbProductIdOffset = 10;
sl@0
  1051
	const TInt KUsbDevReleaseOffset = 12;
sl@0
  1052
	//Change the USB spec number to 2.00
sl@0
  1053
	deviceDescriptor[KUsbSpecOffset]   = 0x00;
sl@0
  1054
	deviceDescriptor[KUsbSpecOffset+1] = 0x02;
sl@0
  1055
	//Change the Device Class, Device SubClass and Device Protocol
sl@0
  1056
	deviceDescriptor[KUsbDeviceClassOffset] = 0x00;
sl@0
  1057
	deviceDescriptor[KUsbDeviceClassOffset+1] = 0x00;
sl@0
  1058
	deviceDescriptor[KUsbDeviceClassOffset+2] = 0x00;
sl@0
  1059
	//Change the device vendor ID (VID) to 0x0E22 (Symbian)
sl@0
  1060
	deviceDescriptor[KUsbVendorIdOffset]   = 0x22;   // little endian
sl@0
  1061
	deviceDescriptor[KUsbVendorIdOffset+1] = 0x0E;
sl@0
  1062
	//Change the device product ID (PID) to 0x1111
sl@0
  1063
	deviceDescriptor[KUsbProductIdOffset]   = 0x12;
sl@0
  1064
	deviceDescriptor[KUsbProductIdOffset+1] = 0x11;
sl@0
  1065
	//Change the device release number to 3.05
sl@0
  1066
	deviceDescriptor[KUsbDevReleaseOffset]   = 0x05;
sl@0
  1067
	deviceDescriptor[KUsbDevReleaseOffset+1] = 0x03;
sl@0
  1068
	error = usb.SetDeviceDescriptor(deviceDescriptor);
sl@0
  1069
	User::LeaveIfError(error);
sl@0
  1070
sl@0
  1071
	// Remove possible Remote-Wakup support in Configuration descriptor,
sl@0
  1072
	// so that we can use the MSC device also easily for Chapter9 testing.
sl@0
  1073
	TBuf8<KUsbDescSize_Config> configDescriptor;
sl@0
  1074
	error = usb.GetConfigurationDescriptor(configDescriptor);
sl@0
  1075
	User::LeaveIfError(error);
sl@0
  1076
	const TInt KConfDesc_AttribOffset = 7;
sl@0
  1077
	configDescriptor[KConfDesc_AttribOffset] &= ~KUsbDevAttr_RemoteWakeup;
sl@0
  1078
	error = usb.SetConfigurationDescriptor(configDescriptor);
sl@0
  1079
	User::LeaveIfError(error);
sl@0
  1080
sl@0
  1081
	_LIT16(productID_L, "Symbian USB Mass Storage Device (Base)");
sl@0
  1082
	TBuf16<KUsbStringDescStringMaxSize / 2> productID(productID_L);
sl@0
  1083
	error = usb.SetProductStringDescriptor(productID);
sl@0
  1084
	User::LeaveIfError(error);
sl@0
  1085
sl@0
  1086
	TRequestStatus enum_status;
sl@0
  1087
	console->SetPos(0,5);
sl@0
  1088
	LogPrint(_L("Re-enumerating...\n"));
sl@0
  1089
	usb.ReEnumerate(enum_status);
sl@0
  1090
	User::LeaveIfError(error);
sl@0
  1091
	console->SetPos(0,5);
sl@0
  1092
	User::WaitForRequest(enum_status);
sl@0
  1093
	if(enum_status.Int() == KErrNone)
sl@0
  1094
		LogPrint(_L("Re-enumeration Done\n"));
sl@0
  1095
	else
sl@0
  1096
		LogPrint(_L("Re-enumeration not successfully done\n"));
sl@0
  1097
sl@0
  1098
sl@0
  1099
    console->SetPos(0,14);
sl@0
  1100
    TBuf<3>password(KDefPwd);
sl@0
  1101
    LogPrint(_L("Password: %S"), &password);
sl@0
  1102
sl@0
  1103
	ShowDriveSelection();
sl@0
  1104
sl@0
  1105
	console->SetPos(0,17);
sl@0
  1106
sl@0
  1107
	_LIT(KMsgTitleB,"Menu: q=quit  d=chg drv\n      m=mount u=unmount\n       l=lock i=lock n=unlock\n      c=clr pwd");
sl@0
  1108
sl@0
  1109
sl@0
  1110
	//RDebug::Print(_L("USBMSAPP: Start CActiveScheduler\n"));
sl@0
  1111
sl@0
  1112
	console->Printf(KMsgTitleB);
sl@0
  1113
sl@0
  1114
	CActiveScheduler::Start();
sl@0
  1115
sl@0
  1116
	error = UsbMs.Stop();
sl@0
  1117
	User::LeaveIfError(error);
sl@0
  1118
	UsbMs.Close();
sl@0
  1119
	error = fs.RemoveFileSystem(KMsFs);
sl@0
  1120
	User::LeaveIfError(error);
sl@0
  1121
sl@0
  1122
	CleanupStack::PopAndDestroy(11);
sl@0
  1123
sl@0
  1124
	iOtgPort.StopStacks();
sl@0
  1125
	iOtgPort.Close();
sl@0
  1126
	error = User::FreeLogicalDevice(RUsbOtgDriver::Name());
sl@0
  1127
	User::LeaveIfError(error);
sl@0
  1128
sl@0
  1129
	error = User::FreeLogicalDevice(_L("USBC"));
sl@0
  1130
	User::LeaveIfError(error);
sl@0
  1131
sl@0
  1132
	}
sl@0
  1133
sl@0
  1134
GLDEF_C TInt E32Main()
sl@0
  1135
	{
sl@0
  1136
	__UHEAP_MARK;
sl@0
  1137
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
  1138
sl@0
  1139
    msfsMountedList.Reset();
sl@0
  1140
    unmountedFsList.Reset();
sl@0
  1141
sl@0
  1142
sl@0
  1143
	TRAPD(error,RunAppL());
sl@0
  1144
	__ASSERT_ALWAYS(!error, User::Panic(KTxtApp, error));
sl@0
  1145
sl@0
  1146
	delete cleanup;
sl@0
  1147
	__UHEAP_MARKEND;
sl@0
  1148
	return 0;
sl@0
  1149
	}
sl@0
  1150
sl@0
  1151
sl@0
  1152
//-----------------------------------------------------------------------------
sl@0
  1153
sl@0
  1154
CFileSystemDescriptor::~CFileSystemDescriptor()
sl@0
  1155
    {
sl@0
  1156
    iFsName.Close();
sl@0
  1157
    iPrimaryExtName.Close();
sl@0
  1158
    }
sl@0
  1159
sl@0
  1160
//-----------------------------------------------------------------------------
sl@0
  1161
CFileSystemDescriptor* CFileSystemDescriptor::NewL(const TDesC& aFsName, const TDesC& aPrimaryExtName, TBool aDrvSynch)
sl@0
  1162
    {
sl@0
  1163
    CFileSystemDescriptor* pSelf = new (ELeave) CFileSystemDescriptor;
sl@0
  1164
sl@0
  1165
    CleanupStack::PushL(pSelf);
sl@0
  1166
sl@0
  1167
    pSelf->iFsName.CreateMaxL(aFsName.Length());
sl@0
  1168
    pSelf->iFsName.Copy(aFsName);
sl@0
  1169
sl@0
  1170
    pSelf->iPrimaryExtName.CreateMaxL(aPrimaryExtName.Length());
sl@0
  1171
    pSelf->iPrimaryExtName.Copy(aPrimaryExtName);
sl@0
  1172
sl@0
  1173
    pSelf->iDriveSynch = aDrvSynch;
sl@0
  1174
sl@0
  1175
    CleanupStack::Pop();
sl@0
  1176
sl@0
  1177
    return pSelf;
sl@0
  1178
    }