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