4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include "user_request.h"
29 #include "communicator.h"
31 struct tcore_user_request_type {
34 /* Communicator information */
40 enum tcore_request_command command;
43 unsigned int data_len;
46 unsigned int metainfo_len;
48 UserRequestFreeHook free_hook;
49 GSList *response_hook;
52 struct hook_response_type {
53 UserRequestResponseHook func;
57 UserRequest *tcore_user_request_new(Communicator *comm, const char *modem_name)
61 ur = calloc(1, sizeof(struct tcore_user_request_type));
68 ur->modem_name = strdup(modem_name);
70 ur->modem_name = NULL;
75 void tcore_user_request_free(UserRequest *ur)
85 g_slist_free_full(ur->response_hook, free);
99 dbg("user_request(%p) free.", ur);
104 UserRequest *tcore_user_request_ref(UserRequest *ur)
114 void tcore_user_request_unref(UserRequest *ur)
122 tcore_user_request_free(ur);
127 TReturn tcore_user_request_set_free_hook(UserRequest *ur,
128 UserRequestFreeHook free_hook)
131 return TCORE_RETURN_EINVAL;
133 ur->free_hook = free_hook;
135 return TCORE_RETURN_SUCCESS;
138 TReturn tcore_user_request_set_response_hook(UserRequest *ur,
139 UserRequestResponseHook resp_hook, void *user_data)
141 struct hook_response_type *hook;
144 return TCORE_RETURN_EINVAL;
146 hook = calloc(1, sizeof(struct hook_response_type));
148 return TCORE_RETURN_ENOMEM;
150 hook->func = resp_hook;
151 hook->user_data = user_data;
153 ur->response_hook = g_slist_append(ur->response_hook, hook);
155 return TCORE_RETURN_SUCCESS;
158 Communicator *tcore_user_request_ref_communicator(UserRequest *ur)
166 char *tcore_user_request_get_modem_name(UserRequest *ur)
174 return strdup(ur->modem_name);
177 TReturn tcore_user_request_set_user_info(UserRequest *ur, void *user_info)
180 return TCORE_RETURN_EINVAL;
182 ur->user_info = user_info;
184 return TCORE_RETURN_SUCCESS;
187 void *tcore_user_request_ref_user_info(UserRequest *ur)
192 return ur->user_info;
195 TReturn tcore_user_request_send_response(UserRequest *ur,
196 enum tcore_response_command command,
197 unsigned int data_len, const void *data)
200 struct hook_response_type *hook;
204 return TCORE_RETURN_EINVAL;
207 for (list = ur->response_hook; list;) {
215 hook->func(ur, command, data_len, data, hook->user_data);
219 return tcore_communicator_send_response(ur->comm, ur,
220 command, data_len, data);
223 return TCORE_RETURN_SUCCESS;
226 TReturn tcore_user_request_set_command(UserRequest *ur,
227 enum tcore_request_command command)
230 return TCORE_RETURN_EINVAL;
232 ur->command = command;
234 return TCORE_RETURN_SUCCESS;
237 enum tcore_request_command tcore_user_request_get_command(UserRequest *ur)
245 TReturn tcore_user_request_set_data(UserRequest *ur,
246 unsigned int data_len, const void *data)
249 return TCORE_RETURN_EINVAL;
251 if (ur->data != NULL) {
256 ur->data_len = data_len;
260 ur->data = calloc(data_len, 1);
263 return TCORE_RETURN_ENOMEM;
266 memcpy(ur->data, data, data_len);
272 return TCORE_RETURN_SUCCESS;
275 TReturn tcore_user_request_set_metainfo(UserRequest *ur,
276 unsigned int metainfo_len, const void *metainfo)
279 return TCORE_RETURN_EINVAL;
281 if (ur->metainfo != NULL) {
286 ur->metainfo_len = metainfo_len;
288 if (metainfo_len > 0) {
289 if (metainfo != NULL) {
290 ur->metainfo = calloc(metainfo_len, 1);
292 ur->metainfo_len = 0;
293 return TCORE_RETURN_ENOMEM;
296 memcpy(ur->metainfo, metainfo, metainfo_len);
298 ur->metainfo_len = 0;
302 return TCORE_RETURN_SUCCESS;
305 const void *tcore_user_request_ref_data(UserRequest *ur,
306 unsigned int *data_len)
312 *data_len = ur->data_len;
317 const void *tcore_user_request_ref_metainfo(UserRequest *ur,
318 unsigned int *metainfo_len)
324 *metainfo_len = ur->metainfo_len;