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