Matter SDK Coverage Report
Current view: top level - wifipaf - WiFiPAFEndPoint.cpp (source / functions) Coverage Total Hit
Test: SHA:704d97f9c619242ad76fcf75aeabc67802fa72d4 Lines: 81.4 % 490 399
Test Date: 2026-05-18 07:37:39 Functions: 90.7 % 43 39

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2025 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : /**
      19              :  *    @file
      20              :  *      This file implements a WiFiPAF endpoint abstraction for CHIP over WiFiPAF (CHIPoPAF)
      21              :  *      Public Action Frame Transport Protocol (PAFTP).
      22              :  *
      23              :  */
      24              : 
      25              : #include "WiFiPAFEndPoint.h"
      26              : 
      27              : #include <cstdint>
      28              : #include <cstring>
      29              : #include <utility>
      30              : 
      31              : #include <lib/support/BitFlags.h>
      32              : #include <lib/support/BufferReader.h>
      33              : #include <lib/support/CodeUtils.h>
      34              : #include <lib/support/logging/CHIPLogging.h>
      35              : #include <system/SystemClock.h>
      36              : #include <system/SystemLayer.h>
      37              : #include <system/SystemPacketBuffer.h>
      38              : 
      39              : #include "WiFiPAFConfig.h"
      40              : #include "WiFiPAFError.h"
      41              : #include "WiFiPAFLayer.h"
      42              : #include "WiFiPAFTP.h"
      43              : 
      44              : // Define below to enable extremely verbose, WiFiPAF end point-specific debug logging.
      45              : #undef CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_ENABLED
      46              : #define CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_LEVEL 0
      47              : 
      48              : #ifdef CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_ENABLED
      49              : #define ChipLogDebugWiFiPAFEndPoint_L0(MOD, MSG, ...) ChipLogDetail(MOD, MSG, ##__VA_ARGS__)
      50              : #if (CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_LEVEL == 0)
      51              : #define ChipLogDebugWiFiPAFEndPoint(MOD, MSG, ...)
      52              : #else
      53              : #define ChipLogDebugWiFiPAFEndPoint(MOD, MSG, ...) ChipLogDetail(MOD, MSG, ##__VA_ARGS__)
      54              : #endif // CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_LEVEL
      55              : #define ChipLogDebugBufferWiFiPAFEndPoint(MOD, BUF)                                                                                \
      56              :     ChipLogByteSpan(MOD, ByteSpan((BUF)->Start(), ((BUF)->DataLength() < 8 ? (BUF)->DataLength() : 8u)))
      57              : #else
      58              : #define ChipLogDebugWiFiPAFEndPoint(MOD, MSG, ...)
      59              : #define ChipLogDebugBufferWiFiPAFEndPoint(MOD, BUF)
      60              : #endif
      61              : 
      62              : /**
      63              :  *  @def WIFIPAF_CONFIG_IMMEDIATE_ACK_WINDOW_THRESHOLD
      64              :  *
      65              :  *  @brief
      66              :  *    If an end point's receive window drops equal to or below this value, it will send an immediate acknowledgement
      67              :  *    packet to re-open its window instead of waiting for the send-ack timer to expire.
      68              :  *
      69              :  */
      70              : #define WIFIPAF_CONFIG_IMMEDIATE_ACK_WINDOW_THRESHOLD 1
      71              : 
      72              : #define WIFIPAF_ACK_SEND_TIMEOUT_MS 2500
      73              : #define WIFIPAF_WAIT_RES_TIMEOUT_MS 1000
      74              : // Drop the connection if network resources remain unavailable for the period.
      75              : #define WIFIPAF_MAX_RESOURCE_BLOCK_COUNT (PAFTP_CONN_IDLE_TIMEOUT_MS / WIFIPAF_WAIT_RES_TIMEOUT_MS)
      76              : 
      77              : /**
      78              :  *  @def WIFIPAF_WINDOW_NO_ACK_SEND_THRESHOLD
      79              :  *
      80              :  *  @brief
      81              :  *    Data fragments may only be sent without piggybacked acks if receiver's window size is above this threshold.
      82              :  *
      83              :  */
      84              : #define WIFIPAF_WINDOW_NO_ACK_SEND_THRESHOLD 1
      85              : 
      86              : namespace chip {
      87              : namespace WiFiPAF {
      88              : 
      89            1 : CHIP_ERROR WiFiPAFEndPoint::StartConnect()
      90              : {
      91            1 :     CHIP_ERROR err = CHIP_NO_ERROR;
      92              :     PAFTransportCapabilitiesRequestMessage req;
      93            1 :     PacketBufferHandle buf;
      94            1 :     constexpr uint8_t numVersions =
      95              :         CHIP_PAF_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION - CHIP_PAF_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION + 1;
      96              :     static_assert(numVersions <= NUM_PAFTP_SUPPORTED_PROTOCOL_VERSIONS, "Incompatibly protocol versions");
      97              : 
      98              :     // Ensure we're in the correct state.
      99            1 :     VerifyOrExit(mState == kState_Ready, err = CHIP_ERROR_INCORRECT_STATE);
     100            1 :     mState = kState_Connecting;
     101              : 
     102              :     // Build PAF transport protocol capabilities request.
     103            1 :     buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
     104            1 :     VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY);
     105              : 
     106              :     // Zero-initialize PAF transport capabilities request.
     107            1 :     memset(&req, 0, sizeof(req));
     108            1 :     req.mMtu        = CHIP_PAF_DEFAULT_MTU;
     109            1 :     req.mWindowSize = PAF_MAX_RECEIVE_WINDOW_SIZE;
     110              : 
     111              :     // Populate request with highest supported protocol versions
     112            2 :     for (uint8_t i = 0; i < numVersions; i++)
     113              :     {
     114            1 :         req.SetSupportedProtocolVersion(i, static_cast<uint8_t>(CHIP_PAF_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION - i));
     115              :     }
     116              : 
     117            1 :     err = req.Encode(buf);
     118            1 :     SuccessOrExit(err);
     119              : 
     120              :     // Start connect timer. Canceled when end point freed or connection established.
     121            1 :     err = StartConnectTimer();
     122            1 :     SuccessOrExit(err);
     123              : 
     124              :     // Send PAF transport capabilities request to peripheral.
     125              :     // Add reference to message fragment. CHIP retains partial ownership of message fragment's packet buffer,
     126              :     // since this is the same buffer as that of the whole message, just with a fragmenter-modified payload offset
     127              :     // and data length, by a Retain() on the handle when calling this function.
     128            1 :     err = SendWrite(buf.Retain());
     129            1 :     SuccessOrExit(err);
     130              :     // Free request buffer on write confirmation. Stash a reference to it in mSendQueue, which we don't use anyway
     131              :     // until the connection has been set up.
     132            1 :     QueueTx(std::move(buf), kType_Data);
     133              : 
     134            1 : exit:
     135              :     // If we failed to initiate the connection, close the end point.
     136            2 :     if (err != CHIP_NO_ERROR)
     137              :     {
     138            0 :         StopConnectTimer();
     139            0 :         DoClose(kWiFiPAFCloseFlag_AbortTransmission, err);
     140              :     }
     141              : 
     142            2 :     return err;
     143            1 : }
     144              : 
     145            2 : CHIP_ERROR WiFiPAFEndPoint::HandleConnectComplete()
     146              : {
     147            2 :     CHIP_ERROR err = CHIP_NO_ERROR;
     148              : 
     149            2 :     mState = kState_Connected;
     150              :     // Cancel the connect timer.
     151            2 :     StopConnectTimer();
     152              : 
     153              :     // We've successfully completed the PAF transport protocol handshake, so let the application know we're open for business.
     154            2 :     if (mWiFiPafLayer != nullptr)
     155              :     {
     156              :         // Indicate connect complete to next-higher layer.
     157            2 :         mWiFiPafLayer->OnEndPointConnectComplete(this, CHIP_NO_ERROR);
     158              :     }
     159              :     else
     160              :     {
     161              :         // If no connect complete callback has been set up, close the end point.
     162            0 :         err = WIFIPAF_ERROR_NO_CONNECT_COMPLETE_CALLBACK;
     163              :     }
     164            2 :     return err;
     165              : }
     166              : 
     167           14 : bool WiFiPAFEndPoint::IsConnected(uint8_t state) const
     168              : {
     169           14 :     return (state == kState_Connected || state == kState_Closing);
     170              : }
     171              : 
     172            3 : void WiFiPAFEndPoint::DoClose(uint8_t flags, CHIP_ERROR err)
     173              : {
     174            3 :     uint8_t oldState = mState;
     175              : 
     176              :     // If end point is not closed or closing, OR end point was closing gracefully, but tx abort has been specified...
     177            3 :     if ((mState != kState_Closed && mState != kState_Closing) ||
     178            0 :         (mState == kState_Closing && (flags & kWiFiPAFCloseFlag_AbortTransmission)))
     179              :     {
     180              :         // Cancel Connect and ReceiveConnect timers if they are running.
     181              :         // Check role first to avoid needless iteration over timer pool.
     182            3 :         if (mRole == kWiFiPafRole_Subscriber)
     183              :         {
     184            1 :             StopConnectTimer();
     185              :         }
     186              : 
     187              :         // Free the packets in re-order queue if ones exist
     188           21 :         for (uint8_t qidx = 0; qidx < PAFTP_REORDER_QUEUE_SIZE; qidx++)
     189              :         {
     190           18 :             if (!ReorderQueue[qidx].IsNull())
     191              :             {
     192            2 :                 ReorderQueue[qidx] = nullptr;
     193              :             }
     194              :         }
     195            3 :         ItemsInReorderQueue = 0;
     196              : 
     197              :         // If transmit buffer is empty or a transmission abort was specified...
     198            3 :         if (mPafTP.TxState() == WiFiPAFTP::kState_Idle || (flags & kWiFiPAFCloseFlag_AbortTransmission))
     199              :         {
     200            3 :             FinalizeClose(oldState, flags, err);
     201              :         }
     202              :         else
     203              :         {
     204              :             // Wait for send queue and fragmenter's tx buffer to become empty, to ensure all pending messages have been
     205              :             // sent. Only free end point and tell platform it can throw away the underlying connection once all
     206              :             // pending messages have been sent and acknowledged by the remote CHIPoPAF stack, or once the remote stack
     207              :             // closes the CHIPoPAF connection.
     208              :             //
     209              :             // In so doing, WiFiPAFEndPoint attempts to emulate the level of reliability afforded by TCPEndPoint and TCP
     210              :             // sockets in general with a typical default SO_LINGER option. That said, there is no hard guarantee that
     211              :             // pending messages will be sent once (Do)Close() is called, so developers should use application-level
     212              :             // messages to confirm the receipt of all data sent prior to a Close() call.
     213            0 :             mState = kState_Closing;
     214              : 
     215            0 :             if ((flags & kWiFiPAFCloseFlag_SuppressCallback) == 0)
     216              :             {
     217            0 :                 DoCloseCallback(oldState, flags, err);
     218              :             }
     219              :         }
     220              :     }
     221            3 : }
     222              : 
     223            3 : void WiFiPAFEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err)
     224              : {
     225            3 :     mState = kState_Closed;
     226              : 
     227              :     // Ensure transmit queue is empty and set to NULL.
     228            3 :     mSendQueue = nullptr;
     229              :     // Clear the session information
     230            3 :     ChipLogProgress(WiFiPAF, "Shutdown PAF session (%u, %u)", mSessionInfo.id, mSessionInfo.role);
     231            3 :     TEMPORARY_RETURN_IGNORED mWiFiPafLayer->mWiFiPAFTransport->WiFiPAFCloseSession(mSessionInfo);
     232            3 :     memset(&mSessionInfo, 0, sizeof(mSessionInfo));
     233              :     // Fire application's close callback if we haven't already, and it's not suppressed.
     234            3 :     if (oldState != kState_Closing && (flags & kWiFiPAFCloseFlag_SuppressCallback) == 0)
     235              :     {
     236            2 :         DoCloseCallback(oldState, flags, err);
     237              :     }
     238              : 
     239              :     // If underlying WiFiPAF connection has closed, connection object is invalid, so just free the end point and return.
     240            9 :     if (err == WIFIPAF_ERROR_REMOTE_DEVICE_DISCONNECTED || err == WIFIPAF_ERROR_APP_CLOSED_CONNECTION)
     241              :     {
     242            2 :         Free();
     243              :     }
     244              :     else // Otherwise, try to signal close to remote device before end point releases WiFiPAF connection and frees itself.
     245              :     {
     246            1 :         if (mRole == kWiFiPafRole_Subscriber)
     247              :         {
     248              :             // Cancel send and receive-ack timers, if running.
     249            0 :             StopAckReceivedTimer();
     250            0 :             StopSendAckTimer();
     251            0 :             StopWaitResourceTimer();
     252            0 :             mConnStateFlags.Set(ConnectionStateFlag::kOperationInFlight);
     253              :         }
     254              :         else
     255              :         {
     256            1 :             Free();
     257              :         }
     258              :     }
     259            3 :     ClearAll();
     260            3 : }
     261              : 
     262            2 : void WiFiPAFEndPoint::DoCloseCallback(uint8_t state, uint8_t flags, CHIP_ERROR err)
     263              : {
     264              :     // Callback fires once per end point lifetime.
     265            2 :     mOnPafSubscribeComplete = nullptr;
     266            2 :     mOnPafSubscribeError    = nullptr;
     267            2 :     OnConnectionClosed      = nullptr;
     268            2 : }
     269              : 
     270            3 : void WiFiPAFEndPoint::Free()
     271              : {
     272              :     // Clear fragmentation and reassembly engine's Tx and Rx buffers. Counters will be reset by next engine init.
     273            3 :     FreePAFtpEngine();
     274              : 
     275              :     // Clear pending ack buffer, if any.
     276            3 :     mAckToSend = nullptr;
     277              : 
     278              :     // Cancel all timers.
     279            3 :     StopConnectTimer();
     280            3 :     StopAckReceivedTimer();
     281            3 :     StopSendAckTimer();
     282            3 :     StopWaitResourceTimer();
     283              : 
     284              :     // Clear callbacks.
     285            3 :     mOnPafSubscribeComplete = nullptr;
     286            3 :     mOnPafSubscribeError    = nullptr;
     287            3 :     OnMessageReceived       = nullptr;
     288            3 :     OnConnectionClosed      = nullptr;
     289            3 : }
     290              : 
     291            3 : void WiFiPAFEndPoint::FreePAFtpEngine()
     292              : {
     293              :     // Free transmit disassembly buffer
     294            3 :     mPafTP.ClearTxPacket();
     295              : 
     296              :     // Free receive reassembly buffer
     297            3 :     mPafTP.ClearRxPacket();
     298            3 : }
     299              : 
     300            3 : CHIP_ERROR WiFiPAFEndPoint::Init(WiFiPAFLayer * WiFiPafLayer, WiFiPAFSession & SessionInfo)
     301              : {
     302              :     // Fail if already initialized.
     303            3 :     VerifyOrReturnError(mWiFiPafLayer == nullptr, CHIP_ERROR_INCORRECT_STATE);
     304              : 
     305              :     // Validate args.
     306            3 :     VerifyOrReturnError(WiFiPafLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
     307              : 
     308              :     // If end point plays subscriber role, expect ack as last step of PAFTP handshake.
     309              :     // If being publisher, subscriber's handshake indication 'ack's write sent by publisher to kick off the PAFTP handshake.
     310            3 :     bool expectInitialAck = (SessionInfo.role == kWiFiPafRole_Publisher);
     311              : 
     312            3 :     CHIP_ERROR err = mPafTP.Init(this, expectInitialAck);
     313            6 :     if (err != CHIP_NO_ERROR)
     314              :     {
     315            0 :         ChipLogError(WiFiPAF, "WiFiPAFTP init failed");
     316            0 :         return err;
     317              :     }
     318              : 
     319            3 :     mWiFiPafLayer = WiFiPafLayer;
     320              : 
     321              :     // WiFiPAF EndPoint data members:
     322            3 :     memcpy(&mSessionInfo, &SessionInfo, sizeof(mSessionInfo));
     323            3 :     mRole = SessionInfo.role;
     324            3 :     mTimerStateFlags.ClearAll();
     325            3 :     mLocalReceiveWindowSize  = 0;
     326            3 :     mRemoteReceiveWindowSize = 0;
     327            3 :     mReceiveWindowMaxSize    = 0;
     328            3 :     mSendQueue               = nullptr;
     329            3 :     mAckToSend               = nullptr;
     330              : 
     331              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "initialized local rx window, size = %u", mLocalReceiveWindowSize);
     332              : 
     333              :     // End point is ready.
     334            3 :     mState = kState_Ready;
     335              : 
     336            3 :     return CHIP_NO_ERROR;
     337              : }
     338              : 
     339            9 : CHIP_ERROR WiFiPAFEndPoint::SendCharacteristic(PacketBufferHandle && buf)
     340              : {
     341            9 :     CHIP_ERROR err = CHIP_NO_ERROR;
     342              : 
     343            9 :     SuccessOrExit(err = SendWrite(std::move(buf)));
     344              :     // Write succeeded, so shrink remote receive window counter by 1.
     345            9 :     mRemoteReceiveWindowSize = static_cast<SequenceNumber_t>(mRemoteReceiveWindowSize - 1);
     346              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "decremented remote rx window, new size = %u", mRemoteReceiveWindowSize);
     347            9 : exit:
     348            9 :     return err;
     349              : }
     350              : 
     351              : /*
     352              :  *  Routine to queue the Tx packet with a packet type
     353              :  *  kType_Data(0)       - data packet
     354              :  *  kType_Control(1)    - control packet
     355              :  */
     356            9 : void WiFiPAFEndPoint::QueueTx(PacketBufferHandle && data, PacketType_t type)
     357              : {
     358            9 :     if (mSendQueue.IsNull())
     359              :     {
     360            7 :         mSendQueue = std::move(data);
     361              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "%s: Set data as new mSendQueue %p, type %d", __FUNCTION__, mSendQueue->Start(), type);
     362              :     }
     363              :     else
     364              :     {
     365            2 :         mSendQueue->AddToEnd(std::move(data));
     366              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "%s: Append data to mSendQueue %p, type %d", __FUNCTION__, mSendQueue->Start(), type);
     367              :     }
     368            9 : }
     369              : 
     370            7 : CHIP_ERROR WiFiPAFEndPoint::Send(PacketBufferHandle && data)
     371              : {
     372            7 :     CHIP_ERROR err = CHIP_NO_ERROR;
     373              : 
     374            7 :     VerifyOrExit(!data.IsNull(), err = CHIP_ERROR_INVALID_ARGUMENT);
     375            7 :     VerifyOrExit(IsConnected(mState), err = CHIP_ERROR_INCORRECT_STATE);
     376              : 
     377              :     // Ensure outgoing message fits in a single contiguous packet buffer, as currently required by the
     378              :     // message fragmentation and reassembly engine.
     379            7 :     if (data->HasChainedBuffer())
     380              :     {
     381            1 :         data->CompactHead();
     382              : 
     383            1 :         if (data->HasChainedBuffer())
     384              :         {
     385            0 :             err = CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG;
     386            0 :             ExitNow();
     387              :         }
     388              :     }
     389              : 
     390              :     // Add new message to send queue.
     391            7 :     QueueTx(std::move(data), kType_Data);
     392              : 
     393              :     // Send first fragment of new message, if we can.
     394            7 :     err = DriveSending();
     395            7 :     SuccessOrExit(err);
     396            7 : exit:
     397           14 :     if (err != CHIP_NO_ERROR)
     398              :     {
     399            0 :         DoClose(kWiFiPAFCloseFlag_AbortTransmission, err);
     400              :     }
     401              : 
     402            7 :     return err;
     403              : }
     404              : 
     405            8 : bool WiFiPAFEndPoint::PrepareNextFragment(PacketBufferHandle && data, bool & sentAck)
     406              : {
     407              :     // If we have a pending fragment acknowledgement to send, piggyback it on the fragment we're about to transmit.
     408            8 :     if (mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning))
     409              :     {
     410              :         // Reset local receive window counter.
     411            2 :         mLocalReceiveWindowSize = mReceiveWindowMaxSize;
     412              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "reset local rx window on piggyback ack tx, size = %u", mLocalReceiveWindowSize);
     413              : 
     414              :         // Tell caller AND fragmenter we have an ack to piggyback.
     415            2 :         sentAck = true;
     416              :     }
     417              :     else
     418              :     {
     419              :         // No ack to piggyback.
     420            6 :         sentAck = false;
     421              :     }
     422              : 
     423            8 :     return mPafTP.HandleCharacteristicSend(std::move(data), sentAck);
     424              : }
     425              : 
     426            7 : CHIP_ERROR WiFiPAFEndPoint::SendNextMessage()
     427              : {
     428              :     // Get the first queued packet to send
     429            7 :     PacketBufferHandle data = mSendQueue.PopHead();
     430              : 
     431              :     // Hand whole message payload to the fragmenter.
     432              :     bool sentAck;
     433            7 :     VerifyOrReturnError(PrepareNextFragment(std::move(data), sentAck), WIFIPAF_ERROR_CHIPPAF_PROTOCOL_ABORT);
     434              : 
     435            7 :     ReturnErrorOnFailure(SendCharacteristic(mPafTP.BorrowTxPacket()));
     436              : 
     437            7 :     if (sentAck)
     438              :     {
     439              :         // If sent piggybacked ack, stop send-ack timer.
     440            2 :         StopSendAckTimer();
     441              :     }
     442              : 
     443              :     // Start ack received timer, if it's not already running.
     444            7 :     return StartAckReceivedTimer();
     445            7 : }
     446              : 
     447            1 : CHIP_ERROR WiFiPAFEndPoint::ContinueMessageSend()
     448              : {
     449              :     bool sentAck;
     450              : 
     451            1 :     if (!PrepareNextFragment(nullptr, sentAck))
     452              :     {
     453              :         // Log PAFTP error
     454            0 :         ChipLogError(WiFiPAF, "paftp fragmenter error on send!");
     455            0 :         mPafTP.LogState();
     456              : 
     457            0 :         return WIFIPAF_ERROR_CHIPPAF_PROTOCOL_ABORT;
     458              :     }
     459              : 
     460            1 :     ReturnErrorOnFailure(SendCharacteristic(mPafTP.BorrowTxPacket()));
     461              : 
     462            1 :     if (sentAck)
     463              :     {
     464              :         // If sent piggybacked ack, stop send-ack timer.
     465            0 :         StopSendAckTimer();
     466              :     }
     467              : 
     468              :     // Start ack received timer, if it's not already running.
     469            1 :     return StartAckReceivedTimer();
     470              : }
     471              : 
     472            2 : CHIP_ERROR WiFiPAFEndPoint::HandleHandshakeConfirmationReceived()
     473              : {
     474              :     // Free capabilities request/response payload.
     475            2 :     mSendQueue.FreeHead();
     476              : 
     477            2 :     return CHIP_NO_ERROR;
     478              : }
     479              : 
     480            7 : CHIP_ERROR WiFiPAFEndPoint::HandleFragmentConfirmationReceived(bool result)
     481              : {
     482            7 :     CHIP_ERROR err = CHIP_NO_ERROR;
     483              :     // Ensure we're in correct state to receive confirmation of non-handshake GATT send.
     484            7 :     VerifyOrExit(IsConnected(mState), err = CHIP_ERROR_INCORRECT_STATE);
     485              : 
     486            7 :     if (mConnStateFlags.Has(ConnectionStateFlag::kStandAloneAckInFlight))
     487              :     {
     488              :         // If confirmation was received for stand-alone ack, free its tx buffer.
     489            0 :         mAckToSend = nullptr;
     490            0 :         mConnStateFlags.Clear(ConnectionStateFlag::kStandAloneAckInFlight);
     491              :     }
     492              : 
     493            7 :     if (result != true)
     494              :     {
     495              :         // Something wrong in writing packets
     496            0 :         ChipLogError(WiFiPAF, "Failed to send PAF packet");
     497            0 :         err = CHIP_ERROR_SENDING_BLOCKED;
     498            0 :         StopAckReceivedTimer();
     499            0 :         SuccessOrExit(err);
     500              :     }
     501              : 
     502              :     // If local receive window size has shrunk to or below immediate ack threshold, AND a message fragment is not
     503              :     // pending on which to piggyback an ack, send immediate stand-alone ack.
     504              :     //
     505              :     // This check covers the case where the local receive window has shrunk between transmission and confirmation of
     506              :     // the stand-alone ack, and also the case where a window size < the immediate ack threshold was detected in
     507              :     // Receive(), but the stand-alone ack was deferred due to a pending outbound message fragment.
     508            7 :     if (mLocalReceiveWindowSize <= WIFIPAF_CONFIG_IMMEDIATE_ACK_WINDOW_THRESHOLD && mSendQueue.IsNull() &&
     509            0 :         mPafTP.TxState() != WiFiPAFTP::kState_InProgress)
     510              :     {
     511            0 :         err = DriveStandAloneAck(); // Encode stand-alone ack and drive sending.
     512            0 :         SuccessOrExit(err);
     513              :     }
     514              :     else
     515              :     {
     516            7 :         err = DriveSending();
     517            7 :         SuccessOrExit(err);
     518              :     }
     519              : 
     520            7 : exit:
     521           14 :     if (err != CHIP_NO_ERROR)
     522              :     {
     523            0 :         DoClose(kWiFiPAFCloseFlag_AbortTransmission, err);
     524              :     }
     525              : 
     526            7 :     return err;
     527              : }
     528              : 
     529            9 : CHIP_ERROR WiFiPAFEndPoint::HandleSendConfirmationReceived(bool result)
     530              : {
     531              :     // Mark outstanding operation as finished.
     532            9 :     mConnStateFlags.Clear(ConnectionStateFlag::kOperationInFlight);
     533              : 
     534              :     // If confirmation was for outbound portion of PAFTP connect handshake...
     535            9 :     if (!mConnStateFlags.Has(ConnectionStateFlag::kCapabilitiesConfReceived))
     536              :     {
     537            2 :         mConnStateFlags.Set(ConnectionStateFlag::kCapabilitiesConfReceived);
     538            2 :         return HandleHandshakeConfirmationReceived();
     539              :     }
     540              : 
     541            7 :     return HandleFragmentConfirmationReceived(result);
     542              : }
     543              : 
     544            1 : CHIP_ERROR WiFiPAFEndPoint::DriveStandAloneAck()
     545              : {
     546              :     // Stop send-ack timer if running.
     547            1 :     StopSendAckTimer();
     548              : 
     549              :     // If stand-alone ack not already pending, allocate new payload buffer here.
     550            1 :     if (mAckToSend.IsNull())
     551              :     {
     552            1 :         mAckToSend = System::PacketBufferHandle::New(kTransferProtocolStandaloneAckHeaderSize);
     553            1 :         VerifyOrReturnError(!mAckToSend.IsNull(), CHIP_ERROR_NO_MEMORY);
     554              :     }
     555              : 
     556              :     // Attempt to send stand-alone ack.
     557            1 :     return DriveSending();
     558              : }
     559              : 
     560            1 : CHIP_ERROR WiFiPAFEndPoint::DoSendStandAloneAck()
     561              : {
     562              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "sending stand-alone ack");
     563              : 
     564              :     // Encode and transmit stand-alone ack.
     565            1 :     ReturnErrorOnFailure(mPafTP.EncodeStandAloneAck(mAckToSend));
     566            1 :     ReturnErrorOnFailure(SendCharacteristic(mAckToSend.Retain()));
     567              : 
     568              :     // Reset local receive window counter.
     569            1 :     mLocalReceiveWindowSize = mReceiveWindowMaxSize;
     570              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "reset local rx window on stand-alone ack tx, size = %u", mLocalReceiveWindowSize);
     571              : 
     572            1 :     mConnStateFlags.Set(ConnectionStateFlag::kStandAloneAckInFlight);
     573              : 
     574              :     // Start ack received timer, if it's not already running.
     575            1 :     return StartAckReceivedTimer();
     576              : }
     577              : 
     578           19 : CHIP_ERROR WiFiPAFEndPoint::DriveSending()
     579              : {
     580              :     // If receiver's window is almost closed and we don't have an ack to send, OR we do have an ack to send but
     581              :     // receiver's window is completely empty, OR another operation is in flight, awaiting confirmation...
     582           39 :     if ((mRemoteReceiveWindowSize <= WIFIPAF_WINDOW_NO_ACK_SEND_THRESHOLD &&
     583            1 :          !mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning) && mAckToSend.IsNull()) ||
     584           20 :         (mRemoteReceiveWindowSize == 0) || (mConnStateFlags.Has(ConnectionStateFlag::kOperationInFlight)))
     585              :     {
     586            4 :         if (mRemoteReceiveWindowSize <= WIFIPAF_WINDOW_NO_ACK_SEND_THRESHOLD &&
     587            3 :             !mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning) && mAckToSend.IsNull())
     588              :         {
     589              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "NO SEND: receive window almost closed, and no ack to send");
     590              :         }
     591              : 
     592            3 :         if (mRemoteReceiveWindowSize == 0)
     593              :         {
     594              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "NO SEND: remote receive window closed");
     595              :         }
     596              : 
     597            3 :         if (mConnStateFlags.Has(ConnectionStateFlag::kOperationInFlight))
     598              :         {
     599              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "NO SEND: Operation in flight");
     600              :         }
     601              :         // Can't send anything.
     602            3 :         return CHIP_NO_ERROR;
     603              :     }
     604              : 
     605           16 :     if (!mWiFiPafLayer->mWiFiPAFTransport->WiFiPAFResourceAvailable() && (!mAckToSend.IsNull() || !mSendQueue.IsNull()))
     606              :     {
     607              :         // Resource is currently unavailable, send packets later
     608            1 :         return StartWaitResourceTimer();
     609              :     }
     610           15 :     mResourceWaitCount = 0;
     611              : 
     612              :     // Otherwise, let's see what we can send.
     613           15 :     if ((!mAckToSend.IsNull()) && !mConnStateFlags.Has(ConnectionStateFlag::kStandAloneAckInFlight))
     614              :     {
     615              :         // If immediate, stand-alone ack is pending, send it.
     616            0 :         ChipLogProgress(WiFiPAF, "Send the pending stand-alone ack");
     617            0 :         ReturnErrorOnFailure(DoSendStandAloneAck());
     618              :     }
     619           15 :     else if (mPafTP.TxState() == WiFiPAFTP::kState_Idle) // Else send next message fragment, if any.
     620              :     {
     621              :         // Fragmenter's idle, let's see what's in the send queue...
     622            9 :         if (!mSendQueue.IsNull())
     623              :         {
     624              :             // Transmit first fragment of next whole message in send queue.
     625            6 :             ReturnErrorOnFailure(SendNextMessage());
     626              :         }
     627              :         else
     628              :         {
     629              :             // Nothing to send!
     630              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "=> No pending packets, nothing to send!");
     631              :         }
     632              :     }
     633            6 :     else if (mPafTP.TxState() == WiFiPAFTP::kState_InProgress)
     634              :     {
     635              :         // Send next fragment of message currently held by fragmenter.
     636              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "Send the next fragment");
     637            1 :         ReturnErrorOnFailure(ContinueMessageSend());
     638              :     }
     639            5 :     else if (mPafTP.TxState() == WiFiPAFTP::kState_Complete)
     640              :     {
     641              :         // Clear fragmenter's pointer to sent message buffer and reset its Tx state.
     642              :         // Buffer will be freed at scope exit.
     643            5 :         PacketBufferHandle sentBuf = mPafTP.TakeTxPacket();
     644              : 
     645            5 :         if (!mSendQueue.IsNull())
     646              :         {
     647              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "Send the next pkt");
     648              :             // Transmit first fragment of next whole message in send queue.
     649            1 :             ReturnErrorOnFailure(SendNextMessage());
     650              :         }
     651            4 :         else if (mState == kState_Closing && !mPafTP.ExpectingAck()) // and mSendQueue is NULL, per above...
     652              :         {
     653              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "Closing and no expect ack!");
     654              :             // If end point closing, got last ack, and got out-of-order confirmation for last send, finalize close.
     655            0 :             FinalizeClose(mState, kWiFiPAFCloseFlag_SuppressCallback, CHIP_NO_ERROR);
     656              :         }
     657              :         else
     658              :         {
     659              :             // Nothing to send!
     660              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "No more packets to send");
     661              :         }
     662            5 :     }
     663              :     else
     664              :     {
     665            0 :         ChipLogError(WiFiPAF, "Unknown TxState: %u", mPafTP.TxState());
     666              :     }
     667           15 :     return CHIP_NO_ERROR;
     668              : }
     669              : 
     670            2 : CHIP_ERROR WiFiPAFEndPoint::HandleCapabilitiesRequestReceived(PacketBufferHandle && data)
     671              : {
     672              :     PAFTransportCapabilitiesRequestMessage req;
     673              :     PAFTransportCapabilitiesResponseMessage resp;
     674              :     uint16_t mtu;
     675              : 
     676            2 :     VerifyOrReturnError(!data.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
     677              : 
     678            2 :     mState = kState_Connecting;
     679              : 
     680              :     // Decode PAFTP capabilities request.
     681            2 :     ReturnErrorOnFailure(PAFTransportCapabilitiesRequestMessage::Decode(data, req));
     682              : 
     683            1 :     PacketBufferHandle responseBuf = System::PacketBufferHandle::New(kCapabilitiesResponseLength);
     684            1 :     VerifyOrReturnError(!responseBuf.IsNull(), CHIP_ERROR_NO_MEMORY);
     685              : 
     686            1 :     if (req.mMtu > 0) // If MTU was observed and provided by central...
     687              :     {
     688            1 :         mtu = req.mMtu; // Accept central's observation of the MTU.
     689              :     }
     690              :     else
     691              :     {
     692            0 :         mtu = CHIP_PAF_DEFAULT_MTU;
     693              :     }
     694              : 
     695              :     // Select fragment size for connection based on MTU.
     696            1 :     resp.mFragmentSize = std::min(static_cast<uint16_t>(mtu), WiFiPAFTP::sMaxFragmentSize);
     697              : 
     698              :     // Select local and remote max receive window size based on local resources available for both incoming writes
     699            1 :     auto windowSize = std::min(req.mWindowSize, static_cast<uint8_t>(PAF_MAX_RECEIVE_WINDOW_SIZE));
     700            1 :     if (windowSize < PAF_MIN_RECEIVE_WINDOW_SIZE)
     701              :     {
     702              :         // The Window size should be at least PAF_MIN_RECEIVE_WINDOW_SIZE to ensure PAF stability and avoid underflow problems.
     703            0 :         ChipLogError(WiFiPAF, "Small window size: %u, reject due to stability requirement", windowSize);
     704            0 :         mState = kState_Aborting;
     705            0 :         return CHIP_ERROR_INVALID_ARGUMENT;
     706              :     }
     707            1 :     mRemoteReceiveWindowSize = mLocalReceiveWindowSize = mReceiveWindowMaxSize = windowSize;
     708            1 :     resp.mWindowSize                                                           = mReceiveWindowMaxSize;
     709            1 :     ChipLogProgress(WiFiPAF, "local and remote recv window sizes = %u", resp.mWindowSize);
     710              : 
     711              :     // Select PAF transport protocol version from those supported by central, or none if no supported version found.
     712            1 :     resp.mSelectedProtocolVersion = WiFiPAFLayer::GetHighestSupportedProtocolVersion(req);
     713            1 :     ChipLogProgress(WiFiPAF, "selected PAFTP version %d", resp.mSelectedProtocolVersion);
     714              : 
     715            1 :     if (resp.mSelectedProtocolVersion == kWiFiPAFTransportProtocolVersion_None)
     716              :     {
     717              :         // If WiFiPAF transport protocol versions incompatible, prepare to close connection after capabilities response
     718              :         // has been sent.
     719            0 :         ChipLogError(WiFiPAF, "incompatible PAFTP versions; peripheral expected between %d and %d",
     720              :                      CHIP_PAF_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION, CHIP_PAF_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION);
     721            0 :         mState = kState_Aborting;
     722              :     }
     723              :     else
     724              :     {
     725              :         // Set Rx and Tx fragment sizes to the same value
     726            1 :         mPafTP.SetRxFragmentSize(resp.mFragmentSize);
     727            1 :         mPafTP.SetTxFragmentSize(resp.mFragmentSize);
     728              :     }
     729              : 
     730            1 :     ChipLogProgress(WiFiPAF, "using PAFTP fragment sizes rx %d / tx %d.", mPafTP.GetRxFragmentSize(), mPafTP.GetTxFragmentSize());
     731            1 :     ReturnErrorOnFailure(resp.Encode(responseBuf));
     732              : 
     733              :     CHIP_ERROR err;
     734            1 :     err = SendWrite(responseBuf.Retain());
     735            1 :     SuccessOrExit(err);
     736              : 
     737              :     // Stash capabilities response payload
     738            1 :     QueueTx(std::move(responseBuf), kType_Data);
     739              : 
     740              :     // Response has been sent
     741            1 :     return HandleConnectComplete();
     742            0 : exit:
     743            0 :     return err;
     744            1 : }
     745              : 
     746            1 : CHIP_ERROR WiFiPAFEndPoint::HandleCapabilitiesResponseReceived(PacketBufferHandle && data)
     747              : {
     748              :     PAFTransportCapabilitiesResponseMessage resp;
     749              : 
     750            1 :     VerifyOrReturnError(!data.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
     751              : 
     752              :     // Decode PAFTP capabilities response.
     753            1 :     ReturnErrorOnFailure(PAFTransportCapabilitiesResponseMessage::Decode(data, resp));
     754              : 
     755            1 :     VerifyOrReturnError(resp.mFragmentSize > 0, WIFIPAF_ERROR_INVALID_FRAGMENT_SIZE);
     756              : 
     757            1 :     ChipLogProgress(WiFiPAF, "Publisher chose PAFTP version %d; subscriber expected between %d and %d",
     758              :                     resp.mSelectedProtocolVersion, CHIP_PAF_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION,
     759              :                     CHIP_PAF_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION);
     760              : 
     761            1 :     if ((resp.mSelectedProtocolVersion < CHIP_PAF_TRANSPORT_PROTOCOL_MIN_SUPPORTED_VERSION) ||
     762            1 :         (resp.mSelectedProtocolVersion > CHIP_PAF_TRANSPORT_PROTOCOL_MAX_SUPPORTED_VERSION))
     763              :     {
     764            0 :         return WIFIPAF_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS;
     765              :     }
     766              : 
     767              :     // Set fragment size as minimum of (reported ATT MTU, BTP characteristic size)
     768            1 :     resp.mFragmentSize = std::min(resp.mFragmentSize, WiFiPAFTP::sMaxFragmentSize);
     769              : 
     770            1 :     mPafTP.SetRxFragmentSize(resp.mFragmentSize);
     771            1 :     mPafTP.SetTxFragmentSize(resp.mFragmentSize);
     772              : 
     773            1 :     ChipLogProgress(WiFiPAF, "using PAFTP fragment sizes rx %d / tx %d.", mPafTP.GetRxFragmentSize(), mPafTP.GetTxFragmentSize());
     774              : 
     775              :     // Select local and remote max receive window size based on local resources available for both incoming indications
     776            1 :     if (resp.mWindowSize < PAF_MIN_RECEIVE_WINDOW_SIZE)
     777              :     {
     778              :         // The Window size should be at least PAF_MIN_RECEIVE_WINDOW_SIZE to ensure PAF stability and avoid underflow problems.
     779            0 :         ChipLogError(WiFiPAF, "Small window size: %u, reject due to stability requirement", resp.mWindowSize);
     780            0 :         mState = kState_Aborting;
     781            0 :         return CHIP_ERROR_INVALID_ARGUMENT;
     782              :     }
     783            1 :     mRemoteReceiveWindowSize = mLocalReceiveWindowSize = mReceiveWindowMaxSize = resp.mWindowSize;
     784            1 :     ChipLogProgress(WiFiPAF, "local and remote recv window size = %u", resp.mWindowSize);
     785              : 
     786              :     // Shrink local receive window counter by 1, since connect handshake indication requires acknowledgement.
     787            1 :     mLocalReceiveWindowSize = static_cast<SequenceNumber_t>(mLocalReceiveWindowSize - 1);
     788              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "decremented local rx window, new size = %u", mLocalReceiveWindowSize);
     789              : 
     790              :     // Send ack for connection handshake indication when timer expires. Sequence numbers always start at 0,
     791              :     // and the reassembler's "last received seq num" is initialized to 0 and updated when new fragments are
     792              :     // received from the peripheral, so we don't need to explicitly mark the ack num to send here.
     793            1 :     ReturnErrorOnFailure(StartSendAckTimer());
     794              : 
     795              :     // We've sent a capabilities request write and received a compatible response, so the connect
     796              :     // operation has completed successfully.
     797            1 :     return HandleConnectComplete();
     798              : }
     799              : 
     800              : // Returns number of open slots in remote receive window given the input values.
     801            4 : SequenceNumber_t WiFiPAFEndPoint::AdjustRemoteReceiveWindow(SequenceNumber_t lastReceivedAck, SequenceNumber_t maxRemoteWindowSize,
     802              :                                                             SequenceNumber_t newestUnackedSentSeqNum)
     803              : {
     804              :     // Assumption: SequenceNumber_t is uint8_t.
     805              :     // Assumption: Maximum possible sequence number value is UINT8_MAX.
     806              :     // Assumption: Sequence numbers incremented past maximum value wrap to 0.
     807              :     // Assumption: newest unacked sent sequence number never exceeds current (and by extension, new and un-wrapped)
     808              :     //             window boundary, so it never wraps relative to last received ack, if new window boundary would not
     809              :     //             also wrap.
     810              : 
     811              :     // Define new window boundary (inclusive) as uint16_t, so its value can temporarily exceed UINT8_MAX.
     812            4 :     uint16_t newRemoteWindowBoundary = static_cast<uint16_t>(lastReceivedAck + maxRemoteWindowSize);
     813              : 
     814            4 :     if (newRemoteWindowBoundary > UINT8_MAX && newestUnackedSentSeqNum < lastReceivedAck)
     815              :     {
     816              :         // New window boundary WOULD wrap, and latest unacked seq num already HAS wrapped, so add offset to difference.
     817            0 :         return static_cast<uint8_t>(newRemoteWindowBoundary - (newestUnackedSentSeqNum + UINT8_MAX));
     818              :     }
     819              : 
     820              :     // Neither values would or have wrapped, OR new boundary WOULD wrap but latest unacked seq num does not, so no
     821              :     // offset required.
     822            4 :     return static_cast<uint8_t>(newRemoteWindowBoundary - newestUnackedSentSeqNum);
     823              : }
     824              : 
     825            8 : CHIP_ERROR WiFiPAFEndPoint::GetPktSn(Encoding::LittleEndian::Reader & reader, uint8_t * pHead, SequenceNumber_t & seqNum)
     826              : {
     827            8 :     BitFlags<WiFiPAFTP::HeaderFlags> rx_flags;
     828            8 :     size_t SnOffset = 0;
     829              :     SequenceNumber_t * pSn;
     830            8 :     ReturnErrorOnFailure(reader.Read8(rx_flags.RawStorage()).StatusCode());
     831            8 :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kHankshake))
     832              :     {
     833              :         // Handkshake message => No ack/sn
     834            2 :         return CHIP_ERROR_INTERNAL;
     835              :     }
     836              :     // Always has header flag
     837            6 :     SnOffset += kTransferProtocolHeaderFlagsSize;
     838            6 :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kManagementOpcode)) // Has Mgmt_Op
     839              :     {
     840            1 :         SnOffset += kTransferProtocolMgmtOpSize;
     841              :     }
     842            6 :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kFragmentAck)) // Has ack
     843              :     {
     844            6 :         SnOffset += kTransferProtocolAckSize;
     845              :     }
     846            6 :     VerifyOrReturnError(SnOffset + sizeof(seqNum) <= reader.OctetsRead() + reader.Remaining(), CHIP_ERROR_MESSAGE_INCOMPLETE);
     847            5 :     pSn    = pHead + SnOffset;
     848            5 :     seqNum = *pSn;
     849              : 
     850            5 :     return CHIP_NO_ERROR;
     851              : }
     852              : 
     853           18 : CHIP_ERROR WiFiPAFEndPoint::DebugPktAckSn(const PktDirect_t PktDirect, Encoding::LittleEndian::Reader & reader, uint8_t * pHead)
     854              : {
     855              : #ifdef CHIP_WIFIPAF_END_POINT_DEBUG_LOGGING_ENABLED
     856              :     BitFlags<WiFiPAFTP::HeaderFlags> rx_flags;
     857              :     CHIP_ERROR err;
     858              :     uint8_t * pAct = nullptr;
     859              :     char AckBuff[4];
     860              :     uint8_t * pSn;
     861              :     size_t SnOffset = 0;
     862              : 
     863              :     err = reader.Read8(rx_flags.RawStorage()).StatusCode();
     864              :     SuccessOrExit(err);
     865              :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kHankshake))
     866              :     {
     867              :         // Handkshake message => No ack/sn
     868              :         return CHIP_NO_ERROR;
     869              :     }
     870              :     // Always has header flag
     871              :     SnOffset += kTransferProtocolHeaderFlagsSize;
     872              :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kManagementOpcode)) // Has Mgmt_Op
     873              :     {
     874              :         SnOffset += kTransferProtocolMgmtOpSize;
     875              :     }
     876              :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kFragmentAck)) // Has ack
     877              :     {
     878              :         pAct = pHead + kTransferProtocolHeaderFlagsSize;
     879              :         SnOffset += kTransferProtocolAckSize;
     880              :     }
     881              :     VerifyOrExit(SnOffset + sizeof(*pSn) <= reader.OctetsRead() + reader.Remaining(), err = CHIP_ERROR_MESSAGE_INCOMPLETE);
     882              :     pSn = pHead + SnOffset;
     883              :     if (pAct == nullptr)
     884              :     {
     885              :         strcpy(AckBuff, "  ");
     886              :     }
     887              :     else
     888              :     {
     889              :         snprintf(AckBuff, sizeof(AckBuff), "%02hhu", *pAct);
     890              :     }
     891              :     if (PktDirect == PktDirect_t::kTx)
     892              :     {
     893              :         ChipLogDebugWiFiPAFEndPoint_L0(WiFiPAF, "==>[tx] [Sn, Ack] = [   %02u, -- %s]", *pSn, AckBuff);
     894              :     }
     895              :     else if (PktDirect == PktDirect_t::kRx)
     896              :     {
     897              :         ChipLogDebugWiFiPAFEndPoint_L0(WiFiPAF, "<==[rx] [Ack, Sn] = [-- %s,    %02u]", AckBuff, *pSn);
     898              :     }
     899              : exit:
     900              :     return err;
     901              : #else
     902           18 :     return CHIP_NO_ERROR;
     903              : #endif
     904              : }
     905              : 
     906            8 : CHIP_ERROR WiFiPAFEndPoint::Receive(PacketBufferHandle && data)
     907              : {
     908            8 :     SequenceNumber_t ExpRxNextSeqNum = mPafTP.GetRxNextSeqNum();
     909              :     SequenceNumber_t seqNum;
     910            8 :     Encoding::LittleEndian::Reader reader(data->Start(), data->DataLength());
     911            8 :     CHIP_ERROR err = CHIP_NO_ERROR;
     912              : 
     913            8 :     err = GetPktSn(reader, data->Start(), seqNum);
     914           16 :     if (err != CHIP_NO_ERROR)
     915              :     {
     916              :         // Failed to get SeqNum. => Pass down to PAFTP engine directly
     917            3 :         return RxPacketProcess(std::move(data));
     918              :     }
     919              :     /*
     920              :         If reorder-queue is not empty => Need to queue the packet whose SeqNum is the next one at
     921              :         offset 0 to fill the hole.
     922              :     */
     923            5 :     if ((ExpRxNextSeqNum == seqNum) && (ItemsInReorderQueue == 0))
     924            2 :         return RxPacketProcess(std::move(data));
     925              : 
     926            3 :     ChipLogError(WiFiPAF, "Reorder the packet: [%u, %u]", ExpRxNextSeqNum, seqNum);
     927              :     // Start reordering packets
     928            3 :     SequenceNumber_t offset = OffsetSeqNum(seqNum, ExpRxNextSeqNum);
     929            3 :     if (offset >= PAFTP_REORDER_QUEUE_SIZE)
     930              :     {
     931              :         // Offset is too big
     932              :         // => It may be the unexpected packet or duplicate packet => drop it
     933            1 :         ChipLogError(WiFiPAF, "Offset (%u) is too big => drop the packet", offset);
     934              :         ChipLogDebugBufferWiFiPAFEndPoint(WiFiPAF, data);
     935            1 :         return CHIP_NO_ERROR;
     936              :     }
     937              : 
     938              :     // Save the packet to the reorder-queue
     939            2 :     if (ReorderQueue[offset].IsNull())
     940              :     {
     941            2 :         ReorderQueue[offset] = std::move(data);
     942            2 :         ItemsInReorderQueue++;
     943              :     }
     944              : 
     945              :     // Consume the packets in the reorder queue if no hole exists
     946            2 :     if (ReorderQueue[0].IsNull())
     947              :     {
     948              :         // The hole still exists => Can't continue
     949            1 :         ChipLogError(WiFiPAF, "The hole still exists. Packets in reorder-queue: %u", ItemsInReorderQueue);
     950            1 :         return CHIP_NO_ERROR;
     951              :     }
     952              :     uint8_t qidx;
     953            3 :     for (qidx = 0; qidx < PAFTP_REORDER_QUEUE_SIZE; qidx++)
     954              :     {
     955              :         // The head slots should have been filled. => Do rx processing
     956            3 :         if (ReorderQueue[qidx].IsNull())
     957              :         {
     958              :             // Stop consuming packets until the hole or no packets
     959            1 :             break;
     960              :         }
     961              :         // Consume the saved packets
     962            2 :         ChipLogProgress(WiFiPAF, "Rx processing from the re-order queue [%u]", qidx);
     963            2 :         err = RxPacketProcess(std::move(ReorderQueue[qidx]));
     964            2 :         ItemsInReorderQueue--;
     965              :     }
     966              :     // Has reached the 1st hole in the queue => move the rest items forward
     967              :     // Note: It's to continue => No need to reinit "i"
     968            5 :     for (uint8_t newId = 0; qidx < PAFTP_REORDER_QUEUE_SIZE; qidx++, newId++)
     969              :     {
     970            4 :         if (!ReorderQueue[qidx].IsNull())
     971              :         {
     972            0 :             ReorderQueue[newId] = std::move(ReorderQueue[qidx]);
     973            0 :             ReorderQueue[qidx]  = nullptr;
     974              :         }
     975              :     }
     976            1 :     return err;
     977              : }
     978              : 
     979            7 : CHIP_ERROR WiFiPAFEndPoint::RxPacketProcess(PacketBufferHandle && data)
     980              : {
     981              :     ChipLogDebugBufferWiFiPAFEndPoint(WiFiPAF, data);
     982              : 
     983            7 :     CHIP_ERROR err               = CHIP_NO_ERROR;
     984            7 :     SequenceNumber_t receivedAck = 0;
     985            7 :     uint8_t closeFlags           = kWiFiPAFCloseFlag_AbortTransmission;
     986            7 :     bool didReceiveAck           = false;
     987            7 :     BitFlags<WiFiPAFTP::HeaderFlags> rx_flags;
     988            7 :     Encoding::LittleEndian::Reader reader(data->Start(), data->DataLength());
     989            7 :     TEMPORARY_RETURN_IGNORED DebugPktAckSn(PktDirect_t::kRx, reader, data->Start());
     990              : 
     991              :     { // This is a special handling on the first CHIPoPAF data packet, the CapabilitiesRequest.
     992              :         // If we're receiving the first inbound packet of a PAF transport connection handshake...
     993            7 :         if (!mConnStateFlags.Has(ConnectionStateFlag::kCapabilitiesMsgReceived))
     994              :         {
     995            3 :             if (mRole == kWiFiPafRole_Subscriber) // If we're a central receiving a capabilities response indication...
     996              :             {
     997              :                 // Ensure end point's in the right state before continuing.
     998            1 :                 VerifyOrExit(mState == kState_Connecting, err = CHIP_ERROR_INCORRECT_STATE);
     999            1 :                 mConnStateFlags.Set(ConnectionStateFlag::kCapabilitiesMsgReceived);
    1000            1 :                 err = HandleCapabilitiesResponseReceived(std::move(data));
    1001            1 :                 SuccessOrExit(err);
    1002              :             }
    1003              :             else // Or, a peripheral receiving a capabilities request write...
    1004              :             {
    1005              :                 // Ensure end point's in the right state before continuing.
    1006            2 :                 VerifyOrExit(mState == kState_Ready, err = CHIP_ERROR_INCORRECT_STATE);
    1007            2 :                 mConnStateFlags.Set(ConnectionStateFlag::kCapabilitiesMsgReceived);
    1008            2 :                 err = HandleCapabilitiesRequestReceived(std::move(data));
    1009            4 :                 if (err != CHIP_NO_ERROR)
    1010              :                 {
    1011              :                     // If an error occurred decoding and handling the capabilities request, release the BLE connection.
    1012              :                     // Central's connect attempt will time out if peripheral's application decides to keep the BLE
    1013              :                     // connection open, or fail immediately if the application closes the connection.
    1014            1 :                     closeFlags = closeFlags | kWiFiPAFCloseFlag_SuppressCallback;
    1015            1 :                     ExitNow();
    1016              :                 }
    1017              :             }
    1018              :             // If received data was handshake packet, don't feed it to message reassembler.
    1019            2 :             ExitNow();
    1020              :         }
    1021              :     } // End handling the CapabilitiesRequest
    1022              : 
    1023            4 :     err = reader.Read8(rx_flags.RawStorage()).StatusCode();
    1024            4 :     SuccessOrExit(err);
    1025            4 :     if (rx_flags.Has(WiFiPAFTP::HeaderFlags::kHankshake))
    1026              :     {
    1027              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "Unexpected handshake packet => drop");
    1028            0 :         ExitNow();
    1029              :     }
    1030              : 
    1031              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "PAFTP about to rx characteristic, state before:");
    1032            4 :     mPafTP.LogStateDebug();
    1033              : 
    1034              :     // Pass received packet into PAFTP protocol engine.
    1035            4 :     err = mPafTP.HandleCharacteristicReceived(std::move(data), receivedAck, didReceiveAck);
    1036              : 
    1037              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "PAFTP rx'd characteristic, state after:");
    1038            4 :     mPafTP.LogStateDebug();
    1039            4 :     SuccessOrExit(err);
    1040              : 
    1041              :     // Protocol engine accepted the fragment, so shrink local receive window counter by 1.
    1042            4 :     mLocalReceiveWindowSize = static_cast<SequenceNumber_t>(mLocalReceiveWindowSize - 1);
    1043              :     ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "decremented local rx window, new size = %u", mLocalReceiveWindowSize);
    1044              : 
    1045              :     // Respond to received ack, if any.
    1046            4 :     if (didReceiveAck)
    1047              :     {
    1048              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "got paftp ack = %u", receivedAck);
    1049              : 
    1050              :         // If ack was rx'd for newest unacked sent fragment, stop ack received timer.
    1051            4 :         if (!mPafTP.ExpectingAck())
    1052              :         {
    1053              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "got ack for last outstanding fragment");
    1054            1 :             StopAckReceivedTimer();
    1055              : 
    1056            1 :             if (mState == kState_Closing && mSendQueue.IsNull() && mPafTP.TxState() == WiFiPAFTP::kState_Idle)
    1057              :             {
    1058              :                 // If end point closing, got confirmation for last send, and waiting for last ack, finalize close.
    1059            0 :                 FinalizeClose(mState, kWiFiPAFCloseFlag_SuppressCallback, CHIP_NO_ERROR);
    1060            0 :                 ExitNow();
    1061              :             }
    1062              :         }
    1063              :         else // Else there are still sent fragments for which acks are expected, so restart ack received timer.
    1064              :         {
    1065              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "still expecting ack(s), restarting timer...");
    1066            3 :             err = RestartAckReceivedTimer();
    1067            3 :             SuccessOrExit(err);
    1068              :         }
    1069              : 
    1070              :         ChipLogDebugWiFiPAFEndPoint(
    1071              :             WiFiPAF, "about to adjust remote rx window; got ack num = %u, newest unacked sent seq num = %u, \
    1072              :                 old window size = %u, max window size = %u",
    1073              :             receivedAck, mPafTP.GetNewestUnackedSentSequenceNumber(), mRemoteReceiveWindowSize, mReceiveWindowMaxSize);
    1074              : 
    1075              :         // Open remote device's receive window according to sequence number it just acknowledged.
    1076            4 :         mRemoteReceiveWindowSize =
    1077            4 :             AdjustRemoteReceiveWindow(receivedAck, mReceiveWindowMaxSize, mPafTP.GetNewestUnackedSentSequenceNumber());
    1078              : 
    1079              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "adjusted remote rx window, new size = %u", mRemoteReceiveWindowSize);
    1080              : 
    1081              :         // Restart message transmission if it was previously paused due to window exhaustion.
    1082            4 :         err = DriveSending();
    1083            4 :         SuccessOrExit(err);
    1084              :     }
    1085              : 
    1086              :     // The previous DriveSending() might have generated a piggyback acknowledgement if there was
    1087              :     // previously un-acked data.  Otherwise, prepare to send acknowledgement for newly received fragment.
    1088              :     //
    1089              :     // If local receive window is below immediate ack threshold, AND there is no previous stand-alone ack in
    1090              :     // flight, AND there is no pending outbound message fragment on which the ack can and will be piggybacked,
    1091              :     // send immediate stand-alone ack to reopen window for sender.
    1092              :     //
    1093              :     // The "operation in flight" check below covers "pending outbound message fragment" by extension, as when
    1094              :     // a message has been passed to the end point via Send(), its next outbound fragment must either be in flight
    1095              :     // itself, or awaiting the completion of another in-flight operation.
    1096              :     //
    1097              :     // If any operation is in flight that is NOT a stand-alone ack, the window size will be checked against
    1098              :     // this threshold again when the operation is confirmed.
    1099            4 :     if (mPafTP.HasUnackedData())
    1100              :     {
    1101            4 :         if (mLocalReceiveWindowSize <= WIFIPAF_CONFIG_IMMEDIATE_ACK_WINDOW_THRESHOLD &&
    1102            0 :             !mConnStateFlags.Has(ConnectionStateFlag::kOperationInFlight))
    1103              :         {
    1104              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "sending immediate ack");
    1105            0 :             err = DriveStandAloneAck();
    1106            0 :             SuccessOrExit(err);
    1107              :         }
    1108              :         else
    1109              :         {
    1110              :             ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "starting send-ack timer");
    1111              :             // Send ack when timer expires.
    1112            4 :             err = StartSendAckTimer();
    1113            4 :             SuccessOrExit(err);
    1114              :         }
    1115              :     }
    1116              : 
    1117              :     // If we've reassembled a whole message...
    1118            4 :     if (mPafTP.RxState() == WiFiPAFTP::kState_Complete)
    1119              :     {
    1120              :         // Take ownership of message buffer
    1121            2 :         System::PacketBufferHandle full_packet = mPafTP.TakeRxPacket();
    1122              : 
    1123              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "reassembled whole msg, len = %u", static_cast<unsigned>(full_packet->DataLength()));
    1124              : 
    1125              :         // If we have a message received callback, and end point is not closing...
    1126            2 :         if (mWiFiPafLayer != nullptr && mState != kState_Closing)
    1127              :         {
    1128              :             // Pass received message up the stack.
    1129            2 :             err = mWiFiPafLayer->OnWiFiPAFMsgRxComplete(mSessionInfo, std::move(full_packet));
    1130              :         }
    1131            2 :     }
    1132              : 
    1133            2 : exit:
    1134           14 :     if (err != CHIP_NO_ERROR)
    1135              :     {
    1136            1 :         DoClose(closeFlags, err);
    1137              :     }
    1138              : 
    1139            7 :     return err;
    1140              : }
    1141              : 
    1142           11 : CHIP_ERROR WiFiPAFEndPoint::SendWrite(PacketBufferHandle && buf)
    1143              : {
    1144           11 :     mConnStateFlags.Set(ConnectionStateFlag::kOperationInFlight);
    1145              : 
    1146              :     ChipLogDebugBufferWiFiPAFEndPoint(WiFiPAF, buf);
    1147           11 :     Encoding::LittleEndian::Reader reader(buf->Start(), buf->DataLength());
    1148           11 :     TEMPORARY_RETURN_IGNORED DebugPktAckSn(PktDirect_t::kTx, reader, buf->Start());
    1149           11 :     return mWiFiPafLayer->mWiFiPAFTransport->WiFiPAFMessageSend(mSessionInfo, std::move(buf));
    1150              : }
    1151              : 
    1152            1 : CHIP_ERROR WiFiPAFEndPoint::StartConnectTimer()
    1153              : {
    1154            1 :     const CHIP_ERROR timerErr = mWiFiPafLayer->mSystemLayer->StartTimer(System::Clock::Milliseconds32(PAFTP_CONN_RSP_TIMEOUT_MS),
    1155            1 :                                                                         HandleConnectTimeout, this);
    1156            1 :     ReturnErrorOnFailure(timerErr);
    1157            1 :     mTimerStateFlags.Set(TimerStateFlag::kConnectTimerRunning);
    1158              : 
    1159            1 :     return CHIP_NO_ERROR;
    1160              : }
    1161              : 
    1162           12 : CHIP_ERROR WiFiPAFEndPoint::StartAckReceivedTimer()
    1163              : {
    1164           12 :     if (!mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning))
    1165              :     {
    1166            6 :         const CHIP_ERROR timerErr = mWiFiPafLayer->mSystemLayer->StartTimer(System::Clock::Milliseconds32(PAFTP_ACK_TIMEOUT_MS),
    1167            6 :                                                                             HandleAckReceivedTimeout, this);
    1168            6 :         ReturnErrorOnFailure(timerErr);
    1169              : 
    1170            6 :         mTimerStateFlags.Set(TimerStateFlag::kAckReceivedTimerRunning);
    1171              :     }
    1172              : 
    1173           12 :     return CHIP_NO_ERROR;
    1174              : }
    1175              : 
    1176            3 : CHIP_ERROR WiFiPAFEndPoint::RestartAckReceivedTimer()
    1177              : {
    1178            3 :     VerifyOrReturnError(mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning), CHIP_ERROR_INCORRECT_STATE);
    1179              : 
    1180            3 :     StopAckReceivedTimer();
    1181              : 
    1182            3 :     return StartAckReceivedTimer();
    1183              : }
    1184              : 
    1185            5 : CHIP_ERROR WiFiPAFEndPoint::StartSendAckTimer()
    1186              : {
    1187            5 :     if (!mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning))
    1188              :     {
    1189              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "starting new SendAckTimer");
    1190            3 :         const CHIP_ERROR timerErr = mWiFiPafLayer->mSystemLayer->StartTimer(
    1191            3 :             System::Clock::Milliseconds32(WIFIPAF_ACK_SEND_TIMEOUT_MS), HandleSendAckTimeout, this);
    1192            3 :         ReturnErrorOnFailure(timerErr);
    1193              : 
    1194            3 :         mTimerStateFlags.Set(TimerStateFlag::kSendAckTimerRunning);
    1195              :     }
    1196              : 
    1197            5 :     return CHIP_NO_ERROR;
    1198              : }
    1199              : 
    1200            1 : CHIP_ERROR WiFiPAFEndPoint::StartWaitResourceTimer()
    1201              : {
    1202            1 :     mResourceWaitCount++;
    1203            1 :     if (mResourceWaitCount >= WIFIPAF_MAX_RESOURCE_BLOCK_COUNT)
    1204              :     {
    1205            0 :         ChipLogError(WiFiPAF, "Network resource has been unavailable for a long time");
    1206            0 :         mResourceWaitCount = 0;
    1207            0 :         DoClose(kWiFiPAFCloseFlag_AbortTransmission, CHIP_ERROR_NOT_CONNECTED);
    1208            0 :         return CHIP_NO_ERROR;
    1209              :     }
    1210            1 :     if (!mTimerStateFlags.Has(TimerStateFlag::kWaitResTimerRunning))
    1211              :     {
    1212              :         ChipLogDebugWiFiPAFEndPoint(WiFiPAF, "starting new WaitResTimer");
    1213            1 :         const CHIP_ERROR timerErr = mWiFiPafLayer->mSystemLayer->StartTimer(
    1214            1 :             System::Clock::Milliseconds32(WIFIPAF_WAIT_RES_TIMEOUT_MS), HandleWaitResourceTimeout, this);
    1215            1 :         ReturnErrorOnFailure(timerErr);
    1216            1 :         mTimerStateFlags.Set(TimerStateFlag::kWaitResTimerRunning);
    1217              :     }
    1218            1 :     return CHIP_NO_ERROR;
    1219              : }
    1220              : 
    1221            6 : void WiFiPAFEndPoint::StopConnectTimer()
    1222              : {
    1223              :     // Cancel any existing connect timer.
    1224            6 :     mWiFiPafLayer->mSystemLayer->CancelTimer(HandleConnectTimeout, this);
    1225            6 :     mTimerStateFlags.Clear(TimerStateFlag::kConnectTimerRunning);
    1226            6 : }
    1227              : 
    1228            7 : void WiFiPAFEndPoint::StopAckReceivedTimer()
    1229              : {
    1230              :     // Cancel any existing ack-received timer.
    1231            7 :     mWiFiPafLayer->mSystemLayer->CancelTimer(HandleAckReceivedTimeout, this);
    1232            7 :     mTimerStateFlags.Clear(TimerStateFlag::kAckReceivedTimerRunning);
    1233            7 : }
    1234              : 
    1235            6 : void WiFiPAFEndPoint::StopSendAckTimer()
    1236              : {
    1237              :     // Cancel any existing send-ack timer.
    1238            6 :     mWiFiPafLayer->mSystemLayer->CancelTimer(HandleSendAckTimeout, this);
    1239            6 :     mTimerStateFlags.Clear(TimerStateFlag::kSendAckTimerRunning);
    1240            6 : }
    1241              : 
    1242            3 : void WiFiPAFEndPoint::StopWaitResourceTimer()
    1243              : {
    1244              :     // Cancel any existing wait-resource timer.
    1245            3 :     mWiFiPafLayer->mSystemLayer->CancelTimer(HandleWaitResourceTimeout, this);
    1246            3 :     mTimerStateFlags.Clear(TimerStateFlag::kWaitResTimerRunning);
    1247            3 : }
    1248              : 
    1249            0 : void WiFiPAFEndPoint::HandleConnectTimeout(chip::System::Layer * systemLayer, void * appState)
    1250              : {
    1251            0 :     WiFiPAFEndPoint * ep = static_cast<WiFiPAFEndPoint *>(appState);
    1252              : 
    1253              :     // Check for event-based timer race condition.
    1254            0 :     if (ep->mTimerStateFlags.Has(TimerStateFlag::kConnectTimerRunning))
    1255              :     {
    1256            0 :         ChipLogError(WiFiPAF, "connect handshake timed out, closing ep %p", ep);
    1257            0 :         ep->mTimerStateFlags.Clear(TimerStateFlag::kConnectTimerRunning);
    1258            0 :         ep->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_CONNECT_TIMED_OUT);
    1259              :     }
    1260            0 : }
    1261              : 
    1262            0 : void WiFiPAFEndPoint::HandleAckReceivedTimeout(chip::System::Layer * systemLayer, void * appState)
    1263              : {
    1264            0 :     WiFiPAFEndPoint * ep = static_cast<WiFiPAFEndPoint *>(appState);
    1265              : 
    1266              :     // Check for event-based timer race condition.
    1267            0 :     if (ep->mTimerStateFlags.Has(TimerStateFlag::kAckReceivedTimerRunning))
    1268              :     {
    1269            0 :         ChipLogError(WiFiPAF, "ack recv timeout, closing ep %p", ep);
    1270            0 :         ep->mPafTP.LogStateDebug();
    1271            0 :         ep->mTimerStateFlags.Clear(TimerStateFlag::kAckReceivedTimerRunning);
    1272            0 :         ep->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_FRAGMENT_ACK_TIMED_OUT);
    1273              :     }
    1274            0 : }
    1275              : 
    1276            0 : void WiFiPAFEndPoint::HandleSendAckTimeout(chip::System::Layer * systemLayer, void * appState)
    1277              : {
    1278            0 :     WiFiPAFEndPoint * ep = static_cast<WiFiPAFEndPoint *>(appState);
    1279              : 
    1280              :     // Check for event-based timer race condition.
    1281            0 :     if (ep->mTimerStateFlags.Has(TimerStateFlag::kSendAckTimerRunning))
    1282              :     {
    1283            0 :         ep->mTimerStateFlags.Clear(TimerStateFlag::kSendAckTimerRunning);
    1284              : 
    1285              :         // If previous stand-alone ack isn't still in flight...
    1286            0 :         if (!ep->mConnStateFlags.Has(ConnectionStateFlag::kStandAloneAckInFlight))
    1287              :         {
    1288            0 :             CHIP_ERROR sendErr = ep->DriveStandAloneAck();
    1289              : 
    1290            0 :             if (sendErr != CHIP_NO_ERROR)
    1291              :             {
    1292            0 :                 ep->DoClose(kWiFiPAFCloseFlag_AbortTransmission, sendErr);
    1293              :             }
    1294              :         }
    1295              :     }
    1296            0 : }
    1297              : 
    1298            0 : void WiFiPAFEndPoint::HandleWaitResourceTimeout(chip::System::Layer * systemLayer, void * appState)
    1299              : {
    1300            0 :     WiFiPAFEndPoint * ep = static_cast<WiFiPAFEndPoint *>(appState);
    1301              : 
    1302              :     // Check for event-based timer race condition.
    1303            0 :     if (ep->mTimerStateFlags.Has(TimerStateFlag::kWaitResTimerRunning))
    1304              :     {
    1305            0 :         ep->mTimerStateFlags.Clear(TimerStateFlag::kWaitResTimerRunning);
    1306            0 :         CHIP_ERROR sendErr = ep->DriveSending();
    1307            0 :         if (sendErr != CHIP_NO_ERROR)
    1308              :         {
    1309            0 :             ep->DoClose(kWiFiPAFCloseFlag_AbortTransmission, sendErr);
    1310              :         }
    1311              :     }
    1312            0 : }
    1313              : 
    1314            3 : void WiFiPAFEndPoint::ClearAll()
    1315              : {
    1316            3 :     memset(reinterpret_cast<uint8_t *>(this), 0, sizeof(WiFiPAFEndPoint));
    1317            3 :     return;
    1318              : }
    1319              : 
    1320              : } /* namespace WiFiPAF */
    1321              : } /* namespace chip */
        

Generated by: LCOV version 2.0-1