Skip to content

Commit 5925782

Browse files
Mr-Mimeh2zero
authored andcommitted
[Server][Client] Add function to set data length.
Enables the use of BLE data length extension to improve data transfer rates.
1 parent ccea428 commit 5925782

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/NimBLEClient.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,24 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
436436
} // updateConnParams
437437

438438

439+
/**
440+
* @brief Request an update of the data packet length.
441+
* * Can only be used after a connection has been established.
442+
* @details Sends a data length update request to the server the client is connected to.
443+
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
444+
* The server needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
445+
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
446+
*/
447+
void NimBLEClient::setDataLen(uint16_t tx_octets) {
448+
uint16_t tx_time = (tx_octets + 14) * 8;
449+
450+
int rc = ble_gap_set_data_len(m_conn_id, tx_octets, tx_time);
451+
if(rc != 0) {
452+
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
453+
}
454+
} // setDataLen
455+
456+
439457
/**
440458
* @brief Get detailed information about the current peer connection.
441459
*/

src/NimBLEClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class NimBLEClient {
6868
uint16_t scanInterval=16, uint16_t scanWindow=16);
6969
void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
7070
uint16_t latency, uint16_t timeout);
71+
void setDataLen(uint16_t tx_octets);
7172
void discoverAttributes();
7273
NimBLEConnInfo getConnInfo();
7374

src/NimBLEServer.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ void NimBLEServer::startAdvertising() {
735735
*/
736736
void NimBLEServer::stopAdvertising() {
737737
NimBLEDevice::stopAdvertising();
738-
} // startAdvertising
738+
} // stopAdvertising
739739

740740

741741
/**
@@ -773,7 +773,26 @@ void NimBLEServer::updateConnParams(uint16_t conn_handle,
773773
if(rc != 0) {
774774
NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
775775
}
776-
}// updateConnParams
776+
} // updateConnParams
777+
778+
779+
/**
780+
* @brief Request an update of the data packet length.
781+
* * Can only be used after a connection has been established.
782+
* @details Sends a data length update request to the peer.
783+
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
784+
* The peer needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
785+
* @param [in] conn_handle The connection handle of the peer to send the request to.
786+
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
787+
*/
788+
void NimBLEServer::setDataLen(uint16_t conn_handle, uint16_t tx_octets) {
789+
uint16_t tx_time = (tx_octets + 14) * 8;
790+
791+
int rc = ble_gap_set_data_len(conn_handle, tx_octets, tx_time);
792+
if(rc != 0) {
793+
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
794+
}
795+
} // setDataLen
777796

778797

779798
bool NimBLEServer::setIndicateWait(uint16_t conn_handle) {

src/NimBLEServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class NimBLEServer {
5959
void updateConnParams(uint16_t conn_handle,
6060
uint16_t minInterval, uint16_t maxInterval,
6161
uint16_t latency, uint16_t timeout);
62+
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
6263
uint16_t getPeerMTU(uint16_t conn_id);
6364
std::vector<uint16_t> getPeerDevices();
6465
NimBLEConnInfo getPeerInfo(size_t index);

0 commit comments

Comments
 (0)