2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "bluetooth-api.h"
22 #include "bt-internal-types.h"
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
28 GVariant *__g_variant_new_array_split(char *str)
31 g_variant_builder_init(&b, (const GVariantType*) "a{s}");
35 while ((item = strsep(&pos, ",")))
36 g_variant_builder_add(&b, "{s}", item);
38 return g_variant_builder_end(&b);
41 BT_EXPORT_API int bluetooth_map_client_init(void)
43 BT_DBG("bluetooth_map_client_init");
44 bt_user_info_t *user_info;
46 user_info = _bt_get_user_data(BT_COMMON);
47 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
49 return _bt_register_event(BT_MAP_CLIENT_EVENT, user_info->cb, user_info->user_data);
52 BT_EXPORT_API int bluetooth_map_client_deinit(void)
54 BT_DBG("bluetooth_map_client_deinit");
55 return _bt_unregister_event(BT_MAP_CLIENT_EVENT);
58 BT_EXPORT_API int bluetooth_map_client_create_session(
59 bt_map_client_session_info_s *session)
61 BT_DBG("bluetooth_map_client_create_session");
62 int result = BLUETOOTH_ERROR_INTERNAL;
64 BT_CHECK_ENABLED(return);
65 BT_CHECK_PARAMETER(session->remote_address, return);
68 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
70 g_array_append_vals(in_param1, session->remote_address, strlen(session->remote_address)+1);
72 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_CREATE_SESSION,
73 in_param1, in_param2, in_param3, in_param4, &out_param);
75 if (result == BLUETOOTH_ERROR_NONE) {
76 if (out_param->len > 0) {
77 session->session_path = strdup(&g_array_index(out_param, char, 0));
78 BT_DBG("session id: %s", session->session_path);
80 BT_ERR("out_param length is 0!!");
81 return BLUETOOTH_ERROR_INTERNAL;
84 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88 BT_EXPORT_API int bluetooth_map_client_destroy_session(
89 bt_map_client_session_info_s *session)
91 BT_DBG("bluetooth_map_client_destroy_session");
92 int result = BLUETOOTH_ERROR_INTERNAL;
94 BT_CHECK_ENABLED(return);
95 BT_CHECK_PARAMETER(session->session_path, return);
98 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
100 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
102 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_DESTROY_SESSION,
103 in_param1, in_param2, in_param3, in_param4, &out_param);
105 if (result == BLUETOOTH_ERROR_NONE) {
106 BT_DBG("session (%s) was destroyed", session->session_path);
107 free(session->remote_address);
108 free(session->session_path);
111 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
115 BT_EXPORT_API int bluetooth_map_client_set_folder(
116 bt_map_client_session_info_s *session,
119 BT_DBG("bluetooth_map_client_set_folder");
120 int result = BLUETOOTH_ERROR_INTERNAL;
122 BT_CHECK_ENABLED(return);
123 BT_CHECK_PARAMETER(session->session_path, return);
124 BT_CHECK_PARAMETER(name, return);
127 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
129 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path) + 1);
130 g_array_append_vals(in_param2, name, strlen(name) + 1);
132 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_SET_FOLDER,
133 in_param1, in_param2, in_param3, in_param4, &out_param);
135 if (result != BLUETOOTH_ERROR_NONE) {
136 BT_ERR("bluetooth_map_client_set_folder failed");
139 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
144 BT_EXPORT_API int bluetooth_map_client_list_folders(
145 bt_map_client_session_info_s *session,
146 bt_map_client_list_folders_filter_t *filter)
148 BT_DBG("bluetooth_map_list_folders");
151 BT_CHECK_ENABLED(return);
152 BT_CHECK_PARAMETER(session->session_path, return);
154 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
155 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
158 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
161 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
162 if (filter->offset > -1)
163 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
164 if (filter->max_count > -1)
165 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
166 GVariant *filter_variant = g_variant_builder_end(&b);
167 char *filter_serialized = g_variant_print(filter_variant, TRUE);
169 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
170 g_array_append_vals(in_param2, filter_serialized, strlen(filter_serialized)+1);
172 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FOLDERS,
173 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
175 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
177 free(filter_serialized);
182 BT_EXPORT_API int bluetooth_map_client_list_filter_fields(bt_map_client_session_info_s *session)
184 BT_DBG("bluetooth_map_client_list_filter_fields");
185 int result = BLUETOOTH_ERROR_NONE;
186 bt_user_info_t *user_info;
188 BT_CHECK_ENABLED(return);
189 BT_CHECK_PARAMETER(session->session_path, return);
191 user_info = _bt_get_user_data(BT_COMMON);
192 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
195 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
197 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
199 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FILTER_FIELDS,
200 in_param1, in_param2, in_param3, in_param4,
201 user_info->cb, user_info->user_data);
203 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
208 BT_EXPORT_API int bluetooth_map_client_list_messages(bt_map_client_session_info_s *session,
210 bt_map_client_list_messages_filter_t *filter)
212 BT_DBG("bluetooth_map_client_list_messages");
215 BT_CHECK_ENABLED(return);
216 BT_CHECK_PARAMETER(session->session_path, return);
217 BT_CHECK_PARAMETER(folder, return);
218 BT_CHECK_PARAMETER(filter, return);
220 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
221 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
224 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
227 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
228 if (filter->offset > -1)
229 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
230 if (filter->max_count > -1)
231 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
232 if (filter->subject_length > -1)
233 g_variant_builder_add(&b, "{sv}", "SubjectLength", g_variant_new_byte(filter->subject_length));
234 if (filter->fields != NULL)
235 g_variant_builder_add(&b, "{sv}", "Fields", __g_variant_new_array_split(filter->fields));
236 if (filter->types != NULL)
237 g_variant_builder_add(&b, "{sv}", "Types", __g_variant_new_array_split(filter->types));
238 if (filter->period_begin != NULL)
239 g_variant_builder_add(&b, "{sv}", "PeriodBegin", g_variant_new_string(filter->period_begin));
240 if (filter->period_end != NULL)
241 g_variant_builder_add(&b, "{sv}", "PeriodEnd", g_variant_new_string(filter->period_end));
242 if (filter->is_read > -1)
243 g_variant_builder_add(&b, "{sv}", "Read", g_variant_new_boolean(filter->is_read == 1 ? TRUE : FALSE));
244 if (filter->recipient != NULL)
245 g_variant_builder_add(&b, "{sv}", "Recipient", g_variant_new_string(filter->recipient));
246 if (filter->sender != NULL)
247 g_variant_builder_add(&b, "{sv}", "Sender", g_variant_new_string(filter->sender));
248 if (filter->is_priority > -1)
249 g_variant_builder_add(&b, "{sv}", "Priority", g_variant_new_boolean(filter->is_priority == 1 ? TRUE : FALSE));
250 GVariant *filter_variant = g_variant_builder_end(&b);
251 char *filter_serialized = g_variant_print(filter_variant, TRUE);
253 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
254 g_array_append_vals(in_param2, folder, strlen(folder)+1);
255 g_array_append_vals(in_param3, filter_serialized, strlen(filter_serialized)+1);
257 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_MESSAGES,
258 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
260 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
262 free(filter_serialized);
267 BT_EXPORT_API int bluetooth_map_client_update_inbox(bt_map_client_session_info_s *session)
269 BT_DBG("bluetooth_map_client_update_inbox");
270 int result = BLUETOOTH_ERROR_INTERNAL;
272 BT_CHECK_ENABLED(return);
273 BT_CHECK_PARAMETER(session->session_path, return);
276 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
278 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
280 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_UPDATE_INBOX,
281 in_param1, in_param2, in_param3, in_param4, &out_param);
283 if (result != BLUETOOTH_ERROR_NONE) {
284 BT_ERR("bluetooth_map_client_update_inbox failed");
287 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
292 BT_EXPORT_API int bluetooth_map_client_push_message(bt_map_client_session_info_s *session,
293 const char *source_file,
295 bt_map_client_push_message_args_t *args)
297 BT_DBG("Entered bluetooth_map_client_push_message");
300 BT_CHECK_ENABLED(return);
301 BT_CHECK_PARAMETER(session, return);
302 BT_CHECK_PARAMETER(session->session_path, return);
303 BT_CHECK_PARAMETER(source_file, return);
304 BT_CHECK_PARAMETER(folder, return);
306 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
307 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
310 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
313 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
314 if (args->is_transparent > -1)
315 g_variant_builder_add(&b, "{sv}", "Transparent", g_variant_new_boolean(args->is_transparent == 1 ? TRUE : FALSE));
316 if (args->is_retry > -1)
317 g_variant_builder_add(&b, "{sv}", "Retry", g_variant_new_boolean(args->is_retry == 1 ? TRUE : FALSE));
318 if (args->charset != NULL)
319 g_variant_builder_add(&b, "{sv}", "Charset", g_variant_new_string(args->charset));
320 GVariant *args_variant = g_variant_builder_end(&b);
321 char *args_serialized = g_variant_print(args_variant, TRUE);
323 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
324 g_array_append_vals(in_param2, source_file, strlen(source_file)+1);
325 g_array_append_vals(in_param3, folder, strlen(folder)+1);
326 g_array_append_vals(in_param4, args_serialized, strlen(args_serialized)+1);
328 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_PUSH_MESSAGE,
329 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
331 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
336 BT_EXPORT_API int bluetooth_map_client_get_message(bt_map_client_session_info_s *session,
337 const bt_map_client_message_object_t message_object,
338 const char *target_file,
341 BT_DBG("Entered bluetooth_map_client_get_message");
344 BT_CHECK_ENABLED(return);
345 BT_CHECK_PARAMETER(session, return);
346 BT_CHECK_PARAMETER(session->session_path, return);
347 BT_CHECK_PARAMETER(message_object, return);
348 BT_CHECK_PARAMETER(target_file, return);
350 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
351 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
354 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
356 // TODO session currently is not used, but should be valid
357 //g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
358 g_array_append_vals(in_param2, message_object, strlen(message_object)+1);
359 g_array_append_vals(in_param3, target_file, strlen(target_file)+1);
360 g_array_append_vals(in_param4, &attachment, sizeof(attachment));
362 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_GET_MESSAGE,
363 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
365 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);