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