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