Fix the coding style errors (bt-service)
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <glib.h>
19 #include <gio/gio.h>
20 #include <dlog.h>
21 #include <string.h>
22 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
23 #include <syspopup_caller.h>
24 #endif
25 #include <bundle_internal.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-service-common.h"
31 #include "bt-service-event.h"
32 #include "bt-service-device.h"
33 #include "bt-service-adapter-le.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
40
41 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
42 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
43 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
44
45 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
46 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
47 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
49 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
50 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
51 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
52 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
53
54 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT    20000   /* msec */
55
56 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL  30      /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL  50      /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0       /* event */
59
60 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL       7.5     /* msec */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL       10      /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY      0       /* event */
63
64 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL         80      /* msec */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL         100     /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY        2       /* event */
67
68 typedef struct {
69         int req_id;
70         int result;
71         char *addr;
72         gboolean is_autopair;
73         GDBusProxy *device_proxy;
74         GDBusProxy *adapter_proxy;
75         void *agent;
76         unsigned short conn_type;
77 } bt_funcion_data_t;
78
79 typedef struct {
80         char *address;
81         char *pin_code;
82 } bt_pin_code_info_t;
83
84 gboolean is_device_creating;
85 bt_funcion_data_t *bonding_info;
86 bt_funcion_data_t *searching_info;
87 bt_funcion_data_t *le_connection_info;
88
89 static GSList *pin_info_list = NULL;
90
91 /* This HID Mouse does not support pairing precedure. need to skip it. */
92 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
93
94 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
95                                         gpointer user_data);
96
97 static int __bt_retry_bond(void);
98
99
100 /*static void __bt_decline_pair_request()
101 {
102         GVariant *out_param1;
103         GVariant *out_param2;
104         request_info_t *req_info;
105         bluetooth_device_info_t dev_info;
106         bt_remote_dev_info_t *remote_dev_info;
107         GVariant *uuid_list, *manufacture_data;
108         GVariant *param;
109
110         BT_DBG("+");
111         if (bonding_info) {
112                 req_info = _bt_get_request_info(bonding_info->req_id);
113                 if (req_info == NULL) {
114                         BT_ERR("req_info == NULL");
115                         goto done;
116                 }
117                 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
118         } else {
119                 BT_DBG("bonding_info is NULL");
120                 BT_DBG("-");
121                 return;
122         }
123
124         uuid_list =  g_variant_new_from_data((const GVariantType *)"as",
125                                                 remote_dev_info->uuids, remote_dev_info->uuid_count,
126                                                 TRUE, NULL, NULL);
127
128         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
129                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
130                                                 TRUE, NULL, NULL);
131
132         param = g_variant_new("isunsbuba{s}na{y})",
133                         bonding_info->result,
134                         bonding_info->addr,
135                         remote_dev_info->class,
136                         remote_dev_info->rssi,
137                         remote_dev_info->name,
138                         remote_dev_info->paired,
139                         remote_dev_info->connected,
140                         remote_dev_info->trust,
141                         uuid_list,
142                         remote_dev_info->manufacturer_data_len,
143                         manufacture_data);
144
145
146         //Send the event to application
147         if (remote_dev_info != NULL) {
148                 _bt_send_event(BT_ADAPTER_EVENT,
149                         BLUETOOTH_EVENT_BONDING_FINISHED,
150                         param);
151
152                 _bt_free_device_info(remote_dev_info);
153         }
154
155         if (req_info->context == NULL)
156                 goto done;
157
158         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
159         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
160                                         bonding_info->addr);
161
162         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
163                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
164
165         out_param2 = g_variant_new_from_data((const GVariantType *)"i",
166                 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
167
168
169         g_dbus_method_invocation_return_value(req_info->context,
170                         g_variant_new("@ayi", out_param1, out_param2));
171
172
173         _bt_delete_request_list(req_info->req_id);
174 done:
175
176         g_free(bonding_info->addr);
177         g_free(bonding_info);
178         bonding_info = NULL;
179
180         BT_DBG("-");
181 } */
182
183 #ifdef TIZEN_WEARABLE
184 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
185 {
186         int ret;
187         bundle *b;
188         retv_if(user_data == NULL, FALSE);
189
190         b = (bundle *)user_data;
191
192 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
193         ret = syspopup_launch("bt-syspopup", b);
194 #endif
195         if (ret < 0) {
196                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
197         } else {
198                 BT_DBG("Hurray!!! Finally Popup launched");
199                 bundle_free(b);
200         }
201         return (ret < 0) ? TRUE : FALSE;
202 }
203
204 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
205 {
206         BT_DBG("+");
207         int ret = 0;
208         bundle *b = NULL;
209         GDBusConnection *conn;
210
211         conn = _bt_get_system_gconn();
212         if (conn == NULL)
213                 return FALSE;
214
215         b = bundle_create();
216         if (b == NULL)
217                 return FALSE;
218
219         bundle_add(b, "event-type", "unable-to-pairing");
220
221         if (result == BLUETOOTH_ERROR_TIMEOUT)
222                 bundle_add(b, "error", "timeout");
223         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
224                 bundle_add(b, "error", "authfailed");
225         else
226                 bundle_add(b, "error", "error");
227
228 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
229         ret = syspopup_launch("bt-syspopup", b);
230 #endif
231         if (0 > ret) {
232                 BT_ERR("Popup launch failed...retry %d \n", ret);
233                 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
234                                 b);
235         } else {
236                 bundle_free(b);
237         }
238
239         BT_DBG("-");
240         return TRUE;
241 }
242 #endif
243
244 gboolean _bt_is_device_creating(void)
245 {
246         return is_device_creating;
247 }
248
249 gboolean _bt_is_bonding_device_address(const char *address)
250 {
251         if (bonding_info == NULL || bonding_info->addr == NULL)
252                 return FALSE;
253
254         if (g_strcmp0(bonding_info->addr, address) == 0) {
255                 BT_DBG("[%s]  is bonding device", address);
256                 return TRUE;
257         }
258
259         BT_DBG("[%s]  is NOT bonding device", address);
260         return FALSE;
261 }
262
263 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
264 {
265         ret_if(bonding_info == NULL);
266         bonding_info->is_autopair = is_autopair;
267 }
268
269 void _bt_device_path_to_address(const char *device_path,
270                                         char *device_address)
271 {
272         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
273         char *dev_addr;
274         char *pos;
275
276         ret_if(device_path == NULL);
277         ret_if(device_address == NULL);
278
279         dev_addr = strstr(device_path, "dev_");
280         ret_if(dev_addr == NULL);
281
282         dev_addr += 4;
283         g_strlcpy(address, dev_addr, sizeof(address));
284
285         while ((pos = strchr(address, '_')) != NULL) {
286                 *pos = ':';
287         }
288
289         g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
290 }
291
292 void __bt_cancel_search_service_done(void)
293 {
294         int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
295         request_info_t *req_info;
296         bluetooth_device_info_t dev_info;
297         GVariant *out_param1;
298
299         ret_if(searching_info == NULL);
300
301         req_info = _bt_get_request_info(searching_info->req_id);
302         if (req_info == NULL) {
303                 BT_ERR("req_info == NULL");
304                 goto done;
305         }
306
307         if (req_info->context == NULL)
308                 goto done;
309
310         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
311         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
312                                         searching_info->addr);
313
314         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
315                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
316
317
318         g_dbus_method_invocation_return_value(req_info->context,
319                         g_variant_new("(iv)", result, out_param1));
320
321         _bt_delete_request_list(req_info->req_id);
322
323 done:
324
325         g_free(searching_info->addr);
326         g_free(searching_info);
327         searching_info = NULL;
328 }
329
330 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
331 {
332         ret_if(value == NULL);
333         ret_if(info == NULL);
334
335         info->uuid_count = g_variant_get_size(value);
336         info->uuids = g_variant_dup_strv(value, &info->uuid_count);
337 }
338
339 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
340 {
341         bt_remote_dev_info_t *dev_info;
342         char *object_path = NULL;
343         GDBusProxy *adapter_proxy;
344         GDBusProxy *device_proxy;
345         GVariant *value;
346         GVariant *tmp_value;
347         gchar *name;
348         gchar *manufacturer_data = NULL;
349         GDBusConnection *conn;
350         GError *error = NULL;
351         GVariant *result = NULL;
352
353         retv_if(address == NULL, NULL);
354
355         adapter_proxy = _bt_get_adapter_proxy();
356         retv_if(adapter_proxy == NULL, NULL);
357
358
359         object_path = _bt_get_device_object_path(address);
360
361         retv_if(object_path == NULL, NULL);
362
363         conn = _bt_get_system_gconn();
364         if (conn == NULL) {
365                 BT_ERR("conn == NULL");
366                 g_free(object_path);
367                 return NULL;
368         }
369
370         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
371                                                                 NULL, BT_BLUEZ_NAME,
372                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
373
374         g_free(object_path);
375         retv_if(device_proxy == NULL, NULL);
376
377         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
378                                 g_variant_new("(s)", BT_DEVICE_INTERFACE),
379                                 G_DBUS_CALL_FLAGS_NONE,
380                                 -1,
381                                 NULL,
382                                 &error);
383
384         g_object_unref(device_proxy);
385
386         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
387
388         if (result != NULL) {
389                 g_variant_get(result , "(@a{sv})", &value);
390                 g_variant_unref(result);
391
392                 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
393
394                 g_variant_get(tmp_value, "s", &name);
395                 g_variant_unref(tmp_value);
396                 if (name != NULL)
397                         DBG_SECURE("Alias Name [%s]", name);
398                 else {
399                         tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
400                         g_variant_get(tmp_value, "s", &name);
401                         g_variant_unref(tmp_value);
402                 }
403
404                 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
405                 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
406                 g_variant_unref(tmp_value);
407
408                 tmp_value = g_variant_lookup_value(value, "Connected",  G_VARIANT_TYPE_BOOLEAN);
409                 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
410                 g_variant_unref(tmp_value);
411
412                 tmp_value = g_variant_lookup_value(value, "Trusted",  G_VARIANT_TYPE_BOOLEAN);
413                 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
414                 g_variant_unref(tmp_value);
415
416                 tmp_value = g_variant_lookup_value(value, "Paired",  G_VARIANT_TYPE_BOOLEAN);
417                 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
418                 g_variant_unref(tmp_value);
419
420                 BT_DBG("Paired %d", dev_info->paired);
421
422                 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
423                 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
424                 g_variant_unref(tmp_value);
425
426                 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
427                 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
428                 g_variant_unref(tmp_value);
429
430                 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
431                 __bt_get_uuids(tmp_value, dev_info);
432                 g_variant_unref(tmp_value);
433
434                 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
435                 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
436                 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
437                         BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
438                         dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
439                 }
440                 g_variant_unref(tmp_value);
441
442                 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
443                 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
444                 if (manufacturer_data) {
445                         if (dev_info->manufacturer_data_len > 0) {
446                                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
447                                 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
448                         }
449                 }
450                 g_variant_unref(tmp_value);
451
452                 dev_info->address = g_strdup(address);
453                 dev_info->name = g_strdup(name);
454                 g_free(name);
455                 g_variant_unref(value);
456         } else {
457                 BT_ERR("result  is NULL\n");
458                 g_free(dev_info);
459                 dev_info = NULL;
460         }
461
462         return dev_info;
463 }
464
465 char *_bt_get_bonded_device_name(char *address)
466 {
467         bluetooth_device_address_t device_address = { {0} };
468         bluetooth_device_info_t dev_info;
469
470         retv_if(address == NULL, strdup(""));
471
472         _bt_convert_addr_string_to_type(device_address.addr, address);
473
474         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
475
476         _bt_get_bonded_device_info(&device_address, &dev_info);
477
478         return g_strdup(dev_info.device_name.name);
479 }
480
481 static gboolean __ignore_auto_pairing_request(const char *address)
482 {
483         gchar *buffer;
484         char **lines;
485         int i;
486         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
487         char *temp_buffer;
488         FILE *fp;
489         long size;
490         size_t result;
491
492         BT_DBG("+\n");
493
494         if (address == NULL)
495                 return FALSE;
496
497         /* Get the LAP(Lower Address part) */
498         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
499              a "," */
500         snprintf(lap_address, sizeof(lap_address), ",%s", address);
501
502         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
503
504         if (fp == NULL) {
505                 BT_ERR("fopen failed \n");
506                 return FALSE;
507         }
508
509         fseek(fp, 0, SEEK_END);
510         size = ftell(fp);
511         rewind(fp);
512
513         if (size < 0) {
514                 BT_ERR("Get file size failed \n");
515                 fclose(fp);
516                 return FALSE;
517         }
518
519         buffer = g_malloc0(sizeof(char) * size);
520         /* Fix : NULL_RETURNS */
521         if (buffer == NULL) {
522                 BT_ERR("Memory allocation error\n");
523                 fclose(fp);
524                 return FALSE;
525         }
526         result = fread((char *)buffer, 1, size, fp);
527         fclose(fp);
528         if (result != size) {
529                 BT_ERR("Read Error\n");
530                 g_free(buffer);
531                 return FALSE;
532         }
533
534         BT_DBG("Buffer = %s\n", buffer);
535
536         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
537         g_free(buffer);
538
539         if (lines == NULL)
540                 return FALSE;
541
542         /* Write the data and insert new device data */
543         for (i = 0; lines[i] != NULL; i++) {
544                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
545                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
546                         g_free(lines[i]);
547                         lines[i] = temp_buffer;
548                 }
549         }
550         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
551         g_strfreev(lines);
552         /* Fix : NULL_RETURNS */
553         retv_if(buffer == NULL, FALSE);
554
555         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
556
557         if (fp == NULL) {
558                 BT_ERR("fopen failed \n");
559                 g_free(buffer);
560                 return FALSE;
561         }
562
563         BT_DBG("Buffer = %s\n", buffer);
564         fwrite(buffer, 1, strlen(buffer), fp);
565         fclose(fp);
566
567         g_free(buffer);
568
569         BT_DBG("-\n");
570
571         return FALSE;
572 }
573
574 static int __bt_retry_bond(void)
575 {
576         BT_CHECK_PARAMETER(bonding_info, return);
577         BT_CHECK_PARAMETER(bonding_info->addr, return);
578
579         g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
580                                 g_variant_new("(y)", bonding_info->conn_type),
581                                 G_DBUS_CALL_FLAGS_NONE,
582                                 BT_MAX_DBUS_TIMEOUT,
583                                 NULL,
584                                 (GAsyncReadyCallback)__bt_bond_device_cb,
585                                 NULL);
586
587         return BLUETOOTH_ERROR_NONE;
588 }
589
590
591 static int __bt_remove_and_bond(char *device_path)
592 {
593         GDBusProxy *adapter_proxy;
594         GError *err = NULL;
595
596         BT_CHECK_PARAMETER(bonding_info, return);
597         BT_CHECK_PARAMETER(bonding_info->addr, return);
598
599         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
600
601         adapter_proxy = _bt_get_adapter_proxy();
602         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
603
604         g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
605                         g_variant_new("(o)", device_path),
606                         G_DBUS_CALL_FLAGS_NONE,
607                         -1,
608                         NULL,
609                         &err);
610
611         if (err != NULL) {
612                 BT_ERR("UnpairDevice Fail: %s", err->message);
613                 g_error_free(err);
614                 return BLUETOOTH_ERROR_INTERNAL;
615         }
616
617         return __bt_retry_bond();
618 }
619
620 static int __bt_cancel_and_bond(void)
621 {
622         int ret = BLUETOOTH_ERROR_NONE;
623
624         ret = _bt_agent_reply_cancellation();
625         if (ret != BLUETOOTH_ERROR_NONE) {
626                 BT_ERR("Fail to call reply cancellation");
627                 return ret;
628         }
629
630         return __bt_retry_bond();
631 }
632
633
634 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
635                                         gpointer user_data)
636 {
637         int result = BLUETOOTH_ERROR_NONE;
638         GError *err = NULL;
639         GVariant *out_param1;
640         request_info_t *req_info;
641         bluetooth_device_info_t dev_info;
642         bt_remote_dev_info_t *remote_dev_info;
643         GVariant *manufacture_data;
644         GVariant *param;
645         const char *device_path;
646
647 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
648         /* Terminate ALL system popup */
649         syspopup_destroy_all();
650 #endif
651
652          g_dbus_proxy_call_finish(proxy, res, &err);
653
654         is_device_creating = FALSE;
655
656         if (bonding_info == NULL) {
657                 /* Send reply */
658                 BT_ERR("bonding_info == NULL");
659                 if (err)
660                         g_error_free(err);
661                 return;
662         }
663
664         req_info = _bt_get_request_info(bonding_info->req_id);
665         if (req_info == NULL) {
666                 BT_ERR("req_info == NULL");
667                 goto done;
668         }
669
670         device_path = g_dbus_proxy_get_object_path(proxy);
671
672         BT_DBG("device path: %s", device_path);
673
674         if (err != NULL) {
675                 g_dbus_error_strip_remote_error(err);
676                 BT_ERR("Error occured in CreateBonding [%s]", err->message);
677
678                 if (g_strrstr(err->message, "Already Exists")) {
679                         BT_INFO("Existing Bond, remove and retry");
680                         ret_if(__bt_remove_and_bond((char *)device_path) == BLUETOOTH_ERROR_NONE);
681
682                         BT_INFO("Fail to retry bonding");
683
684                         result = BLUETOOTH_ERROR_PARING_FAILED;
685                 } else if (_bt_agent_is_canceled() ||
686                         g_strrstr(err->message, "Authentication Canceled")) {
687                         BT_INFO("Cancelled by USER");
688                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
689                 } else if (g_strrstr(err->message, "Authentication Rejected")) {
690                         BT_INFO("REJECTED");
691                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
692                 } else if (g_strrstr(err->message, "In Progress")) {
693                         BT_INFO("Bond in progress, cancel and retry");
694                         ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
695
696                         result = BLUETOOTH_ERROR_PARING_FAILED;
697                 } else if (g_strrstr(err->message, "Authentication Failed")) {
698                         BT_INFO("Authentication Failed");
699                         if (bonding_info->is_autopair == TRUE) {
700                                 _bt_set_autopair_status_in_bonding_info(FALSE);
701                                 __ignore_auto_pairing_request(bonding_info->addr);
702                         }
703                         result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
704                 } else if (g_strrstr(err->message, "Page Timeout")) {
705                         BT_INFO("Page Timeout");
706                         /* This is the special case
707                              As soon as call bluetooth_bond_device, try to cancel bonding.
708                              In this case, before completing to call 'CreatePairedDevice' method
709                              the procedure is stopped. So 'Cancle' error is not return.
710                         */
711                         result = BLUETOOTH_ERROR_HOST_DOWN;
712                 } else if (g_strrstr(err->message, BT_TIMEOUT_MESSAGE)) {
713                         g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
714                                                 g_variant_new("(s)", bonding_info->addr),
715                                                 G_DBUS_CALL_FLAGS_NONE,
716                                                 -1,
717                                                 NULL,
718                                                 NULL);
719
720                         result = BLUETOOTH_ERROR_INTERNAL;
721                 } else if (g_strrstr(err->message, "Connection Timeout")) {
722                         /* Pairing request timeout */
723                         result = BLUETOOTH_ERROR_TIMEOUT;
724                 } else if (g_strrstr(err->message, "Authentication Timeout")) {
725                         /* Pairing request timeout */
726                         result = BLUETOOTH_ERROR_TIMEOUT;
727                 } else {
728                         BT_DBG("Default case");
729                         result = BLUETOOTH_ERROR_PARING_FAILED;
730                 }
731         }
732
733         if (result == BLUETOOTH_ERROR_PARING_FAILED ||
734                         result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
735                         result == BLUETOOTH_ERROR_TIMEOUT ||
736                         result == BLUETOOTH_ERROR_HOST_DOWN) {
737                 bonding_info->result = result;
738 #ifdef TIZEN_WEARABLE
739                 __bt_launch_unable_to_pairing_syspopup(result);
740 #endif
741         }
742
743         g_object_unref(proxy);
744         bonding_info->device_proxy = NULL;
745
746         if (result != BLUETOOTH_ERROR_NONE)
747                 goto dbus_return;
748
749         remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
750         if (remote_dev_info == NULL)
751                 goto dbus_return;
752
753         GVariant *uuids = NULL;
754         GVariantBuilder *builder = NULL;
755         int i = 0;
756         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
757         for (i = 0; i < remote_dev_info->uuid_count; i++) {
758                 g_variant_builder_add(builder, "s",
759                         remote_dev_info->uuids[i]);
760         }
761         uuids = g_variant_new("as", builder);
762         g_variant_builder_unref(builder);
763         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
764                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
765                                                 TRUE, NULL, NULL);
766
767         param = g_variant_new("(isunsbub@asn@ay)",
768                         result,
769                         bonding_info->addr,
770                         remote_dev_info->class,
771                         remote_dev_info->rssi,
772                         remote_dev_info->name,
773                         remote_dev_info->paired,
774                         remote_dev_info->connected,
775                         remote_dev_info->trust,
776                         uuids,
777                         remote_dev_info->manufacturer_data_len,
778                         manufacture_data);
779
780
781         /* Send the event to application */
782         _bt_send_event(BT_ADAPTER_EVENT,
783                 BLUETOOTH_EVENT_BONDING_FINISHED,
784                 param);
785
786         _bt_free_device_info(remote_dev_info);
787
788 dbus_return:
789         if (req_info->context == NULL)
790                 goto done;
791
792         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
793         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
794                                         bonding_info->addr);
795
796         if (_bt_adapter_get_status() != BT_ACTIVATED)
797                 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
798
799         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
800                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
801
802         g_dbus_method_invocation_return_value(req_info->context,
803                         g_variant_new("(iv)", result, out_param1));
804
805         g_variant_unref(out_param1);
806 //      g_variant_unref(out_param2);
807
808         _bt_delete_request_list(req_info->req_id);
809 done:
810         if (err)
811                 g_error_free(err);
812
813         _bt_agent_set_canceled(FALSE);
814
815
816         g_free(bonding_info->addr);
817         g_free(bonding_info);
818         bonding_info = NULL;
819 }
820
821 int _bt_bond_device(int request_id,
822                 bluetooth_device_address_t *device_address,
823                 unsigned short conn_type, GArray **out_param1)
824 {
825         GDBusProxy *proxy;
826         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
827         bluetooth_device_info_t dev_info;
828
829         GDBusConnection *conn;
830         char *device_path = NULL;
831         GDBusProxy *adapter_proxy;
832         GError *error = NULL;
833
834         BT_CHECK_PARAMETER(device_address, return);
835
836         if (bonding_info) {
837                 BT_ERR("Bonding in progress");
838
839                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
840                 memcpy(dev_info.device_address.addr, device_address->addr,
841                                 BLUETOOTH_ADDRESS_LENGTH);
842
843                 g_array_append_vals(*out_param1, &dev_info,
844                                 sizeof(bluetooth_device_info_t));
845
846                 return BLUETOOTH_ERROR_DEVICE_BUSY;
847         }
848
849         conn = _bt_get_system_gconn();
850         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
851
852         _bt_convert_addr_type_to_string(address, device_address->addr);
853
854         device_path = _bt_get_device_object_path(address);
855
856         if (device_path == NULL) {
857                 BT_ERR("No searched device");
858                 GVariant *ret = NULL;
859                 adapter_proxy = _bt_get_adapter_proxy();
860                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
861
862                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
863                                         g_variant_new("(s)", address),
864                                         G_DBUS_CALL_FLAGS_NONE,
865                                         -1,
866                                         NULL,
867                                         &error);
868
869                 if (error != NULL) {
870                         BT_ERR("CreateDevice Fail: %s", error->message);
871                         g_clear_error(&error);
872                 }
873                 if (ret)
874                         g_variant_unref(ret);
875                 device_path = _bt_get_device_object_path(address);
876                 if (device_path == NULL) {
877                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
878                         memcpy(dev_info.device_address.addr, device_address->addr,
879                                         BLUETOOTH_ADDRESS_LENGTH);
880
881                         g_array_append_vals(*out_param1, &dev_info,
882                                         sizeof(bluetooth_device_info_t));
883
884                         return BLUETOOTH_ERROR_NOT_PAIRED;
885                 } else {
886                         BT_INFO("device_path is created[%s]", device_path);
887                 }
888         }
889
890         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
891                                                                 NULL, BT_BLUEZ_NAME,
892                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
893
894         g_free(device_path);
895         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
896
897         bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
898         bonding_info->addr = g_strdup(address);
899         bonding_info->req_id = request_id;
900
901         bonding_info->device_proxy = proxy;
902         bonding_info->conn_type = conn_type;
903
904         is_device_creating = TRUE;
905
906         g_dbus_proxy_call(proxy, "Pair",
907                                 g_variant_new("(y)", conn_type),
908                                 G_DBUS_CALL_FLAGS_NONE,
909                                 BT_MAX_DBUS_TIMEOUT,
910                                 NULL,
911                                 (GAsyncReadyCallback)__bt_bond_device_cb,
912                                 NULL);
913
914 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
915
916         return BLUETOOTH_ERROR_NONE;
917 /*fail:
918         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
919         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
920                                         bonding_info->addr);
921
922         g_array_append_vals(*out_param1, &dev_info,
923                                 sizeof(bluetooth_device_info_t));
924
925         is_device_creating = FALSE;
926
927         g_free(bonding_info->addr);
928         g_free(bonding_info);
929         bonding_info = NULL;
930
931         return BLUETOOTH_ERROR_INTERNAL;*/
932 }
933
934 int _bt_cancel_bonding(void)
935 {
936         int ret = BLUETOOTH_ERROR_NONE;
937
938         retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
939
940         ret = _bt_agent_reply_cancellation();
941         if (ret != BLUETOOTH_ERROR_NONE) {
942                 BT_ERR("Fail to call reply cancellation");
943                 return ret;
944         }
945
946         _bt_agent_set_canceled(TRUE);
947
948         return BLUETOOTH_ERROR_NONE;
949 }
950
951 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
952                                         gpointer user_data)
953 {
954         GError *err = NULL;
955         GVariant *out_param1;
956         int result = BLUETOOTH_ERROR_NONE;
957         bt_funcion_data_t *unbonding_info;
958         bluetooth_device_info_t dev_info;
959         request_info_t *req_info;
960
961         g_dbus_proxy_call_finish(proxy, res, &err);
962
963         unbonding_info = user_data;
964
965         if (unbonding_info == NULL) {
966                 /* Send reply */
967                 BT_ERR("unbonding_info == NULL");
968                 goto done;
969         }
970
971         req_info = _bt_get_request_info(unbonding_info->req_id);
972         if (req_info == NULL) {
973                 BT_ERR("req_info == NULL");
974                 goto done;
975         }
976
977         if (err != NULL) {
978                 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
979                 result = BLUETOOTH_ERROR_INTERNAL;
980         }
981
982         if (req_info->context == NULL)
983                 goto done;
984
985         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
986         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
987                                         unbonding_info->addr);
988
989         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
990                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
991
992         g_dbus_method_invocation_return_value(req_info->context,
993                         g_variant_new("(iv)", result, out_param1));
994
995
996         _bt_delete_request_list(req_info->req_id);
997
998 done:
999         if (err)
1000                 g_error_free(err);
1001
1002         if (unbonding_info) {
1003                 g_free(unbonding_info->addr);
1004                 g_free(unbonding_info);
1005         }
1006 }
1007
1008 int _bt_unbond_device(int request_id,
1009                         bluetooth_device_address_t *device_address,
1010                         GArray **out_param1)
1011 {
1012         char *device_path = NULL;
1013         bt_funcion_data_t *unbonding_info;
1014         GDBusProxy *adapter_proxy = NULL;
1015         GDBusProxy *device_proxy = NULL;
1016         GDBusConnection *conn;
1017         int result = BLUETOOTH_ERROR_INTERNAL;
1018         bluetooth_device_info_t dev_info;
1019         GError *error = NULL;
1020         GVariant *ret = NULL;
1021
1022         BT_CHECK_PARAMETER(device_address, return);
1023
1024         adapter_proxy = _bt_get_adapter_proxy();
1025         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1026
1027         /* allocate user data so that it can be retrieved in callback */
1028         unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1029         /* Fix : NULL_RETURNS */
1030         if (unbonding_info == NULL) {
1031                 BT_ERR("Memory not allocated !");
1032                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1033         }
1034
1035         unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1036         unbonding_info->req_id = request_id;
1037
1038         _bt_convert_addr_type_to_string(unbonding_info->addr,
1039                                         device_address->addr);
1040
1041         device_path = _bt_get_device_object_path(unbonding_info->addr);
1042
1043         if (device_path == NULL) {
1044                 BT_ERR("No paired device");
1045                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1046                 goto fail;
1047         }
1048
1049         conn = _bt_get_system_gconn();
1050         if (conn == NULL) {
1051                 BT_ERR("conn is NULL");
1052                 result = BLUETOOTH_ERROR_INTERNAL;
1053                 goto fail;
1054         }
1055
1056         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1057                                                                 NULL, BT_BLUEZ_NAME,
1058                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1059
1060         if (device_proxy != NULL) {
1061
1062                 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1063                                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1064                                         G_DBUS_CALL_FLAGS_NONE,
1065                                         -1,
1066                                         NULL,
1067                                         &error);
1068                 if (error) {
1069                         BT_ERR("Getting property failed: [%s]\n", error->message);
1070                         g_error_free(error);
1071                         result = BLUETOOTH_ERROR_NOT_PAIRED;
1072                         goto fail;
1073                 } else {
1074                         if (!ret) {
1075                                 BT_ERR("No paired device");
1076                                 g_object_unref(device_proxy);
1077                                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1078                                 goto fail;
1079                         }
1080                         g_variant_unref(ret);
1081                 }
1082                 g_object_unref(device_proxy);
1083         }
1084
1085         g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1086                                 g_variant_new("(o)", device_path),
1087                                 G_DBUS_CALL_FLAGS_NONE,
1088                                 BT_MAX_DBUS_TIMEOUT,
1089                                 NULL,
1090                                 (GAsyncReadyCallback)__bt_unbond_cb,
1091                                 NULL);
1092
1093         g_free(device_path);
1094         return BLUETOOTH_ERROR_NONE;
1095
1096 fail:
1097         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1098         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1099                                         unbonding_info->addr);
1100
1101         g_array_append_vals(*out_param1, &dev_info,
1102                                 sizeof(bluetooth_device_info_t));
1103         g_free(device_path);
1104         g_free(unbonding_info->addr);
1105         g_free(unbonding_info);
1106         return result;
1107 }
1108
1109 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1110                                         gpointer user_data)
1111 {
1112         GError *err = NULL;
1113         GVariant *out_param1;
1114         int result = BLUETOOTH_ERROR_NONE;
1115         bluetooth_device_info_t dev_info;
1116         bt_remote_dev_info_t *remote_dev_info;
1117         request_info_t *req_info;
1118         GVariant *uuid_list, *manufacture_data;
1119         GVariant *param;
1120         GVariantBuilder *builder = NULL;
1121         int i = 0;
1122
1123         g_dbus_proxy_call_finish(proxy, res, &err);
1124
1125         g_object_unref(proxy);
1126
1127         if (searching_info == NULL) {
1128                 /* Send reply */
1129                 BT_ERR("unbonding_info == NULL");
1130                 goto done;
1131         }
1132
1133         req_info = _bt_get_request_info(searching_info->req_id);
1134         if (req_info == NULL) {
1135                 BT_ERR("req_info == NULL");
1136                 goto done;
1137         }
1138
1139         if (err != NULL) {
1140                 g_dbus_error_strip_remote_error(err);
1141                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1142
1143                 if (g_strrstr("Operation canceled", err->message)) {
1144                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1145                 } else if (g_strrstr("In Progress", err->message)) {
1146                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1147                 } else if (g_strrstr("Host is down", err->message)) {
1148                         result = BLUETOOTH_ERROR_HOST_DOWN;
1149                 } else {
1150                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1151                 }
1152
1153                 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1154                      result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1155                         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1156                         if (remote_dev_info && remote_dev_info->uuids != NULL &&
1157                              remote_dev_info->uuid_count > 0) {
1158                                 result = BLUETOOTH_ERROR_NONE;
1159                                 goto event;
1160                         }
1161                         _bt_free_device_info(remote_dev_info);
1162                 }
1163                 goto dbus_return;
1164         }
1165
1166         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1167         if (remote_dev_info == NULL)
1168                 goto dbus_return;
1169
1170 event:
1171         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1172         for (i = 0; i < remote_dev_info->uuid_count; i++) {
1173                 g_variant_builder_add(builder, "s",
1174                         remote_dev_info->uuids[i]);
1175         }
1176         uuid_list = g_variant_new("as", builder);
1177         g_variant_builder_unref(builder);
1178         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1179                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1180                                                 TRUE, NULL, NULL);
1181
1182         param = g_variant_new("(isunsbub@asn@ay)",
1183                         result,
1184                         searching_info->addr,
1185                         remote_dev_info->class,
1186                         remote_dev_info->rssi,
1187                         remote_dev_info->name,
1188                         remote_dev_info->paired,
1189                         remote_dev_info->connected,
1190                         remote_dev_info->trust,
1191                         uuid_list,
1192                         remote_dev_info->manufacturer_data_len,
1193                         manufacture_data);
1194
1195         /* Send the event to application */
1196         _bt_send_event(BT_ADAPTER_EVENT,
1197                 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1198                 param);
1199
1200         _bt_free_device_info(remote_dev_info);
1201
1202 dbus_return:
1203         if (req_info->context == NULL)
1204                 goto done;
1205
1206         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1207         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1208                                         searching_info->addr);
1209
1210         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1211                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1212
1213         g_dbus_method_invocation_return_value(req_info->context,
1214                         g_variant_new("(iv)", result, out_param1));
1215
1216
1217         _bt_delete_request_list(req_info->req_id);
1218 done:
1219         if (err)
1220                 g_error_free(err);
1221
1222         if (searching_info) {
1223                 g_free(searching_info->addr);
1224                 g_free(searching_info);
1225                 searching_info = NULL;
1226         }
1227 }
1228
1229 int _bt_search_device(int request_id,
1230                         bluetooth_device_address_t *device_address)
1231 {
1232         char *device_path = NULL;
1233         GDBusProxy *device_proxy = NULL;
1234         GDBusConnection *conn;
1235
1236         GDBusProxy *adapter_proxy;
1237         int result = BLUETOOTH_ERROR_INTERNAL;
1238
1239         BT_CHECK_PARAMETER(device_address, return);
1240
1241         if (bonding_info) {
1242                 BT_ERR("Bonding in progress");
1243                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1244         }
1245
1246         if (searching_info) {
1247                 BT_ERR("Service searching in progress");
1248                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1249         }
1250
1251         adapter_proxy = _bt_get_adapter_proxy();
1252         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1253
1254         /* allocate user data so that it can be retrieved in callback */
1255         searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1256         searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1257         searching_info->req_id = request_id;
1258
1259         _bt_convert_addr_type_to_string(searching_info->addr,
1260                                         device_address->addr);
1261
1262         conn = _bt_get_system_gconn();
1263         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1264
1265
1266         device_path = _bt_get_device_object_path(searching_info->addr);
1267
1268         if (device_path == NULL) {
1269                 BT_ERR("No paired device");
1270                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1271                 goto fail;
1272         }
1273
1274         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1275                                                                 NULL, BT_BLUEZ_NAME,
1276                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1277         g_free(device_path);
1278         if (device_proxy == NULL) {
1279                 result = BLUETOOTH_ERROR_INTERNAL;
1280                 goto fail;
1281         }
1282
1283         g_dbus_proxy_call(device_proxy, "DiscoverServices",
1284                         g_variant_new("(s)", ""),
1285                         G_DBUS_CALL_FLAGS_NONE,
1286                         BT_MAX_DBUS_TIMEOUT,
1287                         NULL,
1288                         (GAsyncReadyCallback)__bt_discover_cb,
1289                         searching_info);
1290
1291         searching_info->device_proxy = device_proxy;
1292
1293         return BLUETOOTH_ERROR_NONE;
1294 fail:
1295         g_free(searching_info->addr);
1296         g_free(searching_info);
1297         searching_info = NULL;
1298         return result;
1299 }
1300
1301 int _bt_cancel_search_device(void)
1302 {
1303         GError *err = NULL;
1304
1305         retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1306
1307         if (searching_info->device_proxy) {
1308                 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1309                                 NULL,
1310                                 G_DBUS_CALL_FLAGS_NONE,
1311                                 -1,
1312                                 NULL,
1313                                 &err);
1314         }
1315         __bt_cancel_search_service_done();
1316
1317         return BLUETOOTH_ERROR_NONE;
1318 }
1319
1320 int _bt_set_alias(bluetooth_device_address_t *device_address,
1321                                       const char *alias)
1322 {
1323         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1324         gchar *device_path = NULL;
1325         GDBusProxy *adapter_proxy;
1326         GDBusProxy *device_proxy;
1327         GError *error = NULL;
1328         GDBusConnection *conn;
1329
1330         BT_CHECK_PARAMETER(device_address, return);
1331         BT_CHECK_PARAMETER(alias, return);
1332
1333         adapter_proxy = _bt_get_adapter_proxy();
1334         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1335
1336         conn = _bt_get_system_gconn();
1337         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1338
1339         _bt_convert_addr_type_to_string(address, device_address->addr);
1340
1341         device_path = _bt_get_device_object_path(address);
1342
1343         if (device_path == NULL) {
1344                 BT_ERR("No paired device");
1345                 return BLUETOOTH_ERROR_NOT_PAIRED;
1346         }
1347
1348         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1349                                                                 NULL, BT_BLUEZ_NAME,
1350                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1351
1352         g_free(device_path);
1353         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1354
1355         g_dbus_proxy_call_sync(device_proxy, "Set",
1356                                 g_variant_new("(ssv)", BT_DEVICE_INTERFACE,  "Alias", g_variant_new("s", alias)),
1357                                 G_DBUS_CALL_FLAGS_NONE,
1358                                 -1,
1359                                 NULL,
1360                                 &error);
1361
1362         g_object_unref(device_proxy);
1363
1364         if (error) {
1365                  BT_ERR("SetProperty error: [%s]", error->message);
1366                  g_error_free(error);
1367                  return BLUETOOTH_ERROR_INTERNAL;
1368         }
1369
1370         return BLUETOOTH_ERROR_NONE;
1371 }
1372
1373 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1374                                       gboolean authorize)
1375 {
1376         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1377         gchar *device_path = NULL;
1378         GDBusProxy *device_proxy;
1379         gboolean previous_value;
1380         GError *error = NULL;
1381         GDBusConnection *conn;
1382         GVariant *result = NULL;
1383         int ret = BLUETOOTH_ERROR_NONE;
1384
1385         BT_CHECK_PARAMETER(device_address, return);
1386
1387         conn = _bt_get_system_gconn();
1388         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1389
1390         _bt_convert_addr_type_to_string(address, device_address->addr);
1391
1392         device_path = _bt_get_device_object_path(address);
1393
1394         if (device_path == NULL) {
1395                 BT_ERR("No paired device");
1396                 return BLUETOOTH_ERROR_NOT_PAIRED;
1397         }
1398
1399         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1400                                                                 NULL, BT_BLUEZ_NAME,
1401                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1402
1403         g_free(device_path);
1404         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1405
1406         result = g_dbus_proxy_call_sync(device_proxy, "Get",
1407                                 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1408                                 G_DBUS_CALL_FLAGS_NONE,
1409                                 -1,
1410                                 NULL,
1411                                 &error);
1412         if (error != NULL) {
1413                 BT_ERR("Getting property failed: [%s]\n", error->message);
1414                 g_error_free(error);
1415                 g_object_unref(device_proxy);
1416                 return BLUETOOTH_ERROR_INTERNAL;
1417         }
1418
1419         previous_value = g_variant_get_boolean(result);
1420         g_variant_unref(result);
1421         /* If the input is same with previous value, return error. */
1422         if (previous_value == authorize) {
1423                 BT_ERR("Same value: %d", previous_value);
1424                 g_object_unref(device_proxy);
1425                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1426                 goto done;
1427         }
1428
1429         g_dbus_proxy_call_sync(device_proxy, "Set",
1430                                  g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1431                                  G_DBUS_CALL_FLAGS_NONE,
1432                                  -1,
1433                                  NULL,
1434                                  &error);
1435
1436         g_object_unref(device_proxy);
1437         if (error) {
1438                  BT_ERR("SetProperty error: [%s]", error->message);
1439                  g_error_free(error);
1440                  ret = BLUETOOTH_ERROR_INTERNAL;
1441         }
1442 done:
1443         return ret;
1444 }
1445
1446 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1447                         gboolean *is_connected)
1448 {
1449         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1450         char *object_path = NULL;
1451
1452         GDBusProxy *device_proxy;
1453         GError *error = NULL;
1454         GVariant *value;
1455         GVariant *tmp_value;
1456         GDBusConnection *conn;
1457         GVariant *result = NULL;
1458         int ret = BLUETOOTH_ERROR_NONE;
1459
1460         BT_CHECK_PARAMETER(device_address, return);
1461
1462         conn = _bt_get_system_gconn();
1463         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1464
1465         _bt_convert_addr_type_to_string(address, device_address->addr);
1466
1467         object_path = _bt_get_device_object_path(address);
1468         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1469
1470         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1471                                                                 NULL, BT_BLUEZ_NAME,
1472                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1473         g_free(object_path);
1474         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1475
1476         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1477                           g_variant_new("(s)", BT_DEVICE_INTERFACE),
1478                                 G_DBUS_CALL_FLAGS_NONE,
1479                                 -1,
1480                                 NULL,
1481                                 &error);
1482         if (error != NULL || result == NULL) {
1483                 if (error != NULL) {
1484                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1485                         g_error_free(error);
1486                 }
1487                 g_object_unref(device_proxy);
1488                 return BLUETOOTH_ERROR_INTERNAL;
1489         }
1490
1491         g_variant_get(result , "(@a{sv})", &value);
1492         g_variant_unref(result);
1493
1494         tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1495
1496         if (tmp_value == NULL) {
1497                 g_object_unref(device_proxy);
1498                 return BLUETOOTH_ERROR_INTERNAL;
1499         }
1500
1501         *is_connected = g_variant_get_boolean(tmp_value);
1502
1503         BT_DBG("gatt is connected : %d", *is_connected);
1504         g_variant_unref(tmp_value);
1505         g_variant_unref(value);
1506         g_object_unref(device_proxy);
1507
1508         return ret;
1509 }
1510
1511 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1512                         int connection_type, gboolean *is_connected)
1513 {
1514         char *object_path = NULL;
1515         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1516         GDBusProxy *device_proxy = NULL;
1517         GDBusProxy *adapter_proxy = NULL;
1518         GDBusConnection *conn;
1519         GError *error = NULL;
1520         GVariant *tmp_value = NULL;
1521         GVariant *value = NULL;
1522         GVariant *result = NULL;
1523         char *uuid = NULL;
1524
1525         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1526         retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1527
1528         *is_connected = FALSE;
1529         BT_DBG("connection_type: %d", connection_type);
1530         if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1531                 return _bt_rfcomm_is_device_connected(device_address,
1532                                                 is_connected);
1533         else if (connection_type == BLUETOOTH_GATT_SERVICE)
1534                 return _bt_is_gatt_connected(device_address, is_connected);
1535
1536         adapter_proxy = _bt_get_adapter_proxy();
1537         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1538
1539         conn = _bt_get_system_gconn();
1540         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1541
1542         _bt_convert_addr_type_to_string(address, device_address->addr);
1543
1544         if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE)    {
1545                 object_path = _bt_get_adapter_path();
1546                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1547                                                                         NULL, BT_BLUEZ_NAME,
1548                                                                         object_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
1549                 g_free(object_path);
1550                 if (device_proxy == NULL) {
1551                         BT_DBG("Device don't have this service");
1552                         return BLUETOOTH_ERROR_NONE;
1553                 }
1554
1555                 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1556                                         g_variant_new("(s)", address),
1557                                         G_DBUS_CALL_FLAGS_NONE,
1558                                         -1,
1559                                         NULL,
1560                                         &error);
1561
1562                 if (result == NULL) {
1563                         BT_ERR("Error occured in Proxy call");
1564                         if (error) {
1565                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1566                                 g_error_free(error);
1567                         }
1568                         g_object_unref(device_proxy);
1569                         return BLUETOOTH_ERROR_NONE;
1570                 }
1571                 g_variant_get(result , "(@a{sv})", &value);
1572                 g_variant_unref(result);
1573
1574                 if (value) {
1575                         tmp_value = g_variant_lookup_value(value,
1576                                                         "Connected",
1577                                                         G_VARIANT_TYPE_BOOLEAN);
1578                         if (tmp_value) {
1579                                 *is_connected = g_variant_get_boolean(tmp_value);
1580                                 g_variant_unref(tmp_value);
1581                         }
1582                         g_variant_unref(value);
1583                 }
1584         } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1585                 return _bt_is_network_connected(_bt_get_net_conn(),
1586                                                 device_address->addr, is_connected);
1587         } else {
1588                 uuid = _bt_get_profile_uuid128(connection_type);
1589                 if (uuid == NULL) {
1590                         BT_ERR("uuid is NULL");
1591                         return BLUETOOTH_ERROR_NONE;
1592                 }
1593
1594                 BT_DBG("uuid: %s", uuid);
1595
1596                 object_path = _bt_get_device_object_path(address);
1597                 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1598                 BT_DBG("object_path: %s", object_path);
1599                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1600                                                                         NULL, BT_BLUEZ_NAME,
1601                                                                         object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1602                 g_free(object_path);
1603                 if (device_proxy == NULL) {
1604                         BT_DBG("Device don't have this service");
1605                         g_free(uuid);
1606                         return BLUETOOTH_ERROR_NONE;
1607                 }
1608
1609                 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1610                                         g_variant_new("(s)", uuid),
1611                                         G_DBUS_CALL_FLAGS_NONE,
1612                                         -1,
1613                                         NULL,
1614                                         &error);
1615
1616                 if (result == NULL) {
1617                         BT_ERR("Error occured in Proxy call");
1618                         if (error) {
1619                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1620                                 g_error_free(error);
1621                         }
1622                 } else {
1623                         g_variant_get(result, "(b)", is_connected);
1624                         g_free(uuid);
1625                         g_variant_unref(result);
1626                 }
1627         }
1628
1629         g_object_unref(device_proxy);
1630         return BLUETOOTH_ERROR_NONE;
1631 }
1632
1633 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1634                         bluetooth_connected_link_t *connected)
1635 {
1636         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1637         char *object_path = NULL;
1638
1639         GDBusProxy *device_proxy;
1640         GError *error = NULL;
1641         GDBusConnection *conn;
1642         GVariant *tmp_value = NULL;
1643         GVariant *value = NULL;
1644         GVariant *result = NULL;
1645
1646         int ret = BLUETOOTH_ERROR_NONE;
1647
1648         BT_CHECK_PARAMETER(device_address, return);
1649
1650         conn = _bt_get_system_gconn();
1651         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1652
1653         _bt_convert_addr_type_to_string(address, device_address->addr);
1654
1655         object_path = _bt_get_device_object_path(address);
1656         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1657
1658         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1659                                                                 NULL, BT_BLUEZ_NAME,
1660                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1661         g_free(object_path);
1662         if (device_proxy == NULL) {
1663                 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1664                 return BLUETOOTH_ERROR_NONE;
1665         }
1666
1667         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1668                                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1669                                         G_DBUS_CALL_FLAGS_NONE,
1670                                         -1,
1671                                         NULL,
1672                                         &error);
1673
1674         if (error != NULL) {
1675                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1676                 g_error_free(error);
1677                 g_object_unref(device_proxy);
1678                 return BLUETOOTH_ERROR_INTERNAL;
1679         }
1680
1681         g_variant_get(result , "(@a{sv})", &value);
1682         g_variant_unref(result);
1683
1684         tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1685         if (tmp_value != NULL) {
1686                 g_object_unref(device_proxy);
1687                 g_variant_unref(value);
1688                 return BLUETOOTH_ERROR_INTERNAL;
1689         }
1690         *connected = g_variant_get_boolean(tmp_value);
1691         g_variant_unref(tmp_value);
1692         g_variant_unref(value);
1693         g_object_unref(device_proxy);
1694
1695         return ret;
1696 }
1697
1698 static void __bt_connect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1699                                         gpointer user_data)
1700 {
1701         GError *err = NULL;
1702         GVariant *out_param1;
1703         request_info_t *req_info;
1704         bluetooth_device_address_t device_addr = { {0} };
1705         int result = BLUETOOTH_ERROR_NONE;
1706
1707         BT_DBG("+");
1708         g_dbus_proxy_call_finish(proxy, res, &err);
1709
1710         /* When cb datas of lescan are received, back to parameta values for Web TCT */
1711         if (_bt_is_set_scan_parameter() == FALSE) {
1712                 bluetooth_le_scan_params_t scan_params;
1713                 BT_ERR("set parameter of lescan when receive cb data");
1714                 scan_params.type = BT_LE_ACTIVE_SCAN;
1715                 scan_params.interval = 5000;
1716                 scan_params.window = 500;
1717                 _bt_set_scan_parameters(&scan_params);
1718         }
1719
1720         req_info = _bt_get_request_info(le_connection_info->req_id);
1721         if (req_info == NULL) {
1722                 BT_ERR("req_info == NULL");
1723                 g_object_unref(proxy);
1724                 le_connection_info->device_proxy = NULL;
1725                 goto done;
1726         }
1727
1728         if (err != NULL) {
1729                 BT_ERR("Error occured in ConnectLE [%s]", err->message);
1730
1731                 if (g_strrstr(err->message, "NotSupported")) {
1732                         BT_INFO("Connection Not Supported");
1733                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1734                 } else if (g_strrstr(err->message, "AlreadyConnected")) {
1735                         BT_INFO("Connection already exists");
1736                         result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1737                 } else if (g_strrstr(err->message, "Connection Timeout")) {
1738                         BT_INFO("Connection Timeout");
1739                         result = BLUETOOTH_ERROR_TIMEOUT;
1740                 } else {
1741                         BT_DBG("Default case");
1742                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1743                 }
1744         }
1745
1746         g_object_unref(proxy);
1747         le_connection_info->device_proxy = NULL;
1748
1749         _bt_convert_addr_string_to_type(device_addr.addr,
1750                                         le_connection_info->addr);
1751
1752         if (req_info->context == NULL)
1753                 goto done;
1754
1755         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1756                         &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1757         g_dbus_method_invocation_return_value(req_info->context,
1758                         g_variant_new("(iv)", result, out_param1));
1759
1760         g_variant_unref(out_param1);
1761
1762         _bt_delete_request_list(req_info->req_id);
1763 done:
1764         if (err)
1765                 g_error_free(err);
1766
1767         g_free(le_connection_info->addr);
1768         g_free(le_connection_info);
1769         le_connection_info = NULL;
1770
1771         BT_DBG("-");
1772 }
1773
1774 int _bt_connect_le_device(int request_id,
1775                 const bluetooth_device_address_t *bd_addr,
1776                 gboolean auto_connect)
1777 {
1778         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779         gchar *device_path = NULL;
1780         GDBusProxy *device_proxy = NULL;
1781         GDBusProxy *adapter_proxy;
1782         GDBusConnection *conn;
1783         int ret = BLUETOOTH_ERROR_NONE;
1784
1785         BT_CHECK_PARAMETER(bd_addr, return);
1786
1787         _bt_convert_addr_type_to_string(device_address,
1788                         (unsigned char *)bd_addr->addr);
1789
1790         if (le_connection_info) {
1791                 BT_ERR("LE Connection in progress");
1792                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1793         }
1794
1795 /* When auto_connect is true, Web TCT fail. It need to be checked */
1796         auto_connect = FALSE;
1797
1798         conn = _bt_get_system_gconn();
1799         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1800
1801         adapter_proxy = _bt_get_adapter_proxy();
1802         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1803
1804         device_path = _bt_get_device_object_path(device_address);
1805         if (device_path == NULL) {
1806                 BT_DBG("device_path NULL");
1807                 ret = BLUETOOTH_ERROR_INTERNAL;
1808                 return ret;
1809         }
1810
1811         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1812
1813         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1814                                                                 NULL, BT_BLUEZ_NAME,
1815                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1816         g_free(device_path);
1817         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1818
1819
1820         le_connection_info = g_malloc0(sizeof(bt_funcion_data_t));
1821         le_connection_info->addr = g_strdup(device_address);
1822         le_connection_info->req_id = request_id;
1823         le_connection_info->device_proxy = device_proxy;
1824
1825 /* The connection between Ble device and TM1 often fail. So It need to be set for Web TCT*/
1826         if (_bt_is_set_scan_parameter() == FALSE) {
1827                 BT_ERR("set parameter of le scan when connect device");
1828                 bluetooth_le_scan_params_t scan_params;
1829                 scan_params.type = BT_LE_ACTIVE_SCAN;
1830                 scan_params.interval = 60;
1831                 scan_params.window = 60;
1832                 _bt_set_scan_parameters(&scan_params);
1833         }
1834
1835         auto_connect = FALSE;
1836
1837         g_dbus_proxy_call(device_proxy, "ConnectLE",
1838                                  g_variant_new("(b)", auto_connect),
1839                                  G_DBUS_CALL_FLAGS_NONE,
1840                                  BT_MAX_DBUS_TIMEOUT,
1841                                  NULL,
1842                                  (GAsyncReadyCallback)__bt_connect_le_device_cb,
1843                                  NULL);
1844
1845
1846         return ret;
1847 }
1848
1849 static void __bt_disconnect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1850                                         gpointer user_data)
1851 {
1852         GError *err = NULL;
1853         GVariant *out_param1;
1854         request_info_t *req_info;
1855         bt_funcion_data_t *le_disconnection_info = user_data;
1856         bluetooth_device_address_t device_addr = { {0} };
1857         int result = BLUETOOTH_ERROR_NONE;
1858
1859         BT_DBG("+");
1860         g_dbus_proxy_call_finish(proxy, res, &err);
1861
1862         req_info = _bt_get_request_info(le_disconnection_info->req_id);
1863         if (req_info == NULL) {
1864                 BT_ERR("req_info == NULL");
1865                 g_object_unref(proxy);
1866                 le_disconnection_info->device_proxy = NULL;
1867                 goto done;
1868         }
1869
1870         if (err != NULL) {
1871                 BT_ERR("Error occured in DisconnectLE [%s]", err->message);
1872
1873                 if (g_strrstr(err->message, "NotSupported")) {
1874                         BT_INFO("Connection Not Supported");
1875                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1876                 } else if (g_strrstr(err->message, "NotConnected")) {
1877                         BT_INFO("Connection does not exists");
1878                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
1879                 } else if (g_strrstr(err->message, "Cancelled")) {
1880                         BT_INFO("User Cancelled");
1881                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1882                 } else {
1883                         BT_DBG("Default case");
1884                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1885                 }
1886         }
1887
1888         g_object_unref(proxy);
1889         le_disconnection_info->device_proxy = NULL;
1890
1891         _bt_convert_addr_string_to_type(device_addr.addr,
1892                                         le_disconnection_info->addr);
1893
1894         if (req_info->context == NULL)
1895                 goto done;
1896
1897         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1898                 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1899
1900         g_dbus_method_invocation_return_value(req_info->context,
1901                         g_variant_new("(iv)", result, out_param1));
1902
1903         g_variant_unref(out_param1);
1904
1905         _bt_delete_request_list(req_info->req_id);
1906 done:
1907         if (err)
1908                 g_error_free(err);
1909
1910         g_free(le_disconnection_info->addr);
1911         g_free(le_disconnection_info);
1912         le_disconnection_info = NULL;
1913
1914         BT_DBG("-");
1915 }
1916
1917 int _bt_disconnect_le_device(int request_id,
1918                 const bluetooth_device_address_t *bd_addr)
1919 {
1920         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1921         gchar *device_path = NULL;
1922         GDBusProxy *device_proxy = NULL;
1923         GDBusProxy *adapter_proxy;
1924         GDBusConnection *conn;
1925         int ret = BLUETOOTH_ERROR_NONE;
1926         bt_funcion_data_t *le_disconnection_info = NULL;
1927
1928         BT_CHECK_PARAMETER(bd_addr, return);
1929
1930         _bt_convert_addr_type_to_string(device_address,
1931                         (unsigned char *)bd_addr->addr);
1932
1933         conn = _bt_get_system_gconn();
1934         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1935
1936         adapter_proxy = _bt_get_adapter_proxy();
1937         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1938
1939         device_path = _bt_get_device_object_path(device_address);
1940         if (device_path == NULL) {
1941                 BT_DBG("device_path NULL");
1942                 ret = BLUETOOTH_ERROR_INTERNAL;
1943                 return ret;
1944         }
1945
1946         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1947
1948         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1949                                                                 NULL, BT_BLUEZ_NAME,
1950                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1951         g_free(device_path);
1952         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1953
1954         le_disconnection_info = g_malloc0(sizeof(bt_funcion_data_t));
1955         le_disconnection_info->addr = g_strdup(device_address);
1956         le_disconnection_info->req_id = request_id;
1957         le_disconnection_info->device_proxy = device_proxy;
1958
1959         g_dbus_proxy_call(device_proxy, "DisconnectLE",
1960                                  NULL,
1961                                  G_DBUS_CALL_FLAGS_NONE,
1962                                  BT_MAX_DBUS_TIMEOUT,
1963                                  NULL,
1964                                  (GAsyncReadyCallback)__bt_disconnect_le_device_cb,
1965                                  le_disconnection_info);
1966
1967         return ret;
1968 }
1969
1970 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
1971 {
1972         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1973         gchar *device_path = NULL;
1974         GError *error = NULL;
1975         GDBusProxy *device_proxy = NULL;
1976         GDBusProxy *adapter_proxy;
1977         GDBusConnection *conn;
1978         int ret = BLUETOOTH_ERROR_NONE;
1979
1980         BT_CHECK_PARAMETER(bd_addr, return);
1981
1982         _bt_convert_addr_type_to_string(device_address,
1983                         (unsigned char *)bd_addr->addr);
1984
1985         conn = _bt_get_system_gconn();
1986         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1987
1988         adapter_proxy = _bt_get_adapter_proxy();
1989         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1990
1991         device_path = _bt_get_device_object_path(device_address);
1992         if (device_path == NULL) {
1993                 BT_DBG("device_path NULL");
1994                 ret = BLUETOOTH_ERROR_INTERNAL;
1995                 return ret;
1996         }
1997
1998         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1999
2000         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2001                                                         NULL, BT_BLUEZ_NAME,
2002                                                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2003         g_free(device_path);
2004         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2005
2006         g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2007                                 NULL,
2008                                 G_DBUS_CALL_FLAGS_NONE,
2009                                 -1,
2010                                 NULL,
2011                                 &error);
2012         if (error) {
2013                 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2014                 g_error_free(error);
2015                 g_object_unref(device_proxy);
2016                 return BLUETOOTH_ERROR_INTERNAL;
2017         }
2018
2019         g_object_unref(device_proxy);
2020
2021         return ret;
2022 }
2023
2024 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2025 {
2026         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2027         gchar *device_path = NULL;
2028         GError *error = NULL;
2029         GDBusProxy *device_proxy = NULL;
2030         GDBusProxy *adapter_proxy;
2031         GDBusConnection *conn;
2032         int ret = BLUETOOTH_ERROR_NONE;
2033
2034         BT_CHECK_PARAMETER(bd_addr, return);
2035
2036         _bt_convert_addr_type_to_string(device_address,
2037                         (unsigned char *)bd_addr->addr);
2038
2039         conn = _bt_get_system_gconn();
2040         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2041
2042         adapter_proxy = _bt_get_adapter_proxy();
2043         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2044
2045         device_path = _bt_get_device_object_path(device_address);
2046         if (device_path == NULL) {
2047                 BT_DBG("device_path NULL");
2048                 ret = BLUETOOTH_ERROR_INTERNAL;
2049                 return ret;
2050         }
2051
2052         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2053
2054         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2055                                                         NULL, BT_BLUEZ_NAME,
2056                                                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2057         g_free(device_path);
2058         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2059
2060         g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2061                                 NULL,
2062                                 G_DBUS_CALL_FLAGS_NONE,
2063                                 -1,
2064                                 NULL,
2065                                 &error);
2066         if (error) {
2067                 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2068                 g_error_free(error);
2069                 g_object_unref(device_proxy);
2070                 return BLUETOOTH_ERROR_INTERNAL;
2071         }
2072
2073         g_object_unref(device_proxy);
2074
2075         return ret;
2076 }
2077
2078 int _bt_connect_profile(char *address, char *uuid,
2079                                                 void *cb, gpointer func_data)
2080 {
2081         char *object_path;
2082         GDBusProxy *proxy;
2083         GDBusConnection *conn;
2084         GDBusProxy *adapter_proxy;
2085         GError *error = NULL;
2086
2087         conn = _bt_get_system_gconn();
2088         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2089
2090         object_path = _bt_get_device_object_path(address);
2091         if (object_path == NULL) {
2092                 BT_ERR("No searched device");
2093
2094                 adapter_proxy = _bt_get_adapter_proxy();
2095                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2096
2097                 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2098                                          g_variant_new("(s)", address),
2099                                          G_DBUS_CALL_FLAGS_NONE,
2100                                          -1,
2101                                          NULL,
2102                                          &error);
2103
2104                 if (error != NULL) {
2105                         BT_ERR("CreateDevice Fail: %s", error->message);
2106                         g_error_free(error);
2107                 }
2108
2109                 object_path = _bt_get_device_object_path(address);
2110         }
2111         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2112
2113         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2114                                                                 NULL, BT_BLUEZ_NAME,
2115                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2116         g_free(object_path);
2117         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2118
2119         g_dbus_proxy_call(proxy, "ConnectProfile",
2120                                 g_variant_new("(s)", uuid),
2121                                 G_DBUS_CALL_FLAGS_NONE,
2122                                 BT_MAX_DBUS_TIMEOUT,
2123                                 NULL,
2124                                 (GAsyncReadyCallback)cb,
2125                                 func_data);
2126
2127         return BLUETOOTH_ERROR_NONE;
2128 }
2129
2130 int _bt_disconnect_profile(char *address, char *uuid,
2131                                                 void *cb, gpointer func_data)
2132 {
2133         char *object_path;
2134         GDBusProxy *proxy;
2135         GDBusConnection *conn;
2136
2137         conn = _bt_get_system_gconn();
2138         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2139
2140         object_path = _bt_get_device_object_path(address);
2141         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2142
2143         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2144                                                                 NULL, BT_BLUEZ_NAME,
2145                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2146         g_free(object_path);
2147         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2148
2149         g_dbus_proxy_call(proxy, "DisconnectProfile",
2150                                         g_variant_new("(s)", uuid),
2151                                         G_DBUS_CALL_FLAGS_NONE,
2152                                         BT_MAX_DBUS_TIMEOUT,
2153                                         NULL,
2154                                         (GAsyncReadyCallback)cb,
2155                                         func_data);
2156
2157         return BLUETOOTH_ERROR_NONE;
2158 }
2159
2160 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2161                 int low_threshold, int in_range_threshold, int high_threshold)
2162 {
2163         int ret = BLUETOOTH_ERROR_NONE;
2164         GDBusProxy *proxy;
2165         GError *error = NULL;
2166         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2167
2168         BT_CHECK_PARAMETER(bd_addr, return);
2169         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2170                         bd_addr->addr[0], bd_addr->addr[1],
2171                         bd_addr->addr[2], bd_addr->addr[3],
2172                         bd_addr->addr[4], bd_addr->addr[5],
2173                         link_type);
2174         BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2175                         in_range_threshold, high_threshold);
2176
2177         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2178
2179         proxy = _bt_get_adapter_proxy();
2180         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2181
2182         g_dbus_proxy_call_sync(proxy, "EnableRssi",
2183                                 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2184                                 G_DBUS_CALL_FLAGS_NONE,
2185                                 -1,
2186                                 NULL,
2187                                 &error);
2188         if (error != NULL) {
2189                         BT_ERR("Dbus Call Error:[%s]", error->message);
2190                         g_error_free(error);
2191                         ret = BLUETOOTH_ERROR_INTERNAL;
2192         }
2193         return ret;
2194 }
2195
2196 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2197                                         int link_type)
2198 {
2199         int ret = BLUETOOTH_ERROR_NONE;
2200         GDBusProxy *proxy;
2201         GError *error = NULL;
2202         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2203
2204         BT_CHECK_PARAMETER(bd_addr, return);
2205         BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2206                         bd_addr->addr[0], bd_addr->addr[1],
2207                         bd_addr->addr[2], bd_addr->addr[3],
2208                         bd_addr->addr[4], bd_addr->addr[5],
2209                         link_type);
2210
2211         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2212
2213         proxy = _bt_get_adapter_proxy();
2214         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2215
2216         g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2217                                 g_variant_new("(si)", address, link_type),
2218                                 G_DBUS_CALL_FLAGS_NONE,
2219                                 -1,
2220                                 NULL,
2221                                 &error);
2222
2223         if (error != NULL) {
2224                         BT_ERR("Dbus Call Error:[%s]", error->message);
2225                         g_error_free(error);
2226                         ret = BLUETOOTH_ERROR_INTERNAL;
2227         }
2228         return ret;
2229 }
2230
2231 int _bt_le_conn_update(unsigned char *device_address,
2232                                 guint16 interval_min, guint16 interval_max,
2233                                 guint16 latency, guint16 time_out)
2234 {
2235         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2236         gchar *device_path = NULL;
2237         GError *error = NULL;
2238         GDBusProxy *device_proxy = NULL;
2239         GDBusConnection *conn;
2240         GVariant *reply;
2241         guint32 min, max, to;
2242         guint32 min_supervision_to;
2243         int ret = BLUETOOTH_ERROR_NONE;
2244
2245         BT_DBG("+");
2246
2247         BT_CHECK_PARAMETER(device_address, return);
2248
2249         BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
2250                         interval_min, interval_max, latency, time_out);
2251
2252         if (interval_min > interval_max ||
2253                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2254                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2255                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2256                 goto fail;
2257         }
2258
2259         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2260                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2261                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2262                 goto fail;
2263         }
2264
2265         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2266                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2267                 goto fail;
2268         }
2269
2270         /*
2271          * The Supervision_Timeout in milliseconds shall be larger than
2272          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2273          * where Conn_Interval_Max is given in milliseconds.
2274          */
2275         min_supervision_to = (1 + latency) * interval_max * 2;
2276         if (time_out <= min_supervision_to) {
2277                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2278                 goto fail;
2279         }
2280
2281         _bt_convert_addr_type_to_string(address, device_address);
2282
2283         BT_DBG("Remote device address: %s", address);
2284
2285         device_path = _bt_get_device_object_path(address);
2286
2287         if (device_path == NULL) {
2288                 BT_DBG("device_path NULL");
2289                 ret = BLUETOOTH_ERROR_INTERNAL;
2290                 goto fail;
2291         }
2292
2293         conn = _bt_get_system_gconn();
2294         if (conn == NULL) {
2295                 BT_DBG("conn NULL");
2296                 ret = BLUETOOTH_ERROR_INTERNAL;
2297                 goto fail;
2298         }
2299
2300         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2301                                                                 NULL, BT_BLUEZ_NAME,
2302                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2303
2304         g_free(device_path);
2305         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2306
2307         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2308         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2309         to = time_out / BT_LE_CONN_TO_SPLIT;
2310
2311         reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2312                                 g_variant_new("(uuuu)", min, max, latency, to),
2313                                 G_DBUS_CALL_FLAGS_NONE,
2314                                 -1,
2315                                 NULL,
2316                                 &error);
2317
2318         g_object_unref(device_proxy);
2319         BT_ERR("LeConnUpdate Call Error for %s", address);
2320         if (reply == NULL) {
2321                 if (error) {
2322                         BT_ERR("Error %s[%s]", error->message, address);
2323                         g_error_free(error);
2324                         return BLUETOOTH_ERROR_INTERNAL;
2325                 }
2326         }
2327         g_variant_unref(reply);
2328         BT_DBG("-");
2329
2330 fail:
2331         return ret;
2332 }
2333
2334 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2335                                 bluetooth_device_pin_code_t *pin_code)
2336 {
2337         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2338         GSList *l = NULL;
2339         bt_pin_code_info_t *pin_info = NULL;
2340
2341         BT_CHECK_PARAMETER(device_address, return);
2342         BT_CHECK_PARAMETER(pin_code, return);
2343         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2344                         BLUETOOTH_ERROR_NO_RESOURCES);
2345
2346         _bt_convert_addr_type_to_string(address, device_address->addr);
2347
2348         for (l = pin_info_list; l != NULL; l = l->next) {
2349                 pin_info = l->data;
2350
2351                 if (g_strcmp0(pin_info->address, address) == 0) {
2352                         g_free(pin_info->pin_code);
2353                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2354                         return BLUETOOTH_ERROR_NONE;
2355                 }
2356         }
2357
2358         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2359         if (pin_info != NULL) {
2360                 pin_info->address = g_strdup(address);
2361                 pin_info->pin_code = g_strdup(pin_code->pin_code);
2362                 pin_info_list = g_slist_append(pin_info_list, pin_info);
2363                 return BLUETOOTH_ERROR_NONE;
2364         }
2365
2366         return BLUETOOTH_ERROR_INTERNAL;
2367 }
2368
2369 gint __bt_compare_address(gpointer *a, gpointer *b)
2370 {
2371         bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2372         char *address = (char *)b;
2373         return g_strcmp0(pin_info->address, address);
2374 }
2375
2376 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2377 {
2378         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2379         GSList *l = NULL;
2380         bt_pin_code_info_t *pin_info = NULL;
2381
2382         BT_CHECK_PARAMETER(device_address, return);
2383
2384         _bt_convert_addr_type_to_string(address, device_address->addr);
2385
2386         l = g_slist_find_custom(pin_info_list, address,
2387                 (GCompareFunc)__bt_compare_address);
2388         if (l)
2389                 pin_info = l->data;
2390         if (pin_info) {
2391                 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2392                 g_free(pin_info->address);
2393                 g_free(pin_info->pin_code);
2394                 g_free(pin_info);
2395         }
2396
2397         return BLUETOOTH_ERROR_NONE;
2398 }
2399
2400 int _bt_get_device_pin_code(const char *address, char *pin_code)
2401 {
2402         GSList *l = NULL;
2403
2404         BT_CHECK_PARAMETER(address, return);
2405         BT_CHECK_PARAMETER(pin_code, return);
2406
2407         for (l = pin_info_list; l != NULL; l = l->next) {
2408                 bt_pin_code_info_t *pin_info = l->data;
2409
2410                 if (g_strcmp0(pin_info->address, address) == 0) {
2411                         g_strlcpy(pin_code, pin_info->pin_code,
2412                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2413
2414                         return BLUETOOTH_ERROR_NONE;
2415                 }
2416         }
2417
2418         return BLUETOOTH_ERROR_NOT_FOUND;
2419 }
2420
2421 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2422                 bluetooth_le_connection_param_t *param)
2423 {
2424         if (param == NULL)
2425                 return BLUETOOTH_ERROR_INVALID_PARAM;
2426
2427         if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2428             mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2429                 return BLUETOOTH_ERROR_INVALID_PARAM;
2430
2431         memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2432
2433         switch (mode) {
2434         case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2435                 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2436                 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2437                 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2438                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2439                 break;
2440
2441         case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2442                 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2443                 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2444                 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2445                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2446                 break;
2447
2448         case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2449                 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2450                 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2451                 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2452                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2453                 break;
2454
2455         default:
2456                 BT_ERR("Unhandled mode : %d", mode);
2457                 break;
2458         }
2459
2460         return BLUETOOTH_ERROR_NONE;
2461 }
2462
2463 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
2464 {
2465         GapAgentPrivate *agent = _bt_get_adapter_agent();
2466         retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2467
2468         if (authentication_reply)
2469                 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
2470         else
2471                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
2472
2473         BT_DBG("BT_PASSKEY_REPLY");
2474         return BLUETOOTH_ERROR_NONE;
2475 }
2476
2477 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
2478 {
2479         GapAgentPrivate *agent = _bt_get_adapter_agent();
2480         retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2481
2482         if (confirmation_reply)
2483                 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
2484         else
2485                 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
2486
2487         BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
2488         return BLUETOOTH_ERROR_NONE;
2489 }