00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00036 #include <kern_priv.h>
00037 #include "driver1.h"
00038 #include "driver1_dev.h"
00039
00043 class DDevice1PddFactory : public DPhysicalDevice
00044 {
00045 public:
00046 DDevice1PddFactory();
00047
00048
00049 virtual TInt Install();
00050 virtual void GetCaps(TDes8& aDes) const;
00051 virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
00052 virtual TInt Validate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
00053 public:
00054 TInt iHardwareInUse;
00055 private:
00059 enum TMinimumLDDVersion
00060 {
00061 EMinimumLddMajorVersion=1,
00062 EMinimumLddMinorVersion=0,
00063 EMinimumLddBuild=0
00064 };
00065 };
00066
00067
00068 _LIT(KDriver1PddName,"DRIVER1.template");
00069
00073 class DDriver1Device : public DDriver1
00074 {
00075 public:
00076 DDriver1Device(DDevice1PddFactory* aFactory);
00077 ~DDriver1Device();
00078 TInt DoCreate();
00079
00080
00081 virtual TInt BufferSize() const;
00082 virtual TInt Speed() const;
00083 virtual TInt SetSpeed(TInt aSpeed);
00084 virtual TInt SendData(const TDesC8& aData);
00085 virtual void SendDataCancel();
00086 virtual TInt ReceiveData(TDes8& aBuffer);
00087 virtual void ReceiveDataCancel();
00088 private:
00089 static void SendDataTimerCallback(TAny* aPtr);
00090 void SendDataCallback();
00091 static void ReceiveDataTimerCallback(TAny* aPtr);
00092 void ReceiveDataCallback();
00093 private:
00094 DDevice1PddFactory* iFactory;
00095 TInt iSpeed;
00096 NTimer iSendDataTimer;
00097 NTimer iReceiveDataTimer;
00098 TBuf8<256> iBuffer;
00099 TDes8* iReceiveBuffer;
00100 };
00101
00102
00103
00104
00109 DECLARE_STANDARD_PDD()
00110 {
00111 return new DDevice1PddFactory;
00112 }
00113
00114 DDevice1PddFactory::DDevice1PddFactory()
00115 {
00116
00117 iVersion=RDriver1::VersionRequired();
00118 }
00119
00125 TInt DDevice1PddFactory::Install()
00126 {
00127 return SetName(&KDriver1PddName);
00128 }
00129
00135 void DDevice1PddFactory::GetCaps(TDes8& aDes) const
00136 {
00137
00138 DDriver1::TCaps caps;
00139 caps.iVersion = iVersion;
00140
00141 TInt maxLen = aDes.MaxLength();
00142 aDes.FillZ(maxLen);
00143
00144 TInt size=sizeof(caps);
00145 if(size>maxLen)
00146 size=maxLen;
00147 aDes.Copy((TUint8*)&caps,size);
00148 }
00149
00161 TInt DDevice1PddFactory::Create(DBase*& aChannel, TInt aUnit, const TDesC8* aInfo, const TVersion& aVer)
00162 {
00163
00164 (void)aUnit;
00165 (void)aInfo;
00166 (void)aVer;
00167
00168
00169 DDriver1Device* device=new DDriver1Device(this);
00170 aChannel=device;
00171 if (!device)
00172 return KErrNoMemory;
00173 return device->DoCreate();
00174 }
00175
00186 TInt DDevice1PddFactory::Validate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer)
00187 {
00188
00189 if ((!Kern::QueryVersionSupported(iVersion,aVer)) || (!Kern::QueryVersionSupported(aVer,TVersion(EMinimumLddMajorVersion,EMinimumLddMinorVersion,EMinimumLddBuild))))
00190 return KErrNotSupported;
00191
00192
00193 if (aUnit != -1)
00194 return KErrNotSupported;
00195
00196
00197
00198
00199 (void)aInfo;
00200
00201
00202 return KErrNone;
00203 }
00204
00208 DDriver1Device::DDriver1Device(DDevice1PddFactory* aFactory)
00209 : iFactory(aFactory),
00210 iSpeed(100000),
00211 iSendDataTimer(SendDataTimerCallback,this),
00212 iReceiveDataTimer(ReceiveDataTimerCallback,this)
00213 {
00214 }
00215
00219 DDriver1Device::~DDriver1Device()
00220 {
00221
00222 NKern::LockedDec(iFactory->iHardwareInUse);
00223 }
00224
00225 TInt DDriver1Device::DoCreate()
00226 {
00227
00228
00229
00230
00231
00232
00233
00234 if(NKern::LockedInc(iFactory->iHardwareInUse))
00235 return KErrInUse;
00236
00237
00238 return KErrNone;
00239 }
00243 TInt DDriver1Device::BufferSize() const
00244 {
00245 return iBuffer.MaxSize();
00246 }
00247
00251 TInt DDriver1Device::Speed() const
00252 {
00253 return iSpeed;
00254 }
00255
00260 TInt DDriver1Device::SetSpeed(TInt aSpeed)
00261 {
00262 if(aSpeed<=0)
00263 return KErrArgument;
00264 iSpeed = aSpeed;
00265 return KErrNone;
00266 }
00267
00268
00269
00273 TInt DDriver1Device::SendData(const TDesC8& aData)
00274 {
00275
00276 iBuffer=aData.Right(iBuffer.MaxSize());
00277
00278 iSendDataTimer.OneShot(aData.Size()*iSpeed/NKern::TickPeriod());
00279 return KErrNone;
00280 }
00281
00285 void DDriver1Device::SendDataCancel()
00286 {
00287
00288 iSendDataTimer.Cancel();
00289 }
00290
00295 void DDriver1Device::SendDataTimerCallback(TAny* aPtr)
00296 {
00297
00298 ((DDriver1Device*)aPtr)->SendDataCallback();
00299 }
00300
00304 void DDriver1Device::SendDataCallback()
00305 {
00306
00307 iLdd->SendDataComplete(KErrNone);
00308 }
00309
00310
00311
00312
00316 TInt DDriver1Device::ReceiveData(TDes8& aBuffer)
00317 {
00318
00319 iReceiveBuffer=&aBuffer;
00320
00321 iReceiveDataTimer.OneShot(iBuffer.Size()*iSpeed/NKern::TickPeriod());
00322 return KErrNone;
00323 }
00327 void DDriver1Device::ReceiveDataCancel()
00328 {
00329
00330 iReceiveDataTimer.Cancel();
00331 }
00336 void DDriver1Device::ReceiveDataTimerCallback(TAny* aPtr)
00337 {
00338
00339 ((DDriver1Device*)aPtr)->ReceiveDataCallback();
00340 }
00344 void DDriver1Device::ReceiveDataCallback()
00345 {
00346
00347 *iReceiveBuffer=iBuffer;
00348
00349 iLdd->ReceiveDataComplete(KErrNone);
00350 }