os/kernelhwsrv/halservices/hal/tsrc/t_newhal.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-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
// hal\tsrc\t_newhal.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#define __E32TEST_EXTENSION__
sl@0
    19
sl@0
    20
#include <e32test.h>
sl@0
    21
#include <hal.h>
sl@0
    22
sl@0
    23
RTest test(_L("T_NEWHAL"));
sl@0
    24
sl@0
    25
const TText* AttributeNames[]=
sl@0
    26
	{
sl@0
    27
	_S("EManufacturer"),
sl@0
    28
	_S("EManufacturerHardwareRev"),
sl@0
    29
	_S("EManufacturerSoftwareRev"),
sl@0
    30
	_S("EManufacturerSoftwareBuild"),
sl@0
    31
	_S("EModel"),
sl@0
    32
	_S("EMachineUid"),
sl@0
    33
	_S("EDeviceFamily"),
sl@0
    34
	_S("EDeviceFamilyRev"),
sl@0
    35
	_S("ECPU"),
sl@0
    36
	_S("ECPUArch"),
sl@0
    37
	_S("ECPUABI"),
sl@0
    38
	_S("ECPUSpeed"),
sl@0
    39
	_S("ESystemStartupReason"),
sl@0
    40
	_S("ESystemException"),
sl@0
    41
	_S("ESystemTickPeriod"),		
sl@0
    42
	_S("EMemoryRAM"),
sl@0
    43
	_S("EMemoryRAMFree"),
sl@0
    44
	_S("EMemoryROM"),
sl@0
    45
	_S("EMemoryPageSize"),	
sl@0
    46
	_S("EPowerGood"),
sl@0
    47
	_S("EPowerBatteryStatus"),	
sl@0
    48
	_S("EPowerBackup"),
sl@0
    49
	_S("EPowerBackupStatus"),	
sl@0
    50
	_S("EPowerExternal"),
sl@0
    51
	_S("EKeyboard"),
sl@0
    52
	_S("EKeyboardDeviceKeys"),	
sl@0
    53
	_S("EKeyboardAppKeys"),
sl@0
    54
	_S("EKeyboardClick"),
sl@0
    55
	_S("EKeyboardClickState"),	
sl@0
    56
	_S("EKeyboardClickVolume"),
sl@0
    57
	_S("EKeyboardClickVolumeMax"),
sl@0
    58
	_S("EDisplayXPixels"),
sl@0
    59
	_S("EDisplayYPixels"),
sl@0
    60
	_S("EDisplayXTwips"),
sl@0
    61
	_S("EDisplayYTwips"),
sl@0
    62
	_S("EDisplayColors"),
sl@0
    63
	_S("EDisplayState"),
sl@0
    64
	_S("EDisplayContrast"),
sl@0
    65
	_S("EDisplayContrastMax"),
sl@0
    66
	_S("EBacklight"),
sl@0
    67
	_S("EBacklightState"),
sl@0
    68
	_S("EPen"),
sl@0
    69
	_S("EPenX"),
sl@0
    70
	_S("EPenY"),
sl@0
    71
	_S("EPenDisplayOn"),
sl@0
    72
	_S("EPenClick"),
sl@0
    73
	_S("EPenClickState"),
sl@0
    74
	_S("EPenClickVolume"),
sl@0
    75
	_S("EPenClickVolumeMax"),
sl@0
    76
	_S("EMouse"),
sl@0
    77
	_S("EMouseX"),
sl@0
    78
	_S("EMouseY"),			
sl@0
    79
	_S("EMouseState"),
sl@0
    80
	_S("EMouseSpeed"),
sl@0
    81
	_S("EMouseAcceleration"),
sl@0
    82
	_S("EMouseButtons"),
sl@0
    83
	_S("EMouseButtonState"),
sl@0
    84
	_S("ECaseState"),
sl@0
    85
	_S("ECaseSwitch"),
sl@0
    86
	_S("ECaseSwitchDisplayOn"),
sl@0
    87
	_S("ECaseSwitchDisplayOff"),
sl@0
    88
	_S("ELEDs"),
sl@0
    89
	_S("ELEDmask"),
sl@0
    90
	_S("EIntegratedPhone"),
sl@0
    91
	_S("EDisplayBrightness"),
sl@0
    92
	_S("EDisplayBrightnessMax"),
sl@0
    93
	_S("EKeyboardBacklightState"),
sl@0
    94
	_S("EAccessoryPower"),
sl@0
    95
	_S("ELanguageIndex"),
sl@0
    96
	_S("EKeyboardIndex"),
sl@0
    97
	_S("EMaxRAMDriveSize"),
sl@0
    98
	_S("EKeyboardState"),
sl@0
    99
	_S("ESystemDrive"),
sl@0
   100
	_S("EPenState"),
sl@0
   101
	_S("EDisplayIsMono"),
sl@0
   102
	_S("EDisplayIsPalettized"),
sl@0
   103
	_S("EDisplayBitsPerPixel"),
sl@0
   104
	_S("EDisplayNumModes"),
sl@0
   105
	_S("EDisplayMemoryAddress"),
sl@0
   106
	_S("EDisplayOffsetToFirstPixel"),
sl@0
   107
	_S("EDisplayOffsetBetweenLines"),
sl@0
   108
	_S("EDisplayPaletteEntry"),
sl@0
   109
	_S("EDisplayIsPixelOrderRGB"),
sl@0
   110
	_S("EDisplayIsPixelOrderLandscape"),
sl@0
   111
	_S("EDisplayMode"),
sl@0
   112
	_S("ESwitches"),
sl@0
   113
	_S("EDebugPort"),
sl@0
   114
	_S("ELocaleLoaded"),
sl@0
   115
	_S("EClipboardDrive"),
sl@0
   116
	_S("ECustomRestart"),
sl@0
   117
	_S("ECustomRestartReason"),
sl@0
   118
	_S("EDisplayNumberOfScreens"),
sl@0
   119
	_S("ENanoTickPeriod"),
sl@0
   120
	_S("EFastCounterFrequency"),
sl@0
   121
	_S("EFastCounterCountsUp"),
sl@0
   122
	_S("EPointer3D"),
sl@0
   123
	_S("EPointer3DMaxProximity"),
sl@0
   124
	_S("EPointer3DThetaSupported"),
sl@0
   125
	_S("EPointer3DPhiSupported"),
sl@0
   126
	_S("EPointer3DRotationSupported"),
sl@0
   127
	_S("EPointer3DPressureSupported"),
sl@0
   128
	_S("EHardwareFloatingPoint"),
sl@0
   129
	_S("ETimeNonSecureOffset"),
sl@0
   130
	_S("EPersistStartupModeKernel"),
sl@0
   131
	_S("EMaximumCustomRestartReasons"),
sl@0
   132
	_S("EMaximumRestartStartupModes"),
sl@0
   133
	_S("ECustomResourceDrive"),
sl@0
   134
	_S("EPointer3DProximityStep"),
sl@0
   135
	_S("EPointerMaxPointers"),
sl@0
   136
	_S("EPointerNumberOfPointers"),
sl@0
   137
	_S("EPointer3DMaxPressure"),
sl@0
   138
	_S("EPointer3DPressureStep"),
sl@0
   139
	_S("EPointer3DEnterHighPressureThreshold"),
sl@0
   140
	_S("EPointer3DExitHighPressureThreshold"),
sl@0
   141
	_S("EPointer3DEnterCloseProximityThreshold"),
sl@0
   142
	_S("EPointer3DExitCloseProximityThreshold"),
sl@0
   143
	_S("EDisplayMemoryHandle"),
sl@0
   144
	_S("ESerialNumber"),
sl@0
   145
	_S("ECpuProfilingDefaultInterruptBase"),
sl@0
   146
	_S("ENumCpus"),
sl@0
   147
sl@0
   148
	};
sl@0
   149
sl@0
   150
TInt MatchAbbrev(const TDesC& anInput, const TText** aList, TInt aListLen)
sl@0
   151
	{
sl@0
   152
	TInt first_match=KErrNotFound;
sl@0
   153
	TInt nmatches=0;
sl@0
   154
	TInt i;
sl@0
   155
	for (i=0; i<aListLen; i++)
sl@0
   156
		{
sl@0
   157
		TPtrC list_entry(aList[i]);
sl@0
   158
		TInt r=list_entry.FindF(anInput);
sl@0
   159
		if (r>=0)
sl@0
   160
			{
sl@0
   161
			// substring matches
sl@0
   162
			if (r==0 && list_entry.Length()==anInput.Length())
sl@0
   163
				{
sl@0
   164
				// exact match
sl@0
   165
				return i;
sl@0
   166
				}
sl@0
   167
			if (first_match<0)
sl@0
   168
				first_match=i;
sl@0
   169
			++nmatches;
sl@0
   170
			}
sl@0
   171
		}
sl@0
   172
	if (nmatches>1)
sl@0
   173
		return KErrGeneral;	// ambiguous
sl@0
   174
	return first_match;		// either KErrNotFound or match index
sl@0
   175
	}
sl@0
   176
sl@0
   177
TInt MatchAttribute(const TDesC& anInput)
sl@0
   178
	{
sl@0
   179
	return MatchAbbrev(anInput, AttributeNames, sizeof(AttributeNames)/sizeof(TText*));
sl@0
   180
	}
sl@0
   181
sl@0
   182
void TestGet()
sl@0
   183
	{
sl@0
   184
	TInt i;
sl@0
   185
	TInt n=0;
sl@0
   186
sl@0
   187
	for (i=0; i<HAL::ENumHalAttributes; i++)
sl@0
   188
		{
sl@0
   189
		TPtrC att_name(AttributeNames[i]);
sl@0
   190
		TInt val=-1;
sl@0
   191
		TInt r=HAL::Get((HAL::TAttribute)i,val);
sl@0
   192
		test.Printf(_L("%S: return %d, value %d(0x%08x)\n"),&att_name,r,val,val);
sl@0
   193
		if (++n==16)
sl@0
   194
			{
sl@0
   195
			n=0;
sl@0
   196
			test.Printf(_L("\nPress a key to continue...\n"));
sl@0
   197
			test.Getch();
sl@0
   198
			}
sl@0
   199
		}
sl@0
   200
	}
sl@0
   201
sl@0
   202
sl@0
   203
void TestGetAll()
sl@0
   204
	{
sl@0
   205
	User::Allocator().Check();
sl@0
   206
	HAL::SEntry* pE=NULL;
sl@0
   207
	TInt nEntries=0;
sl@0
   208
	TInt r=HAL::GetAll(nEntries, pE);
sl@0
   209
	test(r==KErrNone);
sl@0
   210
	test.Printf(_L("%d attributes defined\n"),nEntries);
sl@0
   211
	test(nEntries==HAL::ENumHalAttributes);
sl@0
   212
	TInt i;
sl@0
   213
	for (i=0; i<nEntries; ++i)
sl@0
   214
		{
sl@0
   215
		TPtrC att_name(AttributeNames[i]);
sl@0
   216
		TInt f=pE[i].iProperties;
sl@0
   217
		TInt v=pE[i].iValue;
sl@0
   218
		TInt v2=-1;
sl@0
   219
		TInt r=HAL::Get((HAL::TAttribute)i,v2);
sl@0
   220
		if (f & HAL::EEntryValid)
sl@0
   221
			{
sl@0
   222
			test(r==KErrNone);
sl@0
   223
			if (f & HAL::EEntryDynamic)
sl@0
   224
				{
sl@0
   225
				test.Printf(_L("Attribute %S dynamic "),&att_name);
sl@0
   226
				if (v2!=v)
sl@0
   227
					{
sl@0
   228
					test.Printf(_L("Values %d(%08x), %d(%08x)"),v,v,v2,v2);
sl@0
   229
					}
sl@0
   230
				test.Printf(_L("\n"));
sl@0
   231
				}
sl@0
   232
			else
sl@0
   233
				{
sl@0
   234
				test(v==v2);	// constant attribute
sl@0
   235
				}
sl@0
   236
			}
sl@0
   237
		else
sl@0
   238
			{
sl@0
   239
			test.Printf(_L("Attribute %S not supported on this platform or requires parameter\n"),&att_name,r);
sl@0
   240
			test(r==KErrNotSupported || r==KErrArgument);
sl@0
   241
			}
sl@0
   242
		}
sl@0
   243
	User::Free(pE);
sl@0
   244
	User::Allocator().Check();
sl@0
   245
	}
sl@0
   246
sl@0
   247
void InputLine(const TDesC& aPrompt, TDes& aLine)
sl@0
   248
	{
sl@0
   249
	test.Printf(_L("%S"),&aPrompt);
sl@0
   250
	aLine.SetLength(0);
sl@0
   251
	FOREVER
sl@0
   252
		{
sl@0
   253
		TKeyCode k=test.Getch();
sl@0
   254
		switch (k)
sl@0
   255
			{
sl@0
   256
			case EKeyEnter:
sl@0
   257
				test.Printf(_L("\n"));
sl@0
   258
				return;
sl@0
   259
			case EKeyEscape:
sl@0
   260
				aLine.SetLength(0);
sl@0
   261
				test.Printf(_L("\n%S"),&aPrompt);
sl@0
   262
				break;
sl@0
   263
			case EKeyBackspace:
sl@0
   264
			case EKeyDelete:
sl@0
   265
				if (aLine.Length()>0)
sl@0
   266
					{
sl@0
   267
					aLine.SetLength(aLine.Length()-1);
sl@0
   268
					test.Printf(_L("\x8"));
sl@0
   269
					}
sl@0
   270
				break;
sl@0
   271
			default:
sl@0
   272
				{
sl@0
   273
				TChar ch((TUint)k);
sl@0
   274
				TBuf<1> b;
sl@0
   275
				b.Append(ch);
sl@0
   276
				aLine.Append(ch);
sl@0
   277
				test.Printf(_L("%S"),&b);
sl@0
   278
				break;
sl@0
   279
				}
sl@0
   280
			}
sl@0
   281
		}
sl@0
   282
	}
sl@0
   283
sl@0
   284
void TestSet()
sl@0
   285
	{
sl@0
   286
	TBuf<256> line;
sl@0
   287
	FOREVER
sl@0
   288
		{
sl@0
   289
		TInt attrib=KErrNotFound;
sl@0
   290
		while (attrib<0)
sl@0
   291
			{
sl@0
   292
			InputLine(_L("Attribute: "),line);
sl@0
   293
			if (line.Length()==0)
sl@0
   294
				return;
sl@0
   295
			attrib=MatchAttribute(line);
sl@0
   296
			if (attrib==KErrNotFound)
sl@0
   297
				test.Printf(_L("Unrecognised attribute\n"));
sl@0
   298
			else if (attrib==KErrGeneral)
sl@0
   299
				test.Printf(_L("Ambiguous attribute\n"));
sl@0
   300
			}
sl@0
   301
		TPtrC attrib_name(AttributeNames[attrib]);
sl@0
   302
		test.Printf(_L("Attribute %d (%S) selected\n"),attrib,&attrib_name);
sl@0
   303
		TInt value=0;
sl@0
   304
		TInt r=KErrGeneral;
sl@0
   305
		TBool set=ETrue;
sl@0
   306
		while(r!=KErrNone)
sl@0
   307
			{
sl@0
   308
			InputLine(_L("Value: "),line);
sl@0
   309
			line.Trim();
sl@0
   310
			TLex lex(line);
sl@0
   311
			if (line.MatchF(_L("r"))>=0)
sl@0
   312
				{
sl@0
   313
				set=EFalse;
sl@0
   314
				r=KErrNone;
sl@0
   315
				}
sl@0
   316
			else if (line.MatchF(_L("0x"))>=0)
sl@0
   317
				{
sl@0
   318
				lex.Inc(2);
sl@0
   319
				r=lex.Val((TUint&)value,EHex);
sl@0
   320
				}
sl@0
   321
			else
sl@0
   322
				{
sl@0
   323
				r=lex.Val(value);
sl@0
   324
				}
sl@0
   325
			}
sl@0
   326
		if (set)
sl@0
   327
			{
sl@0
   328
			r=HAL::Set((HAL::TAttribute)attrib,value);
sl@0
   329
			test.Printf(_L("Set returns %d\n"),r);
sl@0
   330
			}
sl@0
   331
		TInt v2;
sl@0
   332
		r=HAL::Get((HAL::TAttribute)attrib,v2);
sl@0
   333
		test.Printf(_L("Get returns %d, value %d(%08x)\n"),r,v2,v2);
sl@0
   334
		}
sl@0
   335
	}
sl@0
   336
sl@0
   337
GLDEF_C TInt E32Main()
sl@0
   338
	{
sl@0
   339
	test.Title();
sl@0
   340
	__UHEAP_MARK;
sl@0
   341
	__UHEAP_SETFAIL(RHeap::EDeterministic,1);
sl@0
   342
	TInt machine_id;
sl@0
   343
	TInt r=HAL::Get(HAL::EMachineUid,machine_id);
sl@0
   344
	__UHEAP_RESET;
sl@0
   345
	test(r==KErrNone);
sl@0
   346
	test.Printf(_L("Machine ID %08x\n"),machine_id);
sl@0
   347
sl@0
   348
	test.Start(_L("Check AttributeNames[] is up-to-date"));
sl@0
   349
	test_Equal(HAL::ENumHalAttributes, sizeof(AttributeNames)/sizeof(TText*));
sl@0
   350
sl@0
   351
	test.Next(_L("Test Get"));
sl@0
   352
	TestGet();
sl@0
   353
sl@0
   354
	test.Next(_L("Test GetAll"));
sl@0
   355
	TestGetAll();
sl@0
   356
sl@0
   357
	test.Next(_L("Test Set"));
sl@0
   358
	TestSet();
sl@0
   359
sl@0
   360
	test.End();
sl@0
   361
sl@0
   362
	__UHEAP_MARKEND;	// problem if HAL uses TLS
sl@0
   363
sl@0
   364
	return KErrNone;
sl@0
   365
	}
sl@0
   366