1e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. 2f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * 3f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * Redistribution and use in source and binary forms, with or without 4f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * modification, are permitted provided that the following conditions are 5f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * met: 6f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * * Redistributions of source code must retain the above copyright 7f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * notice, this list of conditions and the following disclaimer. 8f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * * Redistributions in binary form must reproduce the above 9f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * copyright notice, this list of conditions and the following 10f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * disclaimer in the documentation and/or other materials provided 11f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * with the distribution. 12e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * * Neither the name of The Linux Foundation, nor the names of its 13f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * contributors may be used to endorse or promote products derived 14f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * from this software without specific prior written permission. 15f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * 16f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani */ 28f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 29f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include "msg_q.h" 30f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 31f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#define LOG_TAG "LocSvc_utils_q" 32f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include "log_util.h" 33e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include "platform_lib_includes.h" 34f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include "linked_list.h" 35f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include <stdio.h> 36f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include <stdlib.h> 37f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani#include <pthread.h> 38f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 39f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanitypedef struct msg_q { 40f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani void* msg_list; /* Linked list to store information */ 41f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_cond_t list_cond; /* Condition variable for waiting on msg queue */ 42f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_t list_mutex; /* Mutex for exclusive access to message queue */ 43f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani int unblocked; /* Has this message queue been unblocked? */ 44f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} msg_q; 45f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 46f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 47f77c85bb51137f5ba854184e5e9194197027438aAjay DudaniFUNCTION convert_linked_list_err_type 48f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 49f77c85bb51137f5ba854184e5e9194197027438aAjay DudaniDESCRIPTION 50f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani Converts from one set of enum values to another. 51f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 52f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani linked_list_val: Value to convert to msg_q_enum_type 53f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 54f77c85bb51137f5ba854184e5e9194197027438aAjay DudaniDEPENDENCIES 55f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani N/A 56f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 57f77c85bb51137f5ba854184e5e9194197027438aAjay DudaniRETURN VALUE 58f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani Corresponding linked_list_enum_type in msg_q_enum_type 59f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 60f77c85bb51137f5ba854184e5e9194197027438aAjay DudaniSIDE EFFECTS 61f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani N/A 62f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 63f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani===========================================================================*/ 64f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanistatic msq_q_err_type convert_linked_list_err_type(linked_list_err_type linked_list_val) 65f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 66f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani switch( linked_list_val ) 67f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 68f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_SUCCESS: 69f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_SUCCESS; 70f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_INVALID_PARAMETER: 71f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_PARAMETER; 72f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_INVALID_HANDLE: 73f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 74f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_UNAVAILABLE_RESOURCE: 75f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_UNAVAILABLE_RESOURCE; 76f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_INSUFFICIENT_BUFFER: 77f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INSUFFICIENT_BUFFER; 78f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 79f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani case eLINKED_LIST_FAILURE_GENERAL: 80f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani default: 81f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_FAILURE_GENERAL; 82f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 83f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 84f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 85f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/* ----------------------- END INTERNAL FUNCTIONS ---------------------------------------- */ 86f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 87f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 88f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 89f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_init 90f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 91f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 92f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_init(void** msg_q_data) 93f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 94f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_q_data == NULL ) 95f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 96f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 97f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_PARAMETER; 98f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 99f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 100f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* tmp_msg_q; 101f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani tmp_msg_q = (msg_q*)calloc(1, sizeof(msg_q)); 102f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( tmp_msg_q == NULL ) 103f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 104f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Unable to allocate space for message queue!\n", __FUNCTION__); 105f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_FAILURE_GENERAL; 106f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 107f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 108f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( linked_list_init(&tmp_msg_q->msg_list) != 0 ) 109f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 110f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Unable to initialize storage list!\n", __FUNCTION__); 111f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani free(tmp_msg_q); 112f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_FAILURE_GENERAL; 113f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 114f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 115f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( pthread_mutex_init(&tmp_msg_q->list_mutex, NULL) != 0 ) 116f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 117f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Unable to initialize list mutex!\n", __FUNCTION__); 118f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani linked_list_destroy(&tmp_msg_q->msg_list); 119f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani free(tmp_msg_q); 120f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_FAILURE_GENERAL; 121f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 122f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 123f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( pthread_cond_init(&tmp_msg_q->list_cond, NULL) != 0 ) 124f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 125f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Unable to initialize msg q cond var!\n", __FUNCTION__); 126f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani linked_list_destroy(&tmp_msg_q->msg_list); 127f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_destroy(&tmp_msg_q->list_mutex); 128f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani free(tmp_msg_q); 129f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_FAILURE_GENERAL; 130f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 131f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 132f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani tmp_msg_q->unblocked = 0; 133f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 134f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani *msg_q_data = tmp_msg_q; 135f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 136f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_SUCCESS; 137f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 138f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 139f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 140f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 141e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo FUNCTION: msg_q_init2 142e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo 143e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo ===========================================================================*/ 144e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russoconst void* msg_q_init2() 145e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo{ 146e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo void* q = NULL; 147e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo if (eMSG_Q_SUCCESS != msg_q_init(&q)) { 148e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo q = NULL; 149e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo } 150e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo return q; 151e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo} 152e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo 153e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo/*=========================================================================== 154e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo 155f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_destroy 156f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 157f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 158f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_destroy(void** msg_q_data) 159f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 160f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_q_data == NULL ) 161f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 162f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 163f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 164f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 165f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 166f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* p_msg_q = (msg_q*)*msg_q_data; 167f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 168f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani linked_list_destroy(&p_msg_q->msg_list); 169f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_destroy(&p_msg_q->list_mutex); 170f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_cond_destroy(&p_msg_q->list_cond); 171f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 172f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani p_msg_q->unblocked = 0; 173f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 174f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani free(*msg_q_data); 175f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani *msg_q_data = NULL; 176f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 177f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_SUCCESS; 178f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 179f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 180f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 181f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 182f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_snd 183f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 184f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 185f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_snd(void* msg_q_data, void* msg_obj, void (*dealloc)(void*)) 186f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 187f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msq_q_err_type rv; 188f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_q_data == NULL ) 189f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 190f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 191f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 192f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 193f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_obj == NULL ) 194f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 195f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_obj parameter!\n", __FUNCTION__); 196f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_PARAMETER; 197f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 198f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 199f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* p_msg_q = (msg_q*)msg_q_data; 200f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 201f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_lock(&p_msg_q->list_mutex); 202bfff6343845ad9ff062c5fd97bb3b9be1053340eDante Russo LOC_LOGD("%s: Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj); 203f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 204f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( p_msg_q->unblocked ) 205f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 206f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__); 207f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 208f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_UNAVAILABLE_RESOURCE; 209f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 210f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 211f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani rv = convert_linked_list_err_type(linked_list_add(p_msg_q->msg_list, msg_obj, dealloc)); 212f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 213f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani /* Show data is in the message queue. */ 214f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_cond_signal(&p_msg_q->list_cond); 215f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 216f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 217f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 218bfff6343845ad9ff062c5fd97bb3b9be1053340eDante Russo LOC_LOGD("%s: Finished Sending message with handle = 0x%08X\n", __FUNCTION__, msg_obj); 219f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 220f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return rv; 221f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 222f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 223f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 224f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 225f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_rcv 226f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 227f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 228f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_rcv(void* msg_q_data, void** msg_obj) 229f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 230f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msq_q_err_type rv; 231f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_q_data == NULL ) 232f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 233f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 234f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 235f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 236f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 237f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( msg_obj == NULL ) 238f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 239f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_obj parameter!\n", __FUNCTION__); 240f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_PARAMETER; 241f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 242f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 243f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* p_msg_q = (msg_q*)msg_q_data; 244f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 245f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGD("%s: Waiting on message\n", __FUNCTION__); 246f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 247f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_lock(&p_msg_q->list_mutex); 248f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 249f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( p_msg_q->unblocked ) 250f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 251f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__); 252f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 253f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_UNAVAILABLE_RESOURCE; 254f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 255f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 256f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani /* Wait for data in the message queue */ 257f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani while( linked_list_empty(p_msg_q->msg_list) && !p_msg_q->unblocked ) 258f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 259f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_cond_wait(&p_msg_q->list_cond, &p_msg_q->list_mutex); 260f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 261f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 262f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani rv = convert_linked_list_err_type(linked_list_remove(p_msg_q->msg_list, msg_obj)); 263f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 264f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 265f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 266bfff6343845ad9ff062c5fd97bb3b9be1053340eDante Russo LOC_LOGD("%s: Received message 0x%08X rv = %d\n", __FUNCTION__, *msg_obj, rv); 267f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 268f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return rv; 269f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 270f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 271f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 272f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 273f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_flush 274f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 275f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 276f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_flush(void* msg_q_data) 277f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 278f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msq_q_err_type rv; 279f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if ( msg_q_data == NULL ) 280f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 281f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 282f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 283f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 284f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 285f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* p_msg_q = (msg_q*)msg_q_data; 286f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 287f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGD("%s: Flushing Message Queue\n", __FUNCTION__); 288f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 289f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_lock(&p_msg_q->list_mutex); 290f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 291f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani /* Remove all elements from the list */ 292f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani rv = convert_linked_list_err_type(linked_list_flush(p_msg_q->msg_list)); 293f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 294f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 295f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 296f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGD("%s: Message Queue flushed\n", __FUNCTION__); 297f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 298f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return rv; 299f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 300f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 301f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani/*=========================================================================== 302f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 303f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani FUNCTION: msg_q_unblock 304f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 305f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani ===========================================================================*/ 306f77c85bb51137f5ba854184e5e9194197027438aAjay Dudanimsq_q_err_type msg_q_unblock(void* msg_q_data) 307f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani{ 308f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if ( msg_q_data == NULL ) 309f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 310f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Invalid msg_q_data parameter!\n", __FUNCTION__); 311f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_INVALID_HANDLE; 312f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 313f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 314f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani msg_q* p_msg_q = (msg_q*)msg_q_data; 315f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_lock(&p_msg_q->list_mutex); 316f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 317f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani if( p_msg_q->unblocked ) 318f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani { 319f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGE("%s: Message queue has been unblocked.\n", __FUNCTION__); 320f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 321f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_UNAVAILABLE_RESOURCE; 322f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani } 323f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 324f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGD("%s: Unblocking Message Queue\n", __FUNCTION__); 325f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani /* Unblocking message queue */ 326f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani p_msg_q->unblocked = 1; 327f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 328f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani /* Allow all the waiters to wake up */ 329f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_cond_broadcast(&p_msg_q->list_cond); 330f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 331f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani pthread_mutex_unlock(&p_msg_q->list_mutex); 332f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 333f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani LOC_LOGD("%s: Message Queue unblocked\n", __FUNCTION__); 334f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani 335f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani return eMSG_Q_SUCCESS; 336f77c85bb51137f5ba854184e5e9194197027438aAjay Dudani} 337