Modify the privilege for battry RX / TX function
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-adapter.c
index 1ea2b8c..10ea862 100644 (file)
 #include <vconf-internal-radio-keys.h>
 #include <bundle.h>
 #include <eventsystem.h>
+#include <stdio.h>
+#ifdef TIZEN_FEATURE_ACTD
+#include <actd/unit_control.h>
+#endif
 
 #include "bt-core-main.h"
 #include "bt-core-adapter.h"
@@ -27,6 +31,8 @@
 #include "bt-core-dbus-handler.h"
 #include "bt-core-noti-handler.h"
 
+#include "bt-internal-types.h"
+
 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
 
@@ -115,6 +121,53 @@ gboolean _bt_core_is_flight_mode_enabled(void)
        }
 }
 
+#ifdef TIZEN_FEATURE_ACTD
+static int __bt_call_systemact_service(const char *service_name)
+{
+       int ret;
+
+       BT_DBG("Use System Activated : %s", service_name);
+
+       ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
+
+       if (ret != UNIT_CONTROL_OK) {
+               BT_ERR("Failed to activate the %s: %d", service_name, ret);
+               /* Return Success even if the activating result is fail */
+               return 0;
+       }
+
+       return 0;
+}
+#else
+static int __bt_call_systemact_service(const char *file_path)
+{
+       BT_DBG("+");
+       FILE *fp;
+
+       if (!file_path) {
+               BT_ERR("service file path is NULL");
+               return -1;
+       }
+
+       if (!access(file_path, F_OK)) {
+               remove(file_path);
+               usleep(100);
+       }
+
+       fp = fopen(file_path, "w");
+       if (!fp) {
+               BT_ERR("Failed to fopen service file");
+               return -1;
+       }
+
+
+       fclose(fp);
+       BT_DBG("-");
+       return 0;
+
+}
+#endif
+
 static int __execute_command(const char *cmd, char *const arg_list[])
 {
        int pid;
@@ -133,7 +186,12 @@ static int __execute_command(const char *cmd, char *const arg_list[])
                if (pid2 == -1) {
                        BT_ERR("fork failed");
                } else if (pid2 == 0) {
-                       execv(cmd, arg_list);
+                       if (arg_list != NULL) {
+                               execv(cmd, arg_list);
+                       } else {
+                               char *argv[] = { NULL };
+                               execv(cmd, argv);
+                       }
                        exit(256);
                }
                exit(0);
@@ -149,6 +207,36 @@ static int __execute_command(const char *cmd, char *const arg_list[])
        return 0;
 }
 
+static int __bt_stack_up(void)
+{
+       int ret;
+
+       /* activate HCI logger */
+       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
+       if (ret < 0) {
+               BT_ERR("Failed to call systemact service");
+               return -1;
+       }
+
+       /* activate Bluez */
+       ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
+       if (ret < 0) {
+               BT_ERR("Failed to call systemact service");
+               return -1;
+       }
+
+       /* activate bluetooth-share */
+       if (!TIZEN_PROFILE_WEARABLE) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
+               if (ret < 0) {
+                       BT_ERR("Failed to call systemact service");
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
 int _bt_enable_adapter(void)
 {
        int ret;
@@ -179,16 +267,23 @@ int _bt_enable_adapter(void)
 
        __bt_core_set_status(BT_ACTIVATING);
        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);
+               /* activate Bluez */
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
+               if (ret < 0)
+                       BT_ERR("If bluez already exist, skip this error");
+
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
        } else {
-               ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
+               ret = __bt_stack_up();
        }
        if (ret < 0) {
                BT_ERR("running script failed");
                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);
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
                } else {
                        ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
                }
@@ -225,11 +320,9 @@ int _bt_disable_adapter(void)
        if (status == BT_ACTIVATING) {
                /* Forcely terminate */
                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");
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
+                               BT_ERR("Failed to call systemact service");
                } else {
-
 #ifdef TIZEN_FEATURE_RADIO
                        int radio_status = VCONFKEY_RADIO_STATUS_OFF;
 
@@ -240,14 +333,14 @@ int _bt_disable_adapter(void)
                        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)
+                               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
                                        BT_ERR("running script failed");
                        } else {
-                               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
+                               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
                                        BT_ERR("running script failed");
                        }
 #else
-                       if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
                                BT_ERR("running script failed");
 #endif
                }
@@ -260,12 +353,12 @@ int _bt_disable_adapter(void)
 
        __bt_core_set_status(BT_DEACTIVATING);
        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");
+               if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
+                       BT_ERR("Failed to call systemact service");
                        __bt_core_set_status(BT_ACTIVATED);
                        return -1;
                }
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
        } else {
 #ifdef TIZEN_FEATURE_RADIO
                int radio_status = VCONFKEY_RADIO_STATUS_OFF;
@@ -277,20 +370,20 @@ int _bt_disable_adapter(void)
                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) {
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 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 (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 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 (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
                                BT_ERR("running script failed");
                                __bt_core_set_status(BT_ACTIVATED);
                                return -1;
@@ -309,6 +402,7 @@ int _bt_enable_adapter_le(void)
        bt_status_t status;
        bt_le_status_t le_status;
        le_status = _bt_core_get_le_status();
+       retv_if(le_status == BT_LE_ACTIVATED, 0);
        retv_if(le_status != BT_LE_DEACTIVATED, -1);
 
        status = _bt_core_get_status();
@@ -316,16 +410,18 @@ int _bt_enable_adapter_le(void)
                __bt_core_set_le_status(BT_LE_ACTIVATING);
                BT_DBG("Activate BT");
                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);
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
                } else {
-                       ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
+                       ret = __bt_stack_up();
                }
                if (ret < 0) {
                        BT_ERR("running script failed");
                        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);
+                               ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                               if (ret < 0)
+                                       BT_ERR("Failed to call systemact service");
                        } else {
                                ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
                        }
@@ -390,8 +486,9 @@ int _bt_disable_adapter_le(void)
                __bt_core_set_le_status(BT_LE_DEACTIVATING);
                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);
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
                } else {
                        ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
                }
@@ -432,13 +529,15 @@ int _bt_core_service_request_adapter(int service_function)
        return ret;
 }
 
-static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
+static gboolean __bt_core_check_the_adapter_path(void)
 {
        GError *err = NULL;
        GDBusProxy *manager_proxy = NULL;
        GVariant *result = NULL;
        char *adapter_path = NULL;
+       GDBusConnection *conn = NULL;
 
+       conn = _bt_core_get_gdbus_connection();
        if (conn == NULL)
                return FALSE;
 
@@ -460,7 +559,7 @@ static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
        }
 
        result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
-                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+                       G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
        if (!result) {
                if (err != NULL) {
                        BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
@@ -505,7 +604,7 @@ void _bt_core_update_status(void)
        int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
        gboolean ret = FALSE;
 
-       ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
+       ret = __bt_core_check_the_adapter_path();
        BT_INFO("check the real status of bt_adapter");
 
        if (ret != TRUE) {
@@ -541,19 +640,35 @@ gboolean _bt_core_enable_adapter(void)
                return TRUE;
 }
 
+static gboolean __bt_core_terminate_cb(gpointer data)
+{
+       _bt_core_terminate();
+
+       return FALSE;
+}
+
 gboolean _bt_core_disable_adapter(void)
 {
        int ret;
+       gboolean adapter_state;
 
        _bt_set_flightmode_request(FALSE);
-       if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
-               BT_ERR("Set vconf failed");
+
+       adapter_state = __bt_core_check_the_adapter_path();
+       if (adapter_state == FALSE)
+               BT_INFO("Default adapter is NOT normal.");
 
        ret = _bt_disable_adapter();
-       if (ret < 0)
-               return FALSE;
-       else
+       if (adapter_state == FALSE) {
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME,
+                             __bt_core_terminate_cb, NULL);
                return TRUE;
+       } else {
+               if (ret < 0)
+                       return FALSE;
+               else
+                       return TRUE;
+       }
 }
 
 gboolean _bt_core_recover_adapter(void)
@@ -628,7 +743,7 @@ gboolean __bt_core_reset_adapter(void)
        if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
                BT_ERR("running script failed");
 
-       _bt_core_terminate();
+       g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
 
        return TRUE;
 }
@@ -685,12 +800,27 @@ gboolean _bt_core_set_transfer_value(gboolean value)
 
 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
 {
+       int ret;
+       BT_DBG("Test item : %s", type);
 
+#ifdef TIZEN_FEATURE_ACTD
+       if (g_strcmp0(type, "Enable_RF_Test") == 0) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else {
+               BT_DBG("Terminate bt-core process");
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
+               return FALSE;
+       }
+#else
        char *cmd = NULL;
        char *arg_list[3] = { NULL, NULL, NULL };
 
-       BT_DBG("Test item : %s", type);
-
        if (g_strcmp0(type, "Enable_RF_Test") == 0) {
                cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
                arg_list[0] = "bt-edutm-on.sh";
@@ -712,29 +842,25 @@ gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
                arg_list[0] = "bt-enable-rf-channel.sh";
                arg_list[1] = (char *)arg;
        } else {
-               _bt_core_terminate();
                return FALSE;
        }
 
        BT_DBG("Run %s", cmd);
        if (__execute_command(cmd, arg_list) < 0)
                BT_ERR("running script failed");
+#endif
 
-       _bt_core_terminate();
        return TRUE;
 }
 
 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)
@@ -759,7 +885,6 @@ 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);