6be1d05c8405a6fd7fea558bd9b1e5c5e0a5a01c
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter.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 <stdio.h>
25 #include <gio/gio.h>
26 #include <glib.h>
27 #include <dlog.h>
28 #include <string.h>
29 #include <vconf.h>
30 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
31 #include <syspopup_caller.h>
32 #endif
33 #include <aul.h>
34 #ifdef ENABLE_TIZEN_2_4
35 #include <journal/device.h>
36 #endif
37 #include <dbus/dbus-glib.h>
38 #include <dbus/dbus.h>
39
40 #include <bundle.h>
41 #if 0
42 #include <eventsystem.h>
43 #endif
44 #include <bundle_internal.h>
45
46 #include "alarm.h"
47 #include "bluetooth-api.h"
48 #include "bt-internal-types.h"
49 #include "bt-service-common.h"
50 #include "bt-service-event.h"
51 #include "bt-service-adapter.h"
52 #include "bt-service-util.h"
53 #include "bt-service-network.h"
54 #include "bt-service-obex-server.h"
55 #include "bt-service-agent.h"
56 #include "bt-service-main.h"
57 #include "bt-service-avrcp.h"
58 #include "bt-service-device.h"
59
60 typedef struct {
61         guint event_id;
62         int timeout;
63         time_t start_time;
64         gboolean alarm_init;
65         int alarm_id;
66 } bt_adapter_timer_t;
67
68 bt_adapter_timer_t visible_timer = {0, };
69
70 static gboolean is_discovering;
71 static gboolean cancel_by_user;
72 static bt_status_t adapter_status = BT_DEACTIVATED;
73 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
74 static void *adapter_agent = NULL;
75 static GDBusProxy *core_proxy = NULL;
76 static guint timer_id = 0;
77 static guint le_timer_id = 0;
78
79 static int status_reg_id;
80
81 #define BT_CORE_NAME "org.projectx.bt_core"
82 #define BT_CORE_PATH "/org/projectx/bt_core"
83 #define BT_CORE_INTERFACE "org.projectx.btcore"
84
85 #define BT_DISABLE_TIME 500 /* 500 ms */
86
87 GDBusProxy *_bt_init_core_proxy(void)
88 {
89         GDBusProxy *proxy;
90         GDBusConnection *conn;
91
92         conn = _bt_get_system_gconn();
93         if (!conn)
94                 return NULL;
95
96         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
97                                         NULL,
98                                         BT_CORE_NAME,
99                                         BT_CORE_PATH,
100                                         BT_CORE_INTERFACE,
101                                         NULL, NULL);
102
103         if (!proxy)
104                 return NULL;
105
106         core_proxy = proxy;
107
108         return proxy;
109 }
110
111 static GDBusProxy *__bt_get_core_proxy(void)
112 {
113        return (core_proxy) ? core_proxy : _bt_init_core_proxy();
114 }
115
116 static gboolean __bt_is_factory_test_mode(void)
117 {
118         int mode = 0;
119
120 #ifdef ENABLE_TIZEN_2_4
121         if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
122                 BT_ERR("Get the DUT Mode fail");
123                 return TRUE;
124         }
125 #endif
126
127         if (mode != FALSE) {
128                 BT_INFO("DUT Test Mode !!");
129                 return TRUE;
130         }
131
132         return FALSE;
133 }
134
135 static gboolean __bt_timeout_handler(gpointer user_data)
136 {
137         int result = BLUETOOTH_ERROR_NONE;
138         time_t current_time;
139         int time_diff;
140
141         /* Take current time */
142         time(&current_time);
143         time_diff = difftime(current_time, visible_timer.start_time);
144
145         /* Send event to application */
146         _bt_send_event(BT_ADAPTER_EVENT,
147                         BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
148                         g_variant_new("(in)", result, time_diff));
149
150         if (visible_timer.timeout <= time_diff) {
151                 g_source_remove(visible_timer.event_id);
152                 visible_timer.event_id = 0;
153                 visible_timer.timeout = 0;
154
155 #ifndef TIZEN_WEARABLE
156                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
157                         BT_ERR("Set vconf failed\n");
158 #endif
159                 return FALSE;
160         }
161
162         return TRUE;
163 }
164
165 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
166 {
167         BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
168
169         int result = BLUETOOTH_ERROR_NONE;
170         int timeout = 0;
171
172         if (alarm_id != visible_timer.alarm_id)
173                 return 0;
174
175         if (visible_timer.event_id) {
176                 _bt_send_event(BT_ADAPTER_EVENT,
177                                 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
178                                 g_variant_new("(in)", result, timeout));
179                 g_source_remove(visible_timer.event_id);
180                 visible_timer.event_id = 0;
181                 visible_timer.timeout = 0;
182
183 #ifndef TIZEN_WEARABLE
184                 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
185                         BT_ERR("Set vconf failed\n");
186 #endif
187         }
188         /* Switch Off visibility in Bluez */
189         _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
190         visible_timer.alarm_id = 0;
191         return 0;
192 }
193
194 static void __bt_visibility_alarm_create()
195 {
196         alarm_id_t alarm_id;
197         int result;
198
199         result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
200                                                 0, NULL, &alarm_id);
201         if(result < 0) {
202                 BT_ERR("Failed to create alarm error = %d\n", result);
203         } else {
204                 BT_DBG("Alarm created = %d\n", alarm_id);
205                 visible_timer.alarm_id = alarm_id;
206         }
207 }
208
209 static void __bt_visibility_alarm_remove()
210 {
211         if (visible_timer.event_id > 0) {
212                 g_source_remove(visible_timer.event_id);
213                 visible_timer.event_id = 0;
214         }
215
216         if (visible_timer.alarm_id > 0) {
217                 alarmmgr_remove_alarm(visible_timer.alarm_id);
218                 visible_timer.alarm_id = 0;
219         }
220 }
221
222 int __bt_set_visible_time(int timeout)
223 {
224         int result;
225
226         __bt_visibility_alarm_remove();
227
228         visible_timer.timeout = timeout;
229
230         if (timeout <= 0)
231                 return BLUETOOTH_ERROR_NONE;
232
233 #ifndef TIZEN_WEARABLE
234         if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
235                 BT_ERR("Set vconf failed");
236 #endif
237
238         if (!visible_timer.alarm_init) {
239                 /* Set Alarm timer to switch off BT */
240                 result = alarmmgr_init("bt-service");
241                 if (result != 0)
242                         return BLUETOOTH_ERROR_INTERNAL;
243
244                 visible_timer.alarm_init = TRUE;
245         }
246
247         result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
248         if (result != 0)
249                 return BLUETOOTH_ERROR_INTERNAL;
250
251         /* Take start time */
252         time(&(visible_timer.start_time));
253         visible_timer.event_id = g_timeout_add_seconds(1,
254                                 __bt_timeout_handler, NULL);
255
256         __bt_visibility_alarm_create();
257
258         return BLUETOOTH_ERROR_NONE;
259 }
260
261 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
262 {
263         int i = 0;
264         char **parts;
265         GVariantIter *iter;
266         gchar *uuid = NULL;
267
268         ret_if(value == NULL);
269         ret_if(dev == NULL);
270
271         dev->service_index = 0;
272
273         g_variant_get(value, "as", &iter);
274         while (g_variant_iter_loop(iter, "s", &uuid)) {
275                 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
276                 parts = g_strsplit(uuid, "-", -1);
277
278                 if (parts == NULL || parts[0] == NULL) {
279                         g_free(uuid);
280                         break;
281                 }
282
283                 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
284                 g_strfreev(parts);
285
286                 dev->service_index++;
287                 i++;
288         }
289         g_variant_iter_free(iter);
290 }
291
292 static int __bt_get_bonded_device_info(gchar *device_path,
293                 bluetooth_device_info_t *dev_info)
294 {
295         GError *error = NULL;
296         GDBusProxy *device_proxy;
297         const gchar *address = NULL;
298         const gchar *name = NULL;
299         unsigned int cod = 0;
300         gint rssi = 0;
301         gboolean trust = FALSE;
302         gboolean paired = FALSE;
303         guchar connected = 0;
304         GByteArray *manufacturer_data = NULL;
305         int ret;
306         GDBusConnection *conn;
307         GVariant *result;
308         GVariantIter *property_iter;
309         const gchar *key;
310         GVariant *value;
311         guint8 char_value;
312         GVariantIter *char_value_iter;
313
314         BT_CHECK_PARAMETER(device_path, return);
315         BT_CHECK_PARAMETER(dev_info, return);
316
317         conn = _bt_get_system_gconn();
318         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
319
320         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
321                                         NULL,
322                                         BT_BLUEZ_NAME,
323                                         device_path,
324                                         BT_PROPERTIES_INTERFACE,
325                                         NULL, NULL);
326
327         retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
328
329         result = g_dbus_proxy_call_sync(device_proxy,
330                                 "GetAll",
331                                 g_variant_new("(s)", BT_DEVICE_INTERFACE),
332                                 G_DBUS_CALL_FLAGS_NONE,
333                                 -1,
334                                 NULL,
335                                 &error);
336
337         if (!result) {
338                 BT_ERR("Error occured in Proxy call");
339                 if (error != NULL) {
340                         BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
341                         g_clear_error(&error);
342                 }
343                 g_object_unref(device_proxy);
344                 return BLUETOOTH_ERROR_INTERNAL;
345         }
346
347         g_object_unref(device_proxy);
348
349         g_variant_get(result, "(a{sv})", &property_iter);
350
351         while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
352                 if (!g_strcmp0(key,"Paired")) {
353                         paired = g_variant_get_boolean(value);
354                 } else if(!g_strcmp0(key, "Address")) {
355                         address = g_variant_get_string(value, NULL);
356                 } else if (!g_strcmp0(key, "Alias")) {
357                         name = g_variant_get_string(value, NULL);
358                 } else if (!g_strcmp0(key, "Name")) {
359                         if (!name)
360                                 name = g_variant_get_string(value, NULL);
361                 } else if (!g_strcmp0(key, "Class")) {
362                         cod = g_variant_get_uint32(value);
363                 } else if (!g_strcmp0(key, "Connected")) {
364                         connected = g_variant_get_byte(value);
365                 } else if (!g_strcmp0(key, "Trusted")) {
366                         trust = g_variant_get_boolean(value);
367                 } else if (!g_strcmp0(key, "RSSI")) {
368                         rssi = g_variant_get_int16(value);
369                 } else if (!g_strcmp0(key, "UUIDs")) {
370                         __bt_get_service_list(value, dev_info);
371                 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
372                         dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
373                 } else if (!g_strcmp0(key, "ManufacturerData")) {
374                         manufacturer_data = g_byte_array_new();
375                         g_variant_get(value, "ay", &char_value_iter);
376                         while(g_variant_iter_loop(char_value_iter, "y",  &char_value)) {
377                                 g_byte_array_append(manufacturer_data, &char_value, 1);
378                         }
379                         if (manufacturer_data) {
380                                 if (manufacturer_data->len > 0) {
381                                         memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
382                                                 manufacturer_data->len);
383                                 }
384                         }
385                 }
386         }
387
388         BT_DBG("trust: %d, paired: %d", trust, paired);
389
390         g_variant_unref(result);
391
392         if ((paired == FALSE) && (trust == FALSE)) {
393                 return BLUETOOTH_ERROR_NOT_PAIRED;
394         }
395
396         _bt_convert_addr_string_to_type(dev_info->device_address.addr,
397                                         address);
398
399         _bt_divide_device_class(&dev_info->device_class, cod);
400
401         g_strlcpy(dev_info->device_name.name, name,
402                         BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
403
404         dev_info->rssi = rssi;
405         dev_info->trust = trust;
406         dev_info->paired = paired;
407         dev_info->connected = connected;
408         ret = BLUETOOTH_ERROR_NONE;
409
410         return ret;
411 }
412
413 void _bt_set_discovery_status(gboolean mode)
414 {
415         is_discovering = mode;
416 }
417
418 void _bt_set_cancel_by_user(gboolean value)
419 {
420         cancel_by_user = value;
421 }
422
423 gboolean _bt_get_cancel_by_user(void)
424 {
425         return cancel_by_user;
426 }
427
428 void _bt_adapter_set_status(bt_status_t status)
429 {
430         BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
431         adapter_status = status;
432 }
433
434 bt_status_t _bt_adapter_get_status(void)
435 {
436         return adapter_status;
437 }
438
439 void _bt_adapter_set_le_status(bt_le_status_t status)
440 {
441         BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
442         adapter_le_status = status;
443 }
444
445 bt_le_status_t _bt_adapter_get_le_status(void)
446 {
447         return adapter_le_status;
448 }
449
450 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
451 {
452         char *phone_name = NULL;
453         char *ptr = NULL;
454
455         if (node == NULL)
456                 return;
457
458         if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
459                 phone_name = vconf_keynode_get_str(node);
460
461                 if (phone_name && strlen(phone_name) != 0) {
462                         if (!g_utf8_validate(phone_name, -1,
463                                                         (const char **)&ptr))
464                                 *ptr = '\0';
465
466                         _bt_set_local_name(phone_name);
467                 }
468         }
469 }
470
471 #ifndef TIZEN_WEARABLE
472 static void __bt_set_visible_mode(void)
473 {
474         int timeout = 0;
475
476         if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
477                 BT_ERR("Fail to get the timeout value");
478
479         if (timeout == -1) {
480                 if (_bt_set_discoverable_mode(
481                         BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
482                         timeout) != BLUETOOTH_ERROR_NONE) {
483                         if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
484                                 BT_ERR("Set vconf failed");
485                 }
486         }
487 }
488 #endif
489
490 static void __bt_set_local_name(void)
491 {
492         char *phone_name = NULL;
493         char *ptr = NULL;
494
495         phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
496
497         if (!phone_name)
498                 return;
499
500         if (strlen(phone_name) != 0) {
501                 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
502                         *ptr = '\0';
503
504                 _bt_set_local_name(phone_name);
505         }
506         free(phone_name);
507 }
508
509 static int __bt_set_enabled(void)
510 {
511         BT_DBG("+");
512         int adapter_status = BT_ADAPTER_DISABLED;
513         int result = BLUETOOTH_ERROR_NONE;
514
515         _bt_check_adapter(&adapter_status);
516
517         if (adapter_status == BT_ADAPTER_DISABLED) {
518                 BT_ERR("Bluetoothd is not running");
519                 return BLUETOOTH_ERROR_INTERNAL;
520         }
521
522 #ifndef TIZEN_WEARABLE
523         __bt_set_visible_mode();
524 #endif
525
526         __bt_set_local_name();
527
528         /* Update Bluetooth Status to notify other modules */
529         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
530                 BT_ERR("Set vconf failed\n");
531
532         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
533                 BT_ERR("Set vconf failed\n");
534 #if 0
535         if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
536                                                 EVT_VAL_BT_ON) != ES_R_OK)
537                 BT_ERR("Fail to set value");
538 #endif
539
540         /* Send enabled event to API */
541         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
542                                 g_variant_new("(i)", result));
543         BT_DBG("-");
544
545         return BLUETOOTH_ERROR_NONE;
546 }
547
548 void _bt_set_disabled(int result)
549 {
550         int power_off_status = 0;
551         int ret;
552         int ret_pm_ignore;
553         int pm_ignore_mode = 0;
554
555         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
556         BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
557
558         ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
559
560         /* Update the vconf BT status in normal Deactivation case only */
561
562 #if 0
563         if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
564                 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
565 #endif
566                 BT_DBG("Update vconf for BT normal Deactivation");
567
568                 if (result == BLUETOOTH_ERROR_TIMEOUT)
569                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0 )
570                                 BT_ERR("Set vconf failed");
571
572                 /* Update Bluetooth Status to notify other modules */
573                 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
574                         BT_ERR("Set vconf failed");
575 #if 0
576                 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
577                                                         EVT_VAL_BT_OFF) != ES_R_OK)
578                         BT_ERR("Fail to set value");
579 #endif
580 //
581
582         if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
583                 BT_ERR("Set vconf failed\n");
584
585         _bt_adapter_set_status(BT_DEACTIVATED);
586
587         if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
588                 /* Send disabled event */
589                 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
590                                 g_variant_new("(i)", result));
591         }
592
593         BT_INFO("Adapter disabled");
594 }
595
596 static int __bt_set_le_enabled(void)
597 {
598         BT_DBG("+");
599         int result = BLUETOOTH_ERROR_NONE;
600         bt_status_t status;
601
602         __bt_set_local_name();
603
604         /* Update Bluetooth Status to notify other modules */
605         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
606                 BT_ERR("Set vconf failed\n");
607
608 #if 0
609         if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
610                                                 EVT_VAL_BT_LE_ON) != ES_R_OK)
611                 BT_ERR("Fail to set value");
612 #endif
613
614         /* Send enabled event to API */
615         /*
616         _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
617                                 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
618         */
619         status = _bt_adapter_get_status();
620         if (status == BT_DEACTIVATED) {
621                 BT_INFO("BREDR is off, turn off PSCAN");
622                 _bt_set_connectable(FALSE);
623         }
624         if (le_timer_id > 0) {
625                 g_source_remove(le_timer_id);
626                 le_timer_id = 0;
627         }
628
629         /* Send enabled event to API */
630         _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
631                                 g_variant_new("(i)", result));
632
633         BT_DBG("-");
634         return BLUETOOTH_ERROR_NONE;
635 }
636
637 void _bt_set_le_disabled(int result)
638 {
639         int power_off_status;
640         int ret;
641
642         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
643         BT_DBG("ret : %d", ret);
644         BT_DBG("power_off_status : %d", power_off_status);
645
646         /* Update Bluetooth Status to notify other modules */
647         BT_DBG("Update vconf for BT LE normal Deactivation");
648
649         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
650                 BT_ERR("Set vconf failed\n");
651         _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
652 #if 0
653         if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
654                                                 EVT_VAL_BT_LE_OFF) != ES_R_OK)
655                 BT_ERR("Fail to set value");
656 #endif
657
658         if (_bt_adapter_get_status() != BT_DEACTIVATED) {
659                 /* Send disabled event */
660                 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
661                                 g_variant_new_int32(result));
662         }
663 }
664
665 void *_bt_get_adapter_agent(void)
666 {
667         return adapter_agent;
668 }
669
670 int _bt_enable_core(void)
671 {
672         GDBusProxy *proxy;
673         GVariant *result;
674         GError *error = NULL;
675
676         proxy = __bt_get_core_proxy();
677         retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
678
679         /* Clean up the process */
680         result = g_dbus_proxy_call_sync(proxy,
681                                 "EnableCore",
682                                 NULL,
683                                 G_DBUS_CALL_FLAGS_NONE,
684                                 -1,
685                                 NULL,
686                                 &error);
687
688         if (!result) {
689                 if (error != NULL) {
690                         BT_ERR("Bt core call failed(Error: %s)", error->message);
691                         g_clear_error(&error);
692                 } else
693                         BT_ERR("Bt core call failed");
694                 return BLUETOOTH_ERROR_INTERNAL;
695         }
696
697         g_variant_unref(result);
698         return BLUETOOTH_ERROR_NONE;
699 }
700
701 #if defined(TIZEN_BT_FLIGHTMODE_ENABLED) || !defined(TIZEN_WEARABLE)
702 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
703 {
704         gboolean flight_mode = FALSE;
705         int power_saving_mode = 0;
706         int type;
707
708         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
709         type = vconf_keynode_get_type(node);
710         if (type == VCONF_TYPE_BOOL) {
711                 flight_mode = vconf_keynode_get_bool(node);
712                 if (flight_mode != TRUE) {
713                         BT_ERR("Ignore the event");
714                         return;
715                 }
716         } else if (type == VCONF_TYPE_INT) {
717                 power_saving_mode = vconf_keynode_get_int(node);
718                 if (power_saving_mode != 2) {
719                         BT_ERR("Ignore the event");
720                         return;
721                 }
722         } else {
723                 BT_ERR("Invaild vconf key type : %d", type);
724                 return;
725         }
726
727         _bt_enable_core();
728 }
729 #endif
730
731 void _bt_service_register_vconf_handler(void)
732 {
733         BT_DBG("+");
734
735 #ifdef TIZEN_TELEPHONY_ENABLED
736         if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
737                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
738                 BT_ERR("Unable to register key handler");
739 #else
740         BT_DBG("Telephony is disabled");
741 #endif
742
743 #ifndef TIZEN_WEARABLE
744 #ifdef ENABLE_TIZEN_2_4
745         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
746                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
747                 BT_ERR("Unable to register key handler");
748 #endif
749 #endif
750 }
751
752 void _bt_service_unregister_vconf_handler(void)
753 {
754         BT_DBG("+");
755
756 #ifdef TIZEN_TELEPHONY_ENABLED
757         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
758                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
759 #endif
760
761 #ifndef TIZEN_WEARABLEi
762 #ifdef ENABLE_TIZEN_2_4
763         vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
764                         (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
765 #endif
766 #endif
767 }
768
769 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
770 {
771         const char *bt_status = NULL;
772         const char *bt_le_status = NULL;
773         const char *bt_transfering_status = NULL;
774         BT_DBG("bt state set event(%s) received", event_name);
775 #ifdef ENABLE_TIZEN_2_4
776         bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
777         BT_DBG("bt_state: (%s)", bt_status);
778
779         bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
780         BT_DBG("bt_state: (%s)", bt_le_status);
781 #endif
782 }
783
784 void _bt_handle_adapter_added(void)
785 {
786         BT_DBG("+");
787         bt_status_t status;
788         bt_le_status_t le_status;
789         int ret;
790
791         if (timer_id > 0) {
792                 BT_DBG("g_source is removed");
793                 g_source_remove(timer_id);
794                 timer_id = 0;
795         }
796
797         status = _bt_adapter_get_status();
798         le_status = _bt_adapter_get_le_status();
799         BT_DBG("status : %d", status);
800         BT_DBG("le_status : %d", le_status);
801
802         adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
803         if (!adapter_agent) {
804                 BT_ERR("Fail to register agent");
805                 return;
806         }
807
808         if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
809                 BT_ERR("Fail to register media player");
810
811         if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
812                 BT_ERR("Fail to init obex server");
813
814 #ifdef TIZEN_BT_PAN_NAP_ENABLE
815         if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
816                 BT_ERR("Fail to activate network");
817 #endif
818
819         /* add the vconf noti handler */
820         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
821                                         __bt_phone_name_changed_cb, NULL);
822         if (ret < 0)
823                 BT_ERR("Unable to register key handler");
824
825         if (le_status == BT_LE_ACTIVATING) {
826                 __bt_set_le_enabled();
827                 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
828         }
829         if (status == BT_ACTIVATING) {
830                 __bt_set_enabled();
831                 _bt_adapter_set_status(BT_ACTIVATED);
832         }
833 #ifdef ENABLE_TIZEN_2_4
834         journal_bt_on();
835 #endif
836
837         _bt_service_register_vconf_handler();
838 #if 0
839         /* eventsystem */
840         if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
841                         (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
842                 BT_ERR("Fail to register system event");
843         }
844 #endif
845 }
846
847 void _bt_handle_adapter_removed(void)
848 {
849         int ret;
850
851         _bt_adapter_set_status(BT_DEACTIVATED);
852 #ifdef ENABLE_TIZEN_2_4
853         journal_bt_off();
854 #endif
855
856         __bt_visibility_alarm_remove();
857
858         if (visible_timer.alarm_init) {
859                 alarmmgr_fini();
860                 visible_timer.alarm_init = FALSE;
861         }
862
863         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
864                                 (vconf_callback_fn)__bt_phone_name_changed_cb);
865         if (0 != ret) {
866                 ERR("vconf_ignore_key_changed failed\n");
867         }
868
869         _bt_destroy_agent(adapter_agent);
870         adapter_agent = NULL;
871
872         _bt_reliable_terminate_service(NULL);
873 #if 0 
874         if (eventsystem_unregister_event(status_reg_id) != ES_R_OK) {
875                 BT_ERR("Fail to unregister system event");
876         }
877 #endif
878
879 }
880
881 static gboolean __bt_enable_timeout_cb(gpointer user_data)
882 {
883         GDBusProxy *proxy;
884         GVariant *result;
885         GError *error = NULL;
886
887         timer_id = 0;
888
889         retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
890
891         BT_ERR("EnableAdapter is failed");
892
893         proxy = __bt_get_core_proxy();
894         if (!proxy)
895                 return FALSE;
896
897         /* Clean up the process */
898         result = g_dbus_proxy_call_sync(proxy,
899                                 "DisableAdapter",
900                                 NULL,
901                                 G_DBUS_CALL_FLAGS_NONE,
902                                 -1,
903                                 NULL,
904                                 &error);
905
906         if (!result) {
907                 if (error != NULL) {
908                         BT_ERR("Bt core call failed(Error: %s)", error->message);
909                         g_clear_error(&error);
910                 } else {
911                         BT_ERR("Bt core call failed");
912                 }
913                 return FALSE;
914         }
915
916         g_variant_unref(result);
917
918         _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
919
920         _bt_terminate_service(NULL);
921
922         return FALSE;
923 }
924
925 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
926 {
927         GDBusProxy *proxy;
928         GVariant *result;
929         GError *error = NULL;
930
931         le_timer_id = 0;
932
933         retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
934
935         BT_ERR("EnableAdapterLE is failed");
936
937         proxy = __bt_get_core_proxy();
938         if (!proxy)
939                 return FALSE;
940
941         /* Clean up the process */
942         result = g_dbus_proxy_call_sync(proxy,
943                                 "DisableAdapterLe",
944                                 NULL,
945                                 G_DBUS_CALL_FLAGS_NONE,
946                                 -1,
947                                 NULL,
948                                 &error);
949
950         if (!result) {
951                 if (error != NULL) {
952                         BT_ERR("Bt core call failed(Error: %s)", error->message);
953                         g_clear_error(&error);
954                 } else
955                         BT_ERR("Bt core call failed");
956                 return FALSE;
957         }
958
959         g_variant_unref(result);
960
961         _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
962
963         _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
964
965         if (_bt_adapter_get_status() == BT_DEACTIVATED)
966                 _bt_terminate_service(NULL);
967
968         return FALSE;
969 }
970
971 void _bt_adapter_start_le_enable_timer(void)
972 {
973         if (le_timer_id > 0) {
974                 g_source_remove(le_timer_id);
975                 le_timer_id = 0;
976         }
977
978         le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
979                         __bt_enable_le_timeout_cb, NULL);
980
981         return;
982 }
983
984 void _bt_adapter_start_enable_timer(void)
985 {
986         if (timer_id > 0) {
987                 g_source_remove(timer_id);
988                 timer_id = 0;
989         }
990
991         timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
992                         __bt_enable_timeout_cb, NULL);
993
994         return;
995 }
996
997 int _bt_enable_adapter(void)
998 {
999         GDBusProxy *proxy;
1000         GError *error = NULL;
1001         int ret;
1002         GVariant *result = NULL;
1003         bt_status_t status = _bt_adapter_get_status();
1004         bt_le_status_t le_status = _bt_adapter_get_le_status();
1005
1006         BT_DBG("");
1007
1008         if (status == BT_ACTIVATING) {
1009                 BT_ERR("Enabling in progress");
1010                 return BLUETOOTH_ERROR_IN_PROGRESS;
1011         }
1012
1013         if (status == BT_ACTIVATED) {
1014                 BT_ERR("Already enabled");
1015                 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1016         }
1017
1018         if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1019                 BT_ERR("Disabling in progress");
1020                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1021         }
1022
1023         _bt_adapter_set_status(BT_ACTIVATING);
1024
1025         proxy = __bt_get_core_proxy();
1026         if (!proxy)
1027                 return BLUETOOTH_ERROR_INTERNAL;
1028
1029         if (le_status == BT_LE_ACTIVATED) {
1030                 BT_INFO("LE Already enabled. Just turn on PSCAN");
1031                 ret = _bt_set_connectable(TRUE);
1032                 if (ret == BLUETOOTH_ERROR_NONE) {
1033                         _bt_adapter_set_status(BT_ACTIVATED);
1034                 } else {
1035                         return BLUETOOTH_ERROR_INTERNAL;
1036                 }
1037         }
1038
1039         result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1040                                          NULL,
1041                                          G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1042                                          NULL, &error);
1043          if (error) {
1044                  BT_ERR("EnableAdapterLe failed: %s", error->message);
1045                 _bt_adapter_set_status(BT_DEACTIVATED);
1046                 g_clear_error(&error);
1047                 error = NULL;
1048                 result = g_dbus_proxy_call_sync(proxy,
1049                                 "DisableAdapter",
1050                                 NULL,
1051                                 G_DBUS_CALL_FLAGS_NONE,
1052                                 -1,
1053                                 NULL,
1054                                 &error);
1055
1056                 if (error != NULL) {
1057                                 BT_ERR("Bt core call failed(Error: %s)", error->message);
1058                                 g_clear_error(&error);
1059                 }
1060                 g_variant_unref(result);
1061                 /* Terminate myself */
1062                 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1063                 return BLUETOOTH_ERROR_INTERNAL;
1064         }
1065         g_variant_unref(result);
1066
1067         if (le_status == BT_LE_ACTIVATED) {
1068                 __bt_set_enabled();
1069         } else {
1070                 _bt_adapter_start_enable_timer();
1071         }
1072
1073         return BLUETOOTH_ERROR_NONE;
1074 }
1075
1076 static gboolean __bt_disconnect_all(void)
1077 {
1078         int i;
1079         GDBusConnection *conn;
1080         GDBusProxy *dev_proxy;
1081         gboolean ret = FALSE;
1082         GVariant *result;
1083         GError *error = NULL;
1084         GArray *device_list;
1085         bluetooth_device_info_t info;
1086         guint size;
1087         char *device_path = NULL;
1088         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1089
1090
1091         BT_DBG("");
1092
1093         conn = _bt_get_system_gconn();
1094
1095         device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1096
1097         if (_bt_get_bonded_devices(&device_list)
1098                                         != BLUETOOTH_ERROR_NONE) {
1099                 g_array_free(device_list, TRUE);
1100                 return FALSE;
1101         }
1102
1103         size = (device_list->len) / sizeof(bluetooth_device_info_t);
1104
1105         for (i = 0; i < size; i++) {
1106
1107                 info = g_array_index(device_list,
1108                                 bluetooth_device_info_t, i);
1109
1110                 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1111                         BT_DBG("Found Connected device");
1112                         _bt_convert_addr_type_to_string(address, info.device_address.addr);
1113                         device_path = _bt_get_device_object_path(address);
1114                         if (device_path == NULL)
1115                                 continue;
1116
1117                         BT_DBG("Disconnecting : %s", device_path);
1118
1119                         dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1120                                                         NULL,
1121                                                         BT_BLUEZ_NAME,
1122                                                         device_path,
1123                                                         BT_DEVICE_INTERFACE,
1124                                                         NULL, NULL);
1125
1126                         if (dev_proxy == NULL)
1127                                 continue;
1128
1129                         result = g_dbus_proxy_call_sync(dev_proxy,
1130                                                 "Disconnect",
1131                                                 NULL,
1132                                                 G_DBUS_CALL_FLAGS_NONE,
1133                                                 -1,
1134                                                 NULL,
1135                                                 &error);
1136
1137                         if (!result) {
1138                                 if (error != NULL) {
1139                                         BT_ERR("Disconnect call failed(Error: %s)", error->message);
1140                                         g_clear_error(&error);
1141                                 } else
1142                                         BT_ERR("Disconnect call failed");
1143                                 g_object_unref(dev_proxy);
1144                                 return FALSE;
1145                         }
1146
1147                         g_variant_unref(result);
1148                         g_object_unref(dev_proxy);
1149                 }
1150         }
1151         ret = TRUE;
1152         g_array_free(device_list, TRUE);
1153
1154         return ret;
1155 }
1156
1157 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1158 {
1159         BT_DBG("");
1160         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1161
1162         return FALSE;
1163 }
1164
1165 int __bt_disable_cb(void)
1166 {
1167         FN_START;
1168         GDBusProxy *proxy;
1169         bt_le_status_t le_status;
1170         int ret;
1171         GVariant *result;
1172         GError *error = NULL;
1173
1174         _bt_adapter_set_status(BT_DEACTIVATING);
1175         le_status = _bt_adapter_get_le_status();
1176         BT_DBG("le_status : %d", le_status);
1177         if (le_status == BT_LE_ACTIVATED) {
1178                 BT_INFO("LE is enabled. Just turn off PSCAN");
1179
1180                 if (_bt_is_discovering())
1181                         _bt_cancel_discovery();
1182
1183                 if (_bt_is_connectable() == FALSE) {
1184                         g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1185                 } else {
1186                         ret = _bt_set_connectable(FALSE);
1187                         if (ret != BLUETOOTH_ERROR_NONE) {
1188                                 BT_ERR("_bt_set_connectable fail!");
1189                                 _bt_adapter_set_status(BT_ACTIVATED);
1190                                 return BLUETOOTH_ERROR_INTERNAL;
1191                         }
1192                 }
1193         }
1194
1195         proxy = __bt_get_core_proxy();
1196         retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1197
1198         result = g_dbus_proxy_call_sync(proxy,
1199                                 "DisableAdapter",
1200                                 NULL,
1201                                 G_DBUS_CALL_FLAGS_NONE,
1202                                 -1,
1203                                 NULL,
1204                                 &error);
1205
1206         if (!result) {
1207                 if (error != NULL) {
1208                         BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1209                         g_clear_error(&error);
1210                 } else
1211                         BT_ERR("Failed to DisableAdapter");
1212                 _bt_adapter_set_status(BT_ACTIVATED);
1213                 return BLUETOOTH_ERROR_INTERNAL;
1214         }
1215
1216         g_variant_unref(result);
1217
1218         return BLUETOOTH_ERROR_NONE;
1219 }
1220
1221 int _bt_disable_adapter(void)
1222 {
1223         BT_DBG("+");
1224         int ret;
1225
1226         if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1227                 BT_DBG("Disabling in progress");
1228                 return BLUETOOTH_ERROR_IN_PROGRESS;
1229         }
1230
1231         if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1232                 BT_DBG("Already disabled");
1233                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1234         }
1235
1236         if (timer_id > 0) {
1237                 g_source_remove(timer_id);
1238                 timer_id = 0;
1239         }
1240
1241         __bt_disconnect_all();
1242         ret = __bt_disable_cb();
1243
1244         BT_DBG("-");
1245         return ret;
1246 }
1247
1248 int _bt_recover_adapter(void)
1249 {
1250         BT_DBG("+");
1251         GDBusProxy *proxy;
1252         GVariant *result;
1253         GError *error = NULL;
1254
1255         if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1256                 BT_DBG("Disabling in progress");
1257                 return BLUETOOTH_ERROR_IN_PROGRESS;
1258         }
1259
1260         if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1261                 BT_DBG("Already disabled");
1262                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1263         }
1264
1265         _bt_adapter_set_status(BT_DEACTIVATING);
1266
1267         proxy = __bt_get_core_proxy();
1268         retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1269
1270         result = g_dbus_proxy_call_sync(proxy,
1271                                 "RecoverAdapter",
1272                                 NULL,
1273                                 G_DBUS_CALL_FLAGS_NONE,
1274                                 -1,
1275                                 NULL,
1276                                 &error);
1277
1278         if (!result) {
1279                 if (error != NULL) {
1280                         BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1281                         g_clear_error(&error);
1282                 } else
1283                         BT_ERR("Failed to RecoverAdapter");
1284                 return BLUETOOTH_ERROR_INTERNAL;
1285         }
1286
1287         g_variant_unref(result);
1288         __bt_disconnect_all();
1289
1290         BT_DBG("-");
1291         return BLUETOOTH_ERROR_NONE;
1292 }
1293
1294 int _bt_reset_adapter(void)
1295 {
1296         GDBusProxy *proxy;
1297         GVariant *result;
1298         GError *error = NULL;
1299
1300         BT_DBG("");
1301
1302         proxy = __bt_get_core_proxy();
1303         if (!proxy)
1304                 return BLUETOOTH_ERROR_INTERNAL;
1305
1306         result = g_dbus_proxy_call_sync(proxy,
1307                                 "ResetAdapter",
1308                                 NULL,
1309                                 G_DBUS_CALL_FLAGS_NONE,
1310                                 -1,
1311                                 NULL,
1312                                 &error);
1313
1314         if (!result) {
1315                 if (error != NULL) {
1316                         BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1317                         g_clear_error(&error);
1318                 } else
1319                         BT_ERR("Failed to ResetAdapter");
1320                 return BLUETOOTH_ERROR_INTERNAL;
1321         }
1322
1323         g_variant_unref(result);
1324         /* Terminate myself */
1325         if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1326                 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1327         }
1328
1329         return BLUETOOTH_ERROR_NONE;
1330 }
1331
1332 int _bt_check_adapter(int *status)
1333 {
1334
1335         char *adapter_path = NULL;
1336
1337         BT_CHECK_PARAMETER(status, return);
1338
1339         *status = BT_ADAPTER_DISABLED;
1340
1341         adapter_path = _bt_get_adapter_path();
1342
1343
1344         if (adapter_path != NULL)
1345                 *status = BT_ADAPTER_ENABLED;
1346
1347         g_free(adapter_path);
1348         return BLUETOOTH_ERROR_NONE;
1349 }
1350
1351 int _bt_enable_adapter_le(void)
1352 {
1353         BT_DBG("+");
1354         GDBusProxy *proxy;
1355         GError *error = NULL;
1356         bt_status_t status = _bt_adapter_get_status();
1357         bt_le_status_t le_status = _bt_adapter_get_le_status();
1358         GVariant *result;
1359
1360         if (le_status == BT_LE_ACTIVATING) {
1361                 BT_ERR("Enabling in progress");
1362                 return BLUETOOTH_ERROR_IN_PROGRESS;
1363         }
1364
1365         if (le_status == BT_LE_ACTIVATED) {
1366                 BT_ERR("Already enabled");
1367                 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1368         }
1369
1370         if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1371                 BT_ERR("Disabling in progress");
1372                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1373         }
1374
1375         _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1376
1377         proxy = __bt_get_core_proxy();
1378         retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1379
1380         result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1381                                         NULL,
1382                                         G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1383                                         NULL, &error);
1384         if (error) {
1385                 BT_ERR("EnableAdapterLe failed: %s", error->message);
1386                 _bt_adapter_set_le_status(BT_DEACTIVATED);
1387                 g_clear_error(&error);
1388
1389                 /* Clean up the process */
1390                 result = g_dbus_proxy_call_sync(proxy,
1391                                         "DisableAdapterLe",
1392                                         NULL,
1393                                         G_DBUS_CALL_FLAGS_NONE,
1394                                         -1,
1395                                         NULL,
1396                                         &error);
1397
1398                 if (!result) {
1399                                 BT_ERR("Bt core call failed");
1400                                 if (error) {
1401                                         BT_ERR("EnableAdapterLE Failed %s", error->message);
1402                                         g_clear_error(&error);
1403                                 }
1404                 }
1405                 g_variant_unref(result);
1406                 /* Terminate myself */
1407                 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1408                         g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1409                 return BLUETOOTH_ERROR_INTERNAL;
1410         }
1411
1412         if (result)
1413                 g_variant_unref(result);
1414
1415         _bt_adapter_start_le_enable_timer();
1416
1417         if (status == BT_ACTIVATED) {
1418                 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1419                 __bt_set_le_enabled();
1420         }
1421         BT_DBG("le status : %d", _bt_adapter_get_le_status());
1422         BT_DBG("-");
1423         return BLUETOOTH_ERROR_NONE;
1424 }
1425
1426 int _bt_disable_adapter_le(void)
1427 {
1428         BT_DBG("+");
1429         GDBusProxy *proxy;
1430         bt_le_status_t bt_le_state;
1431         GVariant *result;
1432         GError *error = NULL;
1433
1434         bt_le_state = _bt_adapter_get_le_status();
1435         if (bt_le_state == BT_LE_DEACTIVATING) {
1436                 BT_DBG("Disabling in progress");
1437                 return BLUETOOTH_ERROR_IN_PROGRESS;
1438         }
1439
1440         if (bt_le_state == BT_LE_DEACTIVATED) {
1441                 BT_DBG("Already disabled");
1442                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1443         }
1444
1445         _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1446
1447         proxy = __bt_get_core_proxy();
1448         if (!proxy)
1449                 return BLUETOOTH_ERROR_INTERNAL;
1450
1451         result = g_dbus_proxy_call_sync(proxy,
1452                                 "DisableAdapterLe",
1453                                 NULL,
1454                                 G_DBUS_CALL_FLAGS_NONE,
1455                                 -1,
1456                                 NULL,
1457                                 &error);
1458
1459         if (!result) {
1460                 if (error != NULL) {
1461                         BT_ERR("Bt core call failed (Error: %s)", error->message);
1462                         g_clear_error(&error);
1463                 } else
1464                         BT_ERR("Bt core call failed");
1465                 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1466                 return BLUETOOTH_ERROR_INTERNAL;
1467         }
1468
1469         g_variant_unref(result);
1470
1471         _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1472         BT_DBG("le status : %d", _bt_adapter_get_le_status());
1473         BT_DBG("-");
1474         return BLUETOOTH_ERROR_NONE;
1475 }
1476
1477 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1478 {
1479
1480         GDBusProxy *proxy;
1481         GError *error = NULL;
1482         const char *address;
1483         GVariant *result;
1484         GVariant *temp;
1485
1486         BT_CHECK_PARAMETER(local_address, return);
1487
1488         proxy = _bt_get_adapter_properties_proxy();
1489         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1490
1491         result = g_dbus_proxy_call_sync(proxy,
1492                                 "Get",
1493                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1494                                         "Address"),
1495                                 G_DBUS_CALL_FLAGS_NONE,
1496                                 -1,
1497                                 NULL,
1498                                 &error);
1499
1500         if (!result) {
1501                 BT_ERR("Failed to get local address");
1502                 if (error != NULL) {
1503                         BT_ERR("Failed to get local address (Error: %s)", error->message);
1504                         g_clear_error(&error);
1505                 }
1506                 return BLUETOOTH_ERROR_INTERNAL;
1507         }
1508
1509         g_variant_get(result, "(v)", &temp);
1510         address = g_variant_get_string(temp, NULL);
1511         BT_DBG("Address:%s", address);
1512
1513         if (address) {
1514                 _bt_convert_addr_string_to_type(local_address->addr, address);
1515         } else {
1516                 return BLUETOOTH_ERROR_INTERNAL;
1517         }
1518
1519         g_variant_unref(result);
1520         g_variant_unref(temp);
1521         return BLUETOOTH_ERROR_NONE;
1522 }
1523
1524 int _bt_get_local_version(bluetooth_version_t *local_version)
1525 {
1526         GDBusProxy *proxy;
1527         const char *ver = NULL;
1528         char *ptr = NULL;
1529         int ret = BLUETOOTH_ERROR_NONE;
1530         GVariant *result;
1531         GVariant *temp;
1532
1533         BT_CHECK_PARAMETER(local_version, return);
1534
1535         GError *error = NULL;
1536
1537         proxy = _bt_get_adapter_properties_proxy();
1538         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1539
1540         result = g_dbus_proxy_call_sync(proxy,
1541                                 "Get",
1542                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1543                                         "Version"),
1544                                 G_DBUS_CALL_FLAGS_NONE,
1545                                 -1,
1546                                 NULL,
1547                                 &error);
1548
1549         if (!result) {
1550                 if (error != NULL) {
1551                         BT_ERR("Failed to get local version (Error: %s)", error->message);
1552                         g_clear_error(&error);
1553                 } else
1554                         BT_ERR("Failed to get local version");
1555                 return BLUETOOTH_ERROR_INTERNAL;
1556         }
1557
1558         g_variant_get(result, "(v)", &temp);
1559         ver = g_variant_get_string(temp, NULL);
1560         BT_DBG("VERSION: %s", ver);
1561
1562         if (ver && (strlen(ver) > 0)) {
1563                 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1564                 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1565                         *ptr = '\0';
1566
1567                 g_strlcpy(local_version->version, ver,
1568                                 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1569
1570         } else {
1571                 ret = BLUETOOTH_ERROR_INTERNAL;
1572         }
1573
1574         g_variant_unref(result);
1575         g_variant_unref(temp);
1576         return ret;
1577 }
1578
1579 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1580 {
1581         GDBusProxy *proxy;
1582         const char *name = NULL;
1583         char *ptr = NULL;
1584         int ret = BLUETOOTH_ERROR_NONE;
1585         GVariant *result;
1586         GVariant *temp;
1587         GError *error = NULL;
1588
1589         BT_CHECK_PARAMETER(local_name, return);
1590
1591         proxy = _bt_get_adapter_properties_proxy();
1592         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1593
1594         result = g_dbus_proxy_call_sync(proxy,
1595                                 "Get",
1596                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1597                                         "Alias"),
1598                                 G_DBUS_CALL_FLAGS_NONE,
1599                                 -1,
1600                                 NULL,
1601                                 &error);
1602
1603         if (!result) {
1604                 if (error != NULL) {
1605                         BT_ERR("Failed to get local name (Error: %s)", error->message);
1606                         g_clear_error(&error);
1607                 } else
1608                         BT_ERR("Failed to get local name");
1609                 return BLUETOOTH_ERROR_INTERNAL;
1610         }
1611
1612         g_variant_get(result, "(v)", &temp);
1613         name = g_variant_get_string(temp, NULL);
1614         BT_DBG("LOCAL NAME:%s", name);
1615
1616         if (name && (strlen(name) > 0)) {
1617                 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1618                 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1619                         *ptr = '\0';
1620
1621                 g_strlcpy(local_name->name, name,
1622                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1623         } else {
1624                 ret = BLUETOOTH_ERROR_INTERNAL;
1625         }
1626         g_variant_unref(result);
1627         g_variant_unref(temp);
1628         return ret;
1629 }
1630
1631 int _bt_set_local_name(char *local_name)
1632 {
1633         GDBusProxy *proxy;
1634         GError *error = NULL;
1635         char *ptr = NULL;
1636         GVariant *result;
1637
1638         BT_CHECK_PARAMETER(local_name, return);
1639
1640         proxy = _bt_get_adapter_properties_proxy();
1641
1642         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1643
1644         if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1645                 *ptr = '\0';
1646
1647         result = g_dbus_proxy_call_sync(proxy,
1648                                 "Set",
1649                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1650                                         "Alias", g_variant_new("s", local_name)),
1651                                 G_DBUS_CALL_FLAGS_NONE,
1652                                 -1,
1653                                 NULL,
1654                                 &error);
1655
1656         if (!result) {
1657                 if (error != NULL) {
1658                         BT_ERR("Failed to set Alias (Error: %s)", error->message);
1659                         g_clear_error(&error);
1660                 } else
1661                         BT_ERR("Failed to set Alias");
1662                 return BLUETOOTH_ERROR_INTERNAL;
1663         }
1664
1665         g_variant_unref(result);
1666         return BLUETOOTH_ERROR_NONE;
1667 }
1668
1669 int _bt_is_service_used(char *service_uuid, gboolean *used)
1670 {
1671         GDBusProxy *proxy;
1672         GError *error = NULL;
1673         int ret = BLUETOOTH_ERROR_NONE;
1674         GVariant *result;
1675         GVariantIter *iter;
1676         gchar *uuid;
1677
1678         BT_DBG("+");
1679         BT_CHECK_PARAMETER(service_uuid, return);
1680         BT_CHECK_PARAMETER(used, return);
1681
1682         proxy = _bt_get_adapter_properties_proxy();
1683         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1684
1685         result = g_dbus_proxy_call_sync(proxy,
1686                                 "Get",
1687                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1688                                         "UUIDs"),
1689                                 G_DBUS_CALL_FLAGS_NONE,
1690                                 -1,
1691                                 NULL,
1692                                 &error);
1693
1694         if (!result) {
1695                 if (error != NULL) {
1696                         BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1697                         g_clear_error(&error);
1698                 } else
1699                         BT_ERR("Failed to get UUIDs");
1700                 return BLUETOOTH_ERROR_INTERNAL;
1701         }
1702
1703         g_variant_get(result, "as", &iter);
1704         while (g_variant_iter_loop(iter, "s", &uuid)) {
1705                 if (strcasecmp(uuid, service_uuid) == 0) {
1706                         *used = TRUE;
1707                         g_free(uuid);
1708                         goto done;
1709                 }
1710         }
1711
1712         *used = FALSE;
1713
1714 done:
1715         g_variant_iter_free(iter);
1716         g_variant_unref(result);
1717         BT_DBG("Service Used? %d", *used);
1718         return ret;
1719 }
1720
1721 static gboolean __bt_get_discoverable_property(void)
1722 {
1723         GDBusProxy *proxy;
1724         gboolean discoverable_v;
1725         GError *error = NULL;
1726         GVariant *result;
1727         GVariant *temp;
1728
1729         proxy = _bt_get_adapter_properties_proxy();
1730         retv_if(proxy == NULL, FALSE);
1731
1732         result = g_dbus_proxy_call_sync(proxy,
1733                                 "Get",
1734                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1735                                         "Discoverable"),
1736                                 G_DBUS_CALL_FLAGS_NONE,
1737                                 -1,
1738                                 NULL,
1739                                 &error);
1740
1741         if (!result) {
1742                 if (error != NULL) {
1743                         BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1744                         g_clear_error(&error);
1745                 } else
1746                         BT_ERR("Failed to get Discoverable property");
1747                 return FALSE;
1748         }
1749
1750         g_variant_get(result, "(v)", &temp);
1751         discoverable_v = g_variant_get_boolean(temp);
1752         BT_DBG("discoverable_v:%d", discoverable_v);
1753
1754         g_variant_unref(result);
1755         g_variant_unref(temp);
1756
1757         return discoverable_v;
1758 }
1759
1760 int _bt_get_discoverable_mode(int *mode)
1761 {
1762         gboolean discoverable;
1763         unsigned int timeout;
1764
1765         BT_CHECK_PARAMETER(mode, return);
1766
1767         discoverable = __bt_get_discoverable_property();
1768         timeout = _bt_get_discoverable_timeout_property();
1769
1770         if (discoverable == TRUE) {
1771                 if (timeout == 0)
1772                         *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1773                 else
1774                         *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1775         } else {
1776                 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1777         }
1778         return BLUETOOTH_ERROR_NONE;
1779 }
1780
1781
1782 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1783 {
1784         int ret = BLUETOOTH_ERROR_NONE;
1785         gboolean inq_scan;
1786         gboolean pg_scan;
1787         GError *error = NULL;
1788         GDBusProxy *proxy;
1789         GVariant *result;
1790
1791         proxy = _bt_get_adapter_properties_proxy();
1792
1793         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1794
1795         switch (discoverable_mode) {
1796         case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1797                 pg_scan = TRUE;
1798                 inq_scan = FALSE;
1799                 timeout = 0;
1800                 break;
1801         case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1802                 pg_scan = TRUE;
1803                 inq_scan = TRUE;
1804                 timeout = 0;
1805                 break;
1806         case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1807                 inq_scan = TRUE;
1808                 pg_scan = TRUE;
1809                 break;
1810         default:
1811                 return BLUETOOTH_ERROR_INVALID_PARAM;
1812         }
1813
1814         BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1815                         discoverable_mode, timeout);
1816
1817         result = g_dbus_proxy_call_sync(proxy,
1818                                 "Set",
1819                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1820                                         "Connectable", g_variant_new("b", pg_scan)),
1821                                 G_DBUS_CALL_FLAGS_NONE,
1822                                 -1,
1823                                 NULL,
1824                                 &error);
1825
1826         if (!result) {
1827                 if (error != NULL) {
1828                         BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1829                         g_clear_error(&error);
1830                 } else
1831                         BT_ERR("Failed to set connectable property");
1832                 return BLUETOOTH_ERROR_INTERNAL;
1833         }
1834         g_variant_unref(result);
1835         result = g_dbus_proxy_call_sync(proxy,
1836                                 "Set",
1837                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1838                                                 g_variant_new("b", inq_scan)),
1839                                 G_DBUS_CALL_FLAGS_NONE,
1840                                 -1,
1841                                 NULL,
1842                                 &error);
1843
1844         if (!result) {
1845                 if (error != NULL) {
1846                         BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
1847                         g_clear_error(&error);
1848                 } else
1849                         BT_ERR("Failed to set Discoverable property");
1850                 return BLUETOOTH_ERROR_INTERNAL;
1851         }
1852         g_variant_unref(result);
1853         result = g_dbus_proxy_call_sync(proxy,
1854                                 "Set",
1855                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1856                                         "DiscoverableTimeout", g_variant_new("u", timeout)),
1857                                 G_DBUS_CALL_FLAGS_NONE,
1858                                 -1,
1859                                 NULL,
1860                                 &error);
1861
1862         if (!result) {
1863                 if (error != NULL) {
1864                         BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
1865                         g_clear_error(&error);
1866                 } else
1867                         BT_ERR("Failed to set DiscoverableTimeout property");
1868                 return BLUETOOTH_ERROR_INTERNAL;
1869         }
1870
1871         if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
1872                 timeout = -1;
1873
1874         ret = __bt_set_visible_time(timeout);
1875
1876         g_variant_unref(result);
1877
1878         return ret;
1879 }
1880
1881 int _bt_start_discovery(void)
1882 {
1883         GDBusProxy *proxy;
1884         GError *error = NULL;
1885         GVariant *result;
1886
1887         if (_bt_is_discovering() == TRUE) {
1888                 BT_ERR("BT is already in discovering");
1889                 return BLUETOOTH_ERROR_IN_PROGRESS;
1890         } else if (_bt_is_device_creating() == TRUE) {
1891                 BT_ERR("Bonding device is going on");
1892                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1893         }
1894
1895         proxy = _bt_get_adapter_proxy();
1896         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1897
1898         result = g_dbus_proxy_call_sync(proxy,
1899                                 "StartDiscovery",
1900                                 NULL,
1901                                 G_DBUS_CALL_FLAGS_NONE,
1902                                 -1,
1903                                 NULL,
1904                                 &error);
1905
1906         if (!result) {
1907                 if (error != NULL) {
1908                         BT_ERR("StartDiscovery failed (Error: %s)", error->message);
1909                         g_clear_error(&error);
1910                 } else
1911                         BT_ERR("StartDiscovery failed");
1912                 return BLUETOOTH_ERROR_INTERNAL;
1913         }
1914
1915         is_discovering = TRUE;
1916         cancel_by_user = FALSE;
1917         /* discovery status will be change in event */
1918         g_variant_unref(result);
1919         return BLUETOOTH_ERROR_NONE;
1920 }
1921
1922 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
1923 {
1924         GDBusProxy *proxy;
1925         GVariant *result;
1926         GError *error = NULL;
1927         const gchar *disc_type;
1928
1929         if (_bt_is_discovering() == TRUE) {
1930                 BT_ERR("BT is already in discovering");
1931                 return BLUETOOTH_ERROR_IN_PROGRESS;
1932         }
1933
1934         proxy = _bt_get_adapter_proxy();
1935         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1936
1937         if (role == DISCOVERY_ROLE_BREDR)
1938                 disc_type = "BREDR";
1939         else if (role == DISCOVERY_ROLE_LE)
1940                 disc_type = "LE";
1941         else if (role == DISCOVERY_ROLE_LE_BREDR)
1942                 disc_type = "LE_BREDR";
1943         else
1944                 return BLUETOOTH_ERROR_INVALID_PARAM;
1945
1946         result = g_dbus_proxy_call_sync(proxy,
1947                                 "StartCustomDiscovery",
1948                                 g_variant_new("s", disc_type),
1949                                 G_DBUS_CALL_FLAGS_NONE,
1950                                 -1,
1951                                 NULL,
1952                                 &error);
1953
1954         if (!result) {
1955                 if (error != NULL) {
1956                         BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
1957                         g_clear_error(&error);
1958                 } else
1959                         BT_ERR("StartCustomDiscovery failed");
1960                 return BLUETOOTH_ERROR_INTERNAL;
1961         }
1962
1963         is_discovering = TRUE;
1964         cancel_by_user = FALSE;
1965         /* discovery status will be change in event */
1966         g_variant_unref(result);
1967         return BLUETOOTH_ERROR_NONE;
1968 }
1969
1970 int _bt_cancel_discovery(void)
1971 {
1972         GDBusProxy *proxy;
1973         GError *error = NULL;
1974         GVariant *result;
1975
1976         if (_bt_is_discovering() == FALSE) {
1977                 BT_ERR("BT is not in discovering");
1978                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1979         }
1980
1981         proxy = _bt_get_adapter_proxy();
1982         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1983
1984         result = g_dbus_proxy_call_sync(proxy,
1985                                 "StopDiscovery",
1986                                 NULL,
1987                                 G_DBUS_CALL_FLAGS_NONE,
1988                                 -1,
1989                                 NULL,
1990                                 &error);
1991
1992         if (!result) {
1993                 if (error != NULL) {
1994                         BT_ERR("StopDiscovery failed (Error: %s)", error->message);
1995                         g_clear_error(&error);
1996                 } else
1997                         BT_ERR("StopDiscovery failed");
1998                 return BLUETOOTH_ERROR_INTERNAL;
1999         }
2000
2001         cancel_by_user = TRUE;
2002         /* discovery status will be change in event */
2003         g_variant_unref(result);
2004         return BLUETOOTH_ERROR_NONE;
2005 }
2006
2007 gboolean _bt_is_discovering(void)
2008 {
2009         return is_discovering;
2010 }
2011
2012 gboolean _bt_is_connectable(void)
2013 {
2014         GDBusProxy *proxy;
2015         GError *error = NULL;
2016         gboolean is_connectable = FALSE;
2017         GVariant *result;
2018         GVariant *temp;
2019
2020         proxy = _bt_get_adapter_properties_proxy();
2021         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2022
2023         result = g_dbus_proxy_call_sync(proxy,
2024                                 "Get",
2025                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2026                                         "Connectable"),
2027                                 G_DBUS_CALL_FLAGS_NONE,
2028                                 -1,
2029                                 NULL,
2030                                 &error);
2031
2032         if (!result) {
2033                 if (error != NULL) {
2034                         BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2035                         g_clear_error(&error);
2036                 } else
2037                         BT_ERR("Failed to get connectable property");
2038                 return BLUETOOTH_ERROR_INTERNAL;
2039         }
2040
2041         g_variant_get(result, "(v)", &temp);
2042         is_connectable = g_variant_get_boolean(temp);
2043         BT_DBG("discoverable_v:%d", is_connectable);
2044
2045         g_variant_unref(result);
2046         g_variant_unref(temp);
2047
2048         BT_INFO("Get connectable [%d]", is_connectable);
2049         return is_connectable;
2050 }
2051
2052 int _bt_set_connectable(gboolean is_connectable)
2053 {
2054         GDBusProxy *proxy;
2055         GError *error = NULL;
2056         GVariant *result;
2057
2058         if (__bt_is_factory_test_mode()) {
2059                 BT_ERR("Unable to set connectable in factory binary !!");
2060                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2061         }
2062
2063         proxy = _bt_get_adapter_properties_proxy();
2064
2065         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2066
2067         result = g_dbus_proxy_call_sync(proxy,
2068                                 "Set",
2069                                 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2070                                                 g_variant_new("b", is_connectable)),
2071                                 G_DBUS_CALL_FLAGS_NONE,
2072                                 -1,
2073                                 NULL,
2074                                 &error);
2075
2076         if (!result) {
2077                 if (error != NULL) {
2078                         BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2079                         g_clear_error(&error);
2080                 } else
2081                         BT_ERR("Failed to set connectable property");
2082                 return BLUETOOTH_ERROR_INTERNAL;
2083         }
2084
2085         BT_INFO("Set connectable [%d]", is_connectable);
2086         g_variant_unref(result);
2087         return BLUETOOTH_ERROR_NONE;
2088 }
2089
2090 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2091 {
2092         GDBusProxy *proxy;
2093         gboolean discovering_v;
2094         GError *error = NULL;
2095         char *discovering_type =  NULL;
2096         GVariant *result;
2097         GVariant *temp;
2098
2099         proxy = _bt_get_adapter_properties_proxy();
2100         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2101
2102         if (discovery_type == DISCOVERY_ROLE_BREDR)
2103                 discovering_type = "Discovering";
2104         else if (discovery_type == DISCOVERY_ROLE_LE)
2105                 discovering_type = "LEDiscovering";
2106
2107         result = g_dbus_proxy_call_sync(proxy,
2108                                 "Get",
2109                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2110                                         discovering_type),
2111                                 G_DBUS_CALL_FLAGS_NONE,
2112                                 -1,
2113                                 NULL,
2114                                 &error);
2115
2116         if (!result) {
2117                 if (error != NULL) {
2118                         BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2119                         g_clear_error(&error);
2120                 } else
2121                         BT_ERR("Failed to get discovering property");
2122                 return BLUETOOTH_ERROR_INTERNAL;
2123         }
2124
2125         g_variant_get(result, "(v)", &temp);
2126         discovering_v = g_variant_get_boolean(temp);
2127         BT_DBG("discoverable_v:%d", discovering_v);
2128
2129         g_variant_unref(result);
2130         g_variant_unref(temp);
2131
2132         return discovering_v;
2133 }
2134
2135 unsigned int _bt_get_discoverable_timeout_property(void)
2136 {
2137         GDBusProxy *proxy;
2138         unsigned int timeout_v;
2139         GError *error = NULL;
2140         GVariant *result;
2141         GVariant *temp;
2142
2143         proxy = _bt_get_adapter_properties_proxy();
2144         retv_if(proxy == NULL, 0);
2145
2146         result = g_dbus_proxy_call_sync(proxy,
2147                                 "Get",
2148                                 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2149                                         "DiscoverableTimeout"),
2150                                 G_DBUS_CALL_FLAGS_NONE,
2151                                 -1,
2152                                 NULL,
2153                                 &error);
2154
2155         if (!result) {
2156                 BT_ERR("Fail to get discoverable timeout");
2157                 if (error != NULL) {
2158                         BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2159                         g_clear_error(&error);
2160                 }
2161                 return 0;
2162         }
2163
2164         g_variant_get(result, "(v)", &temp);
2165         timeout_v = g_variant_get_uint32(temp);
2166         BT_DBG("discoverable_v:%d", timeout_v);
2167
2168         g_variant_unref(result);
2169         g_variant_unref(temp);
2170
2171         return timeout_v;
2172 }
2173
2174 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2175 {
2176         bluetooth_device_info_t *dev_info;
2177         GVariant *value;
2178         const gchar *key;
2179         GByteArray *manufacturer_data = NULL;
2180         guint8 char_value;
2181         GVariantIter *char_value_iter;
2182
2183         dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2184
2185         while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2186
2187                 if (key == NULL)
2188                         continue;
2189
2190                 if (!g_strcmp0(key, "Address")) {
2191                         const char *address = NULL;
2192                         address = g_variant_get_string(value, NULL);
2193                         _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2194                                                         address);
2195                 } else if(!g_strcmp0(key, "Class")) {
2196                         unsigned int cod;
2197                         cod = g_variant_get_uint32(value);
2198                         _bt_divide_device_class(&dev_info->device_class, cod);
2199                 } else if(!g_strcmp0(key, "Name")) {
2200                         const char *name = NULL;
2201                         name = g_variant_get_string(value, NULL);
2202                         /* If there is no Alias */
2203                         if (strlen(dev_info->device_name.name) == 0) {
2204                                 g_strlcpy(dev_info->device_name.name, name,
2205                                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2206                         }
2207                 } else if(!g_strcmp0(key, "Alias")) {
2208                         const char *alias = NULL;
2209                         alias = g_variant_get_string(value, NULL);
2210                         /* Overwrite the name */
2211                         if (alias) {
2212                                 memset(dev_info->device_name.name, 0x00,
2213                                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2214                                 g_strlcpy(dev_info->device_name.name, alias,
2215                                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2216                         }
2217                 } else if (!g_strcmp0(key, "Connected")) {
2218                         dev_info->connected = g_variant_get_byte(value);
2219                 } else if (!g_strcmp0(key, "Paired")) {
2220                         dev_info->paired = g_variant_get_boolean(value);
2221                 } else if (!g_strcmp0(key, "Trusted")) {
2222                         dev_info->trust = g_variant_get_boolean(value);
2223                 } else if (!g_strcmp0(key, "RSSI")) {
2224                         dev_info->rssi = g_variant_get_int16(value);
2225                 } else if (!g_strcmp0(key, "UUIDs")) {
2226                         GVariantIter *iter;
2227                         gchar *uuid = NULL;
2228                         char **parts;
2229                         int i = 0;
2230
2231                         dev_info->service_index = 0;
2232                         g_variant_get(value, "as", &iter);
2233                         while (g_variant_iter_loop(iter, "s", &uuid)) {
2234                                 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2235                                 parts = g_strsplit(uuid, "-", -1);
2236
2237                                 if (parts == NULL || parts[0] == NULL) {
2238                                         g_free(uuid);
2239                                         break;
2240                                 }
2241
2242                                 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2243                                 g_strfreev(parts);
2244
2245                                 i++;
2246                         }
2247                         dev_info->service_index = i;
2248                         g_variant_iter_free(iter);
2249                 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2250                         dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2251                 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2252                         manufacturer_data = g_byte_array_new();
2253                         g_variant_get(value, "ay", &char_value_iter);
2254                         while(g_variant_iter_loop(char_value_iter, "y",  &char_value)) {
2255                                 g_byte_array_append(manufacturer_data, &char_value, 1);
2256                         }
2257                         if (manufacturer_data) {
2258                                 if (manufacturer_data->len > 0) {
2259                                         memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2260                                 }
2261                         }
2262                         g_variant_iter_free(char_value_iter);
2263                         g_byte_array_free(manufacturer_data, TRUE);
2264                 }
2265         }
2266
2267         return dev_info;
2268 }
2269
2270 static void __bt_extract_device_info(GVariantIter *iter,
2271                                                         GArray **dev_list)
2272 {
2273         bluetooth_device_info_t *dev_info = NULL;
2274         char *object_path = NULL;
2275         GVariantIter *interface_iter;
2276         GVariantIter *svc_iter;
2277         char *interface_str = NULL;
2278
2279         /* Parse the signature:  oa{sa{sv}}} */
2280         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2281                 &interface_iter)) {
2282
2283                 if (object_path == NULL)
2284                         continue;
2285
2286                 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2287                                 &interface_str, &svc_iter)) {
2288                         if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2289                                 BT_DBG("Found a device: %s", object_path);
2290                                 dev_info = __bt_parse_device_info(svc_iter);
2291                                 if (dev_info) {
2292                                         if (dev_info->paired == TRUE) {
2293                                                 g_array_append_vals(*dev_list, dev_info,
2294                                                                 sizeof(bluetooth_device_info_t));
2295                                         }
2296                                         g_free(dev_info);
2297                                 }
2298                                 g_free(interface_str);
2299                                 g_variant_iter_free(svc_iter);
2300                                 break;
2301                         }
2302                 }
2303         }
2304         BT_DBG("-");
2305 }
2306
2307 int _bt_get_bonded_devices(GArray **dev_list)
2308 {
2309         BT_DBG("+");
2310         GDBusConnection *conn;
2311         GDBusProxy *manager_proxy;
2312         GVariant *result = NULL;
2313         GVariantIter *iter = NULL;
2314         GError *error = NULL;
2315
2316         conn = _bt_get_system_conn();
2317         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2318
2319         manager_proxy = _bt_get_manager_proxy();
2320         retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2321
2322         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2323                                 NULL,
2324                                 G_DBUS_CALL_FLAGS_NONE,
2325                                 -1,
2326                                 NULL,
2327                                 NULL);
2328
2329         if (!result) {
2330                 if (error != NULL) {
2331                         BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2332                         g_clear_error(&error);
2333                 } else
2334                         BT_ERR("Failed to Failed to GetManagedObjects");
2335                 return BLUETOOTH_ERROR_INTERNAL;
2336         }
2337
2338         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
2339         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2340
2341         __bt_extract_device_info(iter, dev_list);
2342         g_variant_iter_free(iter);
2343         g_variant_unref(result);
2344
2345         BT_DBG("-");
2346         return BLUETOOTH_ERROR_NONE;
2347 }
2348
2349 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2350                                 bluetooth_device_info_t *dev_info)
2351 {
2352         char *object_path = NULL;
2353         GDBusProxy *adapter_proxy;
2354         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2355         int ret = BLUETOOTH_ERROR_NONE;
2356
2357         BT_CHECK_PARAMETER(device_address, return);
2358         BT_CHECK_PARAMETER(dev_info, return);
2359
2360         adapter_proxy = _bt_get_adapter_proxy();
2361         retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2362
2363         _bt_convert_addr_type_to_string(address, device_address->addr);
2364
2365         object_path = _bt_get_device_object_path(address);
2366
2367         retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2368
2369         ret = __bt_get_bonded_device_info(object_path, dev_info);
2370         g_free(object_path);
2371
2372         return ret;
2373 }
2374
2375 int _bt_get_timeout_value(int *timeout)
2376 {
2377         time_t current_time;
2378         int time_diff;
2379
2380         /* Take current time */
2381         time(&current_time);
2382         time_diff = difftime(current_time, visible_timer.start_time);
2383
2384         BT_DBG("Time diff = %d\n", time_diff);
2385
2386         *timeout = visible_timer.timeout - time_diff;
2387
2388         return BLUETOOTH_ERROR_NONE;
2389 }
2390
2391 int _bt_set_le_privacy(gboolean set_privacy)
2392 {
2393         GDBusProxy *proxy;
2394         GError *error = NULL;
2395         GVariant *result = NULL;
2396
2397         if (__bt_is_factory_test_mode()) {
2398                 BT_ERR("Unable to set le privacy in factory binary !!");
2399                 return BLUETOOTH_ERROR_NOT_SUPPORT;
2400         }
2401
2402         if (_bt_adapter_get_status() != BT_ACTIVATED &&
2403                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2404                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2405         }
2406
2407         proxy = _bt_get_adapter_proxy();
2408         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2409
2410         result = g_dbus_proxy_call_sync(proxy,
2411                                 "SetLePrivacy",
2412                                 g_variant_new("(b)", set_privacy),
2413                                 G_DBUS_CALL_FLAGS_NONE,
2414                                 -1,
2415                                 NULL,
2416                                 &error);
2417
2418         if (!result) {
2419                 if (error != NULL) {
2420                         BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2421                         g_clear_error(&error);
2422                 } else
2423                         BT_ERR("Failed to SetLePrivacy");
2424                 return BLUETOOTH_ERROR_INTERNAL;
2425         }
2426
2427         g_variant_unref(result);
2428         BT_INFO("SetLePrivacy as %d", set_privacy);
2429         return BLUETOOTH_ERROR_NONE;
2430 }
2431
2432 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2433 {
2434         GDBusProxy *proxy;
2435         GError *error = NULL;
2436         int i;
2437         GVariant *val;
2438         GVariant *result;
2439         GVariantBuilder *builder;
2440
2441         BT_CHECK_PARAMETER(m_data, return);
2442
2443         if (_bt_adapter_get_status() != BT_ACTIVATED &&
2444                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2445                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2446         }
2447
2448         proxy = _bt_get_adapter_proxy();
2449         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2450
2451         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2452
2453         for (i = 0; i < (m_data->data_len) + 2; i++) {
2454                 g_variant_builder_add(builder, "y", m_data->data[i]);
2455         }
2456
2457         val = g_variant_new("(ay)", builder);
2458
2459         result = g_dbus_proxy_call_sync(proxy,
2460                                 "SetManufacturerData",
2461                                 val,
2462                                 G_DBUS_CALL_FLAGS_NONE,
2463                                 -1,
2464                                 NULL,
2465                                 &error);
2466         g_variant_builder_unref(builder);
2467         if (!result) {
2468                 if (error != NULL) {
2469                         BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2470                         g_clear_error(&error);
2471                 } else {
2472                         BT_ERR("Failed to SetManufacturerData");
2473                 }
2474                 return BLUETOOTH_ERROR_INTERNAL;
2475         }
2476         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2477
2478         for (i = 0; i < (m_data->data_len) + 2; i++) {
2479                 g_variant_builder_add(builder, "y", m_data->data[i]);
2480         }
2481
2482         val = g_variant_new("(ay)", builder);
2483
2484         _bt_send_event(BT_ADAPTER_EVENT,
2485                         BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2486                         val);
2487
2488         BT_INFO("Set manufacturer data");
2489
2490         g_variant_builder_unref(builder);
2491         g_variant_unref(result);
2492
2493         return BLUETOOTH_ERROR_NONE;
2494 }
2495