Add a BT developer
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-map-client.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include <string.h>
19 #include <stdbool.h>
20
21 #include "bluetooth-api.h"
22 #include "bt-internal-types.h"
23
24 #include "bt-common.h"
25 #include "bt-request-sender.h"
26 #include "bt-event-handler.h"
27
28 #ifdef TIZEN_DPM_ENABLE
29 #include "bt-dpm.h"
30 #endif
31
32 GVariant *__g_variant_new_array_split(char *str)
33 {
34         GVariantBuilder b;
35         g_variant_builder_init(&b, (const GVariantType*) "a{s}");
36         char *pos = str;
37         char *item;
38
39         while ((item = strsep(&pos, ",")))
40                 g_variant_builder_add(&b, "{s}", item);
41
42         return g_variant_builder_end(&b);
43 }
44
45 BT_EXPORT_API int bluetooth_map_client_init(void)
46 {
47         BT_DBG("bluetooth_map_client_init");
48         bt_user_info_t *user_info;
49
50         user_info = _bt_get_user_data(BT_COMMON);
51         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
52
53         return _bt_register_event(BT_MAP_CLIENT_EVENT, user_info->cb, user_info->user_data);
54 }
55
56 BT_EXPORT_API int bluetooth_map_client_deinit(void)
57 {
58         BT_DBG("bluetooth_map_client_deinit");
59         return _bt_unregister_event(BT_MAP_CLIENT_EVENT);
60 }
61
62 BT_EXPORT_API int bluetooth_map_client_create_session(
63         bt_map_client_session_info_s *session)
64 {
65         BT_DBG("bluetooth_map_client_create_session");
66         int result = BLUETOOTH_ERROR_INTERNAL;
67
68         BT_CHECK_ENABLED(return);
69         BT_CHECK_PARAMETER(session->remote_address, return);
70
71         BT_INIT_PARAMS();
72         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
73
74         g_array_append_vals(in_param1, session->remote_address, strlen(session->remote_address)+1);
75
76         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_CREATE_SESSION,
77                 in_param1, in_param2, in_param3, in_param4, &out_param);
78
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);
83                 } else {
84                         BT_ERR("out_param length is 0!!");
85                         return BLUETOOTH_ERROR_INTERNAL;
86                 }
87         }
88         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
89         return result;
90 }
91
92 BT_EXPORT_API int bluetooth_map_client_destroy_session(
93         bt_map_client_session_info_s *session)
94 {
95         BT_DBG("bluetooth_map_client_destroy_session");
96         int result = BLUETOOTH_ERROR_INTERNAL;
97
98         BT_CHECK_ENABLED(return);
99         BT_CHECK_PARAMETER(session->session_path, return);
100
101         BT_INIT_PARAMS();
102         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
103
104         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
105
106         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_DESTROY_SESSION,
107                 in_param1, in_param2, in_param3, in_param4, &out_param);
108
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);
113                 free(session);
114         }
115         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
116         return result;
117 }
118
119 BT_EXPORT_API int bluetooth_map_client_set_folder(
120         bt_map_client_session_info_s *session,
121         const char *name)
122 {
123         BT_DBG("bluetooth_map_client_set_folder");
124         int result = BLUETOOTH_ERROR_INTERNAL;
125
126         BT_CHECK_ENABLED(return);
127         BT_CHECK_PARAMETER(session->session_path, return);
128         BT_CHECK_PARAMETER(name, return);
129
130         BT_INIT_PARAMS();
131         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
132
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);
135
136         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_SET_FOLDER,
137                 in_param1, in_param2, in_param3, in_param4, &out_param);
138
139         if (result != BLUETOOTH_ERROR_NONE) {
140                 BT_ERR("bluetooth_map_client_set_folder failed");
141                 return BLUETOOTH_ERROR_INTERNAL;
142         }
143
144         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
145
146         return result;
147 }
148
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)
152 {
153         BT_DBG("bluetooth_map_list_folders");
154         int result = 0;
155
156         BT_CHECK_ENABLED(return);
157         BT_CHECK_PARAMETER(session->session_path, return);
158
159         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
160         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
161
162         BT_INIT_PARAMS();
163         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
164
165         GVariantBuilder b;
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);
173
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);
176
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);
179
180         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
181
182         free(filter_serialized);
183
184         return result;
185 }
186
187 BT_EXPORT_API int bluetooth_map_client_list_filter_fields(bt_map_client_session_info_s *session)
188 {
189         BT_DBG("bluetooth_map_client_list_filter_fields");
190         int result = BLUETOOTH_ERROR_NONE;
191         bt_user_info_t *user_info;
192
193         BT_CHECK_ENABLED(return);
194         BT_CHECK_PARAMETER(session->session_path, return);
195
196         user_info = _bt_get_user_data(BT_COMMON);
197         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
198
199         BT_INIT_PARAMS();
200         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
201
202         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
203
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);
207
208         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
209
210         return result;
211 }
212
213 BT_EXPORT_API int bluetooth_map_client_list_messages(bt_map_client_session_info_s *session,
214                         const char *folder,
215                         bt_map_client_list_messages_filter_t *filter)
216 {
217         BT_DBG("bluetooth_map_client_list_messages");
218         int result = 0;
219
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);
224
225         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
226         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
227
228         BT_INIT_PARAMS();
229         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
230
231         GVariantBuilder b;
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);
257
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);
261
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);
264
265         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
266
267         free(filter_serialized);
268
269         return result;
270 }
271
272 BT_EXPORT_API int bluetooth_map_client_update_inbox(bt_map_client_session_info_s *session)
273 {
274         BT_DBG("bluetooth_map_client_update_inbox");
275         int result = BLUETOOTH_ERROR_INTERNAL;
276
277         BT_CHECK_ENABLED(return);
278         BT_CHECK_PARAMETER(session->session_path, return);
279
280         BT_INIT_PARAMS();
281         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
282
283         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
284
285         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_UPDATE_INBOX,
286                 in_param1, in_param2, in_param3, in_param4, &out_param);
287
288         if (result != BLUETOOTH_ERROR_NONE) {
289                 BT_ERR("bluetooth_map_client_update_inbox failed");
290                 return BLUETOOTH_ERROR_INTERNAL;
291         }
292
293         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
294
295         return result;
296 }
297
298 BT_EXPORT_API int bluetooth_map_client_push_message(bt_map_client_session_info_s *session,
299                         const char *source_file,
300                         const char *folder,
301                         bt_map_client_push_message_args_t *args)
302 {
303         BT_DBG("Entered bluetooth_map_client_push_message");
304         int result = 0;
305
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);
311
312         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
313         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
314
315         BT_INIT_PARAMS();
316         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
317
318         GVariantBuilder b;
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);
328
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);
333
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);
336
337         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
338
339         return result;
340 }
341
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,
345                         bool attachment)
346 {
347         BT_DBG("Entered bluetooth_map_client_get_message");
348         int result = 0;
349
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);
355
356         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
357         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
358
359         BT_INIT_PARAMS();
360         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
361
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));
367
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);
370
371         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
372
373         return result;
374 }