feat(ble/bluedroid): Support bluedroid dual identity

This commit is contained in:
zhiweijian
2026-07-10 17:57:09 +08:00
parent 18f50fefa9
commit 2358786647
28 changed files with 1774 additions and 37 deletions
@@ -512,6 +512,59 @@ esp_err_t esp_ble_gap_get_local_used_addr(esp_bd_addr_t local_used_addr, uint8_t
}
return ESP_OK;
}
#if (CONFIG_BT_BLE_PERIPH_PSEUDO_ADDR_BOND)
esp_err_t esp_ble_gap_get_real_peer_addr(esp_bd_addr_t pseudo, esp_bd_addr_t real_peer)
{
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
LOG_ERROR("%s, bluedroid status error", __func__);
return ESP_FAIL;
}
if (pseudo == NULL || real_peer == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (!BTM_BleGetRealPeerByPseudo(pseudo, real_peer)) {
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t esp_ble_gap_get_conn_identity(esp_bd_addr_t pseudo, esp_ble_conn_identity_t *identity)
{
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
LOG_ERROR("%s, bluedroid status error", __func__);
return ESP_FAIL;
}
if (pseudo == NULL || identity == NULL) {
return ESP_ERR_INVALID_ARG;
}
UINT8 peer_type = 0, local_type = 0;
if (!BTM_BleGetConnIdentityByPseudo(pseudo, identity->peer_addr, identity->local_addr,
&peer_type, &local_type)) {
return ESP_FAIL;
}
identity->peer_addr_type = peer_type;
identity->local_addr_type = local_type;
return ESP_OK;
}
esp_err_t esp_ble_gap_remove_bond_for_identity(esp_bd_addr_t local_addr,
esp_ble_addr_type_t local_addr_type,
esp_bd_addr_t peer_addr,
esp_ble_addr_type_t peer_addr_type)
{
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
LOG_ERROR("%s, bluedroid status error", __func__);
return ESP_FAIL;
}
if (local_addr == NULL || peer_addr == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_bd_addr_t pseudo;
BTM_BleComputePseudoForIdentity(local_addr, local_addr_type, peer_addr, peer_addr_type, pseudo);
return esp_ble_remove_bond_device(pseudo);
}
#endif // CONFIG_BT_BLE_PERIPH_PSEUDO_ADDR_BOND
#if ((BLE_42_SCAN_EN == TRUE) || (BLE_50_EXTEND_SCAN_EN == TRUE))
uint8_t *esp_ble_resolve_adv_data_by_type( uint8_t *adv_data, uint16_t adv_data_len, esp_ble_adv_data_type type, uint8_t *length)
{
@@ -3597,6 +3597,94 @@ esp_err_t esp_ble_gap_set_key_material(const uint8_t session_key[16], const uint
*/
esp_err_t esp_ble_gap_get_local_used_addr(esp_bd_addr_t local_used_addr, uint8_t * addr_type);
#if (CONFIG_BT_BLE_PERIPH_PSEUDO_ADDR_BOND)
/**
* @brief Reverse-map a Host pseudo address to the real peer identity.
*
* When CONFIG_BT_BLE_PERIPH_PSEUDO_ADDR_BOND is enabled, the
* remote_bda reported to the application for a dual local
* identity link is a Host-internal pseudo address (one peer
* phone connected through two local identities shows up as two
* different pseudo addresses). This helper returns the actual
* over-the-air peer identity for UI / diagnostics.
*
* **Must be called while the link is connected.** The mapping
* lives in a Host-side connection table that is cleared on
* disconnect. If there is no active link for `pseudo`, the call
* returns `ESP_FAIL` and `real_peer` is not modified.
*
* For offline bond information, use
* `esp_ble_get_bond_device_list()` and read
* `bond_key.pid_key.static_addr` for the real peer identity.
*
* @param[in] pseudo - the pseudo address as seen in remote_bda
* @param[out] real_peer - filled with the real peer identity on success
*
* @return - ESP_OK : success (link connected and pseudo known)
* - ESP_FAIL : Bluedroid not enabled, or pseudo not found /
* not connected
* - ESP_ERR_INVALID_ARG : NULL pointer argument
*/
esp_err_t esp_ble_gap_get_real_peer_addr(esp_bd_addr_t pseudo, esp_bd_addr_t real_peer);
/**
* @brief Full identity of a dual local-identity connection.
*/
typedef struct {
esp_bd_addr_t peer_addr; /*!< real over-the-air peer identity */
esp_bd_addr_t local_addr; /*!< local identity used for this link */
esp_ble_addr_type_t peer_addr_type; /*!< peer identity address type */
esp_ble_addr_type_t local_addr_type; /*!< local identity address type */
} esp_ble_conn_identity_t;
/**
* @brief Get the full (peer, local) identity of a dual local-identity
* link, keyed by the pseudo address the application sees as
* remote_bda.
*
* **Must be called while the link is connected.** The mapping
* is kept in a Host-side connection table that is registered at
* connection complete and cleared on disconnect. If there is no
* active link for `pseudo`, or the local identity is not yet
* finalized (`local_ready`), the call returns `ESP_FAIL` and
* `identity` is not modified.
*
* For offline bond information (no connection), use
* `esp_ble_get_bond_device_list()` and read
* `bond_key.pid_key.static_addr` for the real peer identity.
* The bond list key is the stored pseudo address; local identity
* is not exposed by this API offline.
*
* @param[in] pseudo - the pseudo address as seen in remote_bda
* @param[out] identity - filled with the peer/local identity on success
*
* @return - ESP_OK : success (link connected and pseudo known)
* - ESP_FAIL : Bluedroid not enabled, or pseudo not found /
* not connected / local identity not yet ready
* - ESP_ERR_INVALID_ARG : NULL pointer argument
*/
esp_err_t esp_ble_gap_get_conn_identity(esp_bd_addr_t pseudo, esp_ble_conn_identity_t *identity);
/**
* @brief Remove the stored bond for one specific (local, peer)
* identity pair. The pseudo bond section is recomputed from the
* identity, so this only deletes that one local identity's bond
* and never affects the same phone's other local identity.
*
* @param[in] local_addr - local identity used when bonding
* @param[in] local_addr_type - local identity address type
* @param[in] peer_addr - real peer identity
* @param[in] peer_addr_type - peer identity address type
*
* @return - ESP_OK : request accepted
* - other : invalid arguments / not enabled
*/
esp_err_t esp_ble_gap_remove_bond_for_identity(esp_bd_addr_t local_addr,
esp_ble_addr_type_t local_addr_type,
esp_bd_addr_t peer_addr,
esp_ble_addr_type_t peer_addr_type);
#endif // CONFIG_BT_BLE_PERIPH_PSEUDO_ADDR_BOND
/**
* @brief This function is called to get ADV data for a specific type.
*