Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_ble_max_attribute_value_v5.0' into 'release/…
Browse files Browse the repository at this point in the history
…v5.0'

Bugfix/fix ble max attribute value v5.0

See merge request espressif/esp-idf!30414
  • Loading branch information
Isl2017 committed Apr 28, 2024
2 parents 3087b28 + 614b2bb commit 2233d54
Show file tree
Hide file tree
Showing 25 changed files with 1,040 additions and 416 deletions.
63 changes: 62 additions & 1 deletion components/bt/host/bluedroid/api/esp_gap_ble_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "btc/btc_manage.h"
#include "btc_gap_ble.h"
#include "btc/btc_ble_storage.h"

#include "esp_random.h"

esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback)
{
Expand Down Expand Up @@ -188,6 +188,25 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_gap_addr_create_static(esp_bd_addr_t rand_addr)
{
// Static device address: First two bits are '11', rest is random
rand_addr[0] = 0xC0 | (esp_random() & 0x3F);
for (int i = 1; i < 6; i++) {
rand_addr[i] = esp_random() & 0xFF; // Randomize remaining bits
}
return ESP_OK;
}

esp_err_t esp_ble_gap_addr_create_nrpa(esp_bd_addr_t rand_addr)
{
// Non-resolvable private address: First two bits are '00', rest is random
rand_addr[0] = (esp_random() & 0x3F);
for (int i = 1; i < 6; i++) {
rand_addr[i] = esp_random() & 0xFF; // Randomize remaining bits
}
return ESP_OK;
}

esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr)
{
Expand All @@ -204,6 +223,48 @@ esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr)
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_gap_set_resolvable_private_address_timeout(uint16_t rpa_timeout)
{
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

if (rpa_timeout < 0x0001 || rpa_timeout > 0x0E10) {
return ESP_ERR_INVALID_ARG;
}

btc_msg_t msg = {0};
btc_ble_gap_args_t arg;

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT;
arg.set_rpa_timeout.rpa_timeout = rpa_timeout;

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}


esp_err_t esp_ble_gap_add_device_to_resolving_list(esp_bd_addr_t peer_addr, uint8_t addr_type, uint8_t *peer_irk)
{
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

if (addr_type > BLE_ADDR_TYPE_RANDOM ||!peer_addr || (addr_type && ((peer_addr[0] & 0xC0) != 0xC0))) {
return ESP_ERR_INVALID_ARG;
}

btc_msg_t msg = {0};
btc_ble_gap_args_t arg;

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_ACT_ADD_DEVICE_TO_RESOLVING_LIST;

memcpy(arg.add_dev_to_resolving_list.addr, peer_addr, ESP_BD_ADDR_LEN);
arg.add_dev_to_resolving_list.addr_type = addr_type;
memcpy(arg.add_dev_to_resolving_list.irk, peer_irk, ESP_PEER_IRK_LEN);

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_gap_clear_rand_addr(void)
{
btc_msg_t msg;
Expand Down
7 changes: 5 additions & 2 deletions components/bt/host/bluedroid/api/include/api/esp_bt_defs.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 @@ -19,7 +19,7 @@ extern "C" {
return ESP_ERR_INVALID_STATE; \
}

#define ESP_BT_STATUS_BASE_FOR_HCI_ERR 0X0100 /* base for coverting HCI error code to ESP status */
#define ESP_BT_STATUS_BASE_FOR_HCI_ERR 0X0100 /* base for converting HCI error code to ESP status */

/* relate to BT_STATUS_xxx in bt_def.h */
/// Status Return Value
Expand Down Expand Up @@ -163,6 +163,9 @@ typedef enum {
/// Bluetooth address length
#define ESP_BD_ADDR_LEN 6

/// Bluetooth peer irk
#define ESP_PEER_IRK_LEN 16

/// Bluetooth device address
typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN];

Expand Down
96 changes: 81 additions & 15 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 @@ -157,7 +157,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 @@ -225,7 +225,9 @@ typedef enum {
ESP_GAP_BLE_DTM_TEST_UPDATE_EVT, /*!< when direct test mode state changes, the event comes */
// BLE_INCLUDED
ESP_GAP_BLE_ADV_CLEAR_COMPLETE_EVT, /*!< When clear advertising complete, the event comes */
ESP_GAP_BLE_VENDOR_CMD_COMPLETE_EVT, /*!< When vendor hci command complete, the event comes */
ESP_GAP_BLE_SET_RPA_TIMEOUT_COMPLETE_EVT, /*!< When set the Resolvable Private Address (RPA) timeout completes, the event comes */
ESP_GAP_BLE_ADD_DEV_TO_RESOLVING_LIST_COMPLETE_EVT, /*!< when add a device to the resolving list completes, the event comes*/
ESP_GAP_BLE_VENDOR_CMD_COMPLETE_EVT, /*!< When vendor hci command complete, the event comes */
ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */
} esp_gap_ble_cb_event_t;

Expand Down Expand Up @@ -799,9 +801,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 @@ -1153,6 +1155,19 @@ typedef union {
struct ble_local_privacy_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicate the set local privacy operation success status */
} local_privacy_cmpl; /*!< Event parameter of ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_SET_RPA_TIMEOUT_COMPLETE_EVT
*/
struct ble_rpa_timeout_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicate the set RPA timeout operation success status */
} set_rpa_timeout_cmpl; /*!< Event parameter of ESP_GAP_BLE_SET_RPA_TIMEOUT_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_ADD_DEV_TO_RESOLVING_LIST_COMPLETE_EVT
*/
struct ble_add_dev_to_resolving_list_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicates the success status of adding a device to the resolving list */
} add_dev_to_resolving_list_cmpl; /*!< Event parameter of ESP_GAP_BLE_ADD_DEV_TO_RESOLVING_LIST_COMPLETE_EVT */

/**
* @brief ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT
*/
Expand Down Expand Up @@ -1629,13 +1644,13 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_
*
* @param[in] rand_addr: The address to be configured. Refer to the table below for possible address subtypes:
*
* | address [47:46] | Address Type |
* |-----------------|--------------------------|
* | 0b00 | Non-Resolvable Private |
* | | Address |
* |-----------------|--------------------------|
* | 0b11 | Static Random Address |
* |-----------------|--------------------------|
* | address [47:46] | Address Type | Corresponding API |
* |-----------------|-----------------------------|----------------------------------------|
* | 0b00 | Non-Resolvable Private | esp_ble_gap_addr_create_nrpa |
* | | Address (NRPA) | |
* |-----------------|-----------------------------|----------------------------------------|
* | 0b11 | Static Random Address | esp_ble_gap_addr_create_static |
* |-----------------|-----------------------------|----------------------------------------|
*
* @return
* - ESP_OK : success
Expand All @@ -1645,16 +1660,68 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_
esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr);

/**
* @brief This function clears the random address for the application
* @brief Create a static device address
* @param[out] rand_addr: Pointer to the buffer where the static device address will be stored.
* @return - ESP_OK : Success
* - Other : Failed
*/
esp_err_t esp_ble_gap_addr_create_static(esp_bd_addr_t rand_addr);

/**
* @brief Create a non-resolvable private address (NRPA)
* @param[out] rand_addr: Pointer to the buffer where the NRPA will be stored.
* @return - ESP_OK : Success
* - Other : Failed
*/
esp_err_t esp_ble_gap_addr_create_nrpa(esp_bd_addr_t rand_addr);

/**
* @brief This function sets the length of time the Controller uses a Resolvable Private Address
* before generating and starting to use a new resolvable private address.
*
* @note Note: This function is currently not supported on the ESP32 but will be enabled in a future update.
*
* @param[in] rpa_timeout: The timeout duration in seconds for how long a Resolvable Private Address
* is used before a new one is generated. The value must be within the range specified by
* the Bluetooth specification (0x0001 to 0x0E10), which corresponds to a time range of
* 1 second to 1 hour. The default value is 0x0384 (900 seconds or 15 minutes).
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gap_clear_rand_addr(void);
esp_err_t esp_ble_gap_set_resolvable_private_address_timeout(uint16_t rpa_timeout);


/**
* @brief This function adds a device to the resolving list used to generate and resolve Resolvable Private Addresses
* in the Controller.
*
* @note Note: This function shall not be used when address resolution is enabled in the Controller and:
* - Advertising (other than periodic advertising) is enabled,
* - Scanning is enabled, or
* - an HCI_LE_Create_Connection, HCI_LE_Extended_Create_Connection, or HCI_LE_Periodic_Advertising_Create_Sync command is pending.
* This command may be used at any time when address resolution is disabled in the Controller.
* The added device shall be set to Network Privacy mode.
*
* @param[in] peer_addr: The peer identity address of the device to be added to the resolving list.
* @param[in] addr_type: The address type of the peer identity address (BLE_ADDR_TYPE_PUBLIC or BLE_ADDR_TYPE_RANDOM).
* @param[in] peer_irk: The Identity Resolving Key (IRK) of the device.
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gap_add_device_to_resolving_list(esp_bd_addr_t peer_addr, uint8_t addr_type, uint8_t *peer_irk);
/**
* @brief This function clears the random address for the application
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gap_clear_rand_addr(void);

/**
* @brief Enable/disable privacy (including address resolution) on the local device
Expand Down Expand Up @@ -1970,7 +2037,6 @@ esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr);
*/
int esp_ble_get_bond_device_num(void);


/**
* @brief Get the device from the security database list of peer device.
* It will return the device bonded information immediately.
Expand Down
Loading

0 comments on commit 2233d54

Please sign in to comment.