os/kernelhwsrv/halservices/hal/src/hal_main.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) 1999-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\src\hal_main.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <kernel/hal_int.h>
sl@0
    19
sl@0
    20
_LIT(KLitHal,"HAL");
sl@0
    21
void HalInternal::Panic(HalInternal::THalPanic aPanic)
sl@0
    22
	{
sl@0
    23
	User::Panic(KLitHal,aPanic);
sl@0
    24
	}
sl@0
    25
sl@0
    26
void HalInternal::InitialiseData()
sl@0
    27
	{
sl@0
    28
	TInt i;
sl@0
    29
	_LIT_SECURITY_POLICY_PASS(KPassPolicy);
sl@0
    30
	_LIT_SECURITY_POLICY_C1(KWriteDevDataPolicy, ECapabilityWriteDeviceData);
sl@0
    31
	
sl@0
    32
	for (i=0; i<HAL::ENumHalAttributes; i++)                // for every attribute,
sl@0
    33
		{
sl@0
    34
		if (Properties[i]&HAL::EValid && !Implementation[i] && (Properties[i] & HAL::ESettable)) 
sl@0
    35
			// if it's implemented, not a constant and not a user function
sl@0
    36
			{
sl@0
    37
			 TInt r = RProperty::Define(KUidSystemCategory, KUidHalPropertyKeyBase+i, RProperty::EInt,
sl@0
    38
					KPassPolicy, KWriteDevDataPolicy); 
sl@0
    39
			 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists,Panic(EInitialAllocFailed1)); 
sl@0
    40
			 if (r==KErrAlreadyExists)
sl@0
    41
				break;
sl@0
    42
sl@0
    43
			 // This will panic if the first instance of the DLL to start does not have WriteDeviceData
sl@0
    44
			 // capability.  Again, there isn't anything we can do about this.  It shouldn't ever happen.
sl@0
    45
			 r = RProperty::Set(KUidSystemCategory, KUidHalPropertyKeyBase+i, InitialValue[i]);
sl@0
    46
			 __ASSERT_ALWAYS(r==KErrNone,Panic(EInitialAllocFailed2));
sl@0
    47
			}
sl@0
    48
		}
sl@0
    49
	}
sl@0
    50
sl@0
    51
TInt HalInternal::ReadWord(TInt aKey)
sl@0
    52
	{
sl@0
    53
	 TInt result;
sl@0
    54
sl@0
    55
	// This is a slow way of doing it, but it is light on memory as it doesn't
sl@0
    56
	// need an offset array.
sl@0
    57
sl@0
    58
	if(aKey>=HAL::ENumHalAttributes)
sl@0
    59
		return KErrNotFound;
sl@0
    60
	TInt r=RProperty::Get(KUidSystemCategory, KUidHalPropertyKeyBase+aKey, result);
sl@0
    61
	if (r!=KErrNone)
sl@0
    62
		{
sl@0
    63
		InitialiseData();
sl@0
    64
		r=RProperty::Get(KUidSystemCategory, KUidHalPropertyKeyBase+aKey, result);
sl@0
    65
		}
sl@0
    66
	__ASSERT_ALWAYS(r==KErrNone,Panic(EGetPropFailed)); 
sl@0
    67
	return (result);
sl@0
    68
	}
sl@0
    69
sl@0
    70
TInt HalInternal::WriteWord(TInt aKey, TInt aValue)
sl@0
    71
	{
sl@0
    72
	TInt r;
sl@0
    73
sl@0
    74
	if(aKey>=HAL::ENumHalAttributes)
sl@0
    75
		return KErrArgument;
sl@0
    76
	r=RProperty::Set(KUidSystemCategory, KUidHalPropertyKeyBase+aKey, aValue);
sl@0
    77
	if (r!=KErrNone && r!=KErrPermissionDenied) 
sl@0
    78
		{
sl@0
    79
		InitialiseData();
sl@0
    80
		r=RProperty::Set(KUidSystemCategory, KUidHalPropertyKeyBase+aKey, aValue);
sl@0
    81
		}
sl@0
    82
	__ASSERT_ALWAYS(r==KErrNone || r==KErrPermissionDenied,Panic(ESetPropFailed));
sl@0
    83
	return r;
sl@0
    84
	}
sl@0
    85
sl@0
    86
sl@0
    87
EXPORT_C TInt HAL::Get(HAL::TAttribute aAttribute, TInt& aValue)
sl@0
    88
	{
sl@0
    89
	return HAL::Get(0,aAttribute,aValue);
sl@0
    90
	}
sl@0
    91
sl@0
    92
sl@0
    93
EXPORT_C TInt HAL::Set(HAL::TAttribute aAttribute, TInt aValue)
sl@0
    94
	{
sl@0
    95
	return HAL::Set(0,aAttribute,aValue);
sl@0
    96
	}
sl@0
    97
sl@0
    98
sl@0
    99
EXPORT_C TInt HAL::GetAll(TInt& aNumEntries, SEntry*& aData)
sl@0
   100
	{
sl@0
   101
	TInt max_devices=1;
sl@0
   102
sl@0
   103
	HAL::Get(EDisplayNumberOfScreens,max_devices);
sl@0
   104
	TInt size=max_devices*(TInt)ENumHalAttributes*(TInt)sizeof(SEntry);
sl@0
   105
	SEntry* pE=(SEntry*)User::Alloc(size);
sl@0
   106
	if (!pE)
sl@0
   107
		{
sl@0
   108
		aNumEntries=0;
sl@0
   109
		aData=NULL;
sl@0
   110
		return KErrNoMemory;
sl@0
   111
		}
sl@0
   112
sl@0
   113
	TInt device;
sl@0
   114
	for(device=0;device<max_devices;device++)
sl@0
   115
		{
sl@0
   116
		TInt r;
sl@0
   117
		TInt i=ENumHalAttributes-1;
sl@0
   118
		for (; i>=0; --i)
sl@0
   119
			{
sl@0
   120
			TInt offset = device*(TInt)ENumHalAttributes + i;
sl@0
   121
			TInt properties=HalInternal::Properties[i];
sl@0
   122
			if (properties & HAL::EValid)
sl@0
   123
				{
sl@0
   124
				THalImplementation f=HalInternal::Implementation[i];
sl@0
   125
				if (f)
sl@0
   126
					{
sl@0
   127
					// Initialise the value before getting it, for consistancy
sl@0
   128
					// when functions take an argument. (-1 is also likely to be
sl@0
   129
					// an invalid argument and return an error in these cases, which
sl@0
   130
					// is probably the safest result.)
sl@0
   131
					pE[offset].iValue = -1;
sl@0
   132
					r=(*f)(device,i,EFalse,&pE[offset].iValue);
sl@0
   133
					if (r==KErrNone)
sl@0
   134
						{
sl@0
   135
						pE[offset].iProperties=EEntryValid|EEntryDynamic;
sl@0
   136
						continue;
sl@0
   137
						}
sl@0
   138
					// drop through to clear EEntryValid
sl@0
   139
					}
sl@0
   140
				else
sl@0
   141
					{
sl@0
   142
					//these attributes do not support multiple devices
sl@0
   143
					if(device==0)
sl@0
   144
						{
sl@0
   145
						TInt p;
sl@0
   146
						if (!(properties & HAL::ESettable))
sl@0
   147
							{
sl@0
   148
							pE[offset].iValue = HalInternal::InitialValue[i];
sl@0
   149
							p=EEntryValid;
sl@0
   150
							}
sl@0
   151
						else
sl@0
   152
							{
sl@0
   153
							r=RProperty::Get(KUidSystemCategory, KUidHalPropertyKeyBase+i, pE[offset].iValue);
sl@0
   154
							if (r!=KErrNone)
sl@0
   155
								{
sl@0
   156
								HalInternal::InitialiseData();
sl@0
   157
								r=RProperty::Get(KUidSystemCategory, KUidHalPropertyKeyBase+i, pE[offset].iValue);
sl@0
   158
								}
sl@0
   159
							p=(r==KErrNone?EEntryValid:0);
sl@0
   160
							p|=EEntryDynamic;
sl@0
   161
							}
sl@0
   162
						pE[offset].iProperties=p;
sl@0
   163
						continue;
sl@0
   164
						}
sl@0
   165
					// drop through to clear EEntryValid
sl@0
   166
					}
sl@0
   167
				}
sl@0
   168
			pE[offset].iProperties=0;
sl@0
   169
			pE[offset].iValue=0;
sl@0
   170
			}
sl@0
   171
		}
sl@0
   172
	aNumEntries=max_devices*(TInt)ENumHalAttributes;
sl@0
   173
	aData=pE;
sl@0
   174
sl@0
   175
	return KErrNone;
sl@0
   176
	}
sl@0
   177
sl@0
   178
sl@0
   179
EXPORT_C TInt HAL::Get(TInt aDeviceNumber, HAL::TAttribute aAttribute, TInt& aValue)
sl@0
   180
	{
sl@0
   181
sl@0
   182
	if (TUint(aAttribute)>=TUint(ENumHalAttributes))
sl@0
   183
		return KErrNotSupported;
sl@0
   184
	TUint8 properties=HalInternal::Properties[aAttribute];
sl@0
   185
	if (!(properties & HAL::EValid))
sl@0
   186
		return KErrNotSupported;
sl@0
   187
	THalImplementation f=HalInternal::Implementation[aAttribute];
sl@0
   188
	if (f)
sl@0
   189
		return (*f)(aDeviceNumber,aAttribute,EFalse,&aValue);
sl@0
   190
	if (!(properties & HAL::ESettable))
sl@0
   191
	{
sl@0
   192
		aValue=HalInternal::InitialValue[aAttribute];
sl@0
   193
		return KErrNone;
sl@0
   194
	}
sl@0
   195
	aValue=HalInternal::ReadWord(aAttribute);
sl@0
   196
	return KErrNone;
sl@0
   197
	}
sl@0
   198
sl@0
   199
sl@0
   200
EXPORT_C TInt HAL::Set(TInt aDeviceNumber, HAL::TAttribute aAttribute, TInt aValue)
sl@0
   201
	{
sl@0
   202
sl@0
   203
     if (TUint(aAttribute)>=TUint(ENumHalAttributes))
sl@0
   204
		return KErrNotSupported;
sl@0
   205
sl@0
   206
	TUint8 properties=HalInternal::Properties[aAttribute];
sl@0
   207
	if (!(properties & HAL::EValid) || !(properties & HAL::ESettable))
sl@0
   208
		return KErrNotSupported;
sl@0
   209
	THalImplementation f=HalInternal::Implementation[aAttribute];
sl@0
   210
	if (f)
sl@0
   211
		return (*f)(aDeviceNumber,aAttribute,ETrue,(TAny*)aValue);
sl@0
   212
	return HalInternal::WriteWord(aAttribute,aValue);
sl@0
   213
	}
sl@0
   214