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