Repository navigation
fix(firmware): dynamic MTU negotiation and audio packet fragmentation on Omi Glass (#8452) - #13488
gianluca-disanto wants to merge 4 commits into
Conversation
…entation on Omi Glass (BasedHardware#8452) - Add BLE_MIN_MTU_SIZE (23) and BLE_ATT_HEADER_SIZE (3) definitions to config.h - Track negotiated MTU dynamically via onMTUChange callback in NimBLE ServerHandler - Reset negotiated MTU to safe BLE minimum on disconnect and configure BLEDevice MTU - Implement sub-index audio packet fragmentation in broadcastAudioPacket when frame exceeds safe ATT payload - Add check_omiglass_mtu.py regression test suite covering static tripwires and 19 boundary test cases
There was a problem hiding this comment.
3 issues found across 3 files
Confidence score: 2/5
omiGlass/firmware/src/app.cppcan emit partial Opus frames when the negotiated MTU is smaller than an encoded frame, causing existing SDK consumers to drop audio; add packet index/sub-index reassembly before decoding or pre-fragment frames in a consumer-compatible way.omiGlass/firmware/src/app.cppuses onecurrent_negotiated_mtufor all subscribed clients, so a notification sized for one peer can exceed another peer’s limit and fail delivery; track MTU per connection and notify each peer accordingly.omiGlass/firmware/scripts/check_omiglass_mtu.pysimulates the algorithm rather than exercising firmware behavior, so it can miss off-by-one errors or missingnotify()calls; add an implementation-level or integration test.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="omiGlass/firmware/src/app.cpp">
<violation number="1" location="omiGlass/firmware/src/app.cpp:74">
P2: When multiple clients subscribe with different negotiated MTUs, `current_negotiated_mtu` sizes one notification for all peers and can exceed the smaller client's limit. Track MTU per connection and notify each peer with a peer-specific chunk, or explicitly enforce a single audio connection.</violation>
<violation number="2" location="omiGlass/firmware/src/app.cpp:392">
P1: When the negotiated MTU is below an encoded frame size, this sends partial Opus frames that existing SDK consumers decode independently and drop. Add reassembly using the packet index/sub-index before decoding, or preserve a complete-frame contract for consumers that do not support fragmentation.</violation>
</file>
<file name="omiGlass/firmware/scripts/check_omiglass_mtu.py">
<violation number="1" location="omiGlass/firmware/scripts/check_omiglass_mtu.py:106">
P2: `run_behavioral_simulation()` never exercises the firmware implementation; it only reproduces its current algorithm in Python. An off-by-one or missing `notify()` in `app.cpp` would still pass because the static checks only search for tokens, so run the cases against the compiled production logic or a shared implementation.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| audioDataCharacteristic->notify(); | ||
| memcpy(audio_packet_buffer + AUDIO_PACKET_HEADER_SIZE, data + offset, chunk_size); | ||
|
|
||
| audioDataCharacteristic->setValue(audio_packet_buffer, chunk_size + AUDIO_PACKET_HEADER_SIZE); |
There was a problem hiding this comment.
P1: When the negotiated MTU is below an encoded frame size, this sends partial Opus frames that existing SDK consumers decode independently and drop. Add reassembly using the packet index/sub-index before decoding, or preserve a complete-frame contract for consumers that do not support fragmentation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At omiGlass/firmware/src/app.cpp, line 392:
<comment>When the negotiated MTU is below an encoded frame size, this sends partial Opus frames that existing SDK consumers decode independently and drop. Add reassembly using the packet index/sub-index before decoding, or preserve a complete-frame contract for consumers that do not support fragmentation.</comment>
<file context>
@@ -360,19 +361,40 @@ void onOpusEncoded(uint8_t *data, size_t len)
- audioDataCharacteristic->notify();
+ memcpy(audio_packet_buffer + AUDIO_PACKET_HEADER_SIZE, data + offset, chunk_size);
+
+ audioDataCharacteristic->setValue(audio_packet_buffer, chunk_size + AUDIO_PACKET_HEADER_SIZE);
+ audioDataCharacteristic->notify();
+
</file context>
| def mock_notify(packet_data): | ||
| notified_packets.append(bytes(packet_data)) | ||
|
|
||
| # Replicate production broadcastAudioPacket algorithm |
There was a problem hiding this comment.
P2: run_behavioral_simulation() never exercises the firmware implementation; it only reproduces its current algorithm in Python. An off-by-one or missing notify() in app.cpp would still pass because the static checks only search for tokens, so run the cases against the compiled production logic or a shared implementation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At omiGlass/firmware/scripts/check_omiglass_mtu.py, line 106:
<comment>`run_behavioral_simulation()` never exercises the firmware implementation; it only reproduces its current algorithm in Python. An off-by-one or missing `notify()` in `app.cpp` would still pass because the static checks only search for tokens, so run the cases against the compiled production logic or a shared implementation.</comment>
<file context>
@@ -0,0 +1,187 @@
+ def mock_notify(packet_data):
+ notified_packets.append(bytes(packet_data))
+
+ # Replicate production broadcastAudioPacket algorithm
+ effective_mtu = max(mtu, 23)
+ header_overhead = BLE_ATT_HEADER_SIZE + AUDIO_PACKET_HEADER_SIZE
</file context>
| bool audioEnabled = true; | ||
| volatile bool audioSubscribed = false; | ||
| uint16_t audioPacketIndex = 0; | ||
| static uint16_t current_negotiated_mtu = BLE_MIN_MTU_SIZE; |
There was a problem hiding this comment.
P2: When multiple clients subscribe with different negotiated MTUs, current_negotiated_mtu sizes one notification for all peers and can exceed the smaller client's limit. Track MTU per connection and notify each peer with a peer-specific chunk, or explicitly enforce a single audio connection.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At omiGlass/firmware/src/app.cpp, line 74:
<comment>When multiple clients subscribe with different negotiated MTUs, `current_negotiated_mtu` sizes one notification for all peers and can exceed the smaller client's limit. Track MTU per connection and notify each peer with a peer-specific chunk, or explicitly enforce a single audio connection.</comment>
<file context>
@@ -71,6 +71,7 @@ BLECharacteristic *otaDataCharacteristic;
bool audioEnabled = true;
volatile bool audioSubscribed = false;
uint16_t audioPacketIndex = 0;
+static uint16_t current_negotiated_mtu = BLE_MIN_MTU_SIZE;
// State
</file context>
…ication (BasedHardware#8452) - Increment audioPacketIndex on each chunk notification matching transport.c contract - Prevent Flutter/macOS client reassembly drop warnings on repeated sequence numbers - Update check_omiglass_mtu.py to assert sequential packet sequence numbers across chunks
Git-on-my-level
left a comment
There was a problem hiding this comment.
Verified against the repo's reference implementations — this is solid work overall. One CI-protocol item blocks, plus follow-ups.
What I verified
config.h—BLE_MIN_MTU_SIZE 23andBLE_ATT_HEADER_SIZE 3are correct (ATT notification = 1 opcode + 2 handle bytes). The two added lines are the only config.h changes here; the Formatting check's violations (lines 139+) are pre-existing lines shifted down by the insert, not new.app.cppbroadcastAudioPacket— buffer-safe:audio_packet_bufferisOPUS_OUTPUT_MAX_BYTES + AUDIO_PACKET_HEADER_SIZE(163) bytes and both call sites caplenat 160 (onOpusEncoded,processAudioTx), sochunk_size + 3can't overflow.max_payloadfloors at 17 bytes for MTU 23, never the degenerate 1.app.cppsequencing — advancingaudioPacketIndexper notification with a per-framesub_indexmatches the reference CV1 transport (omi/firmware/omi/src/lib/core/transport.c,push_to_gatt), and the Swift SDK reassembler (sdks/swift/Sources/omi-lib/helpers/Friend.swift) expects exactly this: sub-index 0 opens a frame, continuations append, packet number increases per notification.- Backward compatibility — with the Android app (native layer calls
requestMtu(512)) or iOS (typically 185), a ≤160-byte Opus frame fits a single notification (max_payload≥ 179), so today's single-packet path is unchanged. - NimBLE-Arduino
^1.4.1—onMTUChange(uint16_t, ble_gap_conn_desc*)andBLEDevice::setMTUexist in 1.4.x with these exact signatures; the override compiles against the pin. Reset-on-disconnect and thelen == 0guard are both in place. - The multi-client MTU concern from the earlier bot review doesn't apply here: this firmware is single-connection by design (one
connectedflag, one audio characteristic). check_omiglass_mtu.py— the 19-case boundary matrix covers the exact 1-chunk/2-chunk edges per MTU tier; useful as a regression tripwire even though it re-simulates the algorithm in Python rather than exercising compiled firmware.
Blocking (CI protocol)
- Hygiene fails on
failure-class-protocol:fix:commit subjects require aFailure-Class:declaration in the PR body. Please add exactly one line to the description —Failure-Class: new(or an existingFC-<slug>if one fits). The required check stays red until then.
Non-blocking follow-ups
sdks/device/PROTOCOL.mddocuments the notify payload as[3-byte header][codec payload]with no notion of multi-notification frames. Now that both CV1 and Omi Glass fragment under small MTUs, a short section describing reassembly (sub-index 0 opens a frame, packet number advances per notification) would keep thin SDKs honest.sdks/python/omi/decoder.pyand the FlutterBleDeviceSourcestrip 3 bytes and decode per notification — fine on hosts negotiating ≥185 (all official apps), but they would drop fragments on MTU ≤166 hosts. Worth a tracking issue if small-MTU hosts matter.
Leaving for human maintainer review: this is a BLE wire-protocol change with no compiled firmware CI path in the repo, so on-device verification across MTU tiers (23 / 64 / 185 / 512) should gate the merge.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
@Git-on-my-level Added Failure-Class: new to the description. Thanks! |
|
Thanks for the quick turnaround — the declaration landed and resolved the first error, but I searched the existing catalog — there is no class for BLE ATT-MTU notification sizing (nothing under GATT / MTU / transport-capacity), so {
"schema_version": 1,
"id": "FC-ble-att-notification-exceeds-negotiated-mtu",
"violated_contract": "A GATT notification must fit the peer-negotiated ATT MTU (payload <= MTU - 3); firmware that sizes audio notifications from an assumed maximum MTU drops or corrupts packets on hosts that negotiate a smaller MTU.",
"canonical_prevention": "Track the negotiated MTU via the server's onMTUChange callback (reset on disconnect) and fragment each audio frame into MTU-bounded notifications carrying a monotonic packet index and per-frame sub-index.",
"canonical_prevention_artifact": ["omiGlass/firmware/scripts/check_omiglass_mtu.py"],
"evidence_prs": [],
"scope_hints": ["omiGlass/firmware/src/app.cpp", "omiGlass/firmware/src/config.h"],
"status": "open"
}Pointing Once the file lands, push and the check re-runs on the new commit; if it ever stays queued, a small description edit re-triggers it. Everything else from the earlier review is unchanged and non-blocking — the MTU tracking and fragmentation loop in The merge decision still rests with a human maintainer: this changes the BLE wire behavior for audio and the repo has no compiled-firmware CI, so on-device verification across MTU tiers (23 / 64 / 185 / 512) should gate the merge. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
|
@Git-on-my-level Thanks! I've added the failure class definition file |
Resolved on head 339c7c3: the PR body now declares Failure-Class: new and .github/failure-classes/FC-ble-att-notification-exceeds-negotiated-mtu.json landed with the required schema (Hygiene green); the remaining C/C++ formatting failure was fixed by the clang-format pass in 339c7c3. No concerns from this review remain open.
|
Both follow-ups landed cleanly on head Re-verified on
The earlier change request is dismissed as resolved. Still open and non-blocking: a short reassembly note in Leaving the merge decision with a human maintainer for a specific reason: this changes the BLE wire behavior for audio, and the repo has no compiled-firmware CI, so on-device verification across MTU tiers (23 / 64 / 185 / 512) should gate it. by AI on behalf of David — if you need David's attention urgently, please @Git-on-my-level and escalate with |
|
Follow-up verification on head Cross-verified against the toolchain and the consuming code
Ecosystem notes (non-blocking)
Automation can't exercise the on-device BLE behavior, and the bounty/lock decision from #8452 rides on this PR — those are the two items needing maintainer sign-off before merge. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
Summary
Fixes #8452
Follow-up implementation to my lock request on #8452 ($50 bounty task). Adds dynamic ATT MTU negotiation tracking
and sub-index packet fragmentation to Omi Glass firmware (
omiGlass/firmware/src/app.cppandconfig.h), ensuringaudio packets never exceed the negotiated ATT MTU across legacy, intermediate, and modern host devices.
Problem & Root Cause
Omi Glass streams microphone audio (Opus encoded frames up to 160 bytes) over Bluetooth Low Energy. The firmware
was previously hardcoded assuming a 517-byte MTU:
ServerHandler(NimBLEServerCallbacks) lacked anonMTUChangecallback, leaving the firmware unaware ofthe actual link MTU negotiated by the host.
broadcastAudioPacketpreviously sent full Opus frames in a single notification withsub_index = 0withoutbounding check.
bytes or minimum 23 bytes), attempting to notify characteristic buffers larger than
MTU - 3resulted in packetdrops or firmware crashes.
Solution & Changes
omiGlass/firmware/src/config.h:BLE_MIN_MTU_SIZE 23(Bluetooth Core Specification minimum ATT MTU).BLE_ATT_HEADER_SIZE 3(1-byte notification opcode0x1B+ 2-byte attribute handle).omiGlass/firmware/src/app.cpp:current_negotiated_mtustate variable initialized toBLE_MIN_MTU_SIZE.onMTUChange(uint16_t MTU, ble_gap_conn_desc *desc)inServerHandlerto record negotiated MTU.current_negotiated_mtuback toBLE_MIN_MTU_SIZEononDisconnect().BLEDevice::setMTU(BLE_MTU_SIZE)inconfigure_ble().broadcastAudioPacket(), computedmax_payload = mtu - BLE_ATT_HEADER_SIZE - AUDIO_PACKET_HEADER_SIZE. If a frame exceedsmax_payload, it is chunked across sequentialnotifications sharing the same
audioPacketIndexand incrementingsub_index(0, 1, 2...). Standard frames onMTU 185/517 continue to be transmitted in a single packet (
sub_index = 0), preserving complete backwardscompatibility.
omiGlass/firmware/scripts/check_omiglass_mtu.py:config.h/app.cppandbehavioral simulation across 19 boundary test cases (MTU 23, 64, 185, 247, 517 with payloads from 0 to 500 bytes).
Verification & Test Results
Ran regression test suite
python omiGlass/firmware/scripts/check_omiglass_mtu.py:[STATIC TRIPWIRE] Verifying firmware source contracts...
PASS: All static tripwires satisfied in config.h and app.cpp.
[BEHAVIORAL SIMULATION] Running simulated audio packet fragmentation test cases:
[PASS] MTU= 23 audio_bytes= 0 -> chunks=0 delivered=0 bytes
[PASS] MTU= 23 audio_bytes= 1 -> chunks=1 delivered=1 bytes
[PASS] MTU= 23 audio_bytes= 16 -> chunks=1 delivered=16 bytes
[PASS] MTU= 23 audio_bytes= 17 -> chunks=1 delivered=17 bytes
[PASS] MTU= 23 audio_bytes= 18 -> chunks=2 delivered=18 bytes
[PASS] MTU= 23 audio_bytes= 34 -> chunks=2 delivered=34 bytes
[PASS] MTU= 23 audio_bytes= 35 -> chunks=3 delivered=35 bytes
[PASS] MTU= 23 audio_bytes=120 -> chunks=8 delivered=120 bytes
[PASS] MTU= 23 audio_bytes=160 -> chunks=10 delivered=160 bytes
[PASS] MTU= 64 audio_bytes=160 -> chunks=3 delivered=160 bytes
[PASS] MTU=185 audio_bytes= 17 -> chunks=1 delivered=17 bytes
[PASS] MTU=185 audio_bytes=160 -> chunks=1 delivered=160 bytes
[PASS] MTU=185 audio_bytes=179 -> chunks=1 delivered=179 bytes
[PASS] MTU=185 audio_bytes=180 -> chunks=2 delivered=180 bytes
[PASS] MTU=185 audio_bytes=320 -> chunks=2 delivered=320 bytes
[PASS] MTU=247 audio_bytes=160 -> chunks=1 delivered=160 bytes
[PASS] MTU=247 audio_bytes=300 -> chunks=2 delivered=300 bytes
[PASS] MTU=517 audio_bytes=160 -> chunks=1 delivered=160 bytes
[PASS] MTU=517 audio_bytes=500 -> chunks=1 delivered=500 bytes
19 of 19 test cases passed (0 failed)
len <= MTU - 3.Failure-Class: new