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