Sync code with Tizen 3.0 branch
[platform/core/connectivity/mtp-responder.git] / src / util / mtp_msgq.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sys/types.h>
18 #include <sys/ipc.h>
19 #include <sys/msg.h>
20 #include "mtp_msgq.h"
21
22 /*
23  * FUNCTIONS
24  */
25 mtp_bool _util_msgq_init(msgq_id_t *mq_id, mtp_uint32 flags)
26 {
27         *mq_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
28         if (*mq_id == -1) {
29                 ERR("msgget() Fail");
30                 _util_print_error();
31                 return FALSE;
32         }
33         return TRUE;
34 }
35
36 mtp_bool _util_msgq_send(msgq_id_t mq_id, void *buf, mtp_uint32 size,
37                 mtp_uint32 flags)
38 {
39         if (-1 == msgsnd(mq_id, buf, size, flags)) {
40                 ERR("msgsnd() Fail : mq_id = [%d]", mq_id);
41                 _util_print_error();
42                 return FALSE;
43         }
44         return TRUE;
45 }
46
47 mtp_bool _util_msgq_receive(msgq_id_t mq_id, void *buf, mtp_uint32 size,
48                 mtp_uint32 flags, mtp_int32 *nbytes)
49 {
50         int ret = 0;
51
52         if (flags == 1)
53                 ret = msgrcv(mq_id, buf, size, 0, IPC_NOWAIT);
54         else
55                 ret = msgrcv(mq_id, buf, size, 0, 0);
56
57         if (ret == -1) {
58                 ERR("msgrcv() Fail");
59                 _util_print_error();
60                 *nbytes = 0;
61                 return FALSE;
62         }
63
64         *nbytes = ret;
65         return TRUE;
66 }
67
68 mtp_bool _util_msgq_deinit(msgq_id_t *msgq_id)
69 {
70         if (-1 == msgctl(*msgq_id, IPC_RMID, 0)) {
71                 ERR("msgctl(IPC_RMID) Fail");
72                 return FALSE;
73         }
74         return TRUE;
75 }
76
77 mtp_bool _util_msgq_set_size(msgq_id_t mq_id, mtp_uint32 nbytes)
78 {
79         struct msqid_ds attr;
80
81         /* Getting the attributes of Message Queue */
82         if (msgctl(mq_id, MSG_STAT, &attr) == -1) {
83                 ERR("msgctl(MSG_STAT) Fail");
84                 return FALSE;
85         }
86
87         attr.msg_qbytes = nbytes;
88
89         /* Setting the message queue size */
90         if (msgctl(mq_id, IPC_SET, &attr) == -1) {
91                 ERR("msgctl(IPC_SET) Fail");
92                 return FALSE;
93         }
94         return TRUE;
95 }
96
97 /*
98  * _util_rcv_msg_from_mq
99  *
100  * This function receives a message pointer from Message Queue
101  * @param[in]   mq_id   Message Queue Id
102  * @param[out]  pkt     Shall be assigned with received buffer ptr
103  * @param[out]  pkt_len Will hold the length of received data
104  * @param[out]  mtype   Will be assigned the type of message received
105  */
106 mtp_bool _util_rcv_msg_from_mq(msgq_id_t mq_id, unsigned char **pkt,
107                 mtp_uint32 *pkt_len, msg_type_t *mtype)
108 {
109         msgq_ptr_t msg = { 0 };
110         mtp_int32 nbytes = 0;
111
112         if (FALSE == _util_msgq_receive(mq_id, (void *)&msg,
113                                 sizeof(msgq_ptr_t) - sizeof(long), 0, &nbytes)) {
114                 ERR("_util_msgq_receive() Fail : mq_id = [%d]", mq_id);
115                 return FALSE;
116         }
117
118         *pkt_len = msg.length;
119         *pkt = msg.buffer;
120         *mtype = msg.mtype;
121
122         return TRUE;
123 }