2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "bluetooth-api.h"
19 #include "bt-internal-types.h"
21 #include "bt-common.h"
22 #include "bt-request-sender.h"
23 #include "bt-event-handler.h"
25 #ifdef TIZEN_DPM_ENABLE
29 BT_EXPORT_API int bluetooth_bond_device(const bluetooth_device_address_t *device_address)
32 bt_user_info_t *user_info;
34 BT_CHECK_PARAMETER(device_address, return);
35 BT_CHECK_ENABLED(return);
37 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE)
38 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
39 BT_ERR("Don't have a privilege to use this API");
40 return BLUETOOTH_ERROR_PERMISSION_DEINED;
43 #ifdef TIZEN_DPM_ENABLE
44 if (_bt_check_dpm(BT_DPM_PAIRING, NULL) == BT_DPM_RESTRICTED) {
45 BT_ERR("Not allow to pair the device");
46 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
49 if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)device_address) == BT_DPM_RESTRICTED) {
50 BT_ERR("Blacklist device");
51 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
55 user_info = _bt_get_user_data(BT_COMMON);
56 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
59 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
61 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
63 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE,
64 in_param1, in_param2, in_param3, in_param4,
65 user_info->cb, user_info->user_data);
67 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
72 BT_EXPORT_API int bluetooth_bond_device_by_type(
73 const bluetooth_device_address_t *device_address,
74 bluetooth_conn_type_t conn_type)
77 bt_user_info_t *user_info;
79 BT_CHECK_PARAMETER(device_address, return);
80 if(conn_type == BLUETOOTH_DEV_CONN_BREDR)
81 BT_CHECK_ENABLED(return);
82 else if(conn_type == BLUETOOTH_DEV_CONN_LE)
83 BT_CHECK_ENABLED_LE(return);
84 else if(conn_type == BLUETOOTH_DEV_CONN_DEFAULT) {
85 BT_CHECK_ENABLED(return);
86 BT_CHECK_ENABLED_LE(return);
89 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE_BY_TYPE)
90 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
91 BT_ERR("Don't have a privilege to use this API");
92 return BLUETOOTH_ERROR_PERMISSION_DEINED;
96 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
98 user_info = _bt_get_user_data(BT_COMMON);
99 retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
101 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
102 g_array_append_vals(in_param2, &conn_type, sizeof(unsigned short));
104 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE_BY_TYPE,
105 in_param1, in_param2, in_param3, in_param4,
106 user_info->cb, user_info->user_data);
108 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
113 BT_EXPORT_API int bluetooth_cancel_bonding(void)
117 BT_CHECK_ENABLED_ANY(return);
120 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
122 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_BONDING,
123 in_param1, in_param2, in_param3, in_param4, &out_param);
125 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
130 BT_EXPORT_API int bluetooth_unbond_device(const bluetooth_device_address_t *device_address)
133 bt_user_info_t *user_info;
135 BT_CHECK_PARAMETER(device_address, return);
136 BT_CHECK_ENABLED_ANY(return);
138 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_UNBOND_DEVICE)
139 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
140 BT_ERR("Don't have a privilege to use this API");
141 return BLUETOOTH_ERROR_PERMISSION_DEINED;
144 user_info = _bt_get_user_data(BT_COMMON);
145 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
148 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
150 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
152 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_UNBOND_DEVICE,
153 in_param1, in_param2, in_param3, in_param4,
154 user_info->cb, user_info->user_data);
156 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
161 BT_EXPORT_API int bluetooth_get_bonded_device(const bluetooth_device_address_t *device_address,
162 bluetooth_device_info_t *dev_info)
166 BT_CHECK_PARAMETER(device_address, return);
167 BT_CHECK_PARAMETER(dev_info, return);
168 BT_CHECK_ENABLED_ANY(return);
171 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
173 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
175 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_BONDED_DEVICE,
176 in_param1, in_param2, in_param3, in_param4, &out_param);
178 if (result == BLUETOOTH_ERROR_NONE) {
179 if (out_param->len > 0) {
180 *dev_info = g_array_index(out_param,
181 bluetooth_device_info_t, 0);
183 BT_ERR("out_param length is 0!!");
187 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
192 BT_EXPORT_API int bluetooth_get_remote_device(const bluetooth_device_address_t *device_address)
194 return BLUETOOTH_ERROR_NONE;
197 BT_EXPORT_API int bluetooth_search_service(const bluetooth_device_address_t *device_address)
200 bt_user_info_t *user_info;
202 BT_CHECK_PARAMETER(device_address, return);
203 BT_CHECK_ENABLED_ANY(return);
205 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SEARCH_SERVICE)
206 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
207 BT_ERR("Don't have a privilege to use this API");
208 return BLUETOOTH_ERROR_PERMISSION_DEINED;
211 user_info = _bt_get_user_data(BT_COMMON);
212 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
215 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
217 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
219 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_SEARCH_SERVICE,
220 in_param1, in_param2, in_param3, in_param4,
221 user_info->cb, user_info->user_data);
223 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
228 BT_EXPORT_API int bluetooth_cancel_service_search(void)
232 BT_CHECK_ENABLED_ANY(return);
235 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
237 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_SEARCH_SERVICE,
238 in_param1, in_param2, in_param3, in_param4, &out_param);
240 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
245 BT_EXPORT_API int bluetooth_set_alias(const bluetooth_device_address_t *device_address,
249 char alias_name[BLUETOOTH_DEVICE_NAME_LENGTH_MAX];
251 BT_CHECK_PARAMETER(device_address, return);
252 BT_CHECK_PARAMETER(alias, return);
253 BT_CHECK_ENABLED_ANY(return);
256 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
258 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
259 g_strlcpy(alias_name, alias, sizeof(alias_name));
260 g_array_append_vals(in_param2, alias_name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
262 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ALIAS,
263 in_param1, in_param2, in_param3, in_param4, &out_param);
265 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
270 BT_EXPORT_API int bluetooth_authorize_device(const bluetooth_device_address_t *device_address,
275 BT_CHECK_PARAMETER(device_address, return);
276 BT_CHECK_ENABLED_ANY(return);
279 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
281 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
282 g_array_append_vals(in_param2, &authorized, sizeof(gboolean));
284 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_AUTHORIZATION,
285 in_param1, in_param2, in_param3, in_param4, &out_param);
287 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
292 BT_EXPORT_API int bluetooth_is_device_connected(const bluetooth_device_address_t *device_address,
293 bluetooth_service_type_t type,
294 gboolean *is_connected)
298 BT_CHECK_PARAMETER(device_address, return);
299 BT_CHECK_PARAMETER(is_connected, return);
300 BT_CHECK_ENABLED_ANY(return);
303 if (type & BLUETOOTH_RFCOMM_SERVICE) {
304 result = bluetooth_rfcomm_client_is_connected(device_address, is_connected);
305 if (*is_connected == FALSE)
306 result = bluetooth_rfcomm_server_is_connected(device_address, is_connected);
313 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
315 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
316 g_array_append_vals(in_param2, &type, sizeof(int));
318 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_DEVICE_CONNECTED,
319 in_param1, in_param2, in_param3, in_param4, &out_param);
321 if (result == BLUETOOTH_ERROR_NONE) {
322 *is_connected = g_array_index(out_param, gboolean, 0);
325 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
330 BT_EXPORT_API int bluetooth_connect_le(const bluetooth_device_address_t *device_address, gboolean auto_connect)
333 bt_user_info_t *user_info;
335 BT_CHECK_PARAMETER(device_address, return);
336 BT_CHECK_ENABLED_ANY(return);
338 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_CONNECT_LE)
339 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
340 BT_ERR("Don't have a privilege to use this API");
341 return BLUETOOTH_ERROR_PERMISSION_DEINED;
345 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
347 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
348 g_array_append_vals(in_param2, &auto_connect, sizeof(gboolean));
350 user_info = _bt_get_user_data(BT_COMMON);
351 retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
353 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_CONNECT_LE,
354 in_param1, in_param2, in_param3, in_param4,
355 user_info->cb, user_info->user_data);
357 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
362 BT_EXPORT_API int bluetooth_disconnect_le(const bluetooth_device_address_t *device_address)
365 bt_user_info_t *user_info;
367 BT_CHECK_PARAMETER(device_address, return);
368 BT_CHECK_ENABLED_ANY(return);
371 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
373 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
375 user_info = _bt_get_user_data(BT_COMMON);
376 retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
378 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_DISCONNECT_LE,
379 in_param1, in_param2, in_param3, in_param4,
380 user_info->cb, user_info->user_data);
382 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
387 BT_EXPORT_API int bluetooth_enable_rssi(const bluetooth_device_address_t *remote_address,
388 int link_type, bt_rssi_threshold_t *rssi_threshold)
392 BT_CHECK_ENABLED_ANY(return);
395 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
396 g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
397 g_array_append_vals(in_param2, &link_type, sizeof(int));
398 g_array_append_vals(in_param3, rssi_threshold, sizeof(bt_rssi_threshold_t));
400 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_RSSI,
401 in_param1, in_param2, in_param3, in_param4, &out_param);
403 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
408 BT_EXPORT_API int bluetooth_get_rssi_strength(const bluetooth_device_address_t *remote_address, int link_type)
412 BT_CHECK_ENABLED_ANY(return);
415 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
416 g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
417 g_array_append_vals(in_param2, &link_type, sizeof(int));
419 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_RSSI,
420 in_param1, in_param2, in_param3, in_param4, &out_param);
422 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
427 BT_EXPORT_API int bluetooth_le_conn_update(const bluetooth_device_address_t *address,
428 const bluetooth_le_connection_param_t *parameters)
432 BT_CHECK_ENABLED_ANY(return);
433 BT_CHECK_PARAMETER(address, return);
434 BT_CHECK_PARAMETER(parameters, return);
437 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
439 g_array_append_vals(in_param1, address,
440 sizeof(bluetooth_device_address_t));
441 g_array_append_vals(in_param2, parameters,
442 sizeof(bluetooth_le_connection_param_t));
444 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_LE_CONN_UPDATE,
445 in_param1, in_param2, in_param3, in_param4, &out_param);
447 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
452 BT_EXPORT_API int bluetooth_get_connected_link_type(
453 const bluetooth_device_address_t *device_address,
454 bluetooth_connected_link_t *connected)
458 BT_CHECK_PARAMETER(device_address, return);
459 BT_CHECK_ENABLED_ANY(return);
462 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
464 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
466 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_CONNECTED_LINK_TYPE,
467 in_param1, in_param2, in_param3, in_param4, &out_param);
469 if (result == BLUETOOTH_ERROR_NONE) {
470 *connected = g_array_index(out_param, guint, 0);
473 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
478 BT_EXPORT_API int bluetooth_set_pin_code(
479 const bluetooth_device_address_t *device_address,
480 const bluetooth_device_pin_code_t *pin_code)
484 BT_CHECK_PARAMETER(device_address, return);
485 BT_CHECK_PARAMETER(pin_code, return);
486 BT_CHECK_ENABLED(return);
489 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
491 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
492 g_array_append_vals(in_param2, pin_code, sizeof(bluetooth_device_pin_code_t));
494 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_PIN_CODE,
495 in_param1, in_param2, in_param3, in_param4, &out_param);
497 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
502 BT_EXPORT_API int bluetooth_unset_pin_code(
503 const bluetooth_device_address_t *device_address)
507 BT_CHECK_PARAMETER(device_address, return);
508 BT_CHECK_ENABLED(return);
511 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
513 g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
515 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNSET_PIN_CODE,
516 in_param1, in_param2, in_param3, in_param4, &out_param);
518 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
523 BT_EXPORT_API int bluetooth_update_le_connection_mode(const bluetooth_device_address_t *address,
524 const bluetooth_le_connection_mode_t mode)
528 BT_CHECK_ENABLED(return);
529 BT_CHECK_PARAMETER(address, return);
532 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
534 g_array_append_vals(in_param1, address,
535 sizeof(bluetooth_device_address_t));
536 g_array_append_vals(in_param2, &mode,
537 sizeof(bluetooth_le_connection_mode_t));
539 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UPDATE_LE_CONNECTION_MODE,
540 in_param1, in_param2, in_param3, in_param4, &out_param);
542 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
547 BT_EXPORT_API int bluetooth_passkey_reply(char *passkey, gboolean reply)
551 char str_passkey[BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX];
553 BT_CHECK_PARAMETER(passkey, return);
554 BT_CHECK_ENABLED(return);
557 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
559 g_strlcpy(str_passkey, passkey, sizeof(str_passkey));
560 g_array_append_vals(in_param1, str_passkey, BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX);
561 g_array_append_vals(in_param2, &reply, sizeof(gboolean));
563 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_REPLY,
564 in_param1, in_param2, in_param3, in_param4, &out_param);
566 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
571 BT_EXPORT_API int bluetooth_passkey_confirmation_reply(gboolean reply)
575 BT_CHECK_ENABLED(return);
578 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
579 g_array_append_vals(in_param1, &reply, sizeof(gboolean));
580 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_CONFIRMATION_REPLY,
581 in_param1, in_param2, in_param3, in_param4, &out_param);
582 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);