From 1cfb537813dd7b77a27283c2e3f40cce17588153 Mon Sep 17 00:00:00 2001 From: liqigan Date: Tue, 10 Sep 2024 17:59:25 +0800 Subject: [PATCH 1/2] fix(bt/bluedroid): Fixed the SDP API compatibility bug --- components/bt/host/bluedroid/Kconfig.in | 1 + .../bluedroid/api/include/api/esp_sdp_api.h | 84 ++++++++++--------- .../bt/host/bluedroid/bta/sdp/bta_sdp_act.c | 24 +++--- .../btc/profile/std/include/bt_sdp.h | 76 +++++++++-------- .../bluedroid/btc/profile/std/sdp/btc_sdp.c | 38 ++++----- .../release-5.x/5.4/bluetooth-classic.rst | 4 +- .../release-5.x/5.4/bluetooth-classic.rst | 4 +- .../classic_bt/bt_l2cap_client/main/main.c | 6 +- .../classic_bt/bt_l2cap_server/main/main.c | 6 +- 9 files changed, 117 insertions(+), 126 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index 53a3755c79..e5769d1959 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -126,6 +126,7 @@ config BT_L2CAP_ENABLED config BT_SDP_COMMON_ENABLED bool "BT SDP COMMON" depends on BT_CLASSIC_ENABLED + default y if BT_L2CAP_ENABLED default n help This enables common SDP operation, such as SDP record creation and deletion. diff --git a/components/bt/host/bluedroid/api/include/api/esp_sdp_api.h b/components/bt/host/bluedroid/api/include/api/esp_sdp_api.h index 99bf61be3f..42b134c8cb 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_sdp_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_sdp_api.h @@ -65,64 +65,66 @@ typedef enum { /** * @brief SDP header structure */ -typedef struct { - esp_bluetooth_sdp_types_t type; /*!< SDP type */ - uint32_t service_name_length; /*!< Service name length */ - char *service_name; /*!< Service name */ - int32_t rfcomm_channel_number; /*!< RFCOMM channel number, if not used set to -1*/ - int32_t l2cap_psm; /*!< L2CAP psm, if not used set to -1 */ - int32_t profile_version; /*!< Profile version */ -} esp_bluetooth_sdp_hdr_t; +typedef struct bluetooth_sdp_hdr_overlay { + esp_bluetooth_sdp_types_t type; /*!< SDP type */ + esp_bt_uuid_t uuid; /*!< UUID type, include uuid and uuid length, only needed to be set for RAW record creation */ + uint32_t service_name_length; /*!< Service name length */ + char *service_name; /*!< Service name */ + int32_t rfcomm_channel_number; /*!< RFCOMM channel number, if not used set to -1*/ + int32_t l2cap_psm; /*!< L2CAP psm, if not used set to -1 */ + int32_t profile_version; /*!< Profile version */ + int user1_ptr_len; /*!< User data1 length, only used for searching RAW record */ + uint8_t *user1_ptr; /*!< User data1 pointer to the raw SDP response data, only used for searching RAW record */ + int user2_ptr_len __attribute__((deprecated)); /*!< User data2 length, only used for searching RAW record */ + uint8_t *user2_ptr __attribute__((deprecated)); /*!< User data2 pointer, only used for searching RAW record */ +} esp_bluetooth_sdp_hdr_overlay_t; /** * @brief Raw SDP record */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ - esp_bt_uuid_t uuid; /*!< UUID type, include uuid and uuid length */ - int user1_ptr_len; /*!< Length of raw SDP data */ - uint8_t *user1_ptr; /*!< Raw SDP data */ +typedef struct bluetooth_sdp_raw_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ } esp_bluetooth_sdp_raw_record_t; /** * @brief Message Access Profile - Server parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ - uint32_t mas_instance_id; /*!< MAS Instance ID */ - uint32_t supported_features; /*!< Map supported features */ - uint32_t supported_message_types; /*!< Supported message types */ +typedef struct bluetooth_sdp_mas_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ + uint32_t mas_instance_id; /*!< MAS Instance ID */ + uint32_t supported_features; /*!< Map supported features */ + uint32_t supported_message_types; /*!< Supported message types */ } esp_bluetooth_sdp_mas_record_t; /** * @brief Message Access Profile - Client (Notification Server) parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ - uint32_t supported_features; /*!< Supported features */ +typedef struct bluetooth_sdp_mns_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ + uint32_t supported_features; /*!< Supported features */ } esp_bluetooth_sdp_mns_record_t; /** * @brief Phone Book Profile - Server parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ - uint32_t supported_features; /*!< PBAP Supported Features */ - uint32_t supported_repositories; /*!< Supported Repositories */ +typedef struct bluetooth_sdp_pse_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ + uint32_t supported_features; /*!< PBAP Supported Features */ + uint32_t supported_repositories; /*!< Supported Repositories */ } esp_bluetooth_sdp_pse_record_t; /** * @brief Phone Book Profile - Client parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ +typedef struct bluetooth_sdp_pce_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ } esp_bluetooth_sdp_pce_record_t; /** * @brief Object Push Profile parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ +typedef struct bluetooth_sdp_ops_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ int supported_formats_list_len; /*!< Supported formats list length */ uint8_t supported_formats_list[SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH]; /*!< Supported formats list */ } esp_bluetooth_sdp_ops_record_t; @@ -130,8 +132,8 @@ typedef struct { /** * @brief SIM Access Profile parameters */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ +typedef struct bluetooth_sdp_sap_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ } esp_bluetooth_sdp_sap_record_t; /** @@ -148,21 +150,21 @@ typedef enum { * @note Only one primary Device Identification service record can be added in the SDP database. If primary * Device Identification service is created multiple times, only the last one will take effect. */ -typedef struct { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ - uint16_t vendor; /*!< Vendor ID */ - uint16_t vendor_id_source; /*!< Vendor ID source, 0x0001 for Bluetooth, 0x0002 for USB, other values reserved, see `esp_sdp_vendor_id_source_t` */ - uint16_t product; /*!< Product ID */ - uint16_t version; /*!< Release version in format 0xJJMN(JJ – major number, M – minor number, N – sub-minor number) */ - bool primary_record; /*!< Indicate if the record is primary, shall set to true if there is a only single device - record, others shall be set to false */ +typedef struct bluetooth_sdp_dip_record { + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ + uint16_t vendor; /*!< Vendor ID */ + uint16_t vendor_id_source; /*!< Vendor ID source, 0x0001 for Bluetooth, 0x0002 for USB, other values reserved, see `esp_sdp_vendor_id_source_t` */ + uint16_t product; /*!< Product ID */ + uint16_t version; /*!< Release version in format 0xJJMN(JJ – major number, M – minor number, N – sub-minor number) */ + bool primary_record; /*!< Indicate if the record is primary, shall set to true if there is a only single device + record, others shall be set to false */ } esp_bluetooth_sdp_dip_record_t; /** * @brief SDP record parameters union */ typedef union { - esp_bluetooth_sdp_hdr_t hdr; /*!< General info */ + esp_bluetooth_sdp_hdr_overlay_t hdr; /*!< General info */ esp_bluetooth_sdp_raw_record_t raw; /*!< Raw SDP search data for unknown UUIDs */ esp_bluetooth_sdp_mas_record_t mas; /*!< Message Access Profile - Server */ esp_bluetooth_sdp_mns_record_t mns; /*!< Message Access Profile - Client (Notification Server) */ @@ -205,7 +207,7 @@ typedef union { /** * @brief ESP_SDP_CREATE_RECORD_COMP_EVT */ - struct sdp_crate_record_evt_param { + struct sdp_create_record_evt_param { esp_sdp_status_t status; /*!< Status */ int record_handle; /*!< SDP record handle */ } create_record; /*!< SDP callback param of ESP_SDP_CREATE_RECORD_COMP_EVT */ diff --git a/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c b/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c index 2ece2df56d..39d5e2895b 100644 --- a/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c +++ b/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c @@ -395,29 +395,29 @@ static void bta_create_raw_sdp_record(bluetooth_sdp_record *record, tSDP_DISC_RE tSDP_DISC_ATTR *p_attr; tSDP_PROTOCOL_ELEM pe; - record->hdr.type = SDP_TYPE_RAW; - record->hdr.service_name_length = 0; - record->hdr.service_name = NULL; - record->hdr.rfcomm_channel_number = -1; - record->hdr.l2cap_psm = -1; - record->hdr.profile_version = -1; + record->raw.hdr.type = SDP_TYPE_RAW; + record->raw.hdr.service_name_length = 0; + record->raw.hdr.service_name = NULL; + record->raw.hdr.rfcomm_channel_number = -1; + record->raw.hdr.l2cap_psm = -1; + record->raw.hdr.profile_version = -1; /* Try to extract a service name */ if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_NAME)) != NULL) { - record->pse.hdr.service_name_length = SDP_DISC_ATTR_LEN(p_attr->attr_len_type); - record->pse.hdr.service_name = (char *)p_attr->attr_value.v.array; + record->raw.hdr.service_name_length = SDP_DISC_ATTR_LEN(p_attr->attr_len_type); + record->raw.hdr.service_name = (char *)p_attr->attr_value.v.array; } if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_GOEP_L2CAP_PSM)) != NULL) { - record->hdr.l2cap_psm = p_attr->attr_value.v.u16; + record->raw.hdr.l2cap_psm = p_attr->attr_value.v.u16; } /* Try to extract an RFCOMM channel */ if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) { - record->pse.hdr.rfcomm_channel_number = pe.params[0]; + record->raw.hdr.rfcomm_channel_number = pe.params[0]; } - record->raw.user1_ptr_len = p_bta_sdp_cfg->p_sdp_db->raw_size; - record->raw.user1_ptr = p_bta_sdp_cfg->p_sdp_db->raw_data; + record->raw.hdr.user1_ptr_len = p_bta_sdp_cfg->p_sdp_db->raw_size; + record->raw.hdr.user1_ptr = p_bta_sdp_cfg->p_sdp_db->raw_data; } diff --git a/components/bt/host/bluedroid/btc/profile/std/include/bt_sdp.h b/components/bt/host/bluedroid/btc/profile/std/include/bt_sdp.h index 5c7202a7ae..69fac9d6cb 100644 --- a/components/bt/host/bluedroid/btc/profile/std/include/bt_sdp.h +++ b/components/bt/host/bluedroid/btc/profile/std/include/bt_sdp.h @@ -38,73 +38,79 @@ typedef enum { SDP_TYPE_DIP_SERVER, // Device Identification Profile } bluetooth_sdp_types; -typedef struct _bluetooth_sdp_hdr { +/** + * Some signals need additional pointers, hence we introduce a + * generic way to handle these pointers. + */ +typedef struct _bluetooth_sdp_hdr_overlay { bluetooth_sdp_types type; + esp_bt_uuid_t uuid; uint32_t service_name_length; char *service_name; int32_t rfcomm_channel_number; int32_t l2cap_psm; int32_t profile_version; -} bluetooth_sdp_hdr; + int user1_ptr_len; + uint8_t *user1_ptr; + int user2_ptr_len; // not used + uint8_t *user2_ptr; // not used +} bluetooth_sdp_hdr_overlay; typedef struct _bluetooth_sdp_raw_record { - bluetooth_sdp_hdr hdr; - esp_bt_uuid_t uuid; - int user1_ptr_len; - uint8_t *user1_ptr; + bluetooth_sdp_hdr_overlay hdr; } bluetooth_sdp_raw_record; typedef struct _bluetooth_sdp_mas_record { - bluetooth_sdp_hdr hdr; - uint32_t mas_instance_id; - uint32_t supported_features; - uint32_t supported_message_types; + bluetooth_sdp_hdr_overlay hdr; + uint32_t mas_instance_id; + uint32_t supported_features; + uint32_t supported_message_types; } bluetooth_sdp_mas_record; typedef struct _bluetooth_sdp_mns_record { - bluetooth_sdp_hdr hdr; - uint32_t supported_features; + bluetooth_sdp_hdr_overlay hdr; + uint32_t supported_features; } bluetooth_sdp_mns_record; typedef struct _bluetooth_sdp_pse_record { - bluetooth_sdp_hdr hdr; - uint32_t supported_features; - uint32_t supported_repositories; + bluetooth_sdp_hdr_overlay hdr; + uint32_t supported_features; + uint32_t supported_repositories; } bluetooth_sdp_pse_record; typedef struct _bluetooth_sdp_pce_record { - bluetooth_sdp_hdr hdr; + bluetooth_sdp_hdr_overlay hdr; } bluetooth_sdp_pce_record; typedef struct _bluetooth_sdp_ops_record { - bluetooth_sdp_hdr hdr; - int supported_formats_list_len; - uint8_t supported_formats_list[SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH]; + bluetooth_sdp_hdr_overlay hdr; + int supported_formats_list_len; + uint8_t supported_formats_list[SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH]; } bluetooth_sdp_ops_record; typedef struct _bluetooth_sdp_sap_record { - bluetooth_sdp_hdr hdr; + bluetooth_sdp_hdr_overlay hdr; } bluetooth_sdp_sap_record; typedef struct _bluetooth_sdp_dip_record { - bluetooth_sdp_hdr hdr; - uint16_t vendor; - uint16_t vendor_id_source; - uint16_t product; - uint16_t version; - bool primary_record; + bluetooth_sdp_hdr_overlay hdr; + uint16_t vendor; + uint16_t vendor_id_source; + uint16_t product; + uint16_t version; + bool primary_record; } bluetooth_sdp_dip_record; typedef union { - bluetooth_sdp_hdr hdr; - bluetooth_sdp_raw_record raw; - bluetooth_sdp_mas_record mas; - bluetooth_sdp_mns_record mns; - bluetooth_sdp_pse_record pse; - bluetooth_sdp_pce_record pce; - bluetooth_sdp_ops_record ops; - bluetooth_sdp_sap_record sap; - bluetooth_sdp_dip_record dip; + bluetooth_sdp_hdr_overlay hdr; + bluetooth_sdp_raw_record raw; + bluetooth_sdp_mas_record mas; + bluetooth_sdp_mns_record mns; + bluetooth_sdp_pse_record pse; + bluetooth_sdp_pce_record pce; + bluetooth_sdp_ops_record ops; + bluetooth_sdp_sap_record sap; + bluetooth_sdp_dip_record dip; } bluetooth_sdp_record; #endif /* __BT_SDP_H__ */ diff --git a/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c b/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c index f395757c7d..75cc0d7d51 100644 --- a/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c +++ b/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c @@ -91,11 +91,6 @@ static int get_sdp_record_size(bluetooth_sdp_record* in_record) records_size = sizeof(bluetooth_sdp_record); break; - case SDP_TYPE_RAW: - if (record->raw.user1_ptr != NULL) { - records_size += record->raw.user1_ptr_len; - } - /* fall through */ default: records_size += sizeof(bluetooth_sdp_record); records_size += record->hdr.service_name_length; @@ -254,12 +249,6 @@ static void copy_sdp_record_common(bluetooth_sdp_record* in_record, bluetooth_sd *(free_ptr) = '\0'; // Set '\0' termination of string free_ptr++; } - - if (in_record->hdr.type == SDP_TYPE_RAW && in_record->raw.user1_ptr != NULL) { - out_record->raw.user1_ptr = (UINT8 *)free_ptr; // Update pointer - memcpy(free_ptr, in_record->raw.user1_ptr, in_record->raw.user1_ptr_len); // Copy content - free_ptr += in_record->raw.user1_ptr_len; - } } static void copy_sdp_record(bluetooth_sdp_record* in_record, bluetooth_sdp_record* out_record) @@ -402,6 +391,7 @@ static int add_raw_sdp(const bluetooth_sdp_raw_record *rec) UINT8 temp[LEN_UUID_128]; UINT8* p_temp = temp; UINT32 sdp_handle = 0; + const esp_bt_uuid_t *p_uuid = &rec->hdr.uuid; BTC_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__, rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name); @@ -411,15 +401,15 @@ static int add_raw_sdp(const bluetooth_sdp_raw_record *rec) return sdp_handle; } - if (rec->uuid.len == ESP_UUID_LEN_16) { + if (p_uuid->len == ESP_UUID_LEN_16) { UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES); - UINT16_TO_BE_STREAM (p_temp, rec->uuid.uuid.uuid16); - } else if (rec->uuid.len == ESP_UUID_LEN_32) { + UINT16_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid16); + } else if (p_uuid->len == ESP_UUID_LEN_32) { UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES); - UINT32_TO_BE_STREAM (p_temp, rec->uuid.uuid.uuid32); - } else if (rec->uuid.len == ESP_UUID_LEN_128) { + UINT32_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid32); + } else if (p_uuid->len == ESP_UUID_LEN_128) { UINT8_TO_BE_STREAM (p_temp, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES); - ARRAY_TO_BE_STREAM (p_temp, rec->uuid.uuid.uuid128, LEN_UUID_128); + ARRAY_TO_BE_STREAM (p_temp, p_uuid->uuid.uuid128, LEN_UUID_128); } else { SDP_DeleteRecord(sdp_handle); sdp_handle = 0; @@ -466,12 +456,12 @@ static int add_raw_sdp(const bluetooth_sdp_raw_record *rec) sdp_handle = 0; BTC_TRACE_ERROR("%s() FAILED, status = %d", __func__, status); } else { - if (rec->uuid.len == ESP_UUID_LEN_16) { - bta_sys_add_uuid(rec->uuid.uuid.uuid16); - } else if (rec->uuid.len == ESP_UUID_LEN_32) { - bta_sys_add_uuid_32(rec->uuid.uuid.uuid32); - } else if (rec->uuid.len == ESP_UUID_LEN_128) { - bta_sys_add_uuid_128((UINT8 *)&rec->uuid.uuid.uuid128); + if (p_uuid->len == ESP_UUID_LEN_16) { + bta_sys_add_uuid(p_uuid->uuid.uuid16); + } else if (p_uuid->len == ESP_UUID_LEN_32) { + bta_sys_add_uuid_32(p_uuid->uuid.uuid32); + } else if (p_uuid->len == ESP_UUID_LEN_128) { + bta_sys_add_uuid_128((UINT8 *)&p_uuid->uuid.uuid128); } BTC_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); } @@ -943,7 +933,7 @@ static int btc_handle_create_record_event(int id) switch (record->hdr.type) { case SDP_TYPE_RAW: sdp_handle = add_raw_sdp(&record->raw); - memcpy(&service_uuid, &record->raw.uuid, sizeof(esp_bt_uuid_t)); + memcpy(&service_uuid, &record->hdr.uuid, sizeof(esp_bt_uuid_t)); break; case SDP_TYPE_MAP_MAS: sdp_handle = add_maps_sdp(&record->mas); diff --git a/docs/en/migration-guides/release-5.x/5.4/bluetooth-classic.rst b/docs/en/migration-guides/release-5.x/5.4/bluetooth-classic.rst index 2c229ad4a2..df2202b973 100644 --- a/docs/en/migration-guides/release-5.x/5.4/bluetooth-classic.rst +++ b/docs/en/migration-guides/release-5.x/5.4/bluetooth-classic.rst @@ -11,6 +11,4 @@ Bluedroid - :component_file:`/bt/host/bluedroid/api/include/api/esp_sdp_api.h` - - structure ``esp_bluetooth_sdp_hdr_overlay_t`` has been renamed to ``esp_bluetooth_sdp_hdr_t`` - - field ``uuid``, ``user1_ptr_len`` and ``user1_ptr`` in ``esp_bluetooth_sdp_hdr_overlay_t`` have been moved into ``esp_bluetooth_sdp_raw_record_t`` - - field ``user2_ptr_len`` and ``user2_ptr`` in ``esp_bluetooth_sdp_hdr_overlay_t`` have been removed + - Field ``user2_ptr_len`` and ``user2_ptr`` is deprecated in structure ``esp_bluetooth_sdp_hdr_overlay_t``, since they are not used in SDP record creation or searching. diff --git a/docs/zh_CN/migration-guides/release-5.x/5.4/bluetooth-classic.rst b/docs/zh_CN/migration-guides/release-5.x/5.4/bluetooth-classic.rst index b911fc11c5..6249c0bd29 100644 --- a/docs/zh_CN/migration-guides/release-5.x/5.4/bluetooth-classic.rst +++ b/docs/zh_CN/migration-guides/release-5.x/5.4/bluetooth-classic.rst @@ -11,6 +11,4 @@ Bluedroid - :component_file:`/bt/host/bluedroid/api/include/api/esp_sdp_api.h` - - 结构体 ``esp_bluetooth_sdp_hdr_overlay_t`` 被重命名为 ``esp_bluetooth_sdp_hdr_t`` - - 结构体 ``esp_bluetooth_sdp_hdr_overlay_t`` 中的字段 ``uuid``, ``user1_ptr_len`` 和 ``user1_ptr`` 被移动到结构体 ``esp_bluetooth_sdp_raw_record_t`` 中 - - 结构体 ``esp_bluetooth_sdp_hdr_overlay_t`` 中的字段 ``user2_ptr_len`` 和 ``user2_ptr`` 被删除 + - 结构体 ``esp_bluetooth_sdp_hdr_overlay_t`` 中的字段 ``user2_ptr_len`` 和 ``user2_ptr`` 被弃用,因为 SDP 的 API 或者事件中不会用到该字段。 diff --git a/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_client/main/main.c b/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_client/main/main.c index e24684d978..623f341345 100644 --- a/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_client/main/main.c +++ b/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_client/main/main.c @@ -340,15 +340,13 @@ static void esp_hdl_sdp_cb_evt(uint16_t event, void *p_param) ESP_LOGI(SDP_TAG, "ESP_SDP_INIT_EVT: status:%d", sdp_param->init.status); if (sdp_param->init.status == ESP_SDP_SUCCESS) { record.hdr.type = ESP_SDP_TYPE_RAW; + record.hdr.uuid.len = sizeof(UUID_UNKNOWN); + memcpy(record.hdr.uuid.uuid.uuid128, UUID_UNKNOWN, sizeof(UUID_UNKNOWN)); record.hdr.service_name_length = strlen(sdp_service_name) + 1; record.hdr.service_name = sdp_service_name; record.hdr.rfcomm_channel_number = BT_UNUSED_RFCOMM; record.hdr.l2cap_psm = BT_L2CAP_DYNMIC_PSM; record.hdr.profile_version = BT_UNKONWN_PROFILE_VERSION; - record.uuid.len = sizeof(UUID_UNKNOWN); - memcpy(record.uuid.uuid.uuid128, UUID_UNKNOWN, sizeof(UUID_UNKNOWN)); - record.user1_ptr = NULL; - record.user1_ptr_len = 0; esp_sdp_create_record((esp_bluetooth_sdp_record_t *)&record); } break; diff --git a/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_server/main/main.c b/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_server/main/main.c index 254f381834..5a9a6760e4 100644 --- a/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_server/main/main.c +++ b/examples/bluetooth/bluedroid/classic_bt/bt_l2cap_server/main/main.c @@ -250,15 +250,13 @@ static void esp_hdl_sdp_cb_evt(uint16_t event, void *p_param) ESP_LOGI(SDP_TAG, "ESP_SDP_INIT_EVT: status:%d", sdp_param->init.status); if (sdp_param->init.status == ESP_SDP_SUCCESS) { record.hdr.type = ESP_SDP_TYPE_RAW; + record.hdr.uuid.len = sizeof(UUID_UNKNOWN); + memcpy(record.hdr.uuid.uuid.uuid128, UUID_UNKNOWN, sizeof(UUID_UNKNOWN)); record.hdr.service_name_length = strlen(sdp_service_name) + 1; record.hdr.service_name = sdp_service_name; record.hdr.rfcomm_channel_number = BT_UNUSED_RFCOMM; record.hdr.l2cap_psm = BT_L2CAP_DYNMIC_PSM; record.hdr.profile_version = BT_UNKONWN_PROFILE_VERSION; - record.uuid.len = sizeof(UUID_UNKNOWN); - memcpy(record.uuid.uuid.uuid128, UUID_UNKNOWN, sizeof(UUID_UNKNOWN)); - record.user1_ptr = NULL; - record.user1_ptr_len = 0; esp_sdp_create_record((esp_bluetooth_sdp_record_t *)&record); } break; From 205e66c9fdbd1e2ee274cb39f572284ccf984408 Mon Sep 17 00:00:00 2001 From: liqigan Date: Tue, 10 Sep 2024 20:13:21 +0800 Subject: [PATCH 2/2] fix(bt/bluedroid): Fixed not deep copy service_name and user1_ptr in RAW SDP search event --- .../bluedroid/bta/include/bta/bta_sdp_api.h | 6 +- .../bt/host/bluedroid/bta/sdp/bta_sdp_act.c | 6 +- .../bt/host/bluedroid/bta/sdp/bta_sdp_api.c | 15 ++- .../bt/host/bluedroid/bta/sdp/bta_sdp_cfg.c | 8 ++ .../bluedroid/btc/profile/std/sdp/btc_sdp.c | 117 ++++++++++++++---- 5 files changed, 121 insertions(+), 31 deletions(-) diff --git a/components/bt/host/bluedroid/bta/include/bta/bta_sdp_api.h b/components/bt/host/bluedroid/bta/include/bta/bta_sdp_api.h index f49c76c4e5..fadc8183f0 100644 --- a/components/bt/host/bluedroid/bta/include/bta/bta_sdp_api.h +++ b/components/bt/host/bluedroid/bta/include/bta/bta_sdp_api.h @@ -85,9 +85,11 @@ typedef void (tBTA_SDP_DM_CBACK)(tBTA_SDP_EVT event, tBTA_SDP *p_data, void *use /* MCE configuration structure */ typedef struct { - UINT16 sdp_db_size; /* The size of p_sdp_db */ + UINT16 sdp_raw_size; /* The size of p_sdp_raw_data */ + UINT16 sdp_db_size; /* The size of p_sdp_db */ #if (SDP_INCLUDED == TRUE) - tSDP_DISCOVERY_DB *p_sdp_db; /* The data buffer to keep SDP database */ + UINT8 *p_sdp_raw_data; /* The data buffer to keep raw data */ + tSDP_DISCOVERY_DB *p_sdp_db; /* The data buffer to keep SDP database */ #endif ///SDP_INCLUDED == TRUE } tBTA_SDP_CFG; diff --git a/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c b/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c index 39d5e2895b..fd79057bcf 100644 --- a/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c +++ b/components/bt/host/bluedroid/bta/sdp/bta_sdp_act.c @@ -416,7 +416,7 @@ static void bta_create_raw_sdp_record(bluetooth_sdp_record *record, tSDP_DISC_RE if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) { record->raw.hdr.rfcomm_channel_number = pe.params[0]; } - record->raw.hdr.user1_ptr_len = p_bta_sdp_cfg->p_sdp_db->raw_size; + record->raw.hdr.user1_ptr_len = p_bta_sdp_cfg->p_sdp_db->raw_used; record->raw.hdr.user1_ptr = p_bta_sdp_cfg->p_sdp_db->raw_data; } @@ -570,6 +570,10 @@ void bta_sdp_search(tBTA_SDP_MSG *p_data) SDP_InitDiscoveryDb (p_bta_sdp_cfg->p_sdp_db, p_bta_sdp_cfg->sdp_db_size, 1, bta_sdp_search_uuid, 0, NULL); + /* tell SDP to keep the raw data */ + p_bta_sdp_cfg->p_sdp_db->raw_size = p_bta_sdp_cfg->sdp_raw_size; + p_bta_sdp_cfg->p_sdp_db->raw_data = p_bta_sdp_cfg->p_sdp_raw_data; + if (!SDP_ServiceSearchAttributeRequest2(p_data->get_search.bd_addr, p_bta_sdp_cfg->p_sdp_db, bta_sdp_search_cback, (void *)bta_sdp_search_uuid)) { bta_sdp_cb.sdp_active = BTA_SDP_ACTIVE_NONE; diff --git a/components/bt/host/bluedroid/bta/sdp/bta_sdp_api.c b/components/bt/host/bluedroid/bta/sdp/bta_sdp_api.c index 86fb61ca41..d46f784bf1 100644 --- a/components/bt/host/bluedroid/bta/sdp/bta_sdp_api.c +++ b/components/bt/host/bluedroid/bta/sdp/bta_sdp_api.c @@ -65,7 +65,9 @@ tBTA_SDP_STATUS BTA_SdpEnable(tBTA_SDP_DM_CBACK *p_cback) #if BTA_DYNAMIC_MEMORY == TRUE /* Malloc buffer for SDP configuration structure */ p_bta_sdp_cfg->p_sdp_db = (tSDP_DISCOVERY_DB *)osi_malloc(p_bta_sdp_cfg->sdp_db_size); - if (p_bta_sdp_cfg->p_sdp_db == NULL) { + p_bta_sdp_cfg->p_sdp_raw_data = (UINT8 *)osi_malloc(p_bta_sdp_cfg->sdp_raw_size); + if (p_bta_sdp_cfg->p_sdp_db == NULL || p_bta_sdp_cfg->p_sdp_raw_data == NULL) { + BTA_SdpCleanup(); return BTA_SDP_FAILURE; } #endif @@ -118,8 +120,15 @@ tBTA_SDP_STATUS BTA_SdpCleanup(void) bta_sys_deregister(BTA_ID_SDP); #if BTA_DYNAMIC_MEMORY == TRUE /* Free buffer for SDP configuration structure */ - osi_free(p_bta_sdp_cfg->p_sdp_db); - p_bta_sdp_cfg->p_sdp_db = NULL; + if (p_bta_sdp_cfg->p_sdp_db) { + osi_free(p_bta_sdp_cfg->p_sdp_db); + p_bta_sdp_cfg->p_sdp_db = NULL; + } + + if (p_bta_sdp_cfg->p_sdp_raw_data) { + osi_free(p_bta_sdp_cfg->p_sdp_raw_data); + p_bta_sdp_cfg->p_sdp_raw_data = NULL; + } #endif return BTA_SDP_SUCCESS; } diff --git a/components/bt/host/bluedroid/bta/sdp/bta_sdp_cfg.c b/components/bt/host/bluedroid/bta/sdp/bta_sdp_cfg.c index 322b25caec..423905bc50 100644 --- a/components/bt/host/bluedroid/bta/sdp/bta_sdp_cfg.c +++ b/components/bt/host/bluedroid/bta/sdp/bta_sdp_cfg.c @@ -30,16 +30,24 @@ #define BTA_SDP_DB_SIZE 1500 #endif +#ifndef BTA_SDP_RAW_DATA_SIZE +#define BTA_SDP_RAW_DATA_SIZE 1024 +#endif + #if BTA_DYNAMIC_MEMORY == FALSE +static UINT8 bta_sdp_raw_data[BTA_SDP_RAW_DATA_SIZE]; static UINT8 __attribute__ ((aligned(4))) bta_sdp_db_data[BTA_SDP_DB_SIZE]; #endif /* SDP configuration structure */ tBTA_SDP_CFG bta_sdp_cfg = { + BTA_SDP_RAW_DATA_SIZE, BTA_SDP_DB_SIZE, #if BTA_DYNAMIC_MEMORY == FALSE + bta_sdp_raw_data, (tSDP_DISCOVERY_DB *)bta_sdp_db_data /* The data buffer to keep SDP database */ #else + NULL, NULL #endif }; diff --git a/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c b/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c index 75cc0d7d51..a517efd6de 100644 --- a/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c +++ b/components/bt/host/bluedroid/btc/profile/std/sdp/btc_sdp.c @@ -1031,6 +1031,83 @@ static bool btc_sdp_remove_record_event(int id, int *p_sdp_handle) return result; } +static void btc_sdp_cb_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + switch (msg->act) { + case BTA_SDP_SEARCH_COMP_EVT: { + tBTA_SDP_SEARCH_COMP *src_search_comp = (tBTA_SDP_SEARCH_COMP *)p_src; + tBTA_SDP_SEARCH_COMP *dest_search_comp = (tBTA_SDP_SEARCH_COMP *)p_dest; + int record_count = src_search_comp->record_count; + + for (int i = 0; i < record_count; i++) { + bluetooth_sdp_record *src_record = &src_search_comp->records[i]; + bluetooth_sdp_record *dest_record = &dest_search_comp->records[i]; + // deep copy service name + uint32_t src_service_name_length = src_record->hdr.service_name_length; + char *src_service_name = src_record->hdr.service_name; + dest_record->hdr.service_name_length = 0; + dest_record->hdr.service_name = NULL; + if (src_service_name && src_service_name_length) { + char *service_name = (char *)osi_malloc(src_service_name_length + 1); + if (service_name) { + memcpy(service_name, src_service_name, src_service_name_length); + service_name[src_service_name_length] = '\0'; + + dest_record->hdr.service_name_length = src_service_name_length; + dest_record->hdr.service_name = service_name; + } else { + BTC_TRACE_ERROR("%s malloc service name failed, orig service name:%s", __func__, src_service_name); + } + } + + // deep copy user1_ptr fow RAW type + int src_user1_ptr_len = src_record->hdr.user1_ptr_len; + uint8_t *src_user1_ptr = src_record->hdr.user1_ptr; + dest_record->hdr.user1_ptr_len = 0; + dest_record->hdr.user1_ptr = NULL; + if (src_record->hdr.type == SDP_TYPE_RAW && src_user1_ptr && src_user1_ptr_len) { + uint8_t *user1_ptr = (uint8_t *)osi_malloc(src_user1_ptr_len); + if (user1_ptr) { + memcpy(user1_ptr, src_user1_ptr, src_user1_ptr_len); + + dest_record->hdr.user1_ptr_len = src_user1_ptr_len; + dest_record->hdr.user1_ptr = user1_ptr; + } else { + BTC_TRACE_ERROR("%s malloc user1_ptr failed", __func__); + } + } + } + + break; + } + default: + break; + } +} + +static void btc_sdp_cb_arg_deep_free(btc_msg_t *msg) +{ + switch (msg->act) { + case BTA_SDP_SEARCH_COMP_EVT: { + tBTA_SDP_SEARCH_COMP *search_comp = (tBTA_SDP_SEARCH_COMP *)msg->arg; + for (size_t i = 0; i < search_comp->record_count; i++) { + bluetooth_sdp_record *record = &search_comp->records[i]; + if (record->hdr.service_name) { + osi_free(record->hdr.service_name); + } + + if (record->hdr.user1_ptr) { + osi_free(record->hdr.user1_ptr); + } + } + + break; + } + default: + break; + } +} + static void btc_sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, void* user_data) { btc_msg_t msg; @@ -1062,7 +1139,7 @@ static void btc_sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, void* user_da msg.pid = BTC_PID_SDP; msg.act = event; - status = btc_transfer_context(&msg, p_data, sizeof(tBTA_SDP), NULL, NULL); + status = btc_transfer_context(&msg, p_data, sizeof(tBTA_SDP), btc_sdp_cb_arg_deep_copy, btc_sdp_cb_arg_deep_free); if (status != BT_STATUS_SUCCESS) { BTC_TRACE_ERROR("%s btc_transfer_context failed", __func__); @@ -1234,11 +1311,10 @@ static void btc_sdp_search(btc_sdp_args_t *arg) void btc_sdp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { - bluetooth_sdp_record **dst_record = &((btc_sdp_args_t *)p_dest)->create_record.record; - bluetooth_sdp_record *src_record = ((btc_sdp_args_t *)p_src)->create_record.record; - switch (msg->act) { - case BTC_SDP_ACT_CREATE_RECORD: + case BTC_SDP_ACT_CREATE_RECORD: { + bluetooth_sdp_record **dst_record = &((btc_sdp_args_t *)p_dest)->create_record.record; + bluetooth_sdp_record *src_record = ((btc_sdp_args_t *)p_src)->create_record.record; bluetooth_sdp_record *record = (bluetooth_sdp_record *)osi_calloc(get_sdp_record_size(src_record)); if (record) { copy_sdp_record(src_record, record); @@ -1249,6 +1325,7 @@ void btc_sdp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) *dst_record = record; break; + } default: break; } @@ -1256,15 +1333,15 @@ void btc_sdp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) void btc_sdp_arg_deep_free(btc_msg_t *msg) { - btc_sdp_args_t *arg = (btc_sdp_args_t *)msg->arg; - bluetooth_sdp_record *record = arg->create_record.record; - switch (msg->act) { - case BTC_SDP_ACT_CREATE_RECORD: + case BTC_SDP_ACT_CREATE_RECORD: { + btc_sdp_args_t *arg = (btc_sdp_args_t *)msg->arg; + bluetooth_sdp_record *record = arg->create_record.record; if (record) { osi_free(record); } break; + } default: break; } @@ -1321,23 +1398,11 @@ void btc_sdp_cb_handler(btc_msg_t *msg) sdp_local_param.search_allowed = true; param.search.status = p_data->sdp_search_comp.status; - if (param.search.status == ESP_SDP_SUCCESS) { - memcpy(param.search.remote_addr, p_data->sdp_search_comp.remote_addr, sizeof(BD_ADDR)); - memcpy(¶m.search.sdp_uuid, &p_data->sdp_search_comp.uuid, sizeof(tSDP_UUID)); - param.search.record_count = p_data->sdp_search_comp.record_count; - param.search.records = osi_malloc(sizeof(esp_bluetooth_sdp_record_t)*p_data->sdp_search_comp.record_count); - if (param.search.records != NULL) { - memcpy(param.search.records, p_data->sdp_search_comp.records, - sizeof(esp_bluetooth_sdp_record_t)*p_data->sdp_search_comp.record_count); - } else { - BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, event); - param.search.status = ESP_SDP_NO_RESOURCE; - } - } + memcpy(param.search.remote_addr, p_data->sdp_search_comp.remote_addr, sizeof(BD_ADDR)); + memcpy(¶m.search.sdp_uuid, &p_data->sdp_search_comp.uuid, sizeof(tSDP_UUID)); + param.search.record_count = p_data->sdp_search_comp.record_count; + param.search.records = (esp_bluetooth_sdp_record_t *)p_data->sdp_search_comp.records; btc_sdp_cb_to_app(ESP_SDP_SEARCH_COMP_EVT, ¶m); - if (param.search.records != NULL) { - osi_free(param.search.records); - } break; case BTA_SDP_CREATE_RECORD_USER_EVT: param.create_record.status = p_data->sdp_create_record.status; @@ -1362,6 +1427,8 @@ void btc_sdp_cb_handler(btc_msg_t *msg) BTC_TRACE_DEBUG("%s: Unhandled event (%d)!", __func__, msg->act); break; } + + btc_sdp_cb_arg_deep_free(msg); } #endif ///defined BTC_SDP_COMMON_INCLUDED && BTC_SDP_COMMON_INCLUDED == TRUE