Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / protocols / secure_channel / StatusReport.cpp
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 #include <protocols/secure_channel/Constants.h>
20 #include <protocols/secure_channel/StatusReport.h>
21
22 #include <support/BufferReader.h>
23 #include <support/CodeUtils.h>
24
25 #include <type_traits>
26
27 using namespace chip::Encoding;
28 using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;
29
30 namespace chip {
31 namespace Protocols {
32 namespace SecureChannel {
33
34 StatusReport::StatusReport() : mGeneralCode(GeneralStatusCode::kSuccess), mProtocolId(0), mProtocolCode(0), mProtocolData(nullptr)
35 {}
36
37 StatusReport::StatusReport(GeneralStatusCode generalCode, uint32_t protocolId, uint16_t protocolCode) :
38     mGeneralCode(generalCode), mProtocolId(protocolId), mProtocolCode(protocolCode), mProtocolData(nullptr)
39 {}
40
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))
45 {}
46
47 CHIP_ERROR StatusReport::Parse(System::PacketBufferHandle buf)
48 {
49     uint16_t tempGeneralCode = 0;
50
51     ReturnErrorCodeIf(buf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
52
53     uint8_t * bufStart = buf->Start();
54     LittleEndian::Reader bufReader(bufStart, buf->DataLength());
55
56     ReturnErrorOnFailure(bufReader.Read16(&tempGeneralCode).Read32(&mProtocolId).Read16(&mProtocolCode).StatusCode());
57     mGeneralCode = static_cast<GeneralStatusCode>(tempGeneralCode);
58
59     // Any data that exists after the required fields is considered protocol-specific data.
60     if (bufReader.OctetsRead() < buf->DataLength())
61     {
62         mProtocolData = System::PacketBufferHandle::NewWithData(buf->Start() + bufReader.OctetsRead(),
63                                                                 buf->DataLength() - bufReader.OctetsRead(),
64                                                                 /* aAdditionalSize = */ 0, /* aReservedSize = */ 0);
65         if (mProtocolData.IsNull())
66         {
67             return CHIP_ERROR_NO_MEMORY;
68         }
69     }
70     else
71     {
72         mProtocolData = nullptr;
73     }
74
75     return CHIP_NO_ERROR;
76 }
77
78 Encoding::LittleEndian::BufferWriter & StatusReport::WriteToBuffer(Encoding::LittleEndian::BufferWriter & buf) const
79 {
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())
83     {
84         buf.Put(mProtocolData->Start(), mProtocolData->DataLength());
85     }
86     return buf;
87 }
88
89 size_t StatusReport::Size() const
90 {
91     LittleEndian::BufferWriter emptyBuf(nullptr, 0);
92     return WriteToBuffer(emptyBuf).Needed();
93 }
94
95 } // namespace SecureChannel
96 } // namespace Protocols
97 } // namespace chip