Skip to content

Commit

Permalink
bluedroid: support GATT robust caching feature
Browse files Browse the repository at this point in the history
  • Loading branch information
esp-cjh committed Sep 6, 2023
1 parent 3565b74 commit 7e6b085
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 18 deletions.
86 changes: 86 additions & 0 deletions components/bt/host/bluedroid/bta/gatt/bta_gatts_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
#include "bta/bta_gatts_co.h"
#include "btc/btc_storage.h"
#include "btc/btc_ble_storage.h"
// #include "btif_util.h"

#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
Expand Down Expand Up @@ -159,5 +161,89 @@ BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,

return FALSE;
}

/*******************************************************************************
**
** Function bta_gatts_co_cl_feat_save
**
** Description This callout function is executed by GATTS when GATT server
** client support feature is requested to write to NV.
**
** Parameter remote_addr - remote device address
** feature - pointer of client support feature
**
** Returns void.
**
*******************************************************************************/
void bta_gatts_co_cl_feat_save(BD_ADDR remote_addr, UINT8 *feature)
{
bt_bdaddr_t bd_addr;

memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
btc_storage_set_gatt_cl_supp_feat(&bd_addr, feature, 1);
}

/*******************************************************************************
**
** Function bta_gatts_co_db_hash_save
**
** Description This callout function is executed by GATTS when GATT server
** client status is requested to write to NV.
**
** Parameter remote_addr - remote device address
** db_hash - pointer of GATT service datebase hash
**
** Returns void.
**
*******************************************************************************/
void bta_gatts_co_db_hash_save(BD_ADDR remote_addr, BT_OCTET16 db_hash)
{
bt_bdaddr_t bd_addr;

memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
btc_storage_set_gatt_db_hash(&bd_addr, db_hash, BT_OCTET16_LEN);
}

/*******************************************************************************
**
** Function bta_gatts_co_cl_feat_load
**
** Description This callout function is executed by GATTS when GATT server
** client status is requested to load from NV.
**
** Parameter remote_addr - remote device address
** feature - pointer of GATT service datebase hash
**
** Returns void.
**
*******************************************************************************/
void bta_gatts_co_cl_feat_load(BD_ADDR remote_addr, UINT8 *feature)
{
bt_bdaddr_t bd_addr;

memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
btc_storage_get_gatt_cl_supp_feat(&bd_addr, feature, 1);
}

/*******************************************************************************
**
** Function bta_gatts_co_db_hash_load
**
** Description This callout function is executed by GATTS when GATT server
** client status is requested to load from NV.
**
** Parameter remote_addr - remote device address
** db_hash - pointer of GATT service datebase hash
**
** Returns void.
**
*******************************************************************************/
void bta_gatts_co_db_hash_load(BD_ADDR remote_addr, BT_OCTET16 db_hash)
{
bt_bdaddr_t bd_addr;

memcpy(bd_addr.address, remote_addr, BD_ADDR_LEN);
btc_storage_get_gatt_db_hash(&bd_addr, db_hash, BT_OCTET16_LEN);
}
#endif
#endif
7 changes: 7 additions & 0 deletions components/bt/host/bluedroid/bta/include/bta/bta_gatts_co.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,12 @@ extern BOOLEAN bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
extern BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,
tBTA_GATTS_HNDL_RANGE *p_handle);

extern void bta_gatts_co_cl_feat_save(BD_ADDR remote_addr, UINT8 *feature);

extern void bta_gatts_co_db_hash_save(BD_ADDR remote_addr, BT_OCTET16 db_hash);

extern void bta_gatts_co_cl_feat_load(BD_ADDR remote_addr, UINT8 *feature);

extern void bta_gatts_co_db_hash_load(BD_ADDR remote_addr, BT_OCTET16 db_hash);

#endif /* BTA_GATTS_CO_H */
80 changes: 79 additions & 1 deletion components/bt/host/bluedroid/btc/core/btc_ble_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ static void _btc_storage_save(void)
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PID_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LENC_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_GATT_DB_HASH_STR)) {
iter = btc_config_section_next(iter);
btc_config_remove_section(section);
continue;
Expand Down Expand Up @@ -932,3 +934,79 @@ int btc_storage_get_num_ble_bond_devices(void)
}
#endif ///BLE_INCLUDED == TRUE
#endif ///SMP_INCLUDED == TRUE

#if (BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
bt_status_t btc_storage_get_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
{
bdstr_t bdstr;
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
int ret = btc_config_get_bin(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR, value, (size_t *)&len);
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
}

bt_status_t btc_storage_set_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
{
int ret;
bdstr_t bdstr;

bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
ret = btc_config_set_bin(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR, value, (size_t)len);
if (ret == false) {
return BT_STATUS_FAIL;
}

return BT_STATUS_SUCCESS;
}

bt_status_t btc_storage_get_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
{
bdstr_t bdstr;
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
int ret = btc_config_get_bin(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR, value, (size_t *)&len);
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
}

bt_status_t btc_storage_set_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
{
int ret;
bdstr_t bdstr;

bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
ret = btc_config_set_bin(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR, value, (size_t)len);
if (ret == false) {
return BT_STATUS_FAIL;
}

return BT_STATUS_SUCCESS;
}

bt_status_t btc_storage_remove_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr)
{
bool ret = true;
bdstr_t bdstr;

bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));

ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR);
if (ret == false) {
return BT_STATUS_FAIL;
}

return BT_STATUS_SUCCESS;
}

bt_status_t btc_storage_remove_gatt_db_hash(bt_bdaddr_t *remote_bd_addr)
{
bool ret = true;
bdstr_t bdstr;

bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));

ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR);
if (ret == false) {
return BT_STATUS_FAIL;
}

return BT_STATUS_SUCCESS;
}
#endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
4 changes: 4 additions & 0 deletions components/bt/host/bluedroid/btc/core/btc_dm.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ static void btc_dm_remove_ble_bonding_keys(void)

bdcpy(bd_addr.address, btc_dm_cb.pairing_cb.bd_addr);

btc_storage_remove_gatt_cl_supp_feat(&bd_addr);
btc_storage_remove_gatt_db_hash(&bd_addr);
btc_storage_remove_remote_addr_type(&bd_addr, false);
btc_storage_remove_ble_dev_auth_mode(&bd_addr, false);
btc_storage_remove_ble_dev_type(&bd_addr, false);
Expand Down Expand Up @@ -817,6 +819,8 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)

if (p_data->link_down.status == HCI_SUCCESS) {
//remove the bonded key in the config and nvs flash.
btc_storage_remove_gatt_cl_supp_feat(&bd_addr);
btc_storage_remove_gatt_db_hash(&bd_addr);
btc_storage_remove_ble_dev_type(&bd_addr, false);
btc_storage_remove_remote_addr_type(&bd_addr, false);
btc_storage_remove_ble_dev_auth_mode(&bd_addr, false);
Expand Down
16 changes: 15 additions & 1 deletion components/bt/host/bluedroid/btc/include/btc/btc_ble_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ bt_status_t btc_storage_load_bonded_ble_devices(void);
bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev, int dev_num);

int btc_storage_get_num_ble_bond_devices(void);

#endif ///SMP_INCLUDED == TRUE

#define BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR "GATT_CL_SUPP_FEAT"
#define BTC_BLE_STORAGE_GATT_DB_HASH_STR "GATT_DB_HASH"

bt_status_t btc_storage_get_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len);

bt_status_t btc_storage_set_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len);

bt_status_t btc_storage_remove_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr);

bt_status_t btc_storage_get_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len);

bt_status_t btc_storage_set_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len);

bt_status_t btc_storage_remove_gatt_db_hash(bt_bdaddr_t *remote_bd_addr);
#endif ///__BTC_BLE_STORAGE_H__
16 changes: 16 additions & 0 deletions components/bt/host/bluedroid/stack/gatt/gatt_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ BOOLEAN GATTS_NVRegister (const tGATT_APPL_INFO *p_cb_info)
return status;
}

static void gatt_update_for_database_change(void)
{
UINT8 i;

gatts_calculate_datebase_hash(gatt_cb.database_hash);

for (i = 0; i < GATT_MAX_PHY_CHANNEL; i++) {
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(i);
if (p_tcb && p_tcb->in_use) {
gatt_sr_update_cl_status(p_tcb, false);
}
}
}

/*******************************************************************************
**
** Function GATTS_CreateService
Expand Down Expand Up @@ -398,6 +412,7 @@ 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 {
gatt_update_for_database_change();
if (GATTS_SEND_SERVICE_CHANGE_MODE == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}
Expand Down Expand Up @@ -510,6 +525,7 @@ 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) {
gatt_update_for_database_change();
if (GATTS_SEND_SERVICE_CHANGE_MODE == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}
Expand Down
Loading

0 comments on commit 7e6b085

Please sign in to comment.