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