[Bluetooth][OTP] Add base code for OTP server role
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-adapter.c
old mode 100755 (executable)
new mode 100644 (file)
index 490175c..f42caa9
@@ -17,6 +17,7 @@
 
 #include <vconf.h>
 #include <vconf-keys.h>
+#include <vconf-internal-radio-keys.h>
 #include <bundle.h>
 #include <eventsystem.h>
 
 static bt_status_t adapter_status = BT_DEACTIVATED;
 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
 static gboolean is_recovery_mode = FALSE;
+static guint core_enable_timer_id;
+
 
 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
 
+static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
+
 static void __bt_core_set_status(bt_status_t status)
 {
        adapter_status = status;
@@ -91,18 +96,18 @@ static gboolean __bt_core_idle_terminate(gpointer data)
 
 gboolean _bt_core_is_flight_mode_enabled(void)
 {
-#ifdef TIZEN_BT_FLIGHTMODE_ENABLED
        int isFlightMode = 0;
        int ret = -1;
 
-       ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
-       if (ret != 0) {
-               BT_ERR("vconf_get_bool failed");
+       if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
+               ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
+               if (ret != 0)
+                       BT_ERR("vconf_get_bool failed");
+
+               return isFlightMode;
+       } else {
+               return FALSE;
        }
-       return isFlightMode;
-#else
-       return FALSE;
-#endif
 }
 
 static int __execute_command(const char *cmd, char *const arg_list[])
@@ -143,16 +148,19 @@ int _bt_enable_adapter(void)
 {
        int ret;
        bt_status_t status;
+#if 0
        bt_le_status_t le_status;
-
+#endif
        BT_INFO("");
 
        status = _bt_core_get_status();
        if (status != BT_DEACTIVATED) {
                BT_ERR("Invalid state %d", status);
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
                return -1;
        }
 
+#if 0 /* only the concept of private */
        le_status = _bt_core_get_le_status();
        if (le_status == BT_LE_ACTIVATED) {
                /* Turn on PSCAN, (ISCAN if needed) */
@@ -162,22 +170,23 @@ int _bt_enable_adapter(void)
                g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
                return 0;
        }
+#endif
 
        __bt_core_set_status(BT_ACTIVATING);
-#ifdef USB_BLUETOOTH
-       char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
-       ret = __execute_command("/usr/bin/hciconfig", argv_up);
-#else
-       ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
-#endif
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
+               ret = __execute_command("/usr/bin/hciconfig", argv_up);
+       } else {
+               ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
+       }
        if (ret < 0) {
                BT_ERR("running script failed");
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               ret = __execute_command("/usr/bin/hciconfig", argv_down);
-#else
-               ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
+                       ret = __execute_command("/usr/bin/hciconfig", argv_down);
+               } else {
+                       ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
+               }
                __bt_core_set_status(BT_DEACTIVATED);
                return -1;
        }
@@ -188,13 +197,15 @@ int _bt_enable_adapter(void)
 int _bt_disable_adapter(void)
 {
        bt_status_t status;
+#if 0
        bt_le_status_t le_status;
+#endif
 
        BT_INFO_C("Disable adapter");
 
+#if 0 /* only the concept of private */
        le_status = _bt_core_get_le_status();
        BT_DBG("le_status : %d", le_status);
-#if 0 /* only the concept of private */
        if (le_status == BT_LE_ACTIVATED) {
                /* Turn off PSCAN, (ISCAN if needed) */
                /* Return with 0 for the Disabled response. */
@@ -208,30 +219,78 @@ int _bt_disable_adapter(void)
        status = _bt_core_get_status();
        if (status == BT_ACTIVATING) {
                /* Forcely terminate */
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
+                       if (__execute_command("/usr/bin/hciconfig", argv_down) < 0)
+                               BT_ERR("running script failed");
+               } else {
+
+#ifdef TIZEN_FEATURE_RADIO
+                       int radio_status = VCONFKEY_RADIO_STATUS_OFF;
+
+                       /* Check if radio status on or off */
+                       if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
+                               BT_ERR("Fail to get radio status");
+
+                       BT_DBG("Radio status: %d", radio_status);
+
+                       if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
+                               if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0)
+                                       BT_ERR("running script failed");
+                       } else {
+                               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
+                                       BT_ERR("running script failed");
+                       }
 #else
-               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+                       if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
+                               BT_ERR("running script failed");
 #endif
-                       BT_ERR("running script failed");
                }
                _bt_core_terminate();
                return 0;
        } else if (status != BT_ACTIVATED) {
                BT_ERR("Invalid state %d", status);
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
        }
 
        __bt_core_set_status(BT_DEACTIVATING);
-#ifdef USB_BLUETOOTH
-       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-       if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
+               if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
+                       BT_ERR("running script failed");
+                       __bt_core_set_status(BT_ACTIVATED);
+                       return -1;
+               }
+       } else {
+#ifdef TIZEN_FEATURE_RADIO
+               int radio_status = VCONFKEY_RADIO_STATUS_OFF;
+
+               /* Check if radio status on or off */
+               if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
+                       BT_ERR("Fail to get radio status");
+
+               BT_DBG("Radio status: %d", radio_status);
+
+               if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
+                       if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+                       }
+               } else {
+                       if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+                       }
+               }
 #else
-       if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+               }
 #endif
-               BT_ERR("running script failed");
-               __bt_core_set_status(BT_ACTIVATED);
-               return -1;
        }
 
        return 0;
@@ -251,20 +310,20 @@ int _bt_enable_adapter_le(void)
        if (status == BT_DEACTIVATED) {
                __bt_core_set_le_status(BT_LE_ACTIVATING);
                BT_DBG("Activate BT");
-#ifdef USB_BLUETOOTH
-               char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
-               ret = __execute_command("/usr/bin/hciconfig", argv_up);
-#else
-               ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
+                       ret = __execute_command("/usr/bin/hciconfig", argv_up);
+               } else {
+                       ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
+               }
                if (ret < 0) {
                        BT_ERR("running script failed");
-#ifdef USB_BLUETOOTH
-                       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-                       ret = __execute_command("/usr/bin/hciconfig", argv_down);
-#else
-                       ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
-#endif
+                       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
+                               ret = __execute_command("/usr/bin/hciconfig", argv_down);
+                       } else {
+                               ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
+                       }
                        __bt_core_set_status(BT_DEACTIVATED);
                        __bt_core_set_le_status(BT_LE_DEACTIVATED);
                        return -1;
@@ -273,11 +332,10 @@ int _bt_enable_adapter_le(void)
                __bt_core_set_le_status(BT_LE_ACTIVATED);
                g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
        }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
        ret = _bt_core_start_httpproxy();
-       if (ret < 0) {
+       if (ret < 0)
                BT_ERR("_bt_core_start_httpproxy() failed");
-       }
 #endif
 
        return 0;
@@ -294,21 +352,27 @@ int _bt_disable_adapter_le(void)
        retv_if(le_status == BT_LE_DEACTIVATED, 0);
        retv_if(le_status == BT_LE_DEACTIVATING, -1);
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
        _bt_core_stop_httpproxy();
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+       _bt_core_stop_otp();
+#endif
+
        status = _bt_core_get_status();
        BT_DBG("status : %d", status);
 
        if (status == BT_DEACTIVATED) {
                __bt_core_set_le_status(BT_LE_DEACTIVATING);
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
-#else
-               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
-#endif
+               int ret;
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
+                       ret = __execute_command("/usr/bin/hciconfig", argv_down);
+               } else {
+                       ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
+               }
+               if (ret < 0) {
                        BT_ERR("running script failed");
                        __bt_core_set_le_status(BT_LE_ACTIVATED);
                        return -1;
@@ -472,8 +536,9 @@ gboolean _bt_core_disable_adapter(void)
 gboolean _bt_core_recover_adapter(void)
 {
        int ret;
+#if 0
        int ret_le;
-
+#endif
        BT_INFO_C("Recover bt adapter");
 
        _bt_set_flightmode_request(FALSE);
@@ -486,20 +551,27 @@ gboolean _bt_core_recover_adapter(void)
 
        if (_bt_core_get_status() == BT_ACTIVATED) {
                _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
+#endif
        }
        if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
                _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
+#endif
        }
 
        ret = _bt_disable_adapter();
        if (ret < 0)
                BT_ERR("_bt_disable_adapter() failed");
+
+/* In platform, don't seperate BR/EDR and LE status */
+#if 0
        ret_le = _bt_disable_adapter_le();
        if (ret_le < 0)
                BT_ERR("_bt_disable_adapter_le() failed");
-
+#endif
        return TRUE;
 }
 
@@ -530,9 +602,9 @@ gboolean _bt_core_disable_adapter_le(void)
 gboolean __bt_core_reset_adapter(void)
 {
        /* Forcely terminate */
-       if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
+       if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
                BT_ERR("running script failed");
-       }
+
        _bt_core_terminate();
 
        return TRUE;
@@ -542,6 +614,8 @@ static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
 {
        BT_DBG("+");
 
+       core_enable_timer_id = 0;
+
        _bt_core_init_vconf_value();
 
        return FALSE;
@@ -553,12 +627,39 @@ gboolean _bt_core_enable_core(void)
 
        _bt_core_update_status();
 
-       g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
+       if (core_enable_timer_id > 0)
+               g_source_remove(core_enable_timer_id);
+
+       core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
 
        BT_DBG("-");
        return TRUE;
 }
 
+gboolean _bt_core_set_transfer_value(gboolean value)
+{
+       const char *event_val = NULL;
+       gboolean ret = TRUE;
+
+       BT_DBG("value: %d", value);
+
+       event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
+                                               event_val) != ES_R_OK) {
+               BT_ERR("Fail to set BT state value");
+               ret = FALSE;
+       }
+
+       if (ret == FALSE || value == FALSE) {
+               BT_DBG("Terminate bt-core process");
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
+       }
+
+       BT_DBG("-");
+       return ret;
+}
+
 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
 {
 
@@ -593,9 +694,8 @@ gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
        }
 
        BT_DBG("Run %s", cmd);
-       if (__execute_command(cmd, arg_list) < 0) {
+       if (__execute_command(cmd, arg_list) < 0)
                BT_ERR("running script failed");
-       }
 
        _bt_core_terminate();
        return TRUE;
@@ -604,11 +704,14 @@ gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
 static gboolean __bt_core_recovery_cb(gpointer data)
 {
        int ret = 0;
+#ifdef TIZEN_FEATURE_BUSACT
        gboolean is_request_failed = FALSE;
        static gboolean is_first_failure = TRUE;
+#endif
 
        BT_DBG("+");
 
+#ifdef TIZEN_FEATURE_BUSACT
        if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
                ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
                if (ret < 0)
@@ -633,6 +736,7 @@ static gboolean __bt_core_recovery_cb(gpointer data)
                }
        } else
                is_first_failure = TRUE;
+#endif
 
        if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
                _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
@@ -640,13 +744,15 @@ static gboolean __bt_core_recovery_cb(gpointer data)
                if (ret < 0)
                        BT_ERR("_bt_enable_adapter() failed");
        }
+
+#if 0
        if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
                _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
                ret = _bt_enable_adapter_le();
                if (ret < 0)
                        BT_ERR("_bt_enable_adapter_le() failed");
        }
-
+#endif
        is_recovery_mode = FALSE;
 
        BT_DBG("-");
@@ -703,7 +809,9 @@ static gboolean __bt_core_disable_timeout_cb(gpointer data)
                if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
                        _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
 
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
+#endif
                _bt_disable_adapter();
        }
 
@@ -713,7 +821,9 @@ static gboolean __bt_core_disable_timeout_cb(gpointer data)
                if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
                        _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
 
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
+#endif
                _bt_disable_adapter_le();
        }
 
@@ -764,6 +874,15 @@ void _bt_core_adapter_added_cb(void)
                return;
        }
        _bt_set_flightmode_request(FALSE);
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
+                                               EVT_VAL_BT_ON) != ES_R_OK)
+               BT_ERR("Fail to set BT state value");
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
+                                               EVT_VAL_BT_LE_ON) != ES_R_OK)
+               BT_ERR("Fail to set BT LE state value");
+
        _bt_core_terminate();
 }
 
@@ -774,7 +893,7 @@ void _bt_core_adapter_removed_cb(void)
        gboolean flight_mode_status;
        static int timer_id = -1;
 
-       BT_DBG("");
+       BT_DBG("is_recovery_mode: %d", is_recovery_mode);
 
        __bt_core_set_status(BT_DEACTIVATED);
        __bt_core_set_le_status(BT_LE_DEACTIVATED);