4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-common.h"
31 #include "bt-request-sender.h"
32 #include "bt-event-handler.h"
34 static gboolean is_le_scanning = FALSE;
36 BT_EXPORT_API int bluetooth_check_adapter_le(void)
41 ret = _bt_get_adapter_path(_bt_gdbus_get_system_gconn(), NULL);
43 if (ret != BLUETOOTH_ERROR_NONE) {
44 return BLUETOOTH_ADAPTER_LE_DISABLED;
47 ret = vconf_get_int(VCONFKEY_BT_LE_STATUS, &value);
49 BT_ERR("fail to get vconf key!");
53 BT_DBG("value : %d", value);
54 return value == VCONFKEY_BT_LE_STATUS_ON ? BLUETOOTH_ADAPTER_LE_ENABLED :
55 BLUETOOTH_ADAPTER_LE_DISABLED;
58 BT_EXPORT_API int bluetooth_enable_adapter_le(void)
62 retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_ENABLED,
63 BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED);
66 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
68 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_ADAPTER_LE,
69 in_param1, in_param2, in_param3, in_param4, &out_param);
71 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
75 BT_EXPORT_API int bluetooth_disable_adapter_le(void)
78 retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED,
79 BLUETOOTH_ERROR_DEVICE_NOT_ENABLED);
82 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
84 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_DISABLE_ADAPTER_LE,
85 in_param1, in_param2, in_param3, in_param4, &out_param);
87 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
92 void _bt_set_le_scan_status(gboolean mode)
94 BT_DBG("set LE scan mode : %d", mode);
95 is_le_scanning = mode;
98 BT_EXPORT_API gboolean bluetooth_is_le_scanning(void)
100 return is_le_scanning;
103 BT_EXPORT_API int bluetooth_start_le_discovery(void)
107 BT_CHECK_ENABLED_ANY(return);
110 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
112 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_LE_DISCOVERY,
113 in_param1, in_param2, in_param3, in_param4, &out_param);
115 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
117 if (result == BLUETOOTH_ERROR_NONE)
118 _bt_set_le_scan_status(TRUE);
123 BT_EXPORT_API int bluetooth_stop_le_discovery(void)
127 BT_CHECK_ENABLED_ANY(return);
130 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
132 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_STOP_LE_DISCOVERY,
133 in_param1, in_param2, in_param3, in_param4, &out_param);
135 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
137 if (result == BLUETOOTH_ERROR_NONE)
138 _bt_set_le_scan_status(FALSE);
143 BT_EXPORT_API int bluetooth_is_le_discovering(void)
146 int is_discovering = FALSE;
148 BT_CHECK_ENABLED_ANY(return);
151 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
153 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_LE_DISCOVERYING,
154 in_param1, in_param2, in_param3, in_param4, &out_param);
156 if (result == BLUETOOTH_ERROR_NONE) {
157 is_discovering = g_array_index(out_param,
160 BT_ERR("Fail to send request");
163 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
165 return is_discovering;
168 BT_EXPORT_API int bluetooth_register_scan_filter(bluetooth_le_scan_filter_t *filter, int *slot_id)
172 BT_CHECK_ENABLED_ANY(return);
175 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
177 g_array_append_vals(in_param1, filter, sizeof(bluetooth_le_scan_filter_t));
179 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REGISTER_SCAN_FILTER,
180 in_param1, in_param2, in_param3, in_param4, &out_param);
182 if (result == BLUETOOTH_ERROR_NONE) {
183 *slot_id = g_array_index(out_param, int, 0);
185 BT_ERR("Fail to send request");
188 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
193 BT_EXPORT_API int bluetooth_unregister_scan_filter(int slot_id)
197 BT_CHECK_ENABLED_ANY(return);
200 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
202 g_array_append_vals(in_param1, &slot_id, sizeof(int));
204 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_SCAN_FILTER,
205 in_param1, in_param2, in_param3, in_param4, &out_param);
207 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
212 BT_EXPORT_API int bluetooth_unregister_all_scan_filters(void)
216 BT_CHECK_ENABLED_ANY(return);
219 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
221 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_ALL_SCAN_FILTERS,
222 in_param1, in_param2, in_param3, in_param4, &out_param);
224 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
229 #ifdef TIZEN_WEARABLE
230 gboolean __bluetooth_is_privileged_process(void)
233 char path[30] = {0, };
234 char buf[256] = {0, };
236 snprintf(path, sizeof(path), "/proc/%d/cmdline", getpid());
237 fp = fopen(path, "r");
241 if (fgets(buf, 256, fp) != NULL) {
242 if (strstr(buf, "weconnd") != NULL) {
253 BT_EXPORT_API int bluetooth_set_advertising(int handle, gboolean enable)
256 gboolean use_reserved_slot = FALSE;
258 BT_CHECK_ENABLED_ANY(return);
260 #ifdef TIZEN_WEARABLE
261 use_reserved_slot = __bluetooth_is_privileged_process();
265 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
267 g_array_append_vals(in_param1, &handle, sizeof(int));
268 g_array_append_vals(in_param2, &enable, sizeof(gboolean));
269 g_array_append_vals(in_param3, &use_reserved_slot, sizeof(gboolean));
271 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING,
272 in_param1, in_param2, in_param3, in_param4, &out_param);
274 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
279 BT_EXPORT_API int bluetooth_set_custom_advertising(int handle, gboolean enable,
280 bluetooth_advertising_params_t *params)
283 gboolean use_reserved_slot = FALSE;
285 BT_CHECK_ENABLED_ANY(return);
287 #ifdef TIZEN_WEARABLE
288 use_reserved_slot = __bluetooth_is_privileged_process();
292 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
294 g_array_append_vals(in_param1, &handle, sizeof(int));
295 g_array_append_vals(in_param2, &enable, sizeof(gboolean));
296 g_array_append_vals(in_param3, params, sizeof(bluetooth_advertising_params_t));
297 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
299 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_CUSTOM_ADVERTISING,
300 in_param1, in_param2, in_param3, in_param4, &out_param);
302 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
307 BT_EXPORT_API int bluetooth_get_advertising_data(bluetooth_advertising_data_t *adv_data, int *length)
312 BT_CHECK_PARAMETER(adv_data, return);
313 BT_CHECK_PARAMETER(length, return);
314 BT_CHECK_ENABLED_ANY(return);
317 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
319 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_ADVERTISING_DATA,
320 in_param1, in_param2, in_param3, in_param4, &out_param);
322 if (result == BLUETOOTH_ERROR_NONE) {
323 data = &g_array_index(out_param, guint8, 0);
324 *length = out_param->len;
326 memset(adv_data, 0x00, sizeof(bluetooth_advertising_data_t));
327 memcpy(adv_data->data, data, *length);
330 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
335 BT_EXPORT_API int bluetooth_set_advertising_data(int handle, const bluetooth_advertising_data_t *value, int length)
338 gboolean use_reserved_slot = FALSE;
340 BT_CHECK_PARAMETER(value, return);
341 BT_CHECK_ENABLED_ANY(return);
343 if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 3)
344 return BLUETOOTH_ERROR_INVALID_PARAM;
346 #ifdef TIZEN_WEARABLE
347 use_reserved_slot = __bluetooth_is_privileged_process();
351 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
353 g_array_append_vals(in_param1, &handle, sizeof(int));
354 g_array_append_vals(in_param2, value, sizeof(bluetooth_advertising_data_t));
355 g_array_append_vals(in_param3, &length, sizeof(int));
356 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
358 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING_DATA,
359 in_param1, in_param2, in_param3, in_param4, &out_param);
361 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
366 BT_EXPORT_API int bluetooth_get_scan_response_data(bluetooth_scan_resp_data_t *value, int *length)
371 BT_CHECK_PARAMETER(value, return);
372 BT_CHECK_PARAMETER(length, return);
373 BT_CHECK_ENABLED_ANY(return);
376 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
378 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_SCAN_RESPONSE_DATA,
379 in_param1, in_param2, in_param3, in_param4, &out_param);
381 if (result == BLUETOOTH_ERROR_NONE) {
382 data = &g_array_index(out_param, guint8, 0);
383 *length = out_param->len;
385 memset(value, 0x00, sizeof(bluetooth_scan_resp_data_t));
386 memcpy(value->data, data, *length);
389 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
394 BT_EXPORT_API int bluetooth_set_scan_response_data(int handle,
395 const bluetooth_scan_resp_data_t *value, int length)
398 gboolean use_reserved_slot = FALSE;
400 BT_CHECK_PARAMETER(value, return);
401 BT_CHECK_ENABLED_ANY(return);
403 if (length > BLUETOOTH_SCAN_RESP_DATA_LENGTH_MAX)
404 return BLUETOOTH_ERROR_INVALID_PARAM;
406 #ifdef TIZEN_WEARABLE
407 use_reserved_slot = __bluetooth_is_privileged_process();
411 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
413 g_array_append_vals(in_param1, &handle, sizeof(int));
414 g_array_append_vals(in_param2, value, length);
415 g_array_append_vals(in_param3, &length, sizeof(int));
416 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
418 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_RESPONSE_DATA,
419 in_param1, in_param2, in_param3, in_param4, &out_param);
421 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
426 BT_EXPORT_API int bluetooth_set_scan_parameters(bluetooth_le_scan_params_t *params)
430 BT_CHECK_ENABLED_ANY(return);
433 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
435 g_array_append_vals(in_param1, params, sizeof(bluetooth_le_scan_params_t));
437 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_PARAMETERS,
438 in_param1, in_param2, in_param3, in_param4, &out_param);
440 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
445 BT_EXPORT_API int bluetooth_is_advertising(gboolean *is_advertising)
449 BT_CHECK_ENABLED_ANY(return);
452 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
454 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_ADVERTISING,
455 in_param1, in_param2, in_param3, in_param4, &out_param);
457 if (result == BLUETOOTH_ERROR_NONE) {
458 *is_advertising = g_array_index(out_param, int, 0);
460 BT_ERR("Fail to send request");
463 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
468 BT_EXPORT_API int bluetooth_add_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
472 BT_CHECK_PARAMETER(address, return);
473 BT_CHECK_ENABLED_ANY(return);
476 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
478 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
479 g_array_append_vals(in_param2, &address_type, sizeof(int));
481 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ADD_WHITE_LIST,
482 in_param1, in_param2, in_param3, in_param4, &out_param);
484 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
489 BT_EXPORT_API int bluetooth_remove_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
493 BT_CHECK_PARAMETER(address, return);
494 BT_CHECK_ENABLED_ANY(return);
497 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
499 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
500 g_array_append_vals(in_param2, &address_type, sizeof(int));
502 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REMOVE_WHITE_LIST,
503 in_param1, in_param2, in_param3, in_param4, &out_param);
505 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
510 BT_EXPORT_API int bluetooth_clear_white_list(void)
514 BT_CHECK_ENABLED_ANY(return);
517 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
519 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CLEAR_WHITE_LIST,
520 in_param1, in_param2, in_param3, in_param4, &out_param);
522 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
527 BT_EXPORT_API int bluetooth_enable_le_privacy(gboolean enable_privacy)
531 BT_CHECK_ENABLED_ANY(return);
534 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
536 g_array_append_vals(in_param1, &enable_privacy, sizeof(gboolean));
538 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_LE_PRIVACY,
539 in_param1, in_param2, in_param3, in_param4, &out_param);
541 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
546 BT_EXPORT_API int bluetooth_check_privilege_advertising_parameter(void)
548 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SET_ADVERTISING_PARAMETERS)
549 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
550 BT_ERR("Don't have a privilege to use this API");
551 return BLUETOOTH_ERROR_PERMISSION_DEINED;
554 return BLUETOOTH_ERROR_NONE;
557 BT_EXPORT_API int bluetooth_le_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
561 ret = _bt_register_event(BT_LE_ADAPTER_EVENT, (void *)callback_ptr, user_data);
562 if (ret != BLUETOOTH_ERROR_NONE &&
563 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
564 BT_ERR("Fail to register BT_LE_ADAPTER_EVENT event : %d", ret);
568 return BLUETOOTH_ERROR_NONE;
571 BT_EXPORT_API int bluetooth_le_unregister_callback(void)
573 _bt_unregister_event(BT_LE_ADAPTER_EVENT);
575 return BLUETOOTH_ERROR_NONE;