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