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