3.0 specific patch: enable to build after tizen 2.4 code sync.
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <glib.h>
25 #include <gio/gio.h>
26 #include <dlog.h>
27 #include <string.h>
28 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
29 #include <syspopup_caller.h>
30 #endif
31
32 #include "bluetooth-api.h"
33 #include "bt-internal-types.h"
34
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-device.h"
38 #include "bt-service-rfcomm-client.h"
39 #include "bt-service-util.h"
40 #include "bt-service-agent.h"
41 #include "bt-service-network.h"
42 #include "bt-service-adapter.h"
43
44 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
45 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
46 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
47
48 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
49 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
50 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
51 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
52 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
53 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
54 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
55
56 typedef struct {
57         int req_id;
58         int result;
59         char *addr;
60         gboolean is_autopair;
61         GDBusProxy *device_proxy;
62         GDBusProxy *adapter_proxy;
63         void *agent;
64         unsigned short conn_type;
65 } bt_funcion_data_t;
66
67 gboolean is_device_creating;
68 bt_funcion_data_t *bonding_info;
69 bt_funcion_data_t *searching_info;
70
71 /* This HID Mouse does not support pairing precedure. need to skip it. */
72 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
73
74 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
75                                         gpointer user_data);
76
77 static int __bt_retry_bond(void);
78
79
80 /*static void __bt_decline_pair_request()
81 {
82         GVariant *out_param1;
83         GVariant *out_param2;
84         request_info_t *req_info;
85         bluetooth_device_info_t dev_info;
86         bt_remote_dev_info_t *remote_dev_info;
87         GVariant *uuid_list, *manufacture_data;
88         GVariant *param;
89
90         BT_DBG("+");
91         if (bonding_info) {
92                 req_info = _bt_get_request_info(bonding_info->req_id);
93                 if (req_info == NULL) {
94                         BT_ERR("req_info == NULL");
95                         goto done;
96                 }
97                 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
98         } else {
99                 BT_DBG("bonding_info is NULL");
100                 BT_DBG("-");
101                 return;
102         }
103
104         uuid_list =  g_variant_new_from_data((const GVariantType *)"as",
105                                                 remote_dev_info->uuids, remote_dev_info->uuid_count,
106                                                 TRUE, NULL, NULL);
107
108         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
109                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
110                                                 TRUE, NULL, NULL);
111
112         param = g_variant_new("isunsbuba{s}na{y})",
113                         bonding_info->result,
114                         bonding_info->addr,
115                         remote_dev_info->class,
116                         remote_dev_info->rssi,
117                         remote_dev_info->name,
118                         remote_dev_info->paired,
119                         remote_dev_info->connected,
120                         remote_dev_info->trust,
121                         uuid_list,
122                         remote_dev_info->manufacturer_data_len,
123                         manufacture_data);
124
125
126         //Send the event to application
127         if (remote_dev_info != NULL) {
128                 _bt_send_event(BT_ADAPTER_EVENT,
129                         BLUETOOTH_EVENT_BONDING_FINISHED,
130                         param);
131
132                 _bt_free_device_info(remote_dev_info);
133         }
134
135         if (req_info->context == NULL)
136                 goto done;
137
138         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
139         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
140                                         bonding_info->addr);
141
142         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
143                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
144
145         out_param2 = g_variant_new_from_data((const GVariantType *)"i",
146                 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
147
148
149         g_dbus_method_invocation_return_value(req_info->context,
150                         g_variant_new("@ayi", out_param1, out_param2));
151
152
153         _bt_delete_request_list(req_info->req_id);
154 done:
155
156         g_free(bonding_info->addr);
157         g_free(bonding_info);
158         bonding_info = NULL;
159
160         BT_DBG("-");
161 } */
162
163 #ifdef TIZEN_WEARABLE
164 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
165 {
166         int ret;
167         bundle *b;
168         retv_if(user_data == NULL, FALSE);
169
170         b = (bundle *)user_data;
171
172 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
173         ret = syspopup_launch("bt-syspopup", b);
174 #endif
175         if (ret < 0) {
176                 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
177         } else {
178                 BT_DBG("Hurray!!! Finally Popup launched");
179                 bundle_free(b);
180         }
181         return (ret < 0) ? TRUE : FALSE;
182 }
183
184 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
185 {
186         BT_DBG("+");
187         int ret = 0;
188         bundle *b = NULL;
189         GDBusConnection *conn;
190
191         conn = _bt_get_system_gconn();
192         if (conn == NULL)
193                 return FALSE;
194
195         b = bundle_create();
196         if (b == NULL)
197                 return FALSE;
198
199         bundle_add(b, "event-type", "unable-to-pairing");
200
201         if (result == BLUETOOTH_ERROR_TIMEOUT )
202                 bundle_add(b, "error", "timeout");
203         else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
204                 bundle_add(b, "error", "authfailed");
205         else
206                 bundle_add(b, "error", "error");
207
208 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
209         ret = syspopup_launch("bt-syspopup", b);
210 #endif
211         if (0 > ret) {
212                 BT_ERR("Popup launch failed...retry %d \n", ret);
213                 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
214                                 b);
215         } else {
216                 bundle_free(b);
217         }
218
219         BT_DBG("-");
220         return TRUE;
221 }
222 #endif
223
224 gboolean _bt_is_device_creating(void)
225 {
226         return is_device_creating;
227 }
228
229 gboolean _bt_is_bonding_device_address(const char *address)
230 {
231         if (bonding_info == NULL || bonding_info->addr == NULL)
232                 return FALSE;
233
234         if (g_strcmp0(bonding_info->addr, address) == 0) {
235                 BT_DBG("[%s]  is bonding device", address);
236                 return TRUE;
237         }
238
239         BT_DBG("[%s]  is NOT bonding device", address);
240         return FALSE;
241 }
242
243 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
244 {
245         ret_if(bonding_info == NULL);
246         bonding_info->is_autopair = is_autopair;
247 }
248
249 void _bt_device_path_to_address(const char *device_path,
250                                         char *device_address)
251 {
252         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
253         char *dev_addr;
254         char *pos;
255
256         ret_if(device_path == NULL);
257         ret_if(device_address == NULL);
258
259         dev_addr = strstr(device_path, "dev_");
260         ret_if(dev_addr == NULL);
261
262         dev_addr += 4;
263         g_strlcpy(address, dev_addr, sizeof(address));
264
265         while ((pos = strchr(address, '_')) != NULL) {
266                 *pos = ':';
267         }
268
269         g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
270 }
271
272 void __bt_cancel_search_service_done(void)
273 {
274         int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
275         request_info_t *req_info;
276         bluetooth_device_info_t dev_info;
277         GVariant *out_param1;
278
279         ret_if(searching_info == NULL);
280
281         req_info = _bt_get_request_info(searching_info->req_id);
282         if (req_info == NULL) {
283                 BT_ERR("req_info == NULL");
284                 goto done;
285         }
286
287         if (req_info->context == NULL)
288                 goto done;
289
290         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
291         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
292                                         searching_info->addr);
293
294         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
295                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
296
297
298         g_dbus_method_invocation_return_value(req_info->context,
299                         g_variant_new("(iv)", result, out_param1));
300
301         _bt_delete_request_list(req_info->req_id);
302
303 done:
304
305         g_free(searching_info->addr);
306         g_free(searching_info);
307         searching_info = NULL;
308 }
309
310 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
311 {
312         ret_if(value == NULL);
313         ret_if(info == NULL);
314
315         info->uuid_count = g_variant_get_size(value);
316         info->uuids = g_variant_dup_strv(value, &info->uuid_count);
317 }
318
319 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
320 {
321         bt_remote_dev_info_t *dev_info;
322         char *object_path = NULL;
323         GDBusProxy *adapter_proxy;
324         GDBusProxy *device_proxy;
325         GVariant *value;
326         GVariant *tmp_value;
327         gchar *name;
328         gchar *manufacturer_data = NULL;
329         GDBusConnection *conn;
330         GError *error = NULL;
331         GVariant *result = NULL;
332
333         retv_if(address == NULL, NULL);
334
335         adapter_proxy = _bt_get_adapter_proxy();
336         retv_if(adapter_proxy == NULL, NULL);
337
338
339         object_path = _bt_get_device_object_path(address);
340
341         retv_if(object_path == NULL, NULL);
342
343         conn = _bt_get_system_gconn();
344         if (conn == NULL) {
345                 BT_ERR("conn == NULL");
346                 g_free(object_path);
347                 return NULL;
348         }
349
350         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
351                                                                 NULL, BT_BLUEZ_NAME,
352                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
353
354         g_free(object_path);
355         retv_if(device_proxy == NULL, NULL);
356
357         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
358                                 g_variant_new("(s)", BT_DEVICE_INTERFACE),
359                                 G_DBUS_CALL_FLAGS_NONE,
360                                 -1,
361                                 NULL,
362                                 &error);
363
364         g_object_unref(device_proxy);
365
366         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
367
368         if (result != NULL) {
369                 g_variant_get(result , "(@a{sv})", &value);
370                 g_variant_unref(result);
371
372                 tmp_value = g_variant_lookup_value (value, "Alias", G_VARIANT_TYPE_STRING);
373
374                 g_variant_get(tmp_value, "s", &name);
375                 g_variant_unref(tmp_value);
376                 if (name != NULL)
377                         DBG_SECURE("Alias Name [%s]", name);
378                 else {
379                         tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
380                         g_variant_get(tmp_value, "s", &name);
381                         g_variant_unref(tmp_value);
382                 }
383
384                 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
385                 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
386                 g_variant_unref(tmp_value);
387
388                 tmp_value = g_variant_lookup_value(value, "Connected",  G_VARIANT_TYPE_BOOLEAN);
389                 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
390                 g_variant_unref(tmp_value);
391
392                 tmp_value = g_variant_lookup_value(value, "Trusted",  G_VARIANT_TYPE_BOOLEAN);
393                 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
394                 g_variant_unref(tmp_value);
395
396                 tmp_value = g_variant_lookup_value(value, "Paired",  G_VARIANT_TYPE_BOOLEAN);
397                 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
398                 g_variant_unref(tmp_value);
399
400                 BT_DBG("Paired %d", dev_info->paired );
401
402                 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
403                 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
404                 g_variant_unref(tmp_value);
405
406                 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
407                 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
408                 g_variant_unref(tmp_value);
409
410                 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
411                 __bt_get_uuids(tmp_value, dev_info);
412                 g_variant_unref(tmp_value);
413
414                 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
415                 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
416                 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
417                         BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
418                         dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
419                 }
420                 g_variant_unref(tmp_value);
421
422                 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
423                 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
424                 if (manufacturer_data) {
425                         if (dev_info->manufacturer_data_len > 0) {
426                                 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
427                                 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
428                         }
429                 }
430                 g_variant_unref(tmp_value);
431
432                 dev_info->address = g_strdup(address);
433                 dev_info->name = g_strdup(name);
434                 g_free(name);
435                 g_variant_unref(value);
436         } else {
437                 BT_ERR("result  is NULL\n");
438                 g_free(dev_info);
439                 dev_info = NULL;
440         }
441
442         return dev_info;
443 }
444
445 char *_bt_get_bonded_device_name(char *address)
446 {
447         bluetooth_device_address_t device_address = { {0} };
448         bluetooth_device_info_t dev_info;
449
450         retv_if(address == NULL, strdup(""));
451
452         _bt_convert_addr_string_to_type(device_address.addr, address);
453
454         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
455
456         _bt_get_bonded_device_info(&device_address, &dev_info);
457
458         return g_strdup(dev_info.device_name.name);
459 }
460
461 static gboolean __ignore_auto_pairing_request(const char *address)
462 {
463         gchar *buffer;
464         char **lines;
465         int i;
466         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
467         char *temp_buffer;
468         FILE *fp;
469         long size;
470         size_t result;
471
472         BT_DBG("+\n");
473
474         if (address == NULL)
475                 return FALSE;
476
477         /* Get the LAP(Lower Address part) */
478         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
479              a "," */
480         snprintf(lap_address, sizeof(lap_address), ",%s", address);
481
482         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
483
484         if (fp == NULL) {
485                 BT_ERR("fopen failed \n");
486                 return FALSE;
487         }
488
489         fseek(fp, 0, SEEK_END);
490         size = ftell(fp);
491         rewind(fp);
492
493         if (size < 0) {
494                 BT_ERR("Get file size failed \n");
495                 fclose(fp);
496                 return FALSE;
497         }
498
499         buffer = g_malloc0(sizeof(char) * size);
500         /* Fix : NULL_RETURNS */
501         if (buffer == NULL) {
502                 BT_ERR("Memory allocation error\n");
503                 fclose(fp);
504                 return FALSE;
505         }
506         result = fread((char *)buffer, 1, size, fp);
507         fclose(fp);
508         if (result != size) {
509                 BT_ERR("Read Error\n");
510                 g_free(buffer);
511                 return FALSE;
512         }
513
514         BT_DBG("Buffer = %s\n", buffer);
515
516         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
517         g_free(buffer);
518
519         if (lines == NULL)
520                 return FALSE;
521
522         /* Write the data and insert new device data */
523         for (i = 0; lines[i] != NULL; i++) {
524                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
525                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
526                         g_free(lines[i]);
527                         lines[i] = temp_buffer;
528                 }
529         }
530         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
531         g_strfreev(lines);
532         /* Fix : NULL_RETURNS */
533         retv_if(buffer == NULL, FALSE);
534
535         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
536
537         if (fp == NULL) {
538                 BT_ERR("fopen failed \n");
539                 g_free(buffer);
540                 return FALSE;
541         }
542
543         BT_DBG("Buffer = %s\n", buffer);
544         fwrite(buffer, 1, strlen(buffer), fp);
545         fclose(fp);
546
547         g_free(buffer);
548
549         BT_DBG("-\n");
550
551         return FALSE;
552 }
553
554 static int __bt_retry_bond(void)
555 {
556         BT_CHECK_PARAMETER(bonding_info, return);
557         BT_CHECK_PARAMETER(bonding_info->addr, return);
558
559         g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
560                                 g_variant_new("(y)", bonding_info->conn_type),
561                                 G_DBUS_CALL_FLAGS_NONE,
562                                 BT_MAX_DBUS_TIMEOUT,
563                                 NULL,
564                                 (GAsyncReadyCallback)__bt_bond_device_cb,
565                                 NULL);
566
567         return BLUETOOTH_ERROR_NONE;
568 }
569
570
571 static int __bt_remove_and_bond(void)
572 {
573         GDBusProxy *adapter_proxy;
574         GVariant *result = NULL;
575         GError *err = NULL;
576         char *device_path = NULL;
577
578         BT_CHECK_PARAMETER(bonding_info, return);
579         BT_CHECK_PARAMETER(bonding_info->addr, return);
580
581         adapter_proxy = _bt_get_adapter_proxy();
582         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
583
584         result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
585                                 g_variant_new("(s)", bonding_info->addr),
586                                 G_DBUS_CALL_FLAGS_NONE,
587                                 -1,
588                                 NULL,
589                                 NULL);
590         if (result == NULL)
591                 return BLUETOOTH_ERROR_INTERNAL;
592
593         g_variant_get(result , "(&o)", &device_path);
594         g_variant_unref(result);
595
596         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
597
598         result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
599                                 g_variant_new("(o)", device_path),
600                                 G_DBUS_CALL_FLAGS_NONE,
601                                 -1,
602                                 NULL,
603                                 &err);
604
605         if (err != NULL) {
606                 BT_ERR("UnpairDevice Fail: %s", err->message);
607                 g_error_free(err);
608                 return BLUETOOTH_ERROR_INTERNAL;
609         }
610
611         return __bt_retry_bond();
612 }
613
614 static int __bt_cancel_and_bond(void)
615 {
616         int ret = BLUETOOTH_ERROR_NONE;
617
618         ret = _bt_agent_reply_cancellation();
619         if (ret != BLUETOOTH_ERROR_NONE){
620                 BT_ERR("Fail to call reply cancellation");
621                 return ret;
622         }
623
624         return __bt_retry_bond();
625 }
626
627
628 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
629                                         gpointer user_data)
630 {
631         int result = BLUETOOTH_ERROR_NONE;
632         GError *err = NULL;
633         GVariant *out_param1;
634         request_info_t *req_info;
635         bluetooth_device_info_t dev_info;
636         bt_remote_dev_info_t *remote_dev_info;
637         GVariant *manufacture_data;
638         GVariant *param;
639
640 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
641         /* Terminate ALL system popup */
642         syspopup_destroy_all();
643 #endif
644
645          g_dbus_proxy_call_finish(proxy, res, &err);
646
647         is_device_creating = FALSE;
648
649         if (bonding_info == NULL) {
650                 /* Send reply */
651                 BT_ERR("bonding_info == NULL");
652                 if (err)
653                         g_error_free(err);
654                 return;
655         }
656
657         req_info = _bt_get_request_info(bonding_info->req_id);
658         if (req_info == NULL) {
659                 BT_ERR("req_info == NULL");
660                 goto done;
661         }
662
663         if (err != NULL) {
664                 BT_ERR("Error occured in CreateBonding [%s]", err->message);
665
666                 if (!strcmp(err->message, "Already Exists")) {
667                         BT_INFO("Existing Bond, remove and retry");
668                         ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
669
670                         result = BLUETOOTH_ERROR_PARING_FAILED;
671                 } else if (_bt_agent_is_canceled() ||
672                         !strcmp(err->message, "Authentication Canceled")) {
673                         BT_INFO("Cancelled by USER");
674                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
675                 } else if (!strcmp(err->message, "Authentication Rejected")) {
676                         BT_INFO("REJECTED");
677                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
678                 } else if (!strcmp(err->message, "In Progress")) {
679                         BT_INFO("Bond in progress, cancel and retry");
680                         ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
681
682                         result = BLUETOOTH_ERROR_PARING_FAILED;
683                 } else if (!strcmp(err->message, "Authentication Failed")) {
684                         BT_INFO("Authentication Failed");
685                         if (bonding_info->is_autopair == TRUE) {
686                                 _bt_set_autopair_status_in_bonding_info(FALSE);
687                                 __ignore_auto_pairing_request(bonding_info->addr);
688                         }
689                         result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
690                 } else if (!strcmp(err->message, "Page Timeout")) {
691                         BT_INFO("Page Timeout");
692                         /* This is the special case
693                              As soon as call bluetooth_bond_device, try to cancel bonding.
694                              In this case, before completing to call 'CreatePairedDevice' method
695                              the procedure is stopped. So 'Cancle' error is not return.
696                         */
697                         result = BLUETOOTH_ERROR_HOST_DOWN;
698                 } else if (!strcmp(err->message, BT_TIMEOUT_MESSAGE)) {
699                         g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
700                                                 g_variant_new("(s)", bonding_info->addr),
701                                                 G_DBUS_CALL_FLAGS_NONE,
702                                                 -1,
703                                                 NULL,
704                                                 NULL);
705
706                         result = BLUETOOTH_ERROR_INTERNAL;
707                 } else if (!strcmp(err->message, "Connection Timeout")) {
708                         /* Pairing request timeout */
709                         result = BLUETOOTH_ERROR_TIMEOUT;
710                 } else if (!strcmp(err->message, "Authentication Timeout")) {
711                         /* Pairing request timeout */
712                         result = BLUETOOTH_ERROR_TIMEOUT;
713                 } else {
714                         BT_DBG("Default case");
715                         result = BLUETOOTH_ERROR_PARING_FAILED;
716                 }
717         }
718
719         if (result == BLUETOOTH_ERROR_PARING_FAILED ||
720                         result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
721                         result == BLUETOOTH_ERROR_TIMEOUT ||
722                         result == BLUETOOTH_ERROR_HOST_DOWN) {
723                 bonding_info->result = result;
724 #ifdef TIZEN_WEARABLE
725                 __bt_launch_unable_to_pairing_syspopup(result);
726 #endif
727         }
728
729         g_object_unref(proxy);
730         bonding_info->device_proxy = NULL;
731
732         if (result != BLUETOOTH_ERROR_NONE)
733                 goto dbus_return;
734
735         remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
736
737         GVariant *uuids = NULL;
738         GVariantBuilder *builder = NULL;
739         int i = 0;
740         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
741         for (i=0; i < remote_dev_info->uuid_count; i++) {
742                 g_variant_builder_add(builder, "s",
743                         remote_dev_info->uuids[i]);
744         }
745         uuids = g_variant_new("as", builder);
746         g_variant_builder_unref(builder);
747         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
748                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
749                                                 TRUE, NULL, NULL);
750
751         param = g_variant_new("(isunsbub@asn@ay)",
752                         result,
753                         bonding_info->addr,
754                         remote_dev_info->class,
755                         remote_dev_info->rssi,
756                         remote_dev_info->name,
757                         remote_dev_info->paired,
758                         remote_dev_info->connected,
759                         remote_dev_info->trust,
760                         uuids,
761                         remote_dev_info->manufacturer_data_len,
762                         manufacture_data);
763
764
765         /* Send the event to application */
766         if (remote_dev_info != NULL) {
767
768                 _bt_send_event(BT_ADAPTER_EVENT,
769                         BLUETOOTH_EVENT_BONDING_FINISHED,
770                         param);
771
772                 _bt_free_device_info(remote_dev_info);
773         }
774
775 dbus_return:
776         if (req_info->context == NULL)
777                 goto done;
778
779         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
780         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
781                                         bonding_info->addr);
782
783         if (_bt_adapter_get_status() != BT_ACTIVATED)
784                 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
785
786         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
787                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
788
789         g_dbus_method_invocation_return_value(req_info->context,
790                         g_variant_new("(iv)", result, out_param1));
791
792         g_variant_unref(out_param1);
793 //      g_variant_unref(out_param2);
794
795         _bt_delete_request_list(req_info->req_id);
796 done:
797         if (err)
798                 g_error_free(err);
799
800         _bt_agent_set_canceled(FALSE);
801
802
803         g_free(bonding_info->addr);
804         g_free(bonding_info);
805         bonding_info = NULL;
806 }
807
808 int _bt_bond_device(int request_id,
809                 bluetooth_device_address_t *device_address,
810                 unsigned short conn_type, GArray **out_param1)
811 {
812         GDBusProxy *proxy;
813         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
814         bluetooth_device_info_t dev_info;
815
816         GDBusConnection *conn;
817         char *device_path = NULL;
818         GDBusProxy *adapter_proxy;
819         GError *error = NULL;
820
821         BT_CHECK_PARAMETER(device_address, return);
822
823         if (bonding_info) {
824                 BT_ERR("Bonding in progress");
825
826                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
827                 memcpy(dev_info.device_address.addr, device_address->addr,
828                                 BLUETOOTH_ADDRESS_LENGTH);
829
830                 g_array_append_vals(*out_param1, &dev_info,
831                                 sizeof(bluetooth_device_info_t));
832
833                 return BLUETOOTH_ERROR_DEVICE_BUSY;
834         }
835
836         conn = _bt_get_system_gconn();
837         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
838
839         _bt_convert_addr_type_to_string(address, device_address->addr);
840
841         device_path = _bt_get_device_object_path(address);
842
843         if (device_path == NULL) {
844                 BT_ERR("No searched device");
845                 GVariant *ret = NULL;
846                 adapter_proxy = _bt_get_adapter_proxy();
847                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
848
849                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
850                                         g_variant_new("(s)", address),
851                                         G_DBUS_CALL_FLAGS_NONE,
852                                         -1,
853                                         NULL,
854                                         &error);
855
856                 if (error != NULL) {
857                         BT_ERR("CreateDevice Fail: %s", error->message);
858                         g_clear_error(&error);
859                 }
860                 if (ret)
861                         g_variant_unref(ret);
862                 device_path = _bt_get_device_object_path(address);
863                 if (device_path == NULL) {
864                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
865                         memcpy(dev_info.device_address.addr, device_address->addr,
866                                         BLUETOOTH_ADDRESS_LENGTH);
867
868                         g_array_append_vals(*out_param1, &dev_info,
869                                         sizeof(bluetooth_device_info_t));
870
871                         return BLUETOOTH_ERROR_NOT_PAIRED;
872                 } else {
873                         BT_INFO("device_path is created[%s]", device_path);
874                 }
875         }
876
877         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
878                                                                 NULL, BT_BLUEZ_NAME,
879                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
880
881         g_free(device_path);
882         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
883
884         bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
885         bonding_info->addr = g_strdup(address);
886         bonding_info->req_id = request_id;
887
888         bonding_info->device_proxy = proxy;
889         bonding_info->conn_type = conn_type;
890
891         is_device_creating = TRUE;
892
893         g_dbus_proxy_call(proxy, "Pair",
894                                 g_variant_new("(y)", conn_type),
895                                 G_DBUS_CALL_FLAGS_NONE,
896                                 BT_MAX_DBUS_TIMEOUT,
897                                 NULL,
898                                 (GAsyncReadyCallback)__bt_bond_device_cb,
899                                 NULL);
900
901 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
902
903         return BLUETOOTH_ERROR_NONE;
904 /*fail:
905         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
906         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
907                                         bonding_info->addr);
908
909         g_array_append_vals(*out_param1, &dev_info,
910                                 sizeof(bluetooth_device_info_t));
911
912         is_device_creating = FALSE;
913
914         g_free(bonding_info->addr);
915         g_free(bonding_info);
916         bonding_info = NULL;
917
918         return BLUETOOTH_ERROR_INTERNAL;*/
919 }
920
921 int _bt_cancel_bonding(void)
922 {
923         int ret = BLUETOOTH_ERROR_NONE;
924
925         retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
926
927         ret = _bt_agent_reply_cancellation();
928         if (ret != BLUETOOTH_ERROR_NONE){
929                 BT_ERR("Fail to call reply cancellation");
930                 return ret;
931         }
932
933         _bt_agent_set_canceled(TRUE);
934
935         return BLUETOOTH_ERROR_NONE;
936 }
937
938 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
939                                         gpointer user_data)
940 {
941         GError *err = NULL;
942         GVariant *out_param1;
943         int result = BLUETOOTH_ERROR_NONE;
944         bt_funcion_data_t *unbonding_info;
945         bluetooth_device_info_t dev_info;
946         request_info_t *req_info;
947
948         g_dbus_proxy_call_finish(proxy, res, &err);
949
950         unbonding_info = user_data;
951
952         if (unbonding_info == NULL) {
953                 /* Send reply */
954                 BT_ERR("unbonding_info == NULL");
955                 goto done;
956         }
957
958         req_info = _bt_get_request_info(unbonding_info->req_id);
959         if (req_info == NULL) {
960                 BT_ERR("req_info == NULL");
961                 goto done;
962         }
963
964         if (err != NULL) {
965                 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
966                 result = BLUETOOTH_ERROR_INTERNAL;
967         }
968
969         if (req_info->context == NULL)
970                 goto done;
971
972         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
973         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
974                                         unbonding_info->addr);
975
976         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
977                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
978
979         g_dbus_method_invocation_return_value(req_info->context,
980                         g_variant_new("(iv)", result, out_param1));
981
982
983         _bt_delete_request_list(req_info->req_id);
984
985 done:
986         if (err)
987                 g_error_free(err);
988
989         if (unbonding_info) {
990                 g_free(unbonding_info->addr);
991                 g_free(unbonding_info);
992         }
993 }
994
995 int _bt_unbond_device(int request_id,
996                         bluetooth_device_address_t *device_address,
997                         GArray **out_param1)
998 {
999         char *device_path = NULL;
1000         bt_funcion_data_t *unbonding_info;
1001         GDBusProxy *adapter_proxy = NULL;
1002         GDBusProxy *device_proxy = NULL;
1003         GDBusConnection *conn;
1004         int result = BLUETOOTH_ERROR_INTERNAL;
1005         bluetooth_device_info_t dev_info;
1006         GError *error = NULL;
1007         GVariant *ret = NULL;
1008
1009         BT_CHECK_PARAMETER(device_address, return);
1010
1011         adapter_proxy = _bt_get_adapter_proxy();
1012         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1013
1014         /* allocate user data so that it can be retrieved in callback */
1015         unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1016         /* Fix : NULL_RETURNS */
1017         if (unbonding_info == NULL) {
1018                 BT_ERR("Memory not allocated !");
1019                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1020         }
1021
1022         unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1023         unbonding_info->req_id = request_id;
1024
1025         _bt_convert_addr_type_to_string(unbonding_info->addr,
1026                                         device_address->addr);
1027
1028         device_path = _bt_get_device_object_path(unbonding_info->addr);
1029
1030         if (device_path == NULL) {
1031                 BT_ERR("No paired device");
1032                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1033                 goto fail;
1034         }
1035
1036         conn = _bt_get_system_gconn();
1037         if (conn == NULL) {
1038                 BT_ERR("conn is NULL");
1039                 result = BLUETOOTH_ERROR_INTERNAL;
1040                 goto fail;
1041         }
1042
1043         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1044                                                                 NULL, BT_BLUEZ_NAME,
1045                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1046
1047         if (device_proxy != NULL) {
1048
1049                 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1050                                         g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1051                                         G_DBUS_CALL_FLAGS_NONE,
1052                                         -1,
1053                                         NULL,
1054                                         &error);
1055                 if (error) {
1056                         BT_ERR("Getting property failed: [%s]\n", error->message);
1057                         g_error_free(error);
1058                         result = BLUETOOTH_ERROR_NOT_PAIRED;
1059                         goto fail;
1060                 } else {
1061                         if (!ret) {
1062                                 BT_ERR("No paired device");
1063                                 g_object_unref(device_proxy);
1064                                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1065                                 goto fail;
1066                         }
1067                         g_variant_unref(ret);
1068                 }
1069                 g_object_unref(device_proxy);
1070         }
1071
1072         g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1073                                 g_variant_new("(o)", device_path),
1074                                 G_DBUS_CALL_FLAGS_NONE,
1075                                 BT_MAX_DBUS_TIMEOUT,
1076                                 NULL,
1077                                 (GAsyncReadyCallback)__bt_unbond_cb,
1078                                 NULL);
1079
1080         g_free(device_path);
1081         return BLUETOOTH_ERROR_NONE;
1082
1083 fail:
1084         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1085         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1086                                         unbonding_info->addr);
1087
1088         g_array_append_vals(*out_param1, &dev_info,
1089                                 sizeof(bluetooth_device_info_t));
1090         g_free(device_path);
1091         g_free(unbonding_info->addr);
1092         g_free(unbonding_info);
1093         return result;
1094 }
1095
1096 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1097                                         gpointer user_data)
1098 {
1099         GError *err = NULL;
1100         GVariant *out_param1;
1101         int result = BLUETOOTH_ERROR_NONE;
1102         bluetooth_device_info_t dev_info;
1103         bt_remote_dev_info_t *remote_dev_info;
1104         request_info_t *req_info;
1105         GVariant *uuid_list, *manufacture_data;
1106         GVariant *param;
1107         GVariantBuilder *builder = NULL;
1108         int i = 0;
1109
1110         g_dbus_proxy_call_finish(proxy, res, &err);
1111
1112         g_object_unref(proxy);
1113
1114         if (searching_info == NULL) {
1115                 /* Send reply */
1116                 BT_ERR("unbonding_info == NULL");
1117                 goto done;
1118         }
1119
1120         req_info = _bt_get_request_info(searching_info->req_id);
1121         if (req_info == NULL) {
1122                 BT_ERR("req_info == NULL");
1123                 goto done;
1124         }
1125
1126         if (err != NULL) {
1127                 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1128
1129                 if (!strcmp("Operation canceled", err->message)) {
1130                         result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1131                 } else if (!strcmp("In Progress", err->message)) {
1132                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1133                 } else if (!strcmp("Host is down", err->message)) {
1134                         result = BLUETOOTH_ERROR_HOST_DOWN;
1135                 } else {
1136                         result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1137                 }
1138
1139                 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1140                      result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1141                         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1142                         if (remote_dev_info && remote_dev_info->uuids != NULL &&
1143                              remote_dev_info->uuid_count > 0) {
1144                                 result = BLUETOOTH_ERROR_NONE;
1145                                 goto event;
1146                         }
1147                         _bt_free_device_info(remote_dev_info);
1148                 }
1149                 goto dbus_return;
1150         }
1151
1152         remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1153
1154 event:
1155         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1156         for (i=0; i < remote_dev_info->uuid_count; i++) {
1157                 g_variant_builder_add(builder, "s",
1158                         remote_dev_info->uuids[i]);
1159         }
1160         uuid_list = g_variant_new("as", builder);
1161         g_variant_builder_unref(builder);
1162         manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1163                                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1164                                                 TRUE, NULL, NULL);
1165
1166         param = g_variant_new("(isunsbub@asn@ay)",
1167                         result,
1168                         searching_info->addr,
1169                         remote_dev_info->class,
1170                         remote_dev_info->rssi,
1171                         remote_dev_info->name,
1172                         remote_dev_info->paired,
1173                         remote_dev_info->connected,
1174                         remote_dev_info->trust,
1175                         uuid_list,
1176                         remote_dev_info->manufacturer_data_len,
1177                         manufacture_data);
1178
1179
1180         /* Send the event to application */
1181         if (remote_dev_info != NULL) {
1182
1183                 _bt_send_event(BT_ADAPTER_EVENT,
1184                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
1185                         param);
1186
1187                 _bt_free_device_info(remote_dev_info);
1188         }
1189
1190 dbus_return:
1191         if (req_info->context == NULL)
1192                 goto done;
1193
1194         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1195         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1196                                         searching_info->addr);
1197
1198         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1199                 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1200
1201         g_dbus_method_invocation_return_value(req_info->context,
1202                         g_variant_new("(iv)", result, out_param1));
1203
1204
1205         _bt_delete_request_list(req_info->req_id);
1206 done:
1207         if (err)
1208                 g_error_free(err);
1209
1210         if (searching_info) {
1211                 g_free(searching_info->addr);
1212                 g_free(searching_info);
1213                 searching_info = NULL;
1214         }
1215 }
1216
1217 int _bt_search_device(int request_id,
1218                         bluetooth_device_address_t *device_address)
1219 {
1220         char *device_path = NULL;
1221         GDBusProxy *device_proxy = NULL;
1222         GDBusConnection *conn;
1223
1224         GDBusProxy *adapter_proxy;
1225         int result = BLUETOOTH_ERROR_INTERNAL;
1226
1227         BT_CHECK_PARAMETER(device_address, return);
1228
1229         if (bonding_info) {
1230                 BT_ERR("Bonding in progress");
1231                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1232         }
1233
1234         if (searching_info) {
1235                 BT_ERR("Service searching in progress");
1236                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1237         }
1238
1239         adapter_proxy = _bt_get_adapter_proxy();
1240         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1241
1242         /* allocate user data so that it can be retrieved in callback */
1243         searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1244         searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1245         searching_info->req_id = request_id;
1246
1247         _bt_convert_addr_type_to_string(searching_info->addr,
1248                                         device_address->addr);
1249
1250         conn = _bt_get_system_gconn();
1251         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1252
1253
1254         device_path = _bt_get_device_object_path(searching_info->addr);
1255
1256         if (device_path == NULL) {
1257                 BT_ERR("No paired device");
1258                 result = BLUETOOTH_ERROR_NOT_PAIRED;
1259                 goto fail;
1260         }
1261
1262         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1263                                                                 NULL, BT_BLUEZ_NAME,
1264                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1265         g_free(device_path);
1266         if (device_proxy == NULL) {
1267                 result = BLUETOOTH_ERROR_INTERNAL;
1268                 goto fail;
1269         }
1270
1271         g_dbus_proxy_call(device_proxy, "DiscoverServices",
1272                         g_variant_new("(s)", ""),
1273                         G_DBUS_CALL_FLAGS_NONE,
1274                         BT_MAX_DBUS_TIMEOUT,
1275                         NULL,
1276                         (GAsyncReadyCallback)__bt_discover_cb,
1277                         searching_info);
1278
1279         searching_info->device_proxy = device_proxy;
1280
1281         return BLUETOOTH_ERROR_NONE;
1282 fail:
1283         if (device_proxy)
1284                 g_object_unref(device_proxy);
1285
1286         g_free(searching_info->addr);
1287         g_free(searching_info);
1288         searching_info = NULL;
1289         return result;
1290 }
1291
1292 int _bt_cancel_search_device(void)
1293 {
1294         GError *err = NULL;
1295
1296         retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1297
1298         if (searching_info->device_proxy) {
1299                 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1300                                 NULL,
1301                                 G_DBUS_CALL_FLAGS_NONE,
1302                                 -1,
1303                                 NULL,
1304                                 &err);
1305         }
1306         __bt_cancel_search_service_done();
1307
1308         return BLUETOOTH_ERROR_NONE;
1309 }
1310
1311 int _bt_set_alias(bluetooth_device_address_t *device_address,
1312                                       const char *alias)
1313 {
1314         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1315         gchar *device_path = NULL;
1316         GDBusProxy *adapter_proxy;
1317         GDBusProxy *device_proxy;
1318         GError *error = NULL;
1319         GDBusConnection *conn;
1320
1321         BT_CHECK_PARAMETER(device_address, return);
1322         BT_CHECK_PARAMETER(alias, return);
1323
1324         adapter_proxy = _bt_get_adapter_proxy();
1325         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1326
1327         conn = _bt_get_system_gconn();
1328         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1329
1330         _bt_convert_addr_type_to_string(address, device_address->addr);
1331
1332         device_path = _bt_get_device_object_path(address);
1333
1334         if (device_path == NULL) {
1335                 BT_ERR("No paired device");
1336                 return BLUETOOTH_ERROR_NOT_PAIRED;
1337         }
1338
1339         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1340                                                                 NULL, BT_BLUEZ_NAME,
1341                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1342
1343         g_free(device_path);
1344         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1345
1346         g_dbus_proxy_call_sync(device_proxy, "Set",
1347                                 g_variant_new("(ssv)", BT_DEVICE_INTERFACE,  "Alias", g_variant_new("s", alias)),
1348                                 G_DBUS_CALL_FLAGS_NONE,
1349                                 -1,
1350                                 NULL,
1351                                 &error);
1352
1353         g_object_unref(device_proxy);
1354
1355         if (error) {
1356                  BT_ERR("SetProperty error: [%s]", error->message);
1357                  g_error_free(error);
1358                  return BLUETOOTH_ERROR_INTERNAL;
1359         }
1360
1361         return BLUETOOTH_ERROR_NONE;
1362 }
1363
1364 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1365                                       gboolean authorize)
1366 {
1367         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1368         gchar *device_path = NULL;
1369         GDBusProxy *device_proxy;
1370         gboolean previous_value;
1371         GError *error = NULL;
1372         GDBusConnection *conn;
1373         GVariant *result = NULL;
1374         int ret = BLUETOOTH_ERROR_NONE;
1375
1376         BT_CHECK_PARAMETER(device_address, return);
1377
1378         conn = _bt_get_system_gconn();
1379         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1380
1381         _bt_convert_addr_type_to_string(address, device_address->addr);
1382
1383         device_path = _bt_get_device_object_path(address);
1384
1385         if (device_path == NULL) {
1386                 BT_ERR("No paired device");
1387                 return BLUETOOTH_ERROR_NOT_PAIRED;
1388         }
1389
1390         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1391                                                                 NULL, BT_BLUEZ_NAME,
1392                                                                 device_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1393
1394         g_free(device_path);
1395         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1396
1397         result = g_dbus_proxy_call_sync(device_proxy, "Get",
1398                                 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted" ),
1399                                 G_DBUS_CALL_FLAGS_NONE,
1400                                 -1,
1401                                 NULL,
1402                                 &error);
1403         if (error != NULL) {
1404                 BT_ERR("Getting property failed: [%s]\n", error->message);
1405                 g_error_free(error);
1406                 g_object_unref(device_proxy);
1407                 return BLUETOOTH_ERROR_INTERNAL;
1408         }
1409
1410         previous_value = g_variant_get_boolean(result);
1411         g_variant_unref(result);
1412         /* If the input is same with previous value, return error. */
1413         if (previous_value == authorize) {
1414                 BT_ERR("Same value: %d", previous_value);
1415                 g_object_unref(device_proxy);
1416                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1417                 goto done;
1418         }
1419
1420         g_dbus_proxy_call_sync(device_proxy, "Set",
1421                                  g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1422                                  G_DBUS_CALL_FLAGS_NONE,
1423                                  -1,
1424                                  NULL,
1425                                  &error);
1426
1427         g_object_unref(device_proxy);
1428         if (error) {
1429                  BT_ERR("SetProperty error: [%s]", error->message);
1430                  g_error_free(error);
1431                  ret = BLUETOOTH_ERROR_INTERNAL;
1432         }
1433 done:
1434         return ret;
1435 }
1436
1437 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1438                         gboolean *is_connected)
1439 {
1440         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1441         char *object_path = NULL;
1442
1443         GDBusProxy *device_proxy;
1444         GError *error = NULL;
1445         GVariant *value;
1446         GVariant *tmp_value;
1447         GDBusConnection *conn;
1448         GVariant *result = NULL;
1449         int ret = BLUETOOTH_ERROR_NONE;
1450
1451         BT_CHECK_PARAMETER(device_address, return);
1452
1453         conn = _bt_get_system_gconn();
1454         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1455
1456         _bt_convert_addr_type_to_string(address, device_address->addr);
1457
1458         object_path = _bt_get_device_object_path(address);
1459         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1460
1461         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1462                                                                 NULL, BT_BLUEZ_NAME,
1463                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1464         g_free(object_path);
1465         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1466
1467         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1468                                 NULL,
1469                                 G_DBUS_CALL_FLAGS_NONE,
1470                                 -1,
1471                                 NULL,
1472                                 &error);
1473
1474         if (error != NULL || result == NULL) {
1475                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1476                 g_error_free(error);
1477                 g_object_unref(device_proxy);
1478                 return BLUETOOTH_ERROR_INTERNAL;
1479         }
1480
1481         g_variant_get(result , "(@a{sv})", &value);
1482         g_variant_unref(result);
1483
1484         tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1485         if (tmp_value != NULL) {
1486                 g_object_unref(device_proxy);
1487                 return BLUETOOTH_ERROR_INTERNAL;
1488         }
1489         *is_connected = g_variant_get_boolean (tmp_value);
1490
1491         BT_DBG("gatt is connected : %d", *is_connected);
1492         g_variant_unref(tmp_value);
1493         g_variant_unref(value);
1494         g_object_unref(device_proxy);
1495
1496         return ret;
1497 }
1498
1499 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1500                         int connection_type, gboolean *is_connected)
1501 {
1502         char *object_path = NULL;
1503         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1504         GDBusProxy *device_proxy = NULL;
1505         GDBusProxy *adapter_proxy = NULL;
1506         GDBusConnection *conn;
1507         GError *error = NULL;
1508         GVariant *tmp_value = NULL;
1509         GVariant *value = NULL;
1510         GVariant *result = NULL;
1511         char *uuid = NULL;
1512
1513         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1514         retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1515
1516         *is_connected = FALSE;
1517         BT_DBG("connection_type: %d", connection_type);
1518         if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1519                 return _bt_rfcomm_is_device_connected(device_address,
1520                                                 is_connected);
1521         else if (connection_type == BLUETOOTH_GATT_SERVICE)
1522                 return _bt_is_gatt_connected(device_address, is_connected);
1523
1524         adapter_proxy = _bt_get_adapter_proxy();
1525         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1526
1527         conn = _bt_get_system_gconn();
1528         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1529
1530         _bt_convert_addr_type_to_string(address, device_address->addr);
1531
1532         if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE)     {
1533                 object_path = _bt_get_adapter_path();
1534                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1535                                                                         NULL, BT_BLUEZ_NAME,
1536                                                                         object_path, BT_NETWORK_SERVER_INTERFACE,  NULL, NULL);
1537                 g_free(object_path);
1538                 if (device_proxy == NULL) {
1539                         BT_DBG("Device don't have this service");
1540                         return BLUETOOTH_ERROR_INTERNAL;
1541                 }
1542
1543                 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1544                                         g_variant_new("(s)", address),
1545                                         G_DBUS_CALL_FLAGS_NONE,
1546                                         -1,
1547                                         NULL,
1548                                         &error);
1549
1550                 if (result == NULL) {
1551                         BT_ERR("Error occured in Proxy call");
1552                         if (error) {
1553                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1554                                 g_error_free(error);
1555                         }
1556                         g_object_unref(device_proxy);
1557                         return BLUETOOTH_ERROR_INTERNAL;
1558                 }
1559                 g_variant_get(result , "(@a{sv})", &value);
1560                 g_variant_unref(result);
1561
1562
1563                 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1564                 if (tmp_value != NULL) {
1565                         g_object_unref(device_proxy);
1566                         return BLUETOOTH_ERROR_INTERNAL;
1567                 }
1568                 *is_connected = g_variant_get_boolean (tmp_value);
1569                 g_variant_unref(tmp_value);
1570                 g_variant_unref(value);
1571         } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1572                 return _bt_is_network_connected(_bt_get_net_conn(),
1573                                                 device_address->addr, is_connected);
1574         } else {
1575                 uuid = _bt_get_profile_uuid128(connection_type);
1576                 if (uuid == NULL) {
1577                         BT_ERR("uuid is NULL");
1578                         return BLUETOOTH_ERROR_INTERNAL;
1579                 }
1580
1581                 BT_DBG("uuid: %s", uuid);
1582
1583                 object_path = _bt_get_device_object_path(address);
1584                 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1585                 BT_DBG("object_path: %s", object_path);
1586                 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1587                                                                         NULL, BT_BLUEZ_NAME,
1588                                                                         object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1589                 g_free(object_path);
1590                 if (device_proxy == NULL) {
1591                         BT_DBG("Device don't have this service");
1592                         g_free(uuid);
1593                         return BLUETOOTH_ERROR_INTERNAL;
1594                 }
1595
1596                 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1597                                         g_variant_new("(s)", uuid),
1598                                         G_DBUS_CALL_FLAGS_NONE,
1599                                         -1,
1600                                         NULL,
1601                                         &error);
1602
1603                 if (result == NULL) {
1604                         BT_ERR("Error occured in Proxy call");
1605                         if (error) {
1606                                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1607                                 g_error_free(error);
1608                         }
1609                         g_object_unref(device_proxy);
1610                         return BLUETOOTH_ERROR_INTERNAL;
1611                 }
1612                 g_variant_get(result, "(b)", is_connected);
1613                 g_free(uuid);
1614                 g_variant_unref(result);
1615         }
1616
1617         g_object_unref(device_proxy);
1618         return BLUETOOTH_ERROR_NONE;
1619 }
1620
1621 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1622                         bluetooth_connected_link_t *connected)
1623 {
1624         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1625         char *object_path = NULL;
1626
1627         GDBusProxy *device_proxy;
1628         GError *error = NULL;
1629         GDBusConnection *conn;
1630         GVariant *tmp_value = NULL;
1631         GVariant *value = NULL;
1632         GVariant *result = NULL;
1633
1634         int ret = BLUETOOTH_ERROR_NONE;
1635
1636         BT_CHECK_PARAMETER(device_address, return);
1637
1638         conn = _bt_get_system_gconn();
1639         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1640
1641         _bt_convert_addr_type_to_string(address, device_address->addr);
1642
1643         object_path = _bt_get_device_object_path(address);
1644         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1645
1646         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1647                                                                 NULL, BT_BLUEZ_NAME,
1648                                                                 object_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
1649         g_free(object_path);
1650         if (device_proxy == NULL) {
1651                 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1652                 return BLUETOOTH_ERROR_NONE;
1653         }
1654
1655         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1656                                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1657                                         G_DBUS_CALL_FLAGS_NONE,
1658                                         -1,
1659                                         NULL,
1660                                         &error);
1661
1662         if (error != NULL) {
1663                 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1664                 g_error_free(error);
1665                 g_object_unref(device_proxy);
1666                 return BLUETOOTH_ERROR_INTERNAL;
1667         }
1668
1669         g_variant_get(result , "(@a{sv})", &value);
1670         g_variant_unref(result);
1671
1672         tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1673         if (tmp_value != NULL) {
1674                 g_object_unref(device_proxy);
1675                 g_variant_unref(value);
1676                 return BLUETOOTH_ERROR_INTERNAL;
1677         }
1678         *connected = g_variant_get_boolean (tmp_value);
1679         g_variant_unref(tmp_value);
1680         g_variant_unref(value);
1681         g_object_unref(device_proxy);
1682
1683         return ret;
1684 }
1685
1686 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1687 {
1688         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1689         gchar *device_path = NULL;
1690         GError *error = NULL;
1691         GDBusProxy *device_proxy = NULL;
1692         GDBusProxy *adapter_proxy;
1693         GDBusConnection *conn;
1694         int ret = BLUETOOTH_ERROR_NONE;
1695
1696         BT_CHECK_PARAMETER(bd_addr, return);
1697
1698         _bt_convert_addr_type_to_string(device_address,
1699                         (unsigned char *)bd_addr->addr);
1700
1701         conn = _bt_get_system_gconn();
1702         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1703
1704         adapter_proxy = _bt_get_adapter_proxy();
1705         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1706
1707         device_path = _bt_get_device_object_path(device_address);
1708         if (device_path == NULL) {
1709                 BT_DBG("device_path NULL");
1710                 ret = BLUETOOTH_ERROR_INTERNAL;
1711                 return ret;
1712         }
1713
1714         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1715
1716         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1717                                                                 NULL, BT_BLUEZ_NAME,
1718                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1719         g_free(device_path);
1720         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1721
1722         g_dbus_proxy_call_sync(device_proxy, "ConnectLE",
1723                                  g_variant_new("(b)", auto_connect),
1724                                  G_DBUS_CALL_FLAGS_NONE,
1725                                  -1,
1726                                  NULL,
1727                                  &error);
1728
1729         if (error) {
1730                 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1731                 g_error_free(error);
1732                 g_object_unref(device_proxy);
1733                 return BLUETOOTH_ERROR_INTERNAL;
1734         }
1735
1736         g_object_unref(device_proxy);
1737
1738         return ret;
1739 }
1740
1741 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1742 {
1743         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1744         gchar *device_path = NULL;
1745         GError *error = NULL;
1746         GDBusProxy *device_proxy = NULL;
1747         GDBusProxy *adapter_proxy;
1748         GDBusConnection *conn;
1749         int ret = BLUETOOTH_ERROR_NONE;
1750
1751         BT_CHECK_PARAMETER(bd_addr, return);
1752
1753         _bt_convert_addr_type_to_string(device_address,
1754                         (unsigned char *)bd_addr->addr);
1755
1756         conn = _bt_get_system_gconn();
1757         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1758
1759         adapter_proxy = _bt_get_adapter_proxy();
1760         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1761
1762         device_path = _bt_get_device_object_path(device_address);
1763         if (device_path == NULL) {
1764                 BT_DBG("device_path NULL");
1765                 ret = BLUETOOTH_ERROR_INTERNAL;
1766                 return ret;
1767         }
1768
1769         retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1770
1771         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1772                                                                 NULL, BT_BLUEZ_NAME,
1773                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1774         g_free(device_path);
1775         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1776
1777         g_dbus_proxy_call_sync(device_proxy, "DisconnectLE",
1778                                 NULL,
1779                                 G_DBUS_CALL_FLAGS_NONE,
1780                                 -1,
1781                                 NULL,
1782                                 &error);
1783         if (error) {
1784                 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1785                 g_error_free(error);
1786                 g_object_unref(device_proxy);
1787                 return BLUETOOTH_ERROR_INTERNAL;
1788         }
1789
1790         g_object_unref(device_proxy);
1791
1792         return ret;
1793 }
1794
1795 int _bt_connect_profile(char *address, char *uuid,
1796                                                 void *cb, gpointer func_data)
1797 {
1798         char *object_path;
1799         GDBusProxy *proxy;
1800         GDBusConnection *conn;
1801         GDBusProxy *adapter_proxy;
1802         GError *error = NULL;
1803
1804         conn = _bt_get_system_gconn();
1805         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1806
1807         object_path = _bt_get_device_object_path(address);
1808         if (object_path == NULL) {
1809                 BT_ERR("No searched device");
1810
1811                 adapter_proxy = _bt_get_adapter_proxy();
1812                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1813
1814                 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1815                                          g_variant_new("(s)", address),
1816                                          G_DBUS_CALL_FLAGS_NONE,
1817                                          -1,
1818                                          NULL,
1819                                          &error);
1820
1821                 if (error != NULL) {
1822                         BT_ERR("CreateDevice Fail: %s", error->message);
1823                         g_error_free(error);
1824                 }
1825
1826                 object_path = _bt_get_device_object_path(address);
1827         }
1828         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1829
1830         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1831                                                                 NULL, BT_BLUEZ_NAME,
1832                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1833         g_free(object_path);
1834         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1835
1836         g_dbus_proxy_call(proxy, "ConnectProfile",
1837                                 g_variant_new("(s)", uuid),
1838                                 G_DBUS_CALL_FLAGS_NONE,
1839                                 BT_MAX_DBUS_TIMEOUT,
1840                                 NULL,
1841                                 (GAsyncReadyCallback)cb,
1842                                 func_data);
1843
1844         return BLUETOOTH_ERROR_NONE;
1845 }
1846
1847 int _bt_disconnect_profile(char *address, char *uuid,
1848                                                 void *cb, gpointer func_data)
1849 {
1850         char *object_path;
1851         GDBusProxy *proxy;
1852         GDBusConnection *conn;
1853
1854         conn = _bt_get_system_gconn();
1855         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1856
1857         object_path = _bt_get_device_object_path(address);
1858         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1859
1860         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1861                                                                 NULL, BT_BLUEZ_NAME,
1862                                                                 object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
1863         g_free(object_path);
1864         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1865
1866         g_dbus_proxy_call(proxy, "DisconnectProfile",
1867                                         g_variant_new("(s)", uuid),
1868                                         G_DBUS_CALL_FLAGS_NONE,
1869                                         BT_MAX_DBUS_TIMEOUT,
1870                                         NULL,
1871                                         (GAsyncReadyCallback)cb,
1872                                         func_data);
1873
1874         return BLUETOOTH_ERROR_NONE;
1875 }
1876
1877 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1878                 int low_threshold, int in_range_threshold, int high_threshold)
1879 {
1880         int ret = BLUETOOTH_ERROR_NONE;
1881         GDBusProxy *proxy;
1882         GError *error = NULL;
1883         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1884
1885         BT_CHECK_PARAMETER(bd_addr, return);
1886         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1887                         bd_addr->addr[0], bd_addr->addr[1],
1888                         bd_addr->addr[2], bd_addr->addr[3],
1889                         bd_addr->addr[4], bd_addr->addr[5],
1890                         link_type);
1891         BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1892                         in_range_threshold, high_threshold);
1893
1894         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1895
1896         proxy = _bt_get_adapter_proxy();
1897         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1898
1899         g_dbus_proxy_call_sync(proxy, "EnableRssi",
1900                                 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
1901                                 G_DBUS_CALL_FLAGS_NONE,
1902                                 -1,
1903                                 NULL,
1904                                 &error);
1905         if (error != NULL) {
1906                         BT_ERR("Dbus Call Error:[%s]", error->message);
1907                         g_error_free(error);
1908                         ret = BLUETOOTH_ERROR_INTERNAL;
1909         }
1910         return ret;
1911 }
1912
1913 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1914                                         int link_type)
1915 {
1916         int ret = BLUETOOTH_ERROR_NONE;
1917         GDBusProxy *proxy;
1918         GError *error = NULL;
1919         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1920
1921         BT_CHECK_PARAMETER(bd_addr, return);
1922         BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1923                         bd_addr->addr[0], bd_addr->addr[1],
1924                         bd_addr->addr[2], bd_addr->addr[3],
1925                         bd_addr->addr[4], bd_addr->addr[5],
1926                         link_type);
1927
1928         _bt_convert_addr_type_to_string(address, bd_addr->addr);
1929
1930         proxy = _bt_get_adapter_proxy();
1931         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1932
1933         g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
1934                                 g_variant_new("(si)", address, link_type),
1935                                 G_DBUS_CALL_FLAGS_NONE,
1936                                 -1,
1937                                 NULL,
1938                                 &error);
1939
1940         if (error != NULL) {
1941                         BT_ERR("Dbus Call Error:[%s]", error->message);
1942                         g_error_free(error);
1943                         ret = BLUETOOTH_ERROR_INTERNAL;
1944         }
1945         return ret;
1946 }
1947
1948 int _bt_le_conn_update(unsigned char *device_address,
1949                                 guint16 interval_min, guint16 interval_max,
1950                                 guint16 latency, guint16 time_out)
1951 {
1952         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1953         gchar *device_path = NULL;
1954         GError *error = NULL;
1955         GDBusProxy *device_proxy = NULL;
1956         GDBusConnection *conn;
1957         GVariant *reply;
1958         guint32 min, max, to;
1959         guint32 min_supervision_to;
1960         int ret = BLUETOOTH_ERROR_NONE;
1961
1962         BT_DBG("+");
1963
1964         BT_CHECK_PARAMETER(device_address, return);
1965
1966         BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1967                         interval_min, interval_max, latency, time_out);
1968
1969         if (interval_min > interval_max ||
1970                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
1971                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
1972                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1973                 goto fail;
1974         }
1975
1976         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1977                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
1978                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1979                 goto fail;
1980         }
1981
1982         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
1983                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1984                 goto fail;
1985         }
1986
1987         /*
1988          * The Supervision_Timeout in milliseconds shall be larger than
1989          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
1990          * where Conn_Interval_Max is given in milliseconds.
1991          */
1992         min_supervision_to = (1 + latency) * interval_max * 2;
1993         if (time_out <= min_supervision_to) {
1994                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1995                 goto fail;
1996         }
1997
1998         _bt_convert_addr_type_to_string(address, device_address);
1999
2000         BT_DBG("Remote device address: %s", address);
2001
2002         device_path = _bt_get_device_object_path(address);
2003
2004         if (device_path == NULL) {
2005                 BT_DBG("device_path NULL");
2006                 ret = BLUETOOTH_ERROR_INTERNAL;
2007                 goto fail;
2008         }
2009
2010         conn = _bt_get_system_gconn();
2011         if (conn == NULL) {
2012                 BT_DBG("conn NULL");
2013                 ret = BLUETOOTH_ERROR_INTERNAL;
2014                 goto fail;
2015         }
2016
2017         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2018                                                                 NULL, BT_BLUEZ_NAME,
2019                                                                 device_path, BT_DEVICE_INTERFACE,  NULL, NULL);
2020
2021         g_free(device_path);
2022         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2023
2024         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2025         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2026         to = time_out / BT_LE_CONN_TO_SPLIT;
2027
2028         reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2029                                 g_variant_new("(uuuu)", min, max, latency, to),
2030                                 G_DBUS_CALL_FLAGS_NONE,
2031                                 -1,
2032                                 NULL,
2033                                 &error);
2034
2035         g_object_unref(device_proxy);
2036         BT_ERR("LeConnUpdate Call Error for %s", address);
2037         if (reply == NULL) {
2038                 if (error) {
2039                         BT_ERR("Error %s[%s]", error->message, address);
2040                         g_error_free(error);
2041                         return BLUETOOTH_ERROR_INTERNAL;
2042                 }
2043         }
2044         g_variant_unref(reply);
2045         BT_DBG("-");
2046
2047 fail:
2048         return ret;
2049 }