Skip to content

Commit b1d4150

Browse files
authored
[posix] implement otPlatTcp platform abstraction (openthread#13193)
This commit provides a complete POSIX implementation of the new `otPlatTcp` platform abstraction APIs. It leverages the existing OpenThread `Mainloop` polling mechanism to build a fully event-driven, non-blocking TCP socket implementation without requiring extra threads. Key features and implementation details: - Uses `Mainloop` read/write/error sets to monitor socket states. - Implements `otPlatTcpEnableListener` and `otPlatTcpAccept` to handle incoming connection requests over IPv6. - Implements `otPlatTcpConnect` for asynchronous non-blocking outgoing TCP connections, monitoring `SO_ERROR` to detect handshake completion. - Handles data transmission utilizing `otPlatTcpIsTxPending` to monitor the write descriptor and correctly signal `HandleTxReady`. - Implements `otPlatTcpSend` and `otPlatTcpReceive` ensuring proper buffering, suppressing `SIGPIPE` safely using `SO_NOSIGPIPE` and `MSG_NOSIGNAL`. - Encodes socket file descriptors directly within the platform data `mData.mDescriptor` union, avoiding dynamic memory allocation.
1 parent fcf7b4c commit b1d4150

5 files changed

Lines changed: 605 additions & 0 deletions

File tree

src/posix/platform/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ add_library(openthread-posix
152152
spinel_manager.cpp
153153
spi_interface.cpp
154154
system.cpp
155+
tcp.cpp
155156
tmp_storage.cpp
156157
trel.cpp
157158
udp.cpp

src/posix/platform/system.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "posix/platform/radio_url.hpp"
6161
#include "posix/platform/spinel_driver_getter.hpp"
6262
#include "posix/platform/spinel_manager.hpp"
63+
#include "posix/platform/tcp.hpp"
6364
#include "posix/platform/udp.hpp"
6465

6566
otInstance *gInstance = nullptr;
@@ -149,6 +150,10 @@ void platformInitRcpMode(otPlatformConfig *aPlatformConfig)
149150
ot::Posix::InfraNetif::Get().Init();
150151
#endif
151152

153+
#if OPENTHREAD_CONFIG_PLATFORM_TCP_ENABLE
154+
ot::Posix::Tcp::Get().Init();
155+
#endif
156+
152157
#if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
153158
ot::Posix::MdnsSocket::Get().Init();
154159
#endif
@@ -169,6 +174,7 @@ void platformInitRcpMode(otPlatformConfig *aPlatformConfig)
169174
ot::Posix::Udp::Get().Init(aPlatformConfig->mInterfaceName);
170175
#endif
171176
#endif
177+
172178
exit:
173179
return;
174180
}
@@ -248,6 +254,10 @@ void platformSetUp(otPlatformConfig *aPlatformConfig)
248254
ot::Posix::Udp::Get().SetUp();
249255
#endif
250256

257+
#if OPENTHREAD_CONFIG_PLATFORM_TCP_ENABLE
258+
ot::Posix::Tcp::Get().SetUp();
259+
#endif
260+
251261
#if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
252262
ot::Posix::MdnsSocket::Get().SetUp();
253263
#endif
@@ -310,6 +320,10 @@ void platformTearDown(void)
310320
ot::Posix::Udp::Get().TearDown();
311321
#endif
312322

323+
#if OPENTHREAD_CONFIG_PLATFORM_TCP_ENABLE
324+
ot::Posix::Tcp::Get().TearDown();
325+
#endif
326+
313327
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
314328
platformNetifTearDown();
315329
#endif
@@ -341,6 +355,11 @@ void platformDeinitRcpMode(void)
341355
#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE
342356
ot::Posix::Udp::Get().Deinit();
343357
#endif
358+
359+
#if OPENTHREAD_CONFIG_PLATFORM_TCP_ENABLE
360+
ot::Posix::Tcp::Get().Deinit();
361+
#endif
362+
344363
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
345364
platformNetifDeinit();
346365
#endif

0 commit comments

Comments
 (0)