Fix the TBT fail issue (bonding callback called twice)
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19 #include <gio/gio.h>
20 #include <dlog.h>
21 #include <string.h>
22 #include <syspopup_caller.h>
23 #include <vconf.h>
24 #include <bundle_internal.h>
25
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
40 #include "bt-service-pbap.h"
41
42 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
43 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
44 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
45
46 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
47 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
49 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
50 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
51 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
52 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
53 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
54
55 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT    6000    /* msec */
56
57 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL  30      /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL  50      /* msec */
59 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0       /* event */
60
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL       10      /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL       30      /* msec */
63 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY      0       /* event */
64
65 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL         80      /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL         100     /* msec */
67 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY        2       /* event */
68
69 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
70
71 typedef struct {
72         int req_id;
73         int result;
74         char *addr;
75         gboolean is_autopair;
76         GDBusProxy *device_proxy;
77         GDBusProxy *adapter_proxy;
78         void *agent;
79         unsigned short conn_type;
80         gboolean bonding_wo_discovery;
81 } bt_funcion_data_t;
82
83 typedef struct {
84         char *address;
85         char *pin_code;
86 } bt_pin_code_info_t;
87
88 typedef struct {
89         int req_id;
90         bluetooth_device_address_t bd_addr;
91         gboolean auto_connect;
92 } bt_pending_le_conn_info_s;
93
94 gboolean is_device_creating;
95 bt_funcion_data_t *bonding_info;
96 bt_funcion_data_t *searching_info;
97 bt_funcion_data_t *att_mtu_req_info;
98
99 static GSList *pin_info_list = NULL;
100 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
101 static guint pending_le_conn_timer_id = 0;
102
103 /* This HID Mouse does not support pairing precedure. need to skip it. */
104 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
105
106 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
107                                         gpointer user_data);
108
109 static int __bt_retry_bond(void);
110
111
112 /*static void __bt_decline_pair_request()
113 {
114         GVariant *out_param1;
115         GVariant *out_param2;
116         request_info_t *req_info;
117         bluetooth_device_info_t dev_info;
118         bt_remote_dev_info_t *remote_dev_info;
119         GVariant *uuid_list, *manufacture_data;
120         GVariant *param;
121
122         BT_DBG("+");
123         if (bonding_info) {
124                 req_info = _bt_get_request_info(bonding_info->req_id);
125                 if (req_info == NULL) {
126                         BT_ERR("req_info == NULL");
127                         goto done;
128                 }
129                 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
130         } else {
131                 BT_DBG("bonding_info is NULL");
132                 BT_DBG("-");
133                 return;
134         }
135
136         uuid_list =  g_variant_new_from_data((const GVariantType *)"as",
137                                                 remote_dev_info->uuids, remote_dev_info->uuid_count,
138                                                 TRUE, NULL, NULL);
139
140         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
141                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
142                                                 TRUE, NULL, NULL);
143
144         param = g_variant_new("isunsbuba{s}na{y})",
145                         bonding_info->result,
146                         bonding_info->addr,
147                         remote_dev_info->class,
148                         remote_dev_info->rssi,
149                         remote_dev_info->name,
150                         remote_dev_info->paired,
151                         remote_dev_info->connected,
152                         remote_dev_info->trust,
153                         uuid_list,
154                         remote_dev_info->manufacturer_data_len,
155                         manufacture_data);
156
157
158         //Send the event to application
159         if (remote_dev_info != NULL) {
160                 _bt_send_event(BT_ADAPTER_EVENT,
161                         BLUETOOTH_EVENT_BONDING_FINISHED,
162                         param);
163
164                 _bt_free_device_info(remote_dev_info);
165         }
166
167         if (req_info->context == NULL)
168                 goto done;
169
170         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
171         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
172                                         bonding_info->addr);
173
174         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
175                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
176
177         out_param2 = g_variant_new_from_data((const GVariantType *)"i",
178                 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
179
180
181         g_dbus_method_invocation_return_value(req_info->context,
182                         g_variant_new("@ayi", out_param1, out_param2));
183
184
185         _bt_delete_request_list(req_info->req_id);
186 done:
187
188         g_free(bonding_info->addr);
189         g_free(bonding_info);
190         bonding_info = NULL;
191
192         BT_DBG("-");
193 } */
194
195 #ifdef TIZEN_PROFILE_WEARABLE
196 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
197 {
198         int ret;
199         bundle *b;
200         retv_if(user_data == NULL, FALSE);
201
202         b = (bundle *)user_data;
203
204         ret = syspopup_launch("bt-syspopup", b);
205         if (ret < 0) {
206                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
207         } else {
208                 BT_DBG("Hurray!!! Finally Popup launched");
209                 bundle_free(b);
210         }
211         return (ret < 0) ? TRUE : FALSE;
212 }
213
214 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
215 {
216         BT_DBG("+");
217         int ret = 0;
218         bundle *b = NULL;
219         GDBusConnection *conn;
220
221         conn = _bt_gdbus_get_system_gconn();
222         if (conn == NULL)
223                 return FALSE;
224
225         b = bundle_create();
226         if (b == NULL)
227                 return FALSE;
228
229         bundle_add(b, "event-type", "unable-to-pairing");
230
231         if (result == BLUETOOTH_ERROR_TIMEOUT)
232                 bundle_add(b, "error", "timeout");
233         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
234                 bundle_add(b, "error", "authfailed");
235         else
236                 bundle_add(b, "error", "error");
237
238         ret = syspopup_launch("bt-syspopup", b);
239         if (0 > ret) {
240                 BT_ERR("Popup launch failed...retry %d \n", ret);
241                 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
242                                 b);
243         } else {
244                 bundle_free(b);
245         }
246
247         BT_DBG("-");
248         return TRUE;
249 }
250 #endif
251
252 gboolean _bt_is_device_creating(void)
253 {
254         return is_device_creating;
255 }
256
257 gboolean _bt_is_bonding_device_address(const char *address)
258 {
259         if (bonding_info == NULL || bonding_info->addr == NULL)
260                 return FALSE;
261
262         if (g_strcmp0(bonding_info->addr, address) == 0) {
263                 BT_DBG("[%s]  is bonding device", address);
264                 return TRUE;
265         }
266
267         BT_DBG("[%s]  is NOT bonding device", address);
268         return FALSE;
269 }
270
271 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
272 {
273         ret_if(bonding_info == NULL);
274         bonding_info->is_autopair = is_autopair;
275 }
276
277 void __bt_cancel_search_service_done(void)
278 {
279         int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
280         request_info_t *req_info;
281         bluetooth_device_info_t dev_info;
282         GVariant *out_param1;
283
284         ret_if(searching_info == NULL);
285
286         req_info = _bt_get_request_info(searching_info->req_id);
287         if (req_info == NULL) {
288                 BT_ERR("req_info == NULL");
289                 goto done;
290         }
291
292         if (req_info->context == NULL)
293                 goto done;
294
295         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
296         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
297                                         searching_info->addr);
298
299         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
300                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
301
302
303         g_dbus_method_invocation_return_value(req_info->context,
304                         g_variant_new("(iv)", result, out_param1));
305
306         _bt_delete_request_list(req_info->req_id);
307
308 done:
309
310         g_free(searching_info->addr);
311         g_free(searching_info);
312         searching_info = NULL;
313 }
314
315 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
316 {
317         ret_if(value == NULL);
318         ret_if(info == NULL);
319
320         gsize uuid_count = 0;
321
322         info->uuids = g_variant_dup_strv(value, &uuid_count);
323         info->uuid_count = (unsigned int)uuid_count;
324
325         BT_DBG("uuid count : %d", uuid_count);
326 }
327
328 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
329 {
330         char *object_path = NULL;
331         bt_remote_dev_info_t *dev_info;
332
333         retv_if(address == NULL, NULL);
334
335         object_path = _bt_get_device_object_path(address);
336         retv_if(object_path == NULL, NULL);
337
338         dev_info = _bt_get_remote_device_info_by_object_path(object_path);
339
340         g_free(object_path);
341         return dev_info;
342 }
343
344 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
345                                                         const char *object_path)
346 {
347         bt_remote_dev_info_t *dev_info;
348         GDBusProxy *adapter_proxy;
349         GDBusProxy *device_proxy;
350         GVariant *value;
351         GVariant *tmp_value;
352         gchar *name;
353         gchar * address;
354         GDBusConnection *conn;
355         GError *error = NULL;
356         GVariant *result = NULL;
357         GVariantIter *value_iter;
358         guint8 m_value;
359         int i = 0;
360
361         adapter_proxy = _bt_get_adapter_proxy();
362         retv_if(adapter_proxy == NULL, NULL);
363
364         retv_if(object_path == NULL, NULL);
365
366         conn = _bt_gdbus_get_system_gconn();
367         if (conn == NULL) {
368                 BT_ERR("conn == NULL");
369                 return NULL;
370         }
371
372         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
373                                         NULL, BT_BLUEZ_NAME, object_path,
374                                         BT_PROPERTIES_INTERFACE,  NULL, NULL);
375
376         retv_if(device_proxy == NULL, NULL);
377
378         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
379                                 g_variant_new("(s)", BT_DEVICE_INTERFACE),
380                                 G_DBUS_CALL_FLAGS_NONE,
381                                 -1,
382                                 NULL,
383                                 &error);
384
385         g_object_unref(device_proxy);
386
387         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
388
389         if (result != NULL) {
390                 g_variant_get(result , "(@a{sv})", &value);
391                 g_variant_unref(result);
392
393                 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
394
395                 g_variant_get(tmp_value, "s", &name);
396                 g_variant_unref(tmp_value);
397                 if (name != NULL)
398                         DBG_SECURE("Alias Name [%s]", name);
399                 else {
400                         tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
401                         g_variant_get(tmp_value, "s", &name);
402                         g_variant_unref(tmp_value);
403                 }
404
405                 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
406                 if (tmp_value) {
407                         dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
408                         g_variant_unref(tmp_value);
409                 } else {
410                         dev_info->is_alias_set = FALSE;
411                 }
412                 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
413
414                 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
415                 if (tmp_value) {
416                         dev_info->class = g_variant_get_uint32(tmp_value);
417                         g_variant_unref(tmp_value);
418                 } else
419                         dev_info->class = 0;
420
421                 tmp_value = g_variant_lookup_value(value, "Connected",  G_VARIANT_TYPE_BYTE);
422                 if (tmp_value) {
423                         dev_info->connected = g_variant_get_byte(tmp_value);
424                         g_variant_unref(tmp_value);
425                 } else
426                         dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
427                 BT_DBG("connected link : %d", dev_info->connected);
428
429                 tmp_value = g_variant_lookup_value(value, "Trusted",  G_VARIANT_TYPE_BOOLEAN);
430                 if (tmp_value) {
431                         dev_info->trust = g_variant_get_boolean(tmp_value);
432                         g_variant_unref(tmp_value);
433                 } else
434                         dev_info->trust = FALSE;
435
436                 tmp_value = g_variant_lookup_value(value, "Paired",  G_VARIANT_TYPE_BOOLEAN);
437                 if (tmp_value) {
438                         dev_info->paired = g_variant_get_boolean(tmp_value);
439                         g_variant_unref(tmp_value);
440                 } else
441                         dev_info->paired = FALSE;
442
443                 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
444                 if (tmp_value) {
445                         dev_info->rssi = g_variant_get_int16(tmp_value);
446                         g_variant_unref(tmp_value);
447                 } else
448                         dev_info->rssi = 0;
449
450                 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
451                 if (tmp_value) {
452                         dev_info->addr_type = g_variant_get_byte(tmp_value);
453                         g_variant_unref(tmp_value);
454                 } else
455                         dev_info->addr_type = 0;
456
457                 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
458                 if (tmp_value) {
459                         __bt_get_uuids(tmp_value, dev_info);
460                         g_variant_unref(tmp_value);
461                 }
462
463                 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
464                 if (tmp_value) {
465                         dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
466                         if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
467                                 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
468                                 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
469                         }
470                         g_variant_unref(tmp_value);
471                 } else
472                         dev_info->manufacturer_data_len = 0;
473
474                 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
475                 if (tmp_value) {
476                         if ((dev_info->manufacturer_data_len == 0) ||
477                                         dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
478                                 BT_ERR("manufacturer data length doesn't match");
479                                 dev_info->manufacturer_data_len = 0;
480                                 dev_info->manufacturer_data = NULL;
481                         } else {
482                                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
483                                 g_variant_get(tmp_value, "ay", &value_iter);
484                                 while (g_variant_iter_loop(value_iter, "y", &m_value))
485                                         dev_info->manufacturer_data[i++] = m_value;
486                         }
487                         g_variant_unref(tmp_value);
488                 } else {
489                         BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
490                         dev_info->manufacturer_data_len = 0;
491                         dev_info->manufacturer_data = NULL;
492                 }
493
494                 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
495                 g_variant_get(tmp_value, "s", &address);
496                 g_variant_unref(tmp_value);
497
498                 dev_info->address = g_strdup(address);
499                 dev_info->name = g_strdup(name);
500                 g_free(name);
501                 g_variant_unref(value);
502         } else {
503                 BT_ERR("result  is NULL\n");
504                 g_free(dev_info);
505                 dev_info = NULL;
506         }
507
508         return dev_info;
509 }
510
511 char *_bt_get_bonded_device_name(char *address)
512 {
513         bluetooth_device_address_t device_address = { {0} };
514         bluetooth_device_info_t dev_info;
515
516         retv_if(address == NULL, strdup(""));
517
518         _bt_convert_addr_string_to_type(device_address.addr, address);
519
520         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
521
522         _bt_get_bonded_device_info(&device_address, &dev_info);
523
524         return g_strdup(dev_info.device_name.name);
525 }
526
527 static gboolean __ignore_auto_pairing_request(const char *address)
528 {
529         gchar *buffer;
530         char **lines;
531         int i;
532         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
533         char *temp_buffer;
534         FILE *fp;
535         long size;
536         size_t result;
537
538         BT_DBG("+\n");
539
540         if (address == NULL)
541                 return FALSE;
542
543         /* Get the LAP(Lower Address part) */
544         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
545              a "," */
546         snprintf(lap_address, sizeof(lap_address), ",%s", address);
547
548         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
549
550         if (fp == NULL) {
551                 BT_ERR("fopen failed \n");
552                 return FALSE;
553         }
554
555         fseek(fp, 0, SEEK_END);
556         size = ftell(fp);
557         rewind(fp);
558
559         if (size < 0) {
560                 BT_ERR("Get file size failed \n");
561                 fclose(fp);
562                 return FALSE;
563         }
564
565         buffer = g_malloc0(sizeof(char) * size);
566         result = fread((char *)buffer, 1, size, fp);
567         fclose(fp);
568         if (result != size) {
569                 BT_ERR("Read Error\n");
570                 g_free(buffer);
571                 return FALSE;
572         }
573
574         BT_DBG("Buffer = %s\n", buffer);
575
576         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
577         g_free(buffer);
578
579         if (lines == NULL)
580                 return FALSE;
581
582         /* Write the data and insert new device data */
583         for (i = 0; lines[i] != NULL; i++) {
584                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
585                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
586                         g_free(lines[i]);
587                         lines[i] = temp_buffer;
588                 }
589         }
590         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
591         g_strfreev(lines);
592         /* Fix : NULL_RETURNS */
593         retv_if(buffer == NULL, FALSE);
594
595         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
596
597         if (fp == NULL) {
598                 BT_ERR("fopen failed \n");
599                 g_free(buffer);
600                 return FALSE;
601         }
602
603         BT_DBG("Buffer = %s\n", buffer);
604         fwrite(buffer, 1, strlen(buffer), fp);
605         fclose(fp);
606
607         g_free(buffer);
608
609         BT_DBG("-\n");
610
611         return FALSE;
612 }
613
614 static int __bt_cancel_bonding(void)
615 {
616         BT_CHECK_PARAMETER(bonding_info, return);
617         BT_CHECK_PARAMETER(bonding_info->addr, return);
618         /* First Cancel the ongoing pairing in bluez otherwise if we send
619          * pair request bluez will send inprogress and we again retry bond so
620          * this cycle continues */
621          GError *err = NULL;
622         g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
623                         NULL, G_DBUS_CALL_FLAGS_NONE,
624                         10000, NULL, &err);
625         if (err) {
626                 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
627                 g_clear_error(&err);
628                 return BLUETOOTH_ERROR_PARING_FAILED;
629         }
630         return BLUETOOTH_ERROR_NONE;
631 }
632
633 static int __bt_retry_bond(void)
634 {
635         BT_CHECK_PARAMETER(bonding_info, return);
636         BT_CHECK_PARAMETER(bonding_info->addr, return);
637
638         is_device_creating = TRUE;
639
640         g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
641                                 g_variant_new("(y)", bonding_info->conn_type),
642                                 G_DBUS_CALL_FLAGS_NONE,
643                                 BT_MAX_DBUS_TIMEOUT,
644                                 NULL,
645                                 (GAsyncReadyCallback)__bt_bond_device_cb,
646                                 NULL);
647
648         return BLUETOOTH_ERROR_NONE;
649 }
650
651
652 static int __bt_remove_and_bond(void)
653 {
654         GDBusProxy *adapter_proxy;
655         GVariant *result = NULL;
656         GError *err = NULL;
657         char *device_path = NULL;
658
659         BT_CHECK_PARAMETER(bonding_info, return);
660         BT_CHECK_PARAMETER(bonding_info->addr, return);
661
662         adapter_proxy = _bt_get_adapter_proxy();
663         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
664
665         result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
666                                 g_variant_new("(s)", bonding_info->addr),
667                                 G_DBUS_CALL_FLAGS_NONE,
668                                 -1,
669                                 NULL,
670                                 NULL);
671         if (result == NULL)
672                 return BLUETOOTH_ERROR_INTERNAL;
673
674         g_variant_get(result , "(o)", &device_path);
675         g_variant_unref(result);
676
677         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
678
679         result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
680                                 g_variant_new("(o)", device_path),
681                                 G_DBUS_CALL_FLAGS_NONE,
682                                 -1,
683                                 NULL,
684                                 &err);
685         g_free(device_path);
686         if (err != NULL) {
687                 BT_ERR("UnpairDevice Fail: %s", err->message);
688                 g_error_free(err);
689                 return BLUETOOTH_ERROR_INTERNAL;
690         }
691
692         return __bt_retry_bond();
693 }
694
695 static int __bt_cancel_and_bond(void)
696 {
697         int ret = BLUETOOTH_ERROR_NONE;
698
699         ret = _bt_agent_reply_cancellation();
700         if (ret != BLUETOOTH_ERROR_NONE) {
701                 BT_ERR("Fail to call reply cancellation");
702                 return ret;
703         }
704         return __bt_retry_bond();
705 }
706
707
708 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
709                                         gpointer user_data)
710 {
711         int result = BLUETOOTH_ERROR_NONE;
712         GError *err = NULL;
713         GVariant *reply;
714         GVariant *out_param1;
715         request_info_t *req_info;
716         bluetooth_device_info_t dev_info;
717         bt_remote_dev_info_t *remote_dev_info;
718         GVariant *manufacture_data;
719         GVariant *param;
720
721         /* Terminate ALL system popup */
722         syspopup_destroy_all();
723
724         reply = g_dbus_proxy_call_finish(proxy, res, &err);
725         if (reply)
726                 g_variant_unref(reply);
727
728         is_device_creating = FALSE;
729
730         if (bonding_info == NULL) {
731                 /* Send reply */
732                 BT_ERR("bonding_info == NULL");
733                 if (err)
734                         g_error_free(err);
735                 return;
736         }
737
738         req_info = _bt_get_request_info(bonding_info->req_id);
739         if (req_info == NULL) {
740                 BT_ERR("req_info == NULL");
741                 goto done;
742         }
743
744         if (err != NULL) {
745                 g_dbus_error_strip_remote_error(err);
746                 BT_ERR("Error occured in CreateBonding [%s]", err->message);
747
748                 if (g_strrstr(err->message, "Already Exists")) {
749                         BT_INFO("Existing Bond, remove and retry");
750                         ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
751
752                         result = BLUETOOTH_ERROR_PARING_FAILED;
753                 } else if (_bt_agent_is_canceled() ||
754                         g_strrstr(err->message, "Authentication Canceled")) {
755                         BT_INFO("Cancelled by USER");
756                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
757                 } else if (g_strrstr(err->message, "Authentication Rejected")) {
758                         BT_INFO("REJECTED");
759                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
760                 } else if (g_strrstr(err->message, "In Progress")) {
761                         BT_INFO("Bond in progress, cancel and retry");
762                         ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
763
764                         result = BLUETOOTH_ERROR_PARING_FAILED;
765                 } else if (g_strrstr(err->message, "Authentication Failed")) {
766                         BT_INFO("Authentication Failed");
767                         if (bonding_info->is_autopair == TRUE) {
768                                 _bt_set_autopair_status_in_bonding_info(FALSE);
769                                 __ignore_auto_pairing_request(bonding_info->addr);
770                         }
771                         result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
772                 } else if (g_strrstr(err->message, "Page Timeout")) {
773                         BT_INFO("Page Timeout");
774                         /* This is the special case
775                              As soon as call bluetooth_bond_device, try to cancel bonding.
776                              In this case, before completing to call 'CreatePairedDevice' method
777                              the procedure is stopped. So 'Cancle' error is not return.
778                         */
779                         result = BLUETOOTH_ERROR_HOST_DOWN;
780                 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
781                         BT_INFO("Cancel already running bonding");
782                         if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
783                                 BT_INFO("Error while Cancelling bonding");
784                                 /* we need to unref proxy so continue */
785                         }
786                         result = BLUETOOTH_ERROR_INTERNAL;
787                 } else if (g_strrstr(err->message, "Connection Timeout")) {
788                         /* Pairing request timeout */
789                         result = BLUETOOTH_ERROR_TIMEOUT;
790                 } else if (g_strrstr(err->message, "Authentication Timeout")) {
791                         /* Pairing request timeout */
792                         result = BLUETOOTH_ERROR_TIMEOUT;
793                 } else {
794                         BT_DBG("Default case");
795                         result = BLUETOOTH_ERROR_PARING_FAILED;
796                 }
797         }
798
799         if (result == BLUETOOTH_ERROR_PARING_FAILED ||
800                         result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
801                         result == BLUETOOTH_ERROR_TIMEOUT ||
802                         result == BLUETOOTH_ERROR_HOST_DOWN) {
803 #ifdef TIZEN_PROFILE_WEARABLE
804                 int is_sw_running = 0;
805
806                 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
807                         BT_ERR("vconf_get_int for setup wizard state failed");
808
809                 if (!is_sw_running)
810                         __bt_launch_unable_to_pairing_syspopup(result);
811                 else
812                         BT_ERR("Unable to pair");
813 #endif
814                 bonding_info->result = result;
815         }
816
817         g_object_unref(proxy);
818         bonding_info->device_proxy = NULL;
819
820         if (result != BLUETOOTH_ERROR_NONE) {
821                 if (bonding_info->bonding_wo_discovery) {
822                         GDBusProxy *adapter_proxy;
823                         GVariant *ret = NULL;
824                         GError *error = NULL;
825                         char *device_path;
826
827                         BT_ERR("Bond was tried without discovery. Remove it");
828
829                         adapter_proxy = _bt_get_adapter_proxy();
830                         if (adapter_proxy == NULL) {
831                                 BT_ERR("Cannot get adapter_proxy");
832                                 goto dbus_return;
833                         }
834
835                         device_path = _bt_get_device_object_path(bonding_info->addr);
836                         if (device_path == NULL) {
837                                 BT_ERR("Cannot get device path");
838                                 goto dbus_return;
839                         }
840
841                         ret = g_dbus_proxy_call_sync(adapter_proxy,
842                                         "RemoveDevice",
843                                         g_variant_new("(o)", device_path),
844                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL,
845                                         &error);
846                         if (error != NULL) {
847                                 BT_ERR("RemoveDevice Fail: %s", error->message);
848                                 g_clear_error(&error);
849                         }
850
851                         if (ret)
852                                 g_variant_unref(ret);
853
854                         g_free(device_path);
855                 }
856
857                 goto dbus_return;
858         }
859
860         remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
861         if (!remote_dev_info)
862                 goto dbus_return;
863
864         GVariant *uuids = NULL;
865         GVariantBuilder *builder = NULL;
866         int i = 0;
867         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
868         for (i = 0; i < remote_dev_info->uuid_count; i++) {
869                 g_variant_builder_add(builder, "s",
870                         remote_dev_info->uuids[i]);
871         }
872         uuids = g_variant_new("as", builder);
873         g_variant_builder_unref(builder);
874         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
875                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
876                                                 TRUE, NULL, NULL);
877
878         param = g_variant_new("(isunsbub@asn@ay)",
879                         result,
880                         bonding_info->addr,
881                         remote_dev_info->class,
882                         remote_dev_info->rssi,
883                         remote_dev_info->name,
884                         remote_dev_info->paired,
885                         remote_dev_info->connected,
886                         remote_dev_info->trust,
887                         uuids,
888                         remote_dev_info->manufacturer_data_len,
889                         manufacture_data);
890
891
892         /* Send the event to application */
893         _bt_send_event(BT_ADAPTER_EVENT,
894                 BLUETOOTH_EVENT_BONDING_FINISHED,
895                 param);
896
897         _bt_free_device_info(remote_dev_info);
898
899 dbus_return:
900         if (req_info->context == NULL)
901                 goto done;
902
903         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
904         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
905                                         bonding_info->addr);
906
907         if (_bt_adapter_get_status() != BT_ACTIVATED)
908                 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
909
910         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
911                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
912
913         g_dbus_method_invocation_return_value(req_info->context,
914                         g_variant_new("(iv)", result, out_param1));
915
916         g_variant_unref(out_param1);
917
918         _bt_delete_request_list(req_info->req_id);
919 done:
920         if (err)
921                 g_error_free(err);
922
923         _bt_agent_set_canceled(FALSE);
924
925
926         g_free(bonding_info->addr);
927         g_free(bonding_info);
928         bonding_info = NULL;
929 }
930
931 int _bt_bond_device(int request_id,
932                 bluetooth_device_address_t *device_address,
933                 unsigned short conn_type, GArray **out_param1)
934 {
935         GDBusProxy *proxy;
936         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
937         bluetooth_device_info_t dev_info;
938
939         GDBusConnection *conn;
940         char *device_path = NULL;
941         GDBusProxy *adapter_proxy;
942         GError *error = NULL;
943         gboolean bonding_wo_discovery = FALSE;
944
945         BT_CHECK_PARAMETER(device_address, return);
946
947         if (bonding_info) {
948                 BT_ERR("Bonding in progress");
949
950                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
951                 memcpy(dev_info.device_address.addr, device_address->addr,
952                                 BLUETOOTH_ADDRESS_LENGTH);
953
954                 g_array_append_vals(*out_param1, &dev_info,
955                                 sizeof(bluetooth_device_info_t));
956
957                 return BLUETOOTH_ERROR_DEVICE_BUSY;
958         }
959
960         conn = _bt_gdbus_get_system_gconn();
961         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
962
963         _bt_convert_addr_type_to_string(address, device_address->addr);
964
965         device_path = _bt_get_device_object_path(address);
966
967         if (device_path == NULL) {
968                 BT_ERR("No searched device");
969                 GVariant *ret = NULL;
970                 adapter_proxy = _bt_get_adapter_proxy();
971                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
972
973                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
974                                         g_variant_new("(s)", address),
975                                         G_DBUS_CALL_FLAGS_NONE,
976                                         -1,
977                                         NULL,
978                                         &error);
979
980                 if (error != NULL) {
981                         BT_ERR("CreateDevice Fail: %s", error->message);
982                         g_clear_error(&error);
983                 }
984                 if (ret)
985                         g_variant_unref(ret);
986                 device_path = _bt_get_device_object_path(address);
987                 if (device_path == NULL) {
988                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
989                         memcpy(dev_info.device_address.addr, device_address->addr,
990                                         BLUETOOTH_ADDRESS_LENGTH);
991
992                         g_array_append_vals(*out_param1, &dev_info,
993                                         sizeof(bluetooth_device_info_t));
994
995                         return BLUETOOTH_ERROR_NOT_PAIRED;
996                 } else {
997                         BT_INFO("device_path is created[%s]", device_path);
998                 }
999                 bonding_wo_discovery = TRUE;
1000         }
1001
1002         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1003                                                                 NULL, BT_BLUEZ_NAME,
1004                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1005
1006         g_free(device_path);
1007         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1008
1009         bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1010         bonding_info->addr = g_strdup(address);
1011         bonding_info->req_id = request_id;
1012
1013         bonding_info->device_proxy = proxy;
1014         bonding_info->conn_type = conn_type;
1015         bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1016
1017         is_device_creating = TRUE;
1018
1019         g_dbus_proxy_call(proxy, "Pair",
1020                                 g_variant_new("(y)", conn_type),
1021                                 G_DBUS_CALL_FLAGS_NONE,
1022                                 BT_MAX_DBUS_TIMEOUT,
1023                                 NULL,
1024                                 (GAsyncReadyCallback)__bt_bond_device_cb,
1025                                 NULL);
1026
1027 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1028
1029         return BLUETOOTH_ERROR_NONE;
1030 /*fail:
1031         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1032         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1033                                         bonding_info->addr);
1034
1035         g_array_append_vals(*out_param1, &dev_info,
1036                                 sizeof(bluetooth_device_info_t));
1037
1038         is_device_creating = FALSE;
1039
1040         g_free(bonding_info->addr);
1041         g_free(bonding_info);
1042         bonding_info = NULL;
1043
1044         return BLUETOOTH_ERROR_INTERNAL;*/
1045 }
1046
1047 int _bt_cancel_bonding(void)
1048 {
1049         int ret = BLUETOOTH_ERROR_NONE;
1050
1051         retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1052
1053         ret = _bt_agent_reply_cancellation();
1054         if (ret != BLUETOOTH_ERROR_NONE) {
1055                 BT_ERR("Fail to call reply cancellation");
1056                 return ret;
1057         }
1058
1059         _bt_agent_set_canceled(TRUE);
1060
1061         return BLUETOOTH_ERROR_NONE;
1062 }
1063
1064 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1065                                         gpointer user_data)
1066 {
1067         GError *err = NULL;
1068         GVariant *reply;
1069         GVariant *out_param1;
1070         int result = BLUETOOTH_ERROR_NONE;
1071         bt_funcion_data_t *unbonding_info;
1072         bluetooth_device_info_t dev_info;
1073         request_info_t *req_info;
1074
1075         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1076         if (reply)
1077                 g_variant_unref(reply);
1078
1079         unbonding_info = user_data;
1080
1081         if (unbonding_info == NULL) {
1082                 /* Send reply */
1083                 BT_ERR("unbonding_info == NULL");
1084                 goto done;
1085         }
1086
1087         req_info = _bt_get_request_info(unbonding_info->req_id);
1088         if (req_info == NULL) {
1089                 BT_ERR("req_info == NULL");
1090                 goto done;
1091         }
1092
1093         if (err != NULL) {
1094                 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1095                 result = BLUETOOTH_ERROR_INTERNAL;
1096         }
1097
1098         if (req_info->context == NULL)
1099                 goto done;
1100
1101         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1102         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1103                                         unbonding_info->addr);
1104
1105         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1106                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1107
1108         g_dbus_method_invocation_return_value(req_info->context,
1109                         g_variant_new("(iv)", result, out_param1));
1110
1111
1112         _bt_delete_request_list(req_info->req_id);
1113
1114 done:
1115         if (err)
1116                 g_error_free(err);
1117
1118         if (unbonding_info) {
1119                 g_free(unbonding_info->addr);
1120                 g_free(unbonding_info);
1121         }
1122 }
1123
1124 int _bt_unbond_device(int request_id,
1125                         bluetooth_device_address_t *device_address,
1126                         GArray **out_param1)
1127 {
1128         char *device_path = NULL;
1129         bt_funcion_data_t *unbonding_info;
1130         GDBusProxy *adapter_proxy = NULL;
1131         GDBusProxy *device_proxy = NULL;
1132         GDBusConnection *conn;
1133         int result = BLUETOOTH_ERROR_INTERNAL;
1134         bluetooth_device_info_t dev_info;
1135         GError *error = NULL;
1136         GVariant *ret = NULL;
1137
1138         BT_CHECK_PARAMETER(device_address, return);
1139
1140         adapter_proxy = _bt_get_adapter_proxy();
1141         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1142
1143         /* allocate user data so that it can be retrieved in callback */
1144         unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1145         unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1146         unbonding_info->req_id = request_id;
1147
1148         _bt_convert_addr_type_to_string(unbonding_info->addr,
1149                                         device_address->addr);
1150
1151         device_path = _bt_get_device_object_path(unbonding_info->addr);
1152
1153         if (device_path == NULL) {
1154                 BT_ERR("No paired device");
1155                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1156                 goto fail;
1157         }
1158
1159         conn = _bt_gdbus_get_system_gconn();
1160         if (conn == NULL) {
1161                 BT_ERR("conn is NULL");
1162                 result = BLUETOOTH_ERROR_INTERNAL;
1163                 goto fail;
1164         }
1165
1166         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1167                                                                 NULL, BT_BLUEZ_NAME,
1168                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1169
1170         if (device_proxy != NULL) {
1171
1172                 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1173                                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1174                                         G_DBUS_CALL_FLAGS_NONE,
1175                                         -1,
1176                                         NULL,
1177                                         &error);
1178                 if (error) {
1179                         BT_ERR("Getting property failed: [%s]\n", error->message);
1180                         g_error_free(error);
1181                         result = BLUETOOTH_ERROR_NOT_PAIRED;
1182                         goto fail;
1183                 } else {
1184                         if (!ret) {
1185                                 BT_ERR("No paired device");
1186                                 g_object_unref(device_proxy);
1187                                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1188                                 goto fail;
1189                         }
1190                         g_variant_unref(ret);
1191                 }
1192                 g_object_unref(device_proxy);
1193         }
1194
1195         g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1196                                 g_variant_new("(o)", device_path),
1197                                 G_DBUS_CALL_FLAGS_NONE,
1198                                 BT_MAX_DBUS_TIMEOUT,
1199                                 NULL,
1200                                 (GAsyncReadyCallback)__bt_unbond_cb,
1201                                 unbonding_info);
1202
1203         g_free(device_path);
1204         return BLUETOOTH_ERROR_NONE;
1205
1206 fail:
1207         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1208         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1209                                         unbonding_info->addr);
1210
1211         g_array_append_vals(*out_param1, &dev_info,
1212                                 sizeof(bluetooth_device_info_t));
1213         g_free(device_path);
1214         g_free(unbonding_info->addr);
1215         g_free(unbonding_info);
1216         return result;
1217 }
1218
1219 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1220                                         gpointer user_data)
1221 {
1222         GError *err = NULL;
1223         GVariant *reply;
1224         GVariant *out_param1;
1225         int result = BLUETOOTH_ERROR_NONE;
1226         bluetooth_device_info_t dev_info;
1227         bt_remote_dev_info_t *remote_dev_info;
1228         request_info_t *req_info;
1229         GVariant *uuid_list, *manufacture_data;
1230         GVariant *param;
1231         GVariantBuilder *builder = NULL;
1232         int i = 0;
1233
1234         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1235         if (reply)
1236                 g_variant_unref(reply);
1237
1238         g_object_unref(proxy);
1239
1240         if (searching_info == NULL) {
1241                 /* Send reply */
1242                 BT_ERR("unbonding_info == NULL");
1243                 goto done;
1244         }
1245
1246         req_info = _bt_get_request_info(searching_info->req_id);
1247         if (req_info == NULL) {
1248                 BT_ERR("req_info == NULL");
1249                 goto done;
1250         }
1251
1252         if (err != NULL) {
1253                 g_dbus_error_strip_remote_error(err);
1254                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1255
1256                 if (g_strrstr("Operation canceled", err->message))
1257                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1258                 else if (g_strrstr("In Progress", err->message))
1259                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1260                 else if (g_strrstr("Host is down", err->message))
1261                         result = BLUETOOTH_ERROR_HOST_DOWN;
1262                 else
1263                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1264
1265                 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1266                      result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1267                         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1268                         if (remote_dev_info && remote_dev_info->uuids != NULL &&
1269                              remote_dev_info->uuid_count > 0) {
1270                                 result = BLUETOOTH_ERROR_NONE;
1271                                 goto event;
1272                         }
1273                         _bt_free_device_info(remote_dev_info);
1274                 }
1275                 goto dbus_return;
1276         }
1277
1278         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1279         if (!remote_dev_info)
1280                 goto dbus_return;
1281
1282 event:
1283         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1284         for (i = 0; i < remote_dev_info->uuid_count; i++) {
1285                 g_variant_builder_add(builder, "s",
1286                         remote_dev_info->uuids[i]);
1287         }
1288         uuid_list = g_variant_new("as", builder);
1289         g_variant_builder_unref(builder);
1290         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1291                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1292                                                 TRUE, NULL, NULL);
1293
1294         param = g_variant_new("(isunsbub@asn@ay)",
1295                         result,
1296                         searching_info->addr,
1297                         remote_dev_info->class,
1298                         remote_dev_info->rssi,
1299                         remote_dev_info->name,
1300                         remote_dev_info->paired,
1301                         remote_dev_info->connected,
1302                         remote_dev_info->trust,
1303                         uuid_list,
1304                         remote_dev_info->manufacturer_data_len,
1305                         manufacture_data);
1306
1307         /* Send the event to application */
1308         _bt_send_event(BT_ADAPTER_EVENT,
1309                 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1310                 param);
1311
1312         _bt_free_device_info(remote_dev_info);
1313
1314 dbus_return:
1315         if (req_info->context == NULL)
1316                 goto done;
1317
1318         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1319         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1320                                         searching_info->addr);
1321
1322         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1323                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1324
1325         g_dbus_method_invocation_return_value(req_info->context,
1326                         g_variant_new("(iv)", result, out_param1));
1327
1328
1329         _bt_delete_request_list(req_info->req_id);
1330 done:
1331         if (err)
1332                 g_error_free(err);
1333
1334         if (searching_info) {
1335                 g_free(searching_info->addr);
1336                 g_free(searching_info);
1337                 searching_info = NULL;
1338         }
1339 }
1340
1341 int _bt_search_device(int request_id,
1342                         bluetooth_device_address_t *device_address)
1343 {
1344         char *device_path = NULL;
1345         GDBusProxy *device_proxy = NULL;
1346         GDBusConnection *conn;
1347
1348         GDBusProxy *adapter_proxy;
1349         int result = BLUETOOTH_ERROR_INTERNAL;
1350
1351         BT_CHECK_PARAMETER(device_address, return);
1352
1353         if (bonding_info) {
1354                 BT_ERR("Bonding in progress");
1355                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1356         }
1357
1358         if (searching_info) {
1359                 BT_ERR("Service searching in progress");
1360                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1361         }
1362
1363         adapter_proxy = _bt_get_adapter_proxy();
1364         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1365
1366         /* allocate user data so that it can be retrieved in callback */
1367         searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1368         searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1369         searching_info->req_id = request_id;
1370
1371         _bt_convert_addr_type_to_string(searching_info->addr,
1372                                         device_address->addr);
1373
1374         conn = _bt_gdbus_get_system_gconn();
1375         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1376
1377
1378         device_path = _bt_get_device_object_path(searching_info->addr);
1379
1380         if (device_path == NULL) {
1381                 BT_ERR("No paired device");
1382                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1383                 goto fail;
1384         }
1385
1386         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1387                                                                 NULL, BT_BLUEZ_NAME,
1388                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1389         g_free(device_path);
1390         if (device_proxy == NULL) {
1391                 result = BLUETOOTH_ERROR_INTERNAL;
1392                 goto fail;
1393         }
1394
1395         g_dbus_proxy_call(device_proxy, "DiscoverServices",
1396                         g_variant_new("(s)", ""),
1397                         G_DBUS_CALL_FLAGS_NONE,
1398                         BT_MAX_DBUS_TIMEOUT,
1399                         NULL,
1400                         (GAsyncReadyCallback)__bt_discover_cb,
1401                         searching_info);
1402
1403         searching_info->device_proxy = device_proxy;
1404
1405         return BLUETOOTH_ERROR_NONE;
1406 fail:
1407
1408         g_free(searching_info->addr);
1409         g_free(searching_info);
1410         searching_info = NULL;
1411         return result;
1412 }
1413
1414 int _bt_cancel_search_device(void)
1415 {
1416         GVariant *ret = NULL;
1417         GError *err = NULL;
1418
1419         retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1420
1421         if (searching_info->device_proxy) {
1422                 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1423                                 NULL,
1424                                 G_DBUS_CALL_FLAGS_NONE,
1425                                 -1,
1426                                 NULL,
1427                                 &err);
1428                 if (ret)
1429                         g_variant_unref(ret);
1430         }
1431         __bt_cancel_search_service_done();
1432
1433         return BLUETOOTH_ERROR_NONE;
1434 }
1435
1436 int _bt_set_alias(bluetooth_device_address_t *device_address,
1437                                       const char *alias)
1438 {
1439         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1440         gchar *device_path = NULL;
1441         GDBusProxy *adapter_proxy;
1442         GDBusProxy *device_proxy;
1443         GVariant *ret = NULL;
1444         GError *error = NULL;
1445         GDBusConnection *conn;
1446
1447         BT_CHECK_PARAMETER(device_address, return);
1448         BT_CHECK_PARAMETER(alias, return);
1449
1450         adapter_proxy = _bt_get_adapter_proxy();
1451         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1452
1453         conn = _bt_gdbus_get_system_gconn();
1454         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1455
1456         _bt_convert_addr_type_to_string(address, device_address->addr);
1457
1458         device_path = _bt_get_device_object_path(address);
1459
1460         if (device_path == NULL) {
1461                 BT_ERR("No paired device");
1462                 return BLUETOOTH_ERROR_NOT_PAIRED;
1463         }
1464
1465         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1466                                                                 NULL, BT_BLUEZ_NAME,
1467                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1468
1469         g_free(device_path);
1470         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1471
1472         ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1473                                 g_variant_new("(ssv)", BT_DEVICE_INTERFACE,  "Alias", g_variant_new("s", alias)),
1474                                 G_DBUS_CALL_FLAGS_NONE,
1475                                 -1,
1476                                 NULL,
1477                                 &error);
1478         if (ret)
1479                 g_variant_unref(ret);
1480
1481         g_object_unref(device_proxy);
1482
1483         if (error) {
1484                  BT_ERR("SetProperty error: [%s]", error->message);
1485                  g_error_free(error);
1486                  return BLUETOOTH_ERROR_INTERNAL;
1487         }
1488
1489         return BLUETOOTH_ERROR_NONE;
1490 }
1491
1492 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1493                                       gboolean authorize)
1494 {
1495         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1496         gchar *device_path = NULL;
1497         GDBusProxy *device_proxy;
1498         gboolean previous_value;
1499         GError *error = NULL;
1500         GDBusConnection *conn;
1501         GVariant *result = NULL;
1502         GVariant *temp = NULL;
1503         int ret = BLUETOOTH_ERROR_NONE;
1504
1505         BT_CHECK_PARAMETER(device_address, return);
1506
1507         conn = _bt_gdbus_get_system_gconn();
1508         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1509
1510         _bt_convert_addr_type_to_string(address, device_address->addr);
1511
1512         device_path = _bt_get_device_object_path(address);
1513
1514         if (device_path == NULL) {
1515                 BT_ERR("No paired device");
1516                 return BLUETOOTH_ERROR_NOT_PAIRED;
1517         }
1518
1519         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1520                                                                 NULL, BT_BLUEZ_NAME,
1521                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1522
1523         g_free(device_path);
1524         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1525
1526         result = g_dbus_proxy_call_sync(device_proxy, "Get",
1527                                 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1528                                 G_DBUS_CALL_FLAGS_NONE,
1529                                 -1,
1530                                 NULL,
1531                                 &error);
1532         if (error != NULL) {
1533                 BT_ERR("Getting property failed: [%s]\n", error->message);
1534                 g_error_free(error);
1535                 g_object_unref(device_proxy);
1536                 return BLUETOOTH_ERROR_INTERNAL;
1537         }
1538
1539         g_variant_get(result, "(v)", &temp);
1540         previous_value = g_variant_get_boolean(temp);
1541         g_variant_unref(temp);
1542         g_variant_unref(result);
1543         /* If the input is same with previous value, return error. */
1544         if (previous_value == authorize) {
1545                 BT_ERR("Same value: %d", previous_value);
1546                 g_object_unref(device_proxy);
1547                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1548                 goto done;
1549         }
1550
1551         result = g_dbus_proxy_call_sync(device_proxy, "Set",
1552                                  g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1553                                  G_DBUS_CALL_FLAGS_NONE,
1554                                  -1,
1555                                  NULL,
1556                                  &error);
1557
1558         g_object_unref(device_proxy);
1559         if (error) {
1560                  BT_ERR("SetProperty error: [%s]", error->message);
1561                  g_error_free(error);
1562                  ret = BLUETOOTH_ERROR_INTERNAL;
1563         }
1564 done:
1565         if (result)
1566                 g_variant_unref(result);
1567
1568         return ret;
1569 }
1570
1571 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1572                         gboolean *is_connected)
1573 {
1574         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1575         char *object_path = NULL;
1576
1577         GDBusProxy *device_proxy;
1578         GError *error = NULL;
1579         GVariant *value;
1580         GVariant *tmp_value;
1581         GDBusConnection *conn;
1582         GVariant *result = NULL;
1583         int ret = BLUETOOTH_ERROR_NONE;
1584
1585         BT_CHECK_PARAMETER(device_address, return);
1586
1587         conn = _bt_gdbus_get_system_gconn();
1588         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1589
1590         _bt_convert_addr_type_to_string(address, device_address->addr);
1591
1592         object_path = _bt_get_device_object_path(address);
1593         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1594
1595         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1596                         NULL, BT_BLUEZ_NAME, object_path,
1597                         BT_PROPERTIES_INTERFACE,  NULL, NULL);
1598         g_free(object_path);
1599         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1600
1601         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1602                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1603                         G_DBUS_CALL_FLAGS_NONE,
1604                         -1,
1605                         NULL,
1606                         &error);
1607         if (result == NULL) {
1608                 if (error != NULL) {
1609                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1610                         g_error_free(error);
1611                 }
1612                 g_object_unref(device_proxy);
1613                 return BLUETOOTH_ERROR_INTERNAL;
1614         }
1615
1616         g_variant_get(result , "(@a{sv})", &value);
1617         g_variant_unref(result);
1618
1619         tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1620         if (tmp_value == NULL) {
1621                 g_object_unref(device_proxy);
1622                 g_variant_unref(value);
1623                 return BLUETOOTH_ERROR_INTERNAL;
1624         }
1625
1626         *is_connected = g_variant_get_boolean(tmp_value);
1627
1628         BT_DBG("gatt is connected : %d", *is_connected);
1629
1630         g_variant_unref(tmp_value);
1631         g_variant_unref(value);
1632         g_object_unref(device_proxy);
1633
1634         return ret;
1635 }
1636
1637 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1638                         int connection_type, gboolean *is_connected)
1639 {
1640         char *object_path = NULL;
1641         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1642         char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1643         GDBusProxy *device_proxy = NULL;
1644         GDBusProxy *adapter_proxy = NULL;
1645         GDBusConnection *conn;
1646         GError *error = NULL;
1647         GVariant *tmp_value = NULL;
1648         GVariant *value = NULL;
1649         GVariant *result = NULL;
1650         char *uuid = NULL;
1651
1652         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1653         retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1654
1655         _bt_convert_addr_type_to_string(address, device_address->addr);
1656         _bt_convert_addr_string_to_secure_string(secure_address, address);
1657
1658         *is_connected = FALSE;
1659         BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1660
1661         if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1662                 return _bt_rfcomm_is_device_connected(device_address,
1663                                                 is_connected);
1664         else if (connection_type == BLUETOOTH_GATT_SERVICE)
1665                 return _bt_is_gatt_connected(device_address, is_connected);
1666         else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1667                 return _bt_pbap_is_connected(device_address, is_connected);
1668
1669         adapter_proxy = _bt_get_adapter_proxy();
1670         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1671
1672         conn = _bt_gdbus_get_system_gconn();
1673         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1674
1675         _bt_convert_addr_type_to_string(address, device_address->addr);
1676
1677         if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1678                 object_path = _bt_get_adapter_path();
1679                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1680                                                                         NULL, BT_BLUEZ_NAME,
1681                                                                         object_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
1682                 g_free(object_path);
1683                 if (device_proxy == NULL) {
1684                         BT_DBG("Device don't have this service");
1685                         return BLUETOOTH_ERROR_INTERNAL;
1686                 }
1687
1688                 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1689                                         g_variant_new("(s)", address),
1690                                         G_DBUS_CALL_FLAGS_NONE,
1691                                         -1,
1692                                         NULL,
1693                                         &error);
1694
1695                 if (result == NULL) {
1696                         BT_ERR("[GetProperties] Error occured in Proxy call");
1697                         if (error) {
1698                                 BT_ERR("%s", error->message);
1699                                 g_error_free(error);
1700                         }
1701                         *is_connected = FALSE;
1702                         g_object_unref(device_proxy);
1703                         return BLUETOOTH_ERROR_NONE;
1704                 }
1705                 g_variant_get(result , "(@a{sv})", &value);
1706                 g_variant_unref(result);
1707
1708                 if (value) {
1709                         tmp_value = g_variant_lookup_value(value,
1710                                                         "Connected",
1711                                                         G_VARIANT_TYPE_BOOLEAN);
1712                         if (tmp_value) {
1713                                 *is_connected = g_variant_get_boolean(tmp_value);
1714                                 g_variant_unref(tmp_value);
1715                         }
1716                         g_variant_unref(value);
1717                 }
1718         } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1719                 return _bt_is_network_connected(_bt_get_net_conn(),
1720                                                 device_address->addr, is_connected);
1721         } else {
1722                 uuid = _bt_get_profile_uuid128(connection_type);
1723                 if (uuid == NULL) {
1724                         BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1725                         return BLUETOOTH_ERROR_INTERNAL;
1726                 }
1727                 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1728
1729                 object_path = _bt_get_device_object_path(address);
1730                 if (!object_path) {
1731                         BT_ERR("object_path is NULL");
1732                         g_free(uuid);
1733                         return BLUETOOTH_ERROR_NOT_PAIRED;
1734                 }
1735
1736                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1737                                                                         NULL, BT_BLUEZ_NAME,
1738                                                                         object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1739                 g_free(object_path);
1740                 if (device_proxy == NULL) {
1741                         BT_DBG("Device don't have this service");
1742                         g_free(uuid);
1743                         return BLUETOOTH_ERROR_INTERNAL;
1744                 }
1745
1746                 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1747                                         g_variant_new("(s)", uuid),
1748                                         G_DBUS_CALL_FLAGS_NONE,
1749                                         -1,
1750                                         NULL,
1751                                         &error);
1752
1753                 if (result == NULL) {
1754                         BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1755                         if (error) {
1756                                 BT_ERR("%s", error->message);
1757                                 if (g_strrstr(error->message, "Not Connected"))
1758                                         BT_DBG("Not connected");
1759                                 g_error_free(error);
1760                         }
1761                         *is_connected = FALSE;
1762                         g_object_unref(device_proxy);
1763                         g_free(uuid);
1764                         return BLUETOOTH_ERROR_NONE;
1765                 }
1766                 g_variant_get(result, "(b)", is_connected);
1767                 g_free(uuid);
1768                 g_variant_unref(result);
1769         }
1770
1771         g_object_unref(device_proxy);
1772         return BLUETOOTH_ERROR_NONE;
1773 }
1774
1775 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1776                         bluetooth_connected_link_t *connected)
1777 {
1778         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779         char *object_path = NULL;
1780
1781         GDBusProxy *device_proxy;
1782         GError *error = NULL;
1783         GDBusConnection *conn;
1784         GVariant *tmp_value = NULL;
1785         GVariant *value = NULL;
1786         GVariant *result = NULL;
1787
1788         BT_CHECK_PARAMETER(device_address, return);
1789
1790         conn = _bt_gdbus_get_system_gconn();
1791         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1792
1793         _bt_convert_addr_type_to_string(address, device_address->addr);
1794
1795         object_path = _bt_get_device_object_path(address);
1796         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1797
1798         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1799                                                                 NULL, BT_BLUEZ_NAME,
1800                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1801         g_free(object_path);
1802         if (device_proxy == NULL) {
1803                 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1804                 return BLUETOOTH_ERROR_NONE;
1805         }
1806
1807         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1808                                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1809                                         G_DBUS_CALL_FLAGS_NONE,
1810                                         -1,
1811                                         NULL,
1812                                         &error);
1813
1814         if (error != NULL) {
1815                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1816                 g_error_free(error);
1817                 g_object_unref(device_proxy);
1818                 return BLUETOOTH_ERROR_INTERNAL;
1819         }
1820
1821         g_variant_get(result , "(@a{sv})", &value);
1822         g_variant_unref(result);
1823
1824         tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1825         if (tmp_value != NULL) {
1826                 *connected = g_variant_get_byte(tmp_value);
1827                 g_variant_unref(tmp_value);
1828                 g_object_unref(device_proxy);
1829                 return BLUETOOTH_ERROR_NONE;
1830         } else  {
1831                 BT_ERR("g_variant value is NULL");
1832                 return BLUETOOTH_ERROR_INTERNAL;
1833         }
1834 }
1835
1836 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1837                 gpointer user_data)
1838 {
1839         GError *err = NULL;
1840         GVariant *out_param1;
1841         GVariant *reply;
1842         int result = BLUETOOTH_ERROR_NONE;
1843         bt_function_data_t *func_data = user_data;
1844         request_info_t *req_info = NULL;
1845         bluetooth_device_address_t device_addr = { {0} };
1846
1847         reply = g_dbus_proxy_call_finish(proxy, res, &err);
1848         g_object_unref(proxy);
1849
1850         if (reply == NULL) {
1851                 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1852                 if (err) {
1853                         BT_ERR("Error: %s", err->message);
1854                         g_clear_error(&err);
1855                 }
1856                 result = BLUETOOTH_ERROR_INTERNAL;
1857         } else {
1858                 g_variant_unref(reply);
1859         }
1860
1861         if (func_data == NULL) {
1862                 BT_ERR("func_data is NULL");
1863                 goto done;
1864         }
1865
1866         req_info = _bt_get_request_info(func_data->req_id);
1867         if (req_info == NULL) {
1868                 BT_ERR("req_info is NULL");
1869                 goto done;
1870         }
1871
1872         if (req_info->context == NULL) {
1873                 BT_ERR("req_info->context is NULL");
1874                 goto done;
1875         }
1876
1877         _bt_convert_addr_string_to_type(device_addr.addr,
1878                         (const char *)func_data->address);
1879
1880         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1881                         &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1882                         NULL, NULL);
1883
1884         g_dbus_method_invocation_return_value(req_info->context,
1885                         g_variant_new("(iv)", result, out_param1));
1886
1887 done:
1888         if (req_info)
1889                 _bt_delete_request_list(req_info->req_id);
1890
1891         if (func_data) {
1892                 g_free(func_data->address);
1893                 g_free(func_data);
1894         }
1895 }
1896
1897 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1898         gboolean auto_connect)
1899 {
1900         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1901         char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1902         gchar *device_path = NULL;
1903         GDBusProxy *device_proxy = NULL;
1904         GDBusConnection *conn;
1905         int ret = BLUETOOTH_ERROR_NONE;
1906         bt_function_data_t *func_data;
1907
1908         BT_CHECK_PARAMETER(bd_addr, return);
1909
1910         conn = _bt_gdbus_get_system_gconn();
1911         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1912
1913         _bt_convert_addr_type_to_string(device_address,
1914                         (unsigned char *)bd_addr->addr);
1915         device_path = _bt_get_device_object_path(device_address);
1916         if (device_path == NULL) {
1917                 BT_ERR_C("device_path NULL : [%s]", device_address);
1918                 ret = BLUETOOTH_ERROR_INTERNAL;
1919                 return ret;
1920         }
1921
1922         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1923                         NULL, BT_BLUEZ_NAME,
1924                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1925         g_free(device_path);
1926         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1927
1928         func_data = g_malloc0(sizeof(bt_function_data_t));
1929         func_data->address = g_strdup(device_address);
1930         if (func_data->address == NULL) {
1931                 BT_ERR("Unable to allocate memory for address");
1932                 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1933                 goto fail;
1934         }
1935
1936         _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1937         BT_INFO("Connect LE [%s]", secure_address);
1938
1939         func_data->req_id = req_id;
1940
1941         g_dbus_proxy_call(device_proxy, "ConnectLE",
1942                         g_variant_new("(b)", auto_connect),
1943                         G_DBUS_CALL_FLAGS_NONE,
1944                         BT_MAX_DBUS_TIMEOUT,
1945                         NULL,
1946                         (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1947
1948         return ret;
1949
1950 fail:
1951         if (device_proxy)
1952                 g_object_unref(device_proxy);
1953
1954         g_free(func_data->address);
1955         g_free(func_data);
1956
1957         return ret;
1958 }
1959
1960 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1961 {
1962         BT_INFO("Try to initiate pending LE connection");
1963
1964         pending_le_conn_timer_id = 0;
1965
1966         __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1967                 &pending_le_conn_info->bd_addr,
1968                 pending_le_conn_info->auto_connect);
1969
1970         g_free(pending_le_conn_info);
1971         pending_le_conn_info = NULL;
1972
1973         return FALSE;
1974 }
1975
1976 void _bt_pending_connect_le_device(void)
1977 {
1978         if (pending_le_conn_timer_id > 0) {
1979                 g_source_remove(pending_le_conn_timer_id);
1980                 __bt_connect_le_timer_cb(NULL);
1981         }
1982 }
1983
1984 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1985                 gboolean auto_connect)
1986 {
1987         int ret = BLUETOOTH_ERROR_NONE;
1988
1989         BT_CHECK_PARAMETER(bd_addr, return);
1990
1991         ret = _bt_hold_current_advertising();
1992         if (ret == BLUETOOTH_ERROR_NONE) {
1993                 BT_INFO("Current advertising is held");
1994                 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1995                 pending_le_conn_info->req_id = req_id;
1996                 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1997                                 BLUETOOTH_ADDRESS_LENGTH);
1998                 pending_le_conn_info->auto_connect = auto_connect;
1999
2000                 pending_le_conn_timer_id =
2001                         g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2002
2003                 return BLUETOOTH_ERROR_NONE;
2004         }
2005
2006         BT_ERR("Unable to hold advertising");
2007
2008         return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2009 }
2010
2011 int _bt_disconnect_le_device(int req_id,
2012                 const bluetooth_device_address_t *bd_addr)
2013 {
2014         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2015         char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2016         gchar *device_path;
2017         GDBusProxy *device_proxy;
2018         GDBusConnection *conn;
2019         int ret = BLUETOOTH_ERROR_NONE;
2020         bt_function_data_t *func_data;
2021
2022         BT_CHECK_PARAMETER(bd_addr, return);
2023
2024         conn = _bt_gdbus_get_system_gconn();
2025         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2026
2027         _bt_convert_addr_type_to_string(device_address,
2028                         (unsigned char *)bd_addr->addr);
2029         device_path = _bt_get_device_object_path(device_address);
2030         if (device_path == NULL) {
2031                 BT_DBG("device_path NULL");
2032                 ret = BLUETOOTH_ERROR_INTERNAL;
2033                 return ret;
2034         }
2035
2036         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2037
2038         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2039                         NULL, BT_BLUEZ_NAME,
2040                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2041         g_free(device_path);
2042         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2043
2044         func_data = g_malloc0(sizeof(bt_function_data_t));
2045         func_data->address = g_strdup(device_address);
2046         if (func_data->address == NULL) {
2047                 BT_ERR("Unable to allocate memory for address");
2048                 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2049                 goto fail;
2050         }
2051
2052         _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2053         BT_INFO("Disconnect LE [%s]", secure_address);
2054
2055         func_data->req_id = req_id;
2056
2057         g_dbus_proxy_call(device_proxy, "DisconnectLE",
2058                         NULL,
2059                         G_DBUS_CALL_FLAGS_NONE,
2060                         BT_MAX_DBUS_TIMEOUT,
2061                         NULL,
2062                         (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2063         return ret;
2064
2065 fail:
2066         if (device_proxy)
2067                 g_object_unref(device_proxy);
2068
2069         g_free(func_data->address);
2070         g_free(func_data);
2071
2072         return ret;
2073 }
2074
2075 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2076 {
2077         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2078         gchar *device_path = NULL;
2079         GError *error = NULL;
2080         GDBusProxy *device_proxy = NULL;
2081         GDBusProxy *adapter_proxy;
2082         GDBusConnection *conn;
2083         int ret = BLUETOOTH_ERROR_NONE;
2084
2085         BT_CHECK_PARAMETER(bd_addr, return);
2086
2087         _bt_convert_addr_type_to_string(device_address,
2088                         (unsigned char *)bd_addr->addr);
2089
2090         conn = _bt_gdbus_get_system_gconn();
2091         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2092
2093         adapter_proxy = _bt_get_adapter_proxy();
2094         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2095
2096         device_path = _bt_get_device_object_path(device_address);
2097         if (device_path == NULL) {
2098                 BT_DBG("device_path NULL");
2099                 ret = BLUETOOTH_ERROR_INTERNAL;
2100                 return ret;
2101         }
2102
2103         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2104
2105         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2106                                                         NULL, BT_BLUEZ_NAME,
2107                                                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2108         g_free(device_path);
2109         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2110
2111         g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2112                                 NULL,
2113                                 G_DBUS_CALL_FLAGS_NONE,
2114                                 -1,
2115                                 NULL,
2116                                 &error);
2117         if (error) {
2118                 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2119                 g_error_free(error);
2120                 g_object_unref(device_proxy);
2121                 return BLUETOOTH_ERROR_INTERNAL;
2122         }
2123         g_object_unref(device_proxy);
2124
2125         /* IPSP daemon launch */
2126         GDBusProxy *ipsp_proxy;
2127
2128         ipsp_proxy = _bt_get_ipsp_proxy();
2129         retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2130
2131         g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2132                         NULL, G_DBUS_CALL_FLAGS_NONE,
2133                         -1, NULL, NULL, NULL);
2134
2135         return ret;
2136 }
2137
2138 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2139 {
2140         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2141         gchar *device_path = NULL;
2142         GError *error = NULL;
2143         GDBusProxy *device_proxy = NULL;
2144         GDBusProxy *adapter_proxy;
2145         GDBusConnection *conn;
2146         int ret = BLUETOOTH_ERROR_NONE;
2147
2148         BT_CHECK_PARAMETER(bd_addr, return);
2149
2150         _bt_convert_addr_type_to_string(device_address,
2151                         (unsigned char *)bd_addr->addr);
2152
2153         conn = _bt_gdbus_get_system_gconn();
2154         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2155
2156         adapter_proxy = _bt_get_adapter_proxy();
2157         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2158
2159         device_path = _bt_get_device_object_path(device_address);
2160         if (device_path == NULL) {
2161                 BT_DBG("device_path NULL");
2162                 ret = BLUETOOTH_ERROR_INTERNAL;
2163                 return ret;
2164         }
2165
2166         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2167
2168         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2169                                                         NULL, BT_BLUEZ_NAME,
2170                                                         device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2171         g_free(device_path);
2172         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2173
2174         g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2175                                 NULL,
2176                                 G_DBUS_CALL_FLAGS_NONE,
2177                                 -1,
2178                                 NULL,
2179                                 &error);
2180         if (error) {
2181                 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2182                 g_error_free(error);
2183                 g_object_unref(device_proxy);
2184                 return BLUETOOTH_ERROR_INTERNAL;
2185         }
2186
2187         g_object_unref(device_proxy);
2188
2189         return ret;
2190 }
2191
2192 int _bt_connect_profile(char *address, char *uuid,
2193                                                 void *cb, gpointer func_data)
2194 {
2195         char *object_path;
2196         GDBusProxy *proxy;
2197         GDBusConnection *conn;
2198         GDBusProxy *adapter_proxy;
2199         GVariant *result = NULL;
2200         GError *error = NULL;
2201
2202         conn = _bt_gdbus_get_system_gconn();
2203         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2204
2205         object_path = _bt_get_device_object_path(address);
2206         if (object_path == NULL) {
2207                 BT_ERR("No searched device");
2208
2209                 adapter_proxy = _bt_get_adapter_proxy();
2210                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2211
2212                 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2213                                          g_variant_new("(s)", address),
2214                                          G_DBUS_CALL_FLAGS_NONE,
2215                                          -1,
2216                                          NULL,
2217                                          &error);
2218
2219                 if (error != NULL) {
2220                         BT_ERR("CreateDevice Fail: %s", error->message);
2221                         g_error_free(error);
2222                 }
2223                 if (result)
2224                         g_variant_unref(result);
2225
2226                 object_path = _bt_get_device_object_path(address);
2227         }
2228         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2229
2230         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2231                                                                 NULL, BT_BLUEZ_NAME,
2232                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2233         g_free(object_path);
2234         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2235
2236         g_dbus_proxy_call(proxy, "ConnectProfile",
2237                                 g_variant_new("(s)", uuid),
2238                                 G_DBUS_CALL_FLAGS_NONE,
2239                                 BT_MAX_DBUS_TIMEOUT,
2240                                 NULL,
2241                                 (GAsyncReadyCallback)cb,
2242                                 func_data);
2243
2244         return BLUETOOTH_ERROR_NONE;
2245 }
2246
2247 int _bt_disconnect_all(char *address)
2248 {
2249         int ret = BLUETOOTH_ERROR_NONE;
2250         char *object_path;
2251         GDBusProxy *proxy;
2252         GDBusConnection *conn;
2253         GVariant *result = NULL;
2254         GError *err = NULL;
2255
2256         BT_DBG("");
2257         conn = _bt_gdbus_get_system_gconn();
2258         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2259
2260         object_path = _bt_get_device_object_path(address);
2261         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2262
2263         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2264                                         NULL, BT_BLUEZ_NAME,
2265                                         object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2266         g_free(object_path);
2267         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2268
2269         result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2270                 NULL,
2271                 G_DBUS_CALL_FLAGS_NONE,
2272                 -1, NULL,
2273                 &err);
2274
2275         if (err != NULL) {
2276                 BT_ERR("Dbus Call Error:[%s]", err->message);
2277                 g_error_free(err);
2278                 ret = BLUETOOTH_ERROR_INTERNAL;
2279         }
2280
2281         g_object_unref(proxy);
2282         if (result)
2283                 g_variant_unref(result);
2284
2285         return ret;
2286 }
2287
2288 int _bt_disconnect_profile(char *address, char *uuid,
2289                                                 void *cb, gpointer func_data)
2290 {
2291         char *object_path;
2292         GDBusProxy *proxy;
2293         GDBusConnection *conn;
2294
2295         conn = _bt_gdbus_get_system_gconn();
2296         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2297
2298         object_path = _bt_get_device_object_path(address);
2299         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2300
2301         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2302                                                                 NULL, BT_BLUEZ_NAME,
2303                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2304         g_free(object_path);
2305         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2306
2307         g_dbus_proxy_call(proxy, "DisconnectProfile",
2308                                         g_variant_new("(s)", uuid),
2309                                         G_DBUS_CALL_FLAGS_NONE,
2310                                         BT_MAX_DBUS_TIMEOUT,
2311                                         NULL,
2312                                         (GAsyncReadyCallback)cb,
2313                                         func_data);
2314
2315         return BLUETOOTH_ERROR_NONE;
2316 }
2317
2318 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2319                 int low_threshold, int in_range_threshold, int high_threshold)
2320 {
2321         int ret = BLUETOOTH_ERROR_NONE;
2322         GDBusProxy *proxy;
2323         GVariant *result = NULL;
2324         GError *error = NULL;
2325         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2326
2327         BT_CHECK_PARAMETER(bd_addr, return);
2328         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2329                         bd_addr->addr[0], bd_addr->addr[1],
2330                         bd_addr->addr[2], bd_addr->addr[3],
2331                         bd_addr->addr[4], bd_addr->addr[5],
2332                         link_type);
2333         BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2334                         in_range_threshold, high_threshold);
2335
2336         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2337
2338         proxy = _bt_get_adapter_proxy();
2339         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2340
2341         result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2342                                 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2343                                 G_DBUS_CALL_FLAGS_NONE,
2344                                 -1,
2345                                 NULL,
2346                                 &error);
2347         if (error != NULL) {
2348                         BT_ERR("Dbus Call Error:[%s]", error->message);
2349                         g_error_free(error);
2350                         ret = BLUETOOTH_ERROR_INTERNAL;
2351         }
2352
2353         if (result)
2354                 g_variant_unref(result);
2355
2356         return ret;
2357 }
2358
2359 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2360                                         int link_type)
2361 {
2362         int ret = BLUETOOTH_ERROR_NONE;
2363         GDBusProxy *proxy;
2364         GVariant *result = NULL;
2365         GError *error = NULL;
2366         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2367
2368         BT_CHECK_PARAMETER(bd_addr, return);
2369         BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2370                         bd_addr->addr[0], bd_addr->addr[1],
2371                         bd_addr->addr[2], bd_addr->addr[3],
2372                         bd_addr->addr[4], bd_addr->addr[5],
2373                         link_type);
2374
2375         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2376
2377         proxy = _bt_get_adapter_proxy();
2378         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2379
2380         result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2381                                 g_variant_new("(si)", address, link_type),
2382                                 G_DBUS_CALL_FLAGS_NONE,
2383                                 -1,
2384                                 NULL,
2385                                 &error);
2386
2387         if (error != NULL) {
2388                         BT_ERR("Dbus Call Error:[%s]", error->message);
2389                         g_error_free(error);
2390                         ret = BLUETOOTH_ERROR_INTERNAL;
2391         }
2392
2393         if (result)
2394                 g_variant_unref(result);
2395
2396         return ret;
2397 }
2398
2399 int _bt_le_conn_update(unsigned char *device_address,
2400                                 float interval_min, float interval_max,
2401                                 guint16 latency, guint16 time_out)
2402 {
2403         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2404         gchar *device_path = NULL;
2405         GError *error = NULL;
2406         GDBusProxy *device_proxy = NULL;
2407         GDBusConnection *conn;
2408         GVariant *reply;
2409         guint32 min, max, to;
2410         guint32 min_supervision_to;
2411         int ret = BLUETOOTH_ERROR_NONE;
2412
2413         BT_CHECK_PARAMETER(device_address, return);
2414
2415         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2416                         interval_min, interval_max, latency, time_out);
2417
2418         if (interval_min > interval_max ||
2419                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2420                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2421                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2422                 goto fail;
2423         }
2424
2425         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2426                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2427                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2428                 goto fail;
2429         }
2430
2431         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2432                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2433                 goto fail;
2434         }
2435
2436         /*
2437          * The Supervision_Timeout in milliseconds shall be larger than
2438          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2439          * where Conn_Interval_Max is given in milliseconds.
2440          */
2441         min_supervision_to = (1 + latency) * interval_max * 2;
2442         if (time_out <= min_supervision_to) {
2443                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2444                 goto fail;
2445         }
2446
2447         _bt_convert_addr_type_to_string(address, device_address);
2448
2449         BT_DBG("Remote device address: %s", address);
2450
2451         device_path = _bt_get_device_object_path(address);
2452
2453         if (device_path == NULL) {
2454                 BT_ERR("device_path NULL");
2455                 ret = BLUETOOTH_ERROR_INTERNAL;
2456                 goto fail;
2457         }
2458
2459         conn = _bt_gdbus_get_system_gconn();
2460         if (conn == NULL) {
2461                 BT_ERR("conn NULL");
2462                 ret = BLUETOOTH_ERROR_INTERNAL;
2463                 goto fail;
2464         }
2465
2466         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2467                                                                 NULL, BT_BLUEZ_NAME,
2468                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2469
2470         g_free(device_path);
2471         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2472
2473         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2474         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2475         to = time_out / BT_LE_CONN_TO_SPLIT;
2476
2477         reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2478                                 g_variant_new("(uuuu)", min, max, latency, to),
2479                                 G_DBUS_CALL_FLAGS_NONE,
2480                                 -1,
2481                                 NULL,
2482                                 &error);
2483
2484         g_object_unref(device_proxy);
2485         if (reply == NULL) {
2486                 if (error) {
2487                         BT_ERR("Error %s[%s]", error->message, address);
2488                         if (g_strrstr(error->message, "In Progress"))
2489                                 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2490                         else
2491                                 ret = BLUETOOTH_ERROR_INTERNAL;
2492                         g_error_free(error);
2493                         return ret;
2494                 }
2495         }
2496         g_variant_unref(reply);
2497
2498 fail:
2499         return ret;
2500 }
2501
2502 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2503                                 bluetooth_device_pin_code_t *pin_code)
2504 {
2505         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2506         GSList *l = NULL;
2507         bt_pin_code_info_t *pin_info = NULL;
2508
2509         BT_CHECK_PARAMETER(device_address, return);
2510         BT_CHECK_PARAMETER(pin_code, return);
2511         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2512                         BLUETOOTH_ERROR_NO_RESOURCES);
2513
2514         _bt_convert_addr_type_to_string(address, device_address->addr);
2515
2516         for (l = pin_info_list; l != NULL; l = l->next) {
2517                 pin_info = l->data;
2518
2519                 if (g_strcmp0(pin_info->address, address) == 0) {
2520                         g_free(pin_info->pin_code);
2521                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2522                         return BLUETOOTH_ERROR_NONE;
2523                 }
2524         }
2525
2526         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2527         pin_info->address = g_strdup(address);
2528         pin_info->pin_code = g_strdup(pin_code->pin_code);
2529         pin_info_list = g_slist_append(pin_info_list, pin_info);
2530
2531         return BLUETOOTH_ERROR_NONE;
2532 }
2533
2534 gint __bt_compare_address(gpointer *a, gpointer *b)
2535 {
2536         bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2537         char *address = (char *)b;
2538         return g_strcmp0(pin_info->address, address);
2539 }
2540
2541 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2542 {
2543         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2544         GSList *l = NULL;
2545         bt_pin_code_info_t *pin_info = NULL;
2546
2547         BT_CHECK_PARAMETER(device_address, return);
2548
2549         _bt_convert_addr_type_to_string(address, device_address->addr);
2550
2551         l = g_slist_find_custom(pin_info_list, address,
2552                 (GCompareFunc)__bt_compare_address);
2553         if (l)
2554                 pin_info = l->data;
2555         if (pin_info) {
2556                 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2557                 g_free(pin_info->address);
2558                 g_free(pin_info->pin_code);
2559                 g_free(pin_info);
2560         }
2561
2562         return BLUETOOTH_ERROR_NONE;
2563 }
2564
2565 int _bt_get_device_pin_code(const char *address, char *pin_code)
2566 {
2567         GSList *l = NULL;
2568
2569         BT_CHECK_PARAMETER(address, return);
2570         BT_CHECK_PARAMETER(pin_code, return);
2571
2572         for (l = pin_info_list; l != NULL; l = l->next) {
2573                 bt_pin_code_info_t *pin_info = l->data;
2574
2575                 if (g_strcmp0(pin_info->address, address) == 0) {
2576                         g_strlcpy(pin_code, pin_info->pin_code,
2577                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2578
2579                         return BLUETOOTH_ERROR_NONE;
2580                 }
2581         }
2582
2583         return BLUETOOTH_ERROR_NOT_FOUND;
2584 }
2585
2586 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2587                 bluetooth_le_connection_param_t *param)
2588 {
2589         if (param == NULL)
2590                 return BLUETOOTH_ERROR_INVALID_PARAM;
2591
2592         if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2593             mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2594                 return BLUETOOTH_ERROR_INVALID_PARAM;
2595
2596         memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2597
2598         switch (mode) {
2599         case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2600                 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2601                 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2602                 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2603                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2604                 break;
2605
2606         case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2607                 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2608                 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2609                 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2610                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2611                 break;
2612
2613         case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2614                 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2615                 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2616                 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2617                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2618                 break;
2619
2620         default:
2621                 BT_ERR("Unhandled mode : %d", mode);
2622                 break;
2623         }
2624
2625         return BLUETOOTH_ERROR_NONE;
2626 }
2627
2628 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2629                 guint trusted_profile_flag, guint *trusted)
2630 {
2631         int trust_profile;
2632         *trusted = FALSE;
2633
2634         switch (profile) {
2635         case TRUSTED_PROFILE_PBAP:
2636                 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2637                         trust_profile = trusted_profile_flag & (1 << 1);
2638                 else
2639                         return BLUETOOTH_ERROR_NOT_SUPPORT;
2640                 break;
2641         case TRUSTED_PROFILE_MAP:
2642                 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2643                         trust_profile = trusted_profile_flag & (1 << 3);
2644                 else
2645                         return BLUETOOTH_ERROR_NOT_SUPPORT;
2646                 break;
2647         case TRUSTED_PROFILE_SAP:
2648                 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2649                         trust_profile = trusted_profile_flag & (1 << 5);
2650                 else
2651                         return BLUETOOTH_ERROR_NOT_SUPPORT;
2652                 break;
2653         case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2654                 *trusted = trusted_profile_flag;
2655                 return BLUETOOTH_ERROR_NONE;
2656         default:
2657                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2658         }
2659
2660         if (trust_profile)
2661                 *trusted = TRUE;
2662
2663         return BLUETOOTH_ERROR_NONE;
2664 }
2665
2666 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2667                 guint restricted_profile_flag, guint *restricted)
2668 {
2669         int restrict_profile;
2670         *restricted = FALSE;
2671
2672         switch (profile) {
2673         case RESTRICTED_PROFILE_HFP_HS:
2674                         restrict_profile = restricted_profile_flag & (1 << 0);
2675                 break;
2676         case RESTRICTED_PROFILE_A2DP:
2677                         restrict_profile = restricted_profile_flag & (1 << 2);
2678                 break;
2679         default:
2680                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2681         }
2682
2683         if (restrict_profile)
2684                 *restricted = TRUE;
2685
2686         return BLUETOOTH_ERROR_NONE;
2687 }
2688
2689 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2690 {
2691         switch (profile) {
2692         case TRUSTED_PROFILE_PBAP:
2693                 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2694         case TRUSTED_PROFILE_MAP:
2695                 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2696         case TRUSTED_PROFILE_SAP:
2697                 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2698         case TRUSTED_PROFILE_ALL:
2699                 return NULL;
2700         }
2701
2702         return NULL;
2703 }
2704
2705 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2706 {
2707         switch (profile) {
2708         case RESTRICTED_PROFILE_HFP_HS:
2709                 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2710         case RESTRICTED_PROFILE_A2DP:
2711                 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2712         }
2713
2714         return NULL;
2715 }
2716
2717 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2718 {
2719         if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2720                 return TRUSTED_PROFILE_PBAP;
2721         else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2722                 return TRUSTED_PROFILE_MAP;
2723         else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2724                 return TRUSTED_PROFILE_SAP;
2725
2726         return 0; /* 0 - Unknown Profile */
2727 }
2728
2729 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2730                 bluetooth_trusted_profile_t profile, gboolean trust)
2731 {
2732         int ret = BLUETOOTH_ERROR_NONE;
2733         GDBusConnection *conn;
2734         GDBusProxy *proxy;
2735         GError *error = NULL;
2736         char *device_path = NULL;
2737         char *uuid = NULL;
2738         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2739         GVariant *reply;
2740
2741         BT_CHECK_PARAMETER(bd_addr, return);
2742         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2743                         bd_addr->addr[0], bd_addr->addr[1],
2744                         bd_addr->addr[2], bd_addr->addr[3],
2745                         bd_addr->addr[4], bd_addr->addr[5],
2746                         profile, trust);
2747
2748         conn = _bt_gdbus_get_system_gconn();
2749         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2750
2751         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2752
2753         device_path = _bt_get_device_object_path(address);
2754         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2755
2756         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2757                         NULL, BT_BLUEZ_NAME, device_path,
2758                         BT_DEVICE_INTERFACE, NULL, NULL);
2759
2760         g_free(device_path);
2761         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2762
2763         uuid = _bt_get_trusted_profile_uuid(profile);
2764         if (uuid == NULL) {
2765                 g_object_unref(proxy);
2766                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2767         }
2768
2769         reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2770                         g_variant_new("(sb)", uuid, trust),
2771                         G_DBUS_CALL_FLAGS_NONE, -1,
2772                         NULL, &error);
2773         g_object_unref(proxy);
2774
2775         if (reply == NULL) {
2776                 BT_ERR("Failed to Set Profile Trusted");
2777                 ret = BLUETOOTH_ERROR_INTERNAL;
2778                 if (error) {
2779                         BT_ERR("Error %s[%s]", error->message, address);
2780                         g_error_free(error);
2781                 }
2782                 goto finish;
2783         }
2784         g_variant_unref(reply);
2785
2786 finish:
2787         g_free(uuid);
2788         return ret;
2789 }
2790
2791 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2792                 bluetooth_trusted_profile_t profile, guint *trust)
2793 {
2794         int ret = BLUETOOTH_ERROR_NONE;
2795         GDBusConnection *conn;
2796         GDBusProxy *proxy;
2797         GError *error = NULL;
2798         char *device_path = NULL;
2799         guint trusted_profile_flag;
2800         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2801         GVariant *reply;
2802
2803         BT_CHECK_PARAMETER(bd_addr, return);
2804         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2805                         bd_addr->addr[0], bd_addr->addr[1],
2806                         bd_addr->addr[2], bd_addr->addr[3],
2807                         bd_addr->addr[4], bd_addr->addr[5],
2808                         profile, *trust);
2809
2810         conn = _bt_gdbus_get_system_gconn();
2811         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2812
2813         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2814
2815         device_path = _bt_get_device_object_path(address);
2816         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2817
2818         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2819                         NULL, BT_BLUEZ_NAME, device_path,
2820                         BT_PROPERTIES_INTERFACE, NULL, NULL);
2821
2822         g_free(device_path);
2823         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2824
2825         reply = g_dbus_proxy_call_sync(proxy, "Get",
2826                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2827                         G_DBUS_CALL_FLAGS_NONE, -1,
2828                         NULL, &error);
2829         g_object_unref(proxy);
2830
2831         if (reply == NULL) {
2832                 BT_ERR("Failed to Get Profile Trusted");
2833                 ret = BLUETOOTH_ERROR_INTERNAL;
2834                 if (error) {
2835                         BT_ERR("Error %s[%s]", error->message, address);
2836                         g_error_free(error);
2837                 }
2838                 *trust = 0;
2839         } else {
2840                 GVariant *temp;
2841                 g_variant_get(reply, "(v)", &temp);
2842                 trusted_profile_flag = g_variant_get_uint32(temp);
2843                 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2844
2845                 ret = _bt_get_trusted_profile_from_flag(profile,
2846                                 trusted_profile_flag, trust);
2847                 g_variant_unref(temp);
2848                 g_variant_unref(reply);
2849         }
2850
2851         BT_DBG("TRUST %d", *trust);
2852         return ret;
2853 }
2854
2855 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2856                 bluetooth_restricted_profile_t profile, gboolean restricted)
2857 {
2858         int ret = BLUETOOTH_ERROR_NONE;
2859         GDBusConnection *conn;
2860         GDBusProxy *proxy;
2861         GError *error = NULL;
2862         char *device_path = NULL;
2863         char *uuid = NULL;
2864         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2865         GVariant *reply;
2866
2867         BT_CHECK_PARAMETER(bd_addr, return);
2868         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2869                         bd_addr->addr[0], bd_addr->addr[1],
2870                         bd_addr->addr[2], bd_addr->addr[3],
2871                         bd_addr->addr[4], bd_addr->addr[5],
2872                         profile, restricted);
2873
2874         conn = _bt_gdbus_get_system_gconn();
2875         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2876
2877         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2878
2879         device_path = _bt_get_device_object_path(address);
2880         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2881
2882         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2883                         NULL, BT_BLUEZ_NAME, device_path,
2884                         BT_DEVICE_INTERFACE, NULL, NULL);
2885
2886         g_free(device_path);
2887         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2888
2889         uuid = _bt_get_restricted_profile_uuid(profile);
2890         if (uuid == NULL) {
2891                 g_object_unref(proxy);
2892                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2893         }
2894
2895         reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2896                         g_variant_new("(sb)", uuid, restricted),
2897                         G_DBUS_CALL_FLAGS_NONE, -1,
2898                         NULL, &error);
2899         g_object_unref(proxy);
2900
2901         if (reply == NULL) {
2902                 BT_ERR("Failed to Set Profile Restricted");
2903                 ret = BLUETOOTH_ERROR_INTERNAL;
2904                 if (error) {
2905                         BT_ERR("Error %s[%s]", error->message, address);
2906                         g_error_free(error);
2907                 }
2908                 goto finish;
2909         }
2910         g_variant_unref(reply);
2911
2912 finish:
2913         g_free(uuid);
2914         return ret;
2915 }
2916
2917 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2918                 bluetooth_restricted_profile_t profile, guint *restricted)
2919 {
2920         int ret = BLUETOOTH_ERROR_NONE;
2921         GDBusConnection *conn;
2922         GDBusProxy *proxy;
2923         GError *error = NULL;
2924         char *device_path = NULL;
2925         guint restricted_profile_flag;
2926         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2927         GVariant *reply;
2928
2929         BT_CHECK_PARAMETER(bd_addr, return);
2930         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2931                         bd_addr->addr[0], bd_addr->addr[1],
2932                         bd_addr->addr[2], bd_addr->addr[3],
2933                         bd_addr->addr[4], bd_addr->addr[5],
2934                         profile, *restricted);
2935
2936         conn = _bt_gdbus_get_system_gconn();
2937         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2938
2939         _bt_convert_addr_type_to_string(address, bd_addr->addr);
2940
2941         device_path = _bt_get_device_object_path(address);
2942         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2943
2944         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2945                         NULL, BT_BLUEZ_NAME, device_path,
2946                         BT_PROPERTIES_INTERFACE, NULL, NULL);
2947
2948         g_free(device_path);
2949         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2950
2951         reply = g_dbus_proxy_call_sync(proxy, "Get",
2952                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2953                         G_DBUS_CALL_FLAGS_NONE, -1,
2954                         NULL, &error);
2955         g_object_unref(proxy);
2956
2957         if (reply == NULL) {
2958                 BT_ERR("Failed to Get Profile Restricted");
2959                 ret = BLUETOOTH_ERROR_INTERNAL;
2960                 if (error) {
2961                         BT_ERR("Error %s[%s]", error->message, address);
2962                         g_error_free(error);
2963                 }
2964                 *restricted = 0;
2965         } else {
2966                 GVariant *temp;
2967                 g_variant_get(reply, "(v)", &temp);
2968                 restricted_profile_flag = g_variant_get_uint32(temp);
2969                 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2970
2971                 ret = _bt_get_restricted_profile_from_flag(profile,
2972                                 restricted_profile_flag, restricted);
2973                 g_variant_unref(temp);
2974                 g_variant_unref(reply);
2975         }
2976
2977         BT_DBG("TRUST %d", *restricted);
2978         return ret;
2979 }
2980
2981 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2982                                         gpointer user_data)
2983 {
2984         GError *err = NULL;
2985         GVariant *out_param1;
2986         request_info_t *req_info;
2987         GVariant *val = NULL;
2988         GVariant *param = NULL;
2989         guint8 status = 0;
2990         guint16 mtu = 0;
2991         bluetooth_device_address_t device_addr = { {0} };
2992         int result = BLUETOOTH_ERROR_NONE;
2993
2994         BT_DBG("+");
2995         val = g_dbus_proxy_call_finish(proxy, res, &err);
2996
2997         req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2998         if (req_info == NULL) {
2999                 BT_ERR("req_info == NULL");
3000                 g_object_unref(proxy);
3001                 att_mtu_req_info->device_proxy = NULL;
3002                 goto done;
3003         }
3004
3005         if (err != NULL) {
3006                 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3007
3008                 if (g_strrstr(err->message, "NotSupported")) {
3009                         BT_INFO("Connection Not Supported");
3010                         result = BLUETOOTH_ERROR_NOT_SUPPORT;
3011                 } else if (g_strrstr(err->message, "NotConnected")) {
3012                         BT_INFO("Not connected");
3013                         result = BLUETOOTH_ERROR_NOT_CONNECTED;
3014                 } else if (g_strrstr(err->message, "InvalidArguments")) {
3015                         BT_INFO("Not connected");
3016                         result = BLUETOOTH_ERROR_INVALID_PARAM;
3017                 } else {
3018                         BT_DBG("Default case");
3019                         result = BLUETOOTH_ERROR_INTERNAL;
3020                 }
3021         }
3022
3023         g_object_unref(proxy);
3024         att_mtu_req_info->device_proxy = NULL;
3025
3026         if (result != BLUETOOTH_ERROR_NONE)
3027                 goto dbus_return;
3028
3029         if (val) {
3030                  g_variant_get(val, "(qy)", &mtu, &status);
3031                  g_variant_unref(val);
3032         }
3033
3034         BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3035
3036         param = g_variant_new("(isqy)",
3037                         result,
3038                         att_mtu_req_info->addr,
3039                         mtu,
3040                         status);
3041
3042         /* Send the event to application */
3043         _bt_send_event(BT_DEVICE_EVENT,
3044                 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3045                 param);
3046
3047 dbus_return:
3048         if (req_info->context == NULL)
3049                 goto done;
3050
3051         _bt_convert_addr_string_to_type(device_addr.addr,
3052                                         (const char *)att_mtu_req_info->addr);
3053
3054         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3055                         &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3056         g_dbus_method_invocation_return_value(req_info->context,
3057                         g_variant_new("(iv)", result, out_param1));
3058
3059         _bt_delete_request_list(req_info->req_id);
3060 done:
3061         if (err)
3062                 g_error_free(err);
3063
3064         g_free(att_mtu_req_info->addr);
3065         g_free(att_mtu_req_info);
3066         att_mtu_req_info = NULL;
3067
3068         BT_DBG("-");
3069 }
3070
3071 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3072                                                                 unsigned int mtu)
3073 {
3074         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3075         gchar *device_path = NULL;
3076         GDBusProxy *adapter_proxy;
3077         GDBusProxy *device_proxy;
3078         GDBusConnection *conn;
3079         int ret = BLUETOOTH_ERROR_NONE;
3080
3081         BT_CHECK_PARAMETER(device_address, return);
3082
3083         if (att_mtu_req_info) {
3084                 BT_ERR("ATT MTU request in progress");
3085                 return BLUETOOTH_ERROR_DEVICE_BUSY;
3086         }
3087
3088         conn = _bt_gdbus_get_system_gconn();
3089         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3090
3091         adapter_proxy = _bt_get_adapter_proxy();
3092         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3093
3094         _bt_convert_addr_type_to_string(address, device_address->addr);
3095
3096         BT_DBG("Remote device address: %s", address);
3097
3098         device_path = _bt_get_device_object_path(address);
3099
3100         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3101
3102         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3103                                                                 NULL, BT_BLUEZ_NAME,
3104                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
3105         g_free(device_path);
3106         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3107
3108         att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3109         att_mtu_req_info->addr = (char *)g_strdup(address);
3110         att_mtu_req_info->req_id = request_id;
3111         att_mtu_req_info->device_proxy = device_proxy;
3112
3113         g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3114                                 g_variant_new("(q)", mtu),
3115                                 G_DBUS_CALL_FLAGS_NONE,
3116                                 BT_MAX_DBUS_TIMEOUT,
3117                                 NULL,
3118                                 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3119                                 NULL);
3120
3121         return ret;
3122 }
3123
3124 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3125                         unsigned int *mtu)
3126 {
3127         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3128         char *object_path = NULL;
3129
3130         GDBusProxy *device_proxy;
3131         GError *error = NULL;
3132         GVariant *value;
3133         GVariant *tmp_value;
3134         GDBusConnection *conn;
3135         GVariant *result = NULL;
3136         int ret = BLUETOOTH_ERROR_NONE;
3137
3138         BT_CHECK_PARAMETER(device_address, return);
3139
3140         conn = _bt_gdbus_get_system_gconn();
3141         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3142
3143         _bt_convert_addr_type_to_string(address, device_address->addr);
3144
3145         object_path = _bt_get_device_object_path(address);
3146         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3147
3148         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3149                         NULL, BT_BLUEZ_NAME, object_path,
3150                         BT_PROPERTIES_INTERFACE,  NULL, NULL);
3151         g_free(object_path);
3152         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3153
3154         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3155                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
3156                         G_DBUS_CALL_FLAGS_NONE,
3157                         -1,
3158                         NULL,
3159                         &error);
3160         if (result == NULL) {
3161                 if (error != NULL) {
3162                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3163                         g_error_free(error);
3164                 }
3165                 g_object_unref(device_proxy);
3166                 return BLUETOOTH_ERROR_INTERNAL;
3167         }
3168
3169         g_variant_get(result , "(@a{sv})", &value);
3170         g_variant_unref(result);
3171
3172         tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3173         if (tmp_value == NULL) {
3174                 g_object_unref(device_proxy);
3175                 g_variant_unref(value);
3176                 return BLUETOOTH_ERROR_INTERNAL;
3177         }
3178
3179         *mtu = g_variant_get_uint16(tmp_value);
3180
3181         BT_DBG("ATT MTU : %d", *mtu);
3182
3183         g_variant_unref(tmp_value);
3184         g_variant_unref(value);
3185         g_object_unref(device_proxy);
3186
3187         return ret;
3188 }
3189
3190 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3191                         bluetooth_device_address_t *id_address)
3192 {
3193         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3194         gchar *device_path = NULL;
3195         const gchar *idaddress = NULL;
3196         GDBusProxy *device_proxy;
3197         GError *error = NULL;
3198         GVariant *result = NULL;
3199         GDBusConnection *conn;
3200         int ret = BLUETOOTH_ERROR_NONE;
3201
3202         BT_CHECK_PARAMETER(device_address, return);
3203
3204         conn = _bt_gdbus_get_system_gconn();
3205         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3206
3207         _bt_convert_addr_type_to_string(address, device_address->addr);
3208
3209         device_path = _bt_get_device_object_path(address);
3210         retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3211
3212         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3213                                                                 NULL, BT_BLUEZ_NAME,
3214                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
3215         g_free(device_path);
3216         if (!device_proxy) {
3217                 BT_ERR("Unable to get proxy");
3218                 return BLUETOOTH_ERROR_INTERNAL;
3219         }
3220
3221         result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3222                                  NULL,
3223                                  G_DBUS_CALL_FLAGS_NONE,
3224                                  -1,
3225                                  NULL,
3226                                  &error);
3227
3228         if (result == NULL) {
3229                 BT_ERR("Failed to get device ID address");
3230                 if (error != NULL) {
3231                         BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3232                         g_error_free(error);
3233                 }
3234                 g_object_unref(device_proxy);
3235                 return BLUETOOTH_ERROR_INTERNAL;
3236         }
3237
3238         g_variant_get(result , "(s)", &idaddress);
3239         g_variant_unref(result);
3240
3241         if (idaddress == NULL) {
3242                 BT_ERR("No paired device");
3243                 g_object_unref(device_proxy);
3244                 return BLUETOOTH_ERROR_NOT_PAIRED;
3245         }
3246
3247         BT_DBG("ID Address:%s", idaddress);
3248
3249         if (idaddress)
3250                 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3251         else
3252                 ret = BLUETOOTH_ERROR_INTERNAL;
3253
3254         g_object_unref(device_proxy);
3255
3256         return ret;
3257 }
3258
3259 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3260 {
3261         GapAgentPrivate *agent = _bt_get_adapter_agent();
3262         retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3263
3264         if (authentication_reply)
3265                 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3266         else
3267                 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3268
3269         BT_DBG("BT_PASSKEY_REPLY");
3270         return BLUETOOTH_ERROR_NONE;
3271 }
3272
3273 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3274 {
3275         GapAgentPrivate *agent = _bt_get_adapter_agent();
3276         retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3277
3278         if (confirmation_reply)
3279                 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3280         else
3281                 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3282
3283         BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3284         return BLUETOOTH_ERROR_NONE;
3285 }