Skip to content

Commit 0e4fa36

Browse files
authored
[Server] Add callback for MTU change. (#265)
onMTUChange callback added that is called when a connection MTU is updated.
1 parent 7311f07 commit 0e4fa36

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

examples/NimBLE_Server/NimBLE_Server.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class ServerCallbacks: public NimBLEServerCallbacks {
3939
Serial.println("Client disconnected - start advertising");
4040
NimBLEDevice::startAdvertising();
4141
};
42+
void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) {
43+
Serial.printf("MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
44+
};
4245

4346
/********************* Security handled here **********************
4447
****** Note: these are the same return values as defaults ********/

src/NimBLEServer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ NimBLEConnInfo NimBLEServer::getPeerIDInfo(uint16_t id) {
416416
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
417417
event->mtu.conn_handle,
418418
event->mtu.value);
419+
rc = ble_gap_conn_find(event->mtu.conn_handle, &desc);
420+
if (rc != 0) {
421+
return 0;
422+
}
423+
424+
server->m_pServerCallbacks->onMTUChange(event->mtu.value, &desc);
419425
return 0;
420426
} // BLE_GAP_EVENT_MTU
421427

@@ -809,6 +815,10 @@ void NimBLEServerCallbacks::onDisconnect(NimBLEServer* pServer, ble_gap_conn_des
809815
NIMBLE_LOGD("NimBLEServerCallbacks", "onDisconnect(): Default");
810816
} // onDisconnect
811817

818+
void NimBLEServerCallbacks::onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) {
819+
NIMBLE_LOGD("NimBLEServerCallbacks", "onMTUChange(): Default");
820+
} // onMTUChange
821+
812822
uint32_t NimBLEServerCallbacks::onPassKeyRequest(){
813823
NIMBLE_LOGD("NimBLEServerCallbacks", "onPassKeyRequest: default: 123456");
814824
return 123456;

src/NimBLEServer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ class NimBLEServerCallbacks {
131131
*/
132132
virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
133133

134+
/**
135+
* @brief Called when the connection MTU changes.
136+
* @param [in] MTU The new MTU value.
137+
* @param [in] desc A pointer to the connection description structure containig information
138+
* about the connection.
139+
*/
140+
virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);
141+
134142
/**
135143
* @brief Called when a client requests a passkey for pairing.
136144
* @return The passkey to be sent to the client.

0 commit comments

Comments
 (0)