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