os/kernelhwsrv/kerneltest/e32test/demandpaging/d_pagingexample_2_post.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) 2008-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
// e32\e32test\demandpaging\d_pagingexample_2_post.cpp
sl@0
    15
// Demand paging migration example device driver d_pagingexample_2_post: a
sl@0
    16
// DLogicalChannelBase-dervied driver, post-migration
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "d_pagingexample.h"
sl@0
    21
#include <kernel/kernel.h>
sl@0
    22
#include <kernel/kern_priv.h>
sl@0
    23
sl@0
    24
const TInt KDfcQThreadPriority = 25;
sl@0
    25
const TInt KBufferSize = KMaxTransferSize;
sl@0
    26
sl@0
    27
//
sl@0
    28
// Logical channel
sl@0
    29
//
sl@0
    30
sl@0
    31
class DExampleChannel : public DLogicalChannelBase
sl@0
    32
	{
sl@0
    33
public:
sl@0
    34
	typedef RPagingExample::TConfigData TConfigData;
sl@0
    35
	typedef RPagingExample::TValueStruct TValueStruct;
sl@0
    36
public:
sl@0
    37
	DExampleChannel();
sl@0
    38
	~DExampleChannel();
sl@0
    39
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
    40
	virtual TInt Request(TInt aReqNo, TAny* a1, TAny* a2);
sl@0
    41
	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
sl@0
    42
	TInt DoRequest(TInt aFunction, TRequestStatus* aStatus, TAny* a1, TAny* a2);
sl@0
    43
	TInt DoCancel(TUint aMask);
sl@0
    44
private:
sl@0
    45
 	TDfcQue* DfcQ();
sl@0
    46
	void Shutdown();
sl@0
    47
	void GetConfig(TConfigData&);
sl@0
    48
	void SetConfig(const TConfigData&);
sl@0
    49
	TInt StartNotify(TRequestStatus* aStatus);
sl@0
    50
	TInt StartAsyncGetValue(TInt* aValue, TRequestStatus* aStatus);
sl@0
    51
	TInt StartAsyncGetValue2(TInt* aValue1, TInt* aValue2, TRequestStatus* aStatus);
sl@0
    52
	TInt StartRead(TAny* aBuffer, TInt aLength, TRequestStatus* aStatus);
sl@0
    53
	TInt StartReadDes(TDes8* aDesOut, TRequestStatus* aStatus);
sl@0
    54
	TInt StartWrite(TAny* aBuffer, TInt aLength, TRequestStatus* aStatus);
sl@0
    55
	TInt StartWriteDes(const TDesC8* aDesIn, TRequestStatus* aStatus);
sl@0
    56
	void ReceiveToReadBuffer();
sl@0
    57
	void SendFromWriteBuffer();
sl@0
    58
	static void CancelDfcFunc(TAny* aPtr);
sl@0
    59
	static void AsyncGetValueCompleteDfcFunc(TAny* aPtr);
sl@0
    60
	static void AsyncGetValue2CompleteDfcFunc(TAny* aPtr);
sl@0
    61
	static void ReceiveCompleteDfcFunc(TAny* aPtr);
sl@0
    62
	static void SendCompleteDfcFunc(TAny* aPtr);
sl@0
    63
	void CompleteNotify();
sl@0
    64
	void CompleteAsyncGetValue();
sl@0
    65
	void CompleteAsyncGetValue2();
sl@0
    66
	void CompleteRead();
sl@0
    67
	void CompleteWrite();
sl@0
    68
	void Cancel();
sl@0
    69
private:
sl@0
    70
	NFastMutex iLock;
sl@0
    71
	TDynamicDfcQue* iDynamicDfcQ;
sl@0
    72
	TConfigData iConfig;
sl@0
    73
	DThread* iClient;
sl@0
    74
	TDfc iCancelDfc;
sl@0
    75
	
sl@0
    76
	// Notify
sl@0
    77
	TClientRequest* iNotifyRequest;
sl@0
    78
	
sl@0
    79
	// Async get value
sl@0
    80
	TClientDataRequest<TValueStruct>* iAsyncGetValueRequest;
sl@0
    81
	NTimer iAsyncGetValueTimer;
sl@0
    82
	TDfc iAsyncGetValueDfc;
sl@0
    83
sl@0
    84
	// Async get value
sl@0
    85
	TClientDataRequest2<TInt,TInt>* iAsyncGetValue2Request;
sl@0
    86
	NTimer iAsyncGetValue2Timer;
sl@0
    87
	TDfc iAsyncGetValue2Dfc;
sl@0
    88
sl@0
    89
	// Read
sl@0
    90
	TClientBufferRequest* iReadRequest;
sl@0
    91
	NTimer iReadTimer;
sl@0
    92
	TClientBuffer* iClientReadBuffer;
sl@0
    93
	TDfc iCompleteReadDfc;
sl@0
    94
	
sl@0
    95
	// Write
sl@0
    96
	TClientBufferRequest* iWriteRequest;
sl@0
    97
	NTimer iWriteTimer;
sl@0
    98
	TClientBuffer* iClientWriteBuffer;
sl@0
    99
	TDfc iCompleteWriteDfc;
sl@0
   100
	TUint8 iBuffer[KBufferSize];
sl@0
   101
	};
sl@0
   102
sl@0
   103
DExampleChannel::DExampleChannel() :
sl@0
   104
	iCancelDfc(CancelDfcFunc, this, 0),
sl@0
   105
	iAsyncGetValueTimer(NULL, this),
sl@0
   106
	iAsyncGetValueDfc(AsyncGetValueCompleteDfcFunc, this, 0),
sl@0
   107
	iAsyncGetValue2Timer(NULL, this),
sl@0
   108
	iAsyncGetValue2Dfc(AsyncGetValue2CompleteDfcFunc, this, 0),
sl@0
   109
	iReadTimer(NULL, this),
sl@0
   110
	iCompleteReadDfc(ReceiveCompleteDfcFunc, this, 0),
sl@0
   111
	iWriteTimer(NULL, this),
sl@0
   112
	iCompleteWriteDfc(SendCompleteDfcFunc, this, 0)
sl@0
   113
	{
sl@0
   114
	iClient = &Kern::CurrentThread();
sl@0
   115
	iClient->Open();
sl@0
   116
	}
sl@0
   117
sl@0
   118
DExampleChannel::~DExampleChannel()
sl@0
   119
	{
sl@0
   120
	Kern::SafeClose((DObject*&)iClient, NULL);
sl@0
   121
	if (iDynamicDfcQ)
sl@0
   122
		iDynamicDfcQ->Destroy();
sl@0
   123
	Kern::DestroyClientRequest(iNotifyRequest);
sl@0
   124
	Kern::DestroyClientRequest(iAsyncGetValueRequest);
sl@0
   125
	Kern::DestroyClientRequest(iAsyncGetValue2Request);
sl@0
   126
	Kern::DestroyClientBufferRequest(iReadRequest);
sl@0
   127
	Kern::DestroyClientBufferRequest(iWriteRequest);
sl@0
   128
	}
sl@0
   129
sl@0
   130
TDfcQue* DExampleChannel::DfcQ()
sl@0
   131
	{
sl@0
   132
	return iDynamicDfcQ;
sl@0
   133
	}
sl@0
   134
sl@0
   135
TInt DExampleChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& /*aVer*/)
sl@0
   136
	{
sl@0
   137
	TInt r;
sl@0
   138
	
sl@0
   139
	r = Kern::CreateClientRequest(iNotifyRequest);
sl@0
   140
	if (r != KErrNone)
sl@0
   141
		return r;
sl@0
   142
	r = Kern::CreateClientDataRequest(iAsyncGetValueRequest);
sl@0
   143
	if (r != KErrNone)
sl@0
   144
		return r;
sl@0
   145
	r = Kern::CreateClientDataRequest2(iAsyncGetValue2Request);
sl@0
   146
	if (r != KErrNone)
sl@0
   147
		return r;
sl@0
   148
	r = Kern::CreateClientBufferRequest(iWriteRequest, 1, TClientBufferRequest::EPinVirtual);
sl@0
   149
	if (r != KErrNone)
sl@0
   150
		return r;
sl@0
   151
	
sl@0
   152
	// create a dynamic DFC queue, which is used for handling client messages and for our own DFCs
sl@0
   153
	r = Kern::DynamicDfcQCreate(iDynamicDfcQ, KDfcQThreadPriority, KPagingExample1PostLdd);
sl@0
   154
	if (r != KErrNone)
sl@0
   155
		return r;
sl@0
   156
sl@0
   157
	// todo: this will be the default anyway
sl@0
   158
	iDynamicDfcQ->SetRealtimeState(ERealtimeStateOn);
sl@0
   159
	
sl@0
   160
	iCancelDfc.SetDfcQ(DfcQ());
sl@0
   161
	iAsyncGetValueDfc.SetDfcQ(DfcQ());
sl@0
   162
	iAsyncGetValue2Dfc.SetDfcQ(DfcQ());
sl@0
   163
	iCompleteReadDfc.SetDfcQ(DfcQ());
sl@0
   164
	iCompleteWriteDfc.SetDfcQ(DfcQ());
sl@0
   165
	return KErrNone;
sl@0
   166
	}
sl@0
   167
sl@0
   168
TInt DExampleChannel::Request(TInt aReqNo, TAny* a1, TAny* a2)
sl@0
   169
	{
sl@0
   170
	TInt r = KErrNone;
sl@0
   171
	if (&Kern::CurrentThread() != iClient)
sl@0
   172
		r = KErrAccessDenied;  // we only support one client
sl@0
   173
	else if (aReqNo==KMaxTInt)
sl@0
   174
		{
sl@0
   175
		// DoCancel
sl@0
   176
		r = DoCancel((TInt)a1);
sl@0
   177
		}
sl@0
   178
    else if (aReqNo<0)
sl@0
   179
		{
sl@0
   180
		// DoRequest
sl@0
   181
		TRequestStatus* pS=(TRequestStatus*)a1;
sl@0
   182
		TAny* array[2] = { NULL, NULL };
sl@0
   183
		kumemget32(array, a2, 2 * sizeof(TAny*));
sl@0
   184
		r = DoRequest(~aReqNo, pS, array[0], array[1]);
sl@0
   185
		if(r != KErrNone)
sl@0
   186
			Kern::RequestComplete(pS, r);
sl@0
   187
		r = KErrNone;
sl@0
   188
		}
sl@0
   189
    else
sl@0
   190
		{
sl@0
   191
		// DoControl
sl@0
   192
		r = DoControl(aReqNo, a1, a2);
sl@0
   193
		}
sl@0
   194
	return r;
sl@0
   195
	}
sl@0
   196
sl@0
   197
TInt DExampleChannel::DoControl(TInt aFunction,TAny* a1,TAny* /*a2*/)
sl@0
   198
	{
sl@0
   199
	TInt r = KErrNone;
sl@0
   200
	TConfigData configBuffer;
sl@0
   201
	switch (aFunction)
sl@0
   202
		{
sl@0
   203
		case RPagingExample::EGetConfig:
sl@0
   204
			GetConfig(configBuffer);
sl@0
   205
			umemput32(a1, (TAny*)&configBuffer, sizeof(TConfigData));
sl@0
   206
			break;
sl@0
   207
sl@0
   208
		case RPagingExample::ESetConfig:
sl@0
   209
			umemget32(&configBuffer, a1, sizeof(TConfigData));
sl@0
   210
			SetConfig(configBuffer);
sl@0
   211
			break;
sl@0
   212
sl@0
   213
		default:
sl@0
   214
			r = KErrNotSupported;
sl@0
   215
		}
sl@0
   216
	return r;
sl@0
   217
	}
sl@0
   218
sl@0
   219
TInt DExampleChannel::DoRequest(TInt aFunction, TRequestStatus* aStatus, TAny* a1, TAny* a2)
sl@0
   220
	{
sl@0
   221
	TInt r = KErrNotSupported;
sl@0
   222
	switch (aFunction)
sl@0
   223
		{
sl@0
   224
		case RPagingExample::ERequestNotify:
sl@0
   225
			r = StartNotify(aStatus);
sl@0
   226
			break;
sl@0
   227
			
sl@0
   228
		case RPagingExample::ERequestAsyncGetValue:
sl@0
   229
			r = StartAsyncGetValue((TInt*)a1, aStatus);
sl@0
   230
			break;
sl@0
   231
			
sl@0
   232
		case RPagingExample::ERequestAsyncGetValue2:
sl@0
   233
			r = StartAsyncGetValue2((TInt*)a1, (TInt*)a2, aStatus);
sl@0
   234
			break;
sl@0
   235
			
sl@0
   236
		case RPagingExample::ERequestRead:
sl@0
   237
			r = StartRead(a1, (TInt)a2, aStatus);
sl@0
   238
			break;
sl@0
   239
			
sl@0
   240
		case RPagingExample::ERequestReadDes:
sl@0
   241
			r = StartReadDes((TDes8*)a1, aStatus);
sl@0
   242
			break;
sl@0
   243
sl@0
   244
		case RPagingExample::ERequestWrite:
sl@0
   245
			r = StartWrite(a1, (TInt)a2, aStatus);
sl@0
   246
			break;
sl@0
   247
			
sl@0
   248
		case RPagingExample::ERequestWriteDes:
sl@0
   249
			r = StartWriteDes((TDes8*)a1, aStatus);
sl@0
   250
			break;
sl@0
   251
		}
sl@0
   252
	return r;
sl@0
   253
	}
sl@0
   254
sl@0
   255
TInt DExampleChannel::DoCancel(TUint /*aMask*/)
sl@0
   256
	{
sl@0
   257
	iCancelDfc.Enque();
sl@0
   258
	return KErrNone;
sl@0
   259
	}
sl@0
   260
sl@0
   261
void DExampleChannel::CancelDfcFunc(TAny* aPtr)
sl@0
   262
	{
sl@0
   263
	DExampleChannel* self = (DExampleChannel*)aPtr;
sl@0
   264
	self->Cancel();
sl@0
   265
	}
sl@0
   266
sl@0
   267
void DExampleChannel::Cancel()
sl@0
   268
	{
sl@0
   269
	if (iAsyncGetValueRequest->IsReady())
sl@0
   270
		{
sl@0
   271
		iAsyncGetValueTimer.Cancel();
sl@0
   272
		iAsyncGetValueDfc.Cancel();
sl@0
   273
		Kern::QueueRequestComplete(iClient, iAsyncGetValueRequest, KErrCancel);
sl@0
   274
		}
sl@0
   275
	
sl@0
   276
	if (iAsyncGetValue2Request->IsReady())
sl@0
   277
		{
sl@0
   278
		iAsyncGetValue2Timer.Cancel();
sl@0
   279
		iAsyncGetValue2Dfc.Cancel();
sl@0
   280
		Kern::QueueRequestComplete(iClient, iAsyncGetValue2Request, KErrCancel);
sl@0
   281
		}
sl@0
   282
sl@0
   283
	if (iReadRequest && iReadRequest->IsReady())
sl@0
   284
		{
sl@0
   285
		iReadTimer.Cancel();
sl@0
   286
		iCompleteReadDfc.Cancel();
sl@0
   287
		Kern::QueueBufferRequestComplete(iClient, iReadRequest, KErrCancel);
sl@0
   288
		Kern::DestroyClientBufferRequest(iReadRequest);
sl@0
   289
		}
sl@0
   290
sl@0
   291
	if (iWriteRequest->IsReady())
sl@0
   292
		{
sl@0
   293
		iWriteTimer.Cancel();
sl@0
   294
		iCompleteWriteDfc.Cancel();
sl@0
   295
		Kern::QueueBufferRequestComplete(iClient, iWriteRequest, KErrCancel);
sl@0
   296
		}
sl@0
   297
	}
sl@0
   298
sl@0
   299
void DExampleChannel::Shutdown()
sl@0
   300
	{
sl@0
   301
	}
sl@0
   302
sl@0
   303
void DExampleChannel::GetConfig(TConfigData& aConfigOut)
sl@0
   304
	{
sl@0
   305
	NKern::FMWait(&iLock);
sl@0
   306
	aConfigOut = iConfig;
sl@0
   307
	NKern::FMSignal(&iLock);
sl@0
   308
	}
sl@0
   309
sl@0
   310
void DExampleChannel::SetConfig(const TConfigData& aNewConfig)
sl@0
   311
	{
sl@0
   312
	NKern::FMWait(&iLock);
sl@0
   313
	iConfig = aNewConfig;
sl@0
   314
	NKern::FMSignal(&iLock);
sl@0
   315
	}
sl@0
   316
sl@0
   317
TInt DExampleChannel::StartNotify(TRequestStatus* aStatus)
sl@0
   318
	{
sl@0
   319
	// example implementation completes the request immediately
sl@0
   320
	TInt r = iNotifyRequest->SetStatus(aStatus);
sl@0
   321
	if (r != KErrNone)
sl@0
   322
		return r;
sl@0
   323
	CompleteNotify(); // example implementation completes the request immediately
sl@0
   324
	return KErrNone;
sl@0
   325
	}
sl@0
   326
sl@0
   327
void DExampleChannel::CompleteNotify()
sl@0
   328
	{
sl@0
   329
	Kern::QueueRequestComplete(iClient, iNotifyRequest, KErrNone);
sl@0
   330
	}
sl@0
   331
sl@0
   332
TInt DExampleChannel::StartAsyncGetValue(TInt* aValue, TRequestStatus* aStatus)
sl@0
   333
	{
sl@0
   334
	// use of TClientDataRequest API protected by fast mutex
sl@0
   335
	NKern::FMWait(&iLock);
sl@0
   336
	TInt r = iAsyncGetValueRequest->SetStatus(aStatus);
sl@0
   337
	if (r == KErrNone)
sl@0
   338
		iAsyncGetValueRequest->SetDestPtr(aValue);
sl@0
   339
	NKern::FMSignal(&iLock);
sl@0
   340
	if (r != KErrNone)
sl@0
   341
		return r;
sl@0
   342
sl@0
   343
	// queue a timer to simulate an asynchronous operation
sl@0
   344
	iAsyncGetValueTimer.OneShot(KAsyncDelay, iAsyncGetValueDfc);
sl@0
   345
	return KErrNone;
sl@0
   346
	}
sl@0
   347
sl@0
   348
void DExampleChannel::AsyncGetValueCompleteDfcFunc(TAny* aPtr)
sl@0
   349
	{
sl@0
   350
	DExampleChannel* self = (DExampleChannel*)aPtr;
sl@0
   351
	self->CompleteAsyncGetValue();
sl@0
   352
	}
sl@0
   353
sl@0
   354
void DExampleChannel::CompleteAsyncGetValue()
sl@0
   355
	{
sl@0
   356
	// use of TClientDataRequest API protected by fast mutex
sl@0
   357
	NKern::FMWait(&iLock);
sl@0
   358
	if (iAsyncGetValueRequest->IsReady())
sl@0
   359
		{
sl@0
   360
		iAsyncGetValueRequest->Data().iValue1 = 1;
sl@0
   361
		iAsyncGetValueRequest->Data().iValue2 = _L8("shrt");
sl@0
   362
		Kern::QueueRequestComplete(iClient, iAsyncGetValueRequest, KErrNone);
sl@0
   363
		}
sl@0
   364
	NKern::FMSignal(&iLock);
sl@0
   365
	}
sl@0
   366
sl@0
   367
TInt DExampleChannel::StartAsyncGetValue2(TInt* aValue1, TInt* aValue2, TRequestStatus* aStatus)
sl@0
   368
	{
sl@0
   369
	// use of TClientDataRequest API protected by fast mutex
sl@0
   370
	NKern::FMWait(&iLock);
sl@0
   371
	TInt r = iAsyncGetValue2Request->SetStatus(aStatus);
sl@0
   372
	if (r == KErrNone)
sl@0
   373
		iAsyncGetValue2Request->SetDestPtr1(aValue1);
sl@0
   374
	if (r == KErrNone)
sl@0
   375
		iAsyncGetValue2Request->SetDestPtr2(aValue2);
sl@0
   376
	NKern::FMSignal(&iLock);
sl@0
   377
	if (r != KErrNone)
sl@0
   378
		return r;
sl@0
   379
sl@0
   380
	// queue a timer to simulate an asynchronous operation
sl@0
   381
	iAsyncGetValue2Timer.OneShot(KAsyncDelay, iAsyncGetValue2Dfc);
sl@0
   382
	return KErrNone;
sl@0
   383
	}
sl@0
   384
sl@0
   385
void DExampleChannel::AsyncGetValue2CompleteDfcFunc(TAny* aPtr)
sl@0
   386
	{
sl@0
   387
	DExampleChannel* self = (DExampleChannel*)aPtr;
sl@0
   388
	self->CompleteAsyncGetValue2();
sl@0
   389
	}
sl@0
   390
sl@0
   391
void DExampleChannel::CompleteAsyncGetValue2()
sl@0
   392
	{
sl@0
   393
	// use of TClientDataRequest API protected by fast mutex
sl@0
   394
	NKern::FMWait(&iLock);
sl@0
   395
	if (iAsyncGetValue2Request->IsReady())
sl@0
   396
		{
sl@0
   397
		iAsyncGetValue2Request->Data1() = 1;
sl@0
   398
		iAsyncGetValue2Request->Data2() = 2;
sl@0
   399
		Kern::QueueRequestComplete(iClient, iAsyncGetValue2Request, KErrNone);
sl@0
   400
		}
sl@0
   401
	NKern::FMSignal(&iLock);
sl@0
   402
	}
sl@0
   403
sl@0
   404
TInt DExampleChannel::StartRead(TAny* aBuffer, TInt aLength, TRequestStatus* aStatus)
sl@0
   405
	{
sl@0
   406
	// check length argument first
sl@0
   407
	if (aLength < 1 || aLength > KBufferSize)
sl@0
   408
		return KErrArgument;
sl@0
   409
sl@0
   410
	// normally drivers would pre-create a TClientBufferRequest where possible, but here we create
sl@0
   411
	// one on demand to test this possibilty
sl@0
   412
	NKern::ThreadEnterCS();
sl@0
   413
	TInt r = Kern::CreateClientBufferRequest(iReadRequest, 1, TClientBufferRequest::EPinVirtual);
sl@0
   414
	NKern::ThreadLeaveCS();  // iReadRequest is deleted by the destructor
sl@0
   415
	if (r != KErrNone)
sl@0
   416
		return r;
sl@0
   417
sl@0
   418
	// start request setup
sl@0
   419
	r = iReadRequest->StartSetup(aStatus);
sl@0
   420
	if (r != KErrNone)
sl@0
   421
		return r;
sl@0
   422
	
sl@0
   423
	// add client buffer, this does pinning in context of client thread
sl@0
   424
	r = iReadRequest->AddBuffer(iClientReadBuffer, (TLinAddr)aBuffer, aLength, ETrue);
sl@0
   425
	if (r != KErrNone)
sl@0
   426
		return KErrNoMemory;
sl@0
   427
sl@0
   428
	iReadRequest->EndSetup();
sl@0
   429
	
sl@0
   430
	ReceiveToReadBuffer();
sl@0
   431
	return KErrNone;
sl@0
   432
	}
sl@0
   433
sl@0
   434
TInt DExampleChannel::StartReadDes(TDes8* aDesOut, TRequestStatus* aStatus)
sl@0
   435
	{
sl@0
   436
	// normally drivers would pre-create a TClientBufferRequest where possible, but here we create
sl@0
   437
	// one on demand to test this possibilty
sl@0
   438
	NKern::ThreadEnterCS();
sl@0
   439
	TInt r = Kern::CreateClientBufferRequest(iReadRequest, 1, TClientBufferRequest::EPinVirtual);
sl@0
   440
	NKern::ThreadLeaveCS();  // iReadRequest is deleted by the destructor
sl@0
   441
	if (r != KErrNone)
sl@0
   442
		return r;
sl@0
   443
	
sl@0
   444
	// start request setup
sl@0
   445
	r = iReadRequest->StartSetup(aStatus);
sl@0
   446
	if (r != KErrNone)
sl@0
   447
		return r;
sl@0
   448
sl@0
   449
	// add client descriptor, this does pinning in context of client thread
sl@0
   450
	r = iReadRequest->AddBuffer(iClientReadBuffer, aDesOut);
sl@0
   451
	if (r != KErrNone)
sl@0
   452
		return r;
sl@0
   453
sl@0
   454
	// can check length argument here
sl@0
   455
	TInt length = iClientReadBuffer->MaxLength();
sl@0
   456
	if (length < 1 || length > KBufferSize)
sl@0
   457
		{
sl@0
   458
		// need to reset object so it can be reused
sl@0
   459
		iReadRequest->Reset();
sl@0
   460
		return KErrArgument;
sl@0
   461
		}
sl@0
   462
sl@0
   463
	iReadRequest->EndSetup();	
sl@0
   464
sl@0
   465
	ReceiveToReadBuffer();
sl@0
   466
	return KErrNone;
sl@0
   467
	}
sl@0
   468
sl@0
   469
TInt DExampleChannel::StartWrite(TAny* aBuffer, TInt aLength, TRequestStatus* aStatus)
sl@0
   470
	{
sl@0
   471
	// check length argument first
sl@0
   472
	if (aLength < 1 || aLength > KBufferSize)
sl@0
   473
		return KErrArgument;
sl@0
   474
sl@0
   475
	// demonstrate use the single-buffer version of Setup
sl@0
   476
	TInt r = iWriteRequest->Setup(iClientWriteBuffer, aStatus, (TLinAddr)aBuffer, aLength);
sl@0
   477
	if (r != KErrNone)
sl@0
   478
		return r;
sl@0
   479
sl@0
   480
	SendFromWriteBuffer();
sl@0
   481
	return KErrNone;
sl@0
   482
	}
sl@0
   483
sl@0
   484
TInt DExampleChannel::StartWriteDes(const TDesC8* aDesIn, TRequestStatus* aStatus)
sl@0
   485
	{
sl@0
   486
	// demonstrate use the single-buffer version of Setup
sl@0
   487
	TInt r = iWriteRequest->Setup(iClientWriteBuffer, aStatus, (TAny*)aDesIn);
sl@0
   488
	if (r != KErrNone)
sl@0
   489
		return r;
sl@0
   490
sl@0
   491
	// can check length argument here
sl@0
   492
	TInt length = iClientWriteBuffer->Length();
sl@0
   493
	if (length < 1 || length > KBufferSize)
sl@0
   494
		{
sl@0
   495
		// need to reset object so it can be reused
sl@0
   496
		iWriteRequest->Reset();
sl@0
   497
		return KErrArgument;
sl@0
   498
		}
sl@0
   499
	
sl@0
   500
	SendFromWriteBuffer();
sl@0
   501
	return KErrNone;
sl@0
   502
	}
sl@0
   503
sl@0
   504
void DExampleChannel::ReceiveToReadBuffer()
sl@0
   505
	{
sl@0
   506
	// just queue a timer to simulate an asynchronous receive operation
sl@0
   507
	// actually will return the previous contents of the buffer
sl@0
   508
	NKern::FMWait(&iLock);
sl@0
   509
	// The synchonisation is for illustrative purposes only - in a real driver we might make use of
sl@0
   510
	// the configution here, so this would need to be protected from concurrent modification.
sl@0
   511
	NKern::FMSignal(&iLock);
sl@0
   512
	iReadTimer.OneShot(KAsyncDelay, iCompleteReadDfc);
sl@0
   513
	}
sl@0
   514
sl@0
   515
void DExampleChannel::SendFromWriteBuffer()
sl@0
   516
	{
sl@0
   517
	// just queue a timer to simulate an asynchronous send operation
sl@0
   518
	NKern::FMWait(&iLock);
sl@0
   519
	// The synchonisation is for illustrative purposes only - in a real driver we might make use of
sl@0
   520
	// the configution here, so this would need to be protected from concurrent modification.
sl@0
   521
	NKern::FMSignal(&iLock);
sl@0
   522
	iWriteTimer.OneShot(KAsyncDelay, iCompleteWriteDfc);
sl@0
   523
	}
sl@0
   524
sl@0
   525
void DExampleChannel::ReceiveCompleteDfcFunc(TAny* aPtr)
sl@0
   526
	{
sl@0
   527
	DExampleChannel* self = (DExampleChannel*)aPtr;
sl@0
   528
	self->CompleteRead();
sl@0
   529
	}
sl@0
   530
sl@0
   531
void DExampleChannel::SendCompleteDfcFunc(TAny* aPtr)
sl@0
   532
	{
sl@0
   533
	DExampleChannel* self = (DExampleChannel*)aPtr;
sl@0
   534
	self->CompleteWrite();
sl@0
   535
	}
sl@0
   536
sl@0
   537
void DExampleChannel::CompleteRead()
sl@0
   538
	{
sl@0
   539
	TPtrC8 des(iBuffer, iClientReadBuffer->MaxLength());
sl@0
   540
	TInt r = Kern::ThreadBufWrite(iClient, iClientReadBuffer, des, 0, KChunkShiftBy0, iClient);
sl@0
   541
	Kern::QueueBufferRequestComplete(iClient, iReadRequest, r);
sl@0
   542
	Kern::DestroyClientBufferRequest(iReadRequest);
sl@0
   543
	}
sl@0
   544
sl@0
   545
void DExampleChannel::CompleteWrite()
sl@0
   546
	{
sl@0
   547
	TPtr8 des(iBuffer, iClientWriteBuffer->Length());
sl@0
   548
	TInt r = Kern::ThreadBufRead(iClient, iClientWriteBuffer, des, 0, KChunkShiftBy0);
sl@0
   549
	Kern::QueueBufferRequestComplete(iClient, iWriteRequest, r);
sl@0
   550
	}
sl@0
   551
sl@0
   552
//
sl@0
   553
// Logical device
sl@0
   554
//
sl@0
   555
sl@0
   556
class DExampleFactory : public DLogicalDevice
sl@0
   557
	{
sl@0
   558
public:
sl@0
   559
	DExampleFactory();
sl@0
   560
	virtual TInt Install();
sl@0
   561
	virtual void GetCaps(TDes8& aDes) const;
sl@0
   562
	virtual TInt Create(DLogicalChannelBase*& aChannel);
sl@0
   563
	};
sl@0
   564
sl@0
   565
DExampleFactory::DExampleFactory()
sl@0
   566
	{
sl@0
   567
	iParseMask = 0;
sl@0
   568
	iVersion = TVersion(1, 0, 0);
sl@0
   569
	}
sl@0
   570
sl@0
   571
TInt DExampleFactory::Install()
sl@0
   572
	{
sl@0
   573
	return SetName(&KPagingExample2PostLdd);
sl@0
   574
	}
sl@0
   575
sl@0
   576
void DExampleFactory::GetCaps(TDes8& /*aDes*/) const
sl@0
   577
	{
sl@0
   578
	// not used but required as DLogicalDevice::GetCaps is pure virtual
sl@0
   579
	}
sl@0
   580
sl@0
   581
TInt DExampleFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   582
	{
sl@0
   583
	aChannel = new DExampleChannel;
sl@0
   584
	return aChannel ? KErrNone : KErrNoMemory;
sl@0
   585
	}
sl@0
   586
sl@0
   587
DECLARE_STANDARD_LDD()
sl@0
   588
	{
sl@0
   589
	return new DExampleFactory;
sl@0
   590
	}