[cleanup]revise file location and name
[platform/core/connectivity/mtp-responder.git] / src / mtp_util_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_util_msgq.h"
21
22 /*
23  * FUNCTIONS
24  */
25 /* LCOV_EXCL_START */
26 mtp_bool _util_msgq_init(msgq_id_t *mq_id, mtp_uint32 flags)
27 {
28         *mq_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
29         if (*mq_id == -1) {
30                 ERR("msgget() Fail");
31                 _util_print_error();
32                 return FALSE;
33         }
34         return TRUE;
35 }
36
37 mtp_bool _util_msgq_send(msgq_id_t mq_id, void *buf, mtp_uint32 size,
38                 mtp_uint32 flags)
39 {
40         if (-1 == msgsnd(mq_id, buf, size, flags)) {
41                 ERR("msgsnd() Fail : mq_id = [%d]", mq_id);
42                 _util_print_error();
43                 return FALSE;
44         }
45         return TRUE;
46 }
47
48 mtp_bool _util_msgq_receive(msgq_id_t mq_id, void *buf, mtp_uint32 size,
49                 mtp_uint32 flags, mtp_int32 *nbytes)
50 {
51         int ret = 0;
52
53         if (flags == 1)
54                 ret = msgrcv(mq_id, buf, size, 0, IPC_NOWAIT);
55         else
56                 ret = msgrcv(mq_id, buf, size, 0, 0);
57
58         if (ret == -1) {
59                 ERR("msgrcv() Fail");
60                 _util_print_error();
61                 *nbytes = 0;
62                 return FALSE;
63         }
64
65         *nbytes = ret;
66         return TRUE;
67 }
68
69 mtp_bool _util_msgq_deinit(msgq_id_t *msgq_id)
70 {
71         if (-1 == msgctl(*msgq_id, IPC_RMID, 0)) {
72                 ERR("msgctl(IPC_RMID) Fail");
73                 return FALSE;
74         }
75         return TRUE;
76 }
77
78 mtp_bool _util_msgq_set_size(msgq_id_t mq_id, mtp_uint32 nbytes)
79 {
80         struct msqid_ds attr;
81
82         /* Getting the attributes of Message Queue */
83         if (msgctl(mq_id, MSG_STAT, &attr) == -1) {
84                 ERR("msgctl(MSG_STAT) Fail");
85                 return FALSE;
86         }
87
88         attr.msg_qbytes = nbytes;
89
90         /* Setting the message queue size */
91         if (msgctl(mq_id, IPC_SET, &attr) == -1) {
92                 ERR("msgctl(IPC_SET) Fail");
93                 return FALSE;
94         }
95         return TRUE;
96 }
97
98 /*
99  * _util_rcv_msg_from_mq
100  *
101  * This function receives a message pointer from Message Queue
102  * @param[in]   mq_id   Message Queue Id
103  * @param[out]  pkt     Shall be assigned with received buffer ptr
104  * @param[out]  pkt_len Will hold the length of received data
105  * @param[out]  mtype   Will be assigned the type of message received
106  */
107 mtp_bool _util_rcv_msg_from_mq(msgq_id_t mq_id, unsigned char **pkt,
108                 mtp_uint32 *pkt_len, msg_type_t *mtype)
109 {
110         msgq_ptr_t msg = { 0 };
111         mtp_int32 nbytes = 0;
112
113         if (FALSE == _util_msgq_receive(mq_id, (void *)&msg,
114                                 sizeof(msgq_ptr_t) - sizeof(long), 0, &nbytes)) {
115                 ERR("_util_msgq_receive() Fail : mq_id = [%d]", mq_id);
116                 return FALSE;
117         }
118
119         *pkt_len = msg.length;
120         *pkt = msg.buffer;
121         *mtype = msg.mtype;
122
123         return TRUE;
124 }
125 /* LCOV_EXCL_STOP */