libtcore: fix calloc parameters order
[platform/core/telephony/libtcore.git] / src / user_request.c
1 /*
2  * libtcore
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib.h>
26
27 #include "tcore.h"
28 #include "user_request.h"
29 #include "communicator.h"
30
31 struct tcore_user_request_type {
32         int ref;
33         struct tcore_user_info ui;
34
35         Communicator *comm;
36
37         char *modem_name;
38
39         enum tcore_request_command command;
40
41         void *data;
42         unsigned int data_len;
43
44         void *metainfo;
45         unsigned int metainfo_len;
46
47         UserRequestFreeHook free_hook;
48         UserRequestResponseHook response_hook;
49         void *response_hook_user_data;
50 };
51
52 UserRequest *tcore_user_request_new(Communicator *comm, const char *modem_name)
53 {
54         UserRequest *ur;
55
56         ur = calloc(1, sizeof(struct tcore_user_request_type));
57         if (!ur)
58                 return NULL;
59
60         ur->comm = comm;
61
62         if (modem_name)
63                 ur->modem_name = strdup(modem_name);
64         else
65                 ur->modem_name = NULL;
66
67         return ur;
68 }
69
70 void tcore_user_request_free(UserRequest *ur)
71 {
72         if (!ur)
73                 return;
74
75         if (ur->ref > 0) {
76                 ur->ref--;
77                 return;
78         }
79
80         if (ur->free_hook)
81                 ur->free_hook(ur);
82
83         if (ur->modem_name)
84                 free(ur->modem_name);
85
86         if (ur->data)
87                 free(ur->data);
88
89         if(ur->metainfo)
90                 free(ur->metainfo);
91
92         dbg("user_request(0x%x) free.", (unsigned int)ur);
93
94         free(ur);
95 }
96
97 UserRequest *tcore_user_request_ref(UserRequest *ur)
98 {
99         if (!ur)
100                 return NULL;
101
102         ur->ref++;
103
104         return ur;
105 }
106
107 void tcore_user_request_unref(UserRequest *ur)
108 {
109         if (!ur)
110                 return;
111
112         if (ur->ref > 0)
113                 ur->ref--;
114         else
115                 tcore_user_request_free(ur);
116
117         return;
118 }
119
120 TReturn tcore_user_request_set_free_hook(UserRequest *ur,
121                 UserRequestFreeHook free_hook)
122 {
123         if (!ur)
124                 return TCORE_RETURN_EINVAL;
125
126         ur->free_hook = free_hook;
127
128         return TCORE_RETURN_SUCCESS;
129 }
130
131 TReturn tcore_user_request_set_response_hook(UserRequest *ur,
132                 UserRequestResponseHook resp_hook, void *user_data)
133 {
134         if (!ur)
135                 return TCORE_RETURN_EINVAL;
136
137         ur->response_hook = resp_hook;
138         ur->response_hook_user_data = user_data;
139
140         return TCORE_RETURN_SUCCESS;
141 }
142
143 Communicator *tcore_user_request_ref_communicator(UserRequest *ur)
144 {
145         if (!ur)
146                 return NULL;
147
148         return ur->comm;
149 }
150
151 char *tcore_user_request_get_modem_name(UserRequest *ur)
152 {
153         if (!ur)
154                 return NULL;
155
156         if (!ur->modem_name)
157                 return NULL;
158
159         return strdup(ur->modem_name);
160 }
161
162 TReturn tcore_user_request_set_user_info(UserRequest *ur,
163                 const struct tcore_user_info *ui)
164 {
165         if (!ur || !ui)
166                 return TCORE_RETURN_EINVAL;
167
168         ur->ui.uid = ui->uid;
169         ur->ui.gid = ui->gid;
170         ur->ui.pid = ui->pid;
171         ur->ui.channel_id = ui->channel_id;
172         ur->ui.client_cmd = ui->client_cmd;
173         ur->ui.user_data = ui->user_data;
174
175         if (ur->ui.appname) {
176                 dbg("free old appname (%s)", ur->ui.appname);
177                 free(ur->ui.appname);
178                 ur->ui.appname = NULL;
179         }
180
181         if (ui->appname) {
182                 ur->ui.appname = strdup(ui->appname);
183                 dbg("alloc appname(%s, %s)", ur->ui.appname, ui->appname);
184         }
185
186         return TCORE_RETURN_SUCCESS;
187 }
188
189 const struct tcore_user_info *tcore_user_request_ref_user_info(UserRequest *ur)
190 {
191         if (!ur)
192                 return NULL;
193
194         return &(ur->ui);
195 }
196
197 TReturn tcore_user_request_send_response(UserRequest *ur,
198                 enum tcore_response_command command,
199                 unsigned int data_len, const void *data)
200 {
201         if (!ur) {
202                 dbg("ur is NULL");
203                 return TCORE_RETURN_EINVAL;
204         }
205
206         if (ur->response_hook) {
207                 ur->response_hook(ur, command, data_len, data,
208                                 ur->response_hook_user_data);
209         }
210
211         if (ur->comm) {
212                 return tcore_communicator_send_response(ur->comm, ur,
213                                 command, data_len, data);
214         }
215
216         return TCORE_RETURN_SUCCESS;
217 }
218
219 TReturn tcore_user_request_set_command(UserRequest *ur,
220                 enum tcore_request_command command)
221 {
222         if (!ur)
223                 return TCORE_RETURN_EINVAL;
224
225         ur->command = command;
226
227         return TCORE_RETURN_SUCCESS;
228 }
229
230 enum tcore_request_command tcore_user_request_get_command(UserRequest *ur)
231 {
232         if (!ur)
233                 return 0;
234
235         return ur->command;
236 }
237
238 TReturn tcore_user_request_set_data(UserRequest *ur,
239                 unsigned int data_len, const void *data)
240 {
241         if (!ur)
242                 return TCORE_RETURN_EINVAL;
243
244         ur->data_len = data_len;
245
246         if (data_len > 0 && data != NULL) {
247                 ur->data = calloc(data_len, 1);
248                 if (!ur->data)
249                         return TCORE_RETURN_ENOMEM;
250
251                 memcpy(ur->data, data, data_len);
252         }
253         else {
254                 ur->data = NULL;
255         }
256
257         return TCORE_RETURN_SUCCESS;
258 }
259
260 TReturn tcore_user_request_set_metainfo(UserRequest *ur,
261                 unsigned int metainfo_len, const void *metainfo)
262 {
263         if (!ur)
264                 return TCORE_RETURN_EINVAL;
265
266         if (metainfo_len > 0 && metainfo != NULL) {
267                 ur->metainfo = calloc(metainfo_len, 1);
268                 if (!ur->metainfo)
269                         return TCORE_RETURN_ENOMEM;
270
271                 ur->metainfo_len = metainfo_len;
272                 memcpy(ur->metainfo, metainfo, metainfo_len);
273         }
274         else {
275                 ur->metainfo = NULL;
276                 ur->metainfo_len = 0;
277         }
278
279         return TCORE_RETURN_SUCCESS;
280 }
281
282 const void *tcore_user_request_ref_data(UserRequest *ur,
283                 unsigned int *data_len)
284 {
285         if (!ur)
286                 return NULL;
287
288         if (data_len)
289                 *data_len = ur->data_len;
290
291         return ur->data;
292 }
293
294 const void *tcore_user_request_ref_metainfo(UserRequest *ur,
295                 unsigned int *metainfo_len)
296 {
297         if (!ur)
298                 return NULL;
299
300         if (metainfo_len)
301                 *metainfo_len = ur->metainfo_len;
302
303         return ur->metainfo;
304 }