Skip to content

HCI data length configuration option #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand All @@ -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!**
5 changes: 5 additions & 0 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand Down
16 changes: 16 additions & 0 deletions src/utility/HCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/utility/HCI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
#include <Arduino.h>
#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();
Expand Down Expand Up @@ -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);

Expand Down