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