Merge the code from tizen_2.4
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-device.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 "bluetooth-api.h"
25 #include "bt-internal-types.h"
26
27 #include "bt-common.h"
28 #include "bt-request-sender.h"
29 #include "bt-event-handler.h"
30
31 BT_EXPORT_API int bluetooth_bond_device(const bluetooth_device_address_t *device_address)
32 {
33         int result;
34         bt_user_info_t *user_info;
35
36         BT_CHECK_PARAMETER(device_address, return);
37         BT_CHECK_ENABLED(return);
38
39         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE)
40              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
41                 BT_ERR("Don't have a privilege to use this API");
42                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
43         }
44
45         user_info = _bt_get_user_data(BT_COMMON);
46         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
47
48         BT_INIT_PARAMS();
49         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
50
51         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
52
53         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE,
54                 in_param1, in_param2, in_param3, in_param4,
55                 user_info->cb, user_info->user_data);
56
57         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
58
59         return result;
60 }
61
62 BT_EXPORT_API int bluetooth_bond_device_by_type(
63         const bluetooth_device_address_t *device_address,
64         bluetooth_conn_type_t conn_type)
65 {
66         int result;
67         bt_user_info_t *user_info;
68
69         BT_CHECK_PARAMETER(device_address, return);
70         if(conn_type == BLUETOOTH_DEV_CONN_BREDR)
71                 BT_CHECK_ENABLED(return);
72         else if(conn_type == BLUETOOTH_DEV_CONN_LE)
73                 BT_CHECK_ENABLED_LE(return);
74         else if(conn_type == BLUETOOTH_DEV_CONN_DEFAULT) {
75                 BT_CHECK_ENABLED(return);
76                 BT_CHECK_ENABLED_LE(return);
77         }
78
79         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE_BY_TYPE)
80              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
81                 BT_ERR("Don't have a privilege to use this API");
82                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
83         }
84
85         BT_INIT_PARAMS();
86         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
87
88         user_info = _bt_get_user_data(BT_COMMON);
89         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
90
91         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
92         g_array_append_vals(in_param2, &conn_type, sizeof(unsigned short));
93
94         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE_BY_TYPE,
95                 in_param1, in_param2, in_param3, in_param4,
96                 user_info->cb, user_info->user_data);
97
98         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
99
100         return result;
101 }
102
103 BT_EXPORT_API int bluetooth_cancel_bonding(void)
104 {
105         int result;
106
107         BT_CHECK_ENABLED_ANY(return);
108
109         BT_INIT_PARAMS();
110         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
111
112         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_BONDING,
113                 in_param1, in_param2, in_param3, in_param4, &out_param);
114
115         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
116
117         return result;
118 }
119
120 BT_EXPORT_API int bluetooth_unbond_device(const bluetooth_device_address_t *device_address)
121 {
122         int result;
123         bt_user_info_t *user_info;
124
125         BT_CHECK_PARAMETER(device_address, return);
126         BT_CHECK_ENABLED_ANY(return);
127
128         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_UNBOND_DEVICE)
129              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
130                 BT_ERR("Don't have a privilege to use this API");
131                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
132         }
133
134         user_info = _bt_get_user_data(BT_COMMON);
135         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
136
137         BT_INIT_PARAMS();
138         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
139
140         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
141
142         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_UNBOND_DEVICE,
143                 in_param1, in_param2, in_param3, in_param4,
144                 user_info->cb, user_info->user_data);
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_get_bonded_device(const bluetooth_device_address_t *device_address,
152                                               bluetooth_device_info_t *dev_info)
153 {
154         int result;
155
156         BT_CHECK_PARAMETER(device_address, return);
157         BT_CHECK_PARAMETER(dev_info, return);
158         BT_CHECK_ENABLED_ANY(return);
159
160         BT_INIT_PARAMS();
161         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
162
163         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
164
165         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_BONDED_DEVICE,
166                 in_param1, in_param2, in_param3, in_param4, &out_param);
167
168         if (result == BLUETOOTH_ERROR_NONE) {
169                 if (out_param->len > 0) {
170                         *dev_info = g_array_index(out_param,
171                                         bluetooth_device_info_t, 0);
172                 } else {
173                         BT_ERR("out_param length is 0!!");
174                 }
175         }
176
177         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
178
179         return result;
180 }
181
182 BT_EXPORT_API int bluetooth_get_remote_device(const bluetooth_device_address_t *device_address)
183 {
184         return BLUETOOTH_ERROR_NONE;
185 }
186
187 BT_EXPORT_API int bluetooth_search_service(const bluetooth_device_address_t *device_address)
188 {
189         int result;
190         bt_user_info_t *user_info;
191
192         BT_CHECK_PARAMETER(device_address, return);
193         BT_CHECK_ENABLED_ANY(return);
194
195         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SEARCH_SERVICE)
196              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
197                 BT_ERR("Don't have a privilege to use this API");
198                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
199         }
200
201         user_info = _bt_get_user_data(BT_COMMON);
202         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
203
204         BT_INIT_PARAMS();
205         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
206
207         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
208
209         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_SEARCH_SERVICE,
210                 in_param1, in_param2, in_param3, in_param4,
211                 user_info->cb, user_info->user_data);
212
213         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
214
215         return result;
216 }
217
218 BT_EXPORT_API int bluetooth_cancel_service_search(void)
219 {
220         int result;
221
222         BT_CHECK_ENABLED_ANY(return);
223
224         BT_INIT_PARAMS();
225         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
226
227         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_SEARCH_SERVICE,
228                 in_param1, in_param2, in_param3, in_param4, &out_param);
229
230         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
231
232         return result;
233 }
234
235 BT_EXPORT_API int bluetooth_set_alias(const bluetooth_device_address_t *device_address,
236                                       const char *alias)
237 {
238         int result;
239         char alias_name[BLUETOOTH_DEVICE_NAME_LENGTH_MAX];
240
241         BT_CHECK_PARAMETER(device_address, return);
242         BT_CHECK_PARAMETER(alias, return);
243         BT_CHECK_ENABLED_ANY(return);
244
245         BT_INIT_PARAMS();
246         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
247
248         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
249         g_strlcpy(alias_name, alias, sizeof(alias_name));
250         g_array_append_vals(in_param2, alias_name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
251
252         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ALIAS,
253                 in_param1, in_param2, in_param3, in_param4, &out_param);
254
255         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
256
257         return result;
258 }
259
260 BT_EXPORT_API int bluetooth_authorize_device(const bluetooth_device_address_t *device_address,
261                                              gboolean authorized)
262 {
263         int result;
264
265         BT_CHECK_PARAMETER(device_address, return);
266         BT_CHECK_ENABLED_ANY(return);
267
268         BT_INIT_PARAMS();
269         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
270
271         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
272         g_array_append_vals(in_param2, &authorized, sizeof(gboolean));
273
274         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_AUTHORIZATION,
275                 in_param1, in_param2, in_param3, in_param4, &out_param);
276
277         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
278
279         return result;
280 }
281
282 BT_EXPORT_API int bluetooth_is_device_connected(const bluetooth_device_address_t *device_address,
283                                 bluetooth_service_type_t type,
284                                 gboolean *is_connected)
285 {
286         int result;
287
288         BT_CHECK_PARAMETER(device_address, return);
289         BT_CHECK_PARAMETER(is_connected, return);
290         BT_CHECK_ENABLED_ANY(return);
291
292 #ifdef RFCOMM_DIRECT
293         if (type & BLUETOOTH_RFCOMM_SERVICE) {
294                 result = bluetooth_rfcomm_client_is_connected(device_address, is_connected);
295                 if (*is_connected == FALSE)
296                         result = bluetooth_rfcomm_server_is_connected(device_address, is_connected);
297
298                 return result;
299         }
300 #endif
301
302         BT_INIT_PARAMS();
303         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
304
305         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
306         g_array_append_vals(in_param2, &type, sizeof(int));
307
308         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_DEVICE_CONNECTED,
309                 in_param1, in_param2, in_param3, in_param4, &out_param);
310
311         if (result == BLUETOOTH_ERROR_NONE) {
312                 *is_connected = g_array_index(out_param, gboolean, 0);
313         }
314
315         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
316
317         return result;
318 }
319
320 BT_EXPORT_API int bluetooth_connect_le(const bluetooth_device_address_t *device_address, gboolean auto_connect)
321 {
322         int result;
323         bt_user_info_t *user_info;
324
325         BT_CHECK_PARAMETER(device_address, return);
326         BT_CHECK_ENABLED_ANY(return);
327
328         BT_INIT_PARAMS();
329         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
330
331         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
332         g_array_append_vals(in_param2, &auto_connect, sizeof(gboolean));
333
334         user_info = _bt_get_user_data(BT_COMMON);
335         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
336
337         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_CONNECT_LE,
338                 in_param1, in_param2, in_param3, in_param4,
339                 user_info->cb, user_info->user_data);
340
341         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
342
343         return result;
344 }
345
346 BT_EXPORT_API int bluetooth_disconnect_le(const bluetooth_device_address_t *device_address)
347 {
348         int result;
349         bt_user_info_t *user_info;
350
351         BT_CHECK_PARAMETER(device_address, return);
352         BT_CHECK_ENABLED_ANY(return);
353
354         BT_INIT_PARAMS();
355         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
356
357         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
358
359         user_info = _bt_get_user_data(BT_COMMON);
360         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
361
362         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_DISCONNECT_LE,
363                 in_param1, in_param2, in_param3, in_param4,
364                 user_info->cb, user_info->user_data);
365
366         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
367
368         return result;
369 }
370
371 BT_EXPORT_API int bluetooth_enable_rssi(const bluetooth_device_address_t *remote_address,
372                 int link_type, bt_rssi_threshold_t *rssi_threshold)
373 {
374         int result;
375
376         BT_CHECK_ENABLED_ANY(return);
377
378         BT_INIT_PARAMS();
379         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
380         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
381         g_array_append_vals(in_param2, &link_type, sizeof(int));
382         g_array_append_vals(in_param3, rssi_threshold, sizeof(bt_rssi_threshold_t));
383
384         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_RSSI,
385                 in_param1, in_param2, in_param3, in_param4, &out_param);
386
387         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
388
389         return result;
390 }
391
392 BT_EXPORT_API int bluetooth_get_rssi_strength(const bluetooth_device_address_t *remote_address, int link_type)
393 {
394         int result;
395
396         BT_CHECK_ENABLED_ANY(return);
397
398         BT_INIT_PARAMS();
399         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
400         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
401         g_array_append_vals(in_param2, &link_type, sizeof(int));
402
403         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_RSSI,
404                 in_param1, in_param2, in_param3, in_param4, &out_param);
405
406         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
407
408         return result;
409 }
410
411 BT_EXPORT_API int bluetooth_le_conn_update(const bluetooth_device_address_t *address,
412                                         const bluetooth_le_connection_param_t *parameters)
413 {
414         int result;
415
416         BT_CHECK_ENABLED_ANY(return);
417         BT_CHECK_PARAMETER(address, return);
418         BT_CHECK_PARAMETER(parameters, return);
419
420         BT_INIT_PARAMS();
421         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
422
423         g_array_append_vals(in_param1, address,
424                         sizeof(bluetooth_device_address_t));
425         g_array_append_vals(in_param2, parameters,
426                         sizeof(bluetooth_le_connection_param_t));
427
428         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_LE_CONN_UPDATE,
429                         in_param1, in_param2, in_param3, in_param4, &out_param);
430
431         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
432
433         return result;
434 }
435
436 BT_EXPORT_API int bluetooth_get_connected_link_type(
437                 const bluetooth_device_address_t *device_address,
438                 bluetooth_connected_link_t *connected)
439 {
440         int result;
441
442         BT_CHECK_PARAMETER(device_address, return);
443         BT_CHECK_ENABLED_ANY(return);
444
445         BT_INIT_PARAMS();
446         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
447
448         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
449
450         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_CONNECTED_LINK_TYPE,
451                         in_param1, in_param2, in_param3, in_param4, &out_param);
452
453         if (result == BLUETOOTH_ERROR_NONE) {
454                 *connected = g_array_index(out_param, guint, 0);
455         }
456
457         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
458
459         return result;
460 }
461
462 BT_EXPORT_API int bluetooth_set_pin_code(
463                 const bluetooth_device_address_t *device_address,
464                 const bluetooth_device_pin_code_t *pin_code)
465 {
466         int result;
467
468         BT_CHECK_PARAMETER(device_address, return);
469         BT_CHECK_PARAMETER(pin_code, return);
470         BT_CHECK_ENABLED(return);
471
472         BT_INIT_PARAMS();
473         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
474
475         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
476         g_array_append_vals(in_param2, pin_code, sizeof(bluetooth_device_pin_code_t));
477
478         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_PIN_CODE,
479                 in_param1, in_param2, in_param3, in_param4, &out_param);
480
481         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
482
483         return result;
484 }
485
486 BT_EXPORT_API int bluetooth_unset_pin_code(
487                 const bluetooth_device_address_t *device_address)
488 {
489         int result;
490
491         BT_CHECK_PARAMETER(device_address, return);
492         BT_CHECK_ENABLED(return);
493
494         BT_INIT_PARAMS();
495         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
496
497         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
498
499         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNSET_PIN_CODE,
500                 in_param1, in_param2, in_param3, in_param4, &out_param);
501
502         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
503
504         return result;
505 }
506
507 BT_EXPORT_API int bluetooth_update_le_connection_mode(const bluetooth_device_address_t *address,
508                 const bluetooth_le_connection_mode_t mode)
509 {
510         int result;
511
512         BT_CHECK_ENABLED(return);
513         BT_CHECK_PARAMETER(address, return);
514
515         BT_INIT_PARAMS();
516         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
517
518         g_array_append_vals(in_param1, address,
519                         sizeof(bluetooth_device_address_t));
520         g_array_append_vals(in_param2, &mode,
521                         sizeof(bluetooth_le_connection_mode_t));
522
523         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UPDATE_LE_CONNECTION_MODE,
524                         in_param1, in_param2, in_param3, in_param4, &out_param);
525
526         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
527
528         return result;
529 }
530
531 BT_EXPORT_API int bluetooth_passkey_reply(char *passkey, gboolean reply)
532 {
533         int result;
534
535         char str_passkey[BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX];
536
537         BT_CHECK_PARAMETER(passkey, return);
538         BT_CHECK_ENABLED(return);
539
540         BT_INIT_PARAMS();
541         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
542
543         g_strlcpy(str_passkey, passkey, sizeof(str_passkey));
544         g_array_append_vals(in_param1, str_passkey, BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX);
545         g_array_append_vals(in_param2, &reply, sizeof(gboolean));
546
547         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_REPLY,
548                 in_param1, in_param2, in_param3, in_param4, &out_param);
549
550         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
551
552         return result;
553 }
554
555 BT_EXPORT_API int bluetooth_passkey_confirmation_reply(gboolean reply)
556 {
557         int result;
558
559         BT_CHECK_ENABLED(return);
560         BT_INIT_PARAMS();
561
562         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
563         g_array_append_vals(in_param1, &reply, sizeof(gboolean));
564         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_CONFIRMATION_REPLY,
565                         in_param1, in_param2, in_param3, in_param4, &out_param);
566         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
567
568         return result;
569 }