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