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 15136 : CHIP_ERROR TransportMgrBase::SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf)
27 : {
28 15136 : 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::ActiveTCPConnectionHandle & peerConnState)
34 : {
35 0 : return mTransport->TCPConnect(address, appState, peerConnState);
36 : }
37 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
38 :
39 77 : CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
40 : {
41 77 : if (mTransport != nullptr)
42 : {
43 0 : return CHIP_ERROR_INCORRECT_STATE;
44 : }
45 77 : mTransport = transport;
46 77 : mTransport->SetDelegate(this);
47 :
48 77 : ChipLogDetail(Inet, "TransportMgr initialized");
49 77 : return CHIP_NO_ERROR;
50 : }
51 :
52 58 : void TransportMgrBase::Close()
53 : {
54 58 : mSessionManager = nullptr;
55 58 : mTransport = nullptr;
56 58 : }
57 :
58 0 : CHIP_ERROR TransportMgrBase::MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join)
59 : {
60 0 : return mTransport->MulticastGroupJoinLeave(address, join);
61 : }
62 :
63 15044 : 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 15044 : assertChipStackLockedByCurrentThread();
69 :
70 15044 : 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 15044 : if (mSessionManager != nullptr)
80 : {
81 15044 : 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 14 : void TransportMgrBase::HandleConnectionReceived(Transport::ActiveTCPConnectionState & conn)
93 : {
94 14 : if (mSessionManager != nullptr)
95 : {
96 14 : mSessionManager->HandleConnectionReceived(conn);
97 : }
98 14 : }
99 :
100 20 : void TransportMgrBase::HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionHandle & conn, CHIP_ERROR conErr)
101 : {
102 20 : if (mSessionManager != nullptr)
103 : {
104 20 : mSessionManager->HandleConnectionAttemptComplete(conn, conErr);
105 : }
106 20 : }
107 :
108 10 : void TransportMgrBase::HandleConnectionClosed(Transport::ActiveTCPConnectionState & conn, CHIP_ERROR conErr)
109 : {
110 10 : if (mSessionManager != nullptr)
111 : {
112 10 : mSessionManager->HandleConnectionClosed(conn, conErr);
113 : }
114 10 : }
115 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
116 :
117 : } // namespace chip
|