OSDN Git Service

Enum-ify stack/include/gatt_api::GATT_REASON
authorChris Manton <cmanton@google.com>
Fri, 20 Nov 2020 02:13:14 +0000 (18:13 -0800)
committerChris Manton <cmanton@google.com>
Thu, 7 Jan 2021 00:56:34 +0000 (16:56 -0800)
Towards readable code

Bug: 163134718
Tag: #refactor
Test: gd/cert/run --host

Change-Id: I4432ffbfaeb2167840ff9873787ed96bc7858c66

stack/gatt/gatt_api.cc
stack/gatt/gatt_int.h
stack/gatt/gatt_main.cc
stack/gatt/gatt_utils.cc
stack/include/gatt_api.h

index 6eb1030..7e3d5e3 100644 (file)
@@ -1100,7 +1100,8 @@ void GATT_StartIf(tGATT_IF gatt_if) {
       p_tcb = gatt_find_tcb_by_addr(bda, transport);
       if (p_reg->app_cb.p_conn_cb && p_tcb) {
         conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
-        (*p_reg->app_cb.p_conn_cb)(gatt_if, bda, conn_id, true, 0, transport);
+        (*p_reg->app_cb.p_conn_cb)(gatt_if, bda, conn_id, true,
+                                   GATT_CONN_UNKNOWN, transport);
       }
       start_idx = ++found_idx;
     }
index b7729d7..b26da78 100644 (file)
@@ -570,7 +570,8 @@ extern tGATT_STATUS gatt_send_write_msg(tGATT_TCB& p_tcb, tGATT_CLCB* p_clcb,
                                         uint8_t op_code, uint16_t handle,
                                         uint16_t len, uint16_t offset,
                                         uint8_t* p_data);
-extern void gatt_cleanup_upon_disc(const RawAddress& bda, uint16_t reason,
+extern void gatt_cleanup_upon_disc(const RawAddress& bda,
+                                   tGATT_DISCONN_REASON reason,
                                    tBT_TRANSPORT transport);
 extern void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status,
                                void* p_data);
index 0cbfc4c..9114095 100644 (file)
@@ -258,7 +258,8 @@ bool gatt_disconnect(tGATT_TCB* p_tcb) {
       ret = L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_tcb->peer_bda);
     } else {
       L2CA_CancelBleConnectReq(p_tcb->peer_bda);
-      gatt_cleanup_upon_disc(p_tcb->peer_bda, HCI_ERR_CONN_CAUSE_LOCAL_HOST, p_tcb->transport);
+      gatt_cleanup_upon_disc(p_tcb->peer_bda, GATT_CONN_TERMINATE_LOCAL_HOST,
+                             p_tcb->transport);
       return true;
     }
     gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);
@@ -421,8 +422,10 @@ static void gatt_le_connect_cback(uint16_t chan, const RawAddress& bd_addr,
   bool check_srv_chg = false;
   tGATTS_SRV_CHG* p_srv_chg_clt = NULL;
 
-  /* ignore all fixed channel connect/disconnect on BR/EDR link for GATT */
-  if (transport == BT_TRANSPORT_BR_EDR) return;
+  if (transport == BT_TRANSPORT_BR_EDR) {
+    LOG_WARN("Ignoring fixed channel connect/disconnect on br_edr for GATT");
+    return;
+  }
 
   VLOG(1) << "GATT   ATT protocol channel with BDA: " << bd_addr << " is "
           << ((connected) ? "connected" : "disconnected");
@@ -436,8 +439,8 @@ static void gatt_le_connect_cback(uint16_t chan, const RawAddress& bd_addr,
   }
 
   if (!connected) {
-    gatt_cleanup_upon_disc(bd_addr, reason, transport);
-    VLOG(1) << "ATT disconnected";
+    gatt_cleanup_upon_disc(bd_addr, static_cast<tGATT_DISCONN_REASON>(reason),
+                           transport);
     return;
   }
 
@@ -630,7 +633,9 @@ static void gatt_on_l2cap_error(uint16_t lcid, uint16_t result) {
   tGATT_TCB* p_tcb = gatt_find_tcb_by_cid(lcid);
   if (p_tcb == nullptr) return;
   if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN) {
-    gatt_cleanup_upon_disc(p_tcb->peer_bda, result, BT_TRANSPORT_BR_EDR);
+    gatt_cleanup_upon_disc(p_tcb->peer_bda,
+                           static_cast<tGATT_DISCONN_REASON>(result),
+                           BT_TRANSPORT_BR_EDR);
   } else {
     gatt_l2cif_disconnect(lcid);
   }
@@ -708,8 +713,9 @@ void gatt_l2cif_disconnect_ind_cback(uint16_t lcid, bool ack_needed) {
   }
   /* if ACL link is still up, no reason is logged, l2cap is disconnect from
    * peer */
-  uint16_t reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport);
-  if (reason == 0) reason = GATT_CONN_TERMINATE_PEER_USER;
+  tGATT_DISCONN_REASON reason = static_cast<tGATT_DISCONN_REASON>(
+      L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport));
+  if (reason == GATT_CONN_OK) reason = GATT_CONN_TERMINATE_PEER_USER;
 
   /* send disconnect callback */
   gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, BT_TRANSPORT_BR_EDR);
@@ -731,8 +737,9 @@ static void gatt_l2cif_disconnect(uint16_t lcid) {
   /* send disconnect callback */
   /* if ACL link is still up, no reason is logged, l2cap is disconnect from
    * peer */
-  uint16_t reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport);
-  if (reason == 0) reason = GATT_CONN_TERMINATE_LOCAL_HOST;
+  tGATT_DISCONN_REASON reason = static_cast<tGATT_DISCONN_REASON>(
+      L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport));
+  if (reason == GATT_CONN_OK) reason = GATT_CONN_TERMINATE_LOCAL_HOST;
 
   gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, BT_TRANSPORT_BR_EDR);
 }
@@ -776,8 +783,9 @@ static void gatt_send_conn_cback(tGATT_TCB* p_tcb) {
 
     if (p_reg->app_cb.p_conn_cb) {
       conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
-      (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id, true,
-                                 0, p_tcb->transport);
+      (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id,
+                                 kGattConnected, GATT_CONN_OK,
+                                 p_tcb->transport);
     }
   }
 
index 6a88f7d..ee44762 100644 (file)
@@ -1539,7 +1539,7 @@ void gatt_end_operation(tGATT_CLCB* p_clcb, tGATT_STATUS status, void* p_data) {
 }
 
 /** This function cleans up the control blocks when L2CAP channel disconnect */
-void gatt_cleanup_upon_disc(const RawAddress& bda, uint16_t reason,
+void gatt_cleanup_upon_disc(const RawAddress& bda, tGATT_DISCONN_REASON reason,
                             tBT_TRANSPORT transport) {
   VLOG(1) << __func__;
 
index 654f3c7..6d32c08 100644 (file)
@@ -191,20 +191,20 @@ inline std::string gatt_op_code_text(const tGATT_OP_CODE& op_code) {
 
 #define GATT_HANDLE_IS_VALID(x) ((x) != 0)
 
-#define GATT_CONN_UNKNOWN 0
-/* general L2cap failure  */
-#define GATT_CONN_L2C_FAILURE 1
-/* 0x08 connection timeout  */
-#define GATT_CONN_TIMEOUT HCI_ERR_CONNECTION_TOUT
-/* 0x13 connection terminate by peer user  */
-#define GATT_CONN_TERMINATE_PEER_USER HCI_ERR_PEER_USER
-/* 0x16 connectionterminated by local host  */
-#define GATT_CONN_TERMINATE_LOCAL_HOST HCI_ERR_CONN_CAUSE_LOCAL_HOST
-/* 0x22 connection fail for LMP response tout */
-#define GATT_CONN_LMP_TIMEOUT HCI_ERR_LMP_RESPONSE_TIMEOUT
-/* 0x0100 L2CAP connection cancelled  */
-#define GATT_CONN_CANCEL L2CAP_CONN_CANCEL
-typedef uint16_t tGATT_DISCONN_REASON;
+typedef enum : uint16_t {
+  GATT_CONN_OK = 0,
+  GATT_CONN_UNKNOWN = 0,
+  /* general L2cap failure  */
+  GATT_CONN_L2C_FAILURE = 1,
+  /* 0x08 connection timeout  */
+  GATT_CONN_TIMEOUT = HCI_ERR_CONNECTION_TOUT,
+  /* 0x13 connection terminate by peer user  */
+  GATT_CONN_TERMINATE_PEER_USER = HCI_ERR_PEER_USER,
+  /* 0x16 connectionterminated by local host  */
+  GATT_CONN_TERMINATE_LOCAL_HOST = HCI_ERR_CONN_CAUSE_LOCAL_HOST,
+  /* 0x22 connection fail for LMP response tout */
+  GATT_CONN_LMP_TIMEOUT = HCI_ERR_LMP_RESPONSE_TIMEOUT,
+} tGATT_DISCONN_REASON;
 
 /* MAX GATT MTU size
 */