Skip to content

Commit 3d53219

Browse files
authored
Merge pull request #2 from KMeldgaard/feature/hci_set_data_length
Feature/hci set data length
2 parents 49ac7b4 + 78b918c commit 3d53219

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ https://github.com/stm32duino/wiki/wiki/STM32duinoBLE#stm32duinoble-with-x-nucle
1515

1616
For more information about ArduinoBLE library please visit the official web page at:
1717
https://github.com/arduino-libraries/ArduinoBLE
18-
19-
# Configuration
20-
STM32Cube_WPAN has several configuration options, which are set in the `app_conf.h`.
21-
This package has a default configuration named `app_conf_default.h`.
22-
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.
23-
2418
## License
2519

2620
```
@@ -41,3 +35,12 @@ You should have received a copy of the GNU Lesser General Public
4135
License along with this library; if not, write to the Free Software
4236
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4337
```
38+
39+
# Configuration
40+
STM32Cube_WPAN has several configuration options, which are set in the `app_conf.h`.
41+
This package has a default configuration named `app_conf_default.h`.
42+
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.
43+
## HCI data length
44+
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.
45+
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.
46+
**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!**

src/utility/ATT.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ void ATTClass::addConnection(uint16_t handle, uint8_t role, uint8_t peerBdaddrTy
256256
if (_eventHandlers[BLEConnected]) {
257257
_eventHandlers[BLEConnected](BLEDevice(peerBdaddrType, peerBdaddr));
258258
}
259+
// TODO (krm) increase speed!
260+
#ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH
261+
#if CFG_BLE_ENABLE_SET_DATA_LENGTH < 252 && CFG_BLE_ENABLE_SET_DATA_LENGTH > 26
262+
HCI.hciSetDataLength(handle, CFG_BLE_ENABLE_SET_DATA_LENGTH, 2120);
263+
#endif // CFG_BLE_ENABLE_SET_DATA_LENGTH < 252 && CFG_BLE_ENABLE_SET_DATA_LENGTH > 26
264+
#endif // CFG_BLE_ENABLE_SET_DATA_LENGTH
259265
}
260266

261267
void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[])

src/utility/HCI.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,22 @@ void HCIClass::setTransport(HCITransportInterface *HCITransport)
672672
_HCITransport = HCITransport;
673673
}
674674

675+
#ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH
676+
int HCIClass::hciSetDataLength(uint16_t connectionHandle, uint16_t txOctects, uint16_t txTime){
677+
const uint8_t payload_len = 6;
678+
const uint16_t opcode = 0x2022;
679+
uint8_t cmd_buffer[BLE_CMD_MAX_PARAM_LEN];
680+
hci_le_set_data_length_cp0 *cp0 = (hci_le_set_data_length_cp0*)(cmd_buffer);
681+
682+
// create payload
683+
cp0->Connection_Handle = connectionHandle;
684+
cp0->TxOctets = txOctects;
685+
cp0->TxTime = txTime;
686+
687+
return sendCommand(opcode, payload_len, cmd_buffer);
688+
}
689+
#endif // CFG_BLE_ENABLE_SET_DATA_LENGTH
690+
675691
#if !defined(FAKE_HCI)
676692
HCIClass HCIObj;
677693
HCIClass& HCI = HCIObj;

src/utility/HCI.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
#include <Arduino.h>
2424
#include "HCITransport.h"
2525

26+
#if __has_include("app_conf_custom.h")
27+
#include "app_conf_custom.h"
28+
#endif
29+
30+
#define BLE_CMD_MAX_PARAM_LEN 255
31+
32+
struct hci_le_set_data_length_cp0{
33+
uint16_t Connection_Handle;
34+
uint16_t TxOctets;
35+
uint16_t TxTime;
36+
};
37+
2638
class HCIClass {
2739
public:
2840
HCIClass();
@@ -74,6 +86,23 @@ class HCIClass {
7486

7587
void setTransport(HCITransportInterface *HCITransport);
7688

89+
#ifdef CFG_BLE_ENABLE_SET_DATA_LENGTH
90+
//-----------------------------
91+
// @brief
92+
// @param connectionHandle Connection_Handle Connection handle for which the command applies.
93+
// Values: 0x0000 ... 0x0EFF
94+
// @param txOctects TxOctets Preferred maximum number of payload octets that the local
95+
// Controller should include in a single Link Layer packet on this
96+
// connection.
97+
// Values: 0x001B ... 0x00FB
98+
// @param txTime TxTime Preferred maximum number of microseconds that the local
99+
// Controller should use to transmit a single Link Layer packet on this
100+
// connection.
101+
// Values: 0x0148 ... 0x4290
102+
// @return Value indicating success or error code.
103+
int hciSetDataLength(uint16_t connectionHandle, uint16_t txOctects, uint16_t txTime);
104+
#endif // CFG_BLE_ENABLE_SET_DATA_LENGTH
105+
77106
private:
78107
virtual int sendCommand(uint16_t opcode, uint8_t plen = 0, void* parameters = NULL);
79108

0 commit comments

Comments
 (0)