Migrate insmod logic into bt-stack-up.service
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-adapter.c
index 00b9c5c..ee43d10 100644 (file)
@@ -20,6 +20,7 @@
 #include <vconf-internal-radio-keys.h>
 #include <bundle.h>
 #include <eventsystem.h>
+#include <stdio.h>
 
 #include "bt-core-main.h"
 #include "bt-core-adapter.h"
 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
 
+#ifdef TIZEN_FEATURE_BT_OTP
+#define BT_OTP_OBJECT_PATH     "/org/projectx/otp"
+#define BT_OTP_INTERFACE_NAME  "org.projectx.otp_service"
+#define BLE_DISABLED           "LeDisabled"
+#endif
 
 static bt_status_t adapter_status = BT_DEACTIVATED;
 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
@@ -96,18 +102,46 @@ static gboolean __bt_core_idle_terminate(gpointer data)
 
 gboolean _bt_core_is_flight_mode_enabled(void)
 {
-#ifdef TIZEN_FEATURE_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;
+       }
+}
+
+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;
 
-       return isFlightMode;
-#else
-       return FALSE;
-#endif
 }
 
 static int __execute_command(const char *cmd, char *const arg_list[])
@@ -144,6 +178,44 @@ 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;
+       }
+
+       ret = __bt_call_systemact_service(BT_SYSTEMACT_STACK_UP);
+       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 */
+       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;
@@ -173,20 +245,22 @@ int _bt_enable_adapter(void)
 #endif
 
        __bt_core_set_status(BT_ACTIVATING);
-#ifdef 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);
-#endif
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else {
+               ret = __bt_stack_up();
+       }
        if (ret < 0) {
                BT_ERR("running script failed");
-#ifdef 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);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       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);
+               }
                __bt_core_set_status(BT_DEACTIVATED);
                return -1;
        }
@@ -219,33 +293,31 @@ int _bt_disable_adapter(void)
        status = _bt_core_get_status();
        if (status == BT_ACTIVATING) {
                /* Forcely terminate */
-#ifdef 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
-
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       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;
+                       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");
+                       /* 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);
+                       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");
-               }
+                       if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
+                               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
+                                       BT_ERR("running script failed");
+                       } else {
+                               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)
-                       BT_ERR("running script failed");
-#endif
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
+                               BT_ERR("running script failed");
 #endif
+               }
                _bt_core_terminate();
                return 0;
        } else if (status != BT_ACTIVATED) {
@@ -254,44 +326,43 @@ int _bt_disable_adapter(void)
        }
 
        __bt_core_set_status(BT_DEACTIVATING);
-#ifdef 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");
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               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;
                }
        } 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;
+#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 (__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 (__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) {
-                       BT_ERR("running script failed");
-                       __bt_core_set_status(BT_ACTIVATED);
-                       return -1;
-       }
-#endif
+               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+               }
 #endif
+       }
 
        return 0;
 }
@@ -310,20 +381,24 @@ int _bt_enable_adapter_le(void)
        if (status == BT_DEACTIVATED) {
                __bt_core_set_le_status(BT_LE_ACTIVATING);
                BT_DBG("Activate BT");
-#ifdef 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);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
+               } else {
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_STACK_UP);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
+               }
                if (ret < 0) {
                        BT_ERR("running script failed");
-#ifdef 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);
-#endif
+                       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                               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);
+                       }
                        __bt_core_set_status(BT_DEACTIVATED);
                        __bt_core_set_le_status(BT_LE_DEACTIVATED);
                        return -1;
@@ -341,6 +416,24 @@ int _bt_enable_adapter_le(void)
        return 0;
 }
 
+#ifdef TIZEN_FEATURE_BT_OTP
+static void _bt_core_stop_otp()
+{
+       GError *error = NULL;
+       GDBusMessage *msg = NULL;
+       GDBusConnection *conn = _bt_core_get_gdbus_connection();
+       msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
+       if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
+               if (error != NULL) {
+                       BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
+                                       message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+       }
+}
+#endif
+
 int _bt_disable_adapter_le(void)
 {
        BT_DBG("+");
@@ -356,17 +449,24 @@ int _bt_disable_adapter_le(void)
        _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 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) {
-#else
-               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
-#endif
+               int ret;
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       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);
+               }
+               if (ret < 0) {
                        BT_ERR("running script failed");
                        __bt_core_set_le_status(BT_LE_ACTIVATED);
                        return -1;
@@ -403,13 +503,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;
 
@@ -431,7 +533,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);
@@ -476,7 +578,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) {
@@ -512,19 +614,37 @@ 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)
@@ -599,7 +719,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;
 }
@@ -683,7 +803,6 @@ 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;
        }
 
@@ -691,7 +810,6 @@ gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
        if (__execute_command(cmd, arg_list) < 0)
                BT_ERR("running script failed");
 
-       _bt_core_terminate();
        return TRUE;
 }