Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020 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 : * limitations under the License. 16 : */ 17 : 18 : /** 19 : * @file 20 : * Defines base properties and constants valid across all transport 21 : * classes (UDP, TCP, BLE, ....) 22 : */ 23 : 24 : #pragma once 25 : 26 : #include <inet/IPAddress.h> 27 : #include <inet/UDPEndPoint.h> 28 : #include <lib/core/CHIPError.h> 29 : #include <system/SystemPacketBuffer.h> 30 : #include <transport/raw/MessageHeader.h> 31 : #include <transport/raw/PeerAddress.h> 32 : 33 : namespace chip { 34 : namespace Transport { 35 : 36 : class RawTransportDelegate 37 : { 38 : public: 39 1 : virtual ~RawTransportDelegate() {} 40 : virtual void HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg) = 0; 41 : }; 42 : 43 : /** 44 : * Transport class base, defining common methods among transports (message 45 : * packing by encoding and decoding headers) and generic message transport 46 : * methods. 47 : */ 48 : class Base 49 : { 50 : public: 51 13 : virtual ~Base() {} 52 : 53 : /** 54 : * Sets the delegate of the transport 55 : * 56 : * @param[in] delegate The argument to pass in to the handler function 57 : * 58 : */ 59 9 : void SetDelegate(RawTransportDelegate * delegate) { mDelegate = delegate; } 60 : 61 : /** 62 : * @brief Send a message to the specified target. 63 : * 64 : * On connection-oriented transports, sending a message implies connecting to the target first. 65 : */ 66 : virtual CHIP_ERROR SendMessage(const PeerAddress & address, System::PacketBufferHandle && msgBuf) = 0; 67 : 68 : /** 69 : * Determine if this transport can SendMessage to the specified peer address. 70 : * 71 : * Generally it is expected that a transport can send to any peer from which it receives a message. 72 : */ 73 : virtual bool CanSendToPeer(const PeerAddress & address) = 0; 74 : 75 : /** 76 : * Determine if this transport can Listen to IPV6 Multicast. 77 : */ 78 0 : virtual bool CanListenMulticast() { return false; } 79 : 80 : /** 81 : * Handle disconnection from the specified peer if currently connected to it. 82 : */ 83 0 : virtual void Disconnect(const PeerAddress & address) {} 84 : 85 : /** 86 : * Enable Listening for multicast messages ( IPV6 UDP only) 87 : */ 88 0 : virtual CHIP_ERROR MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join) { return CHIP_ERROR_INTERNAL; } 89 : 90 : /** 91 : * Close the open endpoint without destroying the object 92 : */ 93 1 : virtual void Close() {} 94 : 95 : protected: 96 : /** 97 : * Method used by subclasses to notify that a packet has been received after 98 : * any associated headers have been decoded. 99 : */ 100 11 : void HandleMessageReceived(const PeerAddress & source, System::PacketBufferHandle && buffer) 101 : { 102 11 : mDelegate->HandleMessageReceived(source, std::move(buffer)); 103 11 : } 104 : 105 : RawTransportDelegate * mDelegate; 106 : }; 107 : 108 : } // namespace Transport 109 : } // namespace chip