diff --git a/README.md b/README.md index b2d48b13..938897c5 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,6 @@ https://github.com/stm32duino/wiki/wiki/STM32duinoBLE#stm32duinoble-with-x-nucle For more information about ArduinoBLE library please visit the official web page at: https://github.com/arduino-libraries/ArduinoBLE - -# Configuration -STM32Cube_WPAN has several configuration options, which are set in the `app_conf.h`. -This package has a default configuration named `app_conf_default.h`. -The user can include the file `app_conf_custom.h` to customize the ble application. Options wrapped in `#ifndef, #endif` in `app_conf_default.h` can be overwritten. Additional options can be added. - ## License ``` @@ -41,3 +35,12 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ``` + +# Configuration +STM32Cube_WPAN has several configuration options, which are set in the `app_conf.h`. +This package has a default configuration named `app_conf_default.h`. +The user can include the file `app_conf_custom.h` to customize the ble application. Options wrapped in `#ifndef, #endif` in `app_conf_default.h` can be overwritten. Additional options can be added. +## HCI data length +By default the data length (max payload per BLE packet) is set to 27 bytes. This can cause fragmentation when transmitting large characteristics using a large ATT_MTU. +To increase the data length user must define `CFG_BLE_ENABLE_SET_DATA_LENGTH` (in `app_conf_default.h`). Further more, the wanted data length must bbe set in same define - eg: `#define CFG_BLE_ENABLE_SET_DATA_LENGTH 251`. Valid range: 27 --> 251. +**Note: if this is enabled the pheripheral will attempt to increase the HCI data length with every connected device! There is no guarantee all BLE devices support all sizes!** diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index 6c502068..11ac3eb7 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -256,6 +256,11 @@ void ATTClass::addConnection(uint16_t handle, uint8_t role, uint8_t peerBdaddrTy if (_eventHandlers[BLEConnected]) { _eventHandlers[BLEConnected](BLEDevice(peerBdaddrType, peerBdaddr)); } + #ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH + #if CFG_BLE_ENABLE_SET_DATA_LENGTH < 252 && CFG_BLE_ENABLE_SET_DATA_LENGTH > 26 + HCI.hciSetDataLength(handle, CFG_BLE_ENABLE_SET_DATA_LENGTH, 2120); + #endif // CFG_BLE_ENABLE_SET_DATA_LENGTH < 252 && CFG_BLE_ENABLE_SET_DATA_LENGTH > 26 + #endif // CFG_BLE_ENABLE_SET_DATA_LENGTH } void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]) diff --git a/src/utility/HCI.cpp b/src/utility/HCI.cpp index 96b42537..6f9c9737 100644 --- a/src/utility/HCI.cpp +++ b/src/utility/HCI.cpp @@ -672,6 +672,22 @@ void HCIClass::setTransport(HCITransportInterface *HCITransport) _HCITransport = HCITransport; } +#ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH +int HCIClass::hciSetDataLength(uint16_t connectionHandle, uint16_t txOctects, uint16_t txTime){ + const uint8_t payload_len = 6; + const uint16_t opcode = 0x2022; + uint8_t cmd_buffer[BLE_CMD_MAX_PARAM_LEN]; + hci_le_set_data_length_cp0 *cp0 = (hci_le_set_data_length_cp0*)(cmd_buffer); + + // create payload + cp0->Connection_Handle = connectionHandle; + cp0->TxOctets = txOctects; + cp0->TxTime = txTime; + + return sendCommand(opcode, payload_len, cmd_buffer); +} +#endif // CFG_BLE_ENABLE_SET_DATA_LENGTH + #if !defined(FAKE_HCI) HCIClass HCIObj; HCIClass& HCI = HCIObj; diff --git a/src/utility/HCI.h b/src/utility/HCI.h index 0efd8125..034950f8 100644 --- a/src/utility/HCI.h +++ b/src/utility/HCI.h @@ -23,6 +23,18 @@ #include #include "HCITransport.h" +#if __has_include("app_conf_custom.h") +#include "app_conf_custom.h" +#endif + +#define BLE_CMD_MAX_PARAM_LEN 255 + +struct hci_le_set_data_length_cp0{ + uint16_t Connection_Handle; + uint16_t TxOctets; + uint16_t TxTime; +}; + class HCIClass { public: HCIClass(); @@ -74,6 +86,23 @@ class HCIClass { void setTransport(HCITransportInterface *HCITransport); + #ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH + //----------------------------- + // @brief + // @param connectionHandle Connection_Handle Connection handle for which the command applies. + // Values: 0x0000 ... 0x0EFF + // @param txOctects TxOctets Preferred maximum number of payload octets that the local + // Controller should include in a single Link Layer packet on this + // connection. + // Values: 0x001B ... 0x00FB + // @param txTime TxTime Preferred maximum number of microseconds that the local + // Controller should use to transmit a single Link Layer packet on this + // connection. + // Values: 0x0148 ... 0x4290 + // @return Value indicating success or error code. + int hciSetDataLength(uint16_t connectionHandle, uint16_t txOctects, uint16_t txTime); + #endif // CFG_BLE_ENABLE_SET_DATA_LENGTH + private: virtual int sendCommand(uint16_t opcode, uint8_t plen = 0, void* parameters = NULL);