94cd20aa87adf8b95b624968bc0fdd35122b9566
[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 <dbus/dbus-glib.h>
25 #include <dbus/dbus.h>
26 #include <glib.h>
27 #include <dlog.h>
28 #include <string.h>
29 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
30 #include <syspopup_caller.h>
31 #endif
32
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
35
36 #include "bt-service-common.h"
37 #include "bt-service-event.h"
38 #include "bt-service-device.h"
39 #include "bt-service-rfcomm-client.h"
40 #include "bt-service-util.h"
41 #include "bt-service-agent.h"
42 #include "bt-service-network.h"
43 #include "bt-service-adapter.h"
44
45 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
46 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
47 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
48
49 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
50 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
51 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
52 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
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         DBusGProxy *device_proxy;
62         DBusGProxy *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(DBusGProxy *proxy, DBusGProxyCall *call,
75                                                      gpointer user_data);
76
77 static int __bt_retry_bond(void);
78
79
80 static void __bt_decline_pair_request()
81 {
82         GArray *out_param1;
83         GArray *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
88         BT_DBG("+");
89         if (bonding_info) {
90                 req_info = _bt_get_request_info(bonding_info->req_id);
91                 if (req_info == NULL) {
92                         BT_ERR("req_info == NULL");
93                         goto done;
94                 }
95                 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
96         } else {
97                 BT_DBG("bonding_info is NULL");
98                 BT_DBG("-");
99                 return;
100         }
101
102
103
104         /* Send the event to application */
105         if (remote_dev_info != NULL) {
106                 _bt_send_event(BT_ADAPTER_EVENT,
107                         BLUETOOTH_EVENT_BONDING_FINISHED,
108                         DBUS_TYPE_INT32, &bonding_info->result,
109                         DBUS_TYPE_STRING, &bonding_info->addr,
110                         DBUS_TYPE_UINT32, &remote_dev_info->class,
111                         DBUS_TYPE_INT16, &remote_dev_info->rssi,
112                         DBUS_TYPE_STRING, &remote_dev_info->name,
113                         DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
114                         DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
115                         DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
116                         DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
117                         &remote_dev_info->uuids, remote_dev_info->uuid_count,
118                         DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
119                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
120                         &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
121                         DBUS_TYPE_INVALID);
122
123                 _bt_free_device_info(remote_dev_info);
124         }
125
126         if (req_info->context == NULL)
127                 goto done;
128
129         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
130         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
131
132         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
133         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
134                                         bonding_info->addr);
135
136         g_array_append_vals(out_param1, &dev_info,
137                                 sizeof(bluetooth_device_info_t));
138         g_array_append_vals(out_param2, &bonding_info->result, sizeof(int));
139
140         dbus_g_method_return(req_info->context, out_param1, out_param2);
141
142         g_array_free(out_param1, TRUE);
143         g_array_free(out_param2, TRUE);
144
145         _bt_delete_request_list(req_info->req_id);
146 done:
147
148         g_free(bonding_info->addr);
149         g_free(bonding_info);
150         bonding_info = NULL;
151
152         BT_DBG("-");
153 }
154
155 #ifdef TIZEN_WEARABLE
156 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
157 {
158         int ret;
159         bundle *b;
160         retv_if(user_data == NULL, FALSE);
161
162         b = (bundle *)user_data;
163
164 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
165         ret = syspopup_launch("bt-syspopup", b);
166 #endif
167         if (ret < 0) {
168                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
169         } else {
170                 BT_DBG("Hurray!!! Finally Popup launched");
171                 bundle_free(b);
172         }
173         return (ret < 0) ? TRUE : FALSE;
174 }
175
176 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
177 {
178         BT_DBG("+");
179         int ret = 0;
180         bundle *b = NULL;
181         DBusGConnection *conn;
182
183         conn = _bt_get_system_gconn();
184         if (conn == NULL)
185                 return FALSE;
186
187         b = bundle_create();
188         if (b == NULL)
189                 return FALSE;
190
191         bundle_add(b, "event-type", "unable-to-pairing");
192
193         if (result == BLUETOOTH_ERROR_TIMEOUT )
194                 bundle_add(b, "error", "timeout");
195         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
196                 bundle_add(b, "error", "authfailed");
197         else
198                 bundle_add(b, "error", "error");
199
200 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
201         ret = syspopup_launch("bt-syspopup", b);
202 #endif
203         if (0 > ret) {
204                 BT_ERR("Popup launch failed...retry %d \n", ret);
205                 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
206                                 b);
207         } else {
208                 bundle_free(b);
209         }
210
211         BT_DBG("-");
212         return TRUE;
213 }
214 #endif
215
216 gboolean _bt_is_device_creating(void)
217 {
218         return is_device_creating;
219 }
220
221 gboolean _bt_is_bonding_device_address(const char *address)
222 {
223         if (bonding_info == NULL || bonding_info->addr == NULL)
224                 return FALSE;
225
226         if (g_strcmp0(bonding_info->addr, address) == 0) {
227                 BT_DBG("[%s]  is bonding device", address);
228                 return TRUE;
229         }
230
231         BT_DBG("[%s]  is NOT bonding device", address);
232         return FALSE;
233 }
234
235 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
236 {
237         ret_if(bonding_info == NULL);
238         bonding_info->is_autopair = is_autopair;
239 }
240
241 void _bt_device_path_to_address(const char *device_path,
242                                         char *device_address)
243 {
244         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
245         char *dev_addr;
246         char *pos;
247
248         ret_if(device_path == NULL);
249         ret_if(device_address == NULL);
250
251         dev_addr = strstr(device_path, "dev_");
252         ret_if(dev_addr == NULL);
253
254         dev_addr += 4;
255         g_strlcpy(address, dev_addr, sizeof(address));
256
257         while ((pos = strchr(address, '_')) != NULL) {
258                 *pos = ':';
259         }
260
261         g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
262 }
263
264 void __bt_cancel_search_service_done(void)
265 {
266         int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
267         request_info_t *req_info;
268         bluetooth_device_info_t dev_info;
269         GArray *out_param1;
270         GArray *out_param2;
271
272         ret_if(searching_info == NULL);
273
274         req_info = _bt_get_request_info(searching_info->req_id);
275         if (req_info == NULL) {
276                 BT_ERR("req_info == NULL");
277                 goto done;
278         }
279
280         if (req_info->context == NULL)
281                 goto done;
282
283         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
284         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
285
286         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
287         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
288                                         searching_info->addr);
289
290         g_array_append_vals(out_param1, &dev_info,
291                                 sizeof(bluetooth_device_info_t));
292         g_array_append_vals(out_param2, &result, sizeof(int));
293
294         dbus_g_method_return(req_info->context, out_param1, out_param2);
295
296         g_array_free(out_param1, TRUE);
297         g_array_free(out_param2, TRUE);
298
299         _bt_delete_request_list(req_info->req_id);
300
301 done:
302
303         g_free(searching_info->addr);
304         g_free(searching_info);
305         searching_info = NULL;
306 }
307
308 static void __bt_get_uuids(GValue *value, bt_remote_dev_info_t *info)
309 {
310         int i = 0;
311         char **uuid_value;
312
313         ret_if(value == NULL);
314         ret_if(info == NULL);
315
316         info->uuid_count = 0;
317
318         uuid_value = g_value_get_boxed(value);
319         ret_if(uuid_value == NULL);
320
321         while (uuid_value[i]) {
322                 i++;
323         }
324         ret_if(i == 0);
325
326         info->uuid_count = i;
327
328         info->uuids = g_new0(char *, info->uuid_count + 1);
329
330         for (i = 0; uuid_value[i] != NULL; i++) {
331                 info->uuids[i] = g_strdup(uuid_value[i]);
332         }
333 }
334
335 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
336 {
337         bt_remote_dev_info_t *dev_info;
338         char *object_path = NULL;
339         DBusGProxy *adapter_proxy;
340         DBusGProxy *device_proxy;
341         GHashTable *hash = NULL;
342         GValue *value;
343         const gchar *name;
344         GByteArray *manufacturer_data = NULL;
345         DBusGConnection *conn;
346
347         retv_if(address == NULL, NULL);
348
349         adapter_proxy = _bt_get_adapter_proxy();
350         retv_if(adapter_proxy == NULL, NULL);
351
352         object_path = _bt_get_device_object_path(address);
353
354         retv_if(object_path == NULL, NULL);
355
356         conn = _bt_get_system_gconn();
357         if (conn == NULL) {
358                 BT_ERR("conn == NULL");
359                 g_free(object_path);
360                 return NULL;
361         }
362
363         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
364                                 object_path, BT_PROPERTIES_INTERFACE);
365         g_free(object_path);
366         retv_if(device_proxy == NULL, NULL);
367
368         dbus_g_proxy_call(device_proxy, "GetAll", NULL,
369                                 G_TYPE_STRING, BT_DEVICE_INTERFACE,
370                                 G_TYPE_INVALID,
371                                 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
372                                 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
373
374         g_object_unref(device_proxy);
375
376         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
377
378         if (hash != NULL) {
379                 value = g_hash_table_lookup(hash, "Alias");
380                 name = value ? g_value_get_string(value) : NULL;
381
382                 if (name != NULL)
383                         DBG_SECURE("Alias Name [%s]", name);
384                 else {
385                         value = g_hash_table_lookup(hash, "Name");
386                         name = value ? g_value_get_string(value) : NULL;
387                 }
388
389                 value = g_hash_table_lookup(hash, "Class");
390                 dev_info->class = value ? g_value_get_uint(value) : 0;
391
392                 value = g_hash_table_lookup(hash, "Connected");
393                 dev_info->connected = value ? g_value_get_boolean(value) : FALSE;
394
395                 value = g_hash_table_lookup(hash, "Trusted");
396                 dev_info->trust = value ? g_value_get_boolean(value) : FALSE;
397
398                 value = g_hash_table_lookup(hash, "Paired");
399                 dev_info->paired = value ? g_value_get_boolean(value) : FALSE;
400
401                 BT_DBG("Paired %d", dev_info->paired );
402
403                 value = g_hash_table_lookup(hash, "RSSI");
404                 dev_info->rssi = value ? g_value_get_int(value) : 0;
405
406                 value = g_hash_table_lookup(hash, "LastAddrType");
407                 dev_info->addr_type = value ? g_value_get_uchar(value) : 0;
408
409                 value = g_hash_table_lookup(hash, "UUIDs");
410                 __bt_get_uuids(value, dev_info);
411
412                 value = g_hash_table_lookup(hash, "ManufacturerDataLen");
413                 dev_info->manufacturer_data_len = value ? g_value_get_uint(value) : 0;
414                 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
415                         BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
416                         dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
417                 }
418
419                 value = g_hash_table_lookup(hash, "ManufacturerData");
420                 manufacturer_data = value ? g_value_get_boxed(value) : NULL;
421                 if (manufacturer_data) {
422                         if (dev_info->manufacturer_data_len > 0) {
423                                 BT_DBG("manufacturer_data_len  = %d", dev_info->manufacturer_data_len);
424                                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
425                                 memcpy(dev_info->manufacturer_data, manufacturer_data->data, dev_info->manufacturer_data_len);
426                         }
427                 }
428
429                 dev_info->address = g_strdup(address);
430                 dev_info->name = g_strdup(name);
431
432                 g_hash_table_destroy(hash);
433         } else {
434                 BT_ERR("Hash is NULL\n");
435                 g_free(dev_info);
436                 dev_info = NULL;
437         }
438
439         return dev_info;
440 }
441
442 static gboolean __ignore_auto_pairing_request(const char *address)
443 {
444         gchar *buffer;
445         char **lines;
446         int i;
447         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
448         char *temp_buffer;
449         FILE *fp;
450         long size;
451         size_t result;
452
453         BT_DBG("+\n");
454
455         if (address == NULL)
456                 return FALSE;
457
458         /* Get the LAP(Lower Address part) */
459         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
460              a "," */
461         snprintf(lap_address, sizeof(lap_address), ",%s", address);
462
463         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
464
465         if (fp == NULL) {
466                 BT_ERR("fopen failed \n");
467                 return FALSE;
468         }
469
470         fseek(fp, 0, SEEK_END);
471         size = ftell(fp);
472         rewind(fp);
473
474         if (size < 0) {
475                 BT_ERR("Get file size failed \n");
476                 fclose(fp);
477                 return FALSE;
478         }
479
480         buffer = g_malloc0(sizeof(char) * size);
481         result = fread((char *)buffer, 1, size, fp);
482         fclose(fp);
483         if (result != size) {
484                 BT_ERR("Read Error\n");
485                 g_free(buffer);
486                 return FALSE;
487         }
488
489         BT_DBG("Buffer = %s\n", buffer);
490
491         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
492         g_free(buffer);
493
494         if (lines == NULL)
495                 return FALSE;
496
497         /* Write the data and insert new device data */
498         for (i = 0; lines[i] != NULL; i++) {
499                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
500                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
501                         g_free(lines[i]);
502                         lines[i] = temp_buffer;
503                 }
504         }
505         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
506         g_strfreev(lines);
507
508         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
509
510         if (fp == NULL) {
511                 BT_ERR("fopen failed \n");
512                 g_free(buffer);
513                 return FALSE;
514         }
515
516         BT_DBG("Buffer = %s\n", buffer);
517         fwrite(buffer, 1, strlen(buffer), fp);
518         fclose(fp);
519
520         g_free(buffer);
521
522         BT_DBG("-\n");
523
524         return FALSE;
525 }
526
527 static int __bt_retry_bond(void)
528 {
529         BT_CHECK_PARAMETER(bonding_info, return);
530         BT_CHECK_PARAMETER(bonding_info->addr, return);
531
532         if (!dbus_g_proxy_begin_call_with_timeout(bonding_info->device_proxy,
533                                 "Pair",
534                                 (DBusGProxyCallNotify) __bt_bond_device_cb,
535                                 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
536                                 G_TYPE_UCHAR, bonding_info->conn_type,
537                                 G_TYPE_INVALID)) {
538                 BT_ERR("RePair call fail");
539
540                 g_object_unref(bonding_info->device_proxy);
541                 goto fail;
542         }
543
544
545         return BLUETOOTH_ERROR_NONE;
546
547 fail:
548         __bt_decline_pair_request();
549         is_device_creating = FALSE;
550
551         return BLUETOOTH_ERROR_INTERNAL;
552 }
553
554
555 static int __bt_remove_and_bond(void)
556 {
557         DBusGProxy *adapter_proxy;
558         GError *err = NULL;
559         char *device_path = NULL;
560
561         BT_CHECK_PARAMETER(bonding_info, return);
562         BT_CHECK_PARAMETER(bonding_info->addr, return);
563
564         adapter_proxy = _bt_get_adapter_proxy();
565         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
566
567         dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
568                           G_TYPE_STRING, bonding_info->addr,
569                           G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
570                           &device_path,
571                           G_TYPE_INVALID);
572
573         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
574
575         dbus_g_proxy_call(adapter_proxy, "UnpairDevice",
576                           &err, DBUS_TYPE_G_OBJECT_PATH, device_path,
577                           G_TYPE_INVALID, G_TYPE_INVALID);
578         g_free(device_path);
579         if (err != NULL) {
580                 BT_ERR("UnpairDevice Fail: %s", err->message);
581                 g_error_free(err);
582                 return BLUETOOTH_ERROR_INTERNAL;
583         }
584
585         return __bt_retry_bond();
586 }
587
588 static int __bt_cancel_and_bond(void)
589 {
590         int ret = BLUETOOTH_ERROR_NONE;
591
592         ret = _bt_agent_reply_cancellation();
593         if (ret != BLUETOOTH_ERROR_NONE){
594                 BT_ERR("Fail to call reply cancellation");
595                 return ret;
596         }
597
598         return __bt_retry_bond();
599 }
600
601
602 static void __bt_bond_device_cb(DBusGProxy *proxy, DBusGProxyCall *call,
603                                                      gpointer user_data)
604 {
605         int result = BLUETOOTH_ERROR_NONE;
606         GError *err = NULL;
607         GArray *out_param1;
608         GArray *out_param2;
609         request_info_t *req_info;
610         bluetooth_device_info_t dev_info;
611         bt_remote_dev_info_t *remote_dev_info;
612
613         /* Terminate ALL system popup */
614 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
615         //syspopup_destroy_all();
616 #endif
617
618         dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
619
620         g_object_unref(proxy);
621
622         is_device_creating = FALSE;
623
624         if (bonding_info == NULL) {
625                 /* Send reply */
626                 BT_ERR("bonding_info == NULL");
627                 if (err)
628                         g_error_free(err);
629                 return;
630         }
631
632         bonding_info->device_proxy = NULL;
633
634         req_info = _bt_get_request_info(bonding_info->req_id);
635         if (req_info == NULL) {
636                 BT_ERR("req_info == NULL");
637                 goto done;
638         }
639
640         if (err != NULL) {
641                 BT_ERR("Error occured in Pair [%s]", err->message);
642
643                 if (!strcmp(err->message, "Already Exists")) {
644                         BT_INFO("Existing Bond, remove and retry");
645                         ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
646
647                         result = BLUETOOTH_ERROR_PARING_FAILED;
648 //              } else if (_bt_agent_is_canceled() ||
649 //                      !strcmp(err->message, "Authentication Canceled")) {
650 //                      result = BLUETOOTH_ERROR_CANCEL_BY_USER;
651                 } else if (!strcmp(err->message, "Authentication Rejected")) {
652                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
653                 } else if (!strcmp(err->message, "In Progress")) {
654                         BT_INFO("Bond in progress, cancel and retry");
655                         ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
656
657                         result = BLUETOOTH_ERROR_PARING_FAILED;
658                 } else if (!strcmp(err->message, "Authentication Failed")) {
659                         if (bonding_info->is_autopair == TRUE) {
660                                 _bt_set_autopair_status_in_bonding_info(FALSE);
661                                 __ignore_auto_pairing_request(bonding_info->addr);
662                         }
663                         result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
664                 } else if (!strcmp(err->message, "Page Timeout")) {
665                         /* This is the special case
666                              As soon as call bluetooth_bond_device, try to cancel bonding.
667                              In this case, before completing to call 'CreatePairedDevice' method
668                              the procedure is stopped. So 'Cancle' error is not return.
669                         */
670                         result = BLUETOOTH_ERROR_HOST_DOWN;
671                 } else if (!strcmp(err->message, BT_TIMEOUT_MESSAGE)) {
672                         dbus_g_proxy_call(proxy, "CancelDeviceCreation", NULL,
673                                            G_TYPE_STRING, bonding_info->addr,
674                                            G_TYPE_INVALID, G_TYPE_INVALID);
675
676                         result = BLUETOOTH_ERROR_INTERNAL;
677                 } else if (!strcmp(err->message, "Connection Timeout")) {
678                         BT_INFO("pairing request timeout");
679                         /* Pairing request timeout */
680                         result = BLUETOOTH_ERROR_TIMEOUT;
681                 } else if (!strcmp(err->message, "Authentication Timeout")) {
682                         /* Pairing request timeout */
683                         result = BLUETOOTH_ERROR_TIMEOUT;
684                 } else {
685                         BT_DBG("Default case");
686                         result = BLUETOOTH_ERROR_PARING_FAILED;
687                 }
688         }
689 #if 0
690         if (result == BLUETOOTH_ERROR_PARING_FAILED ||
691                         result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
692                         result == BLUETOOTH_ERROR_TIMEOUT ||
693                         result == BLUETOOTH_ERROR_HOST_DOWN) {
694
695                         BT_INFO("result error %d", result);
696                         bonding_info->result = result;
697 #ifdef TIZEN_WEARABLE
698                 __bt_launch_unable_to_pairing_syspopup(result);
699 #endif
700         }
701
702         g_object_unref(proxy);
703         bonding_info->device_proxy = NULL;
704 #endif
705         if (result != BLUETOOTH_ERROR_NONE)
706                 goto dbus_return;
707
708         remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
709
710         /* Send the event to application */
711         if (remote_dev_info != NULL) {
712                 _bt_send_event(BT_ADAPTER_EVENT,
713                         BLUETOOTH_EVENT_BONDING_FINISHED,
714                         DBUS_TYPE_INT32, &result,
715                         DBUS_TYPE_STRING, &bonding_info->addr,
716                         DBUS_TYPE_UINT32, &remote_dev_info->class,
717                         DBUS_TYPE_INT16, &remote_dev_info->rssi,
718                         DBUS_TYPE_STRING, &remote_dev_info->name,
719                         DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
720                         DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
721                         DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
722                         DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
723                         &remote_dev_info->uuids, remote_dev_info->uuid_count,
724                         DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
725                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
726                         &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
727                         DBUS_TYPE_INVALID);
728
729                 _bt_free_device_info(remote_dev_info);
730         }
731
732 dbus_return:
733
734         if (req_info->context == NULL)
735                 goto done;
736
737         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
738         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
739
740         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
741         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
742                                         bonding_info->addr);
743
744 #if 0
745         if (_bt_adapter_get_status() != BT_ACTIVATED)
746                 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
747 #endif
748
749         g_array_append_vals(out_param1, &dev_info,
750                                 sizeof(bluetooth_device_info_t));
751         g_array_append_vals(out_param2, &result, sizeof(int));
752
753         dbus_g_method_return(req_info->context, out_param1, out_param2);
754
755         g_array_free(out_param1, TRUE);
756         g_array_free(out_param2, TRUE);
757
758         _bt_delete_request_list(req_info->req_id);
759
760 done:
761         if (err)
762                 g_error_free(err);
763
764 #if 0
765         _bt_agent_set_canceled(FALSE);
766 #endif
767
768         g_free(bonding_info->addr);
769         g_free(bonding_info);
770         bonding_info = NULL;
771 }
772
773 int _bt_bond_device(int request_id,
774                 bluetooth_device_address_t *device_address,
775                 unsigned short conn_type, GArray **out_param1)
776 {
777         DBusGProxy *proxy;
778         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
779         bluetooth_device_info_t dev_info;
780
781         DBusGConnection *conn;
782         char *device_path = NULL;
783         DBusGProxy *adapter_proxy;
784         GError *error = NULL;
785
786         BT_CHECK_PARAMETER(device_address, return);
787
788         if (bonding_info) {
789                 BT_ERR("Bonding in progress");
790
791                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
792                 memcpy(dev_info.device_address.addr, device_address->addr,
793                                 BLUETOOTH_ADDRESS_LENGTH);
794
795                 g_array_append_vals(*out_param1, &dev_info,
796                                 sizeof(bluetooth_device_info_t));
797
798                 return BLUETOOTH_ERROR_DEVICE_BUSY;
799         }
800
801         conn = _bt_get_system_gconn();
802         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
803
804         _bt_convert_addr_type_to_string(address, device_address->addr);
805
806         device_path = _bt_get_device_object_path(address);
807
808         if (device_path == NULL) {
809                 BT_ERR("No searched device");
810
811                 adapter_proxy = _bt_get_adapter_proxy();
812                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
813
814                 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
815                         G_TYPE_STRING, address,
816                         G_TYPE_INVALID, G_TYPE_INVALID);
817
818                 if (error != NULL) {
819                         BT_ERR("CreateDevice Fail: %s", error->message);
820                         g_error_free(error);
821                 }
822
823                 device_path = _bt_get_device_object_path(address);
824                 if (device_path == NULL) {
825                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
826                         memcpy(dev_info.device_address.addr, device_address->addr,
827                                         BLUETOOTH_ADDRESS_LENGTH);
828
829                         g_array_append_vals(*out_param1, &dev_info,
830                                         sizeof(bluetooth_device_info_t));
831
832                         return BLUETOOTH_ERROR_NOT_PAIRED;
833                 } else {
834                         BT_INFO("device_path is created[%s]", device_path);
835                 }
836         }
837
838         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
839                                 device_path, BT_DEVICE_INTERFACE);
840
841         g_free(device_path);
842         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
843
844         bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
845         bonding_info->addr = g_strdup(address);
846         bonding_info->req_id = request_id;
847
848         bonding_info->device_proxy = proxy;
849         bonding_info->conn_type = conn_type;
850
851         is_device_creating = TRUE;
852
853         if (!dbus_g_proxy_begin_call_with_timeout(proxy, "Pair",
854                                 (DBusGProxyCallNotify) __bt_bond_device_cb,
855                                 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
856                                 G_TYPE_UCHAR, conn_type,
857                                 G_TYPE_INVALID)) {
858                 BT_ERR("Pair call fail");
859                 g_object_unref(proxy);
860                 bonding_info->device_proxy = NULL;
861                 goto fail;
862         }
863 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
864
865         return BLUETOOTH_ERROR_NONE;
866 fail:
867         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
868         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
869                                         bonding_info->addr);
870
871         g_array_append_vals(*out_param1, &dev_info,
872                                 sizeof(bluetooth_device_info_t));
873
874         is_device_creating = FALSE;
875
876         g_free(bonding_info->addr);
877         g_free(bonding_info);
878         bonding_info = NULL;
879
880         return BLUETOOTH_ERROR_INTERNAL;
881 }
882
883 int _bt_cancel_bonding(void)
884 {
885         int ret = BLUETOOTH_ERROR_NONE;
886
887         retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
888
889         ret = _bt_agent_reply_cancellation();
890         if (ret != BLUETOOTH_ERROR_NONE){
891                 BT_ERR("Fail to call reply cancellation");
892                 return ret;
893         }
894
895         _bt_agent_set_canceled(TRUE);
896
897         return BLUETOOTH_ERROR_NONE;
898 }
899
900 static void __bt_unbond_cb(DBusGProxy *proxy, DBusGProxyCall *call,
901                                 gpointer user_data)
902 {
903         GError *err = NULL;
904         GArray *out_param1;
905         GArray *out_param2;
906         int result = BLUETOOTH_ERROR_NONE;
907         bt_funcion_data_t *unbonding_info;
908         bluetooth_device_info_t dev_info;
909         request_info_t *req_info;
910
911         dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
912
913         unbonding_info = user_data;
914
915         if (unbonding_info == NULL) {
916                 /* Send reply */
917                 BT_ERR("unbonding_info == NULL");
918                 goto done;
919         }
920
921         req_info = _bt_get_request_info(unbonding_info->req_id);
922         if (req_info == NULL) {
923                 BT_ERR("req_info == NULL");
924                 goto done;
925         }
926
927         if (err != NULL) {
928                 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
929                 result = BLUETOOTH_ERROR_INTERNAL;
930         }
931
932         if (req_info->context == NULL)
933                 goto done;
934
935         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
936         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
937
938         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
939         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
940                                         unbonding_info->addr);
941
942         g_array_append_vals(out_param1, &dev_info,
943                                 sizeof(bluetooth_device_info_t));
944         g_array_append_vals(out_param2, &result, sizeof(int));
945
946         dbus_g_method_return(req_info->context, out_param1, out_param2);
947
948         _bt_delete_request_list(req_info->req_id);
949
950         g_array_free(out_param1, TRUE);
951         g_array_free(out_param2, TRUE);
952
953 done:
954         if (err)
955                 g_error_free(err);
956
957         if (unbonding_info) {
958                 g_free(unbonding_info->addr);
959                 g_free(unbonding_info);
960         }
961 }
962
963 int _bt_unbond_device(int request_id,
964                         bluetooth_device_address_t *device_address,
965                         GArray **out_param1)
966 {
967         char *device_path = NULL;
968         bt_funcion_data_t *unbonding_info;
969         DBusGProxy *adapter_proxy = NULL;
970         DBusGProxy *device_proxy = NULL;
971         DBusGConnection *conn;
972         int result = BLUETOOTH_ERROR_INTERNAL;
973         bluetooth_device_info_t dev_info;
974         GValue paired = { 0 };
975         GError *error = NULL;
976
977         BT_CHECK_PARAMETER(device_address, return);
978
979         adapter_proxy = _bt_get_adapter_proxy();
980         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
981
982         /* allocate user data so that it can be retrieved in callback */
983         unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
984         unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
985         unbonding_info->req_id = request_id;
986
987         _bt_convert_addr_type_to_string(unbonding_info->addr,
988                                         device_address->addr);
989
990         device_path = _bt_get_device_object_path(unbonding_info->addr);
991
992         if (device_path == NULL) {
993                 BT_ERR("No paired device");
994                 result = BLUETOOTH_ERROR_NOT_PAIRED;
995                 goto fail;
996         }
997
998         conn = _bt_get_system_gconn();
999         if (conn == NULL) {
1000                 BT_ERR("conn is NULL");
1001                 result = BLUETOOTH_ERROR_INTERNAL;
1002                 goto fail;
1003         }
1004
1005         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1006                                 device_path, BT_PROPERTIES_INTERFACE);
1007         if (device_proxy != NULL) {
1008                 if (!dbus_g_proxy_call(device_proxy, "Get", &error,
1009                                 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1010                                 G_TYPE_STRING, "Paired",
1011                                 G_TYPE_INVALID,
1012                                 G_TYPE_VALUE, &paired,
1013                                 G_TYPE_INVALID)) {
1014                         if (error != NULL) {
1015                                 BT_ERR("Getting property failed: [%s]\n", error->message);
1016                                 g_error_free(error);
1017                         }
1018                 } else {
1019                         if (g_value_get_boolean(&paired) == FALSE) {
1020                                 BT_ERR("No paired device");
1021                                 g_object_unref(device_proxy);
1022                                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1023                                 goto fail;
1024                         }
1025                 }
1026                 g_object_unref(device_proxy);
1027         }
1028
1029         if (!dbus_g_proxy_begin_call(adapter_proxy, "UnpairDevice",
1030                                 (DBusGProxyCallNotify) __bt_unbond_cb,
1031                                 (gpointer)unbonding_info, NULL,
1032                                 DBUS_TYPE_G_OBJECT_PATH, device_path,
1033                                 G_TYPE_INVALID)) {
1034                 BT_ERR("RemoveBonding begin failed\n");
1035                 goto fail;
1036         }
1037         g_free(device_path);
1038         return BLUETOOTH_ERROR_NONE;
1039
1040 fail:
1041         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1042         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1043                                         unbonding_info->addr);
1044
1045         g_array_append_vals(*out_param1, &dev_info,
1046                                 sizeof(bluetooth_device_info_t));
1047         g_free(device_path);
1048         g_free(unbonding_info->addr);
1049         g_free(unbonding_info);
1050         return result;
1051 }
1052
1053 static void __bt_discover_cb(DBusGProxy *proxy, DBusGProxyCall *call,
1054                                 gpointer user_data)
1055 {
1056         GError *err = NULL;
1057         GHashTable *hash = NULL;
1058         GArray *out_param1;
1059         GArray *out_param2;
1060         int result = BLUETOOTH_ERROR_NONE;
1061         bluetooth_device_info_t dev_info;
1062         bt_remote_dev_info_t *remote_dev_info;
1063         request_info_t *req_info;
1064
1065         dbus_g_proxy_end_call(proxy, call, &err,
1066                               dbus_g_type_get_map("GHashTable", G_TYPE_UINT, G_TYPE_STRING), &hash,
1067                               G_TYPE_INVALID);
1068
1069         g_object_unref(proxy);
1070
1071         if (searching_info == NULL) {
1072                 /* Send reply */
1073                 BT_ERR("unbonding_info == NULL");
1074                 goto done;
1075         }
1076
1077         req_info = _bt_get_request_info(searching_info->req_id);
1078         if (req_info == NULL) {
1079                 BT_ERR("req_info == NULL");
1080                 goto done;
1081         }
1082
1083         if (err != NULL) {
1084                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1085
1086                 if (!strcmp("Operation canceled", err->message)) {
1087                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1088                 } else if (!strcmp("In Progress", err->message)) {
1089                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1090                 } else if (!strcmp("Host is down", err->message)) {
1091                         result = BLUETOOTH_ERROR_HOST_DOWN;
1092                 } else {
1093                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1094                 }
1095
1096                 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1097                      result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1098                         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1099                         if (remote_dev_info && remote_dev_info->uuids != NULL &&
1100                              remote_dev_info->uuid_count > 0) {
1101                                 result = BLUETOOTH_ERROR_NONE;
1102                                 goto event;
1103                         }
1104                         _bt_free_device_info(remote_dev_info);
1105                 }
1106                 goto dbus_return;
1107         }
1108
1109         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1110
1111 event:
1112         /* Send the event to application */
1113         if (remote_dev_info != NULL) {
1114                 _bt_send_event(BT_ADAPTER_EVENT,
1115                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
1116                         DBUS_TYPE_INT32, &result,
1117                         DBUS_TYPE_STRING, &searching_info->addr,
1118                         DBUS_TYPE_UINT32, &remote_dev_info->class,
1119                         DBUS_TYPE_INT16, &remote_dev_info->rssi,
1120                         DBUS_TYPE_STRING, &remote_dev_info->name,
1121                         DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
1122                         DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
1123                         DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
1124                         DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1125                         &remote_dev_info->uuids, remote_dev_info->uuid_count,
1126                         DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
1127                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
1128                         &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1129                         DBUS_TYPE_INVALID);
1130
1131                 _bt_free_device_info(remote_dev_info);
1132         }
1133
1134 dbus_return:
1135         if (req_info->context == NULL)
1136                 goto done;
1137
1138         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1139         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
1140
1141         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1142         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1143                                         searching_info->addr);
1144
1145         g_array_append_vals(out_param1, &dev_info,
1146                                 sizeof(bluetooth_device_info_t));
1147         g_array_append_vals(out_param2, &result, sizeof(int));
1148
1149         dbus_g_method_return(req_info->context, out_param1, out_param2);
1150
1151         g_array_free(out_param1, TRUE);
1152         g_array_free(out_param2, TRUE);
1153
1154         _bt_delete_request_list(req_info->req_id);
1155 done:
1156         if (err)
1157                 g_error_free(err);
1158
1159         g_hash_table_destroy(hash);
1160
1161         if (searching_info) {
1162                 g_free(searching_info->addr);
1163                 g_free(searching_info);
1164                 searching_info = NULL;
1165         }
1166 }
1167
1168 int _bt_search_device(int request_id,
1169                         bluetooth_device_address_t *device_address)
1170 {
1171         char *device_path = NULL;
1172         DBusGProxy *device_proxy = NULL;
1173         DBusGConnection *conn;
1174
1175         DBusGProxy *adapter_proxy;
1176         int result = BLUETOOTH_ERROR_INTERNAL;
1177
1178         BT_CHECK_PARAMETER(device_address, return);
1179
1180         if (searching_info) {
1181                 BT_ERR("Service searching in progress");
1182                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1183         }
1184
1185         adapter_proxy = _bt_get_adapter_proxy();
1186         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1187
1188         /* allocate user data so that it can be retrieved in callback */
1189         searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1190         searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1191         searching_info->req_id = request_id;
1192
1193         _bt_convert_addr_type_to_string(searching_info->addr,
1194                                         device_address->addr);
1195
1196         conn = _bt_get_system_gconn();
1197         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1198
1199
1200         device_path = _bt_get_device_object_path(searching_info->addr);
1201
1202         if (device_path == NULL) {
1203                 BT_ERR("No paired device");
1204                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1205                 goto fail;
1206         }
1207
1208         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1209                                       device_path, BT_PROPERTIES_INTERFACE);
1210         g_free(device_path);
1211         if (device_proxy == NULL) {
1212                 result = BLUETOOTH_ERROR_INTERNAL;
1213                 goto fail;
1214         }
1215
1216         if (!dbus_g_proxy_begin_call(device_proxy, "Get",
1217                         (DBusGProxyCallNotify)__bt_discover_cb,
1218                         (gpointer)searching_info, NULL,
1219                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
1220                         G_TYPE_STRING, "UUIDs",
1221                         G_TYPE_INVALID)) {
1222                 BT_ERR("DiscoverServices failed");
1223                 goto fail;
1224         }
1225
1226         searching_info->device_proxy = device_proxy;
1227
1228         return BLUETOOTH_ERROR_NONE;
1229 fail:
1230         if (device_proxy)
1231                 g_object_unref(device_proxy);
1232
1233         g_free(searching_info->addr);
1234         g_free(searching_info);
1235         searching_info = NULL;
1236         return result;
1237 }
1238
1239 int _bt_cancel_search_device(void)
1240 {
1241         GError *err = NULL;
1242
1243         retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1244
1245         if (searching_info->device_proxy) {
1246                 dbus_g_proxy_call(searching_info->device_proxy,
1247                                 "CancelDiscovery",
1248                                 &err,
1249                                 G_TYPE_INVALID, G_TYPE_INVALID);
1250         }
1251         __bt_cancel_search_service_done();
1252
1253         return BLUETOOTH_ERROR_NONE;
1254 }
1255
1256 int _bt_set_alias(bluetooth_device_address_t *device_address,
1257                                       const char *alias)
1258 {
1259         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1260         gchar *device_path = NULL;
1261         DBusGProxy *adapter_proxy;
1262         DBusGProxy *device_proxy;
1263         GError *error = NULL;
1264         GValue name = { 0 };
1265         DBusGConnection *conn;
1266
1267         BT_CHECK_PARAMETER(device_address, return);
1268         BT_CHECK_PARAMETER(alias, return);
1269
1270         adapter_proxy = _bt_get_adapter_proxy();
1271         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1272
1273         conn = _bt_get_system_gconn();
1274         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1275
1276         _bt_convert_addr_type_to_string(address, device_address->addr);
1277
1278         device_path = _bt_get_device_object_path(address);
1279
1280         if (device_path == NULL) {
1281                 BT_ERR("No paired device");
1282                 return BLUETOOTH_ERROR_NOT_PAIRED;
1283         }
1284
1285         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1286                                 device_path, BT_PROPERTIES_INTERFACE);
1287
1288         g_free(device_path);
1289         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1290         g_value_init(&name, G_TYPE_STRING);
1291         g_value_set_string(&name, alias);
1292
1293         dbus_g_proxy_call(device_proxy, "Set", &error,
1294                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
1295                         G_TYPE_STRING, "Alias",
1296                         G_TYPE_VALUE, &name,
1297                         G_TYPE_INVALID, G_TYPE_INVALID);
1298
1299         g_object_unref(device_proxy);
1300
1301         g_value_unset(&name);
1302
1303         if (error) {
1304                  BT_ERR("SetProperty error: [%s]", error->message);
1305                  g_error_free(error);
1306                  return BLUETOOTH_ERROR_INTERNAL;
1307         }
1308
1309         return BLUETOOTH_ERROR_NONE;
1310 }
1311
1312 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1313                                       gboolean authorize)
1314 {
1315         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1316         gchar *device_path = NULL;
1317         DBusGProxy *device_proxy;
1318         gboolean previous_value;
1319         GError *error = NULL;
1320         GValue trusted = { 0 };
1321         GValue trusted_v = { 0 };
1322         DBusGConnection *conn;
1323         int ret = BLUETOOTH_ERROR_NONE;
1324
1325         BT_CHECK_PARAMETER(device_address, return);
1326
1327         conn = _bt_get_system_gconn();
1328         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1329
1330         _bt_convert_addr_type_to_string(address, device_address->addr);
1331
1332         device_path = _bt_get_device_object_path(address);
1333
1334         if (device_path == NULL) {
1335                 BT_ERR("No paired device");
1336                 return BLUETOOTH_ERROR_NOT_PAIRED;
1337         }
1338
1339         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1340                                 device_path, BT_PROPERTIES_INTERFACE);
1341         g_free(device_path);
1342         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1343
1344         if (!dbus_g_proxy_call(device_proxy, "Get", &error,
1345                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
1346                         G_TYPE_STRING, "Trusted",
1347                         G_TYPE_INVALID,
1348                         G_TYPE_VALUE, &trusted_v,
1349                         G_TYPE_INVALID)) {
1350                 if (error != NULL) {
1351                         BT_ERR("Getting property failed: [%s]\n", error->message);
1352                         g_error_free(error);
1353                 }
1354                 g_object_unref(device_proxy);
1355                 return BLUETOOTH_ERROR_INTERNAL;
1356         }
1357
1358         previous_value = g_value_get_boolean(&trusted_v);
1359
1360         /* If the input is same with previous value, return error. */
1361         if (previous_value == authorize) {
1362                 BT_ERR("Same value: %d", previous_value);
1363                 g_object_unref(device_proxy);
1364                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1365                 goto done;
1366         }
1367
1368         g_value_init(&trusted, G_TYPE_BOOLEAN);
1369         g_value_set_boolean(&trusted, authorize);
1370
1371         dbus_g_proxy_call(device_proxy, "Set", &error,
1372                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
1373                         G_TYPE_STRING, "Trusted",
1374                         G_TYPE_VALUE, &trusted,
1375                         G_TYPE_INVALID, G_TYPE_INVALID);
1376
1377         g_object_unref(device_proxy);
1378         g_value_unset(&trusted);
1379
1380         if (error) {
1381                  BT_ERR("SetProperty error: [%s]", error->message);
1382                  g_error_free(error);
1383                  ret = BLUETOOTH_ERROR_INTERNAL;
1384         }
1385 done:
1386         return ret;
1387 }
1388
1389 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1390                         gboolean *is_connected)
1391 {
1392         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1393         char *object_path = NULL;
1394
1395         DBusGProxy *device_proxy;
1396         GError *error = NULL;
1397         GValue *value;
1398         GHashTable *hash = NULL;
1399         DBusGConnection *conn;
1400         int ret = BLUETOOTH_ERROR_NONE;
1401
1402         BT_CHECK_PARAMETER(device_address, return);
1403
1404         conn = _bt_get_system_gconn();
1405         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1406
1407         _bt_convert_addr_type_to_string(address, device_address->addr);
1408
1409         object_path = _bt_get_device_object_path(address);
1410         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1411
1412         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1413                                 object_path, BT_PROPERTIES_INTERFACE);
1414         g_free(object_path);
1415         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1416
1417         dbus_g_proxy_call(device_proxy, "GetAll", &error,
1418                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
1419                         G_TYPE_INVALID,
1420                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
1421                         G_TYPE_VALUE), &hash, G_TYPE_INVALID);
1422
1423         if (error != NULL) {
1424                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1425                 g_error_free(error);
1426                 g_object_unref(device_proxy);
1427                 return BLUETOOTH_ERROR_INTERNAL;
1428         }
1429
1430         if (hash == NULL) {
1431                 g_object_unref(device_proxy);
1432                 return BLUETOOTH_ERROR_INTERNAL;
1433         }
1434
1435         value = g_hash_table_lookup(hash, "GattConnected");
1436         *is_connected = g_value_get_boolean(value);
1437
1438         BT_DBG("gatt is connected : %d", *is_connected);
1439
1440         g_hash_table_destroy(hash);
1441         g_object_unref(device_proxy);
1442
1443         return ret;
1444 }
1445
1446 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1447                         int connection_type, gboolean *is_connected)
1448 {
1449         char *object_path = NULL;
1450         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1451         DBusGProxy *device_proxy = NULL;
1452         DBusGProxy *adapter_proxy = NULL;
1453         DBusGConnection *conn;
1454         GError *error = NULL;
1455         GHashTable *hash = NULL;
1456         GValue *value = NULL;
1457
1458         dbus_bool_t val = FALSE;
1459         char *uuid;
1460
1461         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1462         retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1463
1464         *is_connected = FALSE;
1465
1466         if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1467                 return _bt_rfcomm_is_device_connected(device_address,
1468                                                 is_connected);
1469         else if (connection_type == BLUETOOTH_GATT_SERVICE)
1470                 return _bt_is_gatt_connected(device_address, is_connected);
1471
1472         adapter_proxy = _bt_get_adapter_proxy();
1473         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1474
1475         conn = _bt_get_system_gconn();
1476         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1477
1478         _bt_convert_addr_type_to_string(address, device_address->addr);
1479
1480         if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE)     {
1481                 object_path = _bt_get_adapter_path();
1482                 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1483                                                 object_path, BT_NETWORK_SERVER_INTERFACE);
1484                 g_free(object_path);
1485                 if (device_proxy == NULL) {
1486                         BT_DBG("Device don't have this service");
1487                         return BLUETOOTH_ERROR_INTERNAL;
1488                 }
1489                 dbus_g_proxy_call(device_proxy, "GetProperties", NULL,
1490                                         G_TYPE_STRING, address,
1491                                         G_TYPE_INVALID,
1492                                         dbus_g_type_get_map("GHashTable",
1493                                         G_TYPE_STRING, G_TYPE_VALUE),
1494                                         &hash, G_TYPE_INVALID);
1495                 if (hash != NULL) {
1496                         value = g_hash_table_lookup(hash, "Connected");
1497                         *is_connected = value ? g_value_get_boolean(value) : FALSE;
1498                         g_hash_table_destroy(hash);
1499                 }
1500         } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1501                 return _bt_is_network_connected(_bt_get_net_conn(),
1502                                                 device_address->addr, is_connected);
1503         } else {
1504                 uuid = _bt_get_profile_uuid128(connection_type);
1505                 if (uuid == NULL) {
1506                         BT_ERR("uuid is NULL");
1507                         return BLUETOOTH_ERROR_INTERNAL;
1508                 }
1509
1510                 BT_DBG("uuid: %s", uuid);
1511
1512                 object_path = _bt_get_device_object_path(address);
1513                 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1514
1515                 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1516                                                 object_path, BT_DEVICE_INTERFACE);
1517                 g_free(object_path);
1518                 if (device_proxy == NULL) {
1519                         BT_DBG("Device don't have this service");
1520                         g_free(uuid);
1521                         return BLUETOOTH_ERROR_INTERNAL;
1522                 }
1523                 dbus_g_proxy_call(device_proxy, "IsConnectedProfile", &error,
1524                                         G_TYPE_STRING, uuid,
1525                                         G_TYPE_INVALID,
1526                                         G_TYPE_BOOLEAN, &val,
1527                                         G_TYPE_INVALID);
1528
1529                 if (error != NULL) {
1530                         BT_ERR("Failed to get properties: %s\n", error->message);
1531                         g_error_free(error);
1532                 }
1533
1534                 *is_connected = val;
1535                 g_free(uuid);
1536         }
1537
1538         g_object_unref(device_proxy);
1539         return BLUETOOTH_ERROR_NONE;
1540 }
1541
1542 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1543 {
1544         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1545         gchar *device_path = NULL;
1546         GError *error = NULL;
1547         DBusGProxy *device_proxy = NULL;
1548         DBusGProxy *adapter_proxy;
1549         DBusGConnection *conn;
1550         int ret = BLUETOOTH_ERROR_NONE;
1551
1552         BT_CHECK_PARAMETER(bd_addr, return);
1553
1554         _bt_convert_addr_type_to_string(device_address,
1555                         (unsigned char *)bd_addr->addr);
1556
1557         conn = _bt_get_system_gconn();
1558         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1559
1560         adapter_proxy = _bt_get_adapter_proxy();
1561         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1562
1563         device_path = _bt_get_device_object_path(device_address);
1564         if (device_path == NULL) {
1565                 BT_DBG("device_path NULL");
1566                 ret = BLUETOOTH_ERROR_INTERNAL;
1567                 return ret;
1568         }
1569
1570         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1571
1572         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1573                                 device_path, BT_DEVICE_INTERFACE);
1574         g_free(device_path);
1575         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1576
1577         dbus_g_proxy_call(device_proxy, "ConnectLE", &error,
1578                         G_TYPE_BOOLEAN, auto_connect, G_TYPE_INVALID,
1579                         G_TYPE_INVALID);
1580         if (error) {
1581                 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1582                 g_error_free(error);
1583                 g_object_unref(device_proxy);
1584                 return BLUETOOTH_ERROR_INTERNAL;
1585         }
1586
1587         g_object_unref(device_proxy);
1588
1589         return ret;
1590 }
1591
1592 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1593 {
1594         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1595         gchar *device_path = NULL;
1596         GError *error = NULL;
1597         DBusGProxy *device_proxy = NULL;
1598         DBusGProxy *adapter_proxy;
1599         DBusGConnection *conn;
1600         int ret = BLUETOOTH_ERROR_NONE;
1601
1602         BT_CHECK_PARAMETER(bd_addr, return);
1603
1604         _bt_convert_addr_type_to_string(device_address,
1605                         (unsigned char *)bd_addr->addr);
1606
1607         conn = _bt_get_system_gconn();
1608         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1609
1610         adapter_proxy = _bt_get_adapter_proxy();
1611         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1612
1613         device_path = _bt_get_device_object_path(device_address);
1614         if (device_path == NULL) {
1615                 BT_DBG("device_path NULL");
1616                 ret = BLUETOOTH_ERROR_INTERNAL;
1617                 return ret;
1618         }
1619
1620         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1621
1622         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1623                                 device_path, BT_DEVICE_INTERFACE);
1624         g_free(device_path);
1625         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1626
1627         dbus_g_proxy_call(device_proxy, "DisconnectLE", &error, G_TYPE_INVALID, G_TYPE_INVALID);
1628         if (error) {
1629                 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1630                 g_error_free(error);
1631                 g_object_unref(device_proxy);
1632                 return BLUETOOTH_ERROR_INTERNAL;
1633         }
1634
1635         g_object_unref(device_proxy);
1636
1637         return ret;
1638 }
1639
1640 int _bt_connect_profile(char *address, char *uuid,
1641                                                 void *cb, gpointer func_data)
1642 {
1643         char *object_path;
1644         DBusGProxy *proxy;
1645         DBusGConnection *conn;
1646         DBusGProxy *adapter_proxy;
1647         GError *error = NULL;
1648
1649         conn = _bt_get_system_gconn();
1650         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1651
1652         object_path = _bt_get_device_object_path(address);
1653         if (object_path == NULL) {
1654                 BT_ERR("No searched device");
1655
1656                 adapter_proxy = _bt_get_adapter_proxy();
1657                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1658
1659                 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
1660                         G_TYPE_STRING, address,
1661                         G_TYPE_INVALID, G_TYPE_INVALID);
1662
1663                 if (error != NULL) {
1664                         BT_ERR("CreateDevice Fail: %s", error->message);
1665                         g_error_free(error);
1666                 }
1667
1668                 object_path = _bt_get_device_object_path(address);
1669         }
1670         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1671
1672         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1673                                 object_path, BT_DEVICE_INTERFACE);
1674         g_free(object_path);
1675         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1676
1677         if (!dbus_g_proxy_begin_call(proxy, "ConnectProfile",
1678                         (DBusGProxyCallNotify)cb,
1679                         func_data, NULL,
1680                         G_TYPE_STRING, uuid,
1681                         G_TYPE_INVALID)) {
1682                 BT_ERR("Connect Dbus Call Error");
1683                 g_object_unref(proxy);
1684                 return BLUETOOTH_ERROR_INTERNAL;
1685         }
1686         return BLUETOOTH_ERROR_NONE;
1687 }
1688
1689 int _bt_disconnect_profile(char *address, char *uuid,
1690                                                 void *cb, gpointer func_data)
1691 {
1692         char *object_path;
1693         DBusGProxy *proxy;
1694         DBusGConnection *conn;
1695
1696         conn = _bt_get_system_gconn();
1697         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1698
1699         object_path = _bt_get_device_object_path(address);
1700         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1701
1702         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1703                                 object_path, BT_DEVICE_INTERFACE);
1704         g_free(object_path);
1705         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1706
1707         if (!dbus_g_proxy_begin_call(proxy, "DisconnectProfile",
1708                         (DBusGProxyCallNotify)cb,
1709                         func_data, NULL,
1710                         G_TYPE_STRING, uuid,
1711                         G_TYPE_INVALID)) {
1712                 BT_ERR("Connect Dbus Call Error");
1713                 g_object_unref(proxy);
1714                 return BLUETOOTH_ERROR_INTERNAL;
1715         }
1716         return BLUETOOTH_ERROR_NONE;
1717 }
1718
1719 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1720                 int low_threshold, int in_range_threshold, int high_threshold)
1721 {
1722         int ret = BLUETOOTH_ERROR_NONE;
1723         DBusGProxy *proxy;
1724         GError *error = NULL;
1725         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1726
1727         BT_CHECK_PARAMETER(bd_addr, return);
1728         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1729                         bd_addr->addr[0], bd_addr->addr[1],
1730                         bd_addr->addr[2], bd_addr->addr[3],
1731                         bd_addr->addr[4], bd_addr->addr[5],
1732                         link_type);
1733         BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1734                         in_range_threshold, high_threshold);
1735
1736         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1737
1738         proxy = _bt_get_adapter_proxy();
1739         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1740
1741         if (!dbus_g_proxy_call(proxy, "EnableRssi", &error,
1742                                 G_TYPE_STRING, address,
1743                                 G_TYPE_INT, link_type,
1744                                 G_TYPE_INT, low_threshold,
1745                                 G_TYPE_INT, in_range_threshold,
1746                                 G_TYPE_INT, high_threshold,
1747                                 G_TYPE_INVALID, G_TYPE_INVALID)) {
1748                 BT_ERR("Failed to Enable RSSI");
1749                 ret = BLUETOOTH_ERROR_INTERNAL;
1750                 if (error != NULL) {
1751                                 BT_ERR("Dbus Call Error:[%s]", error->message);
1752                                 g_error_free(error);
1753                                 ret = BLUETOOTH_ERROR_INTERNAL;
1754                 }
1755         }
1756
1757         return ret;
1758 }
1759
1760 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1761                                         int link_type)
1762 {
1763         int ret = BLUETOOTH_ERROR_NONE;
1764         DBusGProxy *proxy;
1765         GError *error = NULL;
1766         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1767
1768         BT_CHECK_PARAMETER(bd_addr, return);
1769         BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1770                         bd_addr->addr[0], bd_addr->addr[1],
1771                         bd_addr->addr[2], bd_addr->addr[3],
1772                         bd_addr->addr[4], bd_addr->addr[5],
1773                         link_type);
1774
1775         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1776
1777         proxy = _bt_get_adapter_proxy();
1778         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1779
1780         if (!dbus_g_proxy_call(proxy, "GetRssiStrength", &error,
1781                                 G_TYPE_STRING, address,
1782                                 G_TYPE_INT, link_type,
1783                                 G_TYPE_INVALID, G_TYPE_INVALID)) {
1784                 BT_ERR("Failed to get Raw RSSI");
1785                 ret = BLUETOOTH_ERROR_INTERNAL;
1786                 if (error != NULL) {
1787                                 BT_ERR("Dbus Call Error:[%s]", error->message);
1788                                 g_error_free(error);
1789                                 ret = BLUETOOTH_ERROR_INTERNAL;
1790                 }
1791         }
1792
1793         return ret;
1794 }
1795
1796 int _bt_le_conn_update(unsigned char *device_address,
1797                                 guint16 interval_min, guint16 interval_max,
1798                                 guint16 latency, guint16 time_out)
1799 {
1800         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1801         gchar *device_path = NULL;
1802         GError *error = NULL;
1803         DBusGProxy *device_proxy = NULL;
1804         DBusGConnection *conn;
1805         guint32 min, max, to;
1806         int ret = BLUETOOTH_ERROR_NONE;
1807
1808         BT_DBG("+");
1809
1810         BT_CHECK_PARAMETER(device_address, return);
1811
1812         BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1813                         interval_min, interval_max, latency, time_out);
1814
1815         if (interval_min > interval_max ||
1816                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
1817                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
1818                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1819                 goto fail;
1820         }
1821
1822         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
1823         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
1824
1825         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1826                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
1827                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1828                 goto fail;
1829         }
1830
1831         to = time_out / BT_LE_CONN_TO_SPLIT;
1832
1833         if (latency > ((to / max) - 1)) {
1834                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1835                 goto fail;
1836         }
1837
1838         _bt_convert_addr_type_to_string(address, device_address);
1839
1840         BT_DBG("Remote device address: %s", address);
1841
1842         device_path = _bt_get_device_object_path(address);
1843
1844         if (device_path == NULL) {
1845                 BT_DBG("device_path NULL");
1846                 ret = BLUETOOTH_ERROR_INTERNAL;
1847                 goto fail;
1848         }
1849
1850         conn = _bt_get_system_gconn();
1851         if (conn == NULL) {
1852                 BT_DBG("conn NULL");
1853                 ret = BLUETOOTH_ERROR_INTERNAL;
1854                 goto fail;
1855         }
1856
1857         device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1858                                 device_path, BT_DEVICE_INTERFACE);
1859         g_free(device_path);
1860         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1861
1862         dbus_g_proxy_call(device_proxy, "LeConnUpdate", &error,
1863                         G_TYPE_UINT, min,
1864                         G_TYPE_UINT, max,
1865                         G_TYPE_UINT, latency,
1866                         G_TYPE_UINT, to,
1867                         G_TYPE_INVALID, G_TYPE_INVALID);
1868         if (error) {
1869                 BT_ERR("LeConnUpdate Call Error %s[%s]",
1870                                 error->message, address);
1871                 g_error_free(error);
1872                 g_object_unref(device_proxy);
1873                 return BLUETOOTH_ERROR_INTERNAL;
1874         }
1875
1876         g_object_unref(device_proxy);
1877         BT_DBG("-");
1878
1879 fail:
1880         return ret;
1881 }