-
-
Notifications
You must be signed in to change notification settings - Fork 86
feat(L2CAP): add disconnect API and harden CoC send/error handling #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0d417dd
f3559e4
e7fee6f
181c725
36e59d6
27ff34b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| dependencies: | ||
| local/esp-nimble-cpp: | ||
| path: ../../../../../esp-nimble-cpp/ | ||
| mickeyl/esp-hpl: | ||
| git: https://github.com/mickeyl/esp-hpl.git | ||
| version: "1.1.0" |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,28 @@ | ||||||||||||||||||
| #include <NimBLEDevice.h> | ||||||||||||||||||
| #include <esp_hpl.hpp> | ||||||||||||||||||
| #include <esp_timer.h> | ||||||||||||||||||
|
|
||||||||||||||||||
| // See the following for generating UUIDs: | ||||||||||||||||||
| // https://www.uuidgenerator.net/ | ||||||||||||||||||
|
|
||||||||||||||||||
| // The remote service we wish to connect to. | ||||||||||||||||||
| static BLEUUID serviceUUID("dcbc7255-1e9e-49a0-a360-b0430b6c6905"); | ||||||||||||||||||
| // The characteristic of the remote service we are interested in. | ||||||||||||||||||
| static BLEUUID charUUID("371a55c8-f251-4ad2-90b3-c7c195b049be"); | ||||||||||||||||||
|
|
||||||||||||||||||
| #define L2CAP_CHANNEL 150 | ||||||||||||||||||
| #define L2CAP_PSM 192 | ||||||||||||||||||
| #define L2CAP_MTU 5000 | ||||||||||||||||||
| #define INITIAL_PAYLOAD_SIZE 64 | ||||||||||||||||||
| #define BLOCKS_BEFORE_DOUBLE 50 | ||||||||||||||||||
| #define MAX_PAYLOAD_SIZE 4900 | ||||||||||||||||||
|
|
||||||||||||||||||
| const BLEAdvertisedDevice* theDevice = NULL; | ||||||||||||||||||
| BLEClient* theClient = NULL; | ||||||||||||||||||
| BLEL2CAPChannel* theChannel = NULL; | ||||||||||||||||||
|
|
||||||||||||||||||
| size_t bytesSent = 0; | ||||||||||||||||||
| size_t bytesReceived = 0; | ||||||||||||||||||
| size_t currentPayloadSize = INITIAL_PAYLOAD_SIZE; | ||||||||||||||||||
| uint32_t blocksSent = 0; | ||||||||||||||||||
| uint64_t startTime = 0; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Heap monitoring | ||||||||||||||||||
|
Comment on lines
15
to
+21
|
||||||||||||||||||
| size_t initialHeap = 0; | ||||||||||||||||||
| size_t lastHeap = 0; | ||||||||||||||||||
| size_t heapDecreaseCount = 0; | ||||||||||||||||||
| const size_t HEAP_LEAK_THRESHOLD = 10; // Warn after 10 consecutive decreases | ||||||||||||||||||
|
|
||||||||||||||||||
| class L2CAPChannelCallbacks: public BLEL2CAPChannelCallbacks { | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -43,7 +49,7 @@ class MyClientCallbacks: public BLEClientCallbacks { | |||||||||||||||||
| printf("GAP connected\n"); | ||||||||||||||||||
| pClient->setDataLen(251); | ||||||||||||||||||
|
|
||||||||||||||||||
| theChannel = BLEL2CAPChannel::connect(pClient, L2CAP_CHANNEL, L2CAP_MTU, new L2CAPChannelCallbacks()); | ||||||||||||||||||
| theChannel = BLEL2CAPChannel::connect(pClient, L2CAP_PSM, L2CAP_MTU, new L2CAPChannelCallbacks()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| void onDisconnect(BLEClient* pClient, int reason) { | ||||||||||||||||||
|
|
@@ -61,23 +67,68 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { | |||||||||||||||||
| if (theDevice) { return; } | ||||||||||||||||||
| printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str()); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!advertisedDevice->haveServiceUUID()) { return; } | ||||||||||||||||||
| if (!advertisedDevice->isAdvertisingService(serviceUUID)) { return; } | ||||||||||||||||||
| // Look for device named "l2cap" | ||||||||||||||||||
| if (advertisedDevice->haveName() && advertisedDevice->getName() == "l2cap") { | ||||||||||||||||||
| printf("Found l2cap device!\n"); | ||||||||||||||||||
| BLEDevice::getScan()->stop(); | ||||||||||||||||||
| theDevice = advertisedDevice; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| void statusTask(void *pvParameters) { | ||||||||||||||||||
| while (true) { | ||||||||||||||||||
| vTaskDelay(1000 / portTICK_PERIOD_MS); | ||||||||||||||||||
|
|
||||||||||||||||||
| printf("Found the device we're interested in!\n"); | ||||||||||||||||||
| BLEDevice::getScan()->stop(); | ||||||||||||||||||
| if (startTime > 0 && blocksSent > 0) { | ||||||||||||||||||
| uint64_t currentTime = esp_timer_get_time(); | ||||||||||||||||||
| double elapsedSeconds = (currentTime - startTime) / 1000000.0; | ||||||||||||||||||
| double bytesPerSecond = bytesSent / elapsedSeconds; | ||||||||||||||||||
| double kbPerSecond = bytesPerSecond / 1024.0; | ||||||||||||||||||
|
Comment on lines
+86
to
+87
|
||||||||||||||||||
| double bytesPerSecond = bytesSent / elapsedSeconds; | |
| double kbPerSecond = bytesPerSecond / 1024.0; | |
| double bytesPerSecond = 0.0; | |
| double kbPerSecond = 0.0; | |
| if (elapsedSeconds > 0.0) { | |
| bytesPerSecond = bytesSent / elapsedSeconds; | |
| kbPerSecond = bytesPerSecond / 1024.0; | |
| } |
Copilot
AI
Mar 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning printf includes non-ASCII characters (
| printf("\n⚠️ WARNING: POSSIBLE MEMORY LEAK DETECTED! ⚠️\n"); | |
| printf("\n*** WARNING: POSSIBLE MEMORY LEAK DETECTED! ***\n"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| dependencies: | ||
| local/esp-nimble-cpp: | ||
| path: ../../../../../esp-nimble-cpp/ | ||
| mickeyl/esp-hpl: | ||
| git: https://github.com/mickeyl/esp-hpl.git | ||
| version: "1.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bytesReceivedis declared but never read/updated anywhere in this example now. If examples are built with warnings-as-errors, this can trigger an unused-variable warning; remove it or wire it into the receive path.