4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
22 #include <syspopup_caller.h>
25 #include "bluetooth-api.h"
26 #include "bt-internal-types.h"
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
32 static int __bt_fill_device_list(GArray *out_param2, GPtrArray **dev_list)
36 bluetooth_device_info_t info;
38 BT_CHECK_PARAMETER(out_param2, return);
39 BT_CHECK_PARAMETER(dev_list, return);
41 size = out_param2->len;
42 retv_if(size == 0, BLUETOOTH_ERROR_NONE);
44 size = (out_param2->len) / sizeof(bluetooth_device_info_t);
46 for (i = 0; i < size; i++) {
47 bluetooth_device_info_t *dev_info = NULL;
49 info = g_array_index(out_param2,
50 bluetooth_device_info_t, i);
52 dev_info = g_memdup(&info, sizeof(bluetooth_device_info_t));
55 g_ptr_array_add(*dev_list, (gpointer)dev_info);
59 return BLUETOOTH_ERROR_NONE;
62 BT_EXPORT_API int bluetooth_check_adapter(void)
65 bluetooth_adapter_state_t state;
68 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
70 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CHECK_ADAPTER,
71 in_param1, in_param2, in_param3, in_param4, &out_param);
73 if (result == BLUETOOTH_ERROR_NONE) {
74 state = g_array_index(out_param, bluetooth_adapter_state_t, 0);
77 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
79 return state ? BLUETOOTH_ADAPTER_ENABLED : BLUETOOTH_ADAPTER_DISABLED;
82 BT_EXPORT_API int bluetooth_enable_adapter(void)
87 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
89 retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED,
90 BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED);
92 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_ADAPTER,
93 in_param1, in_param2, in_param3, in_param4, &out_param);
95 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
100 BT_EXPORT_API int bluetooth_disable_adapter(void)
104 BT_CHECK_ENABLED(return);
107 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
109 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_DISABLE_ADAPTER,
110 in_param1, in_param2, in_param3, in_param4, &out_param);
112 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
117 BT_EXPORT_API int bluetooth_reset_adapter(void)
122 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
124 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RESET_ADAPTER,
125 in_param1, in_param2, in_param3, in_param4, &out_param);
127 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
132 BT_EXPORT_API int bluetooth_get_local_address(bluetooth_device_address_t *local_address)
136 BT_CHECK_PARAMETER(local_address, return);
139 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_LOCAL_ADDRESS,
142 in_param1, in_param2, in_param3, in_param4, &out_param);
144 if (result == BLUETOOTH_ERROR_NONE) {
145 *local_address = g_array_index(out_param,
146 bluetooth_device_address_t, 0);
149 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
154 BT_EXPORT_API int bluetooth_get_local_name(bluetooth_device_name_t *local_name)
158 BT_CHECK_PARAMETER(local_name, return);
161 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
163 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_LOCAL_NAME,
164 in_param1, in_param2, in_param3, in_param4, &out_param);
166 if (result == BLUETOOTH_ERROR_NONE) {
167 *local_name = g_array_index(out_param,
168 bluetooth_device_name_t, 0);
171 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
176 BT_EXPORT_API int bluetooth_set_local_name(const bluetooth_device_name_t *local_name)
180 BT_CHECK_PARAMETER(local_name, return);
181 BT_CHECK_ENABLED(return);
184 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
186 g_array_append_vals(in_param1, local_name, sizeof(bluetooth_device_name_t));
188 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_LOCAL_NAME,
189 in_param1, in_param2, in_param3, in_param4, &out_param);
191 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
196 BT_EXPORT_API int bluetooth_is_service_used(const char *service_uuid,
200 char uuid[BLUETOOTH_UUID_STRING_MAX];
202 BT_CHECK_PARAMETER(service_uuid, return);
203 BT_CHECK_PARAMETER(used, return);
204 BT_CHECK_ENABLED(return);
207 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
209 g_strlcpy(uuid, service_uuid, sizeof(uuid));
210 g_array_append_vals(in_param1, uuid, BLUETOOTH_UUID_STRING_MAX);
212 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_SERVICE_USED,
213 in_param1, in_param2, in_param3, in_param4, &out_param);
215 if (result == BLUETOOTH_ERROR_NONE) {
216 *used = g_array_index(out_param, gboolean, 0);
219 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
224 BT_EXPORT_API int bluetooth_get_discoverable_mode(bluetooth_discoverable_mode_t *
225 discoverable_mode_ptr)
230 BT_CHECK_PARAMETER(discoverable_mode_ptr, return);
233 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
235 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_DISCOVERABLE_MODE,
236 in_param1, in_param2, in_param3, in_param4, &out_param);
238 if (result == BLUETOOTH_ERROR_NONE) {
239 *discoverable_mode_ptr = g_array_index(out_param,
243 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
248 BT_EXPORT_API int bluetooth_set_discoverable_mode(bluetooth_discoverable_mode_t discoverable_mode,
253 BT_CHECK_ENABLED(return);
256 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
258 g_array_append_vals(in_param1, &discoverable_mode, sizeof(int));
259 g_array_append_vals(in_param2, &timeout, sizeof(int));
261 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_DISCOVERABLE_MODE,
262 in_param1, in_param2, in_param3, in_param4, &out_param);
264 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
269 BT_EXPORT_API int bluetooth_get_timeout_value(int *timeout)
273 BT_CHECK_PARAMETER(timeout, return);
274 BT_CHECK_ENABLED(return);
277 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
279 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_DISCOVERABLE_TIME,
280 in_param1, in_param2, in_param3, in_param4, &out_param);
282 if (result == BLUETOOTH_ERROR_NONE) {
283 *timeout = g_array_index(out_param, int, 0);
286 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
291 BT_EXPORT_API int bluetooth_start_discovery(unsigned short max_response,
292 unsigned short discovery_duration,
293 unsigned int classOfDeviceMask)
297 BT_CHECK_ENABLED(return);
300 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
302 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_DISCOVERY,
303 in_param1, in_param2, in_param3, in_param4, &out_param);
305 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
310 BT_EXPORT_API int bluetooth_cancel_discovery(void)
314 BT_CHECK_ENABLED(return);
317 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
319 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_DISCOVERY,
320 in_param1, in_param2, in_param3, in_param4, &out_param);
323 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
328 BT_EXPORT_API int bluetooth_is_discovering(void)
331 int is_discovering = FALSE;
333 BT_CHECK_ENABLED(return);
336 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
338 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_DISCOVERYING,
339 in_param1, in_param2, in_param3, in_param4, &out_param);
341 if (result == BLUETOOTH_ERROR_NONE) {
342 is_discovering = g_array_index(out_param,
345 BT_ERR("Fail to send request");
348 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
350 return is_discovering;
353 BT_EXPORT_API int bluetooth_get_bonded_device_list(GPtrArray **dev_list)
357 BT_CHECK_PARAMETER(dev_list, return);
358 BT_CHECK_ENABLED(return);
361 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
363 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_BONDED_DEVICES,
364 in_param1, in_param2, in_param3, in_param4, &out_param);
366 if (result == BLUETOOTH_ERROR_NONE) {
367 result = __bt_fill_device_list(out_param, dev_list);
370 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);