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