From 3b432735c6e81c163627637e62c343c33cd3ceb4 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Mon, 21 Nov 2016 22:38:00 +0800 Subject: [PATCH] component/bt : fix advertising data and refer bug --- components/bt/bluedroid/api/esp_gap_ble_api.c | 4 +++ .../btc/profile/std/gap/btc_gap_ble.c | 15 +++++----- .../btc/profile/std/gatt/btc_gatt_util.c | 28 +++++++++++++++++++ .../profile/std/gatt/include/btc_gatt_util.h | 1 + examples/09_gatt_server/main/gatts_demo.c | 18 +++++++----- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 72b3e1c26c..d6c819bacb 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -36,6 +36,10 @@ esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data) return ESP_ERR_INVALID_ARG; } + if (adv_data->service_uuid_len & 0xf) { //not 16*n + return ESP_ERR_INVALID_ARG; + } + msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GAP_BLE; msg.act = BTC_GAP_BLE_ACT_CFG_ADV_DATA; diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index d6c88cec4f..070060d83b 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -19,6 +19,7 @@ #include "btc_task.h" #include "btc_manage.h" #include "btc_gap_ble.h" +#include "btc_gatt_util.h" #include "esp_bt_defs.h" #include "esp_gap_ble_api.h" @@ -204,20 +205,18 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT { tBT_UUID bt_uuid; - memcpy(&bt_uuid.uu, p_adv_data->p_service_uuid + position, LEN_UUID_128); - bt_uuid.len = p_adv_data->service_uuid_len; + btc128_to_bta_uuid(&bt_uuid, p_adv_data->p_service_uuid + position); + switch(bt_uuid.len) { case (LEN_UUID_16): { if (NULL == bta_adv_data->p_services) { - bta_adv_data->p_services = - GKI_getbuf(sizeof(tBTA_BLE_SERVICE)); + bta_adv_data->p_services = GKI_getbuf(sizeof(tBTA_BLE_SERVICE)); bta_adv_data->p_services->list_cmpl = FALSE; bta_adv_data->p_services->num_service = 0; - bta_adv_data->p_services->p_uuid = - GKI_getbuf(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_16); + bta_adv_data->p_services->p_uuid = GKI_getbuf(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_16); p_uuid_out16 = bta_adv_data->p_services->p_uuid; } @@ -246,7 +245,7 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT if (NULL != bta_adv_data->p_service_32b->p_uuid) { - LOG_DEBUG("%s - In 32-UUID_data", __FUNCTION__); + LOG_ERROR("%s - In 32-UUID_data", __FUNCTION__); mask |= BTM_BLE_AD_BIT_SERVICE_32; ++bta_adv_data->p_service_32b->num_service; *p_uuid_out32++ = bt_uuid.uu.uuid32; @@ -263,7 +262,7 @@ static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DAT GKI_getbuf(sizeof(tBTA_BLE_128SERVICE)); if (NULL != bta_adv_data->p_services_128b) { - LOG_DEBUG("%s - In 128-UUID_data", __FUNCTION__); + LOG_ERROR("%s - In 128-UUID_data", __FUNCTION__); mask |= BTM_BLE_AD_BIT_SERVICE_128; memcpy(bta_adv_data->p_services_128b->uuid128, bt_uuid.uu.uuid128, LEN_UUID_128); diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatt_util.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatt_util.c index c4693380af..619891ce20 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatt_util.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatt_util.c @@ -40,6 +40,34 @@ int uuidType(unsigned char* p_uuid) return LEN_UUID_128; } +int btc128_to_bta_uuid(tBT_UUID *p_dest, uint8_t *p_src) +{ + int i = 0; + + p_dest->len = uuidType(p_src); + + switch (p_dest->len) + { + case LEN_UUID_16: + p_dest->uu.uuid16 = (p_src[13] << 8) + p_src[12]; + break; + + case LEN_UUID_32: + p_dest->uu.uuid32 = (p_src[13] << 8) + p_src[12]; + p_dest->uu.uuid32 += (p_src[15] << 24) + (p_src[14] << 16); + break; + + case LEN_UUID_128: + for(i = 0; i != 16; ++i) + p_dest->uu.uuid128[i] = p_src[i]; + break; + + default: + LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len); + break; + } +} + /******************************************************************************* * BTC -> BTA conversion functions *******************************************************************************/ diff --git a/components/bt/bluedroid/btc/profile/std/gatt/include/btc_gatt_util.h b/components/bt/bluedroid/btc/profile/std/gatt/include/btc_gatt_util.h index 1b79da540a..18f9a030c9 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/include/btc_gatt_util.h +++ b/components/bt/bluedroid/btc/profile/std/gatt/include/btc_gatt_util.h @@ -7,6 +7,7 @@ #include "esp_gatt_defs.h" #include "esp_gattc_api.h" +int btc128_to_bta_uuid(tBT_UUID *p_dest, uint8_t *p_src); void btc_to_bta_uuid(tBT_UUID *p_dest, esp_bt_uuid_t *p_src); void btc_to_bta_gatt_id(tBTA_GATT_ID *p_dest, esp_gatt_id_t *p_src); void btc_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, esp_gatt_srvc_id_t *p_src); diff --git a/examples/09_gatt_server/main/gatts_demo.c b/examples/09_gatt_server/main/gatts_demo.c index e3634a79de..73c36c9c6b 100644 --- a/examples/09_gatt_server/main/gatts_demo.c +++ b/examples/09_gatt_server/main/gatts_demo.c @@ -39,8 +39,12 @@ #define TEST_MANUFACTURER_DATA_LEN 17 static uint16_t test_service_uuid = GATTS_SERVICE_UUID_TEST; -static uint8_t test_service_uuid128[16] = { 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +static uint8_t test_service_uuid128[32] = { + /* LSB <--------------------------------------------------------------------------------> MSB */ + //first uuid, 16bit, [12],[13] is the value + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0x00, 0x00, + //second uuid, 32bit, [12], [13], [14], [15] is the value + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0xAB, 0xCD,}; static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x12, 0x23, 0x45, 0x56}; static esp_ble_adv_data_t test_adv_data = { @@ -49,14 +53,14 @@ static esp_ble_adv_data_t test_adv_data = { .include_txpower = true, .min_interval = 0x20, .max_interval = 0x40, - .appearance = 0x0, - .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN, - .p_manufacturer_data = NULL, // &test_manufacturer[0], + .appearance = 0x00, + .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN, + .p_manufacturer_data = NULL, //&test_manufacturer[0], .service_data_len = 0, .p_service_data = NULL, - .service_uuid_len = 2, + .service_uuid_len = 32, .p_service_uuid = test_service_uuid128, - .flag = 0, + .flag = 0x2, }; static esp_ble_adv_params_t test_adv_params = { -- 2.40.0