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