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