os/kernelhwsrv/kerneltest/e32test/pccd/t_medch.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1997-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
// e32test\pccd\t_medch.cpp
sl@0
    15
// Continuously generate media changes followesd by a remount of the peripheral bus controller.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#define __E32TEST_EXTENSION__
sl@0
    20
#include <e32test.h>
sl@0
    21
#include <e32svr.h>
sl@0
    22
#include <hal.h>
sl@0
    23
#include "d_medch.h"
sl@0
    24
sl@0
    25
//#define __SOAK_TEST__				// Define to run until a key is pressed (Automatic tests only)
sl@0
    26
//#define __MANUAL_TEST__			// Define to allow manual control of the door/media
sl@0
    27
//#define __DEVICE_HAS_NO_DOOR__	// Define for devices that have no door (Manual tests only)
sl@0
    28
sl@0
    29
#if defined(__MANUAL_TEST__) && defined(__WINS__)
sl@0
    30
#define __DEVICE_HAS_NO_DOOR__
sl@0
    31
#endif
sl@0
    32
sl@0
    33
#if !defined(__MANUAL_TEST__) && defined(__DEVICE_HAS_NO_DOOR__)
sl@0
    34
#undef __DEVICE_HAS_NO_DOOR__
sl@0
    35
#endif
sl@0
    36
sl@0
    37
#if defined(__MANUAL_TEST__) && defined(__SOAK_TEST__)
sl@0
    38
#undef __SOAK_TEST__
sl@0
    39
#endif
sl@0
    40
sl@0
    41
#ifndef __SOAK_TEST__
sl@0
    42
#ifdef __WINS__
sl@0
    43
const TInt KMaxTestTime	=  5000000;	// Run the test for 5 seconds on emulator
sl@0
    44
#else
sl@0
    45
const TInt KMaxTestTime	= 10000000;	// Run the test for 10 seconds on target
sl@0
    46
#endif
sl@0
    47
#endif
sl@0
    48
sl@0
    49
#define MMC_PDD_NAME _L("MEDMMC")
sl@0
    50
sl@0
    51
const TInt KPowerUpTimeOut = 5000000; // Give the card 5 seconds to power up
sl@0
    52
sl@0
    53
LOCAL_D	RTest test(_L("Media change test"));
sl@0
    54
sl@0
    55
LOCAL_D	TBusLocalDrive TheDrive;
sl@0
    56
LOCAL_D	RMedCh TheMediaChange;
sl@0
    57
LOCAL_D TRequestStatus TheMediaStatus;
sl@0
    58
LOCAL_D TBool TheChangedFlag;
sl@0
    59
sl@0
    60
sl@0
    61
LOCAL_C TBool SetupDrivesForPlatform(TInt& aDrive, TInt& aSocket)
sl@0
    62
/**
sl@0
    63
 * Finds a suitable drive for the media change test
sl@0
    64
 *
sl@0
    65
 * @param aDrive  The number of the local drive to test
sl@0
    66
 * @param aSocket The number of the socket to test
sl@0
    67
 * @return TBool ETrue if a suitable drive is found, EFalse otherwise.
sl@0
    68
 */
sl@0
    69
	{
sl@0
    70
	
sl@0
    71
	TDriveInfoV1Buf diBuf;
sl@0
    72
	UserHal::DriveInfo(diBuf);
sl@0
    73
	TDriveInfoV1 &di=diBuf();
sl@0
    74
sl@0
    75
	aDrive  = -1;
sl@0
    76
	aSocket = -1;
sl@0
    77
	
sl@0
    78
	for(aDrive=0; aDrive < di.iTotalSupportedDrives; aDrive++)
sl@0
    79
		{
sl@0
    80
		test.Printf(_L(" Drive %d - %S\r\n"), aDrive, &di.iDriveName[aDrive]);
sl@0
    81
		if(di.iDriveName[aDrive].MatchF(_L("MultiMediaCard0")) == KErrNone)
sl@0
    82
			break;
sl@0
    83
		}
sl@0
    84
sl@0
    85
	if(aDrive == di.iTotalSupportedDrives)
sl@0
    86
		{
sl@0
    87
		test.Printf(_L(" MMC Drive Not Found\r\n"));
sl@0
    88
		return EFalse;
sl@0
    89
		}
sl@0
    90
		
sl@0
    91
	
sl@0
    92
	for(aSocket=0; aSocket < di.iTotalSockets; aSocket++)
sl@0
    93
		{
sl@0
    94
		test.Printf(_L("Socket %d - %S\r\n"), aSocket, &di.iSocketName[aSocket]);
sl@0
    95
		if(di.iSocketName[aSocket].MatchF(_L("MultiMediaCard0")) == KErrNone)
sl@0
    96
			break;
sl@0
    97
		}
sl@0
    98
sl@0
    99
	if(aSocket == di.iTotalSockets)
sl@0
   100
		{
sl@0
   101
		test.Printf(_L(" MMC Socket Not Found\r\n"));
sl@0
   102
		return EFalse;
sl@0
   103
		}
sl@0
   104
sl@0
   105
	return ETrue;
sl@0
   106
	}
sl@0
   107
sl@0
   108
LOCAL_C void TestMediaAccess(TInt aExpectedError, TBool aExpectedChange)
sl@0
   109
/**
sl@0
   110
 * Tests that the drive is accessable (or not) by issuing a request 
sl@0
   111
 * to power up the media.  Also verifies that the attributes are correct.
sl@0
   112
 * 
sl@0
   113
 * @param aExpectedError The expected result of powering up the drive
sl@0
   114
 * @param aExpectedChange ETrue if the changed flag is expected to be set
sl@0
   115
 *
sl@0
   116
 * @return ETrue if successful, EFalse otherwise
sl@0
   117
 */
sl@0
   118
	{
sl@0
   119
	
sl@0
   120
	RTimer rto;
sl@0
   121
	TInt r = rto.CreateLocal();
sl@0
   122
	test(r == KErrNone);
sl@0
   123
	
sl@0
   124
	TRequestStatus rtoStat;
sl@0
   125
	rto.After(rtoStat, KPowerUpTimeOut);
sl@0
   126
	test(rtoStat == KRequestPending);
sl@0
   127
	
sl@0
   128
	if(aExpectedChange)
sl@0
   129
		{
sl@0
   130
		// TheChangedFlag is set when the door is opened if media was present.
sl@0
   131
		// The asynch notifier is signalled when media is removed OR inserted.
sl@0
   132
		User::WaitForRequest(TheMediaStatus, rtoStat);
sl@0
   133
		test(TheMediaStatus != KRequestPending);
sl@0
   134
		}
sl@0
   135
sl@0
   136
	// ...aChangedFlag's purpose is to notify us of media removal.
sl@0
   137
	test_Equal(aExpectedChange,TheChangedFlag);
sl@0
   138
sl@0
   139
	TheDrive.NotifyChange(&TheMediaStatus);
sl@0
   140
	TheChangedFlag = EFalse;
sl@0
   141
sl@0
   142
	// Attempt to power up the drive
sl@0
   143
	TLocalDriveCapsV2Buf info;
sl@0
   144
	do
sl@0
   145
		{
sl@0
   146
		r = TheDrive.Caps(info);
sl@0
   147
		}
sl@0
   148
	while(r != aExpectedError && rtoStat == KRequestPending);
sl@0
   149
sl@0
   150
	rto.Cancel();
sl@0
   151
	rto.Close();
sl@0
   152
sl@0
   153
	// ...was the error as expected?
sl@0
   154
	test(r == aExpectedError);
sl@0
   155
sl@0
   156
	// ...and are the caps still OK?
sl@0
   157
	if(r == KErrNone)
sl@0
   158
		test(info().iType == EMediaHardDisk);
sl@0
   159
	else if(r == KErrNotReady)
sl@0
   160
		test(info().iType == EMediaNotPresent);
sl@0
   161
sl@0
   162
	if(aExpectedChange == EFalse)
sl@0
   163
		test(TheMediaStatus == KRequestPending);
sl@0
   164
	}
sl@0
   165
sl@0
   166
LOCAL_C void NextTest(const TDesC& aTitle, TInt aCycles)
sl@0
   167
/**
sl@0
   168
 * Simply displays a string on the console and the current iteration.
sl@0
   169
 * 
sl@0
   170
 * @param aTitle  The text to be displayed
sl@0
   171
 * @param aCycles The current iteration
sl@0
   172
 */
sl@0
   173
	{
sl@0
   174
	test.Console()->SetPos(20, 25);
sl@0
   175
	test.Printf(_L("%S [%d cycles]"), &aTitle, aCycles);
sl@0
   176
#ifdef __MANUAL_TEST__
sl@0
   177
	test.Console()->SetPos(20, 27);
sl@0
   178
	test.Printf(_L("<press a key>"));
sl@0
   179
	test.Getch();
sl@0
   180
#endif
sl@0
   181
	}
sl@0
   182
		
sl@0
   183
GLDEF_C TInt E32Main()
sl@0
   184
/**
sl@0
   185
 * Test Entry Point for T_MEDCH.
sl@0
   186
 * 
sl@0
   187
 * This test uses the associated driver (D_MEDCH) to simulate media removal and 
sl@0
   188
 * door opening/closing.  The media is powered up in each state and verified that 
sl@0
   189
 * the correct error code and changed count is returned.
sl@0
   190
 */
sl@0
   191
    {
sl@0
   192
	TBuf<64> b;
sl@0
   193
	test.Title();
sl@0
   194
sl@0
   195
	/**
sl@0
   196
	 * Load the associated media driver (MEDMMC by default).  This is required to ensure 
sl@0
   197
	 * that the device can be powered up and the capabilities if the media accessed.
sl@0
   198
	 */
sl@0
   199
	test.Start(_L("Load Media Driver"));
sl@0
   200
	TInt r;
sl@0
   201
	r=User::LoadPhysicalDevice(MMC_PDD_NAME);
sl@0
   202
	if(r==KErrNotFound)
sl@0
   203
		{
sl@0
   204
		test.Printf(_L("Test not supported on this platform \n"));
sl@0
   205
		test.End();
sl@0
   206
		return(0);
sl@0
   207
		}
sl@0
   208
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   209
sl@0
   210
	/**
sl@0
   211
	 * Connect to the required local drive.
sl@0
   212
	 * TheChangedFlag is used for detection of media removal.
sl@0
   213
	 */
sl@0
   214
	TInt drive;
sl@0
   215
	TInt socket;
sl@0
   216
	if(SetupDrivesForPlatform(drive, socket))
sl@0
   217
		{
sl@0
   218
		b.Format(_L("Connect to local drive %d"), drive);
sl@0
   219
		test.Next(b);
sl@0
   220
		TheDrive.Connect(drive, TheChangedFlag);
sl@0
   221
	
sl@0
   222
		/**
sl@0
   223
		 * Read the drive capabilities to ensure that this test may be run.
sl@0
   224
		 */
sl@0
   225
		test.Next(_L("Get drive capabilities"));
sl@0
   226
		TLocalDriveCapsV2Buf info;
sl@0
   227
		r = TheDrive.Caps(info);
sl@0
   228
		if(r == KErrNotReady || r == KErrNotSupported)
sl@0
   229
			{
sl@0
   230
			test.Next(_L("\r\nTest requires media to be present and the door closed - Disconnecting"));
sl@0
   231
			TheDrive.Disconnect();		
sl@0
   232
			test.End();
sl@0
   233
			return KErrNone;
sl@0
   234
			}	
sl@0
   235
		test(r == KErrNone);
sl@0
   236
	
sl@0
   237
		test(TheDrive.Caps(info) == KErrNone);
sl@0
   238
		test(info().iType == EMediaHardDisk);
sl@0
   239
	
sl@0
   240
		/**
sl@0
   241
		 * Load the media simulation test driver
sl@0
   242
		 */
sl@0
   243
		test.Next(_L("Load media change logical device"));
sl@0
   244
		r=User::LoadLogicalDevice(_L("D_MEDCH"));
sl@0
   245
		test(r == KErrNone || r == KErrAlreadyExists);
sl@0
   246
	
sl@0
   247
		test.Next(_L("Open device"));
sl@0
   248
		r=TheMediaChange.Open(socket, TheMediaChange.VersionRequired());
sl@0
   249
		if(r == KErrNotSupported)
sl@0
   250
			{
sl@0
   251
			test.Next(_L("\r\nTest not supported on this drive - Disconnecting"));
sl@0
   252
			r=User::FreeLogicalDevice(_L("MedCh"));
sl@0
   253
			test(r == KErrNone);
sl@0
   254
			TheDrive.Disconnect();
sl@0
   255
			test.End();
sl@0
   256
			return KErrNone;
sl@0
   257
			}	
sl@0
   258
		test(r == KErrNone);
sl@0
   259
	
sl@0
   260
		/**
sl@0
   261
		 * Verify that the system supports simulation of media change events
sl@0
   262
		 */
sl@0
   263
		test.Next(_L("Test support for media change simulation"));
sl@0
   264
		r = TheMediaChange.DoorNormal();
sl@0
   265
		test(r == KErrNone || r == KErrNotSupported);
sl@0
   266
	
sl@0
   267
		/**
sl@0
   268
		 * Now for the real testing...
sl@0
   269
		 */
sl@0
   270
		if(r == KErrNone)
sl@0
   271
			{
sl@0
   272
			/**
sl@0
   273
	    	 * Test0 - Simulate 2 consecutive door open interrupts
sl@0
   274
			 */
sl@0
   275
			test.Next(_L("Test that the pbus can handle 2 consecutive door open interrupts"));
sl@0
   276
			TheDrive.NotifyChange(&TheMediaStatus);
sl@0
   277
			r = TheMediaChange.DoubleDoorOpen();
sl@0
   278
			test(r == KErrNone || r == KErrNotSupported);
sl@0
   279
			TestMediaAccess(KErrNone, ETrue);
sl@0
   280
		
sl@0
   281
sl@0
   282
			TInt cycles=0;
sl@0
   283
#if defined(__SOAK_TEST__)
sl@0
   284
			TRequestStatus endStat;
sl@0
   285
			test.Console()->Read(endStat);
sl@0
   286
			while(endStat == KRequestPending)
sl@0
   287
#elif !defined(__MANUAL_TEST__)
sl@0
   288
			RTimer t;
sl@0
   289
			r=t.CreateLocal();
sl@0
   290
			test(r == KErrNone);
sl@0
   291
			TRequestStatus endStat;
sl@0
   292
			t.After(endStat, KMaxTestTime);
sl@0
   293
			test(endStat == KRequestPending);
sl@0
   294
			while(endStat == KRequestPending)
sl@0
   295
#endif
sl@0
   296
				{
sl@0
   297
				TheChangedFlag = EFalse;
sl@0
   298
	
sl@0
   299
				TheDrive.NotifyChange(&TheMediaStatus);
sl@0
   300
	
sl@0
   301
				/**
sl@0
   302
	    		 * Test1 - Simulate door open
sl@0
   303
				 *		 - Power up responds with KErrNotReady
sl@0
   304
				 */
sl@0
   305
				NextTest(_L("Open Door......"), cycles);
sl@0
   306
#ifndef __MANUAL_TEST__
sl@0
   307
				test(TheMediaChange.DoorOpen() == KErrNone);
sl@0
   308
#endif
sl@0
   309
				TestMediaAccess(KErrNotReady, ETrue);
sl@0
   310
				TheDrive.NotifyChange(&TheMediaStatus);
sl@0
   311
	
sl@0
   312
				/**
sl@0
   313
	    		 * Test2 - Simulate door closed (with media removed)
sl@0
   314
				 *		 - Power up responds with KErrNotReady
sl@0
   315
				 */
sl@0
   316
#ifndef __DEVICE_HAS_NO_DOOR__
sl@0
   317
	    		NextTest(_L("Remove Media..."), cycles);
sl@0
   318
#ifndef __MANUAL_TEST__
sl@0
   319
				test(TheMediaChange.DoorClose(EFalse) == KErrNone);		
sl@0
   320
#endif
sl@0
   321
				TestMediaAccess(KErrNotReady, EFalse);					
sl@0
   322
				/**
sl@0
   323
	    		 * Test3 - Simulate door open
sl@0
   324
				 *		 - Power up responds with KErrNotReady
sl@0
   325
				 */
sl@0
   326
				NextTest(_L("Open Door......"), cycles);
sl@0
   327
#ifndef __MANUAL_TEST__
sl@0
   328
				test(TheMediaChange.DoorOpen() == KErrNone);			
sl@0
   329
#endif
sl@0
   330
				TestMediaAccess(KErrNotReady, EFalse);				// Power up responds with KErrNotReady
sl@0
   331
#endif
sl@0
   332
				/**
sl@0
   333
	    		 * Test4 - Simulate door closed (with media present)
sl@0
   334
				 *		 - Power up responds with KErrNone
sl@0
   335
				 */
sl@0
   336
	    		NextTest(_L("Insert Media..."), cycles);
sl@0
   337
#ifndef __MANUAL_TEST__
sl@0
   338
				test(TheMediaChange.DoorClose(ETrue) == KErrNone);
sl@0
   339
#endif
sl@0
   340
				TestMediaAccess(KErrNone, ETrue);
sl@0
   341
				++cycles;
sl@0
   342
				}
sl@0
   343
	
sl@0
   344
			test.Console()->SetPos(0, 27);
sl@0
   345
#if !defined(__SOAK_TEST__) && !defined(__MANUAL_TEST__)
sl@0
   346
			t.Close();
sl@0
   347
#endif
sl@0
   348
			}
sl@0
   349
		else if(r == KErrNotSupported)
sl@0
   350
			{
sl@0
   351
			test.Printf(_L("Media change simulation not supported"));
sl@0
   352
			}
sl@0
   353
	
sl@0
   354
		/**
sl@0
   355
		 * Tidy up and exit
sl@0
   356
		 */
sl@0
   357
		test.Next(_L("\r\nClose device"));
sl@0
   358
		TheMediaChange.Close();
sl@0
   359
	
sl@0
   360
		test.Next(_L("Free device"));
sl@0
   361
		r=User::FreeLogicalDevice(_L("MedCh"));
sl@0
   362
		test(r == KErrNone);
sl@0
   363
	
sl@0
   364
		b.Format(_L("\r\nDisconnect from local drive %d "), drive);
sl@0
   365
		test.Next(b);
sl@0
   366
		TheDrive.Disconnect();
sl@0
   367
		}
sl@0
   368
sl@0
   369
	test.End();
sl@0
   370
	return(0);
sl@0
   371
	}
sl@0
   372