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