OSDN Git Service

Encapsulate tACL_CB::tACL_CONN::switch_role_state
authorChris Manton <cmanton@google.com>
Sun, 23 Aug 2020 06:26:47 +0000 (23:26 -0700)
committerChris Manton <cmanton@google.com>
Wed, 26 Aug 2020 18:28:57 +0000 (11:28 -0700)
Towards proper interfaces

Bug: 163134718
Tag: #refactor
Test: compile & verify basic functions working

Change-Id: I320b9709a23746e5c7e752214126dafcf158b26a

stack/acl/acl.h
stack/acl/btm_acl.cc

index 19c3ae2..0b63102 100644 (file)
@@ -33,6 +33,15 @@ enum btm_acl_encrypt_state_t {
   BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON = 3,
 };
 
+enum btm_acl_swkey_state_t {
+  BTM_ACL_SWKEY_STATE_IDLE = 0,
+  BTM_ACL_SWKEY_STATE_MODE_CHANGE = 1,
+  BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF = 2,
+  BTM_ACL_SWKEY_STATE_SWITCHING = 3,
+  BTM_ACL_SWKEY_STATE_ENCRYPTION_ON = 4,
+  BTM_ACL_SWKEY_STATE_IN_PROGRESS = 5,
+};
+
 /* Structure returned with Role Switch information (in tBTM_CMPL_CB callback
  * function) in response to BTM_SwitchRole call.
  */
@@ -90,7 +99,51 @@ typedef struct {
   uint8_t lmp_version;
   uint8_t num_read_pages;
   uint8_t switch_role_failed_attempts;
-  uint8_t switch_role_state;
+
+ private:
+  uint8_t switch_role_state_;
+
+ public:
+  void reset_switch_role() { switch_role_state_ = BTM_ACL_SWKEY_STATE_IDLE; }
+  void set_switch_role_changing() {
+    switch_role_state_ = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
+  }
+  void set_switch_role_encryption_off() {
+    switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
+  }
+  void set_switch_role_encryption_on() {
+    switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
+  }
+  void set_switch_role_in_progress() {
+    switch_role_state_ = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
+  }
+  void set_switch_role_switching() {
+    switch_role_state_ = BTM_ACL_SWKEY_STATE_SWITCHING;
+  }
+
+  bool is_switch_role_idle() const {
+    return switch_role_state_ != BTM_ACL_SWKEY_STATE_IDLE;
+  }
+  bool is_switch_role_encryption_off() const {
+    return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
+  }
+  bool is_switch_role_encryption_on() const {
+    return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
+  }
+  bool is_switch_role_switching() const {
+    return switch_role_state_ == BTM_ACL_SWKEY_STATE_SWITCHING;
+  }
+  bool is_switch_role_in_progress() const {
+    return switch_role_state_ == BTM_ACL_SWKEY_STATE_IN_PROGRESS;
+  }
+  bool is_switch_role_mode_change() const {
+    return switch_role_state_ == BTM_ACL_SWKEY_STATE_MODE_CHANGE;
+  }
+  bool is_switch_role_switching_or_in_progress() const {
+    return is_switch_role_switching() || is_switch_role_in_progress();
+  }
+
+ public:
   uint8_t sca; /* Sleep clock accuracy */
 } tACL_CONN;
 
index 3595829..b98f8ab 100644 (file)
@@ -72,13 +72,6 @@ namespace {
 StackAclBtmAcl internal_;
 }
 
-#define BTM_ACL_SWKEY_STATE_IDLE 0
-#define BTM_ACL_SWKEY_STATE_MODE_CHANGE 1
-#define BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF 2
-#define BTM_ACL_SWKEY_STATE_SWITCHING 3
-#define BTM_ACL_SWKEY_STATE_ENCRYPTION_ON 4
-#define BTM_ACL_SWKEY_STATE_IN_PROGRESS 5
-
 #define BTM_MAX_SW_ROLE_FAILED_ATTEMPTS 3
 
 extern tBTM_CB btm_cb;
@@ -356,7 +349,7 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
         btm_ble_refresh_local_resolvable_private_addr(
             bda, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
       p->switch_role_failed_attempts = 0;
-      p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
+      p->reset_switch_role();
 
       /*******************************************************************************
        *
@@ -596,9 +589,9 @@ tBTM_STATUS BTM_SwitchRole(const RawAddress& remote_bd_addr, uint8_t new_role) {
   if (is_sco_active) return (BTM_NO_RESOURCES);
 
   /* Ignore role switch request if the previous request was not completed */
-  if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE) {
-    BTM_TRACE_DEBUG("BTM_SwitchRole busy: %d", p->switch_role_state);
-    return (BTM_BUSY);
+  if (!p->is_switch_role_idle()) {
+    LOG_DEBUG("%s switch role in progress", __func__);
+    return BTM_BUSY;
   }
 
   if (interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &remote_bd_addr)) {
@@ -618,7 +611,7 @@ tBTM_STATUS BTM_SwitchRole(const RawAddress& remote_bd_addr, uint8_t new_role) {
     status = BTM_SetPowerMode(BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
     if (status != BTM_CMD_STARTED) return (BTM_WRONG_MODE);
 
-    p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
+    p->set_switch_role_changing();
   }
   /* some devices do not support switch while encryption is on */
   else {
@@ -628,11 +621,10 @@ tBTM_STATUS BTM_SwitchRole(const RawAddress& remote_bd_addr, uint8_t new_role) {
         !BTM_EPR_AVAILABLE(p)) {
       /* bypass turning off encryption if change link key is already doing it */
       p->set_encryption_off();
-      p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
+      p->set_switch_role_encryption_off();
     } else {
       btsnd_hcic_switch_role(remote_bd_addr, new_role);
-      p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
-
+      p->set_switch_role_in_progress();
       if (p_dev_rec) p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
     }
   }
@@ -668,14 +660,14 @@ void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
     return;
 
   /* Process Role Switch if active */
-  if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF) {
+  if (p->is_switch_role_encryption_off()) {
     /* if encryption turn off failed we still will try to switch role */
     if (encr_enable) {
-      p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
       p->set_encryption_idle();
+      p->reset_switch_role();
     } else {
-      p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
       p->set_encryption_switching();
+      p->set_switch_role_switching();
     }
 
     btsnd_hcic_switch_role(p->remote_addr, (uint8_t)!p->link_role);
@@ -684,8 +676,8 @@ void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
 
   }
   /* Finished enabling Encryption after role switch */
-  else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON) {
-    p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
+  else if (p->is_switch_role_encryption_on()) {
+    p->reset_switch_role();
     p->set_encryption_idle();
     auto new_role = btm_cb.acl_cb_.switch_role_ref_data.role;
     auto hci_status = btm_cb.acl_cb_.switch_role_ref_data.hci_status;
@@ -693,9 +685,8 @@ void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
         btm_cb.acl_cb_.switch_role_ref_data.remote_bd_addr, new_role,
         hci_status);
 
-    BTM_TRACE_DEBUG(
-        "%s: Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
-        __func__, new_role, hci_status, p->switch_role_state);
+    BTM_TRACE_DEBUG("%s: Role Switch Event: new_role 0x%02x, HCI Status 0x%02x",
+                    __func__, new_role, hci_status);
 
     /* If a disconnect is pending, issue it now that role switch has completed
      */
@@ -1448,8 +1439,7 @@ void btm_blacklist_role_change_device(const RawAddress& bd_addr,
        p_dev_rec->dev_class[2]) &
       0xffffff;
   if ((hci_status != HCI_SUCCESS) &&
-      ((p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) ||
-       (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)) &&
+      (p->is_switch_role_switching_or_in_progress()) &&
       ((cod & cod_audio_device) == cod_audio_device) &&
       (!interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &bd_addr))) {
     p->switch_role_failed_attempts++;
@@ -1510,25 +1500,23 @@ void StackAclBtmAcl::btm_acl_role_changed(uint8_t hci_status,
 
   /* if switching state is switching we need to turn encryption on */
   /* if idle, we did not change encryption */
-  if (p_acl->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
+  if (p_acl->is_switch_role_switching()) {
     p_acl->set_encryption_on();
-    p_acl->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
+    p_acl->set_switch_role_encryption_on();
     return;
   }
 
   /* Set the switch_role_state to IDLE since the reply received from HCI */
   /* regardless of its result either success or failed. */
-  if (p_acl->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
-    p_acl->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
+  if (p_acl->is_switch_role_in_progress()) {
     p_acl->set_encryption_idle();
+    p_acl->reset_switch_role();
   }
 
   BTA_dm_report_role_change(bd_addr, new_role, hci_status);
   BTM_TRACE_DEBUG(
-      "%s: peer %s Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, "
-      "rs_st:%d",
-      __func__, bd_addr.ToString().c_str(), p_switch_role->role,
-      p_switch_role->hci_status, p_acl->switch_role_state);
+      "%s: peer %s Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, ",
+      __func__, bd_addr.ToString().c_str(), new_role, hci_status);
 
   /* If a disconnect is pending, issue it now that role switch has completed */
   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
@@ -2253,7 +2241,7 @@ void btm_cont_rswitch(tACL_CONN* p, tBTM_SEC_DEV_REC* p_dev_rec) {
   BTM_TRACE_DEBUG("btm_cont_rswitch");
   /* Check to see if encryption needs to be turned off if pending
      change of link key or role switch */
-  if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
+  if (p->is_switch_role_mode_change()) {
     /* Must turn off Encryption first if necessary */
     /* Some devices do not support switch or change of link key while encryption
      * is on */
@@ -2261,13 +2249,14 @@ void btm_cont_rswitch(tACL_CONN* p, tBTM_SEC_DEV_REC* p_dev_rec) {
         ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0) &&
         !BTM_EPR_AVAILABLE(p)) {
       p->set_encryption_off();
-      if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
-        p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
+      if (p->is_switch_role_mode_change()) {
+        p->set_switch_role_encryption_off();
+      }
     } else /* Encryption not used or EPR supported, continue with switch
               and/or change of link key */
     {
-      if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
-        p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
+      if (p->is_switch_role_mode_change()) {
+        p->set_switch_role_in_progress();
         if (p_dev_rec) p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
         btsnd_hcic_switch_role(p->remote_addr, (uint8_t)!p->link_role);
       }
@@ -2679,7 +2668,7 @@ bool acl_is_switch_role_idle(const RawAddress& bd_addr,
   if (p_acl == nullptr) {
     return false;
   }
-  return p_acl->switch_role_state == BTM_ACL_SWKEY_STATE_IDLE;
+  return p_acl->is_switch_role_idle();
 }
 
 /*******************************************************************************