Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user pointers to avoid using global variables in callbacks (IDFGH-12601) #13599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion components/bt/common/btc/core/btc_manage.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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)
Expand All @@ -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;
}
}

Expand All @@ -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];
}
16 changes: 13 additions & 3 deletions components/bt/common/btc/core/btc_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion components/bt/common/btc/include/btc/btc_manage.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -11,13 +11,18 @@

#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);

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__ */
14 changes: 13 additions & 1 deletion components/bt/host/bluedroid/api/esp_gap_ble_api.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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)
{
Expand Down
16 changes: 14 additions & 2 deletions components/bt/host/bluedroid/api/esp_gatts_api.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 25 additions & 4 deletions components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 22 additions & 1 deletion components/bt/host/bluedroid/api/include/api/esp_gatts_api.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
7 changes: 0 additions & 7 deletions tools/ci/check_public_headers_exceptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand Down