genericopenlibs/openenvcore/libc/test/testmsgqueue/src/tmsgqueueblocks.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "tmsgqueue.h"
       
    22 #include <string.h>
       
    23 #include <unistd.h>
       
    24 #define MESSAGE_Q_KEY 1000
       
    25 #define MESSAGE_Q_KEY1 1001
       
    26 #define MESSAGE_Q_KEY_TH 1002
       
    27 #define WITH_ERROR 1
       
    28 #define WITHOUT_ERROR 0
       
    29 #define MSGSZ 128
       
    30 #define IPC_KEY 1000
       
    31 #define IPC_CREAT_EXCL 3000
       
    32 #define IPC_NO_CREAT_EXCL 2000
       
    33 #define USE_DEFAULT_ID 1
       
    34 
       
    35 static void* ThreadEntryRead(TAny* arg);
       
    36 
       
    37 TInt MsgQueueGet();
       
    38 TInt MsgQueueThread(int a);
       
    39 TInt MsgQueueSend(int a);
       
    40 TInt MsgQueueRecieve();
       
    41 TInt MsgQueueDelete();
       
    42         
       
    43 #define RETURN_ON_ERROR(err) if ( err ) \
       
    44             { \
       
    45             ERR_PRINTF2 ( KFunc, err); \
       
    46             return err; \
       
    47             }
       
    48             
       
    49 typedef struct 
       
    50 		{
       
    51         long    mtype;
       
    52         char    mtext[4097];
       
    53         } message_buf;
       
    54          
       
    55 TInt CTestMsgqueue::MsgKey(  TInt& aKey )
       
    56     {
       
    57     _LIT(KFunc, "MsgKey");
       
    58     INFO_PRINTF1 ( KFunc);
       
    59     _LIT( KaKey, "Param%d" );
       
    60     TBuf<8> pNameBuf;
       
    61     pNameBuf.Format(KaKey,++iParamCnt);
       
    62 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aKey);
       
    63 	if(!res)
       
    64 		{
       
    65 	 	_LIT(Kerr , "Unable to retrieve aKey") ;
       
    66 	 	INFO_PRINTF1(Kerr) ;
       
    67 	 	return KErrGeneral;
       
    68 		}
       
    69     return KErrNone;
       
    70     }
       
    71     
       
    72 TInt CTestMsgqueue::MsgFlag(  TInt& aFlag )
       
    73     {
       
    74     _LIT(KFunc, "MsgFlag");
       
    75     INFO_PRINTF1 ( KFunc);
       
    76     _LIT( KaFlag, "Param%d" );
       
    77 	TBuf<16> pNameBuf;
       
    78     pNameBuf.Format(KaFlag,++iParamCnt);
       
    79 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aFlag);
       
    80 	 
       
    81 	if(!res)
       
    82 		{
       
    83 	 	_LIT(Kerr , "Unable to retrieve message flag") ;
       
    84 	 	INFO_PRINTF1(Kerr) ;
       
    85 	 	return KErrGeneral;
       
    86 		}
       
    87     return KErrNone;
       
    88     }
       
    89 TInt CTestMsgqueue::MsgId(  TInt& aId )
       
    90     {
       
    91     _LIT(KFunc, "MsgId");
       
    92     INFO_PRINTF1 ( KFunc);
       
    93     _LIT( KaId, "Param%d" );
       
    94 	TBuf<16> pNameBuf;
       
    95     pNameBuf.Format(KaId,++iParamCnt);
       
    96 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aId);
       
    97 	if(!res)
       
    98 		{
       
    99 	 	_LIT(Kerr , "Unable to retrieve message Id") ;
       
   100 	 	INFO_PRINTF1(Kerr) ;
       
   101 		}
       
   102     if ( aId == USE_DEFAULT_ID )
       
   103         {
       
   104         aId = iNoOfMsqIds[0];
       
   105         }    
       
   106     return KErrNone;
       
   107     }
       
   108 
       
   109 TInt CTestMsgqueue::MsgCmd(  TInt& aCmd )
       
   110     {
       
   111     _LIT(KFunc, "MsgCmd");
       
   112     INFO_PRINTF1( KFunc);
       
   113     _LIT( KaCmd, "Param%d" );
       
   114 	TBuf<16> pNameBuf;
       
   115 	pNameBuf.Format(KaCmd,++iParamCnt);
       
   116 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aCmd);
       
   117 	
       
   118 	if(!res)
       
   119 		{
       
   120 	 	_LIT(Kerr , "Unable to retrieve command") ;
       
   121 	 	INFO_PRINTF1(Kerr) ;
       
   122 		}
       
   123     return KErrNone;
       
   124     } 
       
   125 TInt CTestMsgqueue::MsgType(  TInt& aType )
       
   126     {
       
   127     _LIT(KFunc, "MsgType");
       
   128     INFO_PRINTF1( KFunc);
       
   129     _LIT( KaType, "Param%d" );
       
   130 	TBuf<16> pNameBuf;
       
   131     pNameBuf.Format(KaType,++iParamCnt);
       
   132 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aType);
       
   133 	 
       
   134 	if(!res)
       
   135 		{
       
   136 	 	_LIT(Kerr , "Unable to retrieve message type") ;
       
   137 	 	INFO_PRINTF1(Kerr) ;
       
   138 		return KErrGeneral;
       
   139 		}
       
   140 	 
       
   141     return KErrNone;
       
   142     }    
       
   143 TInt CTestMsgqueue::MsgLen(  TInt& aLen )
       
   144     {
       
   145     _LIT(KFunc, "MsgLen");
       
   146     INFO_PRINTF1( KFunc);
       
   147     _LIT( KaLen, "Param%d" );
       
   148 	TBuf<16> pNameBuf;
       
   149     pNameBuf.Format(KaLen,++iParamCnt);
       
   150 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aLen);
       
   151 	 
       
   152 	if(!res)
       
   153 		{
       
   154 	 	_LIT(Kerr , "Unable to retrieve message length") ;
       
   155 	 	INFO_PRINTF1(Kerr) ;
       
   156 		return KErrGeneral;
       
   157 		}
       
   158     return KErrNone;
       
   159     }
       
   160 TInt CTestMsgqueue::MsgSegs(  TInt& aSegs )
       
   161     {
       
   162     _LIT(KFunc, "MsgSegs");
       
   163     INFO_PRINTF1( KFunc);
       
   164     _LIT( KaSegs, "Param%d" );
       
   165 	TBuf<16> pNameBuf;
       
   166     pNameBuf.Format(KaSegs,++iParamCnt);
       
   167 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aSegs);
       
   168 	 
       
   169 	if(!res)
       
   170 		{
       
   171 	 	_LIT(Kerr , "Unable to retrieve message flag") ;
       
   172 	 	INFO_PRINTF1(Kerr) ;
       
   173 	 	return KErrGeneral;
       
   174 		}
       
   175     return KErrNone;
       
   176     }       
       
   177 TInt CTestMsgqueue::ErrCode(  TInt& aErrCode )
       
   178     {
       
   179     _LIT(KFunc, "ErrCode");
       
   180     INFO_PRINTF1( KFunc);
       
   181     _LIT( KaErrCode, "Param%d" );
       
   182 	TBuf<16> pNameBuf;
       
   183     pNameBuf.Format(KaErrCode,++iParamCnt);
       
   184 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, aErrCode);
       
   185 	 
       
   186 	if(!res)
       
   187 		{
       
   188 	 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   189 	 	INFO_PRINTF1(Kerr) ;
       
   190 		return KErrGeneral;
       
   191 		}
       
   192     return KErrNone;
       
   193     }
       
   194     
       
   195 TInt CTestMsgqueue::CheckMessageQueueStats(struct msqid_ds& buf)
       
   196 	{
       
   197     _LIT(KFunc, "CheckMessageQueueStats");
       
   198     INFO_PRINTF1( KFunc);
       
   199     
       
   200     if ( buf.msg_cbytes != 0 )
       
   201         {
       
   202         return KErrGeneral;
       
   203         }
       
   204     if ( buf.msg_qnum != 0 )
       
   205         {
       
   206         return KErrGeneral;
       
   207         }
       
   208     if ( buf.msg_perm.key != IPC_KEY )
       
   209         {
       
   210         return KErrGeneral;
       
   211         }
       
   212     if ( buf.msg_qbytes != 2097152 )
       
   213         {
       
   214         return KErrGeneral;
       
   215         }   
       
   216     return KErrNone;
       
   217 }
       
   218 
       
   219 void CTestMsgqueue::SetMessageQueueParms(struct msqid_ds* /*buf*/)
       
   220 	{
       
   221 	}    
       
   222 
       
   223     
       
   224 // -----------------------------------------------------------------------------
       
   225 // CTestMsgqueue::MessageQueueCreate
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 TInt CTestMsgqueue::MessageQueueCreate(  )
       
   229     {
       
   230     _LIT(KFunc, "MessageQueueCreate");
       
   231     INFO_PRINTF1( KFunc);
       
   232     
       
   233     TInt key;
       
   234     TInt msgFlag;
       
   235     TInt msgSegs;
       
   236     TInt terror;
       
   237     TInt err;
       
   238     TInt i = 0;
       
   239     TInt msgid = -1;
       
   240     TInt retVal = KErrGeneral;
       
   241     
       
   242     // retrieve passed parameters
       
   243     err = MsgKey( key);
       
   244     RETURN_ON_ERROR(err);
       
   245 
       
   246     err = MsgFlag( msgFlag);
       
   247     RETURN_ON_ERROR(err);
       
   248 
       
   249     err = MsgSegs( msgSegs);
       
   250     RETURN_ON_ERROR(err);
       
   251     
       
   252     err = ErrCode( terror);
       
   253     RETURN_ON_ERROR(err);
       
   254     int startIndex = 0;
       
   255     TInt index = 0;
       
   256     for(index = 0; index < MAX_IPC_OBJ; index++)
       
   257         {
       
   258         if(iNoOfMsqIds[i] == -1)
       
   259             {
       
   260             startIndex = index;
       
   261             break;
       
   262             }
       
   263         }
       
   264     errno=0;
       
   265     for (i=0; i < msgSegs; i++)
       
   266         {
       
   267         msgid = msgget(key, msgFlag);
       
   268         if (msgid == -1)
       
   269 			{
       
   270             break;        
       
   271 			}
       
   272         iNoOfMsqIds[startIndex + i] = msgid;
       
   273         }    
       
   274     
       
   275     
       
   276     if (msgid == -1)
       
   277         {
       
   278         if(terror == WITHOUT_ERROR)
       
   279             {
       
   280             retVal = errno;
       
   281             }
       
   282         else
       
   283             {
       
   284             TInt errCode = 0;
       
   285             _LIT( KerrCode, "Param%d" );
       
   286 			TBuf<8> pNameBuf;
       
   287 		    pNameBuf.Format(KerrCode,++iParamCnt);
       
   288 			TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   289 			 
       
   290 			if(!res)
       
   291 				{
       
   292 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   293 			 	INFO_PRINTF1(Kerr) ;
       
   294 			 	return KErrGeneral ;
       
   295 				}
       
   296 		          
       
   297             if ( errCode == errno)
       
   298                 {
       
   299                 retVal = KErrNone;
       
   300                 }
       
   301 			}
       
   302         }
       
   303     else
       
   304         {
       
   305         if(terror != WITH_ERROR)
       
   306             {
       
   307             retVal = errno;
       
   308             }
       
   309         }
       
   310     	errno=0;    
       
   311     if(retVal != KErrNone && i < msgSegs && msgSegs > 1)
       
   312         {
       
   313         // do the cleanup
       
   314         for(index = startIndex; index < MAX_IPC_OBJ; index++)
       
   315             {
       
   316             if(iNoOfMsqIds[index] > 0)
       
   317 				{
       
   318 			
       
   319                 msgctl(iNoOfMsqIds[index], IPC_RMID, 0);
       
   320 				}
       
   321             }
       
   322         }        
       
   323         
       
   324 	return retVal;
       
   325     }
       
   326     
       
   327 // -----------------------------------------------------------------------------
       
   328 // CTestMsgqueue::MessageQueueClose
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 TInt CTestMsgqueue::MessageQueueClose(  )
       
   332     {
       
   333     _LIT(KFunc, "MessageQueueClose");
       
   334     INFO_PRINTF1( KFunc);
       
   335 
       
   336     TInt retVal = KErrGeneral;
       
   337     TInt terror;
       
   338     TInt err;
       
   339     
       
   340     err = ErrCode( terror);
       
   341     RETURN_ON_ERROR(err);
       
   342     errno=0;
       
   343     for(TInt index = 0; index < MAX_IPC_OBJ; index++)
       
   344         {
       
   345         if(iNoOfMsqIds[index] >= 0)
       
   346 			{
       
   347             err = msgctl(iNoOfMsqIds[index], IPC_RMID, 0);
       
   348 			}
       
   349         }
       
   350     
       
   351     if (err == -1)
       
   352         {
       
   353         if(terror == WITHOUT_ERROR)
       
   354             {
       
   355             retVal = errno;
       
   356             }
       
   357         }
       
   358     else
       
   359         {
       
   360         errno = 0;
       
   361         if(terror != WITH_ERROR)
       
   362             {
       
   363             retVal = errno;
       
   364             }
       
   365         }
       
   366         
       
   367 	return retVal;
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CTestMsgqueue::MessageQueueControl
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 TInt CTestMsgqueue::MessageQueueControl(  )
       
   375     {
       
   376     _LIT(KFunc, "MessageQueueControl");
       
   377     INFO_PRINTF1( KFunc);
       
   378     
       
   379     TInt msqId;
       
   380     TInt msqCmd;
       
   381     TInt terror;
       
   382     TInt err;
       
   383     struct msqid_ds buf = {0};
       
   384     TInt retVal = KErrGeneral;
       
   385     
       
   386     // retrieve passed parameters
       
   387     err = MsgId( msqId);
       
   388     RETURN_ON_ERROR(err);
       
   389  
       
   390     err = MsgCmd( msqCmd);
       
   391     RETURN_ON_ERROR(err);
       
   392     
       
   393     if ( msqCmd == IPC_SET )
       
   394         {
       
   395         SetMessageQueueParms(&buf);
       
   396         }
       
   397     
       
   398     err = ErrCode( terror);
       
   399     RETURN_ON_ERROR(err);
       
   400     
       
   401     err = msgctl(msqId, msqCmd, &buf);
       
   402     
       
   403     if ( err == -1 )
       
   404         {
       
   405         if(terror == WITHOUT_ERROR)
       
   406             {
       
   407             retVal = errno;
       
   408             }
       
   409         else
       
   410             {
       
   411             TInt errCode = 0;
       
   412             _LIT( KerrCode, "Param%d" );
       
   413 			 TBuf<8> pNameBuf;
       
   414 		     pNameBuf.Format(KerrCode,++iParamCnt);
       
   415 			 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   416 			 
       
   417 			 if(!res)
       
   418 				{
       
   419 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   420 			 	INFO_PRINTF1(Kerr) ;
       
   421 			 	return KErrGeneral ;
       
   422 				}
       
   423             if ( errCode == errno)
       
   424                 {
       
   425                 retVal = KErrNone;
       
   426                 }
       
   427             }    
       
   428         }
       
   429     else
       
   430         {
       
   431         if ( msqCmd == IPC_STAT )
       
   432             {
       
   433             retVal = CheckMessageQueueStats(buf);
       
   434             }
       
   435         else if ( terror != WITH_ERROR )
       
   436             {
       
   437             retVal = KErrNone;
       
   438             }    
       
   439         }
       
   440     return retVal;
       
   441     }
       
   442 
       
   443 void MessageCreate(TInt msgLen, TInt msgType, message_buf* buf  )
       
   444 	{
       
   445     TInt i;
       
   446     
       
   447     buf->mtype = msgType;
       
   448     if (msgLen > 0 && msgLen <= 4096)
       
   449         {
       
   450         for (i=0; i< msgLen; i++)
       
   451             {
       
   452             buf->mtext[i] = 'a';
       
   453             }
       
   454         buf->mtext[i] = '\0';    
       
   455         }
       
   456     else 
       
   457         {
       
   458          buf->mtext[0] = '\0';
       
   459         }
       
   460 	}
       
   461 // -----------------------------------------------------------------------------
       
   462 // CTestMsgqueue::CreateThread
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 TInt CTestMsgqueue::CreateThread(  )
       
   466 	{
       
   467     _LIT(KFunc, "CreateThread");
       
   468     INFO_PRINTF1( KFunc);
       
   469     TInt threadType = -1;
       
   470     message_buf buf = {0};
       
   471     _LIT( KthreadType, "Param%d" );
       
   472 	TBuf<16> pNameBuf;
       
   473 	TInt err = KErrNone;
       
   474 	pNameBuf.Format(KthreadType,++iParamCnt);
       
   475 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, threadType);
       
   476 	
       
   477 	if(!res)
       
   478 		{
       
   479 	 	_LIT(Kerr , "Unable to retrieve eror code") ;
       
   480 	 	INFO_PRINTF1(Kerr) ;
       
   481 	 	return KErrGeneral ;
       
   482 		}
       
   483     switch(threadType)
       
   484         {
       
   485         case 1: 
       
   486             iBlocked = 0;
       
   487             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionRead,(void*)this);
       
   488             if (!err)
       
   489                 {
       
   490                 err = SendMessage();
       
   491                 }
       
   492             break;
       
   493         case 2:
       
   494             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionDestroyQ,(void*)this);
       
   495             if (!err)
       
   496                 {
       
   497                 err = RecvMessage();
       
   498                 }
       
   499             break;
       
   500         case 3:
       
   501             iBlocked = 0;
       
   502             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionWrite,(void*)this);
       
   503             while(!iBlocked)
       
   504                 {
       
   505                 User::After(100000);
       
   506                 }
       
   507             // fake msgctl...
       
   508             iBlocked = 0;
       
   509             msgctl(iNoOfMsqIds[0], IPC_STAT, (struct msqid_ds *)&buf);
       
   510             err = MessageQueueRecv();
       
   511             /*while(!iBlocked)
       
   512                 {
       
   513                 User::After(100000);
       
   514                 }*/
       
   515             err = (iErrno) ?KErrGeneral : KErrNone;
       
   516             break;
       
   517         case 4:
       
   518             iBlocked = 0;
       
   519             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionWrite,(void*)this);
       
   520             while(!iBlocked)
       
   521                 {
       
   522                 User::After(100000);
       
   523                 }
       
   524             // fake msgctl...
       
   525             iBlocked = 0; // again we want to block
       
   526             msgctl(iNoOfMsqIds[0], IPC_STAT, (struct msqid_ds *)&buf);
       
   527             err = msgctl(iNoOfMsqIds[0], IPC_RMID, 0);/*
       
   528             while(!iBlocked)
       
   529                 {
       
   530                 User::After(100000);
       
   531                 }*/
       
   532             if (err == KErrNone) 
       
   533                 {
       
   534                 err = (iErrno == EIDRM) ?KErrNone : KErrGeneral;
       
   535                 }
       
   536             break;            
       
   537         default:
       
   538             err = KErrNone;
       
   539             break;            
       
   540         }
       
   541     if ( err )
       
   542         {
       
   543         ERR_PRINTF2 ( _L("pthread_create error:%d"), err);
       
   544         }
       
   545     return err;
       
   546 	}
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CTestMsgqueue::SendMessage
       
   550 // -----------------------------------------------------------------------------
       
   551 //
       
   552 TInt CTestMsgqueue::SendMessage()
       
   553 	{
       
   554     _LIT(KFunc, "SendMessage");
       
   555     INFO_PRINTF1( KFunc);
       
   556     
       
   557     TInt msqId;
       
   558     TInt msgType;
       
   559     TInt msqFlag;
       
   560     TInt terror;
       
   561     TInt err;
       
   562     message_buf buf = {0};
       
   563     TInt msgLen;
       
   564     TInt retVal = KErrGeneral;
       
   565     
       
   566     // retrieve passed parameters
       
   567     err = MsgId( msqId);
       
   568     RETURN_ON_ERROR(err);
       
   569   
       
   570     err = MsgLen( msgLen);
       
   571     RETURN_ON_ERROR(err);
       
   572      
       
   573     err = MsgType( msgType);
       
   574     RETURN_ON_ERROR(err);
       
   575     
       
   576     err = MsgFlag( msqFlag);
       
   577     RETURN_ON_ERROR(err);
       
   578     
       
   579     err = ErrCode( terror);
       
   580     RETURN_ON_ERROR(err);
       
   581     
       
   582     MessageCreate(msgLen, msgType, &buf);
       
   583     
       
   584     while (iBlocked == 0)
       
   585         {
       
   586         User::After(100000);
       
   587         }
       
   588     err = msgsnd(msqId, &buf, msgLen, msqFlag);
       
   589     
       
   590     if ( err == -1 )
       
   591         {
       
   592         if(terror == WITHOUT_ERROR)
       
   593             {
       
   594             retVal = errno;
       
   595             }
       
   596         else
       
   597             {
       
   598             TInt errCode = 0;
       
   599             _LIT( KerrCode, "Param%d" );
       
   600 			 TBuf<8> pNameBuf;
       
   601 		     pNameBuf.Format(KerrCode,++iParamCnt);
       
   602 			 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   603 			 
       
   604 			 if(!res)
       
   605 				{
       
   606 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   607 			 	INFO_PRINTF1(Kerr) ;
       
   608 			 	return KErrGeneral ;
       
   609 				}
       
   610             if ( errCode == errno)
       
   611                 {
       
   612                 retVal = KErrNone;
       
   613                 }
       
   614             }    
       
   615         }
       
   616     else
       
   617         {
       
   618         if ( terror != WITH_ERROR)
       
   619             {
       
   620             TInt tmout = 0;
       
   621             while (iBlocked && (tmout < 5))
       
   622                 {
       
   623                 User::After(10000);
       
   624                 tmout++;
       
   625                 }
       
   626             retVal = (iBlocked == 0) ?KErrNone: KErrGeneral;    
       
   627             }       
       
   628         }
       
   629 
       
   630     return retVal;
       
   631 	}
       
   632 // -----------------------------------------------------------------------------
       
   633 // CTestMsgqueue::RecvMessage
       
   634 // -----------------------------------------------------------------------------
       
   635 //
       
   636 TInt CTestMsgqueue::RecvMessage()
       
   637 	{
       
   638     _LIT(KFunc, "RecvMessage");
       
   639     INFO_PRINTF1( KFunc);
       
   640     
       
   641     TInt msgType;
       
   642     TInt msqFlag = 0;
       
   643     TInt err;
       
   644     TInt terror;
       
   645     message_buf buf = {0};
       
   646     TInt msgLen = 128;
       
   647     TInt retVal = KErrGeneral;
       
   648     
       
   649     err = ErrCode( terror);
       
   650     RETURN_ON_ERROR(err);
       
   651     
       
   652     msgType = 0; //any message
       
   653     msqFlag =  ~IPC_NOWAIT; //Blocking
       
   654     
       
   655     iBlocked = 1;
       
   656     
       
   657     
       
   658     char array[128+1];
       
   659     for(int idx=0; idx<128; idx++)
       
   660 	    {
       
   661     	array[idx]='a';    	
       
   662     	}
       
   663     array[128]='\0';	
       
   664 
       
   665 	    
       
   666     err = msgrcv(iNoOfMsqIds[0], &buf, msgLen, msgType, msqFlag);    
       
   667     while (!iBlocked)
       
   668         {
       
   669         User::After(10000);
       
   670         }
       
   671 
       
   672     if ( err == -1 )
       
   673         {
       
   674         if(terror == WITHOUT_ERROR)
       
   675             {
       
   676             retVal = errno;
       
   677             }
       
   678         else
       
   679             {
       
   680             TInt errCode = 0;
       
   681             _LIT( KerrCode, "Param%d" );
       
   682 			 TBuf<8> pNameBuf;
       
   683 		     pNameBuf.Format(KerrCode,++iParamCnt);
       
   684 			 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   685 			 
       
   686 			 if(!res)
       
   687 				{
       
   688 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   689 			 	INFO_PRINTF1(Kerr) ;
       
   690 			 	return KErrGeneral ;
       
   691 				}
       
   692             if ( errCode == errno)
       
   693                 {
       
   694                 retVal = KErrNone;
       
   695                 }
       
   696             }    
       
   697         }
       
   698     else
       
   699         {
       
   700         if ( terror != WITH_ERROR && (msgLen>0 && msgLen<256) && !(strncmp(&buf.mtext[0], &array[0], msgLen)))
       
   701 		    {            
       
   702             retVal = KErrNone;    
       
   703             }       
       
   704         }
       
   705 
       
   706     return retVal;
       
   707 	}
       
   708 //-----------------------------------------------------------------------------
       
   709 //CTestMsgqueue::ThreadEntryFunctionRead
       
   710 //-----------------------------------------------------------------------------
       
   711      
       
   712 void* CTestMsgqueue::ThreadEntryFunctionRead(void* arg)
       
   713 	{    
       
   714     TInt msgType;
       
   715     TInt msqFlag = 0;
       
   716     TInt err;
       
   717     message_buf buf = {0};
       
   718     TInt msgLen = 128;
       
   719     CTestMsgqueue *self = static_cast<CTestMsgqueue*> (arg);
       
   720     
       
   721     TInt msqId = msgget(IPC_KEY, 0);
       
   722     if ( msqId < 0)
       
   723         {
       
   724         return NULL;
       
   725         }
       
   726     msgType = 0; //any message
       
   727     msqFlag =  ~IPC_NOWAIT; //Blocking
       
   728     
       
   729     self->iBlocked = 1;    
       
   730     
       
   731     err = msgrcv(msqId, &buf, msgLen, msgType, msqFlag);
       
   732     
       
   733     if ((err > 0) && (self->iBlocked))
       
   734         {
       
   735         self->iBlocked = 0;
       
   736         }    
       
   737     return NULL;
       
   738 	}
       
   739 //-----------------------------------------------------------------------------
       
   740 //CTestMsgqueue::ThreadEntryFunctionWrite
       
   741 //-----------------------------------------------------------------------------
       
   742      
       
   743 void* CTestMsgqueue::ThreadEntryFunctionDestroyQ(void* arg)
       
   744 	{    
       
   745     message_buf buf = {0};
       
   746     CTestMsgqueue *self = static_cast<CTestMsgqueue*> (arg);
       
   747     
       
   748     while (!self->iBlocked)
       
   749         {
       
   750         User::After(10000);
       
   751         }
       
   752     self->iBlocked = 0;
       
   753     TInt msqId = msgget(IPC_KEY, 0);
       
   754     if ( msqId < 0)
       
   755         {
       
   756         return NULL;
       
   757         }
       
   758     
       
   759     msgctl(msqId, IPC_STAT, (struct msqid_ds *)&buf);
       
   760     msgctl(msqId, IPC_RMID, 0);
       
   761     self->iBlocked = 1;
       
   762     
       
   763     return NULL;
       
   764 	}
       
   765 //-----------------------------------------------------------------------------
       
   766 //CTestMsgqueue::ThreadEntryFunctionWrite
       
   767 //-----------------------------------------------------------------------------
       
   768      
       
   769 void* CTestMsgqueue::ThreadEntryFunctionWrite(void* arg)
       
   770 	{  
       
   771     TInt msgType;
       
   772     TInt msqFlag = 0;
       
   773     HBufC8* messPtr = HBufC8::NewLC(65295);
       
   774     char *bufPtr = (char*)messPtr->Des().PtrZ();
       
   775     
       
   776     struct msg_buf{
       
   777          long    mtype;
       
   778          char    mtext[1];
       
   779          };
       
   780     struct msg_buf *pMess = (struct msg_buf *)bufPtr;
       
   781     TInt msgLen = 4090;
       
   782     CTestMsgqueue *self = static_cast<CTestMsgqueue*> (arg);
       
   783     
       
   784     TInt msqId = msgget(IPC_KEY, 0);
       
   785     if ( msqId < 0)
       
   786         {
       
   787         return NULL;
       
   788         }
       
   789     msgType = 1; //any message
       
   790     msqFlag =  ~IPC_NOWAIT; //Blocking
       
   791     
       
   792     memset(pMess, 0, 65295);
       
   793     pMess->mtype = msgType;
       
   794     pMess->mtext[msgLen + 4] = '\0';
       
   795     self->iBlocked = 1;
       
   796     msgsnd(msqId, pMess, msgLen, msqFlag);
       
   797     self->iBlocked = 1;
       
   798     self->iErrno = errno;
       
   799     CleanupStack::PopAndDestroy();
       
   800     return NULL;
       
   801 	}
       
   802 // -----------------------------------------------------------------------------
       
   803 // CTestMsgqueue::MessageQueueSend
       
   804 // -----------------------------------------------------------------------------
       
   805 //
       
   806 TInt CTestMsgqueue::MessageQueueSend(  )
       
   807     {
       
   808     _LIT(KFunc, "MessageQueueSend");
       
   809     INFO_PRINTF1( KFunc);
       
   810     
       
   811     TInt msqId = -1;
       
   812     TInt msgType;
       
   813     TInt msqFlag;
       
   814     TInt terror;
       
   815     TInt err;
       
   816     message_buf buf = {0};
       
   817     TInt msgLen;
       
   818     TInt retVal = KErrGeneral;
       
   819     
       
   820     // retrieve passed parameters
       
   821     err = MsgId( msqId);
       
   822     RETURN_ON_ERROR(err);
       
   823   
       
   824     err = MsgLen( msgLen);
       
   825     RETURN_ON_ERROR(err);
       
   826      
       
   827     err = MsgType( msgType);
       
   828     RETURN_ON_ERROR(err);
       
   829     
       
   830     err = MsgFlag( msqFlag);
       
   831     RETURN_ON_ERROR(err);
       
   832     
       
   833     err = ErrCode( terror);
       
   834     RETURN_ON_ERROR(err);
       
   835     
       
   836     MessageCreate(msgLen, msgType, &buf);
       
   837     
       
   838     err = msgsnd(msqId, &buf, msgLen, msqFlag);
       
   839     
       
   840     if ( err == -1 )
       
   841         {
       
   842         if(terror == WITHOUT_ERROR)
       
   843             {
       
   844             retVal = errno;
       
   845             }
       
   846         else
       
   847             {
       
   848             TInt errCode = 0;
       
   849             _LIT( KerrCode, "Param%d" );
       
   850 			 TBuf<8> pNameBuf;
       
   851 		     pNameBuf.Format(KerrCode,++iParamCnt);
       
   852 			 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   853 			 
       
   854 			 if(!res)
       
   855 				{
       
   856 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   857 			 	INFO_PRINTF1(Kerr) ;
       
   858 			 	return KErrGeneral ;
       
   859 				}
       
   860             if ( errCode == errno)
       
   861                 {
       
   862                 retVal = KErrNone;
       
   863                 }
       
   864             }    
       
   865         }
       
   866     else
       
   867         {
       
   868         if ( terror != WITH_ERROR )
       
   869             {
       
   870             retVal = KErrNone;
       
   871             }       
       
   872         }
       
   873     
       
   874     return retVal;
       
   875     }
       
   876 
       
   877 // -----------------------------------------------------------------------------
       
   878 // CTestMsgqueue::MessageQueueRecv
       
   879 // -----------------------------------------------------------------------------
       
   880 //
       
   881 TInt CTestMsgqueue::MessageQueueRecv(  )
       
   882     {
       
   883     _LIT(KFunc, "MessageQueueRecv");
       
   884     INFO_PRINTF1( KFunc);
       
   885     
       
   886     TInt msqId;
       
   887     TInt msgType;
       
   888     TInt msqFlag;
       
   889     TInt terror;
       
   890     TInt err;
       
   891     message_buf buf = {0};
       
   892     TInt msgLen;
       
   893     TInt retVal = KErrGeneral;
       
   894     
       
   895     // retrieve passed parameters
       
   896     err = MsgId( msqId);
       
   897     RETURN_ON_ERROR(err);
       
   898   
       
   899     err = MsgLen( msgLen);
       
   900     RETURN_ON_ERROR(err);
       
   901      
       
   902     err = MsgType( msgType);
       
   903     RETURN_ON_ERROR(err);
       
   904     
       
   905     err = MsgFlag( msqFlag);
       
   906     RETURN_ON_ERROR(err);
       
   907     
       
   908     err = ErrCode( terror);
       
   909     RETURN_ON_ERROR(err);
       
   910     
       
   911     char array[256];
       
   912     for(int idx=0; idx<255; idx++)
       
   913 	    {
       
   914     	array[idx]='a';    	
       
   915     	}
       
   916     array[255]='\0';	
       
   917     
       
   918     err = msgrcv(msqId, &buf, msgLen, msgType, msqFlag);
       
   919 
       
   920     if ( err == -1 )
       
   921         {
       
   922         if(terror == WITHOUT_ERROR)
       
   923             {
       
   924             retVal = errno;
       
   925             }
       
   926         else
       
   927             {
       
   928             TInt errCode = 0;
       
   929             _LIT( KerrCode, "Param%d" );
       
   930 			 TBuf<8> pNameBuf;
       
   931 		     pNameBuf.Format(KerrCode,++iParamCnt);
       
   932 			 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errCode);
       
   933 			 
       
   934 			 if(!res)
       
   935 				{
       
   936 			 	_LIT(Kerr , "Unable to retrieve error code") ;
       
   937 			 	INFO_PRINTF1(Kerr) ;
       
   938 			 	return KErrGeneral ;
       
   939 				}
       
   940             if ( errCode == errno)
       
   941                 {
       
   942                 retVal = KErrNone;
       
   943                 }
       
   944             }    
       
   945         }
       
   946     else
       
   947         {                      	
       
   948         if ( terror != WITH_ERROR && ( msgType <= 0 || buf.mtype == msgType))           
       
   949             {            
       
   950            	if((msgLen>0 && msgLen<256) && !(strncmp(&buf.mtext[0], &array[0], msgLen)))
       
   951             	retVal = KErrNone;
       
   952             }    
       
   953         }
       
   954 
       
   955     return retVal;
       
   956     }
       
   957 
       
   958 
       
   959 TInt CTestMsgqueue::IntgTest1()
       
   960 	{
       
   961 
       
   962 	TInt err = MessageQueueCreate();
       
   963 	if(!err)
       
   964 		{
       
   965 		err = MessageQueueClose();
       
   966 		}
       
   967 	return err;
       
   968 	}
       
   969 
       
   970 TInt CTestMsgqueue::IntgTest2()
       
   971 	{
       
   972 	TInt err = MessageQueueCreate();
       
   973 	if(!err)
       
   974 		{
       
   975 		err = MessageQueueCreate();
       
   976 			if(!err) 
       
   977 				{
       
   978 				err = MessageQueueClose();
       
   979 				}
       
   980 		}
       
   981 	return err;
       
   982 	}
       
   983 
       
   984 
       
   985 TInt CTestMsgqueue::IntgTest3()
       
   986 	{
       
   987 	TInt err = MessageQueueCreate();
       
   988 	if(!err)
       
   989 		{
       
   990 			err = MessageQueueControl();
       
   991 			if(!err) 
       
   992 				{
       
   993 				err = MessageQueueClose();
       
   994 				}
       
   995 		}
       
   996 	return err;
       
   997 	}
       
   998 
       
   999 TInt CTestMsgqueue::IntgTest4()
       
  1000 	{
       
  1001 	TInt err = MessageQueueCreate();
       
  1002 	if(!err)
       
  1003 		{
       
  1004 		err = MessageQueueSend();
       
  1005 		if(!err)
       
  1006 			{
       
  1007 			err = MessageQueueRecv();
       
  1008 			if(!err)
       
  1009 				{
       
  1010 				err = MessageQueueClose();
       
  1011 				}
       
  1012 			}
       
  1013 		}
       
  1014 	return err;
       
  1015 	}
       
  1016 
       
  1017 TInt CTestMsgqueue::IntgTest5()
       
  1018 	{
       
  1019 	TInt err = MessageQueueCreate();
       
  1020 	if(!err)
       
  1021 		{
       
  1022 			err = MessageQueueSend();
       
  1023 			if(!err)
       
  1024 				{
       
  1025 				err = MessageQueueClose();
       
  1026 				}	
       
  1027 		}
       
  1028 	return err;
       
  1029 	}
       
  1030 
       
  1031 TInt CTestMsgqueue::IntgTest6()
       
  1032 	{
       
  1033 	TInt err = MessageQueueCreate();
       
  1034 	if(!err)
       
  1035 		{
       
  1036 		err = MessageQueueSend();
       
  1037 		if(!err)
       
  1038 			{
       
  1039 			err = MessageQueueSend();
       
  1040 			if(!err)
       
  1041 				{
       
  1042 				err = MessageQueueClose();
       
  1043 				}
       
  1044 			}
       
  1045 		}
       
  1046 	return err;
       
  1047 	}
       
  1048 
       
  1049 TInt CTestMsgqueue::IntgTest7()
       
  1050 	{
       
  1051 	TInt err = MessageQueueCreate();
       
  1052 	if(!err)
       
  1053 		{
       
  1054 			err = MessageQueueRecv();
       
  1055 			if(!err)
       
  1056 				{
       
  1057 				err = MessageQueueClose();
       
  1058 				}	
       
  1059 		}
       
  1060 	return err;
       
  1061 	}
       
  1062 
       
  1063 TInt CTestMsgqueue::IntgTest8()
       
  1064 	{
       
  1065 	TInt err = MessageQueueCreate();
       
  1066 	if(!err)
       
  1067 		{
       
  1068 		err = MessageQueueSend();
       
  1069 		if(!err)
       
  1070 			{
       
  1071 			err = MessageQueueRecv();
       
  1072 			if(!err) 
       
  1073 				{
       
  1074 				err = MessageQueueRecv();	
       
  1075 				if(!err)
       
  1076 					{
       
  1077 					err = MessageQueueClose();
       
  1078 					}
       
  1079 				}
       
  1080 			}
       
  1081 		}
       
  1082 	return err;
       
  1083 	}
       
  1084 
       
  1085 TInt CTestMsgqueue::IntgTest9()
       
  1086 	{
       
  1087 	TInt err = MessageQueueCreate();
       
  1088 	if(!err)
       
  1089 		{
       
  1090 		err = MessageQueueSend();
       
  1091 		if(!err)
       
  1092 			{
       
  1093 			err = MessageQueueRecv();
       
  1094 			if(!err) 
       
  1095 				{
       
  1096 				err = MessageQueueControl();	
       
  1097 				if(!err)
       
  1098 					{
       
  1099 					err = MessageQueueClose();
       
  1100 					}
       
  1101 				}
       
  1102 			}
       
  1103 		}
       
  1104 	return err;
       
  1105 	}
       
  1106 
       
  1107 
       
  1108 TInt CTestMsgqueue::IntgTest10()
       
  1109 	{
       
  1110 	TInt err = MessageQueueCreate();
       
  1111 	if(err)
       
  1112 		{
       
  1113 		return err;
       
  1114 		}
       
  1115 	err = MessageQueueSend();
       
  1116 	if(err)
       
  1117 		{
       
  1118 		return err;
       
  1119 		}
       
  1120 	err = MessageQueueSend();
       
  1121 	if(err)
       
  1122 		{
       
  1123 		return err;
       
  1124 		}
       
  1125 	err = MessageQueueSend();
       
  1126 	if(err)
       
  1127 		{
       
  1128 		return err;
       
  1129 		}
       
  1130 	err = MessageQueueRecv();
       
  1131 	if(err) return err;
       
  1132 	if(err)
       
  1133 		{
       
  1134 		return err;
       
  1135 		}
       
  1136 	if(err)	return err;
       
  1137 	if(err)
       
  1138 		{
       
  1139 		return err;
       
  1140 		}
       
  1141 	if(err)	return err;
       
  1142 	if(err)
       
  1143 		{
       
  1144 		return err;
       
  1145 		}
       
  1146 	MessageQueueClose();
       
  1147 	return err;
       
  1148 	}
       
  1149 
       
  1150 TInt CTestMsgqueue::IntgTest11()
       
  1151 	{
       
  1152 	TInt err = MessageQueueCreate();
       
  1153 	if(err)
       
  1154 		{
       
  1155 		return err;
       
  1156 		}
       
  1157 	err = MessageQueueSend();
       
  1158 	if(err)
       
  1159 		{
       
  1160 		return err;
       
  1161 		}
       
  1162 	err = MessageQueueSend();
       
  1163 	if(err)
       
  1164 		{
       
  1165 		return err;
       
  1166 		}
       
  1167 	err = MessageQueueSend();
       
  1168 	if(err)
       
  1169 		{
       
  1170 		return err;
       
  1171 		}
       
  1172 	err = MessageQueueRecv();
       
  1173 	if(err)
       
  1174 		{
       
  1175 		return err;
       
  1176 		}
       
  1177 	err = MessageQueueRecv();
       
  1178 	if(err)
       
  1179 		{
       
  1180 		return err;
       
  1181 		}
       
  1182 	err = MessageQueueRecv();
       
  1183 	if(err)
       
  1184 		{
       
  1185 		return err;
       
  1186 		}
       
  1187 	err = MessageQueueRecv();
       
  1188 	if(err)
       
  1189 		{
       
  1190 		return err;
       
  1191 		}
       
  1192 	err = MessageQueueControl();
       
  1193 	if(err)
       
  1194 		{
       
  1195 		return err;
       
  1196 		}
       
  1197 	err = MessageQueueClose();
       
  1198 	return err;
       
  1199 	}
       
  1200 
       
  1201 TInt CTestMsgqueue::IntgTest12()
       
  1202 	{
       
  1203 	TInt err = MessageQueueCreate();
       
  1204 	if(err)
       
  1205 		{
       
  1206 		return err;
       
  1207 		}
       
  1208 	err = MessageQueueSend();
       
  1209 	if(err)
       
  1210 		{
       
  1211 		return err;
       
  1212 		}
       
  1213 	err = MessageQueueSend();
       
  1214 	if(err)
       
  1215 		{
       
  1216 		return err;
       
  1217 		}
       
  1218 	err = MessageQueueRecv();
       
  1219 	if(err)
       
  1220 		{
       
  1221 		return err;
       
  1222 		}
       
  1223 	err = MessageQueueRecv();
       
  1224 	if(err)
       
  1225 		{
       
  1226 		return err;
       
  1227 		}
       
  1228 	err = MessageQueueControl();
       
  1229 	if(err)
       
  1230 		{
       
  1231 		return err;
       
  1232 		}
       
  1233 	err = MessageQueueClose();
       
  1234 	return err;
       
  1235 	}
       
  1236 
       
  1237 TInt CTestMsgqueue::IntgTest13()
       
  1238 	{
       
  1239 	TInt err = MessageQueueCreate();
       
  1240 	if(!err)
       
  1241 		{
       
  1242 			err = CreateThread();
       
  1243 			if(!err)
       
  1244 				{
       
  1245 				err = MessageQueueClose();
       
  1246 				}
       
  1247 		}
       
  1248 	return err;
       
  1249 	}
       
  1250 
       
  1251 TInt CTestMsgqueue::IntgTest14()
       
  1252 	{
       
  1253 	TInt err = MessageQueueCreate();
       
  1254 	if(!err)
       
  1255 		{
       
  1256 		err = CreateThread();
       
  1257 		}
       
  1258 	return err;
       
  1259 	}
       
  1260 
       
  1261 TInt CTestMsgqueue::IntgTest15()
       
  1262 	{
       
  1263 	TInt err = MessageQueueCreate();
       
  1264 	if(!err)
       
  1265 		{
       
  1266 		err = MessageQueueSend();
       
  1267 		if(!err)
       
  1268 			{
       
  1269 			err = CreateThread();
       
  1270 			if(!err)
       
  1271 				{
       
  1272 				err = MessageQueueClose();
       
  1273 				}
       
  1274 			}
       
  1275 		}
       
  1276 	return err;
       
  1277 	}
       
  1278 
       
  1279 TInt CTestMsgqueue::IntgTest16()
       
  1280 	{
       
  1281 	TInt err = MessageQueueCreate();
       
  1282 	if(!err)
       
  1283 		{
       
  1284 			for(int i=0;i<512;i++)
       
  1285 				{
       
  1286 				INFO_PRINTF2(_L("i is %d\n"),i);
       
  1287 				err = MessageQueueSend();
       
  1288 				if(err!=0)
       
  1289 					{
       
  1290 					break;
       
  1291 					}
       
  1292 				}
       
  1293 			if(!err)
       
  1294 				{
       
  1295 				err = CreateThread();
       
  1296 				}
       
  1297 		}
       
  1298 	return err;
       
  1299 	}
       
  1300 
       
  1301 TInt CTestMsgqueue::IntgTest17()
       
  1302 	{
       
  1303 	TInt err = MessageQueueCreate();
       
  1304 	if(!err)
       
  1305 		{
       
  1306 		err = MessageQueueSend();
       
  1307 		if(!err)
       
  1308 			{
       
  1309 			err = MessageQueueRecv();
       
  1310 			if(!err) 
       
  1311 				{
       
  1312 				err = MessageQueueSend();	
       
  1313 				if(!err)
       
  1314 					{
       
  1315 					err = MessageQueueClose();
       
  1316 					}
       
  1317 				}
       
  1318 			}
       
  1319 		}
       
  1320 	return err;
       
  1321 	}
       
  1322 
       
  1323 TInt CTestMsgqueue::IntgTest6_1()
       
  1324 	{
       
  1325 	TInt err = MessageQueueCreate();
       
  1326 	if(!err)
       
  1327 		{
       
  1328 		err = MessageQueueSend();
       
  1329 		if(!err)
       
  1330 			{
       
  1331 			for(int i=0;i<16;i++)
       
  1332 				{
       
  1333 				err = MessageQueueSend();
       
  1334 				if(err!=0)
       
  1335 					{
       
  1336 					break;
       
  1337 					}
       
  1338 				}
       
  1339 			if(!err)
       
  1340 				{
       
  1341 				err = MessageQueueClose();
       
  1342 				}
       
  1343 			}
       
  1344 		}
       
  1345 	return err;
       
  1346 	}
       
  1347 	
       
  1348 
       
  1349 /*
       
  1350 TInt CTestMsgqueue::IntgTest16()
       
  1351 	{
       
  1352 	int msq_id;
       
  1353 	if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1354 		{
       
  1355 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1356       	INFO_PRINTF2(KFunc, errno);
       
  1357        	return KErrGeneral;
       
  1358     	}
       
  1359     int err;
       
  1360     message_buf buf = {0};
       
  1361     MessageCreate(4096, 1, &buf);
       
  1362     for(int i=0;i<16;i++)
       
  1363     	{
       
  1364     	err = msgsnd(msq_id, &buf, 4096, 2048);
       
  1365     	}
       
  1366     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1367     	{
       
  1368 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1369       	INFO_PRINTF2(KFunc, errno);
       
  1370        	return KErrGeneral;
       
  1371     	}
       
  1372 	}
       
  1373 
       
  1374 nt CTestMsgqueue::IntgTest6_1()
       
  1375 	{
       
  1376 	TInt err = MessageQueueCreate();
       
  1377 	if(!err)
       
  1378 		{
       
  1379 		err = MessageQueueSend();
       
  1380 		if(!err)
       
  1381 			{
       
  1382 			err = MessageQueueSend();
       
  1383 			err = MessageQueueSend();
       
  1384 			err = MessageQueueSend();
       
  1385 			err = MessageQueueSend();
       
  1386 			if(!err)
       
  1387 				{
       
  1388 				err = MessageQueueClose();
       
  1389 				}
       
  1390 			}
       
  1391 		}
       
  1392 	return err;
       
  1393 	}
       
  1394 */
       
  1395 //////////////////////////////////////////////////////////////////////////
       
  1396 
       
  1397 					// MSGGET 
       
  1398 					
       
  1399 // with the specified key
       
  1400 
       
  1401 TInt CTestMsgqueue::msggettest1()
       
  1402 	{
       
  1403 	int msq_id;
       
  1404 	
       
  1405 	if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1406 		{
       
  1407 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1408       	INFO_PRINTF2(KFunc, errno);
       
  1409        	return KErrGeneral;
       
  1410     	}
       
  1411     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1412     	{
       
  1413 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1414       	INFO_PRINTF2(KFunc, errno);
       
  1415        	return KErrGeneral;
       
  1416     	}
       
  1417 
       
  1418 	return KErrNone;
       
  1419 	}
       
  1420 
       
  1421 // with the IPC_PRIVATE flag
       
  1422 
       
  1423 TInt CTestMsgqueue::msggettest2()
       
  1424 	{
       
  1425 	int msq_id;
       
  1426 	
       
  1427 	if ((msq_id = msgget(IPC_PRIVATE , IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1428 		{
       
  1429 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1430       	INFO_PRINTF2(KFunc, errno);
       
  1431        	return KErrGeneral;
       
  1432     	}
       
  1433     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1434     	{
       
  1435 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1436       	INFO_PRINTF2(KFunc, errno);
       
  1437        	return KErrGeneral;
       
  1438     	}
       
  1439 	return KErrNone;
       
  1440 	}
       
  1441 
       
  1442 // Max no.of msg ids per process 
       
  1443 // Check for the errno ENOSPC
       
  1444 
       
  1445 TInt CTestMsgqueue::msggettest3()
       
  1446 	{
       
  1447 	int msq_id[1512];
       
  1448 	
       
  1449 	int i=0,j=0;
       
  1450 	int ret = KErrNone;
       
  1451 	for(i=0;;i++)
       
  1452 		{
       
  1453 		if ((msq_id[i] = msgget(IPC_PRIVATE , IPC_CREAT | 0666)) == -1) 
       
  1454 			{
       
  1455 			if(errno == ENOSPC)
       
  1456 				{
       
  1457 				_LIT(KFunc, "Message Q create set the proper errno %d\n");
       
  1458       			INFO_PRINTF2(KFunc,i);
       
  1459        			break;
       
  1460 				}
       
  1461 			else
       
  1462 				{
       
  1463 				_LIT(KFunc, "Message Q create didnt set the proper errno \n");
       
  1464       			INFO_PRINTF1(KFunc);
       
  1465       			ret=KErrGeneral;
       
  1466        			break;
       
  1467 				}
       
  1468     		}
       
  1469 		}
       
  1470 	
       
  1471 	for(j=0;j<i;j++)
       
  1472 		{
       
  1473 		if (msgctl(msq_id[j], IPC_RMID, NULL) == -1) 
       
  1474     		{
       
  1475 			_LIT(KFunc, "Message Q delete failed with errno %d \n");
       
  1476       		INFO_PRINTF2(KFunc, errno);
       
  1477        		ret= KErrGeneral;
       
  1478     		}
       
  1479 		}
       
  1480     	     	
       
  1481 	return ret;
       
  1482 	}
       
  1483 
       
  1484 // check for the errno ENOENT
       
  1485 
       
  1486 TInt CTestMsgqueue::msggettest4()
       
  1487 	{
       
  1488 	
       
  1489 	int msq_id;
       
  1490 	
       
  1491 	msq_id = msgget(MESSAGE_Q_KEY , IPC_CREAT & 0);
       
  1492 	
       
  1493 	if (msq_id == -1 && errno == ENOENT) 
       
  1494 		{
       
  1495 		_LIT(KFunc, "Message Q set the proper the errno\n");
       
  1496       	INFO_PRINTF1(KFunc);
       
  1497        	return KErrNone;
       
  1498     	}
       
  1499     return KErrGeneral;
       
  1500 	
       
  1501 	}
       
  1502 
       
  1503 // check for the errno EEXIST
       
  1504 
       
  1505 TInt CTestMsgqueue::msggettest5()
       
  1506 	{
       
  1507 	int msq_id1,msq_id2;
       
  1508 	
       
  1509 	if ((msq_id1 = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1510 		{
       
  1511 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1512       	INFO_PRINTF2(KFunc, errno);
       
  1513        	return KErrGeneral;
       
  1514     	}
       
  1515     else
       
  1516     	{
       
  1517     	msq_id2 = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666);
       
  1518     	if(msq_id2 == -1 && errno == EEXIST)
       
  1519     		{  		
       
  1520 			_LIT(KFunc, "Message Q set the proper the errno\n");
       
  1521       		INFO_PRINTF1(KFunc);
       
  1522     		}
       
  1523     	else
       
  1524     		{  		
       
  1525 			_LIT(KFunc, "Message Q didnt set the proper the errno\n");
       
  1526       		INFO_PRINTF1(KFunc);
       
  1527        		return KErrGeneral;
       
  1528     		}
       
  1529     	}
       
  1530     if (msgctl(msq_id1, IPC_RMID, NULL) == -1) 
       
  1531     	{
       
  1532 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1533       	INFO_PRINTF2(KFunc, errno);
       
  1534        	return KErrGeneral;
       
  1535     	}
       
  1536 
       
  1537 	return KErrNone;
       
  1538 	}
       
  1539 
       
  1540 // for the defect 
       
  1541 
       
  1542 TInt CTestMsgqueue::msggettest6()
       
  1543 	{
       
  1544 	int msq_id1;
       
  1545 	
       
  1546 	if ((msq_id1 = msgget(0, 0)) == -1) 
       
  1547 		{
       
  1548 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1549       	INFO_PRINTF2(KFunc, errno);
       
  1550        	return KErrGeneral;
       
  1551     	}
       
  1552     
       
  1553     if (msgctl(msq_id1, IPC_RMID, NULL) == -1) 
       
  1554     	{
       
  1555 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1556       	INFO_PRINTF2(KFunc, errno);
       
  1557        	return KErrGeneral;
       
  1558     	}
       
  1559 
       
  1560 	return KErrNone;
       
  1561 	}
       
  1562 
       
  1563 //-------------------------------------------------------------------------------
       
  1564 // Function Name : 	CTestMsgqueue::MsggetCreateKey()
       
  1565 // Description   : 	This test creates a message queue instance with the 
       
  1566 //					specified key, and deletes it.
       
  1567 // Param1		 :  Key to use for msgget (passed through the ini file)
       
  1568 //---------------------------------------------------------------------------------
       
  1569 TInt CTestMsgqueue::MsggetCreateKey()
       
  1570 	{
       
  1571 	int msgid;
       
  1572 	int key;
       
  1573 	TBuf<8> pNameBuf;
       
  1574 	
       
  1575 	iParamCnt = 0;
       
  1576 	_LIT( KaMsgNum, "Param%d" );
       
  1577 	
       
  1578 	pNameBuf.Format(KaMsgNum,++iParamCnt);
       
  1579 	TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, key);
       
  1580 	if(!res)
       
  1581 		{
       
  1582 	 	_LIT(Kerr , "Unable to retrieve msg key") ;
       
  1583 	 	INFO_PRINTF1(Kerr) ;
       
  1584 	 	return KErrGeneral ;
       
  1585 		}
       
  1586 	
       
  1587 	msgid = msgget(key, IPC_CREAT);
       
  1588 	if( msgid < 0 )
       
  1589 		{
       
  1590 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1591       	INFO_PRINTF2(KFunc, errno);
       
  1592        	return KErrGeneral;
       
  1593     	}
       
  1594 	
       
  1595     if (msgctl(msgid, IPC_RMID, NULL) == -1) 
       
  1596     	{
       
  1597 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1598       	INFO_PRINTF2(KFunc, errno);
       
  1599        	return KErrGeneral;
       
  1600     	}
       
  1601 	    
       
  1602     return KErrNone;
       
  1603 	}
       
  1604 
       
  1605 						// MSGCTL
       
  1606 
       
  1607 // check for the errno EINVAL with invalid <id>
       
  1608 				
       
  1609 TInt CTestMsgqueue::msgctltest1()
       
  1610 	{
       
  1611 	
       
  1612 	if ((msgctl(-1, IPC_RMID, NULL) == -1) && errno == EINVAL ) 
       
  1613     	{
       
  1614 		_LIT(KFunc, "msgctl set the proper errno\n");
       
  1615       	INFO_PRINTF1(KFunc);
       
  1616        	return KErrNone;
       
  1617     	}
       
  1618 
       
  1619 	return KErrGeneral;
       
  1620 	}
       
  1621 	
       
  1622 // check for the errno EINVAL with invalid <cmd>
       
  1623 				
       
  1624 TInt CTestMsgqueue::msgctltest2()
       
  1625 	{
       
  1626 	int msq_id;
       
  1627 	
       
  1628 	if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1629 		{
       
  1630 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1631       	INFO_PRINTF2(KFunc, errno);
       
  1632        	return KErrGeneral;
       
  1633     	}
       
  1634     if ((msgctl(msq_id, -5, NULL) == -1) && errno == EINVAL) 
       
  1635     	{
       
  1636 		_LIT(KFunc, "msgctl set the proper errno\n");
       
  1637       	INFO_PRINTF1(KFunc);
       
  1638       	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1639     		{
       
  1640 			_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1641       		INFO_PRINTF2(KFunc, errno);
       
  1642        		return KErrGeneral;
       
  1643     		}
       
  1644        	return KErrNone;
       
  1645     	}
       
  1646 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1647     	{
       
  1648 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1649       	INFO_PRINTF2(KFunc, errno);
       
  1650        	return KErrGeneral;
       
  1651     	}
       
  1652     	
       
  1653 	return KErrGeneral;
       
  1654 	}
       
  1655 
       
  1656 // get the details of the message queue with flag IPC_STAT 
       
  1657 
       
  1658 TInt CTestMsgqueue::msgctltest3()
       
  1659 	{
       
  1660 	int msq_id,ret=KErrNone;
       
  1661 	struct msqid_ds buf;
       
  1662 	int msgflg = IPC_R|IPC_CREAT | IPC_EXCL | 0666;
       
  1663 	if ((msq_id = msgget(MESSAGE_Q_KEY, msgflg)) == -1) 
       
  1664 		{
       
  1665 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1666       	INFO_PRINTF2(KFunc, errno);
       
  1667        	return KErrGeneral;
       
  1668     	}
       
  1669     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  1670     	{
       
  1671 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1672       	INFO_PRINTF2(KFunc, errno);
       
  1673        	ret= KErrGeneral;
       
  1674     	}
       
  1675 	
       
  1676 	if ( buf.msg_cbytes != 0 )
       
  1677         {
       
  1678         ret= KErrGeneral;
       
  1679         }
       
  1680     if ( buf.msg_qnum != 0 )
       
  1681         {
       
  1682         ret= KErrGeneral;
       
  1683         }
       
  1684     if ( buf.msg_perm.key != MESSAGE_Q_KEY )
       
  1685         {
       
  1686         ret= KErrGeneral;
       
  1687         }
       
  1688    if ( buf.msg_qbytes != 2097152 )
       
  1689         {
       
  1690         ret= KErrGeneral;
       
  1691         } 
       
  1692     if( buf.msg_lspid != 0 )
       
  1693     	{
       
  1694         ret= KErrGeneral;
       
  1695         } 	
       
  1696     if( buf.msg_lrpid != 0 )
       
  1697     	{
       
  1698         ret= KErrGeneral;
       
  1699         }
       
  1700     if( buf.msg_stime != 0 )
       
  1701     	{
       
  1702         ret= KErrGeneral;
       
  1703         }
       
  1704     if( buf.msg_rtime != 0 )
       
  1705     	{
       
  1706         ret= KErrGeneral;
       
  1707         }
       
  1708 
       
  1709 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1710     	{
       
  1711 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1712       	INFO_PRINTF2(KFunc, errno);
       
  1713        	ret= KErrGeneral;
       
  1714     	}
       
  1715 	return ret;
       
  1716 	}
       
  1717 
       
  1718 // set the max # of bytes on the queue with IPC_SET flag
       
  1719 
       
  1720 TInt CTestMsgqueue::msgctltest4()
       
  1721 	{
       
  1722 	int msq_id;
       
  1723 	struct msqid_ds buf;
       
  1724 	struct msqid_ds buf1;
       
  1725 	if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1726 		{
       
  1727 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1728       	INFO_PRINTF2(KFunc, errno);
       
  1729        	return KErrGeneral;
       
  1730     	}
       
  1731     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  1732     	{
       
  1733 		_LIT(KFunc, "Message Q set failed with errno %d\n");
       
  1734       	INFO_PRINTF2(KFunc, errno);
       
  1735        	return KErrGeneral;
       
  1736     	}
       
  1737     buf1=buf;
       
  1738     buf1.msg_qbytes = 65546;
       
  1739     if (msgctl(msq_id, IPC_SET, &buf1) == -1) 
       
  1740     	{
       
  1741 		_LIT(KFunc, "Message Q set failed with errno %d\n");
       
  1742       	INFO_PRINTF2(KFunc, errno);
       
  1743        	return KErrGeneral;
       
  1744     	}
       
  1745     	
       
  1746     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  1747     	{
       
  1748 		_LIT(KFunc, "Message Q get failed with errno %d\n");
       
  1749       	INFO_PRINTF2(KFunc, errno);
       
  1750        	return KErrGeneral;
       
  1751     	}
       
  1752 	
       
  1753 	if ( buf.msg_cbytes != 0 )
       
  1754         {
       
  1755         return KErrGeneral;
       
  1756         }
       
  1757     if ( buf.msg_qnum != 0 )
       
  1758         {
       
  1759         return KErrGeneral;
       
  1760         }
       
  1761     if ( buf.msg_perm.key != MESSAGE_Q_KEY )
       
  1762         {
       
  1763         return KErrGeneral;
       
  1764         }
       
  1765     if ( buf.msg_qbytes != 65546 )
       
  1766         {
       
  1767         return KErrGeneral;
       
  1768         } 
       
  1769 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1770     	{
       
  1771 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1772       	INFO_PRINTF2(KFunc, errno);
       
  1773        	return KErrGeneral;
       
  1774     	}
       
  1775 	return KErrNone;
       
  1776 	}
       
  1777 
       
  1778 // check for the errno EACCESS without read permissions over IPC_STAT
       
  1779 
       
  1780 TInt CTestMsgqueue::msgctltest5()
       
  1781 	{
       
  1782 	int msq_id,ret=KErrNone,x;
       
  1783 	struct msqid_ds buf;
       
  1784 	if ((msq_id = msgget(IPC_PRIVATE, 0  )) == -1) 
       
  1785 		{
       
  1786 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1787       	INFO_PRINTF2(KFunc, errno);
       
  1788        	return KErrGeneral;
       
  1789     	}
       
  1790    	x=msgctl(msq_id, IPC_STAT, &buf);     	
       
  1791     if ((x != -1) && errno != EACCES) 
       
  1792     	{
       
  1793 		_LIT(KFunc, "Message Q didnt set the proper errno\n");
       
  1794       	INFO_PRINTF1(KFunc);
       
  1795        	ret= KErrGeneral;
       
  1796     	}
       
  1797 	
       
  1798 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1799     	{
       
  1800 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1801       	INFO_PRINTF2(KFunc, errno);
       
  1802        	ret= KErrGeneral;
       
  1803     	}
       
  1804 	return ret;
       
  1805 	}
       
  1806 	
       
  1807 	
       
  1808 					// MSGSND
       
  1809 					
       
  1810 // check for the errno EIDRM
       
  1811 
       
  1812 TInt CTestMsgqueue::msgsndtest1()
       
  1813 	{
       
  1814 	int msq_id,len;
       
  1815 	struct {
       
  1816        long mtype;
       
  1817        char mtext[128];
       
  1818    } msg_buf;
       
  1819 
       
  1820 	msg_buf.mtype = 1; /* message identifier */
       
  1821     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  1822     len = strlen(msg_buf.mtext)+1;
       
  1823 
       
  1824 	if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1825 		{
       
  1826 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1827       	INFO_PRINTF2(KFunc, errno);
       
  1828        	return KErrGeneral;
       
  1829     	}
       
  1830     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1831     	{
       
  1832 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1833       	INFO_PRINTF2(KFunc, errno);
       
  1834        	return KErrGeneral;
       
  1835     	}
       
  1836     
       
  1837 	if ((msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT) != -1) && errno != EIDRM) 
       
  1838 		{
       
  1839 		_LIT(KFunc, "msgsnd failed in setting errno\n");
       
  1840       	INFO_PRINTF1(KFunc);
       
  1841        	return KErrGeneral;
       
  1842     	}
       
  1843 	return KErrNone;
       
  1844 	}
       
  1845 
       
  1846 // check for the errno EINVAL with invalid <id>
       
  1847 				
       
  1848 TInt CTestMsgqueue::msgsndtest2()
       
  1849 	{
       
  1850 	int msq_id=-1,len,ret=KErrGeneral;
       
  1851 	struct {
       
  1852        long mtype;
       
  1853        char mtext[128];
       
  1854    	} msg_buf;
       
  1855 
       
  1856 	msg_buf.mtype = 1; /* message identifier */
       
  1857     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  1858     len = strlen(msg_buf.mtext)+1;
       
  1859     
       
  1860 	if ((msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  == -1) && errno == EINVAL ) 
       
  1861     	{
       
  1862 		_LIT(KFunc, "msgsnd set the proper errno\n");
       
  1863       	INFO_PRINTF1(KFunc);
       
  1864        	ret=KErrNone;
       
  1865     	}
       
  1866 
       
  1867 	return ret;
       
  1868 	}
       
  1869 
       
  1870 // check for the errno EINVAL with mtype<1
       
  1871 				
       
  1872 TInt CTestMsgqueue::msgsndtest3()
       
  1873 	{
       
  1874 	int msq_id=0,len,ret=KErrNone;
       
  1875 	struct {
       
  1876        long mtype;
       
  1877        char mtext[128];
       
  1878    	} msg_buf;
       
  1879 
       
  1880 	msg_buf.mtype = 0; /* message identifier */
       
  1881     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  1882     len = strlen(msg_buf.mtext)+1;
       
  1883     
       
  1884     if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1885 		{
       
  1886 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1887       	INFO_PRINTF2(KFunc, errno);
       
  1888        	ret= KErrGeneral;
       
  1889     	}
       
  1890     	
       
  1891 	if ((msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  == -1) && errno == EINVAL ) 
       
  1892     	{
       
  1893 		_LIT(KFunc, "msgsnd set the proper errno\n");
       
  1894       	INFO_PRINTF1(KFunc);
       
  1895     	}
       
  1896 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1897     	{
       
  1898 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1899       	INFO_PRINTF2(KFunc, errno);
       
  1900        	ret= KErrGeneral;
       
  1901     	}
       
  1902 	return ret;
       
  1903 	}
       
  1904 
       
  1905 // check for the errno EINVAL with msgsz<0
       
  1906 TInt CTestMsgqueue::msgsndtest4()
       
  1907 	{
       
  1908 	int msq_id=0,ret=KErrNone;
       
  1909 	struct {
       
  1910        long mtype;
       
  1911        char mtext[128];
       
  1912    	} msg_buf;
       
  1913 
       
  1914 	msg_buf.mtype = 1; /* message identifier */
       
  1915     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  1916     int len = strlen(msg_buf.mtext)+1;
       
  1917     
       
  1918     if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1919 		{
       
  1920 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1921       	INFO_PRINTF2(KFunc, errno);
       
  1922        	ret= KErrGeneral;
       
  1923     	}
       
  1924     	
       
  1925 	if ((msgsnd(msq_id, (struct msgbuf *)&msg_buf, (size_t)-1, IPC_NOWAIT)  == -1) && errno == EINVAL ) 
       
  1926     	{
       
  1927 		_LIT(KFunc, "msgsnd set the proper errno\n");
       
  1928       	INFO_PRINTF1(KFunc);
       
  1929     	}
       
  1930 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1931     	{
       
  1932 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1933       	INFO_PRINTF2(KFunc, errno);
       
  1934        	ret= KErrGeneral;
       
  1935     	}
       
  1936 	return ret;
       
  1937 	}
       
  1938 
       
  1939 // successful msgsnd
       
  1940 
       
  1941 TInt CTestMsgqueue::msgsndtest5()
       
  1942 	{
       
  1943 	int msq_id=0,len,ret=KErrNone;
       
  1944 	struct msqid_ds buf;
       
  1945 	struct {
       
  1946        long mtype;
       
  1947        char mtext[128];
       
  1948    	} msg_buf;
       
  1949 
       
  1950 	msg_buf.mtype = 1; /* message identifier */
       
  1951     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  1952     len = strlen(msg_buf.mtext)+1;
       
  1953     
       
  1954     if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  1955 		{
       
  1956 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  1957       	INFO_PRINTF2(KFunc, errno);
       
  1958        	ret= KErrGeneral;
       
  1959     	}
       
  1960     	
       
  1961 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  1962     	{
       
  1963 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  1964       	INFO_PRINTF2(KFunc,errno);
       
  1965       	ret = KErrGeneral;
       
  1966     	}
       
  1967     
       
  1968     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  1969     	{
       
  1970 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1971       	INFO_PRINTF2(KFunc, errno);
       
  1972        	return KErrGeneral;
       
  1973     	}
       
  1974 	/*
       
  1975 	if ( buf.msg_cbytes != len )
       
  1976         {
       
  1977         ret= KErrGeneral;
       
  1978         }*/
       
  1979     if ( buf.msg_qnum != 1 )
       
  1980         {
       
  1981         ret= KErrGeneral;
       
  1982         }
       
  1983     if ( buf.msg_perm.key != IPC_PRIVATE )
       
  1984         {
       
  1985         ret= KErrGeneral;
       
  1986         }
       
  1987     if ( buf.msg_qbytes != 2097152 )
       
  1988         {
       
  1989         ret= KErrGeneral;
       
  1990         }
       
  1991 
       
  1992  	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  1993     	{
       
  1994 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  1995       	INFO_PRINTF2(KFunc, errno);
       
  1996        	ret= KErrGeneral;
       
  1997     	}
       
  1998 	return ret;
       
  1999 	}
       
  2000 
       
  2001 // successful 2 msgsnd
       
  2002 
       
  2003 TInt CTestMsgqueue::msgsndtest6()
       
  2004 	{
       
  2005 	int msq_id=0,len,ret=KErrNone;
       
  2006 	struct msqid_ds buf;
       
  2007 	struct {
       
  2008        long mtype;
       
  2009        char mtext[128];
       
  2010    	} msg_buf;
       
  2011 
       
  2012 	msg_buf.mtype = 1; /* message identifier */
       
  2013     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2014     len = strlen(msg_buf.mtext)+1;
       
  2015     
       
  2016     if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2017 		{
       
  2018 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2019       	INFO_PRINTF2(KFunc, errno);
       
  2020        	ret= KErrGeneral;
       
  2021     	}
       
  2022     	
       
  2023 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2024     	{
       
  2025 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2026       	INFO_PRINTF2(KFunc,errno);
       
  2027       	ret = KErrGeneral;
       
  2028     	}
       
  2029     
       
  2030     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2031     	{
       
  2032 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2033       	INFO_PRINTF2(KFunc,errno);
       
  2034       	ret = KErrGeneral;
       
  2035     	}
       
  2036     
       
  2037     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  2038     	{
       
  2039 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2040       	INFO_PRINTF2(KFunc, errno);
       
  2041        	return KErrGeneral;
       
  2042     	}
       
  2043 	/*
       
  2044 	if ( buf.msg_cbytes != (2*len) )
       
  2045         {
       
  2046         ret= KErrGeneral;
       
  2047         }
       
  2048         */
       
  2049     if ( buf.msg_qnum != 2 )
       
  2050         {
       
  2051         ret= KErrGeneral;
       
  2052         }
       
  2053     if ( buf.msg_perm.key != IPC_PRIVATE )
       
  2054         {
       
  2055         ret= KErrGeneral;
       
  2056         }
       
  2057     if ( buf.msg_qbytes != 2097152 )
       
  2058         {
       
  2059         ret= KErrGeneral;
       
  2060         }
       
  2061 
       
  2062     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2063     	{
       
  2064 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2065       	INFO_PRINTF2(KFunc, errno);
       
  2066        	ret= KErrGeneral;
       
  2067     	}
       
  2068 	return ret;
       
  2069 	}
       
  2070 	
       
  2071 					// MSGRCV
       
  2072 					
       
  2073 
       
  2074 // check for the errno EIDRM
       
  2075 
       
  2076 TInt CTestMsgqueue::msgrcvtest1()
       
  2077 	{
       
  2078 	int msq_id,len;
       
  2079 	struct {
       
  2080        long mtype;
       
  2081        char mtext[128];
       
  2082    } msg_buf;
       
  2083 
       
  2084 	msg_buf.mtype = 1; /* message identifier */
       
  2085     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2086     len = strlen(msg_buf.mtext)+1;
       
  2087 
       
  2088 	if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2089 		{
       
  2090 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2091       	INFO_PRINTF2(KFunc, errno);
       
  2092        	return KErrGeneral;
       
  2093     	}
       
  2094     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2095     	{
       
  2096 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2097       	INFO_PRINTF2(KFunc, errno);
       
  2098        	return KErrGeneral;
       
  2099     	}
       
  2100     
       
  2101 	if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf, len,1, 0) != -1) && errno != EIDRM) 
       
  2102 		{
       
  2103 		_LIT(KFunc, "msgsnd failed in setting errno\n");
       
  2104       	INFO_PRINTF1(KFunc);
       
  2105        	return KErrGeneral;
       
  2106     	}
       
  2107 	return KErrNone;
       
  2108 	}
       
  2109 
       
  2110 // check for the errno E2BIG        
       
  2111 
       
  2112 TInt CTestMsgqueue::msgrcvtest2()
       
  2113 	{
       
  2114 	int msq_id=0,len,ret=KErrNone;
       
  2115 	struct {
       
  2116        long mtype;
       
  2117        char mtext[128];
       
  2118    	} msg_buf;
       
  2119 
       
  2120 	msg_buf.mtype = 1; /* message identifier */
       
  2121     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2122     len = strlen(msg_buf.mtext)+1;
       
  2123     
       
  2124     if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2125 		{
       
  2126 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2127       	INFO_PRINTF2(KFunc, errno);
       
  2128        	ret= KErrGeneral;
       
  2129     	}
       
  2130     	
       
  2131 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2132     	{
       
  2133 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2134       	INFO_PRINTF2(KFunc,errno);
       
  2135       	ret = KErrGeneral;
       
  2136     	}
       
  2137     
       
  2138     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf, 15,1, 0) != -1) || errno != E2BIG) 
       
  2139 		{
       
  2140 		_LIT(KFunc, "msgrcv failed in setting errno %d\n");
       
  2141       	INFO_PRINTF2(KFunc,errno);
       
  2142        	return KErrGeneral;
       
  2143     	}
       
  2144     
       
  2145 	if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2146     	{
       
  2147 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2148       	INFO_PRINTF2(KFunc, errno);
       
  2149        	ret= KErrGeneral;
       
  2150     	}
       
  2151 	return ret;
       
  2152 	}
       
  2153 
       
  2154 // check for the errno EINVAL
       
  2155 
       
  2156 TInt CTestMsgqueue::msgrcvtest3()
       
  2157 	{
       
  2158 	int msq_id=-1,len,ret=KErrNone;
       
  2159 	struct {
       
  2160        long mtype;
       
  2161        char mtext[128];
       
  2162    	} msg_buf;
       
  2163 
       
  2164 	msg_buf.mtype = 1; /* message identifier */
       
  2165     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2166     len = strlen(msg_buf.mtext)+1;
       
  2167     
       
  2168     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf, len,1, 0) != -1) && errno != EINVAL) 
       
  2169 		{
       
  2170 		_LIT(KFunc, "msgsnd failed in setting errno\n");
       
  2171       	INFO_PRINTF1(KFunc);
       
  2172        	return KErrGeneral;
       
  2173     	}
       
  2174     
       
  2175 	return ret;
       
  2176 	}
       
  2177 	
       
  2178 
       
  2179 // check for the errno ENOMSG
       
  2180 
       
  2181 TInt CTestMsgqueue::msgrcvtest4()
       
  2182 	{
       
  2183 	int msq_id,len;
       
  2184 	struct {
       
  2185        long mtype;
       
  2186        char mtext[128];
       
  2187    } msg_buf,msg_buf1;
       
  2188 
       
  2189 	msg_buf.mtype = 1; /* message identifier */
       
  2190     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2191     len = strlen(msg_buf.mtext)+1;
       
  2192 
       
  2193 	if ((msq_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2194 		{
       
  2195 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2196       	INFO_PRINTF2(KFunc, errno);
       
  2197        	return KErrGeneral;
       
  2198     	}
       
  2199     
       
  2200     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2201     	{
       
  2202 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2203       	INFO_PRINTF2(KFunc,errno);
       
  2204       	return KErrGeneral;
       
  2205     	}
       
  2206     	    
       
  2207 	if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf1, len,3, IPC_NOWAIT) != -1) && errno != ENOMSG) 
       
  2208 		{
       
  2209 		_LIT(KFunc, "msgsnd failed in setting errno\n");
       
  2210       	INFO_PRINTF1(KFunc);
       
  2211        	return KErrGeneral;
       
  2212     	}
       
  2213     	
       
  2214     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2215     	{
       
  2216 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2217       	INFO_PRINTF2(KFunc, errno);
       
  2218        	return KErrGeneral;
       
  2219     	}
       
  2220     
       
  2221 	return KErrNone;
       
  2222 	}
       
  2223 
       
  2224 				// MSGSND & MSGRCV 
       
  2225 				
       
  2226 TInt CTestMsgqueue::sndrcvtest1()
       
  2227 	{
       
  2228 	int msq_id=0,len,ret=KErrNone;
       
  2229 	struct msqid_ds buf= {0};
       
  2230 	struct {
       
  2231        long mtype;
       
  2232        char mtext[128];
       
  2233    	} msg_buf,msg_buf1;
       
  2234 
       
  2235 	msg_buf.mtype = 1; // message identifier 
       
  2236     strcpy(msg_buf.mtext, "some_data_to_send"); // data 
       
  2237     len = strlen(msg_buf.mtext)+1;
       
  2238     
       
  2239     if ((msq_id = msgget(MESSAGE_Q_KEY1, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2240 		{
       
  2241 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2242       	INFO_PRINTF2(KFunc, errno);
       
  2243        	ret= KErrGeneral;
       
  2244     	}
       
  2245     	
       
  2246 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2247     	{
       
  2248 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2249       	INFO_PRINTF2(KFunc,errno);
       
  2250       	ret = KErrGeneral;
       
  2251     	}
       
  2252     
       
  2253     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf1, len,1, 0) == -1)) 
       
  2254 		{
       
  2255 		_LIT(KFunc, "msgsnd failed in setting errno %d\n");
       
  2256       	INFO_PRINTF2(KFunc,errno);
       
  2257        	return KErrGeneral;
       
  2258     	}
       
  2259     
       
  2260     if(msg_buf1.mtype != 1)
       
  2261     	{
       
  2262     	return KErrGeneral;
       
  2263     	}
       
  2264     if( (strcmp(msg_buf1.mtext,msg_buf.mtext)) )
       
  2265     	{
       
  2266     	return KErrGeneral;
       
  2267     	}
       
  2268     if ( buf.msg_qnum != 0 )
       
  2269         {
       
  2270         ret= KErrGeneral;
       
  2271         }
       
  2272     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2273     	{
       
  2274 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2275       	INFO_PRINTF2(KFunc, errno);
       
  2276        	ret= KErrGeneral;
       
  2277     	}
       
  2278 	return ret;
       
  2279 	}
       
  2280 /*
       
  2281 
       
  2282 TInt CTestMsgqueue::sndrcvtest1()
       
  2283 	{
       
  2284 	int msq_id=0,len,ret=KErrNone;
       
  2285 	struct msqid_ds buf= {0};
       
  2286 	struct {
       
  2287        long mtype;
       
  2288        char mtext[128];
       
  2289    	} msg_buf,msg_buf1;
       
  2290 
       
  2291 	msg_buf.mtype = 1; // message identifier 
       
  2292     strcpy(msg_buf.mtext, "some_data_to_send"); // data 
       
  2293     len = strlen(msg_buf.mtext)+1;
       
  2294     
       
  2295     if ((msq_id = msgget(MESSAGE_Q_KEY1, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2296 		{
       
  2297 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2298       	INFO_PRINTF2(KFunc, errno);
       
  2299        	ret= KErrGeneral;
       
  2300     	}
       
  2301     	
       
  2302 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, IPC_NOWAIT)  != 0) 
       
  2303     	{
       
  2304 		_LIT(KFunc, "msgsnd failed with errno %d\n");
       
  2305       	INFO_PRINTF2(KFunc,errno);
       
  2306       	ret = KErrGeneral;
       
  2307     	}
       
  2308     
       
  2309     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf1, len-1,1, MSG_NOERROR) == -1)) 
       
  2310 		{
       
  2311 		_LIT(KFunc, "msgsnd failed in setting errno %d\n");
       
  2312       	INFO_PRINTF2(KFunc,errno);
       
  2313        	return KErrGeneral;
       
  2314     	}
       
  2315     
       
  2316     if(msg_buf1.mtype != 1)
       
  2317     	{
       
  2318     	return KErrGeneral;
       
  2319     	}
       
  2320     if( (strcmp(msg_buf1.mtext,msg_buf.mtext)) )
       
  2321     	{
       
  2322     	return KErrGeneral;
       
  2323     	}
       
  2324     if ( buf.msg_qnum != 0 )
       
  2325         {
       
  2326         ret= KErrGeneral;
       
  2327         }
       
  2328     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2329     	{
       
  2330 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2331       	INFO_PRINTF2(KFunc, errno);
       
  2332        	ret= KErrGeneral;
       
  2333     	}
       
  2334 	return ret;
       
  2335 	}*/
       
  2336 	
       
  2337 // If msgtyp is 0, the first message on the queue shall be received.
       
  2338 TInt CTestMsgqueue::sndrcvtest2()
       
  2339 	{
       
  2340 	int msq_id=0,len1,len2,ret=KErrNone;
       
  2341 	struct msqid_ds buf= {0};
       
  2342 	struct {
       
  2343        long mtype;
       
  2344        char mtext[128];
       
  2345    	} msg_buf,msg_buf1,msg_buf2;
       
  2346 
       
  2347 	msg_buf.mtype = 1; /* message identifier */
       
  2348     strcpy(msg_buf.mtext, "some_data_to_send1"); /* data */
       
  2349     len1 = strlen(msg_buf.mtext)+1;
       
  2350     
       
  2351     msg_buf1.mtype = 2; /* message identifier */
       
  2352     strcpy(msg_buf1.mtext, "some_data_to_send2"); /* data */
       
  2353     len2 = strlen(msg_buf1.mtext)+1;
       
  2354     
       
  2355     if ((msq_id = msgget(MESSAGE_Q_KEY1, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2356 		{
       
  2357 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2358       	INFO_PRINTF2(KFunc, errno);
       
  2359        	ret= KErrGeneral;
       
  2360     	}
       
  2361     	
       
  2362 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len1, IPC_NOWAIT)  != 0) 
       
  2363     	{
       
  2364 		_LIT(KFunc, "msgsnd1 failed with errno %d\n");
       
  2365       	INFO_PRINTF2(KFunc,errno);
       
  2366       	ret = KErrGeneral;
       
  2367     	}
       
  2368     
       
  2369     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf1, len2, IPC_NOWAIT)  != 0) 
       
  2370     	{
       
  2371 		_LIT(KFunc, "msgsnd2 failed with errno %d\n");
       
  2372       	INFO_PRINTF2(KFunc,errno);
       
  2373       	ret = KErrGeneral;
       
  2374     	}
       
  2375     	
       
  2376     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf2, 100,0, 0) == -1)) 
       
  2377 		{
       
  2378 		_LIT(KFunc, "msgsnd failed in setting errno %d\n");
       
  2379       	INFO_PRINTF2(KFunc,errno);
       
  2380        	return KErrGeneral;
       
  2381     	}
       
  2382     
       
  2383     if (msgctl(msq_id, IPC_STAT, &buf) == -1) 
       
  2384     	{
       
  2385 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2386       	INFO_PRINTF2(KFunc, errno);
       
  2387        	return KErrGeneral;
       
  2388     	}
       
  2389     	
       
  2390     if(msg_buf2.mtype != 1)
       
  2391     	{
       
  2392     	return KErrGeneral;
       
  2393     	}
       
  2394     if( (strcmp(msg_buf2.mtext,msg_buf.mtext)) )
       
  2395     	{
       
  2396     	return KErrGeneral;
       
  2397     	}
       
  2398     if ( buf.msg_qnum != 1 )
       
  2399         {
       
  2400         ret= KErrGeneral;
       
  2401         }
       
  2402     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2403     	{
       
  2404 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2405       	INFO_PRINTF2(KFunc, errno);
       
  2406        	ret= KErrGeneral;
       
  2407     	}
       
  2408 	return ret;
       
  2409 	}
       
  2410 	
       
  2411 // If msgtyp is greater than 0, the first message of type msgtyp shall be received.
       
  2412 
       
  2413 TInt CTestMsgqueue::sndrcvtest3()
       
  2414 	{
       
  2415 	int msq_id=0,len1,len2,len3,ret=KErrNone;
       
  2416 	
       
  2417 	struct {
       
  2418        long mtype;
       
  2419        char mtext[128];
       
  2420    	} msg_buf,msg_buf1,msg_buf2,msg_buf3;
       
  2421 
       
  2422 	msg_buf.mtype = 1; /* message identifier */
       
  2423     strcpy(msg_buf.mtext, "some_data_to_send1"); /* data */
       
  2424     len1 = strlen(msg_buf.mtext)+1;
       
  2425     
       
  2426     msg_buf1.mtype = 2; /* message identifier */
       
  2427     strcpy(msg_buf1.mtext, "some_data_to_send2"); /* data */
       
  2428     len2 = strlen(msg_buf1.mtext)+1;
       
  2429     
       
  2430     msg_buf3.mtype = 2; /* message identifier */
       
  2431     strcpy(msg_buf3.mtext, "some_data_to_send3"); /* data */
       
  2432     len3 = strlen(msg_buf3.mtext)+1;
       
  2433     
       
  2434     if ((msq_id = msgget(MESSAGE_Q_KEY1, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2435 		{
       
  2436 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2437       	INFO_PRINTF2(KFunc, errno);
       
  2438        	ret= KErrGeneral;
       
  2439     	}
       
  2440     	
       
  2441 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len1, IPC_NOWAIT)  != 0) 
       
  2442     	{
       
  2443 		_LIT(KFunc, "msgsnd1 failed with errno %d\n");
       
  2444       	INFO_PRINTF2(KFunc,errno);
       
  2445       	ret = KErrGeneral;
       
  2446     	}
       
  2447     
       
  2448     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf1, len2, IPC_NOWAIT)  != 0) 
       
  2449     	{
       
  2450 		_LIT(KFunc, "msgsnd2 failed with errno %d\n");
       
  2451       	INFO_PRINTF2(KFunc,errno);
       
  2452       	ret = KErrGeneral;
       
  2453     	}
       
  2454     
       
  2455     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf3, len3, IPC_NOWAIT)  != 0) 
       
  2456     	{
       
  2457 		_LIT(KFunc, "msgsnd3 failed with errno %d\n");
       
  2458       	INFO_PRINTF2(KFunc,errno);
       
  2459       	ret = KErrGeneral;
       
  2460     	}
       
  2461     	
       
  2462     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf2, 100,2, 0) == -1)) 
       
  2463 		{
       
  2464 		_LIT(KFunc, "msgsnd failed in setting errno %d\n");
       
  2465       	INFO_PRINTF2(KFunc,errno);
       
  2466        	return KErrGeneral;
       
  2467     	}
       
  2468     
       
  2469     if(msg_buf2.mtype != 2)
       
  2470     	{
       
  2471     	return KErrGeneral;
       
  2472     	}
       
  2473     if( (strcmp(msg_buf2.mtext,msg_buf1.mtext)) )
       
  2474     	{
       
  2475     	return KErrGeneral;
       
  2476     	}
       
  2477 
       
  2478     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2479     	{
       
  2480 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2481       	INFO_PRINTF2(KFunc, errno);
       
  2482        	ret= KErrGeneral;
       
  2483     	}
       
  2484 	return ret;
       
  2485 	}
       
  2486 
       
  2487 // If msgtyp is less than 0, the first message of the lowest type that is 
       
  2488 // less than or equal to the absolute value of msgtyp shall be received.
       
  2489 
       
  2490 TInt CTestMsgqueue::sndrcvtest4()
       
  2491 	{
       
  2492 	int msq_id=0,len1,len2,len3,ret=KErrNone;
       
  2493 
       
  2494 	struct {
       
  2495        long mtype;
       
  2496        char mtext[128];
       
  2497    	} msg_buf,msg_buf1,msg_buf2,msg_buf3;
       
  2498 
       
  2499 	msg_buf.mtype = 3; /* message identifier */
       
  2500     strcpy(msg_buf.mtext, "some_data_to_send1"); /* data */
       
  2501     len1 = strlen(msg_buf.mtext)+1;
       
  2502     
       
  2503     msg_buf1.mtype = 2; /* message identifier */
       
  2504     strcpy(msg_buf1.mtext, "some_data_to_send2"); /* data */
       
  2505     len2 = strlen(msg_buf1.mtext)+1;
       
  2506     
       
  2507     msg_buf3.mtype = 1; /* message identifier */
       
  2508     strcpy(msg_buf3.mtext, "some_data_to_send3"); /* data */
       
  2509     len3 = strlen(msg_buf3.mtext)+1;
       
  2510     
       
  2511     if ((msq_id = msgget(MESSAGE_Q_KEY1, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2512 		{
       
  2513 		_LIT(KFunc, "Message Q create failed with errno %d\n");
       
  2514       	INFO_PRINTF2(KFunc, errno);
       
  2515        	ret= KErrGeneral;
       
  2516     	}
       
  2517     	
       
  2518 	if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len1, IPC_NOWAIT)  != 0) 
       
  2519     	{
       
  2520 		_LIT(KFunc, "msgsnd1 failed with errno %d\n");
       
  2521       	INFO_PRINTF2(KFunc,errno);
       
  2522       	ret = KErrGeneral;
       
  2523     	}
       
  2524     
       
  2525     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf1, len2, IPC_NOWAIT)  != 0) 
       
  2526     	{
       
  2527 		_LIT(KFunc, "msgsnd2 failed with errno %d\n");
       
  2528       	INFO_PRINTF2(KFunc,errno);
       
  2529       	ret = KErrGeneral;
       
  2530     	}
       
  2531     
       
  2532     if (msgsnd(msq_id, (struct msgbuf *)&msg_buf3, len3, IPC_NOWAIT)  != 0) 
       
  2533     	{
       
  2534 		_LIT(KFunc, "msgsnd3 failed with errno %d\n");
       
  2535       	INFO_PRINTF2(KFunc,errno);
       
  2536       	ret = KErrGeneral;
       
  2537     	}
       
  2538     	
       
  2539     if ((msgrcv(msq_id, (struct msgbuf *)&msg_buf2, 100,-2, 0) == -1)) 
       
  2540 		{
       
  2541 		_LIT(KFunc, "msgsnd failed in setting errno %d\n");
       
  2542       	INFO_PRINTF2(KFunc,errno);
       
  2543        	return KErrGeneral;
       
  2544     	}
       
  2545     
       
  2546     if(msg_buf2.mtype != 1)
       
  2547     	{
       
  2548     	return KErrGeneral;
       
  2549     	}
       
  2550     if( (strcmp(msg_buf2.mtext,msg_buf3.mtext)) )
       
  2551     	{
       
  2552     	return KErrGeneral;
       
  2553     	}/*
       
  2554     if ( buf.msg_qnum != 2 )
       
  2555         {
       
  2556         ret= KErrGeneral;
       
  2557         }*/
       
  2558     if (msgctl(msq_id, IPC_RMID, NULL) == -1) 
       
  2559     	{
       
  2560 		_LIT(KFunc, "Message Q delete failed with errno %d\n");
       
  2561       	INFO_PRINTF2(KFunc, errno);
       
  2562        	ret= KErrGeneral;
       
  2563     	}
       
  2564 	return ret;
       
  2565 	}
       
  2566 
       
  2567 						// THREADS
       
  2568 						
       
  2569 TInt MsgQueueGet()
       
  2570     {
       
  2571 	int msgq_id;
       
  2572     if ((msgq_id = msgget(MESSAGE_Q_KEY_TH, IPC_CREAT | IPC_EXCL | 0666)) == -1) 
       
  2573 		{
       
  2574 		return 0;
       
  2575     	}
       
  2576     return msgq_id;
       
  2577     }
       
  2578 
       
  2579 void* ThreadEntryRead(void* arg)
       
  2580 	{    
       
  2581     TInt msgType;
       
  2582     TInt msqFlag = 0;
       
  2583     message_buf buf = {0};
       
  2584     TInt msgLen = 128;
       
  2585     
       
  2586     msgType = 0; //any message
       
  2587     msqFlag =  ~IPC_NOWAIT; //Blocking
       
  2588     int msgq_id = (int)arg;
       
  2589     int err = msgrcv(msgq_id, &buf, msgLen, msgType, msqFlag);
       
  2590     
       
  2591     if(buf.mtype != 1)
       
  2592     	{
       
  2593     	exit(1);
       
  2594     	}
       
  2595     if( (strcmp(buf.mtext,"some_data_to_send")) )
       
  2596     	{
       
  2597     	exit(1);
       
  2598     	}
       
  2599     return NULL;
       
  2600 	}
       
  2601 
       
  2602 TInt MsgQueueThread( int id )
       
  2603 	{
       
  2604 
       
  2605     TInt err = KErrNone;
       
  2606     int threadRetVal = 0;
       
  2607 	void *threadRetValPtr = (void*)&threadRetVal;
       
  2608     pthread_t testThread;
       
  2609     err = pthread_create(&testThread,(pthread_attr_t *)NULL,ThreadEntryRead,(TAny*)id);
       
  2610     if (!err)
       
  2611     	{
       
  2612     	sleep(5);
       
  2613         err = MsgQueueSend(id);
       
  2614         }
       
  2615     pthread_join(testThread, &threadRetValPtr);        
       
  2616     return err;
       
  2617 	}
       
  2618 
       
  2619 TInt MsgQueueSend(int msgq_id)
       
  2620 	{
       
  2621 	
       
  2622     TInt len;
       
  2623     
       
  2624     struct {
       
  2625        long mtype;
       
  2626        char mtext[128];
       
  2627    	} msg_buf;
       
  2628 
       
  2629 	msg_buf.mtype = 1; /* message identifier */
       
  2630     strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
       
  2631     len = strlen(msg_buf.mtext)+1;
       
  2632     
       
  2633     if (msgsnd(msgq_id, (struct msgbuf *)&msg_buf, len, 0)  != 0) 
       
  2634     	{
       
  2635       	return -1;
       
  2636     	}
       
  2637     
       
  2638     return 0;
       
  2639 	}
       
  2640 	
       
  2641 TInt MsgQueueDelete(int msgq_id)
       
  2642 	{
       
  2643 	User::After(100000);
       
  2644 	if (msgctl(msgq_id, IPC_RMID, NULL) == -1) 
       
  2645     	{
       
  2646        	return -1;
       
  2647        	}
       
  2648     return 0;
       
  2649     	
       
  2650     }
       
  2651 	
       
  2652 TInt CTestMsgqueue::thsndrcvtest1()
       
  2653 	{
       
  2654 	
       
  2655 	int err=0,id;
       
  2656 	
       
  2657 	id = MsgQueueGet();
       
  2658 	
       
  2659 	if(id)
       
  2660 		{
       
  2661 		
       
  2662 		err = MsgQueueThread(id);
       
  2663 		if(!err)
       
  2664 			{
       
  2665 			err = MsgQueueDelete(id);
       
  2666 			if(!err)
       
  2667 				{
       
  2668 				return KErrNone;
       
  2669 				}
       
  2670 			}
       
  2671 		}
       
  2672 		
       
  2673 	return KErrGeneral;
       
  2674 	}