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