From 7a4e7066f95844a24486951d24a648e4dcb0e639 Mon Sep 17 00:00:00 2001 From: Eric Albers Date: Thu, 15 Apr 2021 09:07:45 -0400 Subject: [PATCH] Add setMTU function to BLEClient.cpp/.h (#4999) The current implementation has a getMTU function which returns the mtu sent in a message. This function allows you to set the MTU value on the connected device, it first sets the MTU locally by calling esp_ble_gatt_set_local_mtu. It then calls esp_ble_gattc_send_mtu_req to have the connected device also change its MTU size. --- libraries/BLE/src/BLEClient.cpp | 34 +++++++++++++++++++++++++++++++++ libraries/BLE/src/BLEClient.h | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp index 18e06f2a..6f099c40 100644 --- a/libraries/BLE/src/BLEClient.cpp +++ b/libraries/BLE/src/BLEClient.cpp @@ -10,6 +10,7 @@ #include #include #include +#include // ESP32 BLE #include "BLEClient.h" #include "BLEUtils.h" #include "BLEService.h" @@ -545,6 +546,39 @@ uint16_t BLEClient::getMTU() { return m_mtu; } + +/** + @brief Set the local and remote MTU size. + Should be called once after client connects if MTU size needs to be changed. + @return bool indicating if MTU was successfully set locally and on remote. +*/ +bool BLEClient::setMTU(uint16_t mtu) +{ + esp_err_t err = esp_ble_gatt_set_local_mtu(mtu); //First must set local MTU value. + if (err == ESP_OK) + { + err = esp_ble_gattc_send_mtu_req(m_gattc_if,m_conn_id); //Once local is set successfully set remote size + if (err!=ESP_OK) + { + log_e("Error setting send MTU request MTU: %d err=%d", mtu,err); + return false; + } + } + else + { + log_e("can't set local mtu value: %d", mtu); + return false; + } + log_v("<< setLocalMTU"); + + m_mtu = mtu; //successfully changed + + return true; +} + + + + /** * @brief Return a string representation of this client. * @return A string representation of this client. diff --git a/libraries/BLE/src/BLEClient.h b/libraries/BLE/src/BLEClient.h index faf2f0c3..2550274c 100644 --- a/libraries/BLE/src/BLEClient.h +++ b/libraries/BLE/src/BLEClient.h @@ -57,7 +57,8 @@ public: uint16_t getConnId(); esp_gatt_if_t getGattcIf(); uint16_t getMTU(); - + bool setMTU(uint16_t mtu); + uint16_t m_appId; private: friend class BLEDevice;