Merge branch 'master' into tizen_2.1
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-opp-client.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string.h>
21
22 #include "bluetooth-api.h"
23 #include "bt-internal-types.h"
24
25 #include "bt-common.h"
26 #include "bt-request-sender.h"
27 #include "bt-event-handler.h"
28
29
30 static void __bt_get_file_size(char **files, unsigned long *file_size, int *count)
31 {
32         int file_count = 0;
33         unsigned long size = 0;
34
35         while (files[file_count] != NULL) {
36                 size = size + strlen(files[file_count]);
37                 file_count++;
38         }
39
40         *count = file_count;
41         *file_size = size;
42 }
43
44 BT_EXPORT_API int bluetooth_opc_init(void)
45 {
46         bt_user_info_t *user_info;
47
48         user_info = _bt_get_user_data(BT_COMMON);
49         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
50
51         return _bt_register_event(BT_OPP_CLIENT_EVENT, user_info->cb, user_info->user_data);
52 }
53
54 BT_EXPORT_API int bluetooth_opc_deinit(void)
55 {
56         return _bt_unregister_event(BT_OPP_CLIENT_EVENT);
57 }
58
59 BT_EXPORT_API int bluetooth_opc_push_files(bluetooth_device_address_t *remote_address,
60                                  char **file_name_array)
61 {
62         int result;
63         int i;
64         int file_count;
65         unsigned long size;
66         bt_user_info_t *user_info;
67         char filename[BT_FILE_PATH_MAX];
68
69         BT_CHECK_PARAMETER(remote_address, return);
70         BT_CHECK_PARAMETER(file_name_array, return);
71         BT_CHECK_ENABLED(return);
72
73         __bt_get_file_size(file_name_array, &size, &file_count);
74         retv_if(file_count == 0, BLUETOOTH_ERROR_INVALID_PARAM);
75
76         BT_INIT_PARAMS();
77         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
78
79         user_info = _bt_get_user_data(BT_COMMON);
80         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
81
82         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
83
84         for (i = 0; i < file_count; i++) {
85                 g_strlcpy(filename, file_name_array[i], sizeof(filename));
86                 g_array_append_vals(in_param2, filename, BT_FILE_PATH_MAX);
87         }
88
89         g_array_append_vals(in_param3, &file_count, sizeof(int));
90
91         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_OPP_PUSH_FILES,
92                 in_param1, in_param2, in_param3, in_param4,
93                 user_info->cb, user_info->user_data);
94
95         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
96
97         return result;
98 }
99
100 BT_EXPORT_API int bluetooth_opc_cancel_push(void)
101 {
102         int result;
103
104         BT_CHECK_ENABLED(return);
105
106         BT_INIT_PARAMS();
107         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
108
109         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_CANCEL_PUSH,
110                 in_param1, in_param2, in_param3, in_param4, &out_param);
111
112         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
113
114         return result;
115 }
116
117 BT_EXPORT_API gboolean bluetooth_opc_session_is_exist(void)
118 {
119         int result;
120         gboolean exist = FALSE;
121
122         BT_CHECK_ENABLED(return);
123
124         BT_INIT_PARAMS();
125         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
126
127         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
128                 in_param1, in_param2, in_param3, in_param4, &out_param);
129
130         if (result == BLUETOOTH_ERROR_NONE) {
131                 exist = g_array_index(out_param, gboolean, 0);
132         } else {
133                 BT_ERR("Fail to send request");
134         }
135
136         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
137
138         return result;
139 }
140
141 BT_EXPORT_API int bluetooth_opc_is_sending(gboolean *is_sending)
142 {
143         int result;
144
145         *is_sending = FALSE;
146
147         BT_CHECK_ENABLED(return);
148
149         BT_INIT_PARAMS();
150         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
151
152         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
153                 in_param1, in_param2, in_param3, in_param4, &out_param);
154
155         if (result == BLUETOOTH_ERROR_NONE) {
156                 *is_sending = g_array_index(out_param, gboolean, 0);
157         } else {
158                 BT_ERR("Fail to send request");
159         }
160
161         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
162
163         return result;
164 }
165