diff --git a/src/core/net/tcp6.cpp b/src/core/net/tcp6.cpp index 8fdb407ff..9b18a06dd 100644 --- a/src/core/net/tcp6.cpp +++ b/src/core/net/tcp6.cpp @@ -123,11 +123,9 @@ exit: return error; } -Instance &Tcp::Endpoint::GetInstance(void) +Instance &Tcp::Endpoint::GetInstance(void) const { - struct tcpcb &tp = GetTcb(); - - return AsCoreType(tp.instance); + return AsNonConst(AsCoreType(GetTcb().instance)); } const SockAddr &Tcp::Endpoint::GetLocalAddress(void) const @@ -160,7 +158,7 @@ Error Tcp::Endpoint::Bind(const SockAddr &aSockName) struct tcpcb &tp = GetTcb(); VerifyOrExit(!AsCoreType(&aSockName.mAddress).IsUnspecified(), error = kErrorInvalidArgs); - VerifyOrExit(GetInstance().Get().CanBind(aSockName), error = kErrorInvalidState); + VerifyOrExit(Get().CanBind(aSockName), error = kErrorInvalidState); memcpy(&tp.laddr, &aSockName.mAddress, sizeof(tp.laddr)); tp.lport = HostSwap16(aSockName.mPort); @@ -275,9 +273,7 @@ Error Tcp::Endpoint::Deinitialize(void) { Error error; - Tcp &tcp = GetInstance().Get(); - - SuccessOrExit(error = tcp.mEndpoints.Remove(*this)); + SuccessOrExit(error = Get().mEndpoints.Remove(*this)); SetNext(nullptr); SuccessOrExit(error = Abort()); @@ -356,7 +352,7 @@ void Tcp::Endpoint::SetTimer(uint8_t aTimerFlag, uint32_t aDelay) LogDebg("Endpoint %p set timer %u to %u ms", static_cast(this), static_cast(timerIndex), static_cast(aDelay)); - GetInstance().Get().mTimer.FireAtIfEarlier(newFireTime); + Get().mTimer.FireAtIfEarlier(newFireTime); } void Tcp::Endpoint::CancelTimer(uint8_t aTimerFlag) @@ -451,7 +447,7 @@ void Tcp::Endpoint::PostCallbacksAfterSend(size_t aSent, size_t aBacklogBefore) if (backlogAfter < aBacklogBefore + aSent && mForwardProgressCallback != nullptr) { mPendingCallbacks |= kForwardProgressCallbackFlag; - GetInstance().Get().mTasklet.Post(); + Get().mTasklet.Post(); } } @@ -542,11 +538,9 @@ exit: return error; } -Instance &Tcp::Listener::GetInstance(void) +Instance &Tcp::Listener::GetInstance(void) const { - struct tcpcb_listen *tpl = &GetTcbListen(); - - return AsCoreType(tpl->instance); + return AsNonConst(AsCoreType(GetTcbListen().instance)); } Error Tcp::Listener::Listen(const SockAddr &aSockName) @@ -555,7 +549,7 @@ Error Tcp::Listener::Listen(const SockAddr &aSockName) uint16_t port = HostSwap16(aSockName.mPort); struct tcpcb_listen *tpl = &GetTcbListen(); - VerifyOrExit(GetInstance().Get().CanBind(aSockName), error = kErrorInvalidState); + VerifyOrExit(Get().CanBind(aSockName), error = kErrorInvalidState); memcpy(&tpl->laddr, &aSockName.mAddress, sizeof(tpl->laddr)); tpl->lport = port; @@ -580,9 +574,7 @@ Error Tcp::Listener::Deinitialize(void) { Error error; - Tcp &tcp = GetInstance().Get(); - - SuccessOrExit(error = tcp.mListeners.Remove(*this)); + SuccessOrExit(error = Get().mListeners.Remove(*this)); SetNext(nullptr); exit: @@ -812,7 +804,7 @@ bool Tcp::AutoBind(const SockAddr &aPeer, SockAddr &aToBind, bool aBindAddress, peerInfo.Clear(); peerInfo.SetPeerAddr(aPeer.GetAddress()); - netifAddress = InstanceLocator::GetInstance().Get().SelectSourceAddress(peerInfo); + netifAddress = Get().SelectSourceAddress(peerInfo); VerifyOrExit(netifAddress != nullptr, success = false); aToBind.GetAddress() = netifAddress->GetAddress(); } @@ -854,12 +846,12 @@ exit: void Tcp::HandleTimer(Timer &aTimer) { - OT_ASSERT(&aTimer == &aTimer.GetInstance().Get().mTimer); + OT_ASSERT(&aTimer == &aTimer.Get().mTimer); LogDebg("Main TCP timer expired"); - aTimer.GetInstance().Get().ProcessTimers(); + aTimer.Get().ProcessTimers(); } -void Tcp::ProcessTimers() +void Tcp::ProcessTimers(void) { TimeMilli now = TimerMilli::GetNow(); bool pendingTimer; @@ -1014,7 +1006,7 @@ void tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag) struct tcpcb *tcplp_sys_accept_ready(struct tcpcb_listen *aTcbListen, struct in6_addr *aAddr, uint16_t aPort) { Tcp::Listener & listener = Tcp::Listener::FromTcbListen(*aTcbListen); - Tcp & tcp = listener.GetInstance().Get(); + Tcp & tcp = listener.Get(); struct tcpcb * rv = (struct tcpcb *)-1; otSockAddr addr; otTcpEndpoint * endpointPtr; @@ -1065,7 +1057,7 @@ bool tcplp_sys_accepted_connection(struct tcpcb_listen *aTcbListen, { Tcp::Listener &listener = Tcp::Listener::FromTcbListen(*aTcbListen); Tcp::Endpoint &endpoint = Tcp::Endpoint::FromTcb(*aAccepted); - Tcp & tcp = endpoint.GetInstance().Get(); + Tcp & tcp = endpoint.Get(); bool accepted = true; if (listener.mAcceptDoneCallback != nullptr) diff --git a/src/core/net/tcp6.hpp b/src/core/net/tcp6.hpp index ef827a33d..501fd5ae1 100644 --- a/src/core/net/tcp6.hpp +++ b/src/core/net/tcp6.hpp @@ -83,7 +83,7 @@ public: * This class represents an endpoint of a TCP/IPv6 connection. * */ - class Endpoint : public otTcpEndpoint, public LinkedListEntry + class Endpoint : public otTcpEndpoint, public LinkedListEntry, public GetProvider { friend class Tcp; friend class LinkedList; @@ -118,7 +118,7 @@ public: * @returns The Instance pointer associated with this Endpoint. * */ - Instance &GetInstance(void); + Instance &GetInstance(void) const; /** * Obtains the context pointer that was associated this Endpoint upon @@ -402,7 +402,7 @@ public: * This class represents a TCP/IPv6 listener. * */ - class Listener : public otTcpListener, public LinkedListEntry + class Listener : public otTcpListener, public LinkedListEntry, public GetProvider { friend class LinkedList; @@ -436,7 +436,7 @@ public: * @returns The otInstance pointer associated with this Listener. * */ - Instance &GetInstance(void); + Instance &GetInstance(void) const; /** * Obtains the context pointer that was associated with this Listener upon