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