1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/pccd/d_medch.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,307 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\pccd\d_medch.cpp
1.18 +// This LDD allow simulation of media change on a peripheral bus controller.
1.19 +//
1.20 +//
1.21 +
1.22 +#include <kernel/kernel.h>
1.23 +#include <drivers/pbus.h>
1.24 +#include "d_medch.h"
1.25 +
1.26 +const TInt KMajorVersionNumber=1;
1.27 +const TInt KMinorVersionNumber=0;
1.28 +const TInt KBuildVersionNumber=1;
1.29 +
1.30 +_LIT(KDFCThreadName,"D_MEDCH_DFC_THREAD");
1.31 +const TInt KMedChThreadPriority = 27;
1.32 +
1.33 +class DLddFactoryMedCh : public DLogicalDevice
1.34 + {
1.35 +public:
1.36 + DLddFactoryMedCh();
1.37 + virtual TInt Install();
1.38 + virtual void GetCaps(TDes8 &aDes) const;
1.39 + virtual TInt Create(DLogicalChannelBase*& aChannel);
1.40 + };
1.41 +
1.42 +class DLddMedCh : public DLogicalChannel
1.43 + {
1.44 +public:
1.45 + DLddMedCh();
1.46 + ~DLddMedCh();
1.47 +protected:
1.48 + virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
1.49 + virtual void HandleMsg(class TMessageBase *);
1.50 +private:
1.51 + TInt DoRequest(TInt aReqNo,TAny *a1,TAny *a2);
1.52 + TInt DoControl(TInt aFunction,TAny *a1,TAny *a2);
1.53 +private:
1.54 + static void MsCBFunc(TAny* aPtr);
1.55 +private:
1.56 + DPBusSocket* iSocketP;
1.57 + DThread* iClient;
1.58 + TRequestStatus* iReqStat;
1.59 + TDynamicDfcQue* iDfcQ;
1.60 +
1.61 + NTimer iMsCallBack;
1.62 + TInt iMsInterval;
1.63 +
1.64 + DPBusSocket::TPBusSimulateMediaState iDelayedOperation;
1.65 + };
1.66 +
1.67 +DECLARE_STANDARD_LDD()
1.68 + {
1.69 + return new DLddFactoryMedCh;
1.70 + }
1.71 +
1.72 +DLddFactoryMedCh::DLddFactoryMedCh()
1.73 +/**
1.74 + * Constructor
1.75 + */
1.76 + {
1.77 +
1.78 + iParseMask=KDeviceAllowUnit;
1.79 + iUnitsMask=0xffffffff;
1.80 + iVersion = TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber);
1.81 + }
1.82 +
1.83 +TInt DLddFactoryMedCh::Install()
1.84 +/**
1.85 + * Install the device driver.
1.86 + */
1.87 + {
1.88 +
1.89 + TPtrC name = _L("MedCh");
1.90 + return(SetName(&name));
1.91 + }
1.92 +
1.93 +void DLddFactoryMedCh::GetCaps(TDes8 &aDes) const
1.94 +/**
1.95 + * Return the media change LDD capabilities.
1.96 + */
1.97 + {
1.98 +
1.99 + TCapsMediaChangeV01 caps;
1.100 + caps.version = TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber);
1.101 + Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
1.102 + }
1.103 +
1.104 +TInt DLddFactoryMedCh::Create(DLogicalChannelBase*& aChannel)
1.105 +/**
1.106 + * Create a channel on the device.
1.107 + */
1.108 + {
1.109 +
1.110 + aChannel = new DLddMedCh;
1.111 + return aChannel ? KErrNone : KErrNoMemory;
1.112 + }
1.113 +
1.114 +DLddMedCh::DLddMedCh()
1.115 +/**
1.116 + * Constructor
1.117 + */
1.118 + : iMsCallBack(MsCBFunc, this)
1.119 + {
1.120 +
1.121 + iClient = &Kern::CurrentThread();
1.122 + ((DObject*)iClient)->Open();
1.123 + }
1.124 +
1.125 +DLddMedCh::~DLddMedCh()
1.126 +/**
1.127 + * Destructor
1.128 + */
1.129 + {
1.130 + if(iSocketP)
1.131 + (void)iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
1.132 +
1.133 + Kern::SafeClose((DObject*&)iClient, NULL);
1.134 +
1.135 + if (iDfcQ)
1.136 + iDfcQ->Destroy();
1.137 + }
1.138 +
1.139 +TInt DLddMedCh::DoCreate(TInt aUnit, const TDesC8* /*aInfo*/, const TVersion& aVer)
1.140 +/**
1.141 + * Create channel.
1.142 + */
1.143 + {
1.144 +
1.145 + if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber), aVer))
1.146 + return(KErrNotSupported);
1.147 +
1.148 + //
1.149 + // Obtain the requested socket (as specified by the opened logical unit)
1.150 + //
1.151 + iSocketP = DPBusSocket::SocketFromId(aUnit);
1.152 + if(iSocketP == NULL)
1.153 + return(KErrNoMemory);
1.154 +
1.155 + if (!iDfcQ)
1.156 + {
1.157 + TInt r = Kern::DynamicDfcQCreate(iDfcQ, KMedChThreadPriority, KDFCThreadName);
1.158 + if (r != KErrNone)
1.159 + return r;
1.160 +#ifdef CPU_AFFINITY_ANY
1.161 + NKern::ThreadSetCpuAffinity((NThread*)(iDfcQ->iThread), KCpuAffinityAny);
1.162 +#endif
1.163 +
1.164 + SetDfcQ(iDfcQ);
1.165 + }
1.166 +
1.167 + iMsgQ.Receive();
1.168 +
1.169 + return KErrNone;
1.170 + }
1.171 +
1.172 +void DLddMedCh::HandleMsg(TMessageBase* aMsg)
1.173 +/**
1.174 + * Message Handler
1.175 + */
1.176 + {
1.177 +
1.178 + TThreadMessage& m=*(TThreadMessage*)aMsg;
1.179 + TInt id=m.iValue;
1.180 +
1.181 + if (id == (TInt)ECloseMsg)
1.182 + {
1.183 + iMsCallBack.Cancel();
1.184 + (void)iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
1.185 + m.Complete(KErrNone, EFalse);
1.186 + return;
1.187 + }
1.188 + else if (id == KMaxTInt)
1.189 + {
1.190 + // DoCancel
1.191 + m.Complete(KErrNone, ETrue);
1.192 + return;
1.193 + }
1.194 +
1.195 + if (id < 0)
1.196 + {
1.197 + // DoRequest
1.198 + TRequestStatus* pS = (TRequestStatus*)m.Ptr0();
1.199 + iReqStat = pS;
1.200 + TInt r = DoRequest(~id, m.Ptr1(), m.Ptr2());
1.201 + if (r != KErrNone)
1.202 + Kern::RequestComplete(iClient, pS, r);
1.203 + m.Complete(KErrNone, ETrue);
1.204 + }
1.205 + else
1.206 + {
1.207 + // DoControl
1.208 + TInt r = DoControl(id, m.Ptr0(), m.Ptr1());
1.209 + if(r != KErrCompletion)
1.210 + {
1.211 + m.Complete(r, ETrue);
1.212 + }
1.213 + }
1.214 + }
1.215 +
1.216 +TInt DLddMedCh::DoRequest(TInt aFunction, TAny* a1, TAny* a2)
1.217 +/**
1.218 + * Asynchronous requests
1.219 + */
1.220 + {
1.221 +
1.222 + TInt err = KErrNotSupported;
1.223 +
1.224 + switch (aFunction)
1.225 + {
1.226 + case RMedCh::EDelayedDoorOpen:
1.227 + {
1.228 + const TInt KMsInterval = (TInt)a1;
1.229 + iDelayedOperation = DPBusSocket::EPeriphBusDoorOpen;
1.230 + err = iMsCallBack.OneShot(NKern::TimerTicks(KMsInterval), ETrue);
1.231 + break;
1.232 + }
1.233 +
1.234 + case RMedCh::EDelayedDoorClose:
1.235 + {
1.236 + const TInt KMsInterval = (TInt)a1;
1.237 + const TBool KMediaPresent = (TBool)a2;
1.238 + iDelayedOperation = KMediaPresent ? DPBusSocket::EPeriphBusMediaPresent : DPBusSocket::EPeriphBusMediaRemoved;
1.239 + err = iMsCallBack.OneShot(NKern::TimerTicks(KMsInterval), ETrue);
1.240 + break;
1.241 + }
1.242 +
1.243 + default:
1.244 + {
1.245 + err = KErrNotSupported;
1.246 + break;
1.247 + }
1.248 + }
1.249 +
1.250 + return err;
1.251 + }
1.252 +
1.253 +TInt DLddMedCh::DoControl(TInt aFunction,TAny* a1, TAny* /*a2*/)
1.254 +/**
1.255 + * Synchronous requests
1.256 + */
1.257 + {
1.258 +
1.259 + TInt err = KErrNotSupported;
1.260 +
1.261 + switch (aFunction)
1.262 + {
1.263 + case RMedCh::EDoorOpen:
1.264 + {
1.265 + err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusDoorOpen, NULL);
1.266 + break;
1.267 + }
1.268 +
1.269 + case RMedCh::EDoorClose:
1.270 + {
1.271 + const TBool KMediaPresent = (TBool)a1;
1.272 +
1.273 + err = iSocketP->ControlIO(DPBusSocket::EControlMediaState,
1.274 + (TAny*)(KMediaPresent ? DPBusSocket::EPeriphBusMediaPresent : DPBusSocket::EPeriphBusMediaRemoved),
1.275 + NULL);
1.276 + break;
1.277 + }
1.278 +
1.279 + case RMedCh::EDoorNormal:
1.280 + {
1.281 + err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaNormal, NULL);
1.282 + break;
1.283 + }
1.284 +
1.285 + case RMedCh::EDoubleDoorOpen:
1.286 + {
1.287 + err = iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)DPBusSocket::EPeriphBusMediaDoubleDoorOpen, NULL);
1.288 + break;
1.289 + }
1.290 +
1.291 + default:
1.292 + {
1.293 + err = KErrNotSupported;
1.294 + break;
1.295 + }
1.296 + }
1.297 +
1.298 + return err;
1.299 + }
1.300 +
1.301 +void DLddMedCh::MsCBFunc(TAny* aPtr)
1.302 +/**
1.303 + * Delayed Open/Close timer callback
1.304 + */
1.305 + {
1.306 + DLddMedCh& mcldd=*(DLddMedCh*)aPtr;
1.307 + TInt err = mcldd.iSocketP->ControlIO(DPBusSocket::EControlMediaState, (TAny*)mcldd.iDelayedOperation, NULL);
1.308 + Kern::RequestComplete(mcldd.iClient, mcldd.iReqStat, err);
1.309 + }
1.310 +