8000 Some mtu related bug fixes · chegewara/esp32-snippets@da4394c · GitHub
[go: up one dir, main page]

Skip to content

Commit da4394c

Browse files
committed
Some mtu related bug fixes
1 parent 37a5652 commit da4394c

File tree

6 files changed

+52
-248
lines changed

6 files changed

+52
-248
lines changed

cpp_utils/BLECharacteristic.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <esp_err.h>
1616
#include "BLECharacteristic.h"
1717
#include "BLEService.h"
18+
#include "BLEDevice.h"
1819
#include "BLEUtils.h"
1920
#include "BLE2902.h"
2021
#include "GeneralUtils.h"
@@ -350,8 +351,9 @@ void BLECharacteristic::handleGATTServerEvent(
350351
// The following code has deliberately not been factored to make it fewer statements because this would cloud the
351352
// the logic flow comprehension.
352353
//
353-
uint16_t maxOffset = m_mtu - 1;
354-
if (m_mtu > 512) {
354+
// TODO requires some more research to confirm that 512 is max PDU like in bluetooth specs
355+
uint16_t maxOffset = BLEDevice::getMTU() - 1;
356+
if (BLEDevice::getMTU() > 512) {
355357
maxOffset = 512;
356358
}
357359
if (param->read.need_rsp) {
@@ -418,18 +420,13 @@ void BLECharacteristic::handleGATTServerEvent(
418420
}
419421

420422
case ESP_GATTS_CONNECT_EVT:
421-
m_mtu = 23;
422423
m_semaphoreConfEvt.give();
423424
break;
424425

425426
case ESP_GATTS_DISCONNECT_EVT:
426427
m_semaphoreConfEvt.give();
427428
break;
428429

429-
case ESP_GATTS_MTU_EVT :
430-
m_mtu = param->mtu.mtu;
431-
break;
432-
433430
default: {
434431
break;
435432
} // default
@@ -473,14 +470,11 @@ void BLECharacteristic::indicate() {
473470
return;
474471
}
475472

476-
if (m_value.getValue().length() > 20) {
477-
ESP_LOGD(LOG_TAG, "- Truncating to 20 bytes (maximum notify size)");
473+
if (m_value.getValue().length() > (BLEDevice::getMTU() - 3)) {
474+
ESP_LOGI(LOG_TAG, "- Truncating to %d bytes (maximum indicate size)", BLEDevice::getMTU() - 3);
478475
}
479476

480477
size_t length = m_value.getValue().length();
481-
if (length > 20) {
482-
length = 20;
483-
}
484478

485479
m_semaphoreConfEvt.take("indicate");
486480

@@ -529,14 +523,11 @@ void BLECharacteristic::notify() {
529523
return;
530524
}
531525

532-
if (m_value.getValue().length() > 20) {
533-
ESP_LOGD(LOG_TAG, "- Truncating to 20 bytes (maximum notify size)");
526+
if (m_value.getValue().length() > (BLEDevice::getMTU() - 3)) {
527+
ESP_LOGI(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", BLEDevice::getMTU() - 3);
534528
}
535529

536530
size_t length = m_value.getValue().length();
537-
if (length > 20) {
538-
length = 20;
539-
}
540531

541532
m_semaphoreConfEvt.take("notify");
542533

cpp_utils/BLECharacteristic.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class BLECharacteristic {
103103
BLEService* m_pService;
104104
BLEValue m_value;
105105
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
106-
uint16_t m_mtu = 23;
107106

108107
void handleGATTServerEvent(
109108
esp_gatts_cb_event_t event,

cpp_utils/BLEDevice.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <esp_gap_ble_api.h> // ESP32 BLE
1818
#include <esp_gatts_api.h> // ESP32 BLE
1919
#include <esp_gattc_api.h> // ESP32 BLE
20+
#include <esp_gatt_common_api.h>// ESP32 BLE
2021
#include <esp_err.h> // ESP32 ESP-IDF
2122
#include <esp_log.h> // ESP32 ESP-IDF
2223
#include <map> // Part of C++ Standard library
@@ -42,7 +43,7 @@ BLEClient* BLEDevice::m_pClient = nullptr;
4243
bool initialized = false; // Have we been initialized?
4344
esp_ble_sec_act_t BLEDevice::m_securityLevel = (esp_ble_sec_act_t)0;
4445
BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr;
45-
46+
uint16_t BLEDevice::m_localMTU = 23;
4647

4748
/**
4849
* @brief Create a new instance of a client.
@@ -103,6 +104,11 @@ BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr;
103104
break;
104105
} // ESP_GATTS_CONNECT_EVT
105106

107+
case ESP_GATTS_MTU_EVT: {
108+
BLEDevice::m_localMTU = param->mtu.mtu;
109+
ESP_LOGI(LOG_TAG, "ESP_GATTS_MTU_EVT, MTU %d", BLEDevice::m_localMTU);
110+
break;
111+
}
106112
default: {
107113
break;
108114
}
@@ -135,6 +141,9 @@ BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr;
135141

136142
switch(event) {
137143
case ESP_GATTC_CONNECT_EVT: {
144+
if(BLEDevice::getMTU() != 23){
145+
esp_err_t err = esp_ble_gattc_send_mtu_req(gattc_if, param->connect.conn_id);
146+
}
138147
if(BLEDevice::m_securityLevel){
139148
esp_ble_set_encryption(param->connect.remote_bda, BLEDevice::m_securityLevel);
140149
}
@@ -456,11 +465,42 @@ void BLEDevice::whiteListRemove(BLEAddress address) {
456465
ESP_LOGD(LOG_TAG, "<< whiteListRemove");
457466
} // whiteListRemove
458467

468+
/*
469+
* @brief Set encryption level that will be negotiated with peer device durng connection
470+
* @param [in] level Requested encryption level
471+
*/
459472
void BLEDevice::setEncryptionLevel(esp_ble_sec_act_t level) {
460473
BLEDevice::m_securityLevel = level;
461474
}
462475

476+
/*
477+
* @brief Set callbacks that will be used to handle encryption negotiation events and authentication events
478+
* @param [in] cllbacks Pointer to BLESecurityCallbacks class callback
479+
*/
463480
void BLEDevice::setSecurityCallbacks(BLESecurityCallbacks* callbacks) {
464481
BLEDevice::m_securityCallbacks = callbacks;
465482
}
483+
484+
/*
485+
* @brief Setup local mtu that will be used to negotiate mtu during request from client peer
486+
* @param [in] mtu Value to set local mtu, should be larger than 23 and lower or equal to 517
487+
*/
488+
esp_err_t BLEDevice::setMTU(uint16_t mtu) {
489+
ESP_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu);
490+
esp_err_t err = esp_ble_gatt_set_local_mtu(mtu);
491+
if(err == ESP_OK){
492+
m_localMTU = mtu;
493+
} else {
494+
ESP_LOGE(LOG_TAG, "can't set local mtu value: %d", mtu);
495+
}
496+
ESP_LOGD(LOG_TAG, "<< setLocalMTU");
497+
return err;
498+
}
499+
500+
/*
501+
* @brief Get local MTU value set during mtu request or default value
502+
*/
503+
uint16_t BLEDevice::getMTU() {
504+
return m_localMTU;
505+
}
466506
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ class BLEDevice {
4040
static void whiteListRemove(BLEAddress address); // Remove an entry from the BLE white list.
4141
static void setEncryptionLevel(esp_ble_sec_act_t level);
4242
static void setSecurityCallbacks(BLESecurityCallbacks* pCallbacks);
43+
static esp_err_t setMTU(uint16_t mtu);
44+
static uint16_t getMTU();
4345

4446
private:
4547
static BLEServer *m_pServer;
4648
static BLEScan *m_pScan;
4749
static BLEClient *m_pClient;
4850
static esp_ble_sec_act_t m_securityLevel;
4951
static BLESecurityCallbacks* m_securityCallbacks;
52+
static uint16_t m_localMTU;
5053

5154
static esp_gatt_if_t getGattcIF();
5255

cpp_utils/tests/BLETests/SampleSecureClient.cpp

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0