change(bt/bluedroid): Release record data after SDP record created

This commit is contained in:
liqigan 2024-08-12 17:27:45 +08:00
parent 0c388cf576
commit 9becdfdc6d
2 changed files with 91 additions and 60 deletions

View File

@ -558,7 +558,7 @@ void bta_sdp_create_record(tBTA_SDP_MSG *p_data)
APPL_TRACE_DEBUG("%s() event: %d\n", __func__, p_data->record.hdr.event); APPL_TRACE_DEBUG("%s() event: %d\n", __func__, p_data->record.hdr.event);
tBTA_SDP_CREATE_RECORD_USER bta_sdp = {0}; tBTA_SDP_CREATE_RECORD_USER bta_sdp = {0};
bta_sdp.status = BTA_SDP_SUCCESS; bta_sdp.status = BTA_SDP_SUCCESS;
bta_sdp.handle = (int)p_data->record.user_data; bta_sdp.handle = -1;
if (bta_sdp_cb.p_dm_cback) { if (bta_sdp_cb.p_dm_cback) {
bta_sdp_cb.p_dm_cback(BTA_SDP_CREATE_RECORD_USER_EVT, (tBTA_SDP *)&bta_sdp, p_data->record.user_data); bta_sdp_cb.p_dm_cback(BTA_SDP_CREATE_RECORD_USER_EVT, (tBTA_SDP *)&bta_sdp, p_data->record.user_data);
} }

View File

@ -27,6 +27,7 @@ typedef enum {
typedef struct { typedef struct {
sdp_state_t state; sdp_state_t state;
int sdp_handle; int sdp_handle;
esp_bt_uuid_t uuid;
bluetooth_sdp_record* record_data; bluetooth_sdp_record* record_data;
} sdp_slot_t; } sdp_slot_t;
@ -92,11 +93,11 @@ static int get_sdp_records_size(bluetooth_sdp_record* in_record, int count)
return records_size; return records_size;
} }
static void set_sdp_handle(int id, int handle) static void set_sdp_slot_info(int id, int sdp_handle, esp_bt_uuid_t *uuid)
{ {
sdp_slot_t *slot = NULL; sdp_slot_t *slot = NULL;
BTC_TRACE_DEBUG("%s() id=%d to handle=0x%08x", __func__, id, handle); BTC_TRACE_DEBUG("%s() id=%d to sdp_handle=0x%08x", __func__, id, sdp_handle);
if(id >= SDP_MAX_RECORDS) { if(id >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
@ -104,19 +105,30 @@ static void set_sdp_handle(int id, int handle)
} }
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT); osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
do {
slot = sdp_local_param.sdp_slots[id]; slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) { if (slot == NULL) {
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex); BTC_TRACE_ERROR("%s() id = %d ", __func__, id);
BTC_TRACE_ERROR("%s() id=%d to handle=0x%08x, set failed", __func__, id, handle); break;
return;
} }
slot->sdp_handle = handle;
if (slot->state != SDP_RECORD_ALLOCED) {
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__, id,
sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
break;
}
slot->sdp_handle = sdp_handle;
slot->record_data = NULL;
memcpy(&slot->uuid, uuid, sizeof(esp_bt_uuid_t));
} while (0);
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex); osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
} }
static bool get_sdp_uuid_by_handle(int handle, esp_bt_uuid_t **uuid)
static bool get_sdp_record_by_handle(int handle, bluetooth_sdp_record* record)
{ {
bool ret = false;
sdp_slot_t *slot = NULL; sdp_slot_t *slot = NULL;
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT); osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
@ -124,14 +136,16 @@ static bool get_sdp_record_by_handle(int handle, bluetooth_sdp_record* record)
for (int i = 0; i < SDP_MAX_RECORDS; i++) { for (int i = 0; i < SDP_MAX_RECORDS; i++) {
slot = sdp_local_param.sdp_slots[i]; slot = sdp_local_param.sdp_slots[i];
if ((slot != NULL) && (slot->sdp_handle == handle)) { if ((slot != NULL) && (slot->sdp_handle == handle)) {
memcpy(record, slot->record_data, sizeof(bluetooth_sdp_record)); if (uuid) {
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex); *uuid = &slot->uuid;
return true; }
ret = true;
break;
} }
} }
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex); osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
return false; return ret;
} }
static int get_sdp_slot_id_by_handle(int handle) static int get_sdp_slot_id_by_handle(int handle)
@ -152,9 +166,10 @@ static int get_sdp_slot_id_by_handle(int handle)
return -1; return -1;
} }
static sdp_slot_t *start_create_sdp(int id) static bluetooth_sdp_record *start_create_sdp(int id)
{ {
sdp_slot_t *sdp_slot = NULL; sdp_slot_t *slot = NULL;
bluetooth_sdp_record* record_data = NULL;
if(id >= SDP_MAX_RECORDS) { if(id >= SDP_MAX_RECORDS) {
BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); BTC_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id);
@ -162,18 +177,25 @@ static sdp_slot_t *start_create_sdp(int id)
} }
osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT); osi_mutex_lock(&sdp_local_param.sdp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
sdp_slot = sdp_local_param.sdp_slots[id];
if (sdp_slot == NULL) { do {
slot = sdp_local_param.sdp_slots[id];
if (slot == NULL) {
BTC_TRACE_ERROR("%s() id = %d ", __func__, id); BTC_TRACE_ERROR("%s() id = %d ", __func__, id);
} else if(sdp_slot->state != SDP_RECORD_ALLOCED) { break;
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__,
id, sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
/* The record have been removed before this event occurred - e.g. deinit */
sdp_slot = NULL;
} }
if (slot->state != SDP_RECORD_ALLOCED) {
BTC_TRACE_ERROR("%s() failed - state for id %d is state = %d expected %d", __func__, id,
sdp_local_param.sdp_slots[id]->state, SDP_RECORD_ALLOCED);
break;
}
record_data = slot->record_data;
} while (0);
osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex); osi_mutex_unlock(&sdp_local_param.sdp_slot_mutex);
return sdp_slot; return record_data;
} }
/* Deep copy all content of in_records into out_records. /* Deep copy all content of in_records into out_records.
@ -357,7 +379,7 @@ static int add_raw_sdp(const bluetooth_sdp_record* rec)
UINT_DESC_TYPE, (UINT32)2, temp); UINT_DESC_TYPE, (UINT32)2, temp);
} }
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -448,7 +470,7 @@ static int add_maps_sdp(const bluetooth_sdp_mas_record* rec)
UINT_DESC_TYPE, (UINT32)2, temp); UINT_DESC_TYPE, (UINT32)2, temp);
} }
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -523,7 +545,7 @@ static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec)
UINT_DESC_TYPE, (UINT32)2, temp); UINT_DESC_TYPE, (UINT32)2, temp);
} }
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -603,7 +625,7 @@ static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec)
UINT_DESC_TYPE, (UINT32)2, temp); UINT_DESC_TYPE, (UINT32)2, temp);
} }
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -649,7 +671,7 @@ static int add_pbapc_sdp(const bluetooth_sdp_pce_record* rec)
UUID_SERVCLASS_PHONE_ACCESS, UUID_SERVCLASS_PHONE_ACCESS,
rec->hdr.profile_version); rec->hdr.profile_version);
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -736,7 +758,7 @@ static int add_opps_sdp(const bluetooth_sdp_ops_record* rec)
UINT_DESC_TYPE, (UINT32)2, temp); UINT_DESC_TYPE, (UINT32)2, temp);
} }
/* Make the service browseable */ /* Make the service browsable */
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -799,7 +821,7 @@ static int add_saps_sdp(const bluetooth_sdp_sap_record* rec)
UUID_SERVCLASS_SAP, UUID_SERVCLASS_SAP,
rec->hdr.profile_version); rec->hdr.profile_version);
// Make the service browseable // Make the service browsable
status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse);
if (!status) { if (!status) {
@ -816,46 +838,55 @@ static int add_saps_sdp(const bluetooth_sdp_sap_record* rec)
static int btc_handle_create_record_event(int id) static int btc_handle_create_record_event(int id)
{ {
int handle = -1; int sdp_handle = 0;
const sdp_slot_t *sdp_slot = NULL;
BTC_TRACE_DEBUG("Sdp Server %s", __func__); BTC_TRACE_DEBUG("Sdp Server %s", __func__);
sdp_slot = start_create_sdp(id); bluetooth_sdp_record *record = start_create_sdp(id);
if(sdp_slot != NULL) { if(record != NULL) {
bluetooth_sdp_record* record = sdp_slot->record_data;
switch (record->hdr.type) { switch (record->hdr.type) {
case SDP_TYPE_RAW: case SDP_TYPE_RAW:
handle = add_raw_sdp(record); sdp_handle = add_raw_sdp(record);
break; break;
case SDP_TYPE_MAP_MAS: case SDP_TYPE_MAP_MAS:
handle = add_maps_sdp(&record->mas); sdp_handle = add_maps_sdp(&record->mas);
break; break;
case SDP_TYPE_MAP_MNS: case SDP_TYPE_MAP_MNS:
handle = add_mapc_sdp(&record->mns); sdp_handle = add_mapc_sdp(&record->mns);
break; break;
case SDP_TYPE_PBAP_PSE: case SDP_TYPE_PBAP_PSE:
handle = add_pbaps_sdp(&record->pse); sdp_handle = add_pbaps_sdp(&record->pse);
break; break;
case SDP_TYPE_PBAP_PCE: case SDP_TYPE_PBAP_PCE:
handle = add_pbapc_sdp(&record->pce); sdp_handle = add_pbapc_sdp(&record->pce);
break; break;
case SDP_TYPE_OPP_SERVER: case SDP_TYPE_OPP_SERVER:
handle = add_opps_sdp(&record->ops); sdp_handle = add_opps_sdp(&record->ops);
break; break;
case SDP_TYPE_SAP_SERVER: case SDP_TYPE_SAP_SERVER:
handle = add_saps_sdp(&record->sap); sdp_handle = add_saps_sdp(&record->sap);
break; break;
default: default:
BTC_TRACE_DEBUG("Record type %d is not supported", record->hdr.type); BTC_TRACE_DEBUG("Record type %d is not supported", record->hdr.type);
break; break;
} }
if(handle != -1) {
set_sdp_handle(id, handle); if(sdp_handle != 0) {
set_sdp_slot_info(id, sdp_handle, &record->hdr.bt_uuid);
// free the record, since not use it anymore
osi_free(record);
} else {
sdp_handle = -1;
} }
} else {
sdp_handle = -1;
} }
return handle; if (sdp_handle == -1) {
free_sdp_slot(id);
}
return sdp_handle;
} }
static bool btc_sdp_remove_record_event(int handle) static bool btc_sdp_remove_record_event(int handle)
@ -1022,14 +1053,14 @@ static void btc_sdp_remove_record(btc_sdp_args_t *arg)
break; break;
} }
bluetooth_sdp_record rec; esp_bt_uuid_t *service_uuid = NULL;
if (get_sdp_record_by_handle(arg->remove_record.record_handle, &rec)) { if (get_sdp_uuid_by_handle(arg->remove_record.record_handle, &service_uuid)) {
if (rec.hdr.bt_uuid.len == ESP_UUID_LEN_16) { if (service_uuid->len == ESP_UUID_LEN_16) {
bta_sys_remove_uuid(rec.hdr.bt_uuid.uuid.uuid16); bta_sys_remove_uuid(service_uuid->uuid.uuid16);
} else if (rec.hdr.bt_uuid.len == ESP_UUID_LEN_32) { } else if (service_uuid->len == ESP_UUID_LEN_32) {
bta_sys_remove_uuid_32(rec.hdr.bt_uuid.uuid.uuid32); bta_sys_remove_uuid_32(service_uuid->uuid.uuid32);
} else if (rec.hdr.bt_uuid.len == ESP_UUID_LEN_128) { } else if (service_uuid->len == ESP_UUID_LEN_128) {
bta_sys_remove_uuid_128((UINT8 *)&rec.hdr.bt_uuid.uuid.uuid128); bta_sys_remove_uuid_128((UINT8 *)&service_uuid->uuid.uuid128);
} }
} else { } else {
BTC_TRACE_ERROR("%s SDP record with handle %d not found", BTC_TRACE_ERROR("%s SDP record with handle %d not found",