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