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