You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ko...@apache.org on 2021/02/18 14:30:12 UTC

[mynewt-nimble] 01/03: host/ble_gap.c: optimize code Not all function bodies or methods must compile always. Added preprocessor directives to exclude such code fragments if not required, depending on roles/features.

This is an automated email from the ASF dual-hosted git repository.

kopyscinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 3a9d1d85977a8be91cd8e93a138785e3521291b0
Author: Krzysztof Kopyściński <kr...@codecoup.pl>
AuthorDate: Fri Feb 12 14:33:07 2021 +0100

    host/ble_gap.c: optimize code
    Not all function bodies or methods must compile always. Added
    preprocessor directives to exclude such code fragments if not required,
    depending on roles/features.
---
 nimble/host/src/ble_gap.c | 135 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 120 insertions(+), 15 deletions(-)

diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c
index bbedc31..45747ff 100644
--- a/nimble/host/src/ble_gap.c
+++ b/nimble/host/src/ble_gap.c
@@ -225,7 +225,9 @@ ble_gap_update_entry_remove(uint16_t conn_handle);
 static int ble_gap_adv_enable_tx(int enable);
 #endif
 
+#if NIMBLE_BLE_CONNECT
 static int ble_gap_conn_cancel_tx(void);
+#endif
 
 #if NIMBLE_BLE_SCAN && !MYNEWT_VAL(BLE_EXT_ADV)
 static int ble_gap_disc_enable_tx(int enable, int filter_duplicates);
@@ -387,6 +389,7 @@ ble_gap_log_adv(uint8_t own_addr_type, const ble_addr_t *direct_addr,
  * $snapshot                                                                 *
  *****************************************************************************/
 
+#if NIMBLE_BLE_CONNECT
 static void
 ble_gap_fill_conn_desc(struct ble_hs_conn *conn,
                        struct ble_gap_conn_desc *desc)
@@ -443,10 +446,12 @@ ble_gap_find_snapshot(uint16_t handle, struct ble_gap_snapshot *snap)
         return 0;
     }
 }
+#endif
 
 int
 ble_gap_conn_find(uint16_t handle, struct ble_gap_conn_desc *out_desc)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hs_conn *conn;
 
     ble_hs_lock();
@@ -463,12 +468,16 @@ ble_gap_conn_find(uint16_t handle, struct ble_gap_conn_desc *out_desc)
     } else {
         return 0;
     }
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_conn_find_by_addr(const ble_addr_t *addr,
                           struct ble_gap_conn_desc *out_desc)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hs_conn *conn;
 
     ble_hs_lock();
@@ -485,8 +494,12 @@ ble_gap_conn_find_by_addr(const ble_addr_t *addr,
     }
 
     return 0;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
+#if NIMBLE_BLE_CONNECT
 static int
 ble_gap_extract_conn_cb(uint16_t conn_handle,
                         ble_gap_event_fn **out_cb, void **out_cb_arg)
@@ -514,16 +527,22 @@ ble_gap_extract_conn_cb(uint16_t conn_handle,
         return 0;
     }
 }
+#endif
 
 int
 ble_gap_set_priv_mode(const ble_addr_t *peer_addr, uint8_t priv_mode)
 {
+#if NIMBLE_BLE_CONNECT
     return ble_hs_pvcy_set_mode(peer_addr, priv_mode);
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_read_le_phy(uint16_t conn_handle, uint8_t *tx_phy, uint8_t *rx_phy)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hci_le_rd_phy_cp cmd;
     struct ble_hci_le_rd_phy_rp rsp;
     struct ble_hs_conn *conn;
@@ -554,11 +573,15 @@ ble_gap_read_le_phy(uint16_t conn_handle, uint8_t *tx_phy, uint8_t *rx_phy)
     *rx_phy = rsp.rx_phy;
 
     return 0;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_set_prefered_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hci_le_set_default_phy_cp cmd;
 
     if (tx_phys_mask > (BLE_HCI_LE_PHY_1M_PREF_MASK |
@@ -590,12 +613,16 @@ ble_gap_set_prefered_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
     return ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_LE,
                                         BLE_HCI_OCF_LE_SET_DEFAULT_PHY),
                             &cmd, sizeof(cmd), NULL, 0);
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_set_prefered_le_phy(uint16_t conn_handle, uint8_t tx_phys_mask,
                    uint8_t rx_phys_mask, uint16_t phy_opts)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hci_le_set_phy_cp cmd;
     struct ble_hs_conn *conn;
 
@@ -642,6 +669,9 @@ ble_gap_set_prefered_le_phy(uint16_t conn_handle, uint8_t tx_phys_mask,
 
     return ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PHY),
                              &cmd, sizeof(cmd), NULL, 0);
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 /*****************************************************************************
@@ -673,7 +703,7 @@ ble_gap_call_event_cb(struct ble_gap_event *event,
     return rc;
 }
 
-
+#if NIMBLE_BLE_CONNECT
 static int
 ble_gap_call_conn_event_cb(struct ble_gap_event *event, uint16_t conn_handle)
 {
@@ -693,28 +723,32 @@ ble_gap_call_conn_event_cb(struct ble_gap_event *event, uint16_t conn_handle)
 
     return 0;
 }
+#endif
 
 static bool
 ble_gap_is_preempted(void)
 {
+#if NIMBLE_BLE_ADVERTISE
     int i;
-
+#endif
     BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
 
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER)
     if (ble_gap_master.preempted_op != BLE_GAP_OP_NULL) {
         return true;
     }
-
+#endif
+#if NIMBLE_BLE_ADVERTISE
     for (i = 0; i < BLE_ADV_INSTANCES; i++) {
         if (ble_gap_slave[i].preempted) {
             return true;
         }
     }
-
+#endif
     return false;
 }
 
-#if NIMBLE_BLE_CONNECT
+#if MYNEWT_VAL(BLE_ROLE_OBSERVER) || NIMBLE_BLE_CONNECT
 static void
 ble_gap_master_reset_state(void)
 {
@@ -726,6 +760,7 @@ ble_gap_master_reset_state(void)
 }
 #endif
 
+#if NIMBLE_BLE_ADVERTISE || NIMBLE_BLE_CONNECT
 static void
 ble_gap_slave_reset_state(uint8_t instance)
 {
@@ -736,8 +771,9 @@ ble_gap_slave_reset_state(uint8_t instance)
     ble_hs_timer_resched();
 #endif
 }
+#endif
 
-#if NIMBLE_BLE_CONNECT
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_PERIPHERAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER)
 static bool
 ble_gap_has_client(struct ble_gap_master_state *out_state)
 {
@@ -747,7 +783,9 @@ ble_gap_has_client(struct ble_gap_master_state *out_state)
 
     return out_state->cb != NULL;
 }
+#endif
 
+#if MYNEWT_VAL(BLE_ROLE_OBSERVER) || NIMBLE_BLE_CONNECT
 static void
 ble_gap_master_extract_state(struct ble_gap_master_state *out_state,
                              int reset_state)
@@ -765,6 +803,7 @@ ble_gap_master_extract_state(struct ble_gap_master_state *out_state,
 }
 #endif
 
+#if NIMBLE_BLE_ADVERTISE || NIMBLE_BLE_CONNECT
 static void
 ble_gap_slave_extract_cb(uint8_t instance,
                          ble_gap_event_fn **out_cb, void **out_cb_arg)
@@ -802,6 +841,7 @@ ble_gap_adv_finished(uint8_t instance, int reason, uint16_t conn_handle,
         cb(&event, cb_arg);
     }
 }
+#endif
 
 #if NIMBLE_BLE_CONNECT
 static int
@@ -875,22 +915,27 @@ ble_gap_disc_report(void *desc)
 static void
 ble_gap_disc_complete(void)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_master_state state;
+#endif
     struct ble_gap_event event;
 
     memset(&event, 0, sizeof event);
     event.type = BLE_GAP_EVENT_DISC_COMPLETE;
     event.disc_complete.reason = 0;
 
+#if NIMBLE_BLE_CONNECT
     ble_gap_master_extract_state(&state, 1);
     if (ble_gap_has_client(&state)) {
         ble_gap_call_event_cb(&event, state.cb, state.cb_arg);
     }
+#endif
 
     ble_gap_event_listener_call(&event);
 }
 #endif
 
+#if NIMBLE_BLE_CONNECT
 static void
 ble_gap_update_notify(uint16_t conn_handle, int status)
 {
@@ -909,6 +954,7 @@ ble_gap_update_notify(uint16_t conn_handle, int status)
         ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
     }
 }
+#endif
 
 static uint32_t
 ble_gap_master_ticks_until_exp(void)
@@ -1029,10 +1075,12 @@ static void
 ble_gap_master_failed(int status)
 {
     switch (ble_gap_master.op) {
+#if NIMBLE_BLE_CONNECT
     case BLE_GAP_OP_M_CONN:
         STATS_INC(ble_gap_stats, initiate_fail);
         ble_gap_master_connect_failure(status);
         break;
+#endif
 
 #if NIMBLE_BLE_SCAN
     case BLE_GAP_OP_M_DISC:
@@ -1070,6 +1118,7 @@ ble_gap_update_failed(uint16_t conn_handle, int status)
 void
 ble_gap_conn_broken(uint16_t conn_handle, int reason)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_update_entry *entry;
     struct ble_gap_snapshot snap;
     struct ble_gap_event event;
@@ -1115,6 +1164,7 @@ ble_gap_conn_broken(uint16_t conn_handle, int reason)
     ble_gap_call_event_cb(&event, snap.cb, snap.cb_arg);
 
     STATS_INC(ble_gap_stats, disconnect);
+#endif
 }
 
 #if NIMBLE_BLE_CONNECT
@@ -1237,15 +1287,21 @@ ble_gap_rx_update_complete(const struct ble_hci_ev_le_subev_conn_upd_complete *e
 int
 ble_gap_master_in_progress(void)
 {
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER)
     return ble_gap_master.op != BLE_GAP_OP_NULL;
+#else
+    return false;
+#endif
 }
 
+#if NIMBLE_BLE_ADVERTISE || NIMBLE_BLE_CONNECT
 static int
 ble_gap_adv_active_instance(uint8_t instance)
 {
     /* Assume read is atomic; mutex not necessary. */
     return ble_gap_slave[instance].op == BLE_GAP_OP_S_ADV;
 }
+#endif
 
 /**
  * Clears advertisement and discovery state.  This function is necessary
@@ -1856,6 +1912,7 @@ int
 ble_gap_rx_l2cap_update_req(uint16_t conn_handle,
                             struct ble_gap_upd_params *params)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
     int rc;
 
@@ -1866,11 +1923,15 @@ ble_gap_rx_l2cap_update_req(uint16_t conn_handle,
 
     rc = ble_gap_call_conn_event_cb(&event, conn_handle);
     return rc;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 void
 ble_gap_rx_phy_update_complete(const struct ble_hci_ev_le_subev_phy_update_complete *ev)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
     uint16_t conn_handle = le16toh(ev->conn_handle);
 
@@ -1883,13 +1944,16 @@ ble_gap_rx_phy_update_complete(const struct ble_hci_ev_le_subev_phy_update_compl
 
     ble_gap_event_listener_call(&event);
     ble_gap_call_conn_event_cb(&event, conn_handle);
+#endif
 }
 
 static int32_t
 ble_gap_master_timer(void)
 {
     uint32_t ticks_until_exp;
+#if NIMBLE_BLE_SCAN || NIMBLE_BLE_CONNECT
     int rc;
+#endif
 
     ticks_until_exp = ble_gap_master_ticks_until_exp();
     if (ticks_until_exp != 0) {
@@ -1900,6 +1964,7 @@ ble_gap_master_timer(void)
     /*** Timer expired; process event. */
 
     switch (ble_gap_master.op) {
+#if NIMBLE_BLE_CONNECT
     case BLE_GAP_OP_M_CONN:
         rc = ble_gap_conn_cancel_tx();
         if (rc != 0) {
@@ -1917,6 +1982,7 @@ ble_gap_master_timer(void)
              */
         }
         break;
+#endif
 
     case BLE_GAP_OP_M_DISC:
 #if NIMBLE_BLE_SCAN && !MYNEWT_VAL(BLE_EXT_ADV)
@@ -2003,6 +2069,7 @@ ble_gap_update_timer(void)
 int
 ble_gap_set_event_cb(uint16_t conn_handle, ble_gap_event_fn *cb, void *cb_arg)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hs_conn *conn;
 
     ble_hs_lock();
@@ -2020,6 +2087,9 @@ ble_gap_set_event_cb(uint16_t conn_handle, ble_gap_event_fn *cb, void *cb_arg)
     }
 
     return 0;
+#else
+    return BLE_HS_ENOTCONN;
+#endif
 }
 
 /**
@@ -2581,7 +2651,11 @@ ble_gap_adv_rsp_set_fields(const struct ble_hs_adv_fields *rsp_fields)
 int
 ble_gap_adv_active(void)
 {
+#if NIMBLE_BLE_ADVERTISE
     return ble_gap_adv_active_instance(0);
+#else
+    return 0;
+#endif
 }
 
 #if MYNEWT_VAL(BLE_EXT_ADV)
@@ -4487,8 +4561,12 @@ done:
 int
 ble_gap_disc_active(void)
 {
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER)
     /* Assume read is atomic; mutex not necessary. */
     return ble_gap_master.op == BLE_GAP_OP_M_DISC;
+#else
+    return 0;
+#endif
 }
 
 #if MYNEWT_VAL(BLE_ROLE_CENTRAL) && !MYNEWT_VAL(BLE_EXT_ADV)
@@ -4993,8 +5071,12 @@ done:
 int
 ble_gap_conn_active(void)
 {
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER)
     /* Assume read is atomic; mutex not necessary. */
     return ble_gap_master.op == BLE_GAP_OP_M_CONN;
+#else
+    return 0;
+#endif
 }
 
 /*****************************************************************************
@@ -5003,6 +5085,7 @@ ble_gap_conn_active(void)
 int
 ble_gap_terminate_with_conn(struct ble_hs_conn *conn, uint8_t hci_reason)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hci_lc_disconnect_cp cmd;
     int rc;
 
@@ -5027,11 +5110,15 @@ ble_gap_terminate_with_conn(struct ble_hs_conn *conn, uint8_t hci_reason)
 
     conn->bhc_flags |= BLE_HS_CONN_F_TERMINATING;
     return 0;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_terminate(uint16_t conn_handle, uint8_t hci_reason)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_hs_conn *conn;
     int rc;
 
@@ -5054,12 +5141,16 @@ done:
         STATS_INC(ble_gap_stats, terminate_fail);
     }
     return rc;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 /*****************************************************************************
  * $cancel                                                                   *
  *****************************************************************************/
 
+#if NIMBLE_BLE_CONNECT
 static int
 ble_gap_conn_cancel_tx(void)
 {
@@ -5075,7 +5166,6 @@ ble_gap_conn_cancel_tx(void)
     return 0;
 }
 
-#if NIMBLE_BLE_CONNECT
 static int
 ble_gap_conn_cancel_no_lock(void)
 {
@@ -5559,6 +5649,7 @@ ble_gap_encryption_initiate(uint16_t conn_handle,
 int
 ble_gap_unpair(const ble_addr_t *peer_addr)
 {
+#if NIMBLE_BLE_SM
     struct ble_hs_conn *conn;
 
     if (ble_addr_cmp(peer_addr, BLE_ADDR_ANY) == 0) {
@@ -5578,11 +5669,15 @@ ble_gap_unpair(const ble_addr_t *peer_addr)
                              peer_addr->val);
 
     return ble_store_util_delete_peer(peer_addr);
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_unpair_oldest_peer(void)
 {
+#if NIMBLE_BLE_SM
     ble_addr_t oldest_peer_id_addr;
     int num_peers;
     int rc;
@@ -5603,11 +5698,15 @@ ble_gap_unpair_oldest_peer(void)
     }
 
     return 0;
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 int
 ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr)
 {
+#if NIMBLE_BLE_SM
     ble_addr_t peer_id_addrs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
     int num_peers;
     int rc, i;
@@ -5633,13 +5732,16 @@ ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr)
     }
 
     return ble_gap_unpair(&peer_id_addrs[i]);
+#else
+    return BLE_HS_ENOTSUP;
+#endif
 }
 
 void
 ble_gap_passkey_event(uint16_t conn_handle,
                       struct ble_gap_passkey_params *passkey_params)
 {
-#if NIMBLE_BLE_SM
+#if NIMBLE_BLE_SM && NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     BLE_HS_LOG(DEBUG, "send passkey action request %d\n",
@@ -5657,7 +5759,7 @@ void
 ble_gap_enc_event(uint16_t conn_handle, int status,
                   int security_restored, int bonded)
 {
-#if NIMBLE_BLE_SM
+#if NIMBLE_BLE_SM && NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     memset(&event, 0, sizeof event);
@@ -5693,7 +5795,7 @@ ble_gap_enc_event(uint16_t conn_handle, int status,
 void
 ble_gap_identity_event(uint16_t conn_handle)
 {
-#if NIMBLE_BLE_SM
+#if NIMBLE_BLE_SM && NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     BLE_HS_LOG(DEBUG, "send identity changed");
@@ -5708,7 +5810,7 @@ ble_gap_identity_event(uint16_t conn_handle)
 int
 ble_gap_repeat_pairing_event(const struct ble_gap_repeat_pairing *rp)
 {
-#if NIMBLE_BLE_SM
+#if NIMBLE_BLE_SM && NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
     int rc;
 
@@ -5743,9 +5845,7 @@ void
 ble_gap_notify_rx_event(uint16_t conn_handle, uint16_t attr_handle,
                         struct os_mbuf *om, int is_indication)
 {
-#if !MYNEWT_VAL(BLE_GATT_NOTIFY) && !MYNEWT_VAL(BLE_GATT_INDICATE)
-    return;
-#endif
+#if (MYNEWT_VAL(BLE_GATT_NOTIFY) || MYNEWT_VAL(BLE_GATT_INDICATE)) && NIMBLE_BLE_CONNECT
 
     struct ble_gap_event event;
 
@@ -5759,13 +5859,14 @@ ble_gap_notify_rx_event(uint16_t conn_handle, uint16_t attr_handle,
     ble_gap_call_conn_event_cb(&event, conn_handle);
 
     os_mbuf_free_chain(event.notify_rx.om);
+#endif
 }
 
 void
 ble_gap_notify_tx_event(int status, uint16_t conn_handle, uint16_t attr_handle,
                         int is_indication)
 {
-#if MYNEWT_VAL(BLE_GATT_NOTIFY) || MYNEWT_VAL(BLE_GATT_INDICATE)
+#if (MYNEWT_VAL(BLE_GATT_NOTIFY) || MYNEWT_VAL(BLE_GATT_INDICATE)) && NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     memset(&event, 0, sizeof event);
@@ -5789,6 +5890,7 @@ ble_gap_subscribe_event(uint16_t conn_handle, uint16_t attr_handle,
                         uint8_t prev_notify, uint8_t cur_notify,
                         uint8_t prev_indicate, uint8_t cur_indicate)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     BLE_HS_DBG_ASSERT(prev_notify != cur_notify ||
@@ -5809,6 +5911,7 @@ ble_gap_subscribe_event(uint16_t conn_handle, uint16_t attr_handle,
 
     ble_gap_event_listener_call(&event);
     ble_gap_call_conn_event_cb(&event, conn_handle);
+#endif
 }
 
 /*****************************************************************************
@@ -5818,6 +5921,7 @@ ble_gap_subscribe_event(uint16_t conn_handle, uint16_t attr_handle,
 void
 ble_gap_mtu_event(uint16_t conn_handle, uint16_t cid, uint16_t mtu)
 {
+#if NIMBLE_BLE_CONNECT
     struct ble_gap_event event;
 
     memset(&event, 0, sizeof event);
@@ -5828,6 +5932,7 @@ ble_gap_mtu_event(uint16_t conn_handle, uint16_t cid, uint16_t mtu)
 
     ble_gap_event_listener_call(&event);
     ble_gap_call_conn_event_cb(&event, conn_handle);
+#endif
 }
 
 /*****************************************************************************