Skip to content

Commit

Permalink
fix(bt/bluedroid): Fix BLE robost cashing config
Browse files Browse the repository at this point in the history
  • Loading branch information
esp-zhp committed Dec 5, 2023
1 parent 345013f commit 41facc2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
5 changes: 4 additions & 1 deletion components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ config BT_GATTS_ROBUST_CACHING_ENABLED
depends on BT_GATTS_ENABLE
default n
help
This option enable gatt robust caching feature on server
This option enables the GATT robust caching feature on the server.
if turned on, the Client Supported Features characteristic, Database Hash characteristic,
and Server Supported Features characteristic will be included in the GAP SERVICE.


config BT_GATTS_DEVICE_NAME_WRITABLE
bool "Allow to write device name by GATT clients"
Expand Down
9 changes: 8 additions & 1 deletion components/bt/host/bluedroid/stack/gatt/gatt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ BOOLEAN GATTS_NVRegister (const tGATT_APPL_INFO *p_cb_info)
return status;
}

#if GATTS_ROBUST_CACHING_ENABLED
static void gatt_update_for_database_change(void)
{
UINT8 i;
Expand All @@ -135,7 +136,7 @@ static void gatt_update_for_database_change(void)
}
}
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
/*******************************************************************************
**
** Function GATTS_CreateService
Expand Down Expand Up @@ -414,7 +415,9 @@ BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, UINT16 svc_
GATT_TRACE_DEBUG ("Delete a new service changed item - the service has not yet started");
osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
} else {
#if GATTS_ROBUST_CACHING_ENABLED
gatt_update_for_database_change();
#endif /* GATTS_ROBUST_CACHING_ENABLED */
if (gatt_cb.srv_chg_mode == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}
Expand Down Expand Up @@ -527,7 +530,11 @@ tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle,
if ( (p_buf = gatt_sr_is_new_srv_chg(&p_list->asgn_range.app_uuid128,
&p_list->asgn_range.svc_uuid,
p_list->asgn_range.svc_inst)) != NULL) {

#if GATTS_ROBUST_CACHING_ENABLED
gatt_update_for_database_change();
#endif /* GATTS_ROBUST_CACHING_ENABLED */

if (gatt_cb.srv_chg_mode == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}
Expand Down
35 changes: 19 additions & 16 deletions components/bt/host/bluedroid/stack/gatt/gatt_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@
#define BLE_GATT_CL_SUPP_FEAT_BITMASK 0x07

#define GATTP_MAX_NUM_INC_SVR 0

#if GATTS_ROBUST_CACHING_ENABLED
#define GATTP_MAX_CHAR_NUM 5
#else
#define GATTP_MAX_CHAR_NUM 2
#endif /* GATTS_ROBUST_CACHING_ENABLED */

#define GATTP_MAX_ATTR_NUM (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
#define GATTP_MAX_CHAR_VALUE_SIZE 50

Expand Down Expand Up @@ -196,14 +202,15 @@ tGATT_STATUS gatt_proc_read (UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_RE

GATT_TRACE_DEBUG("%s handle %x", __func__, p_data->handle);

UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *tcb = gatt_get_tcb_by_idx(tcb_idx);

if (p_data->is_long) {
p_rsp->attr_value.offset = p_data->offset;
}

p_rsp->attr_value.handle = p_data->handle;
#if GATTS_ROBUST_CACHING_ENABLED

UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *tcb = gatt_get_tcb_by_idx(tcb_idx);

/* handle request for reading client supported features */
if (p_data->handle == gatt_cb.handle_of_cl_supported_feat) {
Expand All @@ -229,7 +236,7 @@ tGATT_STATUS gatt_proc_read (UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_RE
memcpy(p_rsp->attr_value.value, &gatt_cb.gatt_sr_supported_feat_mask, 1);
return GATT_SUCCESS;
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
/* handle request for reading service changed des and the others */
status = GATTS_GetAttributeValue(p_data->handle, &len, &value);
if(status == GATT_SUCCESS && len > 0 && value) {
Expand All @@ -241,7 +248,7 @@ tGATT_STATUS gatt_proc_read (UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_RE
}
return status;
}

#if GATTS_ROBUST_CACHING_ENABLED
static tGATT_STATUS gatt_sr_write_cl_supp_feat(UINT16 conn_id, tGATT_WRITE_REQ *p_data)
{
UINT8 val_new;
Expand Down Expand Up @@ -286,7 +293,7 @@ static tGATT_STATUS gatt_sr_write_cl_supp_feat(UINT16 conn_id, tGATT_WRITE_REQ *
#endif
return GATT_SUCCESS;
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
/******************************************************************************
**
** Function gatt_proc_write_req
Expand All @@ -301,7 +308,7 @@ tGATT_STATUS gatt_proc_write_req(UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_WRI
if(p_data->len > GATT_MAX_ATTR_LEN) {
p_data->len = GATT_MAX_ATTR_LEN;
}

#if GATTS_ROBUST_CACHING_ENABLED
if (p_data->handle == gatt_cb.handle_of_h_r) {
return GATT_WRITE_NOT_PERMIT;
}
Expand All @@ -317,7 +324,7 @@ tGATT_STATUS gatt_proc_write_req(UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_WRI
if (p_data->handle == gatt_cb.handle_of_sr_supported_feat) {
return GATT_WRITE_NOT_PERMIT;
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
return GATTS_SetAttributeValue(p_data->handle,
p_data->len,
p_data->value);
Expand Down Expand Up @@ -470,7 +477,7 @@ void gatt_profile_db_init (void)
};

GATTS_AddCharDescriptor (service_handle, GATT_PERM_READ | GATT_PERM_WRITE , &descr_uuid, &attr_val, NULL);

#if GATTS_ROBUST_CACHING_ENABLED
/* add Client Supported Features characteristic */
uuid.uu.uuid16 = GATT_UUID_CLIENT_SUP_FEAT;
gatt_cb.handle_of_cl_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ | GATT_PERM_WRITE,
Expand All @@ -483,7 +490,7 @@ void gatt_profile_db_init (void)
/* add Server Supported Features characteristic */
uuid.uu.uuid16 = GATT_UUID_SERVER_SUP_FEAT;
gatt_cb.handle_of_sr_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ, NULL, NULL);

#endif /* GATTS_ROBUST_CACHING_ENABLED */
/* start service */
status = GATTS_StartService (gatt_cb.gatt_if, service_handle, GATTP_TRANSPORT_SUPPORTED );

Expand Down Expand Up @@ -689,6 +696,7 @@ void GATT_ConfigServiceChangeCCC (BD_ADDR remote_bda, BOOLEAN enable, tBT_TRANSP
gatt_cl_start_config_ccc(p_clcb);
}

#if GATTS_ROBUST_CACHING_ENABLED
/*******************************************************************************
**
** Function gatt_sr_is_cl_robust_caching_supported
Expand All @@ -700,14 +708,8 @@ void GATT_ConfigServiceChangeCCC (BD_ADDR remote_bda, BOOLEAN enable, tBT_TRANSP
*******************************************************************************/
static BOOLEAN gatt_sr_is_cl_robust_caching_supported(tGATT_TCB *p_tcb)
{
// Server robust caching not enabled
if (!GATTS_ROBUST_CACHING_ENABLED) {
return FALSE;
}

return (p_tcb->cl_supp_feat & BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK);
}

/*******************************************************************************
**
** Function gatt_sr_is_cl_change_aware
Expand Down Expand Up @@ -791,4 +793,5 @@ void gatt_sr_update_cl_status(tGATT_TCB *p_tcb, BOOLEAN chg_aware)

GATT_TRACE_DEBUG("%s status %d", __func__, chg_aware);
}
#endif /* GATTS_ROBUST_CACHING_ENABLED */
#endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
8 changes: 6 additions & 2 deletions components/bt/host/bluedroid/stack/gatt/gatt_sr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,9 +1680,10 @@ static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
gatts_proc_srv_chg_ind_ack(p_tcb);
/* there is no need to inform the application since srv chg is handled internally by GATT */
continue_processing = FALSE;

#if GATTS_ROBUST_CACHING_ENABLED
/* after receiving ack of svc_chg_ind, reset client status */
gatt_sr_update_cl_status(p_tcb, true);
#endif /* GATTS_ROBUST_CACHING_ENABLED */
}

gatts_chk_pending_ind(p_tcb);
Expand Down Expand Up @@ -1729,6 +1730,7 @@ void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
}
}

#if GATTS_ROBUST_CACHING_ENABLED
static BOOLEAN gatts_handle_db_out_of_sync(tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
Expand Down Expand Up @@ -1808,6 +1810,7 @@ static BOOLEAN gatts_handle_db_out_of_sync(tGATT_TCB *p_tcb, UINT8 op_code,
return should_ignore;
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
/*******************************************************************************
**
** Function gatt_server_handle_client_req
Expand Down Expand Up @@ -1839,11 +1842,12 @@ void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
}
/* otherwise, ignore the pkt */
} else {
#if GATTS_ROBUST_CACHING_ENABLED
// handle database out of sync
if (gatts_handle_db_out_of_sync(p_tcb, op_code, len, p_data)) {
return;
}

#endif /* GATTS_ROBUST_CACHING_ENABLED */
switch (op_code) {
case GATT_REQ_READ_BY_GRP_TYPE: /* discover primary services */
case GATT_REQ_FIND_TYPE_VALUE: /* discover service by UUID */
Expand Down
4 changes: 2 additions & 2 deletions components/bt/host/bluedroid/stack/gatt/gatt_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,9 +1090,9 @@ tGATT_TCB *gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport)
p_tcb->transport = transport;
}
memcpy(p_tcb->peer_bda, bda, BD_ADDR_LEN);
#if (GATTS_INCLUDED == TRUE)
#if GATTS_ROBUST_CACHING_ENABLED
gatt_sr_init_cl_status(p_tcb);
#endif ///GATTS_INCLUDED == TRUE
#endif /* GATTS_ROBUST_CACHING_ENABLED */
}
return p_tcb;
}
Expand Down
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/stack/gatt/include/gatt_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,15 @@ typedef struct {
tGATT_PROFILE_CLCB profile_clcb[GATT_MAX_APPS];
#endif ///GATTS_INCLUDED == TRUE
UINT16 handle_of_h_r; /* Handle of the handles reused characteristic value */
#if GATTS_ROBUST_CACHING_ENABLED
UINT16 handle_of_database_hash;
UINT16 handle_of_cl_supported_feat;
UINT16 handle_of_sr_supported_feat;
BT_OCTET16 database_hash;
UINT8 gatt_sr_supported_feat_mask;
UINT8 gatt_cl_supported_feat_mask;

#endif
tGATT_APPL_INFO cb_info;


Expand Down

0 comments on commit 41facc2

Please sign in to comment.