Skip to content

Commit

Permalink
[BT] Make BLE cfg connection more reliable and faster
Browse files Browse the repository at this point in the history
  • Loading branch information
darthcloud committed Sep 19, 2024
1 parent 0daa3aa commit 2b30fdf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
55 changes: 49 additions & 6 deletions main/bluetooth/att_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ enum {
GAP_DEV_NAME_CHRC_HDL,
GAP_APP_ATT_HDL,
GAP_APP_CHRC_HDL,
GAP_PPCP_ATT_HDL,
GAP_PPCP_CHRC_HDL,
BR_GRP_HDL = 0x0040,
BR_GLBL_CFG_ATT_HDL,
BR_GLBL_CFG_CHRC_HDL,
Expand Down Expand Up @@ -115,6 +117,7 @@ static void bt_att_cmd_gap_char_read_type_rsp(uint16_t handle) {
struct bt_att_read_type_rsp *rd_type_rsp = (struct bt_att_read_type_rsp *)bt_hci_pkt_tmp.att_data;
struct bt_att_data *name_data = (struct bt_att_data *)((uint8_t *)rd_type_rsp->data + 0);
struct bt_att_data *app_data = (struct bt_att_data *)((uint8_t *)rd_type_rsp->data + 7);
struct bt_att_data *ppcp_data = (struct bt_att_data *)((uint8_t *)rd_type_rsp->data + 14);
uint8_t *data;

printf("# %s\n", __FUNCTION__);
Expand All @@ -137,7 +140,15 @@ static void bt_att_cmd_gap_char_read_type_rsp(uint16_t handle) {
data += 2;
*(uint16_t *)data = BT_UUID_GAP_APPEARANCE;

bt_att_cmd(handle, BT_ATT_OP_READ_TYPE_RSP, sizeof(rd_type_rsp->len) + rd_type_rsp->len * 2);
ppcp_data->handle = GAP_PPCP_ATT_HDL;
data = ppcp_data->value;
*data = BT_GATT_CHRC_READ;
data++;
*(uint16_t *)data = GAP_PPCP_CHRC_HDL;
data += 2;
*(uint16_t *)data = BT_UUID_GAP_PPCP;

bt_att_cmd(handle, BT_ATT_OP_READ_TYPE_RSP, sizeof(rd_type_rsp->len) + rd_type_rsp->len * 3);
}

static void bt_att_cmd_blueretro_char_read_type_rsp(uint16_t handle, uint16_t start) {
Expand Down Expand Up @@ -197,6 +208,20 @@ static void bt_att_cmd_app_rd_rsp(uint16_t handle) {
bt_att_cmd(handle, BT_ATT_OP_READ_RSP, sizeof(uint16_t));
}

static void bt_att_cmd_ppcp_rd_rsp(uint16_t handle) {
struct bt_l2cap_conn_param_req *conn_param =
(struct bt_l2cap_conn_param_req *)bt_hci_pkt_tmp.att_data;

printf("# %s\n", __FUNCTION__);

conn_param->min_interval = 6;
conn_param->max_interval = 12;
conn_param->latency = 0;
conn_param->timeout = 960;

bt_att_cmd(handle, BT_ATT_OP_READ_RSP, sizeof(struct bt_l2cap_conn_param_req));
}

static void bt_att_cmd_global_cfg_rd_rsp(uint16_t handle, uint16_t offset) {
uint32_t len = 0;
printf("# %s\n", __FUNCTION__);
Expand Down Expand Up @@ -360,7 +385,7 @@ static void bt_att_cmd_read_group_rsp(uint16_t handle, uint16_t start, uint16_t

printf("# %s\n", __FUNCTION__);

if (start <= GAP_APP_CHRC_HDL) {
if (start <= GAP_PPCP_CHRC_HDL) {
rd_grp_rsp->len = 6;

if (start <= GATT_GRP_HDL && end >= GATT_SRVC_CH_CHRC_HDL) {
Expand All @@ -370,9 +395,9 @@ static void bt_att_cmd_read_group_rsp(uint16_t handle, uint16_t start, uint16_t
len += rd_grp_rsp->len;
}

if (start <= GAP_GRP_HDL && end >= GAP_APP_CHRC_HDL) {
if (start <= GAP_GRP_HDL && end >= GAP_PPCP_CHRC_HDL) {
gap_data->start_handle = GAP_GRP_HDL;
gap_data->end_handle = GAP_APP_CHRC_HDL;
gap_data->end_handle = GAP_PPCP_CHRC_HDL;
*(uint16_t *)gap_data->value = BT_UUID_GAP;
len += rd_grp_rsp->len;
}
Expand Down Expand Up @@ -558,11 +583,11 @@ void bt_att_cfg_hdlr(struct bt_dev *device, struct bt_hci_pkt *bt_hci_acl_pkt, u
bt_att_cmd_gatt_char_read_type_rsp(device->acl_handle);
}
/* GAP */
else if (start >= GATT_SRVC_CH_CHRC_HDL && start < GAP_APP_CHRC_HDL && end >= GAP_APP_CHRC_HDL) {
else if (start >= GATT_SRVC_CH_CHRC_HDL && start < GAP_PPCP_CHRC_HDL && end >= GAP_PPCP_CHRC_HDL) {
bt_att_cmd_gap_char_read_type_rsp(device->acl_handle);
}
/* BLUERETRO */
else if (start >= GAP_APP_CHRC_HDL && start < LAST_HDL && end >= LAST_HDL) {
else if (start >= GAP_PPCP_CHRC_HDL && start < LAST_HDL && end >= LAST_HDL) {
bt_att_cmd_blueretro_char_read_type_rsp(device->acl_handle, start);
}
else {
Expand All @@ -586,6 +611,9 @@ void bt_att_cfg_hdlr(struct bt_dev *device, struct bt_hci_pkt *bt_hci_acl_pkt, u
case GAP_APP_CHRC_HDL:
bt_att_cmd_app_rd_rsp(device->acl_handle);
break;
case GAP_PPCP_CHRC_HDL:
bt_att_cmd_ppcp_rd_rsp(device->acl_handle);
break;
case BR_GLBL_CFG_CHRC_HDL:
bt_att_cmd_global_cfg_rd_rsp(device->acl_handle, 0);
break;
Expand All @@ -608,6 +636,21 @@ void bt_att_cfg_hdlr(struct bt_dev *device, struct bt_hci_pkt *bt_hci_acl_pkt, u
bt_att_cfg_cmd_bdaddr_rsp(device->acl_handle);
break;
case BR_CFG_CMD_CHRC_HDL:
if (!atomic_test_bit(&device->flags, BT_DEV_PPCP_DONE)) {
struct hci_cp_le_conn_update le_conn_update = {
.conn_interval_min = 6,
.conn_interval_max = 12,
.conn_latency = 0,
.supervision_timeout = 960,
.min_ce_len = 0,
.max_ce_len = 0,
};

le_conn_update.handle = device->acl_handle;

bt_hci_le_conn_update(&le_conn_update);
atomic_set_bit(&device->flags, BT_DEV_PPCP_DONE);
}
bt_att_cfg_cmd_rd_hdlr(device->acl_handle);
break;
default:
Expand Down
11 changes: 11 additions & 0 deletions main/bluetooth/hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,17 @@ static void bt_hci_le_meta_evt_hdlr(struct bt_hci_pkt *bt_hci_evt_pkt) {
skip:
break;
}
case BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE:
{
struct bt_hci_evt_le_conn_update_complete *conn =
(struct bt_hci_evt_le_conn_update_complete *)(bt_hci_evt_pkt->evt_data + sizeof(struct bt_hci_evt_le_meta_event));
printf("# BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE sts: %d, int: %d, lat: %d, tout: %d\n",
conn->status, conn->interval, conn->latency, conn->supv_timeout);
if (device) {
atomic_set_bit(&device->flags, BT_DEV_PPCP_DONE);
}
break;
}
case BT_HCI_EV_LE_REMOTE_FEAT_COMPLETE:
{
struct bt_hci_evt_le_remote_feat_complete *le_remote_feat =
Expand Down
1 change: 1 addition & 0 deletions main/bluetooth/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum {
BT_DEV_IS_BLE,
BT_DEV_FB_DELAY,
BT_DEV_CALIB_SET,
BT_DEV_PPCP_DONE,
};

enum {
Expand Down

0 comments on commit 2b30fdf

Please sign in to comment.