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