From 3b9e0014dabcb619cfe39074db885e66f28700ab Mon Sep 17 00:00:00 2001 From: Carlos Barcellos Date: Fri, 12 Apr 2024 11:45:05 -0300 Subject: [PATCH] change(ble): add user pointers to avoid using global variables in callbacks --- components/bt/common/btc/core/btc_manage.c | 25 +++++++++++++++- components/bt/common/btc/core/btc_task.c | 16 ++++++++-- .../bt/common/btc/include/btc/btc_manage.h | 7 ++++- .../bt/host/bluedroid/api/esp_gap_ble_api.c | 14 ++++++++- .../bt/host/bluedroid/api/esp_gatts_api.c | 16 ++++++++-- .../api/include/api/esp_gap_ble_api.h | 29 ++++++++++++++++--- .../bluedroid/api/include/api/esp_gatts_api.h | 23 ++++++++++++++- tools/ci/check_public_headers_exceptions.txt | 7 ----- 8 files changed, 117 insertions(+), 20 deletions(-) diff --git a/components/bt/common/btc/core/btc_manage.c b/components/bt/common/btc/core/btc_manage.c index 52aa837b99..1377bfd6c7 100644 --- a/components/bt/common/btc/core/btc_manage.c +++ b/components/bt/common/btc/core/btc_manage.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,8 +10,10 @@ #if BTC_DYNAMIC_MEMORY == FALSE void *btc_profile_cb_tab[BTC_PID_NUM] = {}; +void *btc_profile_ptr_tab[BTC_PID_NUM] = {}; #else void **btc_profile_cb_tab; +void **btc_profile_ptr_tab; #endif void esp_profile_cb_reset(void) @@ -20,6 +22,7 @@ void esp_profile_cb_reset(void) for (i = 0; i < BTC_PID_NUM; i++) { btc_profile_cb_tab[i] = NULL; + btc_profile_ptr_tab[i] = NULL; } } @@ -42,3 +45,23 @@ void *btc_profile_cb_get(btc_pid_t profile_id) return btc_profile_cb_tab[profile_id]; } + +int btc_profile_ptr_set(btc_pid_t profile_id, void *ptr) +{ + if (profile_id < 0 || profile_id >= BTC_PID_NUM) { + return -1; + } + + btc_profile_ptr_tab[profile_id] = ptr; + + return 0; +} + +void *btc_profile_ptr_get(btc_pid_t profile_id) +{ + if (profile_id < 0 || profile_id >= BTC_PID_NUM) { + return NULL; + } + + return btc_profile_ptr_tab[profile_id]; +} diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index 0f2b6bc0b7..7072a030aa 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -295,8 +295,8 @@ static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout) /** * transfer an message to another module in the different task. * @param msg message - * @param arg paramter - * @param arg_len length of paramter + * @param arg parameter + * @param arg_len length of parameter * @param copy_func deep copy function * @param free_func deep free function * @return BT_STATUS_SUCCESS: success @@ -342,7 +342,7 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg } /** - * transfer an message to another module in tha same task. + * transfer an message to another module in the same task. * @param msg message * @return BT_STATUS_SUCCESS: success * others: fail @@ -380,6 +380,11 @@ static void btc_deinit_mem(void) { btc_profile_cb_tab = NULL; } + if (btc_profile_ptr_tab) { + osi_free(btc_profile_ptr_tab); + btc_profile_ptr_tab = NULL; + } + #if (BLE_INCLUDED == TRUE) if (gl_bta_adv_data_ptr) { osi_free(gl_bta_adv_data_ptr); @@ -442,6 +447,11 @@ static bt_status_t btc_init_mem(void) { } memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM); + if ((btc_profile_ptr_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) { + goto error_exit; + } + memset((void *)btc_profile_ptr_tab, 0, sizeof(void *) * BTC_PID_NUM); + #if (BLE_INCLUDED == TRUE) if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) { goto error_exit; diff --git a/components/bt/common/btc/include/btc/btc_manage.h b/components/bt/common/btc/include/btc/btc_manage.h index b55c1a3a85..526d9982b7 100644 --- a/components/bt/common/btc/include/btc/btc_manage.h +++ b/components/bt/common/btc/include/btc/btc_manage.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,8 +11,10 @@ #if BTC_DYNAMIC_MEMORY == FALSE extern void *btc_profile_cb_tab[BTC_PID_NUM]; +extern void *btc_profile_ptr_tab[BTC_PID_NUM]; #else extern void **btc_profile_cb_tab; +extern void **btc_profile_ptr_tab; #endif /* reset gatt callback table */ void esp_profile_cb_reset(void); @@ -20,4 +22,7 @@ void esp_profile_cb_reset(void); int btc_profile_cb_set(btc_pid_t profile_id, void *cb); void *btc_profile_cb_get(btc_pid_t profile_id); +int btc_profile_ptr_set(btc_pid_t profile_id, void *ptr); +void *btc_profile_ptr_get(btc_pid_t profile_id); + #endif /* __BTC_MANAGE_H__ */ diff --git a/components/bt/host/bluedroid/api/esp_gap_ble_api.c b/components/bt/host/bluedroid/api/esp_gap_ble_api.c index 5db7b62973..e2d3af15b1 100644 --- a/components/bt/host/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_ble_api.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -27,6 +27,18 @@ esp_gap_ble_cb_t esp_ble_gap_get_callback(void) return (esp_gap_ble_cb_t) btc_profile_cb_get(BTC_PID_GAP_BLE); } +esp_err_t esp_ble_gap_register_ptr(void *ptr) +{ + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + return (btc_profile_ptr_set(BTC_PID_GAP_BLE, ptr) == 0 ? ESP_OK : ESP_FAIL); +} + +void *esp_ble_gap_get_ptr(void) +{ + return btc_profile_ptr_get(BTC_PID_GAP_BLE); +} + #if (BLE_42_FEATURE_SUPPORT == TRUE) esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data) { diff --git a/components/bt/host/bluedroid/api/esp_gatts_api.c b/components/bt/host/bluedroid/api/esp_gatts_api.c index 66fa5c6617..b500a1e23f 100644 --- a/components/bt/host/bluedroid/api/esp_gatts_api.c +++ b/components/bt/host/bluedroid/api/esp_gatts_api.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -34,6 +34,18 @@ esp_gatts_cb_t esp_ble_gatts_get_callback(void) return (esp_gatts_cb_t) btc_profile_cb_get(BTC_PID_GATTS); } +esp_err_t esp_ble_gatts_register_ptr(void *ptr) +{ + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + return (btc_profile_ptr_set(BTC_PID_GATTS, ptr) == 0 ? ESP_OK : ESP_FAIL); +} + +void *esp_ble_gatts_get_ptr(void) +{ + return btc_profile_ptr_get(BTC_PID_GATTS); +} + esp_err_t esp_ble_gatts_app_register(uint16_t app_id) { btc_msg_t msg = {0}; @@ -272,7 +284,7 @@ esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id, } if (L2CA_CheckIsCongest(L2CAP_ATT_CID, p_tcb->peer_bda)) { - LOG_DEBUG("%s, the l2cap chanel is congest.", __func__); + LOG_DEBUG("%s, the l2cap channel is congest.", __func__); return ESP_FAIL; } diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index 2976f56f87..5dab0424d3 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -156,7 +156,7 @@ typedef enum { ESP_GAP_BLE_PASSKEY_REQ_EVT, /*!< passkey request event */ ESP_GAP_BLE_OOB_REQ_EVT, /*!< OOB request event */ ESP_GAP_BLE_LOCAL_IR_EVT, /*!< BLE local IR (identity Root 128-bit random static value used to generate Long Term Key) event */ - ESP_GAP_BLE_LOCAL_ER_EVT, /*!< BLE local ER (Encryption Root vakue used to genrate identity resolving key) event */ + ESP_GAP_BLE_LOCAL_ER_EVT, /*!< BLE local ER (Encryption Root value used to generate identity resolving key) event */ ESP_GAP_BLE_NC_REQ_EVT, /*!< Numeric Comparison request event */ //BLE_42_FEATURE_SUPPORT ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT, /*!< When stop adv complete, the event comes */ @@ -786,9 +786,9 @@ typedef uint8_t esp_ble_gap_all_phys_t; #define ESP_BLE_GAP_PRI_PHY_CODED ESP_BLE_GAP_PHY_CODED /*!< Primary Phy is LE CODED */ typedef uint8_t esp_ble_gap_pri_phy_t; // primary phy -#define ESP_BLE_GAP_PHY_1M_PREF_MASK (1 << 0) /*!< The Host prefers use the LE1M transmitter or reciever PHY */ -#define ESP_BLE_GAP_PHY_2M_PREF_MASK (1 << 1) /*!< The Host prefers use the LE2M transmitter or reciever PHY */ -#define ESP_BLE_GAP_PHY_CODED_PREF_MASK (1 << 2) /*!< The Host prefers use the LE CODED transmitter or reciever PHY */ +#define ESP_BLE_GAP_PHY_1M_PREF_MASK (1 << 0) /*!< The Host prefers use the LE1M transmitter or receiver PHY */ +#define ESP_BLE_GAP_PHY_2M_PREF_MASK (1 << 1) /*!< The Host prefers use the LE2M transmitter or receiver PHY */ +#define ESP_BLE_GAP_PHY_CODED_PREF_MASK (1 << 2) /*!< The Host prefers use the LE CODED transmitter or receiver PHY */ typedef uint8_t esp_ble_gap_phy_mask_t; #define ESP_BLE_GAP_PHY_OPTIONS_NO_PREF 0 /*!< The Host has no preferred coding when transmitting on the LE Coded PHY */ @@ -1501,6 +1501,27 @@ esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback); */ esp_gap_ble_cb_t esp_ble_gap_get_callback(void); +/** + * @brief This function is called to register user pointer + * + * @param[in] ptr: user pointer to set + * + * @return + * - ESP_OK : success + * - other : failed + * + */ +esp_err_t esp_ble_gap_register_ptr(void *ptr); + +/** + * @brief This function is called to get the current gap user pointer + * + * @return + * - current user pointer (may be NULL if not set) + * + */ +void *esp_ble_gap_get_ptr(void); + #if (BLE_42_FEATURE_SUPPORT == TRUE) /** * @brief This function is called to override the BTA default ADV parameters. diff --git a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h index 0eb7ddd98b..ac1526bf66 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gatts_api.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -305,6 +305,27 @@ esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback); */ esp_gatts_cb_t esp_ble_gatts_get_callback(void); +/** + * @brief This function is called to register application user pointer + * with BTA GATTS module. + * + * @return + * - ESP_OK : success + * - other : failed + * + */ +esp_err_t esp_ble_gatts_register_ptr(void *ptr); + +/** + * @brief This function is called to get the current application user pointer + * with BTA GATTS module. + * + * @return + * - current user pointer (may be NULL if not set) + * + */ +void *esp_ble_gatts_get_ptr(void); + /** * @brief This function is called to register application identifier * diff --git a/tools/ci/check_public_headers_exceptions.txt b/tools/ci/check_public_headers_exceptions.txt index 8cfb7b46cc..cc8b22300b 100644 --- a/tools/ci/check_public_headers_exceptions.txt +++ b/tools/ci/check_public_headers_exceptions.txt @@ -21,10 +21,7 @@ components/esp_rom/include/esp32s2/rom/rsa_pss.h # LWIP: sockets.h uses #include_next<>, which doesn't work correctly with the checker # memp_std.h is supposed to be included multiple times with different settings -components/lwip/lwip/src/include/lwip/priv/memp_std.h components/lwip/include/lwip/sockets.h -components/lwip/lwip/src/include/lwip/prot/nd6.h -components/lwip/lwip/src/include/netif/ppp/ components/spi_flash/include/spi_flash_chip_issi.h components/spi_flash/include/spi_flash_chip_mxic.h @@ -60,13 +57,9 @@ components/json/cJSON/ components/spiffs/include/spiffs_config.h -components/unity/unity/src/unity_internals.h -components/unity/unity/extras/ components/unity/include/unity_config.h components/unity/include/unity_test_runner.h -components/cmock/CMock/src/cmock.h -components/cmock/CMock/src/cmock_internals.h components/openthread/openthread/