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 15114 : CHIP_ERROR TransportMgrBase::SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf)
27 : {
28 15114 : 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::ActiveTCPConnectionHolder & peerConnState)
34 : {
35 0 : return mTransport->TCPConnect(address, appState, peerConnState);
36 : }
37 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
38 :
39 64 : CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
40 : {
41 64 : if (mTransport != nullptr)
42 : {
43 0 : return CHIP_ERROR_INCORRECT_STATE;
44 : }
45 64 : mTransport = transport;
46 64 : mTransport->SetDelegate(this);
47 :
48 64 : ChipLogDetail(Inet, "TransportMgr initialized");
49 64 : return CHIP_NO_ERROR;
50 : }
51 :
52 50 : void TransportMgrBase::Close()
53 : {
54 50 : mSessionManager = nullptr;
55 50 : mTransport = nullptr;
56 50 : }
57 :
58 0 : CHIP_ERROR TransportMgrBase::MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join)
59 : {
60 0 : return mTransport->MulticastGroupJoinLeave(address, join);
61 : }
62 :
63 14992 : void TransportMgrBase::HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg,
64 : Transport::MessageTransportContext * ctxt)
65 : {
66 : // This is the first point all incoming messages funnel through. Ensure
67 : // that our message receipts are all synchronized correctly.
68 14992 : assertChipStackLockedByCurrentThread();
69 :
70 14992 : if (msg->HasChainedBuffer())
71 : {
72 : // Something in the lower levels messed up.
73 : char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
74 0 : peerAddress.ToString(addrBuffer);
75 0 : ChipLogError(Inet, "message from %s dropped due to lower layers not ensuring a single packet buffer.", addrBuffer);
76 0 : return;
77 : }
78 :
79 14992 : if (mSessionManager != nullptr)
80 : {
81 14992 : mSessionManager->OnMessageReceived(peerAddress, std::move(msg), ctxt);
82 : }
83 : else
84 : {
85 : char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
86 0 : peerAddress.ToString(addrBuffer);
87 0 : ChipLogError(Inet, "message from %s is dropped since no corresponding handler is set in TransportMgr.", addrBuffer);
88 : }
89 : }
90 :
91 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
92 12 : void TransportMgrBase::HandleConnectionReceived(Transport::ActiveTCPConnectionState & conn)
93 : {
94 12 : if (mSessionManager != nullptr)
95 : {
96 12 : mSessionManager->HandleConnectionReceived(conn);
97 : }
98 : else
99 : {
100 : // Close connection here since no upper layer is interested in the connection.
101 0 : Transport::ActiveTCPConnectionHolder releaseConnection(&conn);
102 0 : }
103 12 : }
104 :
105 14 : void TransportMgrBase::HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionHolder & conn, CHIP_ERROR conErr)
106 : {
107 14 : if (mSessionManager != nullptr)
108 : {
109 14 : mSessionManager->HandleConnectionAttemptComplete(conn, conErr);
110 : }
111 14 : }
112 :
113 5 : void TransportMgrBase::HandleConnectionClosed(Transport::ActiveTCPConnectionState & conn, CHIP_ERROR conErr)
114 : {
115 5 : if (mSessionManager != nullptr)
116 : {
117 5 : mSessionManager->HandleConnectionClosed(conn, conErr);
118 : }
119 5 : }
120 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
121 :
122 : } // namespace chip
|