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