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 #ifdef TIZEN_DPM_ENABLE
32 GVariant *__g_variant_new_array_split(char *str)
35 g_variant_builder_init(&b, (const GVariantType*) "a{s}");
39 while ((item = strsep(&pos, ",")))
40 g_variant_builder_add(&b, "{s}", item);
42 return g_variant_builder_end(&b);
45 BT_EXPORT_API int bluetooth_map_client_init(void)
47 BT_DBG("bluetooth_map_client_init");
48 bt_user_info_t *user_info;
50 user_info = _bt_get_user_data(BT_COMMON);
51 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
53 return _bt_register_event(BT_MAP_CLIENT_EVENT, user_info->cb, user_info->user_data);
56 BT_EXPORT_API int bluetooth_map_client_deinit(void)
58 BT_DBG("bluetooth_map_client_deinit");
59 return _bt_unregister_event(BT_MAP_CLIENT_EVENT);
62 BT_EXPORT_API int bluetooth_map_client_create_session(
63 bt_map_client_session_info_s *session)
65 BT_DBG("bluetooth_map_client_create_session");
66 int result = BLUETOOTH_ERROR_INTERNAL;
68 BT_CHECK_ENABLED(return);
69 BT_CHECK_PARAMETER(session->remote_address, return);
72 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
74 g_array_append_vals(in_param1, session->remote_address, strlen(session->remote_address)+1);
76 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_CREATE_SESSION,
77 in_param1, in_param2, in_param3, in_param4, &out_param);
79 if (result == BLUETOOTH_ERROR_NONE) {
80 if (out_param->len > 0) {
81 session->session_path = strdup(&g_array_index(out_param, char, 0));
82 BT_DBG("session id: %s", session->session_path);
84 BT_ERR("out_param length is 0!!");
85 return BLUETOOTH_ERROR_INTERNAL;
88 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
92 BT_EXPORT_API int bluetooth_map_client_destroy_session(
93 bt_map_client_session_info_s *session)
95 BT_DBG("bluetooth_map_client_destroy_session");
96 int result = BLUETOOTH_ERROR_INTERNAL;
98 BT_CHECK_ENABLED(return);
99 BT_CHECK_PARAMETER(session->session_path, return);
102 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
104 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
106 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_DESTROY_SESSION,
107 in_param1, in_param2, in_param3, in_param4, &out_param);
109 if (result == BLUETOOTH_ERROR_NONE) {
110 BT_DBG("session (%s) was destroyed", session->session_path);
111 free(session->remote_address);
112 free(session->session_path);
115 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
119 BT_EXPORT_API int bluetooth_map_client_set_folder(
120 bt_map_client_session_info_s *session,
123 BT_DBG("bluetooth_map_client_set_folder");
124 int result = BLUETOOTH_ERROR_INTERNAL;
126 BT_CHECK_ENABLED(return);
127 BT_CHECK_PARAMETER(session->session_path, return);
128 BT_CHECK_PARAMETER(name, return);
131 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
133 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path) + 1);
134 g_array_append_vals(in_param2, name, strlen(name) + 1);
136 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_SET_FOLDER,
137 in_param1, in_param2, in_param3, in_param4, &out_param);
139 if (result != BLUETOOTH_ERROR_NONE) {
140 BT_ERR("bluetooth_map_client_set_folder failed");
141 return BLUETOOTH_ERROR_INTERNAL;
144 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
149 BT_EXPORT_API int bluetooth_map_client_list_folders(
150 bt_map_client_session_info_s *session,
151 bt_map_client_list_folders_filter_t *filter)
153 BT_DBG("bluetooth_map_list_folders");
156 BT_CHECK_ENABLED(return);
157 BT_CHECK_PARAMETER(session->session_path, return);
159 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
160 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
163 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
166 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
167 if (filter->offset > -1)
168 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
169 if (filter->max_count > -1)
170 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
171 GVariant *filter_variant = g_variant_builder_end(&b);
172 char *filter_serialized = g_variant_print(filter_variant, TRUE);
174 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
175 g_array_append_vals(in_param2, filter_serialized, strlen(filter_serialized)+1);
177 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FOLDERS,
178 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
180 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
182 free(filter_serialized);
187 BT_EXPORT_API int bluetooth_map_client_list_filter_fields(bt_map_client_session_info_s *session)
189 BT_DBG("bluetooth_map_client_list_filter_fields");
190 int result = BLUETOOTH_ERROR_NONE;
191 bt_user_info_t *user_info;
193 BT_CHECK_ENABLED(return);
194 BT_CHECK_PARAMETER(session->session_path, return);
196 user_info = _bt_get_user_data(BT_COMMON);
197 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
200 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
202 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
204 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FILTER_FIELDS,
205 in_param1, in_param2, in_param3, in_param4,
206 user_info->cb, user_info->user_data);
208 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
213 BT_EXPORT_API int bluetooth_map_client_list_messages(bt_map_client_session_info_s *session,
215 bt_map_client_list_messages_filter_t *filter)
217 BT_DBG("bluetooth_map_client_list_messages");
220 BT_CHECK_ENABLED(return);
221 BT_CHECK_PARAMETER(session->session_path, return);
222 BT_CHECK_PARAMETER(folder, return);
223 BT_CHECK_PARAMETER(filter, return);
225 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
226 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
229 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
232 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
233 if (filter->offset > -1)
234 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
235 if (filter->max_count > -1)
236 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
237 if (filter->subject_length > -1)
238 g_variant_builder_add(&b, "{sv}", "SubjectLength", g_variant_new_byte(filter->subject_length));
239 if (filter->fields != NULL)
240 g_variant_builder_add(&b, "{sv}", "Fields", __g_variant_new_array_split(filter->fields));
241 if (filter->types != NULL)
242 g_variant_builder_add(&b, "{sv}", "Types", __g_variant_new_array_split(filter->types));
243 if (filter->period_begin != NULL)
244 g_variant_builder_add(&b, "{sv}", "PeriodBegin", g_variant_new_string(filter->period_begin));
245 if (filter->period_end != NULL)
246 g_variant_builder_add(&b, "{sv}", "PeriodEnd", g_variant_new_string(filter->period_end));
247 if (filter->is_read > -1)
248 g_variant_builder_add(&b, "{sv}", "Read", g_variant_new_boolean(filter->is_read == 1 ? TRUE : FALSE));
249 if (filter->recipient != NULL)
250 g_variant_builder_add(&b, "{sv}", "Recipient", g_variant_new_string(filter->recipient));
251 if (filter->sender != NULL)
252 g_variant_builder_add(&b, "{sv}", "Sender", g_variant_new_string(filter->sender));
253 if (filter->is_priority > -1)
254 g_variant_builder_add(&b, "{sv}", "Priority", g_variant_new_boolean(filter->is_priority == 1 ? TRUE : FALSE));
255 GVariant *filter_variant = g_variant_builder_end(&b);
256 char *filter_serialized = g_variant_print(filter_variant, TRUE);
258 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
259 g_array_append_vals(in_param2, folder, strlen(folder)+1);
260 g_array_append_vals(in_param3, filter_serialized, strlen(filter_serialized)+1);
262 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_MESSAGES,
263 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
265 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
267 free(filter_serialized);
272 BT_EXPORT_API int bluetooth_map_client_update_inbox(bt_map_client_session_info_s *session)
274 BT_DBG("bluetooth_map_client_update_inbox");
275 int result = BLUETOOTH_ERROR_INTERNAL;
277 BT_CHECK_ENABLED(return);
278 BT_CHECK_PARAMETER(session->session_path, return);
281 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
283 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
285 result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_UPDATE_INBOX,
286 in_param1, in_param2, in_param3, in_param4, &out_param);
288 if (result != BLUETOOTH_ERROR_NONE) {
289 BT_ERR("bluetooth_map_client_update_inbox failed");
290 return BLUETOOTH_ERROR_INTERNAL;
293 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
298 BT_EXPORT_API int bluetooth_map_client_push_message(bt_map_client_session_info_s *session,
299 const char *source_file,
301 bt_map_client_push_message_args_t *args)
303 BT_DBG("Entered bluetooth_map_client_push_message");
306 BT_CHECK_ENABLED(return);
307 BT_CHECK_PARAMETER(session, return);
308 BT_CHECK_PARAMETER(session->session_path, return);
309 BT_CHECK_PARAMETER(source_file, return);
310 BT_CHECK_PARAMETER(folder, return);
312 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
313 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
316 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
319 g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
320 if (args->is_transparent > -1)
321 g_variant_builder_add(&b, "{sv}", "Transparent", g_variant_new_boolean(args->is_transparent == 1 ? TRUE : FALSE));
322 if (args->is_retry > -1)
323 g_variant_builder_add(&b, "{sv}", "Retry", g_variant_new_boolean(args->is_retry == 1 ? TRUE : FALSE));
324 if (args->charset != NULL)
325 g_variant_builder_add(&b, "{sv}", "Charset", g_variant_new_string(args->charset));
326 GVariant *args_variant = g_variant_builder_end(&b);
327 char *args_serialized = g_variant_print(args_variant, TRUE);
329 g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
330 g_array_append_vals(in_param2, source_file, strlen(source_file)+1);
331 g_array_append_vals(in_param3, folder, strlen(folder)+1);
332 g_array_append_vals(in_param4, args_serialized, strlen(args_serialized)+1);
334 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_PUSH_MESSAGE,
335 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
337 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
342 BT_EXPORT_API int bluetooth_map_client_get_message(bt_map_client_session_info_s *session,
343 const bt_map_client_message_object_t message_object,
344 const char *target_file,
347 BT_DBG("Entered bluetooth_map_client_get_message");
350 BT_CHECK_ENABLED(return);
351 BT_CHECK_PARAMETER(session, return);
352 BT_CHECK_PARAMETER(session->session_path, return);
353 BT_CHECK_PARAMETER(message_object, return);
354 BT_CHECK_PARAMETER(target_file, return);
356 bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
357 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
360 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
362 // TODO session currently is not used, but should be valid
363 //g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
364 g_array_append_vals(in_param2, message_object, strlen(message_object)+1);
365 g_array_append_vals(in_param3, target_file, strlen(target_file)+1);
366 g_array_append_vals(in_param4, &attachment, sizeof(attachment));
368 result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_GET_MESSAGE,
369 in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
371 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);