Matter SDK Coverage Report
Current view: top level - inet - TCPEndPoint.h (source / functions) Coverage Total Hit
Test: SHA:e98a48c2e59f85a25417956e1d105721433aa5d1 Lines: 100.0 % 12 12
Test Date: 2026-01-09 16:53:50 Functions: 83.3 % 6 5

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020-2025 Project CHIP Authors
       4              :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : 
      19              : /**
      20              :  *    @file
      21              :  *      This header file defines the <tt>Inet::TCPEndPoint</tt>
      22              :  *      class, where the CHIP Inet Layer encapsulates methods for
      23              :  *      interacting with TCP transport endpoints (SOCK_DGRAM sockets
      24              :  *      on Linux and BSD-derived systems) or LwIP TCP protocol
      25              :  *      control blocks, as the system is configured accordingly.
      26              :  */
      27              : 
      28              : #pragma once
      29              : 
      30              : #include <inet/EndPointBasis.h>
      31              : #include <inet/IPAddress.h>
      32              : #include <inet/InetInterface.h>
      33              : #include <inet/InetLayer.h>
      34              : #include <system/SystemLayer.h>
      35              : #include <system/SystemPacketBuffer.h>
      36              : 
      37              : #include <utility>
      38              : 
      39              : namespace chip {
      40              : 
      41              : namespace Inet {
      42              : 
      43              : class TCPTest;
      44              : class TCPEndPoint;
      45              : using TCPEndPointHandle = EndPointHandle<TCPEndPoint>;
      46              : 
      47              : /**
      48              :  * @brief   Objects of this class represent TCP transport endpoints.
      49              :  *
      50              :  * @details
      51              :  *  CHIP Inet Layer encapsulates methods for interacting with TCP transport
      52              :  *  endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP
      53              :  *  TCP protocol control blocks, as the system is configured accordingly.
      54              :  */
      55              : class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
      56              : {
      57              : public:
      58              :     /**
      59              :      * @brief   Bind the endpoint to an interface IP address.
      60              :      *
      61              :      * @param[in]   addrType    the protocol version of the IP address
      62              :      * @param[in]   addr        the IP address (must be an interface address)
      63              :      * @param[in]   port        the TCP port
      64              :      * @param[in]   reuseAddr   option to share binding with other endpoints
      65              :      *
      66              :      * @retval  CHIP_NO_ERROR               success: endpoint bound to address
      67              :      * @retval  CHIP_ERROR_INCORRECT_STATE  endpoint has been bound previously
      68              :      * @retval  CHIP_ERROR_NO_MEMORY        insufficient memory for endpoint
      69              :      *
      70              :      * @retval  INET_ERROR_WRONG_PROTOCOL_TYPE
      71              :      *      \c addrType does not match \c IPVer.
      72              :      *
      73              :      * @retval  INET_ERROR_WRONG_ADDRESS_TYPE
      74              :      *      \c addrType is \c IPAddressType::kAny, or the type of \c addr is not
      75              :      *      equal to \c addrType.
      76              :      *
      77              :      * @retval  other                   another system or platform error
      78              :      *
      79              :      * @details
      80              :      *  Binds the endpoint to the specified network interface IP address.
      81              :      *
      82              :      *  On LwIP, this method must not be called with the LwIP stack lock
      83              :      *  already acquired.
      84              :      */
      85              :     CHIP_ERROR Bind(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr = false);
      86              : 
      87              :     /**
      88              :      * @brief   Prepare the endpoint to receive TCP messages.
      89              :      *
      90              :      * @param[in]   backlog     maximum depth of connection acceptance queue
      91              :      *
      92              :      * @retval  CHIP_NO_ERROR   success: endpoint ready to receive messages.
      93              :      * @retval  CHIP_ERROR_INCORRECT_STATE  endpoint is already listening.
      94              :      *
      95              :      * @details
      96              :      *  If \c mState is already \c State::kListening, then no operation is
      97              :      *  performed, otherwise the \c mState is set to \c State::kListening and
      98              :      *  the endpoint is prepared to received TCP messages, according to the
      99              :      *  semantics of the platform.
     100              :      *
     101              :      *  On some platforms, the \c backlog argument is not used (the depth of
     102              :      *  the queue is fixed; only one connection may be accepted at a time).
     103              :      *
     104              :      *  On LwIP systems, this method must not be called with the LwIP stack
     105              :      *  lock already acquired
     106              :      */
     107              :     CHIP_ERROR Listen(uint16_t backlog);
     108              : 
     109              :     /**
     110              :      * @brief   Initiate a TCP connection.
     111              :      *
     112              :      * @param[in]   addr        the destination IP address
     113              :      * @param[in]   port        the destination TCP port
     114              :      * @param[in]   intfId      an optional network interface indicator
     115              :      *
     116              :      * @retval  CHIP_NO_ERROR       success: \c msg is queued for transmit.
     117              :      * @retval  CHIP_ERROR_NOT_IMPLEMENTED  system implementation not complete.
     118              :      *
     119              :      * @retval  INET_ERROR_WRONG_ADDRESS_TYPE
     120              :      *      the destination address and the bound interface address do not
     121              :      *      have matching protocol versions or address type, or the destination
     122              :      *      address is an IPv6 link-local address and \c intfId is not specified.
     123              :      *
     124              :      * @retval  other                   another system or platform error
     125              :      *
     126              :      * @details
     127              :      *      If possible, then this method initiates a TCP connection to the
     128              :      *      destination \c addr (with \c intfId used as the scope
     129              :      *      identifier for IPv6 link-local destinations) and \c port.
     130              :      */
     131              :     CHIP_ERROR Connect(const IPAddress & addr, uint16_t port, InterfaceId intfId = InterfaceId::Null());
     132              : 
     133              :     /**
     134              :      * @brief   Extract IP address and TCP port of remote endpoint.
     135              :      *
     136              :      * @param[out]  retAddr     IP address of remote endpoint.
     137              :      * @param[out]  retPort     TCP port of remote endpoint.
     138              :      *
     139              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     140              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     141              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     142              :      *
     143              :      * @details
     144              :      *  Do not use \c nullptr for either argument.
     145              :      */
     146              :     virtual CHIP_ERROR GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const = 0;
     147              : 
     148              :     /**
     149              :      * @brief   Extract IP address and TCP port of local endpoint.
     150              :      *
     151              :      * @param[out]  retAddr     IP address of local endpoint.
     152              :      * @param[out]  retPort     TCP port of local endpoint.
     153              :      *
     154              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     155              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     156              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     157              :      *
     158              :      * @details
     159              :      *  Do not use \c nullptr for either argument.
     160              :      */
     161              :     virtual CHIP_ERROR GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const = 0;
     162              : 
     163              :     /**
     164              :      * @brief   Extract the interface id of the TCP endpoint.
     165              :      *
     166              :      * @param[out]  retInterface  The interface id.
     167              :      *
     168              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     169              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     170              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     171              :      */
     172              :     virtual CHIP_ERROR GetInterfaceId(InterfaceId * retInterface) = 0;
     173              : 
     174              :     /**
     175              :      * @brief   Send message text on TCP connection.
     176              :      *
     177              :      * @param[out]  data    Message text to send.
     178              :      * @param[out]  push    If \c true, then send immediately, otherwise queue.
     179              :      *
     180              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     181              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     182              :      */
     183              :     CHIP_ERROR Send(chip::System::PacketBufferHandle && data, bool push = true);
     184              : 
     185              :     /**
     186              :      * Disable reception.
     187              :      *
     188              :      *  Disable all event handlers. Data sent to an endpoint that disables
     189              :      *  reception will be acknowledged until the receive window is exhausted.
     190              :      */
     191              :     void DisableReceive() { mReceiveEnabled = false; }
     192              : 
     193              :     /**
     194              :      * Enable reception.
     195              :      *
     196              :      *  Enable all event handlers. Data sent to an endpoint that disables
     197              :      *  reception will be acknowledged until the receive window is exhausted.
     198              :      */
     199              :     void EnableReceive()
     200              :     {
     201              :         mReceiveEnabled = true;
     202              :         TCPEndPointHandle handle(this);
     203              :         DriveReceiving(handle);
     204              :     }
     205              : 
     206              :     /**
     207              :      * Switch off Nagle buffering algorithm.
     208              :      */
     209              :     virtual CHIP_ERROR EnableNoDelay() = 0;
     210              : 
     211              :     /**
     212              :      * @brief
     213              :      *    Enable TCP keepalive probes on the associated TCP connection.
     214              :      *
     215              :      *  @param[in] interval
     216              :      *    The interval (in seconds) between keepalive probes.  This value also controls
     217              :      *    the time between last data packet sent and the transmission of the first keepalive
     218              :      *    probe.
     219              :      *
     220              :      *  @param[in] timeoutCount
     221              :      *    The maximum number of unacknowledged probes before the connection will be deemed
     222              :      *    to have failed.
     223              :      *
     224              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     225              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     226              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     227              :      * @retval  CHIP_ERROR_NOT_IMPLEMENTED  system implementation not complete.
     228              :      *
     229              :      * @retval  other                   another system or platform error
     230              :      *
     231              :      *  @note
     232              :      *    This method can only be called when the endpoint is in one of the connected states.
     233              :      *
     234              :      *    This method can be called multiple times to adjust the keepalive interval or timeout
     235              :      *    count.
     236              :      *
     237              :      * @details
     238              :      *  Start automatically  transmitting TCP "keep-alive" probe segments every
     239              :      *  \c interval seconds. The connection will abort automatically after
     240              :      *  receiving a negative response, or after sending \c timeoutCount
     241              :      *  probe segments without receiving a positive response.
     242              :      *
     243              :      *  See RFC 1122, section 4.2.3.6 for specification details.
     244              :      */
     245              :     virtual CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) = 0;
     246              : 
     247              :     /**
     248              :      * @brief   Disable the TCP "keep-alive" option.
     249              :      *
     250              :      *    This method can only be called when the endpoint is in one of the connected states.
     251              :      *    This method does nothing if keepalives have not been enabled on the endpoint.
     252              :      *
     253              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     254              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     255              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     256              :      * @retval  CHIP_ERROR_NOT_IMPLEMENTED  system implementation not complete.
     257              :      *
     258              :      * @retval  other                   another system or platform error
     259              :      */
     260              :     virtual CHIP_ERROR DisableKeepAlive() = 0;
     261              : 
     262              :     /**
     263              :      * @brief   Acknowledge receipt of message text.
     264              :      *
     265              :      * @param[in]   len     number of bytes to acknowledge.
     266              :      *
     267              :      * @retval  CHIP_NO_ERROR           success: reception acknowledged.
     268              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     269              :      * @retval  CHIP_ERROR_CONNECTION_ABORTED   TCP connection no longer open.
     270              :      *
     271              :      * @details
     272              :      *  Use this method to acknowledge reception of all or part of the data
     273              :      *  received. The operational semantics are undefined if \c len is larger
     274              :      *  than the total outstanding unacknowledged received data.
     275              :      */
     276              :     virtual CHIP_ERROR AckReceive(size_t len) = 0;
     277              : 
     278              :     /**
     279              :      * @brief   Set the receive queue, for testing.
     280              :      *
     281              :      * @param[out]  data    Message text to push.
     282              :      *
     283              :      * @retval  CHIP_NO_ERROR           success: reception acknowledged.
     284              :      * @retval  CHIP_ERROR_INCORRECT_STATE  TCP connection not established.
     285              :      *
     286              :      * @details
     287              :      *  This method may only be called by data reception event handlers to
     288              :      *  put data on the receive queue for unit test purposes.
     289              :      */
     290              :     CHIP_ERROR SetReceivedDataForTesting(chip::System::PacketBufferHandle && data);
     291              : 
     292              :     /**
     293              :      * @brief   Extract the length of the data awaiting first transmit.
     294              :      *
     295              :      * @return  Number of untransmitted bytes in the transmit queue.
     296              :      */
     297              :     size_t PendingSendLength();
     298              : 
     299              :     /**
     300              :      * @brief   Extract the length of the unacknowledged receive data.
     301              :      *
     302              :      * @return  Number of bytes in the receive queue that have not yet been
     303              :      *      acknowledged with <tt>AckReceive(uint16_t len)</tt>.
     304              :      */
     305              :     size_t PendingReceiveLength();
     306              : 
     307              :     /**
     308              :      * @brief   Initiate TCP half close, in other words, finished with sending.
     309              :      */
     310              :     void Shutdown();
     311              : 
     312              :     /**
     313              :      * @brief   Initiate TCP full close, in other words, finished with both send and
     314              :      *  receive.
     315              :      */
     316              :     void Close();
     317              : 
     318              :     /**
     319              :      * @brief   Abortively close the endpoint, in other words, send RST packets.
     320              :      */
     321              :     void Abort();
     322              : 
     323              :     /**
     324              :      * @brief   Extract whether TCP connection is established.
     325              :      */
     326          239 :     bool IsConnected() const { return IsConnected(mState); }
     327              : 
     328              :     /**
     329              :      * Set timeout for Connect to succeed or return an error.
     330              :      */
     331           21 :     void SetConnectTimeout(const uint32_t connTimeoutMsecs) { mConnectTimeoutMsecs = connTimeoutMsecs; }
     332              : 
     333              : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
     334              :     /**
     335              :      * @brief   Set timer event for idle activity.
     336              :      *
     337              :      * @param[in]   timeoutMS The timeout in milliseconds
     338              :      *
     339              :      * @details
     340              :      *  Set the idle timer interval to \c timeoutMS milliseconds. A zero
     341              :      *  time interval implies the idle timer is disabled.
     342              :      */
     343              :     void SetIdleTimeout(uint32_t timeoutMS);
     344              : #endif // INET_TCP_IDLE_CHECK_INTERVAL > 0
     345              : 
     346              :     /**
     347              :      * @brief   Note activity, in other words, reset the idle timer.
     348              :      *
     349              :      * @details
     350              :      *  Reset the idle timer to zero.
     351              :      */
     352           41 :     void MarkActive()
     353              :     {
     354              : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
     355           41 :         mRemainingIdleTime = mIdleTimeout;
     356              : #endif // INET_TCP_IDLE_CHECK_INTERVAL > 0
     357           41 :     }
     358              : 
     359              :     /**
     360              :      * @brief   Set the TCP TCP_USER_TIMEOUT socket option.
     361              :      *
     362              :      * @param[in]   userTimeoutMillis    Tcp user timeout value in milliseconds.
     363              :      *
     364              :      * @retval  CHIP_NO_ERROR           success: address and port extracted.
     365              :      * @retval  CHIP_ERROR_NOT_IMPLEMENTED  system implementation not complete.
     366              :      *
     367              :      * @retval  other                   another system or platform error
     368              :      *
     369              :      * @details
     370              :      *  When the value is greater than 0, it specifies the maximum amount of
     371              :      *  time in milliseconds that transmitted data may remain
     372              :      *  unacknowledged before TCP will forcibly close the
     373              :      *  corresponding connection. If the option value is specified as 0,
     374              :      *  TCP will to use the system default.
     375              :      *  See RFC 5482, for further details.
     376              :      *
     377              :      *  @note
     378              :      *    This method can only be called when the endpoint is in one of the connected states.
     379              :      *
     380              :      *    This method can be called multiple times to adjust the keepalive interval or timeout
     381              :      *    count.
     382              :      */
     383              :     CHIP_ERROR SetUserTimeout(uint32_t userTimeoutMillis);
     384              : 
     385              :     /**
     386              :      * @brief   Type of connection establishment event handling function.
     387              :      *
     388              :      * @param[in]   endPoint    The TCP endpoint associated with the event.
     389              :      * @param[in]   err         \c CHIP_NO_ERROR if success, else another code.
     390              :      *
     391              :      * @details
     392              :      *  Provide a function of this type to the \c OnConnectComplete delegate
     393              :      *  member to process connection establishment events on \c endPoint. The
     394              :      *  \c err argument distinguishes successful connections from failures.
     395              :      */
     396              :     typedef void (*OnConnectCompleteFunct)(const TCPEndPointHandle & endPoint, CHIP_ERROR err);
     397              : 
     398              :     /**
     399              :      * The endpoint's connection establishment event handling function
     400              :      * delegate.
     401              :      */
     402              :     OnConnectCompleteFunct OnConnectComplete;
     403              : 
     404              :     /**
     405              :      * @brief   Type of data reception event handling function.
     406              :      *
     407              :      * @param[in]   endPoint        The TCP endpoint associated with the event.
     408              :      * @param[in]   data            The data received.
     409              :      *
     410              :      * @retval      CHIP_NO_ERROR   If the received data can be handled by higher layers.
     411              :      * @retval      other           If the received data can not be used, and higher layers will not see it.
     412              :      *
     413              :      * @details
     414              :      *  Provide a function of this type to the \c OnDataReceived delegate
     415              :      *  member to process data reception events on \c endPoint where \c data
     416              :      *  is the message text received.
     417              :      *
     418              :      *  If this function returns an error, the connection will be closed, since higher layers
     419              :      *  are not able to process the data for a better response.
     420              :      */
     421              :     typedef CHIP_ERROR (*OnDataReceivedFunct)(const TCPEndPointHandle & endPoint, chip::System::PacketBufferHandle && data);
     422              : 
     423              :     /**
     424              :      * The endpoint's message text reception event handling function delegate.
     425              :      */
     426              :     OnDataReceivedFunct OnDataReceived;
     427              : 
     428              :     /**
     429              :      * @brief   Type of data transmission event handling function.
     430              :      *
     431              :      * @param[in]   endPoint    The TCP endpoint associated with the event.
     432              :      * @param[in]   len         Number of bytes added to the transmit window.
     433              :      *
     434              :      * @details
     435              :      *  Provide a function of this type to the \c OnDataSent delegate
     436              :      *  member to process data transmission events on \c endPoint where \c len
     437              :      *  is the length of the message text added to the TCP transmit window,
     438              :      *  which are eligible for sending by the underlying network stack.
     439              :      */
     440              :     typedef void (*OnDataSentFunct)(const TCPEndPointHandle & endPoint, size_t len);
     441              : 
     442              :     /**
     443              :      * The endpoint's message text transmission event handling function
     444              :      * delegate.
     445              :      */
     446              :     OnDataSentFunct OnDataSent;
     447              : 
     448              :     /**
     449              :      * @brief   Type of connection establishment event handling function.
     450              :      *
     451              :      * @param[in]   endPoint    The TCP endpoint associated with the event.
     452              :      * @param[in]   err         \c CHIP_NO_ERROR if success, else another code.
     453              :      *
     454              :      * @details
     455              :      *  Provide a function of this type to the \c OnConnectionClosed delegate
     456              :      *  member to process connection termination events on \c endPoint. The
     457              :      *  \c err argument distinguishes successful terminations from failures.
     458              :      */
     459              :     typedef void (*OnConnectionClosedFunct)(const TCPEndPointHandle & endPoint, CHIP_ERROR err);
     460              : 
     461              :     /** The endpoint's close event handling function delegate. */
     462              :     OnConnectionClosedFunct OnConnectionClosed;
     463              : 
     464              :     /**
     465              :      * @brief   Type of half-close reception event handling function.
     466              :      *
     467              :      * @param[in]   endPoint    The TCP endpoint associated with the event.
     468              :      *
     469              :      * @details
     470              :      *  Provide a function of this type to the \c OnPeerClose delegate member
     471              :      *  to process connection termination events on \c endPoint.
     472              :      */
     473              :     typedef void (*OnPeerCloseFunct)(const TCPEndPointHandle & endPoint);
     474              : 
     475              :     /** The endpoint's half-close receive event handling function delegate. */
     476              :     OnPeerCloseFunct OnPeerClose;
     477              : 
     478              :     /**
     479              :      * @brief   Type of connection received event handling function.
     480              :      *
     481              :      * @param[in]   listeningEndPoint   The listening TCP endpoint.
     482              :      * @param[in]   conEndPoint         The newly received TCP endpoint.
     483              :      * @param[in]   peerAddr            The IP address of the remote peer.
     484              :      * @param[in]   peerPort            The TCP port of the remote peer.
     485              :      *
     486              :      * @details
     487              :      *  Provide a function of this type to the \c OnConnectionReceived delegate
     488              :      *  member to process connection reception events on \c listeningEndPoint.
     489              :      *  The newly received endpoint \c conEndPoint is located at IP address
     490              :      *  \c peerAddr and TCP port \c peerPort.
     491              :      */
     492              :     typedef void (*OnConnectionReceivedFunct)(const TCPEndPointHandle & listeningEndPoint, const TCPEndPointHandle & conEndPoint,
     493              :                                               const IPAddress & peerAddr, uint16_t peerPort);
     494              : 
     495              :     /** The endpoint's connection receive event handling function delegate. */
     496              :     OnConnectionReceivedFunct OnConnectionReceived;
     497              : 
     498              :     /**
     499              :      * @brief   Type of connection acceptance error event handling function.
     500              :      *
     501              :      * @param[in]   listeningEndpoint    The listening TCP endpoint associated with the event.
     502              :      * @param[in]   err                  The reason for the error.
     503              :      *
     504              :      * @details
     505              :      *  Provide a function of this type to the \c OnAcceptError delegate
     506              :      *  member to process connection acceptance error events on \c listeningEndpoint. The
     507              :      *  \c err argument provides specific detail about the type of the error.
     508              :      */
     509              :     typedef void (*OnAcceptErrorFunct)(const TCPEndPointHandle & listeningEndpoint, CHIP_ERROR err);
     510              : 
     511              :     /**
     512              :      * The endpoint's connection acceptance event handling function delegate.
     513              :      */
     514              :     OnAcceptErrorFunct OnAcceptError;
     515              : 
     516              :     /**
     517              :      * Size of the largest TCP packet that can be received.
     518              :      */
     519              :     constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxAllocSize;
     520              : 
     521              : #if INET_CONFIG_TEST
     522              :     static bool sForceEarlyFailureIncomingConnection;
     523              : #endif
     524              : 
     525              :     inline bool operator==(const TCPEndPointHandle & other) const { return other == *this; }
     526              :     inline bool operator!=(const TCPEndPointHandle & other) const { return other != *this; }
     527              : 
     528              : protected:
     529              :     friend class TCPTest;
     530              :     friend class EndPointDeletor<TCPEndPoint>;
     531              :     friend class EndPointHandle<TCPEndPoint>;
     532              : 
     533           60 :     TCPEndPoint(EndPointManager<TCPEndPoint> & endPointManager) :
     534           60 :         EndPointBasis(endPointManager), OnConnectComplete(nullptr), OnDataReceived(nullptr), OnDataSent(nullptr),
     535           60 :         OnConnectionClosed(nullptr), OnPeerClose(nullptr), OnConnectionReceived(nullptr), OnAcceptError(nullptr),
     536          120 :         mState(State::kReady), mReceiveEnabled(true), mConnectTimeoutMsecs(0) // Initialize to zero for using system defaults.
     537              : #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
     538              :         ,
     539           60 :         mUserTimeoutMillis(INET_CONFIG_DEFAULT_TCP_USER_TIMEOUT_MSEC), mUserTimeoutTimerRunning(false)
     540              : #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
     541           60 :     {}
     542              : 
     543           60 :     virtual ~TCPEndPoint() = default;
     544              : 
     545              :     /**
     546              :      * Basic dynamic state of the underlying endpoint.
     547              :      *
     548              :      *  Objects are initialized in the "ready" state, proceed to subsequent
     549              :      *  states corresponding to a simplification of the states of the TCP
     550              :      *  transport state machine.
     551              :      */
     552              :     enum class State : uint8_t
     553              :     {
     554              :         kReady           = 0, /**< Endpoint initialized, but not bound. */
     555              :         kBound           = 1, /**< Endpoint bound, but not listening. */
     556              :         kListening       = 2, /**< Endpoint receiving connections. */
     557              :         kConnecting      = 3, /**< Endpoint attempting to connect. */
     558              :         kConnected       = 4, /**< Endpoint connected, ready for tx/rx. */
     559              :         kSendShutdown    = 5, /**< Endpoint initiated its half-close. */
     560              :         kReceiveShutdown = 6, /**< Endpoint responded to half-close. */
     561              :         kClosing         = 7, /**< Endpoint closing bidirectionally. */
     562              :         kClosed          = 8  /**< Endpoint closed, ready for release. */
     563              :     } mState;
     564              : 
     565              :     /** Control switch indicating whether the application is receiving data. */
     566              :     bool mReceiveEnabled;
     567              : 
     568              :     chip::System::PacketBufferHandle mRcvQueue;
     569              :     chip::System::PacketBufferHandle mSendQueue;
     570              : #if INET_TCP_IDLE_CHECK_INTERVAL > 0
     571              :     static void HandleIdleTimer(System::Layer * aSystemLayer, void * aAppState);
     572              :     static bool IsIdleTimerRunning(EndPointManager<TCPEndPoint> & endPointManager);
     573              :     uint16_t mIdleTimeout;       // in units of INET_TCP_IDLE_CHECK_INTERVAL; zero means no timeout
     574              :     uint16_t mRemainingIdleTime; // in units of INET_TCP_IDLE_CHECK_INTERVAL
     575              : #endif                           // INET_TCP_IDLE_CHECK_INTERVAL > 0
     576              : 
     577              :     uint32_t mConnectTimeoutMsecs; // This is the timeout to wait for a Connect call to succeed or
     578              :                                    // return an error; zero means use system defaults.
     579              : 
     580              : #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
     581              :     uint32_t mUserTimeoutMillis;   // The configured TCP user timeout value in milliseconds.
     582              :                                    // If 0, assume not set.
     583              :     bool mUserTimeoutTimerRunning; // Indicates whether the TCP UserTimeout timer has been started.
     584              : 
     585              :     static void TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState);
     586              :     virtual void TCPUserTimeoutHandler() = 0;
     587              : 
     588              :     void StartTCPUserTimeoutTimer();
     589              :     void StopTCPUserTimeoutTimer();
     590              :     void RestartTCPUserTimeoutTimer();
     591              :     void ScheduleNextTCPUserTimeoutPoll(uint32_t aTimeOut);
     592              : #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
     593              : 
     594              :     TCPEndPoint(const TCPEndPoint &) = delete;
     595              : 
     596              :     CHIP_ERROR DriveSending();
     597              :     // Take in a handle to self to guarantee this sticks around until we're done
     598              :     void DriveReceiving(const TCPEndPointHandle & handle);
     599              :     void HandleConnectComplete(CHIP_ERROR err);
     600              :     void HandleAcceptError(CHIP_ERROR err);
     601              :     void DoClose(CHIP_ERROR err, bool suppressCallback);
     602              :     static bool IsConnected(State state);
     603              : 
     604              :     static void TCPConnectTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState);
     605              : 
     606              :     void StartConnectTimerIfSet();
     607              :     void StopConnectTimer();
     608              : 
     609              :     /*
     610              :      * Implementation helpers for shared methods.
     611              :      */
     612              :     virtual CHIP_ERROR BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) = 0;
     613              :     virtual CHIP_ERROR ListenImpl(uint16_t backlog)                                                            = 0;
     614              :     virtual CHIP_ERROR ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId)                  = 0;
     615              :     virtual CHIP_ERROR SendQueuedImpl(bool queueWasEmpty)                                                      = 0;
     616              :     virtual CHIP_ERROR SetUserTimeoutImpl(uint32_t userTimeoutMillis)                                          = 0;
     617              :     virtual CHIP_ERROR DriveSendingImpl()                                                                      = 0;
     618              :     virtual void HandleConnectCompleteImpl()                                                                   = 0;
     619              :     virtual void DoCloseImpl(CHIP_ERROR err, State oldState)                                                   = 0;
     620              : 
     621              :     /**
     622              :      * @brief   Initiate (or continue) TCP full close, ignoring errors.
     623              :      *
     624              :      * @details
     625              :      *  The object is returned to the free pool, and all remaining user
     626              :      *  references are subsequently invalid.
     627              :      */
     628              :     void Free();
     629              : };
     630              : 
     631              : template <>
     632              : struct EndPointProperties<TCPEndPoint>
     633              : {
     634              :     static constexpr char kName[]         = "TCP";
     635              :     static constexpr size_t kNumEndPoints = INET_CONFIG_NUM_TCP_ENDPOINTS;
     636              :     static constexpr int kSystemStatsKey  = System::Stats::kInetLayer_NumTCPEps;
     637              : };
     638              : 
     639              : } // namespace Inet
     640              : } // namespace chip
        

Generated by: LCOV version 2.0-1