Add the status check logic for BT enable / disable method 67/219167/2
authorDoHyun Pyun <dh79.pyun@samsung.com>
Tue, 3 Dec 2019 00:31:03 +0000 (09:31 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Tue, 3 Dec 2019 01:43:18 +0000 (10:43 +0900)
Change-Id: I403f689924135cd8097dd31d51a42f14836022e1
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
bt-service-adaptation/services/adapter/bt-service-core-adapter.c
bt-service-adaptation/services/include/bt-service-core-adapter.h

index ff8e48e..26148dc 100644 (file)
@@ -136,6 +136,29 @@ int _bt_stack_init(void)
 
 int _bt_enable_adapter(void)
 {
+       bt_le_status_t le_status = BT_LE_DEACTIVATED;
+
+       le_status = _bt_adapter_get_le_status();
+
+       BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
+
+       if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
+               BT_ERR("Enabling in progress");
+               return BLUETOOTH_ERROR_IN_PROGRESS;
+       }
+
+       if (adapter_state == BT_ACTIVATED) {
+               BT_ERR("Already enabled");
+               return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
+       }
+
+       if (adapter_state == BT_DEACTIVATING ||
+                adapter_state == BT_TERMINATING ||
+                 le_status == BT_LE_DEACTIVATING) {
+               BT_ERR("Disabling in progress");
+               return BLUETOOTH_ERROR_DEVICE_BUSY;
+       }
+
        return __bt_adapter_state_handle_request(TRUE);
 }
 
@@ -189,6 +212,32 @@ int _bt_check_adapter(int *status)
 
 int _bt_disable_adapter(void)
 {
+       bt_le_status_t le_status = BT_LE_DEACTIVATED;
+
+       le_status = _bt_adapter_get_le_status();
+
+       BT_INFO("Current state [%d], LE [%d]", adapter_state, le_status);
+
+       if (adapter_state == BT_DEACTIVATING ||
+                adapter_state == BT_TERMINATING ||
+                 le_status == BT_LE_DEACTIVATING) {
+               BT_DBG("Disabling in progress");
+               return BLUETOOTH_ERROR_IN_PROGRESS;
+       }
+
+       if (adapter_state == BT_DEACTIVATED) {
+               BT_DBG("Already disabled");
+               return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
+       }
+
+       if (adapter_state == BT_ACTIVATING || le_status == BT_LE_ACTIVATING) {
+               BT_ERR("Enabling in progress");
+               return BLUETOOTH_ERROR_DEVICE_BUSY;
+       }
+
+       /* TODO: Need to add the function to disconnect all devices */
+       /* __bt_disconnect_all(); */
+
        return __bt_adapter_state_handle_request(FALSE);
 }
 
@@ -1293,7 +1342,7 @@ static void __bt_adapter_state_change_callback(int bt_status)
 
        switch (bt_status) {
        case BT_DEACTIVATED:
-               __bt_adapter_state_set_status(bt_status);
+               __bt_adapter_state_set_status(BT_TERMINATING);
 
                /* Adapter is disabled, unregister event handlers */
                _bt_service_unregister_event_handler_callback(BT_ADAPTER_MODULE);
@@ -1334,30 +1383,11 @@ static int __bt_adapter_state_handle_request(gboolean enable)
        BT_DBG("");
 
        switch (adapter_state) {
-       case BT_ACTIVATING: {
-               BT_INFO("Adapter is currently in activating state, state [%d]",
-                               adapter_state);
-               if (enable) {
-                       return BLUETOOTH_ERROR_IN_PROGRESS;
-               } else {
-                       if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
-                                       adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
-                               /*TODO Stop Discovery*/
-                               __bt_adapter_update_discovery_status(FALSE);
-                       }
-                       result = adapter_disable();
-                       if (result != OAL_STATUS_SUCCESS) {
-                               BT_ERR("adapter_enable failed: [%d]", result);
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                               /*TODO: perform if anything more needs to be done to handle failure */
-                       } else {
-                               /* TODO: To be handled */
-                               __bt_adapter_state_set_status(BT_DEACTIVATING);
-                               result = BLUETOOTH_ERROR_NONE;
-                       }
-               }
+       case BT_ACTIVATING:
+       case BT_DEACTIVATING:
+               BT_INFO("Should not be callded : state [%d]", adapter_state);
+               return BLUETOOTH_ERROR_INTERNAL;
                break;
-       }
        case BT_ACTIVATED: {
                BT_INFO("Adapter is currently in activated state, state [%d]",
                                adapter_state);
@@ -1382,27 +1412,6 @@ static int __bt_adapter_state_handle_request(gboolean enable)
                }
                break;
        }
-       case BT_DEACTIVATING: {
-               BT_INFO("Adapter is currently in deactivating state, state [%d]",
-                               adapter_state);
-               if (!enable) {
-                       return BLUETOOTH_ERROR_IN_PROGRESS;
-
-               } else {
-                       result = adapter_enable();
-                       if (result != OAL_STATUS_SUCCESS && result != OAL_STATUS_PENDING) {
-                               BT_ERR("adapter_enable failed: [%d]", result);
-                               adapter_disable();
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                               /*TODO: perform if anything more needs to be done to handle failure */
-                       } else {
-                               /* TODO: To be handled */
-                               __bt_adapter_state_set_status(BT_ACTIVATING);
-                               result = BLUETOOTH_ERROR_NONE;
-                       }
-               }
-               break;
-       }
        case BT_DEACTIVATED: {
                BT_INFO("Adapter is currently in deactivated state, state [%d]",
                                adapter_state);
index 9a061df..0394fac 100644 (file)
@@ -41,6 +41,7 @@ typedef enum {
        BT_ACTIVATED,
        BT_ACTIVATING,
        BT_DEACTIVATING,
+       BT_TERMINATING,
 } bt_status_t;
 
 int _bt_enable_adapter(void);