[ble] clean up message parsing in BleSecure::HandleTransport() (#13117)

This commit refactors `BleSecure::HandleTransport()` to use the
`OffsetRange` and `Message::ReadAndAdvance()` helper methods. This
replaces manual length and offset tracking, resulting in cleaner
and safer message parsing logic.

Additionally:
- Simplified the payload length calculation by using nested `Min()`
  calls instead of multiple `if/else` blocks.
- Added a `RadioPacket` typedef in `BleSecure` to alias the public
  `otBleRadioPacket` structure, aligning with OpenThread's core
  namespace conventions.
This commit is contained in:
Abtin Keshavarzian
2026-05-18 19:11:10 -07:00
committed by GitHub
parent 98b26df890
commit b42e3747ee
2 changed files with 11 additions and 22 deletions
+9 -22
View File
@@ -633,38 +633,25 @@ Error BleSecure::HandleTransport(void *aContext, ot::Message &aMessage, const Ip
Error BleSecure::HandleTransport(ot::Message &aMessage)
{
otBleRadioPacket packet;
uint16_t len = aMessage.GetLength();
uint16_t offset = 0;
Error error = kErrorNone;
Error error = kErrorNone;
RadioPacket packet;
OffsetRange offsetRange;
while (len > 0)
offsetRange.InitFromMessageFullLength(aMessage);
while (!offsetRange.IsEmpty())
{
if (len <= mMtuSize - kGattOverhead)
{
packet.mLength = len;
}
else
{
packet.mLength = mMtuSize - kGattOverhead;
}
packet.mLength = Min(offsetRange.GetLength(), Min<uint16_t>(mMtuSize - kGattOverhead, kPacketBufferSize));
if (packet.mLength > kPacketBufferSize)
{
packet.mLength = kPacketBufferSize;
}
IgnoreError(aMessage.Read(offset, mPacketBuffer, packet.mLength));
IgnoreError(aMessage.ReadAndAdvance(offsetRange, mPacketBuffer, packet.mLength));
packet.mValue = mPacketBuffer;
packet.mPower = OT_BLE_DEFAULT_POWER;
SuccessOrExit(error = otPlatBleGattServerIndicate(&GetInstance(), kTxBleHandle, &packet));
len -= packet.mLength;
offset += packet.mLength;
}
aMessage.Free();
exit:
return error;
}
+2
View File
@@ -346,6 +346,8 @@ private:
kNotAdvertising = 3, // Ble secure is started but not advertising.
};
typedef otBleRadioPacket RadioPacket;
static constexpr uint8_t kInitialMtuSize = OT_BLE_ATT_MTU_DEFAULT;
static constexpr uint8_t kMinMtuSize = OT_BLE_ATT_MTU_MIN;
static constexpr uint8_t kMaxMtuSize = OT_BLE_ATT_MTU_MAX;