From f1c4eda37394c3b2767fd116397069adaadd30c8 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Thu, 24 Aug 2017 19:54:41 +0800 Subject: [PATCH] Component/bt: add set local MTU API - add esp_ble_gatt_set_local_mtu( ) API - modify esp_ble_gattc_config_mtu(esp_gatt_if_t gattc_if, uint16_t conn_id) - modify gattc demo --- .../bt/bluedroid/api/esp_gatt_common_api.c | 49 +++++++++++++++++++ components/bt/bluedroid/api/esp_gattc_api.c | 7 +-- .../api/include/esp_gatt_common_api.h | 46 +++++++++++++++++ .../bt/bluedroid/api/include/esp_gattc_api.h | 12 ++--- components/bt/bluedroid/bta/dm/bta_dm_main.c | 2 +- .../bt/bluedroid/bta/gatt/bta_gatt_common.c | 30 ++++++++++++ .../bt/bluedroid/bta/gatt/bta_gattc_act.c | 2 +- .../bt/bluedroid/bta/gatt/bta_gattc_api.c | 4 +- .../bt/bluedroid/bta/include/bta_gatt_api.h | 4 +- .../bluedroid/bta/include/bta_gatt_common.h | 36 ++++++++++++++ .../bt/bluedroid/bta/include/bta_gattc_int.h | 1 - components/bt/bluedroid/btc/core/btc_main.c | 12 +++++ .../bt/bluedroid/btc/include/btc_main.h | 9 ++++ .../btc/profile/std/gatt/btc_gattc.c | 2 +- .../btc/profile/std/include/btc_gattc.h | 1 - components/bt/bluedroid/stack/gatt/gatt_api.c | 3 +- .../bt/bluedroid/stack/gatt/gatt_main.c | 19 +++++-- components/bt/bluedroid/stack/gatt/gatt_sr.c | 6 +-- .../bluedroid/stack/gatt/include/gatt_int.h | 7 +++ .../bt/bluedroid/stack/include/gatt_api.h | 2 +- .../bluetooth/gatt_client/main/gattc_demo.c | 10 +++- .../main/example_ble_sec_gattc_demo.c | 7 ++- .../bluetooth/gatt_server/main/gatts_demo.c | 5 ++ .../main/gattc_multi_connect.c | 12 +++-- 24 files changed, 249 insertions(+), 39 deletions(-) create mode 100644 components/bt/bluedroid/api/esp_gatt_common_api.c create mode 100644 components/bt/bluedroid/api/include/esp_gatt_common_api.h create mode 100644 components/bt/bluedroid/bta/gatt/bta_gatt_common.c create mode 100644 components/bt/bluedroid/bta/include/bta_gatt_common.h diff --git a/components/bt/bluedroid/api/esp_gatt_common_api.c b/components/bt/bluedroid/api/esp_gatt_common_api.c new file mode 100644 index 000000000..e184325a1 --- /dev/null +++ b/components/bt/bluedroid/api/esp_gatt_common_api.c @@ -0,0 +1,49 @@ +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_gatt_common_api.h" +#include "esp_bt_main.h" +#include "esp_gatt_defs.h" +#include "btc_main.h" + +/** + * @brief This function is called to set local MTU, + * the function is called before BLE connection. + * + * @param[in] mtu: the size of MTU. + * + * @return + * - ESP_OK: success + * - other: failed + * + */ +esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu) +{ + btc_msg_t msg; + btc_ble_main_args_t arg; + + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + if ((mtu < ESP_GATT_DEF_BLE_MTU_SIZE) || (mtu > ESP_GATT_MAX_MTU_SIZE)) { + return ESP_GATT_ILLEGAL_PARAMETER; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MAIN_INIT; + msg.act = BTC_GATT_ACT_SET_LOCAL_MTU; + arg.set_mtu.mtu = mtu; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_main_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} \ No newline at end of file diff --git a/components/bt/bluedroid/api/esp_gattc_api.c b/components/bt/bluedroid/api/esp_gattc_api.c index b58c408f8..75970ad3f 100644 --- a/components/bt/bluedroid/api/esp_gattc_api.c +++ b/components/bt/bluedroid/api/esp_gattc_api.c @@ -99,22 +99,17 @@ esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id) return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -esp_err_t esp_ble_gattc_config_mtu (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t mtu) +esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id) { btc_msg_t msg; btc_ble_gattc_args_t arg; ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); - - if ((mtu < ESP_GATT_DEF_BLE_MTU_SIZE) || (mtu > ESP_GATT_MAX_MTU_SIZE)) { - return ESP_GATT_ILLEGAL_PARAMETER; - } msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GATTC; msg.act = BTC_GATTC_ACT_CFG_MTU; arg.cfg_mtu.conn_id = BTC_GATT_CREATE_CONN_ID(gattc_if, conn_id); - arg.cfg_mtu.mtu = mtu; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gattc_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } diff --git a/components/bt/bluedroid/api/include/esp_gatt_common_api.h b/components/bt/bluedroid/api/include/esp_gatt_common_api.h new file mode 100644 index 000000000..33bc11250 --- /dev/null +++ b/components/bt/bluedroid/api/include/esp_gatt_common_api.h @@ -0,0 +1,46 @@ +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_err.h" +#include "esp_bt_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Maximum Transmission Unit used in GATT +#define ESP_GATT_DEF_BLE_MTU_SIZE 23 /* relate to GATT_DEF_BLE_MTU_SIZE in gatt_api.h */ + +// Maximum Transmission Unit allowed in GATT +#define ESP_GATT_MAX_MTU_SIZE 517 /* relate to GATT_MAX_MTU_SIZE in gatt_api.h */ + +/** + * @brief This function is called to set local MTU, + * the function is called before BLE connection. + * + * @param[in] mtu: the size of MTU. + * + * @return + * - ESP_OK: success + * - other: failed + * + */ +extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu); + +#ifdef __cplusplus +} +#endif diff --git a/components/bt/bluedroid/api/include/esp_gattc_api.h b/components/bt/bluedroid/api/include/esp_gattc_api.h index f28736e3b..b950d438f 100644 --- a/components/bt/bluedroid/api/include/esp_gattc_api.h +++ b/components/bt/bluedroid/api/include/esp_gattc_api.h @@ -68,11 +68,6 @@ typedef enum { ESP_GATTC_DISCONNECT_EVT = 41, /*!< When the ble physical connection disconnected, the event comes */ } esp_gattc_cb_event_t; -/// Maximum Transmission Unit used in GATT -#define ESP_GATT_DEF_BLE_MTU_SIZE 23 - -/// Maximum Transmission Unit allowed in GATT -#define ESP_GATT_MAX_MTU_SIZE 517 /** * @brief Gatt client callback parameters union @@ -355,18 +350,19 @@ esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id); /** * @brief Configure the MTU size in the GATT channel. This can be done - * only once per connection. + * only once per connection. Before using, use esp_ble_gatt_set_local_mtu() + * to configure the local MTU size. + * * * @param[in] gattc_if: Gatt client access interface. * @param[in] conn_id: connection ID. - * @param[in] mtu: desired MTU size to use. * * @return * - ESP_OK: success * - other: failed * */ -esp_err_t esp_ble_gattc_config_mtu (esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t mtu); +esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id); /** diff --git a/components/bt/bluedroid/bta/dm/bta_dm_main.c b/components/bt/bluedroid/bta/dm/bta_dm_main.c index 3d0b84ee6..2f01c248f 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_main.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_main.c @@ -53,7 +53,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = { bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */ bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */ bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */ - bta_dm_close_acl, /* 10 BTA_DM_API_ADD_DEVICE_EVT */ + bta_dm_close_acl, /* 10 BTA_DM_API_REMOVE_ACL_EVT */ #if (SMP_INCLUDED == TRUE) /* security API events */ bta_dm_bond, /* 11 BTA_DM_API_BOND_EVT */ diff --git a/components/bt/bluedroid/bta/gatt/bta_gatt_common.c b/components/bt/bluedroid/bta/gatt/bta_gatt_common.c new file mode 100644 index 000000000..3e4a0f73d --- /dev/null +++ b/components/bt/bluedroid/bta/gatt/bta_gatt_common.c @@ -0,0 +1,30 @@ +/****************************************************************************** +* Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + ******************************************************************************/ + +/****************************************************************************** + * + * This file contains the action functions for gatts and gattc. + * + * + ******************************************************************************/ + +#include "bta_gatt_common.h" +#include "gatt_int.h" + +void BTA_GATT_SetLocalMTU(uint16_t mtu) +{ + gatt_set_local_mtu(mtu); +} \ No newline at end of file diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c index c8283936a..2738b52ad 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c @@ -901,7 +901,7 @@ void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) tBTA_GATT_STATUS status; if (bta_gattc_enqueue(p_clcb, p_data)) { - status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu); + status = GATTC_ConfigureMTU (p_clcb->bta_conn_id); /* if failed, return callback here */ if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) { diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c index bca1c080d..eaaf77ccc 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c @@ -228,7 +228,7 @@ void BTA_GATTC_Close(UINT16 conn_id) ** Returns void ** *******************************************************************************/ -void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu) +void BTA_GATTC_ConfigureMTU (UINT16 conn_id) { tBTA_GATTC_API_CFG_MTU *p_buf; @@ -236,8 +236,6 @@ void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu) p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT; p_buf->hdr.layer_specific = conn_id; - p_buf->mtu = mtu; - bta_sys_sendmsg(p_buf); } return; diff --git a/components/bt/bluedroid/bta/include/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta_gatt_api.h index e6dc5e8db..4b4945aa5 100644 --- a/components/bt/bluedroid/bta/include/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta_gatt_api.h @@ -1157,12 +1157,12 @@ extern void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start); ** only once per connection. ** ** Parameters conn_id: connection ID. -** mtu: desired MTU size to use. +** ** ** Returns void ** *******************************************************************************/ -extern void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu); +extern void BTA_GATTC_ConfigureMTU (UINT16 conn_id); /******************************************************************************* ** BTA GATT Server API diff --git a/components/bt/bluedroid/bta/include/bta_gatt_common.h b/components/bt/bluedroid/bta/include/bta_gatt_common.h new file mode 100644 index 000000000..f6bc884b6 --- /dev/null +++ b/components/bt/bluedroid/bta/include/bta_gatt_common.h @@ -0,0 +1,36 @@ +/****************************************************************************** +* Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + ******************************************************************************/ + +/****************************************************************************** + * + * This file contains the action functions for gatts and gattc. + * + * + ******************************************************************************/ + +#include "bt_types.h" + + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void BTA_GATT_SetLocalMTU(uint16_t mtu); + +#ifdef __cplusplus +} +#endif diff --git a/components/bt/bluedroid/bta/include/bta_gattc_int.h b/components/bt/bluedroid/bta/include/bta_gattc_int.h index 3b71438e8..6ea09ef8c 100644 --- a/components/bt/bluedroid/bta/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/include/bta_gattc_int.h @@ -184,7 +184,6 @@ typedef struct { typedef struct { BT_HDR hdr; - UINT16 mtu; } tBTA_GATTC_API_CFG_MTU; typedef struct { diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index 87892b846..2317074c8 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -20,6 +20,7 @@ #include "btc_config.h" #include "alarm.h" #include "btc_ble_storage.h" +#include "bta_gatt_common.h" static future_t *main_future[BTC_MAIN_FUTURE_NUM]; @@ -73,6 +74,11 @@ static void btc_deinit_bluetooth(void) future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS); } +static void btc_set_local_mtu(uint16_t mtu) +{ + BTA_GATT_SetLocalMTU(mtu); +} + void btc_main_call_handler(btc_msg_t *msg) { LOG_DEBUG("%s act %d\n", __func__, msg->act); @@ -90,6 +96,12 @@ void btc_main_call_handler(btc_msg_t *msg) case BTC_MAIN_ACT_DISABLE: btc_disable_bluetooth(); break; + case BTC_GATT_ACT_SET_LOCAL_MTU: + { + btc_ble_main_args_t *arg = (btc_ble_main_args_t *)(msg->arg); + btc_set_local_mtu(arg->set_mtu.mtu); + break; + } default: LOG_ERROR("%s UNKNOWN ACT %d\n", __func__, msg->act); break; diff --git a/components/bt/bluedroid/btc/include/btc_main.h b/components/bt/bluedroid/btc/include/btc_main.h index b95ae0bbe..523e1bb98 100644 --- a/components/bt/bluedroid/btc/include/btc_main.h +++ b/components/bt/bluedroid/btc/include/btc_main.h @@ -26,6 +26,7 @@ typedef enum { BTC_MAIN_ACT_DEINIT, BTC_MAIN_ACT_ENABLE, BTC_MAIN_ACT_DISABLE, + BTC_GATT_ACT_SET_LOCAL_MTU, } btc_main_act_t; typedef enum { @@ -60,5 +61,13 @@ bt_status_t btc_init_bluetooth(future_t *future); void btc_deinit_bluetooth(future_t *future); #endif +/* btc_ble_gattc_args_t */ +typedef union { + //BTC_GATT_ACT_SET_LOCAL_MTU, + struct set_mtu_arg { + uint16_t mtu; + } set_mtu; +} btc_ble_main_args_t; + void btc_main_call_handler(btc_msg_t *msg); #endif /* __BTC_BT_MAIN_H__ */ diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index 62d6ed4e0..67098e8bf 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -181,7 +181,7 @@ static void btc_gattc_close(btc_ble_gattc_args_t *arg) static void btc_gattc_cfg_mtu(btc_ble_gattc_args_t *arg) { - BTA_GATTC_ConfigureMTU (arg->cfg_mtu.conn_id, arg->cfg_mtu.mtu); + BTA_GATTC_ConfigureMTU (arg->cfg_mtu.conn_id); } static void btc_gattc_search_service(btc_ble_gattc_args_t *arg) diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h b/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h index 1374dc0a7..d1e0e9680 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gattc.h @@ -68,7 +68,6 @@ typedef union { //BTC_GATTC_ACT_CFG_MTU, struct cfg_mtu_arg { uint16_t conn_id; - uint16_t mtu; } cfg_mtu; //BTC_GATTC_ACT_SEARCH_SERVICE, struct search_srvc_arg { diff --git a/components/bt/bluedroid/stack/gatt/gatt_api.c b/components/bt/bluedroid/stack/gatt/gatt_api.c index f8fffc2e6..1c9c6df02 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_api.c +++ b/components/bt/bluedroid/stack/gatt/gatt_api.c @@ -791,7 +791,7 @@ tGATT_STATUS GATTS_GetAttributeValue(UINT16 attr_handle, UINT16 *length, UINT8 * ** Returns GATT_SUCCESS if command started successfully. ** *******************************************************************************/ -tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu) +tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id) { UINT8 ret = GATT_NO_RESOURCES; tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id); @@ -800,6 +800,7 @@ tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu) tGATT_REG *p_reg = gatt_get_regcb(gatt_if); tGATT_CLCB *p_clcb; + uint16_t mtu = gatt_get_local_mtu(); GATT_TRACE_API ("GATTC_ConfigureMTU conn_id=%d mtu=%d", conn_id, mtu ); diff --git a/components/bt/bluedroid/stack/gatt/gatt_main.c b/components/bt/bluedroid/stack/gatt/gatt_main.c index bb226ba52..5ebd2ec0b 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_main.c +++ b/components/bt/bluedroid/stack/gatt/gatt_main.c @@ -79,6 +79,8 @@ static const tL2CAP_APPL_INFO dyn_info = { tGATT_CB gatt_cb; #endif +tGATT_DEFAULT gatt_default; + /******************************************************************************* ** ** Function gatt_init @@ -136,7 +138,8 @@ void gatt_init (void) #if (GATTS_INCLUDED == TRUE) gatt_profile_db_init(); #endif ///GATTS_INCLUDED == TRUE - + //init local MTU size + gatt_default.local_mtu = GATT_MAX_MTU_SIZE; } @@ -596,7 +599,7 @@ static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 lcid, UINT16 /* Send L2CAP config req */ memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); cfg.mtu_present = TRUE; - cfg.mtu = GATT_MAX_MTU_SIZE; + cfg.mtu = gatt_default.local_mtu; L2CA_ConfigReq(lcid, &cfg); } @@ -632,7 +635,7 @@ static void gatt_l2cif_connect_cfm_cback(UINT16 lcid, UINT16 result) /* Send L2CAP config req */ memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); cfg.mtu_present = TRUE; - cfg.mtu = GATT_MAX_MTU_SIZE; + cfg.mtu = gatt_default.local_mtu; L2CA_ConfigReq(lcid, &cfg); } /* else initiating connection failure */ @@ -1169,4 +1172,14 @@ tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb) return ch_state; } +uint16_t gatt_get_local_mtu(void) +{ + return gatt_default.local_mtu; +} + +void gatt_set_local_mtu(uint16_t mtu) +{ + gatt_default.local_mtu = mtu; +} + #endif /* BLE_INCLUDED */ diff --git a/components/bt/bluedroid/stack/gatt/gatt_sr.c b/components/bt/bluedroid/stack/gatt/gatt_sr.c index cf6055e8c..08591334f 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_sr.c +++ b/components/bt/bluedroid/stack/gatt/gatt_sr.c @@ -941,14 +941,12 @@ static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data) /* mtu must be greater than default MTU which is 23/48 */ if (mtu < GATT_DEF_BLE_MTU_SIZE) { p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE; - } else if (mtu > GATT_MAX_MTU_SIZE) { - p_tcb->payload_size = GATT_MAX_MTU_SIZE; + } else if (mtu > gatt_default.local_mtu) { + p_tcb->payload_size = gatt_default.local_mtu; } else { p_tcb->payload_size = mtu; } - GATT_TRACE_ERROR("MTU request PDU with MTU size %d\n", p_tcb->payload_size); - l2cble_set_fixed_channel_tx_data_length(p_tcb->peer_bda, L2CAP_ATT_CID, p_tcb->payload_size); if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL) { diff --git a/components/bt/bluedroid/stack/gatt/include/gatt_int.h b/components/bt/bluedroid/stack/gatt/include/gatt_int.h index eee0d2858..9d836597a 100644 --- a/components/bt/bluedroid/stack/gatt/include/gatt_int.h +++ b/components/bt/bluedroid/stack/gatt/include/gatt_int.h @@ -537,6 +537,9 @@ typedef struct { } tGATT_CB; +typedef struct{ + UINT16 local_mtu; +} tGATT_DEFAULT; #define GATT_SIZE_OF_SRV_CHG_HNDL_RANGE 4 @@ -544,6 +547,8 @@ typedef struct { extern "C" { #endif +extern tGATT_DEFAULT gatt_default; + /* Global GATT data */ #if GATT_DYNAMIC_MEMORY == FALSE extern tGATT_CB gatt_cb; @@ -741,4 +746,6 @@ extern void gatts_update_srv_list_elem(UINT8 i_sreg, UINT16 handle, BOOLEAN is_p extern tBT_UUID *gatts_get_service_uuid (tGATT_SVC_DB *p_db); extern void gatt_reset_bgdev_list(void); +extern uint16_t gatt_get_local_mtu(void); +extern void gatt_set_local_mtu(uint16_t mtu); #endif diff --git a/components/bt/bluedroid/stack/include/gatt_api.h b/components/bt/bluedroid/stack/include/gatt_api.h index 76d8c4659..946e5fee4 100644 --- a/components/bt/bluedroid/stack/include/gatt_api.h +++ b/components/bt/bluedroid/stack/include/gatt_api.h @@ -943,7 +943,7 @@ tGATT_STATUS GATTS_GetAttributeValue(UINT16 attr_handle, UINT16 *length, UINT8 * ** Returns GATT_SUCCESS if command started successfully. ** *******************************************************************************/ -extern tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu); +extern tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id); /******************************************************************************* ** diff --git a/examples/bluetooth/gatt_client/main/gattc_demo.c b/examples/bluetooth/gatt_client/main/gattc_demo.c index dc83d7f10..76b809ede 100644 --- a/examples/bluetooth/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/gatt_client/main/gattc_demo.c @@ -36,6 +36,7 @@ #include "esp_gattc_api.h" #include "esp_gatt_defs.h" #include "esp_bt_main.h" +#include "esp_gatt_common_api.h" #define GATTC_TAG "GATTC_DEMO" #define REMOTE_SERVICE_UUID 0x00FF @@ -117,7 +118,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret); } break; - case ESP_GATTC_CONNECT_EVT: + case ESP_GATTC_CONNECT_EVT:{ //p_data->connect.status always be ESP_GATT_OK ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d, status %d", conn_id, gattc_if, p_data->connect.status); conn_id = p_data->connect.conn_id; @@ -125,11 +126,12 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t)); - esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, conn_id, 200); + esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); } break; + } case ESP_GATTC_OPEN_EVT: if (param->open.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status); @@ -404,6 +406,10 @@ void app_main() if (ret){ ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret); } + esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500); + if (local_mtu_ret){ + ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret); + } } diff --git a/examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c b/examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c index aa2ce9dfe..2964602c9 100644 --- a/examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c +++ b/examples/bluetooth/gatt_security_client/main/example_ble_sec_gattc_demo.c @@ -35,6 +35,7 @@ #include "esp_gattc_api.h" #include "esp_gatt_defs.h" #include "esp_bt_main.h" +#include "esp_gatt_common_api.h" #define GATTC_TAG "SEC_GATTC_DEMO" #define REMOTE_SERVICE_UUID 0x1809 @@ -164,7 +165,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t)); - esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, conn_id, 200); + esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); } @@ -455,6 +456,10 @@ void app_main() if (ret){ ESP_LOGE(GATTC_TAG, "%s gattc app register error, error code = %x\n", __func__, ret); } + ret = esp_ble_gatt_set_local_mtu(200); + if (ret){ + ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", ret); + } } diff --git a/examples/bluetooth/gatt_server/main/gatts_demo.c b/examples/bluetooth/gatt_server/main/gatts_demo.c index 2782c026d..edbe74394 100644 --- a/examples/bluetooth/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/gatt_server/main/gatts_demo.c @@ -38,6 +38,7 @@ #include "esp_bt_defs.h" #include "esp_bt_main.h" #include "esp_bt_main.h" +#include "esp_gatt_common_api.h" #include "sdkconfig.h" @@ -722,6 +723,10 @@ void app_main() ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret); return; } + esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500); + if (local_mtu_ret){ + ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret); + } return; } diff --git a/examples/bluetooth/gattc_multi_connect/main/gattc_multi_connect.c b/examples/bluetooth/gattc_multi_connect/main/gattc_multi_connect.c index cc6a2db69..adeb070f5 100644 --- a/examples/bluetooth/gattc_multi_connect/main/gattc_multi_connect.c +++ b/examples/bluetooth/gattc_multi_connect/main/gattc_multi_connect.c @@ -39,6 +39,7 @@ #include "esp_gattc_api.h" #include "esp_gatt_defs.h" #include "esp_bt_main.h" +#include "esp_gatt_common_api.h" #define GATTC_TAG "GATTC_MULTIPLE_DEMO" #define REMOTE_SERVICE_UUID 0x00FF @@ -166,7 +167,7 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); - esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200); + esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); } @@ -306,7 +307,7 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); - esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200); + esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); } @@ -439,7 +440,7 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d", p_data->open.conn_id, gattc_if, p_data->open.status, p_data->open.mtu); ESP_LOGI(GATTC_TAG, "REMOTE BDA:"); esp_log_buffer_hex(GATTC_TAG, p_data->open.remote_bda, sizeof(esp_bd_addr_t)); - esp_err_t mtu_ret = esp_ble_gattc_config_mtu (gattc_if, p_data->open.conn_id, 200); + esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->open.conn_id); if (mtu_ret){ ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret); } @@ -748,6 +749,11 @@ void app_main() ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret); return; } + ret = esp_ble_gatt_set_local_mtu(200); + if (ret){ + ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", ret); + } + }