From 5925782a65e64f80ece9e0120e1e5f6945e136f9 Mon Sep 17 00:00:00 2001 From: "Author: Mr-Mime" <37423773+Mr-Mime@users.noreply.github.com> Date: Sun, 12 Sep 2021 19:09:02 -0600 Subject: [PATCH] [Server][Client] Add function to set data length. Enables the use of BLE data length extension to improve data transfer rates. --- src/NimBLEClient.cpp | 18 ++++++++++++++++++ src/NimBLEClient.h | 1 + src/NimBLEServer.cpp | 23 +++++++++++++++++++++-- src/NimBLEServer.h | 1 + 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 5ea756c..8cded9f 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -436,6 +436,24 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval, } // updateConnParams +/** + * @brief Request an update of the data packet length. + * * Can only be used after a connection has been established. + * @details Sends a data length update request to the server the client is connected to. + * The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes. + * The server needs to support the Bluetooth 4.2 specifications, to be capable of DLE. + * @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB). + */ +void NimBLEClient::setDataLen(uint16_t tx_octets) { + uint16_t tx_time = (tx_octets + 14) * 8; + + int rc = ble_gap_set_data_len(m_conn_id, tx_octets, tx_time); + if(rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); + } +} // setDataLen + + /** * @brief Get detailed information about the current peer connection. */ diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 7c73ab3..3333f3c 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -68,6 +68,7 @@ public: uint16_t scanInterval=16, uint16_t scanWindow=16); void updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout); + void setDataLen(uint16_t tx_octets); void discoverAttributes(); NimBLEConnInfo getConnInfo(); diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index 86996ab..60a0f66 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -735,7 +735,7 @@ void NimBLEServer::startAdvertising() { */ void NimBLEServer::stopAdvertising() { NimBLEDevice::stopAdvertising(); -} // startAdvertising +} // stopAdvertising /** @@ -773,7 +773,26 @@ void NimBLEServer::updateConnParams(uint16_t conn_handle, if(rc != 0) { NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); } -}// updateConnParams +} // updateConnParams + + +/** + * @brief Request an update of the data packet length. + * * Can only be used after a connection has been established. + * @details Sends a data length update request to the peer. + * The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes. + * The peer needs to support the Bluetooth 4.2 specifications, to be capable of DLE. + * @param [in] conn_handle The connection handle of the peer to send the request to. + * @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB). + */ +void NimBLEServer::setDataLen(uint16_t conn_handle, uint16_t tx_octets) { + uint16_t tx_time = (tx_octets + 14) * 8; + + int rc = ble_gap_set_data_len(conn_handle, tx_octets, tx_time); + if(rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); + } +} // setDataLen bool NimBLEServer::setIndicateWait(uint16_t conn_handle) { diff --git a/src/NimBLEServer.h b/src/NimBLEServer.h index e1517c8..a50e189 100644 --- a/src/NimBLEServer.h +++ b/src/NimBLEServer.h @@ -59,6 +59,7 @@ public: void updateConnParams(uint16_t conn_handle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout); + void setDataLen(uint16_t conn_handle, uint16_t tx_octets); uint16_t getPeerMTU(uint16_t conn_id); std::vector getPeerDevices(); NimBLEConnInfo getPeerInfo(size_t index);