Skip to content

Conversation

@yufengwangca
Copy link
Contributor

Summary

This flag is used to control (1) advertisement of TCP support, (2) handling of TCP requests, ..

Related issues

N/A

Testing

Readability checklist

The checklist below will help the reviewer finish PR review in time and keep the
code readable:

  • PR title is
    descriptive
  • Apply the
    “When in Rome…”
    rule (coding style)
  • PR size is short
  • Try to avoid "squashing" and "force-update" in commit history
  • CI time didn't increase

See: Pull Request Guidelines

Copilot AI review requested due to automatic review settings December 3, 2025 23:25
@yufengwangca yufengwangca marked this pull request as draft December 3, 2025 23:25
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a runtime flag enableTcpEndpoint to control the initialization of TCP endpoints in the DeviceControllerFactory. The changes are well-implemented, adding the flag to FactoryInitParams and using it to configure the TCP listener.

My review includes one suggestion to also control the advertisement of TCP support, which seems to be part of the original intent according to the PR description. This will make the feature complete.

#endif // CONFIG_DEVICE_LAYER

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
auto tcpListenParams = Transport::TcpListenParameters(stateParams.tcpEndPointManager)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully control TCP support, including advertisement as mentioned in the PR summary, you should also call SetTCPServerEnabled on the DnssdServer singleton. This will ensure that TCP support is correctly advertised based on the mEnableTcpEndpoint flag.

You might need to add #include <app/server/Dnssd.h> if it's not already included.

    chip::app::DnssdServer::Instance().SetTCPServerEnabled(mEnableTcpEndpoint);
    auto tcpListenParams = Transport::TcpListenParameters(stateParams.tcpEndPointManager)

Copilot finished reviewing on behalf of yufengwangca December 3, 2025 23:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a runtime flag enableTcpEndpoint to control TCP endpoint behavior in the DeviceControllerFactory. However, there is a critical bug: the implementation introduces a breaking behavior change. The original code explicitly initialized the controller as a TCP client only (with server listening disabled), but the new flag defaults to true, which enables TCP server mode by default.

Key Changes

  • Adds enableTcpEndpoint boolean flag to FactoryInitParams (defaults to true)
  • Adds mEnableTcpEndpoint member variable to DeviceControllerFactory class
  • Uses the flag to control SetServerListenEnabled() for both IPv6 and IPv4 TCP transports

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/controller/CHIPDeviceControllerFactory.h Adds enableTcpEndpoint parameter to FactoryInitParams struct and corresponding member variable to DeviceControllerFactory class
src/controller/CHIPDeviceControllerFactory.cpp Initializes the new flag in Init() and ReinitSystemStateIfNecessary(), and uses it to control TCP server listening in InitSystemState()

.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(params.listenPort)
.SetServerListenEnabled(false); // Initialize as a TCP Client
.SetServerListenEnabled(mEnableTcpEndpoint); // Control TCP via runtime flag
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a critical behavior change. The original code explicitly set SetServerListenEnabled(false) to initialize as a TCP client only. With the default value of mEnableTcpEndpoint = true, this now enables TCP server mode by default.

The documentation states that when enableTcpEndpoint = false, TCP endpoints should not be initialized. However, SetServerListenEnabled doesn't control whether TCP is initialized—it controls whether TCP server listening is enabled. Even with SetServerListenEnabled(false), TCP client functionality remains active.

If the intent is to completely disable TCP when enableTcpEndpoint = false, the TCP parameters should be conditionally omitted from the transport manager initialization (see lines 196-205 where tcpListenParams is added to transportMgr->Init()). If the intent is to control server vs. client mode, then enableTcpEndpoint should default to false to preserve backward compatibility.

Copilot uses AI. Check for mistakes.
.SetAddressType(Inet::IPAddressType::kIPv4)
.SetListenPort(params.listenPort)
.SetServerListenEnabled(false)
.SetServerListenEnabled(mEnableTcpEndpoint)
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as line 143: this changes the behavior from TCP client-only (server listening disabled) to TCP server mode enabled by default, which is a breaking change. This should preserve the original false value for backward compatibility, or the entire TCP initialization should be conditional based on mEnableTcpEndpoint.

Copilot uses AI. Check for mistakes.
Comment on lines +184 to +185
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
bool enableTcpEndpoint = true;
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of true changes the existing behavior. The DeviceControllerFactory previously initialized TCP in client-only mode (with SetServerListenEnabled(false)). Setting this default to true will enable TCP server mode by default, which is a breaking change.

Additionally, the documentation comment states that when false, "TCP endpoints will not be initialized," but SetServerListenEnabled doesn't prevent TCP initialization—it only controls server listening. The implementation and documentation are inconsistent. Either:

  1. Default this to false to preserve backward compatibility, or
  2. Implement conditional TCP initialization based on this flag (skipping TCP parameters in transport manager init when false)
Suggested change
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
bool enableTcpEndpoint = true;
// When false, TCP endpoints will not be initialized, preserving previous client-only behavior.
bool enableTcpEndpoint = false;

Copilot uses AI. Check for mistakes.
Comment on lines +183 to +184
// Controls whether TCP endpoints should be enabled during transport manager initialization.
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comment is inconsistent with the implementation. The comment states "TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined" when this is false, but the implementation only controls SetServerListenEnabled(), which toggles TCP server listening, not TCP initialization itself. TCP client functionality will still be initialized and available even when this is false.

If the intent is to completely disable TCP transport, the implementation should conditionally omit TCP parameters from the transport manager initialization. If the intent is only to control server vs. client mode, the documentation should be clarified to state "TCP server listening will not be enabled" rather than "TCP endpoints will not be initialized."

Suggested change
// Controls whether TCP endpoints should be enabled during transport manager initialization.
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
// Controls whether TCP server listening should be enabled during transport manager initialization.
// When false, TCP server listening will not be enabled even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.

Copilot uses AI. Check for mistakes.
mCertificateValidityPolicy = params.certificateValidityPolicy;
mSessionResumptionStorage = params.sessionResumptionStorage;
mEnableServerInteractions = params.enableServerInteractions;
mEnableTcpEndpoint = params.enableTcpEndpoint;
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new enableTcpEndpoint parameter lacks test coverage. Given that this controls TCP transport initialization behavior and there are existing tests in src/controller/tests/, consider adding tests that verify:

  1. TCP server listening is disabled when enableTcpEndpoint = false
  2. TCP server listening is enabled when enableTcpEndpoint = true
  3. The behavior is correctly preserved across ReinitSystemStateIfNecessary() calls

Reference: Existing transport tests in src/transport/raw/tests/TestTCP.cpp show patterns like InitializeAsTCPClient and InitializeAsTCPClientServer that could be adapted.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

github-actions bot commented Dec 3, 2025

PR #42276: Size comparison from c0f9f61 to 5a19169

Full report (35 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, efr32, esp32, nrfconnect, nxp, psoc6, qpg, realtek, stm32, telink)
platform target config section c0f9f615 5a19169 change % change
bl602 lighting-app bl602+mfd+littlefs+rpc FLASH 1106714 1106714 0 0.0
RAM 178954 178954 0 0.0
bl702 lighting-app bl702+eth FLASH 661534 661534 0 0.0
RAM 135025 135025 0 0.0
bl702+wifi FLASH 837384 837384 0 0.0
RAM 124485 124485 0 0.0
bl706+mfd+rpc+littlefs FLASH 1071032 1071032 0 0.0
RAM 117341 117341 0 0.0
bl702l contact-sensor-app bl702l+mfd+littlefs FLASH 903852 903852 0 0.0
RAM 105932 105932 0 0.0
lighting-app bl702l+mfd+littlefs FLASH 983778 983778 0 0.0
RAM 109844 109844 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 771448 771448 0 0.0
RAM 103392 103392 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 784260 784260 0 0.0
RAM 108712 108712 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 729280 729280 0 0.0
RAM 97452 97452 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 713728 713728 0 0.0
RAM 97660 97660 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 555260 555260 0 0.0
RAM 205472 205472 0 0.0
lock CC3235SF_LAUNCHXL FLASH 589200 589200 0 0.0
RAM 205720 205720 0 0.0
efr32 lock-app BRD4187C FLASH 965420 965420 0 0.0
RAM 123776 123776 0 0.0
BRD4338a FLASH 760240 760240 0 0.0
RAM 254388 254388 0 0.0
window-app BRD4187C FLASH 1061072 1061072 0 0.0
RAM 120004 120004 0 0.0
esp32 all-clusters-app c3devkit DRAM 102804 102804 0 0.0
FLASH 1840120 1840120 0 0.0
IRAM 93540 93540 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 937940 937940 0 0.0
RAM 161720 161720 0 0.0
nxp contact mcxw71+release FLASH 695840 695840 0 0.0
RAM 61744 61744 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1683620 1683620 0 0.0
RAM 214188 214188 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1597956 1597956 0 0.0
RAM 211356 211356 0 0.0
light cy8ckit_062s2_43012 FLASH 1461236 1461236 0 0.0
RAM 197800 197800 0 0.0
lock cy8ckit_062s2_43012 FLASH 1495188 1495188 0 0.0
RAM 225672 225672 0 0.0
qpg lighting-app qpg6200+debug FLASH 839784 839784 0 0.0
RAM 127976 127976 0 0.0
lock-app qpg6200+debug FLASH 776656 776656 0 0.0
RAM 118920 118920 0 0.0
realtek light-switch-app rtl8777g FLASH 709864 709864 0 0.0
RAM 107188 107188 0 0.0
lighting-app rtl8777g FLASH 758560 758560 0 0.0
RAM 127320 127320 0 0.0
stm32 light STM32WB5MM-DK FLASH 470908 470908 0 0.0
RAM 141392 141392 0 0.0
telink bridge-app tl7218x FLASH 704394 704394 0 0.0
RAM 90760 90760 0 0.0
light-app-ota-compress-lzma-shell-factory-data tl3218x FLASH 790742 790742 0 0.0
RAM 41176 41176 0 0.0
light-app-ota-shell-factory-data tl7218x FLASH 782054 782054 0 0.0
RAM 93860 93860 0 0.0
light-switch-app-ota-compress-lzma-factory-data tl7218x_retention FLASH 710538 710538 0 0.0
RAM 52232 52232 0 0.0
light-switch-app-ota-compress-lzma-shell-factory-data tlsr9528a FLASH 746370 746370 0 0.0
RAM 71256 71256 0 0.0
light-switch-app-ota-factory-data tl3218x_retention FLASH 721050 721050 0 0.0
RAM 34956 34956 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 602946 602946 0 0.0
RAM 117320 117320 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 815562 815566 4 0.0
RAM 92248 92248 0 0.0


// Controls whether TCP endpoints should be enabled during transport manager initialization.
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
bool enableTcpEndpoint = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bool is not really an enabler/disabler of TCPEndpoint. It is only meant for controlling whether TCP server listening would happen for incoming connections which requires listening on a socket and accepting incoming connection requests. Also used as a parameter to control the DNSSD advertisements around TCP support.
When false, the client would still need to instantiate a TCPEndpoint to establish a connection with a remote peer server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I try to omit TCP tube in TransportManager when the runtime flag is false, I think this is the safe way to disable the TCP capability on controller.

However, the DeviceTransportMgr type is defined at compile-time and always includes TCP when INET_CONFIG_ENABLE_TCP_ENDPOINT is set. The runtime flag cannot change this type definition.

The fundamental issue: I cannot have an if/else that omits parameters when the compile-time type expects them. The type system requires the same number of parameters regardless of runtime conditions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag was already false on the controller side by default. But we can place it in the params for more flexibility. Would recommend naming it correctly, something like enableTCPServerListen.
The runtime aspect of using TCP at the controller would have to happen w.r.t each peer that it is communicating with, and would depend on whether the peer it wants to interact with supports TCP or not. So, the compile flag should always have TCP built.
The DNSSD records are not percolated up for the caller to the session establishment to make that choice.
So, in the current model, one flow that the controller can do is to always try and establish a TCP session and based on the error returned(most likely Connection Refused), decides to fall back to MRP. This requires higher level logic.
The other model( more involved) is, potentially to use a new intent flag, to capture the results of the DNS discovery to understand if the peer supports TCP and then use TCP as the session transport, otherwise use MRP.

@yufengwangca yufengwangca changed the title Add a flag to disable/enable TCP endpoint in runtime [TEST] Add a flag to disable/enable TCP endpoint in runtime Dec 4, 2025

// Controls whether TCP endpoints should be enabled during transport manager initialization.
// When false, TCP endpoints will not be initialized even if INET_CONFIG_ENABLE_TCP_ENDPOINT is defined.
bool enableTcpEndpoint = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation and naming here need to accurately reflect what this actually does.

"endpoint" in the TCP sense is an implementation detail and does not belong in the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants