Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2021 Project CHIP Authors 4 : * All rights reserved. 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : 19 : #pragma once 20 : 21 : #include <lib/support/BufferWriter.h> 22 : #include <protocols/Protocols.h> 23 : #include <protocols/secure_channel/Constants.h> 24 : #include <system/SystemClock.h> 25 : #include <system/SystemPacketBuffer.h> 26 : 27 : namespace chip { 28 : namespace Protocols { 29 : namespace SecureChannel { 30 : 31 : /** 32 : * Encapsulates the data included in a StatusReport message, and provides methods for writing to and reading from PacketBuffers 33 : * that contain StatusReport messages. 34 : */ 35 : class DLL_EXPORT StatusReport 36 : { 37 : public: 38 : /** 39 : * Construct a StatusReport with zero-d out fields (for use before calling \c Parse() ). 40 : */ 41 : StatusReport(); 42 : 43 : /** 44 : * Construct a StatusReport with no additional ProtocolData. 45 : * 46 : * @param generalCode Required, one of the \c GeneralStatusCode values listed in \c secure_channel/Constants.h 47 : * @param protocolId Must specify a ProtocolId which consists of Vendor Id (upper 16 bits) and ProtocolId (lower 16 bits) 48 : * @param protocolCode A code defined by the specified protocol which provides more information about the status 49 : */ 50 : StatusReport(GeneralStatusCode generalCode, Protocols::Id protocolId, uint16_t protocolCode); 51 : 52 : // 53 : /** 54 : * Construct a StatusReport with additional ProtocolData. 55 : * 56 : * @param generalCode Must specify a GeneralCode (see \c GeneralStatusCode ) 57 : * @param protocolId Must specify a ProtocolId which consists of Vendor Id (upper 16 bits) and ProtocolId (lower 16 bits) 58 : * @param protocolCode A code defined by the specified protocol which provides more information about the status 59 : * @param protocolData A \c PacketBufferHandle containing the protocol-specific data 60 : */ 61 : StatusReport(GeneralStatusCode generalCode, Protocols::Id protocolId, uint16_t protocolCode, 62 : System::PacketBufferHandle protocolData); 63 : 64 : /** 65 : * Read the contents of a \c PacketBuffer containing a StatusReport message and store the field values in this object. 66 : * 67 : * @note If there is additional data after the Protocol Code field in the message, it is assumed to be protocol-specific data. 68 : * 69 : * @note This method assumes that the Header of the message has already been consumed, and that \c PacketBuffer::Start() points 70 : * to the beginning of the StatusReport data. 71 : * 72 : * @param[in] buf A \c PacketBufferHandle containing the StatusReport message. This method will take ownership, and will 73 : * allocate a new PacketBuffer if any protocol-specific data exists. 74 : * 75 : * @return CHIP_ERROR Return an error if the message is malformed or buf is \c NULL 76 : */ 77 : CHIP_ERROR Parse(System::PacketBufferHandle buf); 78 : 79 : /** 80 : * Write the StatusReport contents into a buffer using a \c BufferWriter 81 : * 82 : * @param[out] buf A \c BufferWriter which contains the buffer that will store the message fields. 83 : * 84 : * @return BufferWriter Return a reference to the \c BufferWriter 85 : */ 86 : Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & buf) const; 87 : 88 : /** 89 : * Returns the minimum size of the buffer needed to write the message. 90 : */ 91 : size_t Size() const; 92 : 93 8 : GeneralStatusCode GetGeneralCode() const { return mGeneralCode; } 94 6 : Protocols::Id GetProtocolId() const { return mProtocolId; } 95 6 : uint16_t GetProtocolCode() const { return mProtocolCode; } 96 3 : const System::PacketBufferHandle & GetProtocolData() const { return mProtocolData; } 97 : 98 : /** 99 : * Builds a busy status report with protocol data containing the minimum wait time. 100 : * 101 : * @param[in] minimumWaitTime Time in milliseconds before initiator retries the request 102 : * 103 : * @return Packet buffer handle which can be passed to SendMessage. 104 : */ 105 : static System::PacketBufferHandle MakeBusyStatusReportMessage(System::Clock::Milliseconds16 minimumWaitTime); 106 : 107 : private: 108 : GeneralStatusCode mGeneralCode; 109 : Protocols::Id mProtocolId; 110 : uint16_t mProtocolCode; 111 : 112 : System::PacketBufferHandle mProtocolData; 113 : }; 114 : 115 : } // namespace SecureChannel 116 : } // namespace Protocols 117 : } // namespace chip