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 : /** 18 : * @file 19 : * This file implements a stateless TransportMgr, it will took a raw message 20 : * buffer from transports, and then extract the message header without decode it. 21 : * For secure messages, it will pass it to the SessionManager, and for unsecure 22 : * messages (rendezvous messages), it will pass it to RendezvousSession. 23 : * When sending messages, it will encode the packet header, and pass it to the 24 : * transports. 25 : * The whole process is fully stateless. 26 : */ 27 : 28 : #pragma once 29 : 30 : #include <lib/support/CodeUtils.h> 31 : #include <lib/support/logging/CHIPLogging.h> 32 : #include <transport/TransportMgrBase.h> 33 : #include <transport/raw/Base.h> 34 : #include <transport/raw/MessageHeader.h> 35 : #include <transport/raw/PeerAddress.h> 36 : #include <transport/raw/Tuple.h> 37 : 38 : namespace chip { 39 : 40 : class TransportMgrBase; 41 : 42 : class TransportMgrDelegate 43 : { 44 : public: 45 101 : virtual ~TransportMgrDelegate() = default; 46 : /** 47 : * @brief 48 : * Handle received secure message. 49 : * 50 : * @param source the source address of the package 51 : * @param msgBuf the buffer containing a full CHIP message (except for the optional length field). 52 : */ 53 : virtual void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) = 0; 54 : }; 55 : 56 : template <typename... TransportTypes> 57 : class TransportMgr : public TransportMgrBase 58 : { 59 : public: 60 : template <typename... Args> 61 1 : CHIP_ERROR Init(Args &&... transportInitArgs) 62 : { 63 1 : ReturnErrorOnFailure(mTransport.Init(this, std::forward<Args>(transportInitArgs)...)); 64 1 : return TransportMgrBase::Init(&mTransport); 65 : } 66 : 67 : template <typename... Args> 68 : CHIP_ERROR ResetTransport(Args &&... transportInitArgs) 69 : { 70 : return mTransport.Init(this, std::forward<Args>(transportInitArgs)...); 71 : } 72 : 73 1 : void Close() 74 : { 75 1 : TransportMgrBase::Close(); 76 1 : mTransport.Close(); 77 1 : }; 78 : 79 : private: 80 : Transport::Tuple<TransportTypes...> mTransport; 81 : 82 : public: 83 1 : auto & GetTransport() { return mTransport; } 84 : }; 85 : 86 : } // namespace chip