os/ossrv/genericopenlibs/openenvcore/backend/inc/tsignalmessage.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-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 "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 // Signal data structure that supports marshalling
    15 // 
    16 //
    17 
    18 #include <e32base.h>
    19 
    20 #ifndef TSIGNALMESSAGE_H
    21 #define TSIGNALMESSAGE_H
    22 
    23 const TInt KSigMsgLength = 32;
    24 const TUint8 KSigMsgVersion = 1;
    25 
    26 /*
    27 @internalComponent
    28 */
    29 class TSignalMessage
    30 	{
    31 public:
    32 	TUint8	mType;
    33 	union
    34 		{
    35 		struct
    36 			{
    37 			TUint8	mSignal;
    38 			TUint32	mValue;
    39 			} mSigVal;
    40 		struct
    41 			{
    42 			TUint8 	mSignal;	
    43 			TInt32	mTimerId;
    44 			} mRtSignal;
    45 		TUint8 	mSignal;		
    46 		TUint32 mTimeOut;
    47 		TUint32 mData;
    48 		TUint64	mPid;
    49 		};
    50 
    51 	enum {
    52 		ESignal = 0,
    53 		ESignalValuePair,
    54 		EAlarmRegistration,
    55 		EDequeueSignal,
    56 		EWaitOnChild,
    57 		ERtTimerSignal,
    58 	} TSignalMessageType;
    59 
    60 	virtual ~TSignalMessage() {}
    61 
    62 	static TBool SignalMatchBySigNum(const TSignalMessage& aMsg1, const TSignalMessage& aMsg2)
    63 		{
    64 		if(aMsg1.mType != ESignal || aMsg2.mType != ESignal)
    65 			return EFalse;
    66 
    67 		if (aMsg1.mSignal == aMsg2.mSignal)
    68 			{
    69 			return ETrue;
    70 			}
    71 		return EFalse;
    72 		}
    73 
    74 	static TBool SigValMatchBySigNum(const TSignalMessage& aMsg1, const TSignalMessage& aMsg2)
    75 		{
    76 		if(aMsg1.mType != ESignalValuePair || aMsg2.mType != ESignalValuePair)
    77 			return EFalse;
    78 
    79 		if (aMsg1.mSigVal.mSignal == aMsg2.mSigVal.mSignal)
    80 			{
    81 			return ETrue;
    82 			}
    83 		return EFalse;
    84 		}
    85 
    86 	inline TInt Marshall(TDes8& aBuffer)
    87 		{
    88 		TUint8	lBufPtr[KSigMsgLength] = {0};
    89 
    90 		if(aBuffer.MaxSize() != KSigMsgLength)
    91 			return KErrArgument;
    92 
    93 		lBufPtr[0] = KSigMsgVersion;
    94 		lBufPtr[1] = mType;
    95 
    96 		switch(mType)
    97 			{
    98 			case ESignal:
    99 				lBufPtr[2] = mSignal;
   100 				break;
   101 			case ESignalValuePair:
   102 				lBufPtr[2] = mSigVal.mSignal;
   103 
   104 				lBufPtr[3] = (TUint8)(mSigVal.mValue & 0x000000FF);
   105 				lBufPtr[4] = (TUint8)((mSigVal.mValue & 0x0000FF00) >> 8);
   106 				lBufPtr[5] = (TUint8)((mSigVal.mValue & 0x00FF0000) >> 16);
   107 				lBufPtr[6] = (TUint8)((mSigVal.mValue & 0xFF000000) >> 24);
   108 				break;
   109 			case EAlarmRegistration:
   110 				lBufPtr[2] = (TUint8)(mTimeOut & 0x000000FF);
   111 				lBufPtr[3] = (TUint8)((mTimeOut & 0x0000FF00) >> 8);
   112 				lBufPtr[4] = (TUint8)((mTimeOut & 0x00FF0000) >> 16);
   113 				lBufPtr[5] = (TUint8)((mTimeOut & 0xFF000000) >> 24);
   114 				break;
   115 			case EDequeueSignal:
   116 				lBufPtr[2] = mSignal;
   117 				break;
   118 			case EWaitOnChild:
   119 				memcpy(&lBufPtr[2],&mPid,sizeof(mPid));
   120 				break;
   121 			case ERtTimerSignal:
   122 				Mem::Copy(&lBufPtr[2], &mRtSignal.mSignal, sizeof(mRtSignal.mSignal));
   123 				Mem::Copy(&lBufPtr[2+sizeof(mRtSignal.mSignal)], &mRtSignal.mTimerId, sizeof(mRtSignal.mTimerId));
   124 				break;
   125 			default:
   126 				// do nothing
   127 				return KErrGeneral;
   128 				break;
   129 			}
   130 		aBuffer.Zero();
   131 		aBuffer.Append(lBufPtr,KSigMsgLength);
   132 		return KErrNone;
   133 		}
   134 
   135 	inline TInt Unmarshall(TDes8& aBuffer)
   136 		{
   137 		const TUint8* lBufPtr = aBuffer.Ptr();
   138 
   139 		if(aBuffer.MaxSize() != KSigMsgLength)
   140 			return KErrArgument;
   141 
   142 		if(lBufPtr[0] != KSigMsgVersion)
   143 			return KErrGeneral;
   144 
   145 		mType = lBufPtr[1];
   146 
   147 		switch(mType)
   148 			{
   149 			case ESignal:
   150 				mSignal = lBufPtr[2];
   151 				break;
   152 			case ESignalValuePair:
   153 				mSigVal.mSignal = lBufPtr[2];
   154 				mSigVal.mValue = 0;
   155 				mSigVal.mValue |= (TUint32)lBufPtr[3];
   156 				mSigVal.mValue |= (((TUint32)lBufPtr[4]) << 8);
   157 				mSigVal.mValue |= (((TUint32)lBufPtr[5]) << 16);
   158 				mSigVal.mValue |= (((TUint32)lBufPtr[6]) << 24);
   159 				break;
   160 			case EAlarmRegistration:
   161 				mTimeOut = 0;
   162 				mTimeOut |= (TUint32)lBufPtr[2];
   163 				mTimeOut |= (((TUint32)lBufPtr[3]) << 8);
   164 				mTimeOut |= (((TUint32)lBufPtr[4]) << 16);
   165 				mTimeOut |= (((TUint32)lBufPtr[5]) << 24);
   166 				break;
   167 			case EDequeueSignal:
   168 				mSignal = lBufPtr[2];
   169 				break;
   170 			case EWaitOnChild:
   171 				mPid = 0;
   172 				memcpy(&mPid,&lBufPtr[2],sizeof(mPid));
   173 				break;
   174 			case ERtTimerSignal:
   175 				Mem::Copy(&mRtSignal.mSignal, &lBufPtr[2], sizeof(mRtSignal.mSignal));
   176 				Mem::Copy(&mRtSignal.mTimerId,&lBufPtr[2+sizeof(mRtSignal.mSignal)], sizeof(mRtSignal.mTimerId));
   177 				break;
   178 			default:
   179 				// do nothing
   180 				return KErrGeneral;
   181 				break;
   182 			}
   183 
   184 		return KErrNone;
   185 		}
   186 	};
   187 
   188 #endif // TSIGNALMESSAGE_H