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 #ifdef ENABLE_TIZEN_2_4
48 ret = vconf_get_int(VCONFKEY_BT_LE_STATUS, &value);
50 BT_ERR("fail to get vconf key!");
54 BT_DBG("value : %d", value);
55 return value == VCONFKEY_BT_LE_STATUS_ON ? BLUETOOTH_ADAPTER_LE_ENABLED :
56 BLUETOOTH_ADAPTER_LE_DISABLED;
58 return value = BLUETOOTH_ADAPTER_LE_DISABLED;
62 BT_EXPORT_API int bluetooth_enable_adapter_le(void)
66 retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_ENABLED,
67 BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED);
70 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
72 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_ADAPTER_LE,
73 in_param1, in_param2, in_param3, in_param4, &out_param);
75 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
79 BT_EXPORT_API int bluetooth_disable_adapter_le(void)
82 retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED,
83 BLUETOOTH_ERROR_DEVICE_NOT_ENABLED);
86 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_DISABLE_ADAPTER_LE,
89 in_param1, in_param2, in_param3, in_param4, &out_param);
91 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
96 void _bt_set_le_scan_status(gboolean mode)
98 BT_DBG("set LE scan mode : %d", mode);
99 is_le_scanning = mode;
102 BT_EXPORT_API gboolean bluetooth_is_le_scanning(void)
104 return is_le_scanning;
107 BT_EXPORT_API int bluetooth_start_le_discovery(void)
111 BT_CHECK_ENABLED_ANY(return);
114 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
116 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_LE_DISCOVERY,
117 in_param1, in_param2, in_param3, in_param4, &out_param);
119 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
121 if (result == BLUETOOTH_ERROR_NONE)
122 _bt_set_le_scan_status(TRUE);
127 BT_EXPORT_API int bluetooth_stop_le_discovery(void)
131 BT_CHECK_ENABLED_ANY(return);
134 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
136 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_STOP_LE_DISCOVERY,
137 in_param1, in_param2, in_param3, in_param4, &out_param);
139 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141 if (result == BLUETOOTH_ERROR_NONE)
142 _bt_set_le_scan_status(FALSE);
147 BT_EXPORT_API int bluetooth_is_le_discovering(void)
150 int is_discovering = FALSE;
152 BT_CHECK_ENABLED_ANY(return);
155 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
157 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_LE_DISCOVERYING,
158 in_param1, in_param2, in_param3, in_param4, &out_param);
160 if (result == BLUETOOTH_ERROR_NONE) {
161 is_discovering = g_array_index(out_param,
164 BT_ERR("Fail to send request");
167 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
169 return is_discovering;
172 BT_EXPORT_API int bluetooth_register_scan_filter(bluetooth_le_scan_filter_t *filter, int *slot_id)
176 BT_CHECK_ENABLED_ANY(return);
179 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
181 g_array_append_vals(in_param1, filter, sizeof(bluetooth_le_scan_filter_t));
183 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REGISTER_SCAN_FILTER,
184 in_param1, in_param2, in_param3, in_param4, &out_param);
186 if (result == BLUETOOTH_ERROR_NONE) {
187 *slot_id = g_array_index(out_param, int, 0);
189 BT_ERR("Fail to send request");
192 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
197 BT_EXPORT_API int bluetooth_unregister_scan_filter(int slot_id)
201 BT_CHECK_ENABLED_ANY(return);
204 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
206 g_array_append_vals(in_param1, &slot_id, sizeof(int));
208 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_SCAN_FILTER,
209 in_param1, in_param2, in_param3, in_param4, &out_param);
211 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
216 BT_EXPORT_API int bluetooth_unregister_all_scan_filters(void)
220 BT_CHECK_ENABLED_ANY(return);
223 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
225 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_ALL_SCAN_FILTERS,
226 in_param1, in_param2, in_param3, in_param4, &out_param);
228 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
233 #ifdef TIZEN_WEARABLE
234 gboolean __bluetooth_is_privileged_process(void)
237 char path[30] = {0, };
238 char buf[256] = {0, };
240 snprintf(path, sizeof(path), "/proc/%d/cmdline", getpid());
241 fp = fopen(path, "r");
245 if (fgets(buf, 256, fp) != NULL) {
246 if (strstr(buf, "weconnd") != NULL) {
257 BT_EXPORT_API int bluetooth_set_advertising(int handle, gboolean enable)
260 gboolean use_reserved_slot = FALSE;
262 BT_CHECK_ENABLED_ANY(return);
264 #ifdef TIZEN_WEARABLE
265 use_reserved_slot = __bluetooth_is_privileged_process();
269 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
271 g_array_append_vals(in_param1, &handle, sizeof(int));
272 g_array_append_vals(in_param2, &enable, sizeof(gboolean));
273 g_array_append_vals(in_param3, &use_reserved_slot, sizeof(gboolean));
275 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING,
276 in_param1, in_param2, in_param3, in_param4, &out_param);
278 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
283 BT_EXPORT_API int bluetooth_set_custom_advertising(int handle, gboolean enable,
284 bluetooth_advertising_params_t *params)
287 gboolean use_reserved_slot = FALSE;
289 BT_CHECK_ENABLED_ANY(return);
291 #ifdef TIZEN_WEARABLE
292 use_reserved_slot = __bluetooth_is_privileged_process();
296 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
298 g_array_append_vals(in_param1, &handle, sizeof(int));
299 g_array_append_vals(in_param2, &enable, sizeof(gboolean));
300 g_array_append_vals(in_param3, params, sizeof(bluetooth_advertising_params_t));
301 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
303 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_CUSTOM_ADVERTISING,
304 in_param1, in_param2, in_param3, in_param4, &out_param);
306 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
311 BT_EXPORT_API int bluetooth_get_advertising_data(bluetooth_advertising_data_t *adv_data, int *length)
316 BT_CHECK_PARAMETER(adv_data, return);
317 BT_CHECK_PARAMETER(length, return);
318 BT_CHECK_ENABLED_ANY(return);
321 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
323 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_ADVERTISING_DATA,
324 in_param1, in_param2, in_param3, in_param4, &out_param);
326 if (result == BLUETOOTH_ERROR_NONE) {
327 data = &g_array_index(out_param, guint8, 0);
328 *length = out_param->len;
330 memset(adv_data, 0x00, sizeof(bluetooth_advertising_data_t));
331 memcpy(adv_data->data, data, *length);
334 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
339 BT_EXPORT_API int bluetooth_set_advertising_data(int handle, const bluetooth_advertising_data_t *value, int length)
342 gboolean use_reserved_slot = FALSE;
344 BT_CHECK_PARAMETER(value, return);
345 BT_CHECK_ENABLED_ANY(return);
347 if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 3)
348 return BLUETOOTH_ERROR_INVALID_PARAM;
350 #ifdef TIZEN_WEARABLE
351 use_reserved_slot = __bluetooth_is_privileged_process();
355 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
357 g_array_append_vals(in_param1, &handle, sizeof(int));
358 g_array_append_vals(in_param2, value, sizeof(bluetooth_advertising_data_t));
359 g_array_append_vals(in_param3, &length, sizeof(int));
360 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
362 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING_DATA,
363 in_param1, in_param2, in_param3, in_param4, &out_param);
365 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
370 BT_EXPORT_API int bluetooth_get_scan_response_data(bluetooth_scan_resp_data_t *value, int *length)
375 BT_CHECK_PARAMETER(value, return);
376 BT_CHECK_PARAMETER(length, return);
377 BT_CHECK_ENABLED_ANY(return);
380 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
382 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_SCAN_RESPONSE_DATA,
383 in_param1, in_param2, in_param3, in_param4, &out_param);
385 if (result == BLUETOOTH_ERROR_NONE) {
386 data = &g_array_index(out_param, guint8, 0);
387 *length = out_param->len;
389 memset(value, 0x00, sizeof(bluetooth_scan_resp_data_t));
390 memcpy(value->data, data, *length);
393 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
398 BT_EXPORT_API int bluetooth_set_scan_response_data(int handle,
399 const bluetooth_scan_resp_data_t *value, int length)
402 gboolean use_reserved_slot = FALSE;
404 BT_CHECK_PARAMETER(value, return);
405 BT_CHECK_ENABLED_ANY(return);
407 if (length > BLUETOOTH_SCAN_RESP_DATA_LENGTH_MAX)
408 return BLUETOOTH_ERROR_INVALID_PARAM;
410 #ifdef TIZEN_WEARABLE
411 use_reserved_slot = __bluetooth_is_privileged_process();
415 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
417 g_array_append_vals(in_param1, &handle, sizeof(int));
418 g_array_append_vals(in_param2, value, length);
419 g_array_append_vals(in_param3, &length, sizeof(int));
420 g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
422 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_RESPONSE_DATA,
423 in_param1, in_param2, in_param3, in_param4, &out_param);
425 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
430 BT_EXPORT_API int bluetooth_set_scan_parameters(bluetooth_le_scan_params_t *params)
434 BT_CHECK_ENABLED_ANY(return);
437 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
439 g_array_append_vals(in_param1, params, sizeof(bluetooth_le_scan_params_t));
441 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_PARAMETERS,
442 in_param1, in_param2, in_param3, in_param4, &out_param);
444 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
449 BT_EXPORT_API int bluetooth_is_advertising(gboolean *is_advertising)
453 BT_CHECK_ENABLED_ANY(return);
456 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
458 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_ADVERTISING,
459 in_param1, in_param2, in_param3, in_param4, &out_param);
461 if (result == BLUETOOTH_ERROR_NONE) {
462 *is_advertising = g_array_index(out_param, int, 0);
464 BT_ERR("Fail to send request");
467 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
472 BT_EXPORT_API int bluetooth_add_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
476 BT_CHECK_PARAMETER(address, return);
477 BT_CHECK_ENABLED_ANY(return);
480 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
482 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
483 g_array_append_vals(in_param2, &address_type, sizeof(int));
485 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ADD_WHITE_LIST,
486 in_param1, in_param2, in_param3, in_param4, &out_param);
488 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
493 BT_EXPORT_API int bluetooth_remove_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
497 BT_CHECK_PARAMETER(address, return);
498 BT_CHECK_ENABLED_ANY(return);
501 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
503 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
504 g_array_append_vals(in_param2, &address_type, sizeof(int));
506 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REMOVE_WHITE_LIST,
507 in_param1, in_param2, in_param3, in_param4, &out_param);
509 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
514 BT_EXPORT_API int bluetooth_clear_white_list(void)
518 BT_CHECK_ENABLED_ANY(return);
521 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
523 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CLEAR_WHITE_LIST,
524 in_param1, in_param2, in_param3, in_param4, &out_param);
526 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
531 BT_EXPORT_API int bluetooth_enable_le_privacy(gboolean enable_privacy)
535 BT_CHECK_ENABLED_ANY(return);
538 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
540 g_array_append_vals(in_param1, &enable_privacy, sizeof(gboolean));
542 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_LE_PRIVACY,
543 in_param1, in_param2, in_param3, in_param4, &out_param);
545 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
550 BT_EXPORT_API int bluetooth_check_privilege_advertising_parameter(void)
552 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SET_ADVERTISING_PARAMETERS)
553 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
554 BT_ERR("Don't have a privilege to use this API");
555 return BLUETOOTH_ERROR_PERMISSION_DEINED;
558 return BLUETOOTH_ERROR_NONE;
561 BT_EXPORT_API int bluetooth_le_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
565 ret = _bt_register_event(BT_LE_ADAPTER_EVENT, (void *)callback_ptr, user_data);
566 if (ret != BLUETOOTH_ERROR_NONE &&
567 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
568 BT_ERR("Fail to register BT_LE_ADAPTER_EVENT event : %d", ret);
572 return BLUETOOTH_ERROR_NONE;
575 BT_EXPORT_API int bluetooth_le_unregister_callback(void)
577 _bt_unregister_event(BT_LE_ADAPTER_EVENT);
579 return BLUETOOTH_ERROR_NONE;