Remove BT enable check logic in pbap init / deinit
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-pbap.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 #include <vconf.h>
18 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
19 #include <syspopup_caller.h>
20 #endif
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 static char is_pbap_initialized = FALSE;
30
31 #define BT_CHECK_PBAP_INITIALIZED(func) \
32         if (is_pbap_initialized == FALSE) { \
33                 BT_ERR("BT PBAP Client is not initiatized"); \
34                 func BLUETOOTH_ERROR_NOT_INITIALIZED; \
35         } \
36
37
38 BT_EXPORT_API int bluetooth_pbap_init(void)
39 {
40         bt_user_info_t *user_info;
41         int ret;
42
43         if (is_pbap_initialized)
44                 return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
45
46         user_info = _bt_get_user_data(BT_COMMON);
47         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
48
49         ret = _bt_register_event(BT_PBAP_CLIENT_EVENT, user_info->cb,
50                                         user_info->user_data);
51         if (ret == 0)
52                 is_pbap_initialized = TRUE;
53
54         return ret;
55 }
56
57 BT_EXPORT_API int bluetooth_pbap_deinit(void)
58 {
59         int ret;
60
61         BT_CHECK_PBAP_INITIALIZED(return);
62
63         ret = _bt_unregister_event(BT_PBAP_CLIENT_EVENT);
64
65         if (ret == 0)
66                 is_pbap_initialized = FALSE;
67
68         return ret;
69 }
70
71 BT_EXPORT_API int bluetooth_pbap_connect(const bluetooth_device_address_t *address)
72 {
73         BT_DBG("+");
74         int result;
75
76         BT_CHECK_ENABLED(return);
77         BT_CHECK_PBAP_INITIALIZED(return);
78
79         BT_INIT_PARAMS();
80         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
81
82         g_array_append_vals(in_param1, address,
83                                 sizeof(bluetooth_device_address_t));
84         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_CONNECT,
85                 in_param1, in_param2, in_param3, in_param4, &out_param);
86
87         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88
89         BT_DBG("-");
90         return result;
91 }
92
93 BT_EXPORT_API int bluetooth_pbap_disconnect(const bluetooth_device_address_t *address)
94 {
95         BT_DBG("+");
96         int result;
97
98         BT_CHECK_ENABLED(return);
99         BT_CHECK_PBAP_INITIALIZED(return);
100
101         BT_INIT_PARAMS();
102         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
103
104         g_array_append_vals(in_param1, address,
105                                 sizeof(bluetooth_device_address_t));
106         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_DISCONNECT,
107                 in_param1, in_param2, in_param3, in_param4, &out_param);
108
109         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
110
111         BT_DBG("-");
112         return result;
113 }
114
115 BT_EXPORT_API int bluetooth_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
116                 bt_pbap_folder_t *folder)
117 {
118         BT_DBG("+");
119         int result;
120
121         BT_CHECK_ENABLED(return);
122         BT_CHECK_PBAP_INITIALIZED(return);
123
124         BT_INIT_PARAMS();
125         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
126
127         g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
128
129         g_array_append_vals(in_param2, folder, sizeof(bt_pbap_folder_t));
130
131         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_GET_PHONEBOOK_SIZE,
132                 in_param1, in_param2, in_param3, in_param4, &out_param);
133
134         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
135
136         BT_DBG("-");
137         return result;
138 }
139
140 BT_EXPORT_API int bluetooth_pbap_get_phonebook(const bluetooth_device_address_t *address,
141                 bt_pbap_folder_t *folder, bt_pbap_pull_parameters_t *app_param)
142 {
143         BT_DBG("+");
144         int result;
145
146         BT_CHECK_ENABLED(return);
147         BT_CHECK_PBAP_INITIALIZED(return);
148
149         BT_INIT_PARAMS();
150         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
151
152         g_array_append_vals(in_param1, address,
153                                 sizeof(bluetooth_device_address_t));
154         g_array_append_vals(in_param2, folder, sizeof(bt_pbap_folder_t));
155         g_array_append_vals(in_param3, app_param, sizeof(bt_pbap_pull_parameters_t));
156         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_GET_PHONEBOOK,
157                 in_param1, in_param2, in_param3, in_param4, &out_param);
158
159         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
160
161         BT_DBG("-");
162         return result;
163 }
164
165 BT_EXPORT_API int bluetooth_pbap_get_list(const bluetooth_device_address_t *address,
166                 bt_pbap_folder_t *folder, bt_pbap_list_parameters_t *app_param)
167 {
168         BT_DBG("+");
169         int result;
170
171         BT_CHECK_ENABLED(return);
172         BT_CHECK_PBAP_INITIALIZED(return);
173
174         BT_INIT_PARAMS();
175         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
176
177         g_array_append_vals(in_param1, address,
178                                 sizeof(bluetooth_device_address_t));
179         g_array_append_vals(in_param2, folder, sizeof(bt_pbap_folder_t));
180         g_array_append_vals(in_param3, app_param, sizeof(bt_pbap_list_parameters_t));
181
182         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_GET_LIST,
183                 in_param1, in_param2, in_param3, in_param4, &out_param);
184
185         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
186
187         BT_DBG("-");
188         return result;
189 }
190
191 BT_EXPORT_API int bluetooth_pbap_pull_vcard(const bluetooth_device_address_t *address,
192                 bt_pbap_folder_t *folder, bt_pbap_pull_vcard_parameters_t *app_param)
193 {
194         BT_DBG("+");
195         int result;
196
197         BT_CHECK_ENABLED(return);
198         BT_CHECK_PBAP_INITIALIZED(return);
199
200         BT_INIT_PARAMS();
201         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
202
203         g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
204         g_array_append_vals(in_param2, folder, sizeof(bt_pbap_folder_t));
205         g_array_append_vals(in_param3, app_param, sizeof(bt_pbap_pull_vcard_parameters_t));
206
207         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_PULL_VCARD,
208                 in_param1, in_param2, in_param3, in_param4, &out_param);
209
210         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
211
212         BT_DBG("-");
213         return result;
214 }
215
216 BT_EXPORT_API int bluetooth_pbap_phonebook_search(const bluetooth_device_address_t *address,
217                 bt_pbap_folder_t *folder, bt_pbap_search_parameters_t *app_param)
218 {
219         BT_DBG("+");
220         int result;
221         BT_CHECK_ENABLED(return);
222         BT_CHECK_PBAP_INITIALIZED(return);
223
224         BT_INIT_PARAMS();
225         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
226
227         g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
228         g_array_append_vals(in_param2, folder, sizeof(bt_pbap_folder_t));
229         g_array_append_vals(in_param3, app_param, sizeof(bt_pbap_search_parameters_t));
230
231         result = _bt_send_request(BT_OBEX_SERVICE, BT_PBAP_PHONEBOOK_SEARCH,
232                 in_param1, in_param2, in_param3, in_param4, &out_param);
233
234         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
235
236         BT_DBG("-");
237         return result;
238 }