23e546f63f6a076382a2aa6e7dedf9547d9581b0
[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 GVariant *__g_variant_new_array_split(char *str)
29 {
30         GVariantBuilder b;
31         g_variant_builder_init(&b, (const GVariantType*) "a{s}");
32         char *pos = str;
33         char *item;
34
35         while ((item = strsep(&pos, ",")))
36                 g_variant_builder_add(&b, "{s}", item);
37
38         return g_variant_builder_end(&b);
39 }
40
41 BT_EXPORT_API int bluetooth_map_client_init(void)
42 {
43         BT_DBG("bluetooth_map_client_init");
44         bt_user_info_t *user_info;
45
46         user_info = _bt_get_user_data(BT_COMMON);
47         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
48
49         return _bt_register_event(BT_MAP_CLIENT_EVENT, user_info->cb, user_info->user_data);
50 }
51
52 BT_EXPORT_API int bluetooth_map_client_deinit(void)
53 {
54         BT_DBG("bluetooth_map_client_deinit");
55         return _bt_unregister_event(BT_MAP_CLIENT_EVENT);
56 }
57
58 BT_EXPORT_API int bluetooth_map_client_create_session(
59         bt_map_client_session_info_s *session)
60 {
61         BT_DBG("bluetooth_map_client_create_session");
62         int result = BLUETOOTH_ERROR_INTERNAL;
63
64         BT_CHECK_ENABLED(return);
65         BT_CHECK_PARAMETER(session->remote_address, return);
66
67         BT_INIT_PARAMS();
68         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
69
70         g_array_append_vals(in_param1, session->remote_address, strlen(session->remote_address)+1);
71
72         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_CREATE_SESSION,
73                 in_param1, in_param2, in_param3, in_param4, &out_param);
74
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);
79                 } else {
80                         BT_ERR("out_param length is 0!!");
81                         return BLUETOOTH_ERROR_INTERNAL;
82                 }
83         }
84         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
85         return result;
86 }
87
88 BT_EXPORT_API int bluetooth_map_client_destroy_session(
89         bt_map_client_session_info_s *session)
90 {
91         BT_DBG("bluetooth_map_client_destroy_session");
92         int result = BLUETOOTH_ERROR_INTERNAL;
93
94         BT_CHECK_ENABLED(return);
95         BT_CHECK_PARAMETER(session->session_path, return);
96
97         BT_INIT_PARAMS();
98         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
99
100         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
101
102         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_DESTROY_SESSION,
103                 in_param1, in_param2, in_param3, in_param4, &out_param);
104
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);
109                 free(session);
110         }
111         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
112         return result;
113 }
114
115 BT_EXPORT_API int bluetooth_map_client_set_folder(
116         bt_map_client_session_info_s *session,
117         const char *name)
118 {
119         BT_DBG("bluetooth_map_client_set_folder");
120         int result = BLUETOOTH_ERROR_INTERNAL;
121
122         BT_CHECK_ENABLED(return);
123         BT_CHECK_PARAMETER(session->session_path, return);
124         BT_CHECK_PARAMETER(name, return);
125
126         BT_INIT_PARAMS();
127         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
128
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);
131
132         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_SET_FOLDER,
133                 in_param1, in_param2, in_param3, in_param4, &out_param);
134
135         if (result != BLUETOOTH_ERROR_NONE) {
136                 BT_ERR("bluetooth_map_client_set_folder failed");
137                 return BLUETOOTH_ERROR_INTERNAL;
138         }
139
140         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141
142         return result;
143 }
144
145 BT_EXPORT_API int bluetooth_map_client_list_folders(
146                 bt_map_client_session_info_s *session,
147                 bt_map_client_list_folders_filter_t *filter)
148 {
149         BT_DBG("bluetooth_map_list_folders");
150         int result = 0;
151
152         BT_CHECK_ENABLED(return);
153         BT_CHECK_PARAMETER(session->session_path, return);
154
155         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
156         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
157
158         BT_INIT_PARAMS();
159         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
160
161         GVariantBuilder b;
162         g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
163         if (filter->offset > -1)
164                 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
165         if (filter->max_count > -1)
166                 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
167         GVariant *filter_variant = g_variant_builder_end(&b);
168         char *filter_serialized = g_variant_print(filter_variant, TRUE);
169
170         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
171         g_array_append_vals(in_param2, filter_serialized, strlen(filter_serialized)+1);
172
173         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FOLDERS,
174                         in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
175
176         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
177
178         free(filter_serialized);
179
180         return result;
181 }
182
183 BT_EXPORT_API int bluetooth_map_client_list_filter_fields(bt_map_client_session_info_s *session)
184 {
185         BT_DBG("bluetooth_map_client_list_filter_fields");
186         int result = BLUETOOTH_ERROR_NONE;
187         bt_user_info_t *user_info;
188
189         BT_CHECK_ENABLED(return);
190         BT_CHECK_PARAMETER(session->session_path, return);
191
192         user_info = _bt_get_user_data(BT_COMMON);
193         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
194
195         BT_INIT_PARAMS();
196         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
197
198         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
199
200         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_FILTER_FIELDS,
201                 in_param1, in_param2, in_param3, in_param4,
202                 user_info->cb, user_info->user_data);
203
204         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
205
206         return result;
207 }
208
209 BT_EXPORT_API int bluetooth_map_client_list_messages(bt_map_client_session_info_s *session,
210                         const char *folder,
211                         bt_map_client_list_messages_filter_t *filter)
212 {
213         BT_DBG("bluetooth_map_client_list_messages");
214         int result = 0;
215
216         BT_CHECK_ENABLED(return);
217         BT_CHECK_PARAMETER(session->session_path, return);
218         BT_CHECK_PARAMETER(folder, return);
219         BT_CHECK_PARAMETER(filter, return);
220
221         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
222         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
223
224         BT_INIT_PARAMS();
225         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
226
227         GVariantBuilder b;
228         g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
229         if (filter->offset > -1)
230                 g_variant_builder_add(&b, "{sv}", "Offset", g_variant_new_uint16(filter->offset));
231         if (filter->max_count > -1)
232                 g_variant_builder_add(&b, "{sv}", "MaxCount", g_variant_new_uint16(filter->max_count));
233         if (filter->subject_length > -1)
234                 g_variant_builder_add(&b, "{sv}", "SubjectLength", g_variant_new_byte(filter->subject_length));
235         if (filter->fields != NULL)
236                 g_variant_builder_add(&b, "{sv}", "Fields", __g_variant_new_array_split(filter->fields));
237         if (filter->types != NULL)
238                 g_variant_builder_add(&b, "{sv}", "Types", __g_variant_new_array_split(filter->types));
239         if (filter->period_begin != NULL)
240                 g_variant_builder_add(&b, "{sv}", "PeriodBegin", g_variant_new_string(filter->period_begin));
241         if (filter->period_end != NULL)
242                 g_variant_builder_add(&b, "{sv}", "PeriodEnd", g_variant_new_string(filter->period_end));
243         if (filter->is_read > -1)
244                 g_variant_builder_add(&b, "{sv}", "Read", g_variant_new_boolean(filter->is_read == 1 ? TRUE : FALSE));
245         if (filter->recipient != NULL)
246                 g_variant_builder_add(&b, "{sv}", "Recipient", g_variant_new_string(filter->recipient));
247         if (filter->sender != NULL)
248                 g_variant_builder_add(&b, "{sv}", "Sender", g_variant_new_string(filter->sender));
249         if (filter->is_priority > -1)
250                 g_variant_builder_add(&b, "{sv}", "Priority", g_variant_new_boolean(filter->is_priority == 1 ? TRUE : FALSE));
251         GVariant *filter_variant = g_variant_builder_end(&b);
252         char *filter_serialized = g_variant_print(filter_variant, TRUE);
253
254         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
255         g_array_append_vals(in_param2, folder, strlen(folder)+1);
256         g_array_append_vals(in_param3, filter_serialized, strlen(filter_serialized)+1);
257
258         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_LIST_MESSAGES,
259                         in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
260
261         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
262
263         free(filter_serialized);
264
265         return result;
266 }
267
268 BT_EXPORT_API int bluetooth_map_client_update_inbox(bt_map_client_session_info_s *session)
269 {
270         BT_DBG("bluetooth_map_client_update_inbox");
271         int result = BLUETOOTH_ERROR_INTERNAL;
272
273         BT_CHECK_ENABLED(return);
274         BT_CHECK_PARAMETER(session->session_path, return);
275
276         BT_INIT_PARAMS();
277         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
278
279         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
280
281         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_UPDATE_INBOX,
282                 in_param1, in_param2, in_param3, in_param4, &out_param);
283
284         if (result != BLUETOOTH_ERROR_NONE) {
285                 BT_ERR("bluetooth_map_client_update_inbox failed");
286                 return BLUETOOTH_ERROR_INTERNAL;
287         }
288
289         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
290
291         return result;
292 }
293
294 BT_EXPORT_API int bluetooth_map_client_push_message(bt_map_client_session_info_s *session,
295                         const char *source_file,
296                         const char *folder,
297                         bt_map_client_push_message_args_t *args)
298 {
299         BT_DBG("Entered bluetooth_map_client_push_message");
300         int result = 0;
301
302         BT_CHECK_ENABLED(return);
303         BT_CHECK_PARAMETER(session, return);
304         BT_CHECK_PARAMETER(session->session_path, return);
305         BT_CHECK_PARAMETER(source_file, return);
306         BT_CHECK_PARAMETER(folder, return);
307
308         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
309         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
310
311         BT_INIT_PARAMS();
312         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
313
314         GVariantBuilder b;
315         g_variant_builder_init(&b, (const GVariantType*) "a{sv}");
316         if (args->is_transparent > -1)
317                 g_variant_builder_add(&b, "{sv}", "Transparent", g_variant_new_boolean(args->is_transparent == 1 ? TRUE : FALSE));
318         if (args->is_retry > -1)
319                 g_variant_builder_add(&b, "{sv}", "Retry", g_variant_new_boolean(args->is_retry == 1 ? TRUE : FALSE));
320         if (args->charset != NULL)
321                 g_variant_builder_add(&b, "{sv}", "Charset", g_variant_new_string(args->charset));
322         GVariant *args_variant = g_variant_builder_end(&b);
323         char *args_serialized = g_variant_print(args_variant, TRUE);
324
325         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
326         g_array_append_vals(in_param2, source_file, strlen(source_file)+1);
327         g_array_append_vals(in_param3, folder, strlen(folder)+1);
328         g_array_append_vals(in_param4, args_serialized, strlen(args_serialized)+1);
329
330         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_PUSH_MESSAGE,
331                         in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
332
333         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
334
335         return result;
336 }
337
338 BT_EXPORT_API int bluetooth_map_client_get_message(bt_map_client_session_info_s *session,
339                         const bt_map_client_message_object_t message_object,
340                         const char *target_file,
341                         bool attachment)
342 {
343         BT_DBG("Entered bluetooth_map_client_get_message");
344         int result = 0;
345
346         BT_CHECK_ENABLED(return);
347         BT_CHECK_PARAMETER(session, return);
348         BT_CHECK_PARAMETER(session->session_path, return);
349         BT_CHECK_PARAMETER(message_object, return);
350         BT_CHECK_PARAMETER(target_file, return);
351
352         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
353         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
354
355         BT_INIT_PARAMS();
356         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
357
358         // TODO session currently is not used, but should be valid
359         //g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
360         g_array_append_vals(in_param2, message_object, strlen(message_object)+1);
361         g_array_append_vals(in_param3, target_file, strlen(target_file)+1);
362         g_array_append_vals(in_param4, &attachment, sizeof(attachment));
363
364         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_MAP_GET_MESSAGE,
365                         in_param1, in_param2, in_param3, in_param4, user_info->cb, user_info->user_data);
366
367         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
368
369         return result;
370 }