examples/PIPS/opencmessagequeuelibraryex/engine/src/msgqsend.c

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 #include <errno.h>
00031 #include <sys/msg.h>
00032 #include <sys/sem.h>
00033 #include <string.h>
00034 #include "MsgQInternal.h"
00035 
00036 
00037 /*******************************************************************************
00038 * MsgQSend (qName, msg, nBytes, priority, timeout, err)
00039 * Description:  Function for sending a message with internal copy
00040 *********************************************************************************/
00041 
00042 EXPORT_C int MsgQSend(ULONG qName, char* msg, ULONG nBytes, ULONG priority, int timeout, int* err)
00043 {
00044         MSGQ_INFO* pMsgQInfo = NULL;
00045 
00046         struct {
00047                 long mtype;
00048                 char mtext[MAX_MSG_LEN];
00049         } message;
00050 
00051         /* structure used for semaphore post operation */
00052         struct sembuf op;
00053         
00054         /* init the  semop () structure which is used for wait and signal operations */
00055         op.sem_num = 0;
00056         op.sem_op = -1;
00057         op.sem_flg = SEM_UNDO;
00058 
00059         /* check parameters */
00060         if ((priority == MSG_PRI_NORMAL) || (priority == MSG_PRI_URGENT)) {
00061                 if((pMsgQInfo = MsgQTableLookup(qName)) != NULL) {
00062                         if (pMsgQInfo->sendState == MSG_Q_READY) {
00063                                 op.sem_flg = op.sem_flg | timeout;
00064                                 if((semop(pMsgQInfo->semId, &op, 1)) == OK) {
00065                                         pMsgQInfo->numMsgs++;
00066                                         if(pMsgQInfo->maxNumMsgs < pMsgQInfo->numMsgs)          
00067                                         pMsgQInfo->maxNumMsgs = pMsgQInfo->numMsgs;
00068 
00069                                         message.mtype = 1;
00070                                         bcopy(msg, message.mtext, nBytes);
00071                                         message.mtext[nBytes] = '\0';
00072 
00073                                         if(msgsnd (pMsgQInfo->qId, &message, (size_t)nBytes+4, timeout) == OK) {
00074                                                 *err = OK;
00075                                                 /* After successfull send, unlock the message queue by using post operation on semaphore.*/
00076                                                 op.sem_op = 1;
00077                                                 semop(pMsgQInfo->semId, &op, 1);
00078                                                 return (OK);
00079                                         }
00080                                         else {
00081                                                 *err = errno;
00082                                                 pMsgQInfo->numMsgs--;
00083                                                 op.sem_op = 1;
00084                                                 semop(pMsgQInfo->semId, &op, 1);
00085                                         }
00086                                 }
00087                                 else
00088                                         *err = errno;
00089                         }
00090                         else
00091                                 *err = KMsgQLibQFlushErr;
00092                 }
00093                 else
00094                         *err = KMsgQLibQIdErr;
00095         }
00096         else
00097                 *err = KMsgQLibParamErr;
00098 
00099         return(ERROR);
00100 }
00101 
00102 

Generated by  doxygen 1.6.2