Skip to content

Commit

Permalink
Adjust storage and processing in SystemPacketBuffer
Browse files Browse the repository at this point in the history
and associated code for large payloads.

Extend uint16_t type variables to uint32_t at applicable
places.
  • Loading branch information
pidarped committed Jan 31, 2024
1 parent f63b505 commit e0f445b
Show file tree
Hide file tree
Showing 27 changed files with 179 additions and 175 deletions.
2 changes: 1 addition & 1 deletion src/app/server/EchoHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ chip::Protocols::Echo::EchoServer gEchoServer;
*/
void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload)
{
ChipLogProgress(AppServer, "Echo Request, len=%u ... sending response.\n", payload->DataLength());
ChipLogProgress(AppServer, "Echo Request, len=%u ... sending response.\n", static_cast<uint16_t>(payload->DataLength()));
}

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions src/ble/BtpEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
// mRxFragnentSize may be smaller than the characteristic size. Make sure
// we're not truncating to a data length smaller than what we have already consumed.
VerifyOrExit(reader.OctetsRead() <= mRxFragmentSize, err = BLE_ERROR_REASSEMBLER_INCORRECT_STATE);
data->SetDataLength(chip::min(data->DataLength(), mRxFragmentSize));
data->SetDataLength(chip::min(data->DataLength(), static_cast<uint32_t>(mRxFragmentSize)));

// Now mark the bytes we consumed as consumed.
data->ConsumeHead(static_cast<uint16_t>(reader.OctetsRead()));
Expand Down Expand Up @@ -374,7 +374,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
if (rx_flags.Has(HeaderFlags::kEndMessage))
{
// Trim remainder, if any, of the received packet buffer based on sender-specified length of reassembled message.
int padding = mRxBuf->DataLength() - mRxLength;
uint32_t padding = mRxBuf->DataLength() - mRxLength;

if (padding > 0)
{
Expand Down Expand Up @@ -403,7 +403,7 @@ CHIP_ERROR BtpEngine::HandleCharacteristicReceived(System::PacketBufferHandle &&
}
if (!mRxBuf.IsNull())
{
ChipLogError(Ble, "With rx buf data length = %u", mRxBuf->DataLength());
ChipLogError(Ble, "With rx buf data length = %u", static_cast<uint16_t>(mRxBuf->DataLength()));
}
LogState();

Expand Down Expand Up @@ -456,7 +456,7 @@ bool BtpEngine::HandleCharacteristicSend(System::PacketBufferHandle data, bool s

mTxBuf = std::move(data);
mTxState = kState_InProgress;
mTxLength = mTxBuf->DataLength();
mTxLength = static_cast<uint16_t>(mTxBuf->DataLength());

ChipLogDebugBtpEngine(Ble, ">>> CHIPoBle preparing to send whole message:");
PrintBufDebug(mTxBuf);
Expand Down
2 changes: 1 addition & 1 deletion src/inet/TCPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void TCPEndPoint::DriveReceiving()
{
// Acknowledgement is done after handling the buffers to allow the
// application processing to throttle flow.
uint16_t ackLength = mRcvQueue->TotalLength();
uint32_t ackLength = mRcvQueue->TotalLength();
CHIP_ERROR err = OnDataReceived(this, std::move(mRcvQueue));
if (err != CHIP_NO_ERROR)
{
Expand Down
6 changes: 3 additions & 3 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
* received. The operational semantics are undefined if \c len is larger
* than the total outstanding unacknowledged received data.
*/
virtual CHIP_ERROR AckReceive(uint16_t len) = 0;
virtual CHIP_ERROR AckReceive(uint32_t len) = 0;

/**
* @brief Set the receive queue, for testing.
Expand All @@ -301,7 +301,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
* @brief Extract the length of the unacknowledged receive data.
*
* @return Number of bytes in the receive queue that have not yet been
* acknowledged with <tt>AckReceive(uint16_t len)</tt>.
* acknowledged with <tt>AckReceive(uint32_t len)</tt>.
*/
uint32_t PendingReceiveLength();

Expand Down Expand Up @@ -447,7 +447,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
* is the length of the message text added to the TCP transmit window,
* which are eligible for sending by the underlying network stack.
*/
typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint16_t len);
typedef void (*OnDataSentFunct)(TCPEndPoint * endPoint, uint32_t len);

/**
* The endpoint's message text transmission event handling function
Expand Down
8 changes: 4 additions & 4 deletions src/inet/TCPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ CHIP_ERROR TCPEndPointImplLwIP::DriveSendingImpl()
{
VerifyOrDie(!startOfUnsent.buffer.IsNull());

uint16_t bufDataLen = startOfUnsent.buffer->DataLength();
uint16_t bufDataLen = static_cast<uint16_t>(startOfUnsent.buffer->DataLength());

// Get a pointer to the start of unsent data within the first buffer on the unsent queue.
const uint8_t * sendData = startOfUnsent.buffer->Start() + startOfUnsent.offset;
Expand Down Expand Up @@ -503,7 +503,7 @@ void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState)
}
}

CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint16_t len)
CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint32_t len)
{
VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE);
CHIP_ERROR res = CHIP_NO_ERROR;
Expand All @@ -512,7 +512,7 @@ CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint16_t len)
LOCK_TCPIP_CORE();

if (mTCP != nullptr)
tcp_recved(mTCP, len);
tcp_recved(mTCP, static_cast<uint16_t>(len));
else
res = CHIP_ERROR_CONNECTION_ABORTED;

Expand Down Expand Up @@ -570,7 +570,7 @@ TCPEndPointImplLwIP::BufferOffset TCPEndPointImplLwIP::FindStartOfUnsent()
while (leftToSkip > 0)
{
VerifyOrDie(!startOfUnsent.buffer.IsNull());
uint16_t bufDataLen = startOfUnsent.buffer->DataLength();
uint16_t bufDataLen = static_cast<uint16_t>(startOfUnsent.buffer->DataLength());
if (leftToSkip >= bufDataLen)
{
// We have more to skip than current packet buffer size.
Expand Down
2 changes: 1 addition & 1 deletion src/inet/TCPEndPointImplLwIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TCPEndPointImplLwIP : public TCPEndPoint, public EndPointStateLwIP
CHIP_ERROR EnableNoDelay() override;
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
CHIP_ERROR DisableKeepAlive() override;
CHIP_ERROR AckReceive(uint16_t len) override;
CHIP_ERROR AckReceive(uint32_t len) override;
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
void TCPUserTimeoutHandler() override;
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
Expand Down
2 changes: 1 addition & 1 deletion src/inet/TCPEndPointImplOpenThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ CHIP_ERROR TCPEndPointImplOT::DisableKeepAlive()
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR TCPEndPointImplOT::AckReceive(uint16_t len)
CHIP_ERROR TCPEndPointImplOT::AckReceive(uint32_t len)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
Expand Down
2 changes: 1 addition & 1 deletion src/inet/TCPEndPointImplOpenThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TCPEndPointImplOT : public TCPEndPoint, public EndPointStateOpenThread
CHIP_ERROR EnableNoDelay() override;
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
CHIP_ERROR DisableKeepAlive() override;
CHIP_ERROR AckReceive(uint16_t len) override;
CHIP_ERROR AckReceive(uint32_t len) override;
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
void TCPUserTimeoutHandler() override;
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
Expand Down
14 changes: 7 additions & 7 deletions src/inet/TCPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ CHIP_ERROR TCPEndPointImplSockets::DisableKeepAlive()
return CHIP_NO_ERROR;
}

CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint16_t len)
CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint32_t len)
{
VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE);

Expand Down Expand Up @@ -483,7 +483,7 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl()

while (!mSendQueue.IsNull())
{
uint16_t bufLen = mSendQueue->DataLength();
uint32_t bufLen = mSendQueue->DataLength();

ssize_t lenSentRaw = send(mSocket, mSendQueue->Start(), bufLen, sendFlags);

Expand All @@ -502,8 +502,8 @@ CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl()
break;
}

// Cast is safe because bufLen is uint16_t.
uint16_t lenSent = static_cast<uint16_t>(lenSentRaw);
// Cast is safe because bufLen is uint32_t.
uint32_t lenSent = static_cast<uint32_t>(lenSentRaw);

// Mark the connection as being active.
MarkActive();
Expand Down Expand Up @@ -948,10 +948,10 @@ void TCPEndPointImplSockets::ReceiveData()
{
VerifyOrDie(rcvLen > 0);
size_t newDataLength = rcvBuf->DataLength() + static_cast<size_t>(rcvLen);
VerifyOrDie(CanCastTo<uint16_t>(newDataLength));
VerifyOrDie(CanCastTo<uint32_t>(newDataLength));
if (isNewBuf)
{
rcvBuf->SetDataLength(static_cast<uint16_t>(newDataLength));
rcvBuf->SetDataLength(static_cast<uint32_t>(newDataLength));
rcvBuf.RightSize();
if (mRcvQueue.IsNull())
{
Expand All @@ -964,7 +964,7 @@ void TCPEndPointImplSockets::ReceiveData()
}
else
{
rcvBuf->SetDataLength(static_cast<uint16_t>(newDataLength), mRcvQueue);
rcvBuf->SetDataLength(static_cast<uint32_t>(newDataLength), mRcvQueue);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/inet/TCPEndPointImplSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TCPEndPointImplSockets : public TCPEndPoint, public EndPointStateSockets
CHIP_ERROR EnableNoDelay() override;
CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override;
CHIP_ERROR DisableKeepAlive() override;
CHIP_ERROR AckReceive(uint16_t len) override;
CHIP_ERROR AckReceive(uint32_t len) override;
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
void TCPUserTimeoutHandler() override;
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
Expand Down
4 changes: 2 additions & 2 deletions src/inet/UDPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
if (buf->HasChainedBuffer())
{
// Have to allocate a new big-enough buffer and copy.
uint16_t messageSize = buf->TotalLength();
uint16_t messageSize = static_cast<uint16_t>(buf->TotalLength());
System::PacketBufferHandle copy = System::PacketBufferHandle::New(messageSize, 0);
if (copy.IsNull() || buf->Read(copy->Start(), messageSize) != CHIP_NO_ERROR)
{
ChipLogError(Inet, "No memory to flatten incoming packet buffer chain of size %u", buf->TotalLength());
ChipLogError(Inet, "No memory to flatten incoming packet buffer chain of size %lu", buf->TotalLength());
return;
}
buf = std::move(copy);
Expand Down
4 changes: 2 additions & 2 deletions src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ CHIP_ERROR UDPEndPointImplSockets::SendMsgImpl(const IPPacketInfo * aPktInfo, Sy
{
return CHIP_ERROR_POSIX(errno);
}
if (lenSent != msg->DataLength())
if (msg->DataLength() != static_cast<uint32_t>(lenSent))
{
return CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ void UDPEndPointImplSockets::HandlePendingIO(System::SocketEvents events)
{
lStatus = CHIP_ERROR_POSIX(errno);
}
else if (rcvLen > lBuffer->AvailableDataLength())
else if (lBuffer->AvailableDataLength() < static_cast<uint32_t>(rcvLen))
{
lStatus = CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG;
}
Expand Down
2 changes: 1 addition & 1 deletion src/inet/tests/TestInetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ static void HandleTCPConnectionClosed(TCPEndPoint * aEndPoint, CHIP_ERROR aError
}
}

static void HandleTCPDataSent(TCPEndPoint * aEndPoint, uint16_t len) {}
static void HandleTCPDataSent(TCPEndPoint * aEndPoint, uint32_t len) {}

static CHIP_ERROR HandleTCPDataReceived(TCPEndPoint * aEndPoint, PacketBufferHandle && aBuffer)
{
Expand Down
12 changes: 6 additions & 6 deletions src/inet/tests/TestInetLayerCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ bool WasSuccessful(const TestStatus & aTestStatus)
return (lStatus);
}

static void FillDataBufferPattern(uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
static void FillDataBufferPattern(uint8_t * aBuffer, uint32_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
{
for (uint16_t i = aPatternStartOffset; i < aLength; i++)
for (uint32_t i = aPatternStartOffset; i < aLength; i++)
{
const uint8_t lValue = static_cast<uint8_t>(aFirstValue & 0xFF);

Expand All @@ -135,9 +135,9 @@ static void FillDataBufferPattern(uint8_t * aBuffer, uint16_t aLength, uint16_t
}
}

static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint16_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint32_t aLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
{
for (uint16_t i = aPatternStartOffset; i < aLength; i++)
for (uint32_t i = aPatternStartOffset; i < aLength; i++)
{
const uint8_t lValue = aBuffer[i];

Expand All @@ -156,7 +156,7 @@ static bool CheckDataBufferPattern(const uint8_t * aBuffer, uint16_t aLength, ui
return true;
}

static PacketBufferHandle MakeDataBuffer(uint16_t aDesiredLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
static PacketBufferHandle MakeDataBuffer(uint32_t aDesiredLength, uint16_t aPatternStartOffset, uint8_t aFirstValue)
{
VerifyOrReturnError(aPatternStartOffset <= aDesiredLength, PacketBufferHandle());

Expand Down Expand Up @@ -244,7 +244,7 @@ static bool HandleDataReceived(const PacketBufferHandle & aBuffer, TransferStats

for (PacketBufferHandle lBuffer = aBuffer.Retain(); !lBuffer.IsNull(); lBuffer.Advance())
{
const uint16_t lDataLength = lBuffer->DataLength();
const uint32_t lDataLength = lBuffer->DataLength();
const uint8_t * const p = lBuffer->Start();

if (aCheckBuffer && !CheckDataBufferPattern(p, lDataLength, aPatternStartOffset, aFirstValue))
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/tests/TestTLV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void TestBufferContents(nlTestSuite * inSuite, const System::PacketBufferHandle
System::PacketBufferHandle buf = buffer.Retain();
while (!buf.IsNull())
{
uint16_t len = buf->DataLength();
uint32_t len = buf->DataLength();
NL_TEST_ASSERT(inSuite, len <= expectedLen);

NL_TEST_ASSERT(inSuite, memcmp(buf->Start(), expectedVal, len) == 0);
Expand Down Expand Up @@ -2990,7 +2990,7 @@ void CheckBufferOverflow(nlTestSuite * inSuite, void * inContext)
System::PacketBufferTLVReader reader;

System::PacketBufferHandle buf = System::PacketBufferHandle::New(sizeof(Encoding1), 0);
uint16_t maxDataLen = buf->MaxDataLength();
uint32_t maxDataLen = buf->MaxDataLength();
uint16_t reserve = static_cast<uint16_t>((sizeof(Encoding1) < maxDataLen) ? (maxDataLen - sizeof(Encoding1)) + 2 : 0);

// Repeatedly write and read a TLV encoding to a chain of PacketBuffers. Use progressively larger
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
TLV::TLVType containerType = TLV::kTLVType_Structure;

const uint8_t * buf = msg->Start();
const uint16_t bufLen = msg->DataLength();
const uint32_t bufLen = msg->DataLength();

constexpr size_t kCaseOverheadForFutureTbeData = 128;

Expand Down
Loading

0 comments on commit e0f445b

Please sign in to comment.