1/* Copyright (c) 2011, 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#include <linux/stat.h>
30#include <fcntl.h>
31
32#include <linux/types.h>
33
34#include "log_util.h"
35#include "platform_lib_includes.h"
36#include "loc_eng_dmn_conn_glue_msg.h"
37#include "loc_eng_dmn_conn_handler.h"
38
39/*===========================================================================
40FUNCTION    loc_eng_dmn_conn_glue_msgget
41
42DESCRIPTION
43   This function get a message queue
44
45   q_path - name path of the message queue
46   mode -
47
48DEPENDENCIES
49   None
50
51RETURN VALUE
52   message queue id
53
54SIDE EFFECTS
55   N/A
56
57===========================================================================*/
58int loc_eng_dmn_conn_glue_msgget(const char * q_path, int mode)
59{
60    int msgqid;
61    msgqid = loc_eng_dmn_conn_glue_pipeget(q_path, mode);
62    return msgqid;
63}
64
65/*===========================================================================
66FUNCTION    loc_eng_dmn_conn_glue_msgremove
67
68DESCRIPTION
69   remove a message queue
70
71   q_path - name path of the message queue
72   msgqid - message queue id
73
74DEPENDENCIES
75   None
76
77RETURN VALUE
78   0: success or negative value for failure
79
80SIDE EFFECTS
81   N/A
82
83===========================================================================*/
84int loc_eng_dmn_conn_glue_msgremove(const char * q_path, int msgqid)
85{
86    int result;
87    result = loc_eng_dmn_conn_glue_piperemove(q_path, msgqid);
88    return result;
89}
90
91/*===========================================================================
92FUNCTION    loc_eng_dmn_conn_glue_msgsnd
93
94DESCRIPTION
95   Send a message
96
97   msgqid - message queue id
98   msgp - pointer to the message to be sent
99   msgsz - size of the message
100
101DEPENDENCIES
102   None
103
104RETURN VALUE
105   number of bytes sent out or negative value for failure
106
107SIDE EFFECTS
108   N/A
109
110===========================================================================*/
111int loc_eng_dmn_conn_glue_msgsnd(int msgqid, const void * msgp, size_t msgsz)
112{
113    int result;
114    struct ctrl_msgbuf *pmsg = (struct ctrl_msgbuf *) msgp;
115    pmsg->msgsz = msgsz;
116
117    result = loc_eng_dmn_conn_glue_pipewrite(msgqid, msgp, msgsz);
118    if (result != (int) msgsz) {
119        LOC_LOGE("%s:%d] pipe broken %d, msgsz = %d\n", __func__, __LINE__, result, (int) msgsz);
120        return -1;
121    }
122
123    return result;
124}
125
126/*===========================================================================
127FUNCTION    loc_eng_dmn_conn_glue_msgrcv
128
129DESCRIPTION
130   receive a message
131
132   msgqid - message queue id
133   msgp - pointer to the buffer to hold the message
134   msgsz - size of the buffer
135
136DEPENDENCIES
137   None
138
139RETURN VALUE
140   number of bytes received or negative value for failure
141
142SIDE EFFECTS
143   N/A
144
145===========================================================================*/
146int loc_eng_dmn_conn_glue_msgrcv(int msgqid, void *msgp, size_t msgbufsz)
147{
148    int result;
149    struct ctrl_msgbuf *pmsg = (struct ctrl_msgbuf *) msgp;
150
151    result = loc_eng_dmn_conn_glue_piperead(msgqid, &(pmsg->msgsz), sizeof(pmsg->msgsz));
152    if (result != sizeof(pmsg->msgsz)) {
153        LOC_LOGE("%s:%d] pipe broken %d\n", __func__, __LINE__, result);
154        return -1;
155    }
156
157    if (msgbufsz < pmsg->msgsz) {
158        LOC_LOGE("%s:%d] msgbuf is too small %d < %d\n", __func__, __LINE__, (int) msgbufsz, (int) pmsg->msgsz);
159        return -1;
160    }
161
162    result = loc_eng_dmn_conn_glue_piperead(msgqid, (uint8_t *) msgp + sizeof(pmsg->msgsz), pmsg->msgsz - sizeof(pmsg->msgsz));
163    if (result != (int) (pmsg->msgsz - sizeof(pmsg->msgsz))) {
164        LOC_LOGE("%s:%d] pipe broken %d, msgsz = %d\n", __func__, __LINE__, result, (int) pmsg->msgsz);
165        return -1;
166    }
167
168    return pmsg->msgsz;
169}
170
171/*===========================================================================
172FUNCTION    loc_eng_dmn_conn_glue_msgunblock
173
174DESCRIPTION
175   unblock a message queue
176
177   msgqid - message queue id
178
179DEPENDENCIES
180   None
181
182RETURN VALUE
183   0: success
184
185SIDE EFFECTS
186   N/A
187
188===========================================================================*/
189int loc_eng_dmn_conn_glue_msgunblock(int msgqid)
190{
191    return loc_eng_dmn_conn_glue_pipeunblock(msgqid);
192}
193
194/*===========================================================================
195FUNCTION    loc_eng_dmn_conn_glue_msgflush
196
197DESCRIPTION
198   flush out the message in a queue
199
200   msgqid - message queue id
201
202DEPENDENCIES
203   None
204
205RETURN VALUE
206   number of bytes that are flushed out.
207
208SIDE EFFECTS
209   N/A
210
211===========================================================================*/
212int loc_eng_dmn_conn_glue_msgflush(int msgqid)
213{
214    int length;
215    char buf[128];
216
217    do {
218        length = loc_eng_dmn_conn_glue_piperead(msgqid, buf, 128);
219        LOC_LOGD("%s:%d] %s\n", __func__, __LINE__, buf);
220    } while(length);
221    return length;
222}
223
224