Fix the rebond and crash issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <glib.h>
25 #include <gio/gio.h>
26 #include <dlog.h>
27 #include <string.h>
28 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
29 #include <syspopup_caller.h>
30 #endif
31
32 #include "bluetooth-api.h"
33 #include "bt-internal-types.h"
34
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-device.h"
38 #include "bt-service-rfcomm-client.h"
39 #include "bt-service-util.h"
40 #include "bt-service-agent.h"
41 #include "bt-service-network.h"
42 #include "bt-service-adapter.h"
43
44 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
45 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
46 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
47
48 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
49 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
50 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
51 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
52 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
53 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
54 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
55
56 typedef struct {
57         int req_id;
58         int result;
59         char *addr;
60         gboolean is_autopair;
61         GDBusProxy *device_proxy;
62         GDBusProxy *adapter_proxy;
63         void *agent;
64         unsigned short conn_type;
65 } bt_funcion_data_t;
66
67 gboolean is_device_creating;
68 bt_funcion_data_t *bonding_info;
69 bt_funcion_data_t *searching_info;
70
71 /* This HID Mouse does not support pairing precedure. need to skip it. */
72 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
73
74 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
75                                         gpointer user_data);
76
77 static int __bt_retry_bond(void);
78
79
80 /*static void __bt_decline_pair_request()
81 {
82         GVariant *out_param1;
83         GVariant *out_param2;
84         request_info_t *req_info;
85         bluetooth_device_info_t dev_info;
86         bt_remote_dev_info_t *remote_dev_info;
87         GVariant *uuid_list, *manufacture_data;
88         GVariant *param;
89
90         BT_DBG("+");
91         if (bonding_info) {
92                 req_info = _bt_get_request_info(bonding_info->req_id);
93                 if (req_info == NULL) {
94                         BT_ERR("req_info == NULL");
95                         goto done;
96                 }
97                 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
98         } else {
99                 BT_DBG("bonding_info is NULL");
100                 BT_DBG("-");
101                 return;
102         }
103
104         uuid_list =  g_variant_new_from_data((const GVariantType *)"as",
105                                                 remote_dev_info->uuids, remote_dev_info->uuid_count,
106                                                 TRUE, NULL, NULL);
107
108         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
109                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
110                                                 TRUE, NULL, NULL);
111
112         param = g_variant_new("isunsbuba{s}na{y})",
113                         bonding_info->result,
114                         bonding_info->addr,
115                         remote_dev_info->class,
116                         remote_dev_info->rssi,
117                         remote_dev_info->name,
118                         remote_dev_info->paired,
119                         remote_dev_info->connected,
120                         remote_dev_info->trust,
121                         uuid_list,
122                         remote_dev_info->manufacturer_data_len,
123                         manufacture_data);
124
125
126         //Send the event to application
127         if (remote_dev_info != NULL) {
128                 _bt_send_event(BT_ADAPTER_EVENT,
129                         BLUETOOTH_EVENT_BONDING_FINISHED,
130                         param);
131
132                 _bt_free_device_info(remote_dev_info);
133         }
134
135         if (req_info->context == NULL)
136                 goto done;
137
138         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
139         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
140                                         bonding_info->addr);
141
142         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
143                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
144
145         out_param2 = g_variant_new_from_data((const GVariantType *)"i",
146                 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
147
148
149         g_dbus_method_invocation_return_value(req_info->context,
150                         g_variant_new("@ayi", out_param1, out_param2));
151
152
153         _bt_delete_request_list(req_info->req_id);
154 done:
155
156         g_free(bonding_info->addr);
157         g_free(bonding_info);
158         bonding_info = NULL;
159
160         BT_DBG("-");
161 } */
162
163 #ifdef TIZEN_WEARABLE
164 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
165 {
166         int ret;
167         bundle *b;
168         retv_if(user_data == NULL, FALSE);
169
170         b = (bundle *)user_data;
171
172 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
173         ret = syspopup_launch("bt-syspopup", b);
174 #endif
175         if (ret < 0) {
176                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
177         } else {
178                 BT_DBG("Hurray!!! Finally Popup launched");
179                 bundle_free(b);
180         }
181         return (ret < 0) ? TRUE : FALSE;
182 }
183
184 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
185 {
186         BT_DBG("+");
187         int ret = 0;
188         bundle *b = NULL;
189         GDBusConnection *conn;
190
191         conn = _bt_get_system_gconn();
192         if (conn == NULL)
193                 return FALSE;
194
195         b = bundle_create();
196         if (b == NULL)
197                 return FALSE;
198
199         bundle_add(b, "event-type", "unable-to-pairing");
200
201         if (result == BLUETOOTH_ERROR_TIMEOUT )
202                 bundle_add(b, "error", "timeout");
203         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
204                 bundle_add(b, "error", "authfailed");
205         else
206                 bundle_add(b, "error", "error");
207
208 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
209         ret = syspopup_launch("bt-syspopup", b);
210 #endif
211         if (0 > ret) {
212                 BT_ERR("Popup launch failed...retry %d \n", ret);
213                 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
214                                 b);
215         } else {
216                 bundle_free(b);
217         }
218
219         BT_DBG("-");
220         return TRUE;
221 }
222 #endif
223
224 gboolean _bt_is_device_creating(void)
225 {
226         return is_device_creating;
227 }
228
229 gboolean _bt_is_bonding_device_address(const char *address)
230 {
231         if (bonding_info == NULL || bonding_info->addr == NULL)
232                 return FALSE;
233
234         if (g_strcmp0(bonding_info->addr, address) == 0) {
235                 BT_DBG("[%s]  is bonding device", address);
236                 return TRUE;
237         }
238
239         BT_DBG("[%s]  is NOT bonding device", address);
240         return FALSE;
241 }
242
243 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
244 {
245         ret_if(bonding_info == NULL);
246         bonding_info->is_autopair = is_autopair;
247 }
248
249 void _bt_device_path_to_address(const char *device_path,
250                                         char *device_address)
251 {
252         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
253         char *dev_addr;
254         char *pos;
255
256         ret_if(device_path == NULL);
257         ret_if(device_address == NULL);
258
259         dev_addr = strstr(device_path, "dev_");
260         ret_if(dev_addr == NULL);
261
262         dev_addr += 4;
263         g_strlcpy(address, dev_addr, sizeof(address));
264
265         while ((pos = strchr(address, '_')) != NULL) {
266                 *pos = ':';
267         }
268
269         g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
270 }
271
272 void __bt_cancel_search_service_done(void)
273 {
274         int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
275         request_info_t *req_info;
276         bluetooth_device_info_t dev_info;
277         GVariant *out_param1;
278
279         ret_if(searching_info == NULL);
280
281         req_info = _bt_get_request_info(searching_info->req_id);
282         if (req_info == NULL) {
283                 BT_ERR("req_info == NULL");
284                 goto done;
285         }
286
287         if (req_info->context == NULL)
288                 goto done;
289
290         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
291         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
292                                         searching_info->addr);
293
294         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
295                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
296
297
298         g_dbus_method_invocation_return_value(req_info->context,
299                         g_variant_new("(iv)", result, out_param1));
300
301         _bt_delete_request_list(req_info->req_id);
302
303 done:
304
305         g_free(searching_info->addr);
306         g_free(searching_info);
307         searching_info = NULL;
308 }
309
310 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
311 {
312         ret_if(value == NULL);
313         ret_if(info == NULL);
314
315         info->uuid_count = g_variant_get_size(value);
316         info->uuids = g_variant_dup_strv(value, &info->uuid_count);
317 }
318
319 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
320 {
321         bt_remote_dev_info_t *dev_info;
322         char *object_path = NULL;
323         GDBusProxy *adapter_proxy;
324         GDBusProxy *device_proxy;
325         GVariant *value;
326         GVariant *tmp_value;
327         gchar *name;
328         gchar *manufacturer_data = NULL;
329         GDBusConnection *conn;
330         GError *error = NULL;
331         GVariant *result = NULL;
332
333         retv_if(address == NULL, NULL);
334
335         adapter_proxy = _bt_get_adapter_proxy();
336         retv_if(adapter_proxy == NULL, NULL);
337
338
339         object_path = _bt_get_device_object_path(address);
340
341         retv_if(object_path == NULL, NULL);
342
343         conn = _bt_get_system_gconn();
344         if (conn == NULL) {
345                 BT_ERR("conn == NULL");
346                 g_free(object_path);
347                 return NULL;
348         }
349
350         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
351                                                                 NULL, BT_BLUEZ_NAME,
352                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
353
354         g_free(object_path);
355         retv_if(device_proxy == NULL, NULL);
356
357         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
358                                 g_variant_new("(s)", BT_DEVICE_INTERFACE),
359                                 G_DBUS_CALL_FLAGS_NONE,
360                                 -1,
361                                 NULL,
362                                 &error);
363
364         g_object_unref(device_proxy);
365
366         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
367
368         if (result != NULL) {
369                 g_variant_get(result , "(@a{sv})", &value);
370                 g_variant_unref(result);
371
372                 tmp_value = g_variant_lookup_value (value, "Alias", G_VARIANT_TYPE_STRING);
373
374                 g_variant_get(tmp_value, "s", &name);
375                 g_variant_unref(tmp_value);
376                 if (name != NULL)
377                         DBG_SECURE("Alias Name [%s]", name);
378                 else {
379                         tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
380                         g_variant_get(tmp_value, "s", &name);
381                         g_variant_unref(tmp_value);
382                 }
383
384                 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
385                 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
386                 g_variant_unref(tmp_value);
387
388                 tmp_value = g_variant_lookup_value(value, "Connected",  G_VARIANT_TYPE_BOOLEAN);
389                 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
390                 g_variant_unref(tmp_value);
391
392                 tmp_value = g_variant_lookup_value(value, "Trusted",  G_VARIANT_TYPE_BOOLEAN);
393                 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
394                 g_variant_unref(tmp_value);
395
396                 tmp_value = g_variant_lookup_value(value, "Paired",  G_VARIANT_TYPE_BOOLEAN);
397                 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
398                 g_variant_unref(tmp_value);
399
400                 BT_DBG("Paired %d", dev_info->paired );
401
402                 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
403                 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
404                 g_variant_unref(tmp_value);
405
406                 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
407                 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
408                 g_variant_unref(tmp_value);
409
410                 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
411                 __bt_get_uuids(tmp_value, dev_info);
412                 g_variant_unref(tmp_value);
413
414                 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
415                 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
416                 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
417                         BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
418                         dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
419                 }
420                 g_variant_unref(tmp_value);
421
422                 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
423                 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
424                 if (manufacturer_data) {
425                         if (dev_info->manufacturer_data_len > 0) {
426                                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
427                                 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
428                         }
429                 }
430                 g_variant_unref(tmp_value);
431
432                 dev_info->address = g_strdup(address);
433                 dev_info->name = g_strdup(name);
434                 g_free(name);
435                 g_variant_unref(value);
436         } else {
437                 BT_ERR("result  is NULL\n");
438                 g_free(dev_info);
439                 dev_info = NULL;
440         }
441
442         return dev_info;
443 }
444
445 char *_bt_get_bonded_device_name(char *address)
446 {
447         bluetooth_device_address_t device_address = { {0} };
448         bluetooth_device_info_t dev_info;
449
450         retv_if(address == NULL, strdup(""));
451
452         _bt_convert_addr_string_to_type(device_address.addr, address);
453
454         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
455
456         _bt_get_bonded_device_info(&device_address, &dev_info);
457
458         return g_strdup(dev_info.device_name.name);
459 }
460
461 static gboolean __ignore_auto_pairing_request(const char *address)
462 {
463         gchar *buffer;
464         char **lines;
465         int i;
466         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
467         char *temp_buffer;
468         FILE *fp;
469         long size;
470         size_t result;
471
472         BT_DBG("+\n");
473
474         if (address == NULL)
475                 return FALSE;
476
477         /* Get the LAP(Lower Address part) */
478         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
479              a "," */
480         snprintf(lap_address, sizeof(lap_address), ",%s", address);
481
482         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
483
484         if (fp == NULL) {
485                 BT_ERR("fopen failed \n");
486                 return FALSE;
487         }
488
489         fseek(fp, 0, SEEK_END);
490         size = ftell(fp);
491         rewind(fp);
492
493         if (size < 0) {
494                 BT_ERR("Get file size failed \n");
495                 fclose(fp);
496                 return FALSE;
497         }
498
499         buffer = g_malloc0(sizeof(char) * size);
500         /* Fix : NULL_RETURNS */
501         if (buffer == NULL) {
502                 BT_ERR("Memory allocation error\n");
503                 fclose(fp);
504                 return FALSE;
505         }
506         result = fread((char *)buffer, 1, size, fp);
507         fclose(fp);
508         if (result != size) {
509                 BT_ERR("Read Error\n");
510                 g_free(buffer);
511                 return FALSE;
512         }
513
514         BT_DBG("Buffer = %s\n", buffer);
515
516         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
517         g_free(buffer);
518
519         if (lines == NULL)
520                 return FALSE;
521
522         /* Write the data and insert new device data */
523         for (i = 0; lines[i] != NULL; i++) {
524                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
525                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
526                         g_free(lines[i]);
527                         lines[i] = temp_buffer;
528                 }
529         }
530         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
531         g_strfreev(lines);
532         /* Fix : NULL_RETURNS */
533         retv_if(buffer == NULL, FALSE);
534
535         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
536
537         if (fp == NULL) {
538                 BT_ERR("fopen failed \n");
539                 g_free(buffer);
540                 return FALSE;
541         }
542
543         BT_DBG("Buffer = %s\n", buffer);
544         fwrite(buffer, 1, strlen(buffer), fp);
545         fclose(fp);
546
547         g_free(buffer);
548
549         BT_DBG("-\n");
550
551         return FALSE;
552 }
553
554 static int __bt_retry_bond(void)
555 {
556         BT_CHECK_PARAMETER(bonding_info, return);
557         BT_CHECK_PARAMETER(bonding_info->addr, return);
558
559         g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
560                                 g_variant_new("(y)", bonding_info->conn_type),
561                                 G_DBUS_CALL_FLAGS_NONE,
562                                 BT_MAX_DBUS_TIMEOUT,
563                                 NULL,
564                                 (GAsyncReadyCallback)__bt_bond_device_cb,
565                                 NULL);
566
567         return BLUETOOTH_ERROR_NONE;
568 }
569
570
571 static int __bt_remove_and_bond(void)
572 {
573         GDBusProxy *adapter_proxy;
574         GVariant *result = NULL;
575         GError *err = NULL;
576         char *data = NULL;
577         char *device_path = NULL;
578
579         BT_CHECK_PARAMETER(bonding_info, return);
580         BT_CHECK_PARAMETER(bonding_info->addr, return);
581
582         adapter_proxy = _bt_get_adapter_proxy();
583         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
584
585         result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
586                                 g_variant_new("(s)", bonding_info->addr),
587                                 G_DBUS_CALL_FLAGS_NONE,
588                                 -1,
589                                 NULL,
590                                 NULL);
591         if (result == NULL)
592                 return BLUETOOTH_ERROR_INTERNAL;
593
594         g_variant_get(result , "(&o)", &data);
595         device_path = g_strdup(data);
596         g_variant_unref(result);
597
598         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
599
600         result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
601                                 g_variant_new("(o)", device_path),
602                                 G_DBUS_CALL_FLAGS_NONE,
603                                 -1,
604                                 NULL,
605                                 &err);
606
607         g_free(device_path);
608
609         if (result == NULL) {
610                 if (err != NULL) {
611                         BT_ERR("UnpairDevice Fail: %s", err->message);
612                         g_error_free(err);
613                         return BLUETOOTH_ERROR_INTERNAL;
614                 }
615         }
616         g_variant_unref(result);
617
618         return __bt_retry_bond();
619 }
620
621 static int __bt_cancel_and_bond(void)
622 {
623         int ret = BLUETOOTH_ERROR_NONE;
624
625         ret = _bt_agent_reply_cancellation();
626         if (ret != BLUETOOTH_ERROR_NONE){
627                 BT_ERR("Fail to call reply cancellation");
628                 return ret;
629         }
630
631         return __bt_retry_bond();
632 }
633
634
635 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
636                                         gpointer user_data)
637 {
638         int result = BLUETOOTH_ERROR_NONE;
639         GError *err = NULL;
640         GVariant *out_param1;
641         request_info_t *req_info;
642         bluetooth_device_info_t dev_info;
643         bt_remote_dev_info_t *remote_dev_info = NULL;
644         GVariant *manufacture_data;
645         GVariant *param;
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         if (err != NULL) {
671                 BT_ERR("Error occured in CreateBonding [%s]", err->message);
672
673                 if (g_strrstr(err->message, "Already Exists")) {
674                         BT_INFO("Existing Bond, remove and retry");
675                         ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
676
677                         result = BLUETOOTH_ERROR_PARING_FAILED;
678                 } else if (_bt_agent_is_canceled() ||
679                         g_strrstr(err->message, "Authentication Canceled")) {
680                         BT_INFO("Cancelled by USER");
681                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
682                 } else if (g_strrstr(err->message, "Authentication Rejected")) {
683                         BT_INFO("REJECTED");
684                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
685                 } else if (g_strrstr(err->message, "In Progress")) {
686                         BT_INFO("Bond in progress, cancel and retry");
687                         ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
688
689                         result = BLUETOOTH_ERROR_PARING_FAILED;
690                 } else if (g_strrstr(err->message, "Authentication Failed")) {
691                         BT_INFO("Authentication Failed");
692                         if (bonding_info->is_autopair == TRUE) {
693                                 _bt_set_autopair_status_in_bonding_info(FALSE);
694                                 __ignore_auto_pairing_request(bonding_info->addr);
695                         }
696                         result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
697                 } else if (g_strrstr(err->message, "Page Timeout")) {
698                         BT_INFO("Page Timeout");
699                         /* This is the special case
700                              As soon as call bluetooth_bond_device, try to cancel bonding.
701                              In this case, before completing to call 'CreatePairedDevice' method
702                              the procedure is stopped. So 'Cancle' error is not return.
703                         */
704                         result = BLUETOOTH_ERROR_HOST_DOWN;
705                 } else if (g_strrstr(err->message, BT_TIMEOUT_MESSAGE)) {
706                         g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
707                                                 g_variant_new("(s)", bonding_info->addr),
708                                                 G_DBUS_CALL_FLAGS_NONE,
709                                                 -1,
710                                                 NULL,
711                                                 NULL);
712
713                         result = BLUETOOTH_ERROR_INTERNAL;
714                 } else if (g_strrstr(err->message, "Connection Timeout")) {
715                         /* Pairing request timeout */
716                         result = BLUETOOTH_ERROR_TIMEOUT;
717                 } else if (g_strrstr(err->message, "Authentication Timeout")) {
718                         /* Pairing request timeout */
719                         result = BLUETOOTH_ERROR_TIMEOUT;
720                 } else {
721                         BT_DBG("Default case");
722                         result = BLUETOOTH_ERROR_PARING_FAILED;
723                 }
724         }
725
726         if (result == BLUETOOTH_ERROR_PARING_FAILED ||
727                         result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
728                         result == BLUETOOTH_ERROR_TIMEOUT ||
729                         result == BLUETOOTH_ERROR_HOST_DOWN) {
730                 bonding_info->result = result;
731 #ifdef TIZEN_WEARABLE
732                 __bt_launch_unable_to_pairing_syspopup(result);
733 #endif
734         }
735
736         g_object_unref(proxy);
737         bonding_info->device_proxy = NULL;
738
739         if (result != BLUETOOTH_ERROR_NONE)
740                 goto dbus_return;
741
742         remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
743         if (NULL == remote_dev_info) {
744                 BT_ERR("Failed to get remote device info");
745                 goto dbus_return;
746         }
747
748         GVariant *uuids = NULL;
749         GVariantBuilder *builder = NULL;
750         int i = 0;
751         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
752         for (i=0; i < remote_dev_info->uuid_count; i++) {
753                 g_variant_builder_add(builder, "s",
754                         remote_dev_info->uuids[i]);
755         }
756         uuids = g_variant_new("as", builder);
757         g_variant_builder_unref(builder);
758         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
759                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
760                                                 TRUE, NULL, NULL);
761
762         param = g_variant_new("(isunsbub@asn@ay)",
763                         result,
764                         bonding_info->addr,
765                         remote_dev_info->class,
766                         remote_dev_info->rssi,
767                         remote_dev_info->name,
768                         remote_dev_info->paired,
769                         remote_dev_info->connected,
770                         remote_dev_info->trust,
771                         uuids,
772                         remote_dev_info->manufacturer_data_len,
773                         manufacture_data);
774
775
776         /* Send the event to application */
777         _bt_send_event(BT_ADAPTER_EVENT,
778                 BLUETOOTH_EVENT_BONDING_FINISHED,
779                 param);
780
781         _bt_free_device_info(remote_dev_info);
782
783 dbus_return:
784         if (req_info->context == NULL)
785                 goto done;
786
787         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
788         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
789                                         bonding_info->addr);
790
791         if (_bt_adapter_get_status() != BT_ACTIVATED)
792                 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
793
794         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
795                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
796
797         g_dbus_method_invocation_return_value(req_info->context,
798                         g_variant_new("(iv)", result, out_param1));
799
800         g_variant_unref(out_param1);
801 //      g_variant_unref(out_param2);
802
803         _bt_delete_request_list(req_info->req_id);
804 done:
805         if (err)
806                 g_error_free(err);
807
808         _bt_agent_set_canceled(FALSE);
809
810
811         g_free(bonding_info->addr);
812         g_free(bonding_info);
813         bonding_info = NULL;
814 }
815
816 int _bt_bond_device(int request_id,
817                 bluetooth_device_address_t *device_address,
818                 unsigned short conn_type, GArray **out_param1)
819 {
820         GDBusProxy *proxy;
821         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
822         bluetooth_device_info_t dev_info;
823
824         GDBusConnection *conn;
825         char *device_path = NULL;
826         GDBusProxy *adapter_proxy;
827         GError *error = NULL;
828
829         BT_CHECK_PARAMETER(device_address, return);
830
831         if (bonding_info) {
832                 BT_ERR("Bonding in progress");
833
834                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
835                 memcpy(dev_info.device_address.addr, device_address->addr,
836                                 BLUETOOTH_ADDRESS_LENGTH);
837
838                 g_array_append_vals(*out_param1, &dev_info,
839                                 sizeof(bluetooth_device_info_t));
840
841                 return BLUETOOTH_ERROR_DEVICE_BUSY;
842         }
843
844         conn = _bt_get_system_gconn();
845         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
846
847         _bt_convert_addr_type_to_string(address, device_address->addr);
848
849         device_path = _bt_get_device_object_path(address);
850
851         if (device_path == NULL) {
852                 BT_ERR("No searched device");
853                 GVariant *ret = NULL;
854                 adapter_proxy = _bt_get_adapter_proxy();
855                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
856
857                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
858                                         g_variant_new("(s)", address),
859                                         G_DBUS_CALL_FLAGS_NONE,
860                                         -1,
861                                         NULL,
862                                         &error);
863
864                 if (error != NULL) {
865                         BT_ERR("CreateDevice Fail: %s", error->message);
866                         g_clear_error(&error);
867                 }
868                 if (ret)
869                         g_variant_unref(ret);
870                 device_path = _bt_get_device_object_path(address);
871                 if (device_path == NULL) {
872                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
873                         memcpy(dev_info.device_address.addr, device_address->addr,
874                                         BLUETOOTH_ADDRESS_LENGTH);
875
876                         g_array_append_vals(*out_param1, &dev_info,
877                                         sizeof(bluetooth_device_info_t));
878
879                         return BLUETOOTH_ERROR_NOT_PAIRED;
880                 } else {
881                         BT_INFO("device_path is created[%s]", device_path);
882                 }
883         }
884
885         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
886                                                                 NULL, BT_BLUEZ_NAME,
887                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
888
889         g_free(device_path);
890         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
891
892         bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
893         bonding_info->addr = g_strdup(address);
894         bonding_info->req_id = request_id;
895
896         bonding_info->device_proxy = proxy;
897         bonding_info->conn_type = conn_type;
898
899         is_device_creating = TRUE;
900
901         g_dbus_proxy_call(proxy, "Pair",
902                                 g_variant_new("(y)", conn_type),
903                                 G_DBUS_CALL_FLAGS_NONE,
904                                 BT_MAX_DBUS_TIMEOUT,
905                                 NULL,
906                                 (GAsyncReadyCallback)__bt_bond_device_cb,
907                                 NULL);
908
909 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
910
911         return BLUETOOTH_ERROR_NONE;
912 /*fail:
913         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
914         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
915                                         bonding_info->addr);
916
917         g_array_append_vals(*out_param1, &dev_info,
918                                 sizeof(bluetooth_device_info_t));
919
920         is_device_creating = FALSE;
921
922         g_free(bonding_info->addr);
923         g_free(bonding_info);
924         bonding_info = NULL;
925
926         return BLUETOOTH_ERROR_INTERNAL;*/
927 }
928
929 int _bt_cancel_bonding(void)
930 {
931         int ret = BLUETOOTH_ERROR_NONE;
932
933         retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
934
935         ret = _bt_agent_reply_cancellation();
936         if (ret != BLUETOOTH_ERROR_NONE){
937                 BT_ERR("Fail to call reply cancellation");
938                 return ret;
939         }
940
941         _bt_agent_set_canceled(TRUE);
942
943         return BLUETOOTH_ERROR_NONE;
944 }
945
946 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
947                                         gpointer user_data)
948 {
949         GError *err = NULL;
950         GVariant *out_param1;
951         int result = BLUETOOTH_ERROR_NONE;
952         bt_funcion_data_t *unbonding_info;
953         bluetooth_device_info_t dev_info;
954         request_info_t *req_info;
955
956         g_dbus_proxy_call_finish(proxy, res, &err);
957
958         unbonding_info = user_data;
959
960         if (unbonding_info == NULL) {
961                 /* Send reply */
962                 BT_ERR("unbonding_info == NULL");
963                 goto done;
964         }
965
966         req_info = _bt_get_request_info(unbonding_info->req_id);
967         if (req_info == NULL) {
968                 BT_ERR("req_info == NULL");
969                 goto done;
970         }
971
972         if (err != NULL) {
973                 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
974                 result = BLUETOOTH_ERROR_INTERNAL;
975         }
976
977         if (req_info->context == NULL)
978                 goto done;
979
980         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
981         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
982                                         unbonding_info->addr);
983
984         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
985                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
986
987         g_dbus_method_invocation_return_value(req_info->context,
988                         g_variant_new("(iv)", result, out_param1));
989
990
991         _bt_delete_request_list(req_info->req_id);
992
993 done:
994         if (err)
995                 g_error_free(err);
996
997         if (unbonding_info) {
998                 g_free(unbonding_info->addr);
999                 g_free(unbonding_info);
1000         }
1001 }
1002
1003 int _bt_unbond_device(int request_id,
1004                         bluetooth_device_address_t *device_address,
1005                         GArray **out_param1)
1006 {
1007         char *device_path = NULL;
1008         bt_funcion_data_t *unbonding_info;
1009         GDBusProxy *adapter_proxy = NULL;
1010         GDBusProxy *device_proxy = NULL;
1011         GDBusConnection *conn;
1012         int result = BLUETOOTH_ERROR_INTERNAL;
1013         bluetooth_device_info_t dev_info;
1014         GError *error = NULL;
1015         GVariant *ret = NULL;
1016
1017         BT_CHECK_PARAMETER(device_address, return);
1018
1019         adapter_proxy = _bt_get_adapter_proxy();
1020         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1021
1022         /* allocate user data so that it can be retrieved in callback */
1023         unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1024         /* Fix : NULL_RETURNS */
1025         if (unbonding_info == NULL) {
1026                 BT_ERR("Memory not allocated !");
1027                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1028         }
1029
1030         unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1031         unbonding_info->req_id = request_id;
1032
1033         _bt_convert_addr_type_to_string(unbonding_info->addr,
1034                                         device_address->addr);
1035
1036         device_path = _bt_get_device_object_path(unbonding_info->addr);
1037
1038         if (device_path == NULL) {
1039                 BT_ERR("No paired device");
1040                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1041                 goto fail;
1042         }
1043
1044         conn = _bt_get_system_gconn();
1045         if (conn == NULL) {
1046                 BT_ERR("conn is NULL");
1047                 result = BLUETOOTH_ERROR_INTERNAL;
1048                 goto fail;
1049         }
1050
1051         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1052                                                                 NULL, BT_BLUEZ_NAME,
1053                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1054
1055         if (device_proxy != NULL) {
1056
1057                 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1058                                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1059                                         G_DBUS_CALL_FLAGS_NONE,
1060                                         -1,
1061                                         NULL,
1062                                         &error);
1063                 if (error) {
1064                         BT_ERR("Getting property failed: [%s]\n", error->message);
1065                         g_error_free(error);
1066                         result = BLUETOOTH_ERROR_NOT_PAIRED;
1067                         goto fail;
1068                 } else {
1069                         if (!ret) {
1070                                 BT_ERR("No paired device");
1071                                 g_object_unref(device_proxy);
1072                                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1073                                 goto fail;
1074                         }
1075                         g_variant_unref(ret);
1076                 }
1077                 g_object_unref(device_proxy);
1078         }
1079
1080         g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1081                                 g_variant_new("(o)", device_path),
1082                                 G_DBUS_CALL_FLAGS_NONE,
1083                                 BT_MAX_DBUS_TIMEOUT,
1084                                 NULL,
1085                                 (GAsyncReadyCallback)__bt_unbond_cb,
1086                                 NULL);
1087
1088         g_free(device_path);
1089         return BLUETOOTH_ERROR_NONE;
1090
1091 fail:
1092         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1093         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1094                                         unbonding_info->addr);
1095
1096         g_array_append_vals(*out_param1, &dev_info,
1097                                 sizeof(bluetooth_device_info_t));
1098         g_free(device_path);
1099         g_free(unbonding_info->addr);
1100         g_free(unbonding_info);
1101         return result;
1102 }
1103
1104 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1105                                         gpointer user_data)
1106 {
1107         GError *err = NULL;
1108         GVariant *out_param1;
1109         int result = BLUETOOTH_ERROR_NONE;
1110         bluetooth_device_info_t dev_info;
1111         bt_remote_dev_info_t *remote_dev_info = NULL;
1112         request_info_t *req_info;
1113         GVariant *uuid_list, *manufacture_data;
1114         GVariant *param;
1115         GVariantBuilder *builder = NULL;
1116         int i = 0;
1117
1118         g_dbus_proxy_call_finish(proxy, res, &err);
1119
1120         g_object_unref(proxy);
1121
1122         if (searching_info == NULL) {
1123                 /* Send reply */
1124                 BT_ERR("unbonding_info == NULL");
1125                 goto done;
1126         }
1127
1128         req_info = _bt_get_request_info(searching_info->req_id);
1129         if (req_info == NULL) {
1130                 BT_ERR("req_info == NULL");
1131                 goto done;
1132         }
1133
1134         if (err != NULL) {
1135                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1136
1137                 if (!strcmp("Operation canceled", err->message)) {
1138                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1139                 } else if (!strcmp("In Progress", err->message)) {
1140                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1141                 } else if (!strcmp("Host is down", err->message)) {
1142                         result = BLUETOOTH_ERROR_HOST_DOWN;
1143                 } else {
1144                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1145                 }
1146
1147                 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1148                      result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1149                         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1150                         if (remote_dev_info && remote_dev_info->uuids != NULL &&
1151                              remote_dev_info->uuid_count > 0) {
1152                                 result = BLUETOOTH_ERROR_NONE;
1153                                 goto event;
1154                         }
1155                         _bt_free_device_info(remote_dev_info);
1156                 }
1157                 goto dbus_return;
1158         }
1159
1160         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1161         if (NULL == remote_dev_info) {
1162                 BT_ERR("Failed to get remote device info");
1163                 goto dbus_return;
1164         }
1165
1166 event:
1167         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1168         for (i=0; i < remote_dev_info->uuid_count; i++) {
1169                 g_variant_builder_add(builder, "s",
1170                         remote_dev_info->uuids[i]);
1171         }
1172         uuid_list = g_variant_new("as", builder);
1173         g_variant_builder_unref(builder);
1174         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1175                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1176                                                 TRUE, NULL, NULL);
1177
1178         param = g_variant_new("(isunsbub@asn@ay)",
1179                         result,
1180                         searching_info->addr,
1181                         remote_dev_info->class,
1182                         remote_dev_info->rssi,
1183                         remote_dev_info->name,
1184                         remote_dev_info->paired,
1185                         remote_dev_info->connected,
1186                         remote_dev_info->trust,
1187                         uuid_list,
1188                         remote_dev_info->manufacturer_data_len,
1189                         manufacture_data);
1190
1191
1192         /* Send the event to application */
1193         _bt_send_event(BT_ADAPTER_EVENT,
1194                 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1195                 param);
1196
1197         _bt_free_device_info(remote_dev_info);
1198
1199 dbus_return:
1200         if (req_info->context == NULL)
1201                 goto done;
1202
1203         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1204         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1205                                         searching_info->addr);
1206
1207         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1208                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1209
1210         g_dbus_method_invocation_return_value(req_info->context,
1211                         g_variant_new("(iv)", result, out_param1));
1212
1213
1214         _bt_delete_request_list(req_info->req_id);
1215 done:
1216         if (err)
1217                 g_error_free(err);
1218
1219         if (searching_info) {
1220                 g_free(searching_info->addr);
1221                 g_free(searching_info);
1222                 searching_info = NULL;
1223         }
1224 }
1225
1226 int _bt_search_device(int request_id,
1227                         bluetooth_device_address_t *device_address)
1228 {
1229         char *device_path = NULL;
1230         GDBusProxy *device_proxy = NULL;
1231         GDBusConnection *conn;
1232
1233         GDBusProxy *adapter_proxy;
1234         int result = BLUETOOTH_ERROR_INTERNAL;
1235
1236         BT_CHECK_PARAMETER(device_address, return);
1237
1238         if (bonding_info) {
1239                 BT_ERR("Bonding in progress");
1240                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1241         }
1242
1243         if (searching_info) {
1244                 BT_ERR("Service searching in progress");
1245                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1246         }
1247
1248         adapter_proxy = _bt_get_adapter_proxy();
1249         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1250
1251         /* allocate user data so that it can be retrieved in callback */
1252         searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1253         searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1254         searching_info->req_id = request_id;
1255
1256         _bt_convert_addr_type_to_string(searching_info->addr,
1257                                         device_address->addr);
1258
1259         conn = _bt_get_system_gconn();
1260         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1261
1262
1263         device_path = _bt_get_device_object_path(searching_info->addr);
1264
1265         if (device_path == NULL) {
1266                 BT_ERR("No paired device");
1267                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1268                 goto fail;
1269         }
1270
1271         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1272                                                                 NULL, BT_BLUEZ_NAME,
1273                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1274         g_free(device_path);
1275         if (device_proxy == NULL) {
1276                 result = BLUETOOTH_ERROR_INTERNAL;
1277                 goto fail;
1278         }
1279
1280         g_dbus_proxy_call(device_proxy, "DiscoverServices",
1281                         g_variant_new("(s)", ""),
1282                         G_DBUS_CALL_FLAGS_NONE,
1283                         BT_MAX_DBUS_TIMEOUT,
1284                         NULL,
1285                         (GAsyncReadyCallback)__bt_discover_cb,
1286                         searching_info);
1287
1288         searching_info->device_proxy = device_proxy;
1289
1290         return BLUETOOTH_ERROR_NONE;
1291 fail:
1292         if (device_proxy)
1293                 g_object_unref(device_proxy);
1294
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                                 NULL,
1478                                 G_DBUS_CALL_FLAGS_NONE,
1479                                 -1,
1480                                 NULL,
1481                                 &error);
1482
1483         if (error != NULL || result == NULL) {
1484                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1485                 g_error_free(error);
1486                 g_object_unref(device_proxy);
1487                 return BLUETOOTH_ERROR_INTERNAL;
1488         }
1489
1490         g_variant_get(result , "(@a{sv})", &value);
1491         g_variant_unref(result);
1492
1493         tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1494         if (tmp_value != NULL) {
1495                 g_object_unref(device_proxy);
1496                 return BLUETOOTH_ERROR_INTERNAL;
1497         }
1498         *is_connected = g_variant_get_boolean (tmp_value);
1499
1500         BT_DBG("gatt is connected : %d", *is_connected);
1501         g_variant_unref(tmp_value);
1502         g_variant_unref(value);
1503         g_object_unref(device_proxy);
1504
1505         return ret;
1506 }
1507
1508 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1509                         int connection_type, gboolean *is_connected)
1510 {
1511         char *object_path = NULL;
1512         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1513         GDBusProxy *device_proxy = NULL;
1514         GDBusProxy *adapter_proxy = NULL;
1515         GDBusConnection *conn;
1516         GError *error = NULL;
1517         GVariant *tmp_value = NULL;
1518         GVariant *value = NULL;
1519         GVariant *result = NULL;
1520         char *uuid = NULL;
1521
1522         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1523         retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1524
1525         *is_connected = FALSE;
1526         BT_DBG("connection_type: %d", connection_type);
1527         if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1528                 return _bt_rfcomm_is_device_connected(device_address,
1529                                                 is_connected);
1530         else if (connection_type == BLUETOOTH_GATT_SERVICE)
1531                 return _bt_is_gatt_connected(device_address, is_connected);
1532
1533         adapter_proxy = _bt_get_adapter_proxy();
1534         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1535
1536         conn = _bt_get_system_gconn();
1537         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1538
1539         _bt_convert_addr_type_to_string(address, device_address->addr);
1540
1541         if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE)     {
1542                 object_path = _bt_get_adapter_path();
1543                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1544                                                                         NULL, BT_BLUEZ_NAME,
1545                                                                         object_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
1546                 g_free(object_path);
1547                 if (device_proxy == NULL) {
1548                         BT_DBG("Device don't have this service");
1549                         return BLUETOOTH_ERROR_INTERNAL;
1550                 }
1551
1552                 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1553                                         g_variant_new("(s)", address),
1554                                         G_DBUS_CALL_FLAGS_NONE,
1555                                         -1,
1556                                         NULL,
1557                                         &error);
1558
1559                 if (result == NULL) {
1560                         BT_ERR("Error occured in Proxy call");
1561                         if (error) {
1562                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1563                                 g_error_free(error);
1564                         }
1565                         g_object_unref(device_proxy);
1566                         return BLUETOOTH_ERROR_INTERNAL;
1567                 }
1568                 g_variant_get(result , "(@a{sv})", &value);
1569                 g_variant_unref(result);
1570
1571
1572                 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1573                 if (tmp_value != NULL) {
1574                         g_object_unref(device_proxy);
1575                         return BLUETOOTH_ERROR_INTERNAL;
1576                 }
1577                 *is_connected = g_variant_get_boolean (tmp_value);
1578                 g_variant_unref(tmp_value);
1579                 g_variant_unref(value);
1580         } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1581                 return _bt_is_network_connected(_bt_get_net_conn(),
1582                                                 device_address->addr, is_connected);
1583         } else {
1584                 uuid = _bt_get_profile_uuid128(connection_type);
1585                 if (uuid == NULL) {
1586                         BT_ERR("uuid is NULL");
1587                         return BLUETOOTH_ERROR_INTERNAL;
1588                 }
1589
1590                 BT_DBG("uuid: %s", uuid);
1591
1592                 object_path = _bt_get_device_object_path(address);
1593                 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1594                 BT_DBG("object_path: %s", object_path);
1595                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1596                                                                         NULL, BT_BLUEZ_NAME,
1597                                                                         object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1598                 g_free(object_path);
1599                 if (device_proxy == NULL) {
1600                         BT_DBG("Device don't have this service");
1601                         g_free(uuid);
1602                         return BLUETOOTH_ERROR_INTERNAL;
1603                 }
1604
1605                 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1606                                         g_variant_new("(s)", uuid),
1607                                         G_DBUS_CALL_FLAGS_NONE,
1608                                         -1,
1609                                         NULL,
1610                                         &error);
1611
1612                 if (result == NULL) {
1613                         BT_ERR("Error occured in Proxy call");
1614                         if (error) {
1615                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1616                                 g_error_free(error);
1617                         }
1618                         g_object_unref(device_proxy);
1619                         return BLUETOOTH_ERROR_INTERNAL;
1620                 }
1621                 g_variant_get(result, "(b)", is_connected);
1622                 g_free(uuid);
1623                 g_variant_unref(result);
1624         }
1625
1626         g_object_unref(device_proxy);
1627         return BLUETOOTH_ERROR_NONE;
1628 }
1629
1630 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1631                         bluetooth_connected_link_t *connected)
1632 {
1633         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1634         char *object_path = NULL;
1635
1636         GDBusProxy *device_proxy;
1637         GError *error = NULL;
1638         GDBusConnection *conn;
1639         GVariant *tmp_value = NULL;
1640         GVariant *value = NULL;
1641         GVariant *result = NULL;
1642
1643         int ret = BLUETOOTH_ERROR_NONE;
1644
1645         BT_CHECK_PARAMETER(device_address, return);
1646
1647         conn = _bt_get_system_gconn();
1648         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1649
1650         _bt_convert_addr_type_to_string(address, device_address->addr);
1651
1652         object_path = _bt_get_device_object_path(address);
1653         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1654
1655         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1656                                                                 NULL, BT_BLUEZ_NAME,
1657                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1658         g_free(object_path);
1659         if (device_proxy == NULL) {
1660                 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1661                 return BLUETOOTH_ERROR_NONE;
1662         }
1663
1664         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1665                                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1666                                         G_DBUS_CALL_FLAGS_NONE,
1667                                         -1,
1668                                         NULL,
1669                                         &error);
1670
1671         if (error != NULL) {
1672                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1673                 g_error_free(error);
1674                 g_object_unref(device_proxy);
1675                 return BLUETOOTH_ERROR_INTERNAL;
1676         }
1677
1678         g_variant_get(result , "(@a{sv})", &value);
1679         g_variant_unref(result);
1680
1681         tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1682         if (tmp_value != NULL) {
1683                 g_object_unref(device_proxy);
1684                 g_variant_unref(value);
1685                 return BLUETOOTH_ERROR_INTERNAL;
1686         }
1687         *connected = g_variant_get_boolean (tmp_value);
1688         g_variant_unref(tmp_value);
1689         g_variant_unref(value);
1690         g_object_unref(device_proxy);
1691
1692         return ret;
1693 }
1694
1695 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1696 {
1697         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1698         gchar *device_path = NULL;
1699         GError *error = NULL;
1700         GDBusProxy *device_proxy = NULL;
1701         GDBusProxy *adapter_proxy;
1702         GDBusConnection *conn;
1703         int ret = BLUETOOTH_ERROR_NONE;
1704
1705         BT_CHECK_PARAMETER(bd_addr, return);
1706
1707         _bt_convert_addr_type_to_string(device_address,
1708                         (unsigned char *)bd_addr->addr);
1709
1710         conn = _bt_get_system_gconn();
1711         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1712
1713         adapter_proxy = _bt_get_adapter_proxy();
1714         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1715
1716         device_path = _bt_get_device_object_path(device_address);
1717         if (device_path == NULL) {
1718                 BT_DBG("device_path NULL");
1719                 ret = BLUETOOTH_ERROR_INTERNAL;
1720                 return ret;
1721         }
1722
1723         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1724
1725         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1726                                                                 NULL, BT_BLUEZ_NAME,
1727                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1728         g_free(device_path);
1729         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1730
1731         g_dbus_proxy_call_sync(device_proxy, "ConnectLE",
1732                                  g_variant_new("(b)", auto_connect),
1733                                  G_DBUS_CALL_FLAGS_NONE,
1734                                  -1,
1735                                  NULL,
1736                                  &error);
1737
1738         if (error) {
1739                 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1740                 g_error_free(error);
1741                 g_object_unref(device_proxy);
1742                 return BLUETOOTH_ERROR_INTERNAL;
1743         }
1744
1745         g_object_unref(device_proxy);
1746
1747         return ret;
1748 }
1749
1750 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1751 {
1752         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1753         gchar *device_path = NULL;
1754         GError *error = NULL;
1755         GDBusProxy *device_proxy = NULL;
1756         GDBusProxy *adapter_proxy;
1757         GDBusConnection *conn;
1758         int ret = BLUETOOTH_ERROR_NONE;
1759
1760         BT_CHECK_PARAMETER(bd_addr, return);
1761
1762         _bt_convert_addr_type_to_string(device_address,
1763                         (unsigned char *)bd_addr->addr);
1764
1765         conn = _bt_get_system_gconn();
1766         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1767
1768         adapter_proxy = _bt_get_adapter_proxy();
1769         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1770
1771         device_path = _bt_get_device_object_path(device_address);
1772         if (device_path == NULL) {
1773                 BT_DBG("device_path NULL");
1774                 ret = BLUETOOTH_ERROR_INTERNAL;
1775                 return ret;
1776         }
1777
1778         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1779
1780         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1781                                                                 NULL, BT_BLUEZ_NAME,
1782                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1783         g_free(device_path);
1784         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1785
1786         g_dbus_proxy_call_sync(device_proxy, "DisconnectLE",
1787                                 NULL,
1788                                 G_DBUS_CALL_FLAGS_NONE,
1789                                 -1,
1790                                 NULL,
1791                                 &error);
1792         if (error) {
1793                 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1794                 g_error_free(error);
1795                 g_object_unref(device_proxy);
1796                 return BLUETOOTH_ERROR_INTERNAL;
1797         }
1798
1799         g_object_unref(device_proxy);
1800
1801         return ret;
1802 }
1803
1804 int _bt_connect_profile(char *address, char *uuid,
1805                                                 void *cb, gpointer func_data)
1806 {
1807         char *object_path;
1808         GDBusProxy *proxy;
1809         GDBusConnection *conn;
1810         GDBusProxy *adapter_proxy;
1811         GError *error = NULL;
1812
1813         conn = _bt_get_system_gconn();
1814         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1815
1816         object_path = _bt_get_device_object_path(address);
1817         if (object_path == NULL) {
1818                 BT_ERR("No searched device");
1819
1820                 adapter_proxy = _bt_get_adapter_proxy();
1821                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1822
1823                 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1824                                          g_variant_new("(s)", address),
1825                                          G_DBUS_CALL_FLAGS_NONE,
1826                                          -1,
1827                                          NULL,
1828                                          &error);
1829
1830                 if (error != NULL) {
1831                         BT_ERR("CreateDevice Fail: %s", error->message);
1832                         g_error_free(error);
1833                 }
1834
1835                 object_path = _bt_get_device_object_path(address);
1836         }
1837         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1838
1839         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1840                                                                 NULL, BT_BLUEZ_NAME,
1841                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1842         g_free(object_path);
1843         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1844
1845         g_dbus_proxy_call(proxy, "ConnectProfile",
1846                                 g_variant_new("(s)", uuid),
1847                                 G_DBUS_CALL_FLAGS_NONE,
1848                                 BT_MAX_DBUS_TIMEOUT,
1849                                 NULL,
1850                                 (GAsyncReadyCallback)cb,
1851                                 func_data);
1852
1853         return BLUETOOTH_ERROR_NONE;
1854 }
1855
1856 int _bt_disconnect_profile(char *address, char *uuid,
1857                                                 void *cb, gpointer func_data)
1858 {
1859         char *object_path;
1860         GDBusProxy *proxy;
1861         GDBusConnection *conn;
1862
1863         conn = _bt_get_system_gconn();
1864         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1865
1866         object_path = _bt_get_device_object_path(address);
1867         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1868
1869         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1870                                                                 NULL, BT_BLUEZ_NAME,
1871                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1872         g_free(object_path);
1873         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1874
1875         g_dbus_proxy_call(proxy, "DisconnectProfile",
1876                                         g_variant_new("(s)", uuid),
1877                                         G_DBUS_CALL_FLAGS_NONE,
1878                                         BT_MAX_DBUS_TIMEOUT,
1879                                         NULL,
1880                                         (GAsyncReadyCallback)cb,
1881                                         func_data);
1882
1883         return BLUETOOTH_ERROR_NONE;
1884 }
1885
1886 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1887                 int low_threshold, int in_range_threshold, int high_threshold)
1888 {
1889         int ret = BLUETOOTH_ERROR_NONE;
1890         GDBusProxy *proxy;
1891         GError *error = NULL;
1892         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1893
1894         BT_CHECK_PARAMETER(bd_addr, return);
1895         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1896                         bd_addr->addr[0], bd_addr->addr[1],
1897                         bd_addr->addr[2], bd_addr->addr[3],
1898                         bd_addr->addr[4], bd_addr->addr[5],
1899                         link_type);
1900         BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1901                         in_range_threshold, high_threshold);
1902
1903         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1904
1905         proxy = _bt_get_adapter_proxy();
1906         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1907
1908         g_dbus_proxy_call_sync(proxy, "EnableRssi",
1909                                 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
1910                                 G_DBUS_CALL_FLAGS_NONE,
1911                                 -1,
1912                                 NULL,
1913                                 &error);
1914         if (error != NULL) {
1915                         BT_ERR("Dbus Call Error:[%s]", error->message);
1916                         g_error_free(error);
1917                         ret = BLUETOOTH_ERROR_INTERNAL;
1918         }
1919         return ret;
1920 }
1921
1922 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1923                                         int link_type)
1924 {
1925         int ret = BLUETOOTH_ERROR_NONE;
1926         GDBusProxy *proxy;
1927         GError *error = NULL;
1928         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1929
1930         BT_CHECK_PARAMETER(bd_addr, return);
1931         BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1932                         bd_addr->addr[0], bd_addr->addr[1],
1933                         bd_addr->addr[2], bd_addr->addr[3],
1934                         bd_addr->addr[4], bd_addr->addr[5],
1935                         link_type);
1936
1937         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1938
1939         proxy = _bt_get_adapter_proxy();
1940         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1941
1942         g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
1943                                 g_variant_new("(si)", address, link_type),
1944                                 G_DBUS_CALL_FLAGS_NONE,
1945                                 -1,
1946                                 NULL,
1947                                 &error);
1948
1949         if (error != NULL) {
1950                         BT_ERR("Dbus Call Error:[%s]", error->message);
1951                         g_error_free(error);
1952                         ret = BLUETOOTH_ERROR_INTERNAL;
1953         }
1954         return ret;
1955 }
1956
1957 int _bt_le_conn_update(unsigned char *device_address,
1958                                 guint16 interval_min, guint16 interval_max,
1959                                 guint16 latency, guint16 time_out)
1960 {
1961         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1962         gchar *device_path = NULL;
1963         GError *error = NULL;
1964         GDBusProxy *device_proxy = NULL;
1965         GDBusConnection *conn;
1966         GVariant *reply;
1967         guint32 min, max, to;
1968         guint32 min_supervision_to;
1969         int ret = BLUETOOTH_ERROR_NONE;
1970
1971         BT_DBG("+");
1972
1973         BT_CHECK_PARAMETER(device_address, return);
1974
1975         BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1976                         interval_min, interval_max, latency, time_out);
1977
1978         if (interval_min > interval_max ||
1979                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
1980                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
1981                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1982                 goto fail;
1983         }
1984
1985         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1986                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
1987                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1988                 goto fail;
1989         }
1990
1991         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
1992                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1993                 goto fail;
1994         }
1995
1996         /*
1997          * The Supervision_Timeout in milliseconds shall be larger than
1998          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
1999          * where Conn_Interval_Max is given in milliseconds.
2000          */
2001         min_supervision_to = (1 + latency) * interval_max * 2;
2002         if (time_out <= min_supervision_to) {
2003                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2004                 goto fail;
2005         }
2006
2007         _bt_convert_addr_type_to_string(address, device_address);
2008
2009         BT_DBG("Remote device address: %s", address);
2010
2011         device_path = _bt_get_device_object_path(address);
2012
2013         if (device_path == NULL) {
2014                 BT_DBG("device_path NULL");
2015                 ret = BLUETOOTH_ERROR_INTERNAL;
2016                 goto fail;
2017         }
2018
2019         conn = _bt_get_system_gconn();
2020         if (conn == NULL) {
2021                 BT_DBG("conn NULL");
2022                 ret = BLUETOOTH_ERROR_INTERNAL;
2023                 goto fail;
2024         }
2025
2026         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2027                                                                 NULL, BT_BLUEZ_NAME,
2028                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2029
2030         g_free(device_path);
2031         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2032
2033         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2034         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2035         to = time_out / BT_LE_CONN_TO_SPLIT;
2036
2037         reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2038                                 g_variant_new("(uuuu)", min, max, latency, to),
2039                                 G_DBUS_CALL_FLAGS_NONE,
2040                                 -1,
2041                                 NULL,
2042                                 &error);
2043
2044         g_object_unref(device_proxy);
2045         BT_ERR("LeConnUpdate Call Error for %s", address);
2046         if (reply == NULL) {
2047                 if (error) {
2048                         BT_ERR("Error %s[%s]", error->message, address);
2049                         g_error_free(error);
2050                         return BLUETOOTH_ERROR_INTERNAL;
2051                 }
2052         }
2053         g_variant_unref(reply);
2054         BT_DBG("-");
2055
2056 fail:
2057         return ret;
2058 }