Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 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 : */
16 :
17 : #include <transport/TransportMgrBase.h>
18 :
19 : #include <lib/support/CodeUtils.h>
20 : #include <platform/LockTracker.h>
21 : #include <transport/TransportMgr.h>
22 : #include <transport/raw/Base.h>
23 :
24 : namespace chip {
25 :
26 9857 : CHIP_ERROR TransportMgrBase::SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf)
27 : {
28 9857 : return mTransport->SendMessage(address, std::move(msgBuf));
29 : }
30 :
31 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
32 0 : CHIP_ERROR TransportMgrBase::TCPConnect(const Transport::PeerAddress & address, Transport::AppTCPConnectionCallbackCtxt * appState,
33 : Transport::ActiveTCPConnectionState ** peerConnState)
34 : {
35 0 : return mTransport->TCPConnect(address, appState, peerConnState);
36 : }
37 :
38 0 : void TransportMgrBase::TCPDisconnect(const Transport::PeerAddress & address)
39 : {
40 0 : mTransport->TCPDisconnect(address);
41 0 : }
42 :
43 0 : void TransportMgrBase::TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort)
44 : {
45 0 : mTransport->TCPDisconnect(conn, shouldAbort);
46 0 : }
47 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
48 :
49 59 : CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
50 : {
51 59 : if (mTransport != nullptr)
52 : {
53 0 : return CHIP_ERROR_INCORRECT_STATE;
54 : }
55 59 : mTransport = transport;
56 59 : mTransport->SetDelegate(this);
57 :
58 59 : ChipLogDetail(Inet, "TransportMgr initialized");
59 59 : return CHIP_NO_ERROR;
60 : }
61 :
62 47 : void TransportMgrBase::Close()
63 : {
64 47 : mSessionManager = nullptr;
65 47 : mTransport = nullptr;
66 47 : }
67 :
68 0 : CHIP_ERROR TransportMgrBase::MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join)
69 : {
70 0 : return mTransport->MulticastGroupJoinLeave(address, join);
71 : }
72 :
73 9750 : void TransportMgrBase::HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg,
74 : Transport::MessageTransportContext * ctxt)
75 : {
76 : // This is the first point all incoming messages funnel through. Ensure
77 : // that our message receipts are all synchronized correctly.
78 9750 : assertChipStackLockedByCurrentThread();
79 :
80 9750 : if (msg->HasChainedBuffer())
81 : {
82 : // Something in the lower levels messed up.
83 : char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
84 0 : peerAddress.ToString(addrBuffer);
85 0 : ChipLogError(Inet, "message from %s dropped due to lower layers not ensuring a single packet buffer.", addrBuffer);
86 0 : return;
87 : }
88 :
89 9750 : if (mSessionManager != nullptr)
90 : {
91 9750 : mSessionManager->OnMessageReceived(peerAddress, std::move(msg), ctxt);
92 : }
93 : else
94 : {
95 : char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
96 0 : peerAddress.ToString(addrBuffer);
97 0 : ChipLogError(Inet, "message from %s is dropped since no corresponding handler is set in TransportMgr.", addrBuffer);
98 : }
99 : }
100 :
101 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
102 10 : void TransportMgrBase::HandleConnectionReceived(Transport::ActiveTCPConnectionState * conn)
103 : {
104 10 : if (mSessionManager != nullptr)
105 : {
106 10 : mSessionManager->HandleConnectionReceived(conn);
107 : }
108 : else
109 : {
110 0 : Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
111 :
112 : // Close connection here since no upper layer is interested in the
113 : // connection.
114 0 : if (tcp)
115 : {
116 0 : tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
117 : }
118 : }
119 10 : }
120 :
121 10 : void TransportMgrBase::HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
122 : {
123 10 : if (mSessionManager != nullptr)
124 : {
125 10 : mSessionManager->HandleConnectionAttemptComplete(conn, conErr);
126 : }
127 : else
128 : {
129 0 : Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
130 :
131 : // Close connection here since no upper layer is interested in the
132 : // connection.
133 0 : if (tcp)
134 : {
135 0 : tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
136 : }
137 : }
138 10 : }
139 :
140 10 : void TransportMgrBase::HandleConnectionClosed(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
141 : {
142 10 : if (mSessionManager != nullptr)
143 : {
144 10 : mSessionManager->HandleConnectionClosed(conn, conErr);
145 : }
146 : else
147 : {
148 0 : Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
149 0 : if (tcp)
150 : {
151 0 : tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
152 : }
153 : }
154 10 : }
155 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
156 :
157 : } // namespace chip
|