3 * Copyright (c) 2021 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <protocols/secure_channel/Constants.h>
20 #include <protocols/secure_channel/StatusReport.h>
22 #include <support/BufferReader.h>
23 #include <support/CodeUtils.h>
25 #include <type_traits>
27 using namespace chip::Encoding;
28 using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;
32 namespace SecureChannel {
34 StatusReport::StatusReport() : mGeneralCode(GeneralStatusCode::kSuccess), mProtocolId(0), mProtocolCode(0), mProtocolData(nullptr)
37 StatusReport::StatusReport(GeneralStatusCode generalCode, uint32_t protocolId, uint16_t protocolCode) :
38 mGeneralCode(generalCode), mProtocolId(protocolId), mProtocolCode(protocolCode), mProtocolData(nullptr)
41 StatusReport::StatusReport(GeneralStatusCode generalCode, uint32_t protocolId, uint16_t protocolCode,
42 System::PacketBufferHandle protocolData) :
43 mGeneralCode(generalCode),
44 mProtocolId(protocolId), mProtocolCode(protocolCode), mProtocolData(std::move(protocolData))
47 CHIP_ERROR StatusReport::Parse(System::PacketBufferHandle buf)
49 uint16_t tempGeneralCode = 0;
51 ReturnErrorCodeIf(buf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
53 uint8_t * bufStart = buf->Start();
54 LittleEndian::Reader bufReader(bufStart, buf->DataLength());
56 ReturnErrorOnFailure(bufReader.Read16(&tempGeneralCode).Read32(&mProtocolId).Read16(&mProtocolCode).StatusCode());
57 mGeneralCode = static_cast<GeneralStatusCode>(tempGeneralCode);
59 // Any data that exists after the required fields is considered protocol-specific data.
60 if (bufReader.OctetsRead() < buf->DataLength())
62 mProtocolData = System::PacketBufferHandle::NewWithData(buf->Start() + bufReader.OctetsRead(),
63 buf->DataLength() - bufReader.OctetsRead(),
64 /* aAdditionalSize = */ 0, /* aReservedSize = */ 0);
65 if (mProtocolData.IsNull())
67 return CHIP_ERROR_NO_MEMORY;
72 mProtocolData = nullptr;
78 Encoding::LittleEndian::BufferWriter & StatusReport::WriteToBuffer(Encoding::LittleEndian::BufferWriter & buf) const
80 static_assert(std::is_same<std::underlying_type_t<decltype(mGeneralCode)>, uint16_t>::value, "Cast is not safe");
81 buf.Put16(static_cast<uint16_t>(mGeneralCode)).Put32(mProtocolId).Put16(mProtocolCode);
82 if (!mProtocolData.IsNull())
84 buf.Put(mProtocolData->Start(), mProtocolData->DataLength());
89 size_t StatusReport::Size() const
91 LittleEndian::BufferWriter emptyBuf(nullptr, 0);
92 return WriteToBuffer(emptyBuf).Needed();
95 } // namespace SecureChannel
96 } // namespace Protocols