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