Merge "DPM: Add basic code for device policy manager for BT." into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-opp-client.c
1 /*
2  * Copyright (c) 2011 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
20 #include "bluetooth-api.h"
21 #include "bt-internal-types.h"
22
23 #include "bt-common.h"
24 #include "bt-request-sender.h"
25 #include "bt-event-handler.h"
26
27 static void __bt_get_file_size(char **files, unsigned long *file_size, int *count)
28 {
29         int file_count = 0;
30         unsigned long size = 0;
31
32         while (files[file_count] != NULL) {
33                 size = size + strlen(files[file_count]);
34                 file_count++;
35         }
36
37         *count = file_count;
38         *file_size = size;
39 }
40
41 BT_EXPORT_API int bluetooth_opc_init(void)
42 {
43         bt_user_info_t *user_info;
44
45         user_info = _bt_get_user_data(BT_COMMON);
46         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
47
48         return _bt_register_event(BT_OPP_CLIENT_EVENT, user_info->cb, user_info->user_data);
49 }
50
51 BT_EXPORT_API int bluetooth_opc_deinit(void)
52 {
53         return _bt_unregister_event(BT_OPP_CLIENT_EVENT);
54 }
55
56 BT_EXPORT_API int bluetooth_opc_push_files(bluetooth_device_address_t *remote_address,
57                                  char **file_name_array)
58 {
59         int result;
60         int i;
61         int file_count;
62         unsigned long size;
63         bt_user_info_t *user_info;
64         char filename[BT_FILE_PATH_MAX];
65
66         BT_CHECK_PARAMETER(remote_address, return);
67         BT_CHECK_PARAMETER(file_name_array, return);
68         BT_CHECK_ENABLED(return);
69
70         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_OPP_PUSH_FILES)
71              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
72                 BT_ERR("Don't have a privilege to use this API");
73                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
74         }
75
76         __bt_get_file_size(file_name_array, &size, &file_count);
77         retv_if(file_count == 0, BLUETOOTH_ERROR_INVALID_PARAM);
78
79         user_info = _bt_get_user_data(BT_COMMON);
80         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
81
82         BT_INIT_PARAMS();
83         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
84
85         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
86
87         for (i = 0; i < file_count; i++) {
88                 if (strlen(file_name_array[i]) >= sizeof(filename)) {
89                         BT_ERR("[%s] has too long path.", file_name_array[i]);
90                         BT_FREE_PARAMS(in_param1, in_param2, in_param3,
91                                        in_param4, out_param);
92                         return BLUETOOTH_ERROR_INVALID_PARAM;
93                 }
94                 g_strlcpy(filename, file_name_array[i], sizeof(filename));
95                 g_array_append_vals(in_param2, filename, BT_FILE_PATH_MAX);
96         }
97
98         g_array_append_vals(in_param3, &file_count, sizeof(int));
99
100         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_OPP_PUSH_FILES,
101                 in_param1, in_param2, in_param3, in_param4,
102                 user_info->cb, user_info->user_data);
103
104         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
105
106         return result;
107 }
108
109 BT_EXPORT_API int bluetooth_opc_cancel_push(void)
110 {
111         int result;
112
113         BT_CHECK_ENABLED(return);
114
115         BT_INIT_PARAMS();
116         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
117
118         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_CANCEL_PUSH,
119                 in_param1, in_param2, in_param3, in_param4, &out_param);
120
121         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
122
123         return result;
124 }
125
126 BT_EXPORT_API gboolean bluetooth_opc_session_is_exist(void)
127 {
128         int result;
129         gboolean exist = FALSE;
130
131         BT_CHECK_ENABLED(return);
132
133         BT_INIT_PARAMS();
134         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
135
136         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
137                 in_param1, in_param2, in_param3, in_param4, &out_param);
138
139         if (result == BLUETOOTH_ERROR_NONE) {
140                 exist = g_array_index(out_param, gboolean, 0);
141                 BT_DBG("Exist %d", exist);
142         } else {
143                 BT_ERR("Fail to send request");
144         }
145
146         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
147
148         return result;
149 }
150
151 BT_EXPORT_API int bluetooth_opc_is_sending(gboolean *is_sending)
152 {
153         int result;
154
155         *is_sending = FALSE;
156
157         BT_CHECK_ENABLED(return);
158
159         BT_INIT_PARAMS();
160         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
161
162         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
163                 in_param1, in_param2, in_param3, in_param4, &out_param);
164
165         if (result == BLUETOOTH_ERROR_NONE) {
166                 *is_sending = g_array_index(out_param, gboolean, 0);
167         } else {
168                 BT_ERR("Fail to send request");
169         }
170
171         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
172
173         return result;
174 }
175