Remove wrong dependency in the systemd service file
[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         }
138
139         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
140
141         return result;
142 }
143
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)
147 {
148         BT_DBG("bluetooth_map_list_folders");
149         int result = 0;
150
151         BT_CHECK_ENABLED(return);
152         BT_CHECK_PARAMETER(session->session_path, return);
153
154         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
155         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
156
157         BT_INIT_PARAMS();
158         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
159
160         GVariantBuilder b;
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);
168
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);
171
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);
174
175         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
176
177         free(filter_serialized);
178
179         return result;
180 }
181
182 BT_EXPORT_API int bluetooth_map_client_list_filter_fields(bt_map_client_session_info_s *session)
183 {
184         BT_DBG("bluetooth_map_client_list_filter_fields");
185         int result = BLUETOOTH_ERROR_NONE;
186         bt_user_info_t *user_info;
187
188         BT_CHECK_ENABLED(return);
189         BT_CHECK_PARAMETER(session->session_path, return);
190
191         user_info = _bt_get_user_data(BT_COMMON);
192         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
193
194         BT_INIT_PARAMS();
195         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
196
197         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
198
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);
202
203         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
204
205         return result;
206 }
207
208 BT_EXPORT_API int bluetooth_map_client_list_messages(bt_map_client_session_info_s *session,
209                         const char *folder,
210                         bt_map_client_list_messages_filter_t *filter)
211 {
212         BT_DBG("bluetooth_map_client_list_messages");
213         int result = 0;
214
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);
219
220         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
221         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
222
223         BT_INIT_PARAMS();
224         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
225
226         GVariantBuilder b;
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);
252
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);
256
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);
259
260         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
261
262         free(filter_serialized);
263
264         return result;
265 }
266
267 BT_EXPORT_API int bluetooth_map_client_update_inbox(bt_map_client_session_info_s *session)
268 {
269         BT_DBG("bluetooth_map_client_update_inbox");
270         int result = BLUETOOTH_ERROR_INTERNAL;
271
272         BT_CHECK_ENABLED(return);
273         BT_CHECK_PARAMETER(session->session_path, return);
274
275         BT_INIT_PARAMS();
276         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
277
278         g_array_append_vals(in_param1, session->session_path, strlen(session->session_path)+1);
279
280         result = _bt_send_request(BT_OBEX_SERVICE, BT_MAP_UPDATE_INBOX,
281                 in_param1, in_param2, in_param3, in_param4, &out_param);
282
283         if (result != BLUETOOTH_ERROR_NONE) {
284                 BT_ERR("bluetooth_map_client_update_inbox failed");
285         }
286
287         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
288
289         return result;
290 }
291
292 BT_EXPORT_API int bluetooth_map_client_push_message(bt_map_client_session_info_s *session,
293                         const char *source_file,
294                         const char *folder,
295                         bt_map_client_push_message_args_t *args)
296 {
297         BT_DBG("Entered bluetooth_map_client_push_message");
298         int result = 0;
299
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);
305
306         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
307         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
308
309         BT_INIT_PARAMS();
310         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
311
312         GVariantBuilder b;
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);
322
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);
327
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);
330
331         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
332
333         return result;
334 }
335
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,
339                         bool attachment)
340 {
341         BT_DBG("Entered bluetooth_map_client_get_message");
342         int result = 0;
343
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);
349
350         bt_user_info_t *user_info = _bt_get_user_data(BT_COMMON);
351         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
352
353         BT_INIT_PARAMS();
354         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
355
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));
361
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);
364
365         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
366
367         return result;
368 }