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.
20 #include "bluetooth-api.h"
21 #include "bt-internal-types.h"
23 #include "bt-common.h"
24 #include "bt-request-sender.h"
25 #include "bt-event-handler.h"
27 static int __bt_fill_device_list(GArray *out_param2, GPtrArray **dev_list)
31 bluetooth_device_info_t info;
33 BT_CHECK_PARAMETER(out_param2, return);
34 BT_CHECK_PARAMETER(dev_list, return);
36 size = out_param2->len;
39 BT_ERR("No bonded device");
40 return BLUETOOTH_ERROR_NONE;
43 size = (out_param2->len) / sizeof(bluetooth_device_info_t);
45 for (i = 0; i < size; i++) {
46 bluetooth_device_info_t *dev_info = NULL;
48 info = g_array_index(out_param2,
49 bluetooth_device_info_t, i);
51 dev_info = g_memdup(&info, sizeof(bluetooth_device_info_t));
54 g_ptr_array_add(*dev_list, (gpointer)dev_info);
57 return BLUETOOTH_ERROR_NONE;
60 BT_EXPORT_API int bluetooth_check_adapter(void)
62 int value = VCONFKEY_BT_STATUS_OFF;
64 #ifndef TIZEN_TEST_EMUL
67 ret = _bt_get_adapter_path(_bt_gdbus_get_system_gconn(), NULL);
69 if (ret != BLUETOOTH_ERROR_NONE)
70 return BLUETOOTH_ADAPTER_DISABLED;
73 /* check VCONFKEY_BT_STATUS */
74 if (vconf_get_int(VCONFKEY_BT_STATUS, &value) != 0) {
75 BT_ERR("fail to get vconf key!");
76 return BLUETOOTH_ADAPTER_DISABLED;
79 return value == VCONFKEY_BT_STATUS_OFF ? BLUETOOTH_ADAPTER_DISABLED :
80 BLUETOOTH_ADAPTER_ENABLED;
83 BT_EXPORT_API int bluetooth_enable_adapter(void)
87 BT_INFO_C("### Enable adapter");
88 retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED,
89 BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED);
91 #ifdef TIZEN_FEATURE_BT_DPM
92 retv_if(bluetooth_dpm_is_mode_allowed() == BLUETOOTH_DPM_RESULT_ACCESS_DENIED,
93 BLUETOOTH_ERROR_PERMISSION_DEINED);
97 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
99 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_ADAPTER,
100 in_param1, in_param2, in_param3, in_param4, &out_param);
102 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
107 BT_EXPORT_API int bluetooth_disable_adapter(void)
111 BT_INFO_C("### Disable adapter");
112 BT_CHECK_ENABLED(return);
115 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
117 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_DISABLE_ADAPTER,
118 in_param1, in_param2, in_param3, in_param4, &out_param);
120 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
125 BT_EXPORT_API int bluetooth_recover_adapter(void)
129 BT_INFO("Recover adapter");
130 BT_CHECK_ENABLED(return);
133 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
135 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RECOVER_ADAPTER,
136 in_param1, in_param2, in_param3, in_param4, &out_param);
138 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
143 BT_EXPORT_API int bluetooth_reset_adapter(void)
149 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
151 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RESET_ADAPTER,
152 in_param1, in_param2, in_param3, in_param4, &out_param);
154 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
159 BT_EXPORT_API int bluetooth_get_local_address(bluetooth_device_address_t *local_address)
163 BT_CHECK_PARAMETER(local_address, return);
164 BT_CHECK_ENABLED_ANY(return);
167 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
169 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_LOCAL_ADDRESS,
170 in_param1, in_param2, in_param3, in_param4, &out_param);
172 if (result == BLUETOOTH_ERROR_NONE) {
173 *local_address = g_array_index(out_param,
174 bluetooth_device_address_t, 0);
177 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
182 BT_EXPORT_API int bluetooth_get_local_version(bluetooth_version_t *local_version)
186 BT_CHECK_PARAMETER(local_version, return);
187 BT_CHECK_ENABLED_ANY(return);
190 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
192 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_LOCAL_VERSION,
193 in_param1, in_param2, in_param3, in_param4, &out_param);
195 if (result == BLUETOOTH_ERROR_NONE)
196 *local_version = g_array_index(out_param, bluetooth_version_t, 0);
198 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
203 BT_EXPORT_API int bluetooth_get_local_name(bluetooth_device_name_t *local_name)
207 BT_CHECK_PARAMETER(local_name, return);
208 BT_CHECK_ENABLED_ANY(return);
211 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
213 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_LOCAL_NAME,
214 in_param1, in_param2, in_param3, in_param4, &out_param);
216 if (result == BLUETOOTH_ERROR_NONE) {
217 *local_name = g_array_index(out_param,
218 bluetooth_device_name_t, 0);
221 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
226 BT_EXPORT_API int bluetooth_set_local_name(const bluetooth_device_name_t *local_name)
230 BT_CHECK_PARAMETER(local_name, return);
231 BT_CHECK_ENABLED_ANY(return);
234 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
236 g_array_append_vals(in_param1, local_name, sizeof(bluetooth_device_name_t));
238 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_LOCAL_NAME,
239 in_param1, in_param2, in_param3, in_param4, &out_param);
241 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
246 BT_EXPORT_API int bluetooth_is_service_used(const char *service_uuid,
250 char uuid[BLUETOOTH_UUID_STRING_MAX];
252 BT_CHECK_PARAMETER(service_uuid, return);
253 BT_CHECK_PARAMETER(used, return);
254 BT_CHECK_ENABLED(return);
257 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
259 g_strlcpy(uuid, service_uuid, sizeof(uuid));
260 g_array_append_vals(in_param1, uuid, BLUETOOTH_UUID_STRING_MAX);
262 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_SERVICE_USED,
263 in_param1, in_param2, in_param3, in_param4, &out_param);
265 if (result == BLUETOOTH_ERROR_NONE)
266 *used = g_array_index(out_param, gboolean, 0);
268 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
273 BT_EXPORT_API int bluetooth_get_discoverable_mode(bluetooth_discoverable_mode_t *
274 discoverable_mode_ptr)
278 BT_CHECK_PARAMETER(discoverable_mode_ptr, return);
280 #ifndef TIZEN_PROFILE_WEARABLE
282 /* Requirement in OSP */
283 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) {
284 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0) {
285 BT_ERR("Fail to get the timeout value");
286 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
290 *discoverable_mode_ptr = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
292 *discoverable_mode_ptr = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
294 return BLUETOOTH_ERROR_NONE;
297 BT_CHECK_ENABLED(return);
301 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
303 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_DISCOVERABLE_MODE,
304 in_param1, in_param2, in_param3, in_param4, &out_param);
306 if (result == BLUETOOTH_ERROR_NONE)
307 *discoverable_mode_ptr = g_array_index(out_param, int, 0);
309 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
314 BT_EXPORT_API int bluetooth_set_discoverable_mode(bluetooth_discoverable_mode_t discoverable_mode,
319 BT_CHECK_ENABLED(return);
322 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
324 g_array_append_vals(in_param1, &discoverable_mode, sizeof(int));
325 g_array_append_vals(in_param2, &timeout, sizeof(int));
327 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_DISCOVERABLE_MODE,
328 in_param1, in_param2, in_param3, in_param4, &out_param);
330 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
335 BT_EXPORT_API int bluetooth_get_timeout_value(int *timeout)
339 BT_CHECK_PARAMETER(timeout, return);
340 BT_CHECK_ENABLED(return);
343 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
345 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_DISCOVERABLE_TIME,
346 in_param1, in_param2, in_param3, in_param4, &out_param);
348 if (result == BLUETOOTH_ERROR_NONE)
349 *timeout = g_array_index(out_param, int, 0);
351 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
356 BT_EXPORT_API int bluetooth_start_discovery(unsigned short max_response,
357 unsigned short discovery_duration,
358 unsigned int classOfDeviceMask)
362 BT_CHECK_ENABLED(return);
365 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
367 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_DISCOVERY,
368 in_param1, in_param2, in_param3, in_param4, &out_param);
370 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
375 BT_EXPORT_API int bluetooth_start_custom_discovery(bt_discovery_role_type_t role,
376 unsigned short max_response,
377 unsigned short discovery_duration,
378 unsigned int classOfDeviceMask)
382 if (role == DISCOVERY_ROLE_LE)
383 BT_CHECK_ENABLED_LE(return);
385 BT_CHECK_ENABLED(return);
388 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
390 g_array_append_vals(in_param1, &role, sizeof(bt_discovery_role_type_t));
391 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_CUSTOM_DISCOVERY,
392 in_param1, in_param2, in_param3, in_param4, &out_param);
394 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
399 BT_EXPORT_API int bluetooth_cancel_discovery(void)
403 BT_CHECK_ENABLED(return);
406 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
408 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_DISCOVERY,
409 in_param1, in_param2, in_param3, in_param4, &out_param);
412 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
417 BT_EXPORT_API int bluetooth_is_discovering(void)
420 int is_discovering = FALSE;
422 BT_CHECK_ENABLED(return);
425 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
427 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_DISCOVERYING,
428 in_param1, in_param2, in_param3, in_param4, &out_param);
430 if (result == BLUETOOTH_ERROR_NONE) {
431 is_discovering = g_array_index(out_param,
434 BT_ERR("Fail to send request");
435 is_discovering = result;
438 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
440 return is_discovering;
443 BT_EXPORT_API int bluetooth_is_connectable(gboolean *is_connectable)
447 BT_CHECK_ENABLED_ANY(return);
450 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
452 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_CONNECTABLE,
453 in_param1, in_param2, in_param3, in_param4, &out_param);
455 if (result == BLUETOOTH_ERROR_NONE)
456 *is_connectable = g_array_index(out_param, int, 0);
458 BT_ERR("Fail to send request");
460 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
465 BT_EXPORT_API int bluetooth_set_connectable(gboolean is_connectable)
469 BT_CHECK_ENABLED_ANY(return);
472 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
474 g_array_append_vals(in_param1, &is_connectable, sizeof(gboolean));
476 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_CONNECTABLE,
477 in_param1, in_param2, in_param3, in_param4, &out_param);
479 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
484 BT_EXPORT_API int bluetooth_get_bonded_device_list(GPtrArray **dev_list)
488 BT_CHECK_PARAMETER(dev_list, return);
489 BT_CHECK_ENABLED(return);
492 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
494 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_BONDED_DEVICES,
495 in_param1, in_param2, in_param3, in_param4, &out_param);
497 if (result == BLUETOOTH_ERROR_NONE)
498 result = __bt_fill_device_list(out_param, dev_list);
500 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
505 BT_EXPORT_API int bluetooth_get_profile_connected_device_list(
506 const char *profile_uuid, GPtrArray **addr_list)
508 char uuid[BLUETOOTH_UUID_STRING_MAX];
513 BT_CHECK_PARAMETER(profile_uuid, return);
514 BT_CHECK_PARAMETER(addr_list, return);
515 BT_CHECK_ENABLED(return);
518 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
520 g_strlcpy(uuid, profile_uuid, sizeof(uuid));
521 g_array_append_vals(in_param1, uuid, BLUETOOTH_UUID_STRING_MAX);
523 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_PROFILE_CONNECTED_DEVICES,
524 in_param1, in_param2, in_param3, in_param4, &out_param);
526 if (result == BLUETOOTH_ERROR_NONE) {
527 if (out_param == NULL) {
528 BT_ERR("out_param is NULL");
529 result = BLUETOOTH_ERROR_INTERNAL;
531 size = (out_param->len) / sizeof(bluetooth_device_address_t);
534 BT_ERR("No connected device");
535 result = BLUETOOTH_ERROR_NOT_CONNECTED;
538 for (i = 0; i < size; i++) {
539 bluetooth_device_address_t *addr = NULL;
540 bluetooth_device_address_t *ptr = NULL;
542 ptr = &g_array_index(out_param,
543 bluetooth_device_address_t, i);
545 addr = g_memdup(ptr, sizeof(bluetooth_device_address_t));
548 g_ptr_array_add(*addr_list, (gpointer)addr);
553 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
558 BT_EXPORT_API int bluetooth_set_manufacturer_data(const bluetooth_manufacturer_data_t *value)
562 BT_CHECK_PARAMETER(value, return);
563 BT_CHECK_ENABLED_ANY(return);
565 if (value->data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX)
566 return BLUETOOTH_ERROR_INVALID_PARAM;
569 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
571 g_array_append_vals(in_param1, value, sizeof(bluetooth_manufacturer_data_t));
573 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_MANUFACTURER_DATA,
574 in_param1, in_param2, in_param3, in_param4, &out_param);
576 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
581 BT_EXPORT_API int bluetooth_set_passkey_notification(gboolean enable)
585 BT_CHECK_ENABLED_ANY(return);
588 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
590 g_array_append_vals(in_param1, &enable, sizeof(gboolean));
592 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_PASSKEY_NOTIFICATION,
593 in_param1, in_param2, in_param3, in_param4, &out_param);
595 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);