Repository navigation
fix(examples): update L2CAP examples for reference callbacks and correct onConnect signature #458
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
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 |
|---|---|---|
|
|
@@ -14,13 +14,13 @@ const size_t HEAP_LEAK_THRESHOLD = 10; // Warn after 10 consecutive decreases | |
| class GATTCallbacks: public BLEServerCallbacks { | ||
|
|
||
| public: | ||
| void onConnect(BLEServer* pServer, BLEConnInfo& info) { | ||
| void onConnect(BLEServer* pServer, BLEConnInfo& info) override { | ||
| /// Booster #1 | ||
| pServer->setDataLen(info.getConnHandle(), 251); | ||
| /// Booster #2 (especially for Apple devices) | ||
| BLEDevice::getServer()->updateConnParams(info.getConnHandle(), 12, 12, 0, 200); | ||
| } | ||
| }; | ||
| } gattCallbacks; | ||
|
|
||
| class L2CAPChannelCallbacks: public BLEL2CAPChannelCallbacks { | ||
|
|
||
|
|
@@ -36,8 +36,8 @@ class L2CAPChannelCallbacks: public BLEL2CAPChannelCallbacks { | |
| std::vector<uint8_t> buffer; // Buffer for incomplete frames | ||
|
|
||
| public: | ||
| void onConnect(NimBLEL2CAPChannel* channel) { | ||
| printf("L2CAP connection established on PSM %d\n", L2CAP_PSM); | ||
| void onConnect(NimBLEL2CAPChannel* channel, uint16_t negotiatedMTU) override { | ||
| printf("L2CAP connection established on PSM %d (MTU %u)\n", L2CAP_PSM, negotiatedMTU); | ||
| connected = true; | ||
|
Comment on lines
+39
to
41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| totalBytesReceived = 0; | ||
| totalFramesReceived = 0; | ||
|
|
@@ -49,7 +49,7 @@ class L2CAPChannelCallbacks: public BLEL2CAPChannelCallbacks { | |
| buffer.clear(); | ||
| } | ||
|
|
||
| void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) { | ||
| void onRead(NimBLEL2CAPChannel* channel, std::vector<uint8_t>& data) override { | ||
| // Append new data to buffer | ||
| buffer.insert(buffer.end(), data.begin(), data.end()); | ||
| totalBytesReceived += data.size(); | ||
|
|
@@ -98,7 +98,7 @@ class L2CAPChannelCallbacks: public BLEL2CAPChannelCallbacks { | |
| } | ||
| } | ||
|
|
||
| void onDisconnect(NimBLEL2CAPChannel* channel) { | ||
| void onDisconnect(NimBLEL2CAPChannel* channel) override { | ||
| printf("\nL2CAP disconnected\n"); | ||
| double elapsedSeconds = startTime > 0 ? (esp_timer_get_time() - startTime) / 1000000.0 : 0.0; | ||
| double bytesPerSecond = elapsedSeconds > 0 ? totalBytesReceived / elapsedSeconds : 0.0; | ||
|
|
@@ -143,7 +143,7 @@ void app_main(void) { | |
| (void)channel; // prevent unused warning | ||
|
|
||
| auto server = BLEDevice::createServer(); | ||
| server->setCallbacks(new GATTCallbacks()); | ||
| server->setCallbacks(gattCallbacks); | ||
|
|
||
| auto advertising = BLEDevice::getAdvertising(); | ||
| NimBLEAdvertisementData scanData; | ||
|
|
||
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.
Script executed:
rg -n 'start measuring|startTime|bytesPerSecond|Bandwidth' examples/L2CAPRepository: h2zero/esp-nimble-cpp
Length of output: 2964
Script executed:
Repository: h2zero/esp-nimble-cpp
Length of output: 10030
Remove the connect-time
startTimeassignment so bandwidth starts at the first data.Now that
onConnectruns, line 48 setsstartTimewhen the channel opens. ThestartTime == 0guard inonRead(lines 56-58) then never passes. Bandwidth and the status output include the idle time between connection and the first data. Remove line 48 soonReadstarts the timer when data arrives.Suggested fix
frameErrors = 0; - startTime = esp_timer_get_time(); buffer.clear();