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