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