os/kernelhwsrv/kerneltest/e32test/personality/example/init.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) 2003-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\personality\example\init.cpp
sl@0
    15
// Test code for example RTOS personality.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <kernel/kern_priv.h>
sl@0
    20
#include <personality/example/personality.h>
sl@0
    21
#include <personality/example/personality_int.h>
sl@0
    22
#include "ifcldd.h"
sl@0
    23
sl@0
    24
#define	OC_TASK			0
sl@0
    25
sl@0
    26
#define MSG_ID_INIT		1
sl@0
    27
#define MSG_ID_RUN		2
sl@0
    28
#define MSG_ID_RUN_P	3
sl@0
    29
#define	MSG_ID_RND_ISR	4
sl@0
    30
#define MSG_ID_DONE		5
sl@0
    31
#define	MSG_ID_DATA		6
sl@0
    32
#define	MSG_ID_FLUSH	7
sl@0
    33
#define MSG_ID_SEM_RPT	8
sl@0
    34
#define MSG_ID_RCV_RPT	9
sl@0
    35
#define MSG_ID_TM_RPT	10
sl@0
    36
sl@0
    37
typedef struct _random_isr_msg
sl@0
    38
	{
sl@0
    39
	msghdr			header;
sl@0
    40
	unsigned		random_isr_number;
sl@0
    41
	unsigned		extra;
sl@0
    42
	} random_isr_msg;
sl@0
    43
sl@0
    44
typedef struct _data_msg
sl@0
    45
	{
sl@0
    46
	msghdr			header;
sl@0
    47
	int				length;
sl@0
    48
	unsigned char	checksum;
sl@0
    49
	unsigned char	data[1];
sl@0
    50
	} data_msg;
sl@0
    51
sl@0
    52
typedef struct _report_msg
sl@0
    53
	{
sl@0
    54
	msghdr			header;
sl@0
    55
	int				pad;
sl@0
    56
	unsigned		count;
sl@0
    57
	unsigned		ok_count;
sl@0
    58
	unsigned		bad_count;
sl@0
    59
	} report_msg;
sl@0
    60
sl@0
    61
const TInt KMajorVersionNumber=0;
sl@0
    62
const TInt KMinorVersionNumber=1;
sl@0
    63
const TInt KBuildVersionNumber=1;
sl@0
    64
sl@0
    65
void RxMsg(TAny* aPtr);
sl@0
    66
sl@0
    67
TPMsgQ ThePMsgQ(&RxMsg, 0, Kern::DfcQue0(), 7);
sl@0
    68
sl@0
    69
NONSHARABLE_CLASS(DRtosIfcFactory) : public DLogicalDevice
sl@0
    70
	{
sl@0
    71
public:
sl@0
    72
	DRtosIfcFactory();
sl@0
    73
	virtual TInt Install();						//overriding pure virtual
sl@0
    74
	virtual void GetCaps(TDes8& aDes) const;	//overriding pure virtual
sl@0
    75
	virtual TInt Create(DLogicalChannelBase*& aChannel);	//overriding pure virtual
sl@0
    76
	};
sl@0
    77
sl@0
    78
class TRxQ
sl@0
    79
	{
sl@0
    80
public:
sl@0
    81
	TRxQ(DThread* aThread) : iFirst(0), iLast(0), iStatus(0), iPtr(0), iThread(aThread) {}
sl@0
    82
	TInt QueueReq(TRequestStatus* aStatus, TAny* aPtr);
sl@0
    83
	void AddMsg(msghdr* aM);
sl@0
    84
	inline TBool MsgPresent() {return iFirst!=NULL;}
sl@0
    85
	void CompleteReq();
sl@0
    86
	void CancelReq();
sl@0
    87
	void Close();
sl@0
    88
public:
sl@0
    89
	msghdr* iFirst;
sl@0
    90
	msghdr* iLast;
sl@0
    91
	TRequestStatus* iStatus;
sl@0
    92
	TAny* iPtr;
sl@0
    93
	DThread* iThread;
sl@0
    94
	};
sl@0
    95
sl@0
    96
TInt TRxQ::QueueReq(TRequestStatus* aStatus, TAny* aPtr)
sl@0
    97
	{
sl@0
    98
	if (iStatus)
sl@0
    99
		return KErrInUse;
sl@0
   100
	iStatus = aStatus;
sl@0
   101
	iPtr = aPtr;
sl@0
   102
	if (iFirst)
sl@0
   103
		CompleteReq();
sl@0
   104
	return KErrNone;
sl@0
   105
	}
sl@0
   106
sl@0
   107
void TRxQ::AddMsg(msghdr* aM)
sl@0
   108
	{
sl@0
   109
	aM->next = NULL;
sl@0
   110
	if (iLast)
sl@0
   111
		iLast->next = aM;
sl@0
   112
	else
sl@0
   113
		iFirst = aM;
sl@0
   114
	iLast = aM;
sl@0
   115
	if (iStatus)
sl@0
   116
		CompleteReq();
sl@0
   117
	}
sl@0
   118
sl@0
   119
void TRxQ::CompleteReq()
sl@0
   120
	{
sl@0
   121
	msghdr* m = iFirst;
sl@0
   122
	iFirst = m->next;
sl@0
   123
	if (!iFirst)
sl@0
   124
		iLast = NULL;
sl@0
   125
	TInt r = KErrNone;
sl@0
   126
	switch (m->msg_id)
sl@0
   127
		{
sl@0
   128
		case MSG_ID_DATA:
sl@0
   129
			{
sl@0
   130
			data_msg* dm = (data_msg*)m;
sl@0
   131
			r = Kern::ThreadRawWrite(iThread, iPtr, &dm->length, dm->length + 5, iThread);
sl@0
   132
			break;
sl@0
   133
			}
sl@0
   134
		case MSG_ID_SEM_RPT:
sl@0
   135
		case MSG_ID_RCV_RPT:
sl@0
   136
		case MSG_ID_TM_RPT:
sl@0
   137
			{
sl@0
   138
			report_msg* rpt = (report_msg*)m;
sl@0
   139
			rpt->pad = m->msg_id;
sl@0
   140
			r = Kern::ThreadRawWrite(iThread, iPtr, &rpt->pad, sizeof(SReport), iThread);
sl@0
   141
			break;
sl@0
   142
			}
sl@0
   143
		default:
sl@0
   144
			break;
sl@0
   145
		}
sl@0
   146
	free_mem_block(m);
sl@0
   147
	Kern::RequestComplete(iThread, iStatus, r);
sl@0
   148
	iStatus = NULL;
sl@0
   149
	}
sl@0
   150
sl@0
   151
void TRxQ::CancelReq()
sl@0
   152
	{
sl@0
   153
	if (iStatus)
sl@0
   154
		Kern::RequestComplete(iThread, iStatus, KErrCancel);
sl@0
   155
	iStatus = NULL;
sl@0
   156
	}
sl@0
   157
sl@0
   158
void TRxQ::Close()
sl@0
   159
	{
sl@0
   160
	CancelReq();
sl@0
   161
	while (iFirst)
sl@0
   162
		{
sl@0
   163
		msghdr* m = iFirst;
sl@0
   164
		iFirst = m->next;
sl@0
   165
		free_mem_block(m);
sl@0
   166
		}
sl@0
   167
	iFirst = NULL;
sl@0
   168
	iLast = NULL;
sl@0
   169
	}
sl@0
   170
sl@0
   171
NONSHARABLE_CLASS(DRtosIfc) : public DLogicalChannel
sl@0
   172
	{
sl@0
   173
public:
sl@0
   174
	DRtosIfc();
sl@0
   175
	virtual ~DRtosIfc();
sl@0
   176
protected:
sl@0
   177
	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
sl@0
   178
	virtual void HandleMsg(TMessageBase* aMsg);
sl@0
   179
public:
sl@0
   180
	TInt DoControl(TInt aFunc, TAny* a1, TAny* a2);
sl@0
   181
	TInt DoRequest(TInt aFunc, TRequestStatus* aStatus, TAny* a1, TAny* a2);
sl@0
   182
	void DoCancel(TInt aMask);
sl@0
   183
	void HandleRtosMsg(msghdr* aMsg);
sl@0
   184
public:
sl@0
   185
	DThread* iThread;
sl@0
   186
	TRequestStatus* iDoneStatus;
sl@0
   187
	TRxQ iRxQ;
sl@0
   188
	TRxQ iRptQ;
sl@0
   189
	};
sl@0
   190
sl@0
   191
void RxMsg(TAny* aPtr)
sl@0
   192
	{
sl@0
   193
	msghdr* m = ThePMsgQ.Get();
sl@0
   194
	((DRtosIfc*)aPtr)->HandleRtosMsg(m);
sl@0
   195
	}
sl@0
   196
sl@0
   197
TInt EntryPoint()
sl@0
   198
	{
sl@0
   199
	TPMsgQ::ThePMsgQ = &::ThePMsgQ;
sl@0
   200
	init_personality();
sl@0
   201
sl@0
   202
	assert(current_task_id() == TASK_ID_UNKNOWN);
sl@0
   203
sl@0
   204
	kprintf("Entry point exit");
sl@0
   205
sl@0
   206
	return 0;
sl@0
   207
	}
sl@0
   208
sl@0
   209
void SendInitMsg()
sl@0
   210
	{
sl@0
   211
	kprintf("Send init msg");
sl@0
   212
	msghdr* m = (msghdr*)alloc_mem_block(sizeof(msghdr));
sl@0
   213
	m->msg_id = MSG_ID_INIT;
sl@0
   214
	int r = send_msg(OC_TASK, m);
sl@0
   215
	assert(r == OK);
sl@0
   216
	}
sl@0
   217
sl@0
   218
void SendFlushMsg()
sl@0
   219
	{
sl@0
   220
	msghdr* m = (msghdr*)alloc_mem_block(sizeof(msghdr));
sl@0
   221
	m->msg_id = MSG_ID_FLUSH;
sl@0
   222
	int r = send_msg(OC_TASK, m);
sl@0
   223
	assert(r == OK);
sl@0
   224
	}
sl@0
   225
sl@0
   226
void SendFinishMsg()
sl@0
   227
	{
sl@0
   228
	msghdr* m = (msghdr*)alloc_mem_block(sizeof(msghdr));
sl@0
   229
	m->msg_id = MSG_ID_DONE;
sl@0
   230
	int r = send_msg(OC_TASK, m);
sl@0
   231
	assert(r == OK);
sl@0
   232
	}
sl@0
   233
sl@0
   234
DRtosIfcFactory::DRtosIfcFactory()
sl@0
   235
	{
sl@0
   236
    iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
   237
    //iParseMask=0;//No units, no info, no PDD
sl@0
   238
    //iUnitsMask=0;//Only one thing
sl@0
   239
	}
sl@0
   240
sl@0
   241
TInt DRtosIfcFactory::Create(DLogicalChannelBase*& aChannel)
sl@0
   242
    {
sl@0
   243
	aChannel = new DRtosIfc;
sl@0
   244
    return aChannel ? KErrNone : KErrNoMemory;
sl@0
   245
    }
sl@0
   246
sl@0
   247
TInt DRtosIfcFactory::Install()
sl@0
   248
	{
sl@0
   249
	TInt r = SetName(&KRtosIfcLddName);
sl@0
   250
#ifndef __EPOC32__
sl@0
   251
	if (r == KErrNone)
sl@0
   252
		r = EntryPoint();
sl@0
   253
#endif
sl@0
   254
	return r;
sl@0
   255
	}
sl@0
   256
sl@0
   257
void DRtosIfcFactory::GetCaps(TDes8& aDes) const
sl@0
   258
    {
sl@0
   259
    TCapsRtosIfcV01 b;
sl@0
   260
    b.iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
sl@0
   261
    Kern::InfoCopy(aDes,(TUint8*)&b,sizeof(b));
sl@0
   262
    }
sl@0
   263
sl@0
   264
DRtosIfc::DRtosIfc()
sl@0
   265
	:	iRxQ(&Kern::CurrentThread()),
sl@0
   266
		iRptQ(&Kern::CurrentThread())
sl@0
   267
    {
sl@0
   268
	iThread=&Kern::CurrentThread();
sl@0
   269
	iThread->Open();
sl@0
   270
    }
sl@0
   271
sl@0
   272
DRtosIfc::~DRtosIfc()
sl@0
   273
	{
sl@0
   274
	iRxQ.Close();
sl@0
   275
	iRptQ.Close();
sl@0
   276
	Kern::SafeClose((DObject*&)iThread, NULL);
sl@0
   277
	}
sl@0
   278
sl@0
   279
TInt DRtosIfc::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
sl@0
   280
//
sl@0
   281
// Create channel
sl@0
   282
//
sl@0
   283
    {
sl@0
   284
sl@0
   285
    if (!Kern::QueryVersionSupported(TVersion(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber),aVer))
sl@0
   286
    	return KErrNotSupported;
sl@0
   287
	if (ThePMsgQ.iPtr)
sl@0
   288
		return KErrInUse;
sl@0
   289
	SetDfcQ(Kern::DfcQue0());
sl@0
   290
	iMsgQ.Receive();
sl@0
   291
	ThePMsgQ.iPtr = this;
sl@0
   292
	ThePMsgQ.Receive();
sl@0
   293
	return KErrNone;
sl@0
   294
	}
sl@0
   295
sl@0
   296
void DRtosIfc::HandleMsg(TMessageBase* aMsg)
sl@0
   297
	{
sl@0
   298
	TThreadMessage& m=*(TThreadMessage*)aMsg;
sl@0
   299
	TInt id=m.iValue;
sl@0
   300
	if (id==(TInt)ECloseMsg)
sl@0
   301
		{
sl@0
   302
		ThePMsgQ.CancelReceive();
sl@0
   303
		ThePMsgQ.iPtr = NULL;
sl@0
   304
		m.Complete(KErrNone,EFalse);
sl@0
   305
		iMsgQ.CompleteAll(KErrServerTerminated);
sl@0
   306
		return;
sl@0
   307
		}
sl@0
   308
	else if (id==KMaxTInt)
sl@0
   309
		{
sl@0
   310
		// DoCancel
sl@0
   311
		DoCancel(m.Int0());
sl@0
   312
		m.Complete(KErrNone,ETrue);
sl@0
   313
		return;
sl@0
   314
		}
sl@0
   315
sl@0
   316
	if (id<0)
sl@0
   317
		{
sl@0
   318
		// DoRequest
sl@0
   319
		TRequestStatus* pS=(TRequestStatus*)m.Ptr0();
sl@0
   320
		TInt r=DoRequest(~id,pS,m.Ptr1(),m.Ptr2());
sl@0
   321
		if (r!=KErrNone)
sl@0
   322
			Kern::RequestComplete(iThread,pS,r);
sl@0
   323
		m.Complete(KErrNone,ETrue);
sl@0
   324
		}
sl@0
   325
	else
sl@0
   326
		{
sl@0
   327
		// DoControl
sl@0
   328
		TInt r=DoControl(id,m.Ptr0(),m.Ptr1());
sl@0
   329
		m.Complete(r,ETrue);
sl@0
   330
		}
sl@0
   331
	}
sl@0
   332
sl@0
   333
TInt DRtosIfc::DoControl(TInt aFunc, TAny* a1, TAny* a2)
sl@0
   334
	{
sl@0
   335
	(void)a1;
sl@0
   336
	(void)a2;
sl@0
   337
	TInt r = KErrNone;
sl@0
   338
	switch (aFunc)
sl@0
   339
		{
sl@0
   340
		case RRtosIfc::EControlInit:
sl@0
   341
			SendInitMsg();
sl@0
   342
			break;
sl@0
   343
		case RRtosIfc::EControlFlush:
sl@0
   344
			SendFlushMsg();
sl@0
   345
			break;
sl@0
   346
		case RRtosIfc::EControlFinish:
sl@0
   347
			SendFinishMsg();
sl@0
   348
			break;
sl@0
   349
		case RRtosIfc::EControlSend:
sl@0
   350
			{
sl@0
   351
			data_msg* dm = (data_msg*)alloc_mem_block(512);
sl@0
   352
			TPtr8 lptr(dm->data, 0, 516-sizeof(data_msg));
sl@0
   353
			r = Kern::ThreadDesRead(iThread, a1, lptr, 0, 0);
sl@0
   354
			if (r == KErrNone)
sl@0
   355
				{
sl@0
   356
				dm->header.msg_id = MSG_ID_DATA;
sl@0
   357
				dm->length = lptr.Length();
sl@0
   358
				dm->checksum = 0;
sl@0
   359
				send_msg(OC_TASK, &dm->header);
sl@0
   360
				}
sl@0
   361
			else
sl@0
   362
				free_mem_block(dm);
sl@0
   363
			break;
sl@0
   364
			}
sl@0
   365
		default:
sl@0
   366
			r = KErrNotSupported;
sl@0
   367
			break;
sl@0
   368
		}
sl@0
   369
	return r;
sl@0
   370
	}
sl@0
   371
sl@0
   372
TInt DRtosIfc::DoRequest(TInt aFunc, TRequestStatus* aStatus, TAny* a1, TAny* a2)
sl@0
   373
	{
sl@0
   374
	(void)a1;
sl@0
   375
	(void)a2;
sl@0
   376
	switch (aFunc)
sl@0
   377
		{
sl@0
   378
		case RRtosIfc::ERequestWaitInitialTests:
sl@0
   379
			iDoneStatus = aStatus;
sl@0
   380
			return KErrNone;
sl@0
   381
		case RRtosIfc::ERequestReceive:
sl@0
   382
			return iRxQ.QueueReq(aStatus, a1);
sl@0
   383
		case RRtosIfc::ERequestReport:
sl@0
   384
			return iRptQ.QueueReq(aStatus, a1);
sl@0
   385
		default:
sl@0
   386
			return KErrNotSupported;
sl@0
   387
		}
sl@0
   388
	}
sl@0
   389
sl@0
   390
void DRtosIfc::DoCancel(TInt aMask)
sl@0
   391
	{
sl@0
   392
	if (aMask & RRtosIfc::ECancelWaitInitialTests)
sl@0
   393
		{
sl@0
   394
		Kern::RequestComplete(iThread, iDoneStatus, KErrCancel), iDoneStatus=NULL;
sl@0
   395
		}
sl@0
   396
	if (aMask & RRtosIfc::ECancelReceive)
sl@0
   397
		iRxQ.CancelReq();
sl@0
   398
	if (aMask & RRtosIfc::ECancelReport)
sl@0
   399
		iRptQ.CancelReq();
sl@0
   400
	}
sl@0
   401
sl@0
   402
void DRtosIfc::HandleRtosMsg(msghdr* aM)
sl@0
   403
	{
sl@0
   404
	switch (aM->msg_id)
sl@0
   405
		{
sl@0
   406
		case MSG_ID_DONE:
sl@0
   407
			if (iDoneStatus)
sl@0
   408
				Kern::RequestComplete(iThread, iDoneStatus, KErrNone), iDoneStatus=NULL;
sl@0
   409
			break;
sl@0
   410
		case MSG_ID_DATA:
sl@0
   411
			iRxQ.AddMsg(aM);
sl@0
   412
			aM = NULL;
sl@0
   413
			break;
sl@0
   414
		case MSG_ID_SEM_RPT:
sl@0
   415
		case MSG_ID_RCV_RPT:
sl@0
   416
		case MSG_ID_TM_RPT:
sl@0
   417
			iRptQ.AddMsg(aM);
sl@0
   418
			aM = NULL;
sl@0
   419
			break;
sl@0
   420
		default:
sl@0
   421
			break;
sl@0
   422
		}
sl@0
   423
	if (aM)
sl@0
   424
		free_mem_block(aM);
sl@0
   425
	ThePMsgQ.Receive();
sl@0
   426
	}
sl@0
   427
sl@0
   428
#ifdef __EPOC32__
sl@0
   429
DECLARE_STANDARD_EXTENSION()
sl@0
   430
	{
sl@0
   431
	return EntryPoint();
sl@0
   432
	}
sl@0
   433
sl@0
   434
DECLARE_EXTENSION_LDD()
sl@0
   435
	{
sl@0
   436
	return new DRtosIfcFactory;
sl@0
   437
	}
sl@0
   438
#else
sl@0
   439
DECLARE_STANDARD_LDD()
sl@0
   440
	{
sl@0
   441
	return new DRtosIfcFactory;
sl@0
   442
	}
sl@0
   443
#endif
sl@0
   444