os/kernelhwsrv/kerneltest/e32test/pccd/d_medch.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) 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\d_medch.cpp
sl@0
    15
// This LDD allow simulation of media change on a peripheral bus controller.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <kernel/kernel.h>
sl@0
    20
#include <drivers/pbus.h>
sl@0
    21
#include "d_medch.h"
sl@0
    22
sl@0
    23
const TInt KMajorVersionNumber=1;
sl@0
    24
const TInt KMinorVersionNumber=0;
sl@0
    25
const TInt KBuildVersionNumber=1;
sl@0
    26
sl@0
    27
_LIT(KDFCThreadName,"D_MEDCH_DFC_THREAD");
sl@0
    28
const TInt KMedChThreadPriority = 27;
sl@0
    29
sl@0
    30
class DLddFactoryMedCh : public DLogicalDevice
sl@0
    31
	{
sl@0
    32
public:
sl@0
    33
	DLddFactoryMedCh();
sl@0
    34
	virtual TInt Install();
sl@0
    35
	virtual void GetCaps(TDes8 &aDes) const;
sl@0
    36
	virtual TInt Create(DLogicalChannelBase*& aChannel);
sl@0
    37
	};
sl@0
    38
sl@0
    39
class DLddMedCh : public DLogicalChannel
sl@0
    40
	{
sl@0
    41
public:
sl@0
    42
	 DLddMedCh();
sl@0
    43
	~DLddMedCh();
sl@0
    44
protected:
sl@0
    45
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    46
	virtual void HandleMsg(class TMessageBase *);
sl@0
    47
private:
sl@0
    48
	TInt DoRequest(TInt aReqNo,TAny *a1,TAny *a2);
sl@0
    49
	TInt DoControl(TInt aFunction,TAny *a1,TAny *a2);
sl@0
    50
private:
sl@0
    51
	static void MsCBFunc(TAny* aPtr);
sl@0
    52
private:	
sl@0
    53
	DPBusSocket* iSocketP;
sl@0
    54
	DThread* iClient;
sl@0
    55
	TRequestStatus* iReqStat;
sl@0
    56
	TDynamicDfcQue* iDfcQ;
sl@0
    57
	
sl@0
    58
	NTimer iMsCallBack;
sl@0
    59
	TInt iMsInterval;
sl@0
    60
sl@0
    61
	DPBusSocket::TPBusSimulateMediaState iDelayedOperation;
sl@0
    62
	};
sl@0
    63
sl@0
    64
DECLARE_STANDARD_LDD()
sl@0
    65
	{
sl@0
    66
	return new DLddFactoryMedCh;
sl@0
    67
	}
sl@0
    68
sl@0
    69
DLddFactoryMedCh::DLddFactoryMedCh()
sl@0
    70
/**
sl@0
    71
 * Constructor
sl@0
    72
 */
sl@0
    73
	{
sl@0
    74
sl@0
    75
    iParseMask=KDeviceAllowUnit;
sl@0
    76
	iUnitsMask=0xffffffff;
sl@0
    77
	iVersion = TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber);
sl@0
    78
	}
sl@0
    79
sl@0
    80
TInt DLddFactoryMedCh::Install()
sl@0
    81
/**
sl@0
    82
 * Install the device driver.
sl@0
    83
 */
sl@0
    84
	{
sl@0
    85
sl@0
    86
    TPtrC name = _L("MedCh");
sl@0
    87
	return(SetName(&name));
sl@0
    88
	}
sl@0
    89
sl@0
    90
void DLddFactoryMedCh::GetCaps(TDes8 &aDes) const
sl@0
    91
/**
sl@0
    92
 * Return the media change LDD capabilities.
sl@0
    93
 */
sl@0
    94
	{
sl@0
    95
sl@0
    96
	TCapsMediaChangeV01 caps;
sl@0
    97
	caps.version = TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber);
sl@0
    98
    Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
sl@0
    99
	}
sl@0
   100
sl@0
   101
TInt DLddFactoryMedCh::Create(DLogicalChannelBase*& aChannel)
sl@0
   102
/**
sl@0
   103
 * Create a channel on the device.
sl@0
   104
 */
sl@0
   105
	{
sl@0
   106
sl@0
   107
	aChannel = new DLddMedCh;
sl@0
   108
	return aChannel ? KErrNone : KErrNoMemory;
sl@0
   109
	}
sl@0
   110
sl@0
   111
DLddMedCh::DLddMedCh()
sl@0
   112
/**
sl@0
   113
 * Constructor
sl@0
   114
 */
sl@0
   115
	: iMsCallBack(MsCBFunc, this)
sl@0
   116
	{
sl@0
   117
sl@0
   118
	iClient = &Kern::CurrentThread();
sl@0
   119
	((DObject*)iClient)->Open();
sl@0
   120
	}
sl@0
   121
sl@0
   122
DLddMedCh::~DLddMedCh()
sl@0
   123
/**
sl@0
   124
 * Destructor
sl@0
   125
 */
sl@0
   126
	{ 
sl@0
   127
	if(iSocketP)
sl@0
   128
		(void)iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
sl@0
   129
sl@0
   130
	Kern::SafeClose((DObject*&)iClient, NULL);
sl@0
   131
sl@0
   132
	if (iDfcQ)
sl@0
   133
		iDfcQ->Destroy();
sl@0
   134
	}
sl@0
   135
sl@0
   136
TInt DLddMedCh::DoCreate(TInt aUnit, const TDesC8* /*aInfo*/, const TVersion& aVer)
sl@0
   137
/**
sl@0
   138
 * Create channel.
sl@0
   139
 */
sl@0
   140
	{
sl@0
   141
sl@0
   142
	if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber), aVer))
sl@0
   143
		return(KErrNotSupported);
sl@0
   144
sl@0
   145
	//
sl@0
   146
	// Obtain the requested socket (as specified by the opened logical unit)
sl@0
   147
	//
sl@0
   148
	iSocketP = DPBusSocket::SocketFromId(aUnit);
sl@0
   149
	if(iSocketP == NULL)
sl@0
   150
		return(KErrNoMemory);
sl@0
   151
sl@0
   152
	if (!iDfcQ)
sl@0
   153
 			{
sl@0
   154
 			TInt r = Kern::DynamicDfcQCreate(iDfcQ, KMedChThreadPriority, KDFCThreadName);
sl@0
   155
			if (r != KErrNone)
sl@0
   156
 				return r;
sl@0
   157
#ifdef CPU_AFFINITY_ANY
sl@0
   158
			NKern::ThreadSetCpuAffinity((NThread*)(iDfcQ->iThread), KCpuAffinityAny);			
sl@0
   159
#endif
sl@0
   160
sl@0
   161
			SetDfcQ(iDfcQ);
sl@0
   162
 			}	
sl@0
   163
sl@0
   164
	iMsgQ.Receive();
sl@0
   165
	
sl@0
   166
    return KErrNone;
sl@0
   167
	}
sl@0
   168
sl@0
   169
void DLddMedCh::HandleMsg(TMessageBase* aMsg)
sl@0
   170
/**
sl@0
   171
 * Message Handler
sl@0
   172
 */
sl@0
   173
    {
sl@0
   174
sl@0
   175
    TThreadMessage& m=*(TThreadMessage*)aMsg;
sl@0
   176
    TInt id=m.iValue;
sl@0
   177
    
sl@0
   178
	if (id == (TInt)ECloseMsg)
sl@0
   179
		{
sl@0
   180
		iMsCallBack.Cancel();
sl@0
   181
		(void)iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
sl@0
   182
		m.Complete(KErrNone, EFalse);
sl@0
   183
		return;
sl@0
   184
		}
sl@0
   185
    else if (id == KMaxTInt)
sl@0
   186
		{
sl@0
   187
		// DoCancel
sl@0
   188
		m.Complete(KErrNone, ETrue);
sl@0
   189
		return;
sl@0
   190
		}
sl@0
   191
sl@0
   192
    if (id < 0)
sl@0
   193
		{
sl@0
   194
		// DoRequest
sl@0
   195
		TRequestStatus* pS = (TRequestStatus*)m.Ptr0();
sl@0
   196
		iReqStat = pS;
sl@0
   197
		TInt r = DoRequest(~id, m.Ptr1(), m.Ptr2());
sl@0
   198
		if (r != KErrNone)
sl@0
   199
	    	Kern::RequestComplete(iClient, pS, r);
sl@0
   200
		m.Complete(KErrNone, ETrue);
sl@0
   201
		}
sl@0
   202
    else
sl@0
   203
		{
sl@0
   204
		// DoControl
sl@0
   205
		TInt r = DoControl(id, m.Ptr0(), m.Ptr1());
sl@0
   206
		if(r != KErrCompletion)
sl@0
   207
			{
sl@0
   208
			m.Complete(r, ETrue);
sl@0
   209
			}
sl@0
   210
		}
sl@0
   211
	}
sl@0
   212
sl@0
   213
TInt DLddMedCh::DoRequest(TInt aFunction, TAny* a1, TAny* a2)
sl@0
   214
/**
sl@0
   215
 * Asynchronous requests
sl@0
   216
 */
sl@0
   217
	{
sl@0
   218
sl@0
   219
	TInt err = KErrNotSupported;
sl@0
   220
	
sl@0
   221
	switch (aFunction)
sl@0
   222
		{
sl@0
   223
		case RMedCh::EDelayedDoorOpen:
sl@0
   224
			{
sl@0
   225
			const TInt KMsInterval = (TInt)a1;
sl@0
   226
			iDelayedOperation = DPBusSocket::EPeriphBusDoorOpen;
sl@0
   227
			err = iMsCallBack.OneShot(NKern::TimerTicks(KMsInterval), ETrue);
sl@0
   228
			break;
sl@0
   229
			}
sl@0
   230
		
sl@0
   231
		case RMedCh::EDelayedDoorClose:
sl@0
   232
			{
sl@0
   233
			const TInt KMsInterval = (TInt)a1;
sl@0
   234
			const TBool KMediaPresent = (TBool)a2;
sl@0
   235
			iDelayedOperation = KMediaPresent ? DPBusSocket::EPeriphBusMediaPresent : DPBusSocket::EPeriphBusMediaRemoved;
sl@0
   236
			err = iMsCallBack.OneShot(NKern::TimerTicks(KMsInterval), ETrue);
sl@0
   237
			break;
sl@0
   238
			}
sl@0
   239
				
sl@0
   240
		default:
sl@0
   241
			{
sl@0
   242
			err = KErrNotSupported;
sl@0
   243
			break;
sl@0
   244
			}
sl@0
   245
		}
sl@0
   246
	
sl@0
   247
	return err;
sl@0
   248
	}
sl@0
   249
sl@0
   250
TInt DLddMedCh::DoControl(TInt aFunction,TAny* a1, TAny* /*a2*/)
sl@0
   251
/**
sl@0
   252
 * Synchronous requests
sl@0
   253
 */
sl@0
   254
	{
sl@0
   255
sl@0
   256
	TInt err = KErrNotSupported;
sl@0
   257
	
sl@0
   258
	switch (aFunction)
sl@0
   259
		{
sl@0
   260
		case RMedCh::EDoorOpen:
sl@0
   261
			{
sl@0
   262
			err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusDoorOpen, NULL);
sl@0
   263
			break;
sl@0
   264
			}
sl@0
   265
sl@0
   266
		case RMedCh::EDoorClose:
sl@0
   267
			{
sl@0
   268
			const TBool KMediaPresent = (TBool)a1;
sl@0
   269
sl@0
   270
			err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, 
sl@0
   271
									  (TAny*)(KMediaPresent ? DPBusSocket::EPeriphBusMediaPresent : DPBusSocket::EPeriphBusMediaRemoved),
sl@0
   272
									  NULL);
sl@0
   273
			break;
sl@0
   274
			}
sl@0
   275
sl@0
   276
		case RMedCh::EDoorNormal:
sl@0
   277
			{
sl@0
   278
			err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
sl@0
   279
			break;
sl@0
   280
			}
sl@0
   281
sl@0
   282
		case RMedCh::EDoubleDoorOpen:
sl@0
   283
			{
sl@0
   284
			err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaDoubleDoorOpen, NULL);
sl@0
   285
			break;
sl@0
   286
			}
sl@0
   287
sl@0
   288
		default:
sl@0
   289
			{
sl@0
   290
			err = KErrNotSupported;
sl@0
   291
			break;
sl@0
   292
			}
sl@0
   293
		}
sl@0
   294
	
sl@0
   295
	return err;
sl@0
   296
	}
sl@0
   297
sl@0
   298
void DLddMedCh::MsCBFunc(TAny* aPtr)
sl@0
   299
/**
sl@0
   300
 * Delayed Open/Close timer callback
sl@0
   301
 */
sl@0
   302
	{
sl@0
   303
	DLddMedCh& mcldd=*(DLddMedCh*)aPtr;
sl@0
   304
	TInt err = mcldd.iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)mcldd.iDelayedOperation, NULL);
sl@0
   305
   	Kern::RequestComplete(mcldd.iClient, mcldd.iReqStat, err);
sl@0
   306
	}
sl@0
   307