Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_some_ble_bugs_by_cjh_v4.4' into 'release/v4.4'
Browse files Browse the repository at this point in the history
Fixed some BLE bugs (backport v4.4)

See merge request espressif/esp-idf!21691
  • Loading branch information
jack0c committed Feb 3, 2023
2 parents 202e1c1 + f44f3d1 commit b818abb
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 48 deletions.
4 changes: 4 additions & 0 deletions components/bt/host/bluedroid/api/esp_gap_ble_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ esp_err_t esp_ble_gap_set_security_param(esp_ble_sm_param_t param_type,
return ESP_ERR_INVALID_ARG;
}
}
if (param_type == ESP_BLE_APP_ENC_KEY_SIZE) {
LOG_ERROR("ESP_BLE_APP_ENC_KEY_SIZE is deprecated, use ESP_GATT_PERM_ENCRYPT_KEY_SIZE in characteristic definition");
return ESP_ERR_NOT_SUPPORTED;
}

btc_msg_t msg = {0};
btc_ble_gap_args_t arg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ typedef enum {
#define ESP_GATT_PERM_WRITE_SIGNED_MITM (1 << 8) /* bit 8 - 0x0100 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_AUTHORIZATION (1 << 9) /* bit 9 - 0x0200 */
#define ESP_GATT_PERM_WRITE_AUTHORIZATION (1 << 10) /* bit 10 - 0x0400 */
#define ESP_GATT_PERM_ENCRYPT_KEY_SIZE(keysize) (((keysize - 6) & 0xF) << 12) /* bit 12:15 - 0xF000 */
typedef uint16_t esp_gatt_perm_t;

/* relate to BTA_GATT_CHAR_PROP_BIT_xxx in bta/bta_gatt_api.h */
Expand Down
12 changes: 0 additions & 12 deletions components/bt/host/bluedroid/bta/dm/bta_dm_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ tBTE_APPL_CFG bte_appl_cfg = {
BTM_BLE_MIN_KEY_SIZE,
BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE,
BTM_BLE_OOB_DISABLE,
BTM_BLE_APPL_ENC_KEY_SIZE,
};
#endif

Expand Down Expand Up @@ -428,17 +427,6 @@ void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size)
#endif ///SMP_INCLUDED == TRUE
}

void bta_dm_co_ble_set_appl_enc_key_size(UINT8 ble_key_size)
{
#if (SMP_INCLUDED == TRUE)
if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= bte_appl_cfg.ble_max_key_size) {
bte_appl_cfg.ble_appl_enc_key_size = ble_key_size;
} else {
APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
}
#endif ///SMP_INCLUDED == TRUE
}

void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)
{
#if (SMP_INCLUDED == TRUE)
Expand Down
2 changes: 0 additions & 2 deletions components/bt/host/bluedroid/bta/include/bta/bta_dm_co.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,4 @@ extern UINT8 bta_dm_co_ble_get_accept_auth_enable(void);
extern UINT8 bta_dm_co_ble_get_auth_req(void);

extern void bta_dm_co_ble_oob_support(UINT8 enable);

extern void bta_dm_co_ble_set_appl_enc_key_size(UINT8 ble_key_size);
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -1651,12 +1651,6 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
bta_dm_co_ble_oob_support(enable);
break;
}
case ESP_BLE_APP_ENC_KEY_SIZE: {
uint8_t key_size = 0;
STREAM_TO_UINT8(key_size, value);
bta_dm_co_ble_set_appl_enc_key_size(key_size);
break;
}
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ typedef struct {
UINT8 ble_min_key_size;
UINT8 ble_accept_auth_enable;
UINT8 oob_support;
UINT8 ble_appl_enc_key_size;
#endif

} tBTE_APPL_CFG;
Expand Down
4 changes: 3 additions & 1 deletion components/bt/host/bluedroid/stack/gatt/att_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ BT_HDR *attp_build_browse_cmd(UINT8 op_code, UINT16 s_hdl, UINT16 e_hdl, tBT_UUI
{
BT_HDR *p_buf = NULL;
UINT8 *p;
/* length of ATT_READ_BY_TYPE_REQ PDU: opcode(1) + start_handle (2) + end_handle (2) + uuid (2 or 16) */
const UINT8 payload_size = 1 + 2 + 2 + ((uuid.len == LEN_UUID_16) ? LEN_UUID_16 : LEN_UUID_128);

if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + 8 + L2CAP_MIN_OFFSET)) != NULL) {
if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR) + payload_size + L2CAP_MIN_OFFSET)) != NULL) {
p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
/* Describe the built message location and size */
p_buf->offset = L2CAP_MIN_OFFSET;
Expand Down
9 changes: 0 additions & 9 deletions components/bt/host/bluedroid/stack/gatt/gatt_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "gatt_int.h"
#include "stack/l2c_api.h"
#include "btm_int.h"
#include "common/bte_appl.h"

/********************************************************************************
** L O C A L F U N C T I O N P R O T O T Y P E S *
Expand Down Expand Up @@ -125,14 +124,10 @@ static tGATT_STATUS gatts_check_attr_readability(tGATT_ATTR16 *p_attr,
tGATT_PERM perm = p_attr->permission;

UNUSED(offset);
#if SMP_INCLUDED == TRUE
min_key_size = bte_appl_cfg.ble_appl_enc_key_size;
#else
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
if (min_key_size != 0 ) {
min_key_size += 6;
}
#endif

if (!(perm & GATT_READ_ALLOWED)) {
GATT_TRACE_ERROR( "GATT_READ_NOT_PERMIT\n");
Expand Down Expand Up @@ -1077,14 +1072,10 @@ tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code,
while (p_attr != NULL) {
if (p_attr->handle == handle) {
perm = p_attr->permission;
#if SMP_INCLUDED == TRUE
min_key_size = bte_appl_cfg.ble_appl_enc_key_size;
#else
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
if (min_key_size != 0 ) {
min_key_size += 6;
}
#endif
GATT_TRACE_DEBUG( "gatts_write_attr_perm_check p_attr->permission =0x%04x min_key_size==0x%04x",
p_attr->permission,
min_key_size);
Expand Down
1 change: 0 additions & 1 deletion components/bt/host/bluedroid/stack/include/stack/btm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,6 @@ typedef UINT8 tBTM_IO_CAP;
#define BTM_BLE_RESPONDER_KEY_SIZE 15
#define BTM_BLE_MAX_KEY_SIZE 16
#define BTM_BLE_MIN_KEY_SIZE 7
#define BTM_BLE_APPL_ENC_KEY_SIZE 7

typedef UINT8 tBTM_AUTH_REQ;

Expand Down
34 changes: 23 additions & 11 deletions components/bt/host/bluedroid/stack/l2cap/l2c_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_in
{
tL2C_LCB *p_lcb;
tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
UINT8 status = HCI_SUCCESS;
BOOLEAN need_cb = false;

/* See if we have a link control block for the remote device */
p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
Expand All @@ -135,19 +137,29 @@ BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_in
return (FALSE);
}

/* Check whether the request conn params is already set */
if ((max_int == p_lcb->current_used_conn_interval) && (latency == p_lcb->current_used_conn_latency) &&
(timeout == p_lcb->current_used_conn_timeout)) {
status = HCI_SUCCESS;
need_cb = true;
L2CAP_TRACE_WARNING("%s connection parameter already set", __func__);
}

if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL){
UINT8 status = HCI_ERR_ILLEGAL_COMMAND;
status = HCI_ERR_ILLEGAL_COMMAND;
need_cb = true;
L2CAP_TRACE_ERROR("There are two connection parameter requests that are being updated, please try later ");
if (conn_param_update_cb.update_conn_param_cb != NULL) {
tBTM_LE_UPDATE_CONN_PRAMS update_param;
update_param.max_conn_int = max_int;
update_param.min_conn_int = min_int;
update_param.conn_int = p_lcb->current_used_conn_interval;
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;
(conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
}
return (FALSE);
}

if ((need_cb == TRUE) && (conn_param_update_cb.update_conn_param_cb != NULL)) {
tBTM_LE_UPDATE_CONN_PRAMS update_param;
update_param.max_conn_int = max_int;
update_param.min_conn_int = min_int;
update_param.conn_int = p_lcb->current_used_conn_interval;
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;
(conn_param_update_cb.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
return (status == HCI_SUCCESS);
}

p_lcb->waiting_update_conn_min_interval = min_int;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *
case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: {
ESP_LOGI(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__);
ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, param->vendor_write.length);
break;
}
case ESP_HIDD_EVENT_BLE_LED_REPORT_WRITE_EVT: {
ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_LED_REPORT_WRITE_EVT");
ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->led_write.data, param->led_write.length);
break;
}
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {
ESP_HIDD_EVENT_BLE_CONNECT,
ESP_HIDD_EVENT_BLE_DISCONNECT,
ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT,
ESP_HIDD_EVENT_BLE_LED_REPORT_WRITE_EVT,
} esp_hidd_cb_event_t;

/// HID config status
Expand Down Expand Up @@ -104,6 +105,15 @@ typedef union {
uint8_t *data; /*!< The pointer to the data */
} vendor_write; /*!< HID callback param of ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT */

/**
* @brief ESP_HIDD_EVENT_BLE_LED_REPORT_WRITE_EVT
*/
struct hidd_led_write_evt_param {
uint16_t conn_id;
uint8_t report_id;
uint8_t length;
uint8_t *data;
} led_write;
} esp_hidd_cb_param_t;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ static const uint8_t hidReportMap[] = {
0x81, 0x01, // Input: (Constant)
//
// LED report
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x05, 0x08, // Usage Pg (LEDs)
0x19, 0x01, // Usage Min (1)
0x29, 0x05, // Usage Max (5)
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x91, 0x02, // Output: (Data, Variable, Absolute)
//
// LED report padding
Expand Down Expand Up @@ -291,6 +291,7 @@ static const uint8_t char_prop_write_nr = ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_READ;
static const uint8_t char_prop_read_notify = ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_NOTIFY;
static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_NOTIFY;
static const uint8_t char_prop_read_write_write_nr = ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_WRITE_NR;

/// battary Service
static const uint16_t battary_svc = ESP_GATT_UUID_BATTERY_SERVICE_SVC;
Expand Down Expand Up @@ -433,7 +434,7 @@ static esp_gatts_attr_db_t hidd_le_gatt_db[HIDD_LE_IDX_NB] =
[HIDD_LE_IDX_REPORT_LED_OUT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid,
ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE,
(uint8_t *)&char_prop_read_write}},
(uint8_t *)&char_prop_read_write_write_nr}},

[HIDD_LE_IDX_REPORT_LED_OUT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid,
ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
Expand Down Expand Up @@ -593,8 +594,15 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
case ESP_GATTS_CLOSE_EVT:
break;
case ESP_GATTS_WRITE_EVT: {
#if (SUPPORT_REPORT_VENDOR == true)
esp_hidd_cb_param_t cb_param = {0};
if (param->write.handle == hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_LED_OUT_VAL]) {
cb_param.led_write.conn_id = param->write.conn_id;
cb_param.led_write.report_id = HID_RPT_ID_LED_OUT;
cb_param.led_write.length = param->write.len;
cb_param.led_write.data = param->write.value;
(hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_BLE_LED_REPORT_WRITE_EVT, &cb_param);
}
#if (SUPPORT_REPORT_VENDOR == true)
if (param->write.handle == hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL] &&
hidd_le_env.hidd_cb != NULL) {
cb_param.vendor_write.conn_id = param->write.conn_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define HID_RPT_ID_KEY_IN 2 // Keyboard input report ID
#define HID_RPT_ID_CC_IN 3 //Consumer Control input report ID
#define HID_RPT_ID_VENDOR_OUT 4 // Vendor output report ID
#define HID_RPT_ID_LED_OUT 0 // LED output report ID
#define HID_RPT_ID_LED_OUT 2 // LED output report ID
#define HID_RPT_ID_FEATURE 0 // Feature report ID

#define HIDD_APP_ID 0x1812//ATT_SVC_HID
Expand Down

0 comments on commit b818abb

Please sign in to comment.