Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / TLVPacketBufferBackingStore.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    Copyright (c) 2013-2017 Nest Labs, Inc.
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 /**
20  *    @file
21  *      This file contains an implementation of TLVBackingStore using PacketBuffers.
22  */
23
24 #include <system/TLVPacketBufferBackingStore.h>
25
26 #include <support/SafeInt.h>
27
28 namespace chip {
29 namespace System {
30
31 CHIP_ERROR TLVPacketBufferBackingStore::OnInit(chip::TLV::TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen)
32 {
33     bufStart = mHeadBuffer->Start();
34     bufLen   = mHeadBuffer->DataLength();
35     return CHIP_NO_ERROR;
36 }
37
38 CHIP_ERROR TLVPacketBufferBackingStore::GetNextBuffer(chip::TLV::TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen)
39 {
40     if (mUseChainedBuffers)
41     {
42         mCurrentBuffer.Advance();
43     }
44     else
45     {
46         mCurrentBuffer = nullptr;
47     }
48
49     if (mCurrentBuffer.IsNull())
50     {
51         bufStart = nullptr;
52         bufLen   = 0;
53     }
54     else
55     {
56         bufStart = mCurrentBuffer->Start();
57         bufLen   = mCurrentBuffer->DataLength();
58     }
59
60     return CHIP_NO_ERROR;
61 }
62
63 CHIP_ERROR TLVPacketBufferBackingStore::OnInit(chip::TLV::TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen)
64 {
65     bufStart = mHeadBuffer->Start() + mHeadBuffer->DataLength();
66     bufLen   = mHeadBuffer->AvailableDataLength();
67     return CHIP_NO_ERROR;
68 }
69
70 CHIP_ERROR TLVPacketBufferBackingStore::FinalizeBuffer(chip::TLV::TLVWriter & writer, uint8_t * bufStart, uint32_t dataLen)
71 {
72     uint8_t * endPtr = bufStart + dataLen;
73
74     intptr_t length = endPtr - mCurrentBuffer->Start();
75     if (!CanCastTo<uint16_t>(length))
76     {
77         return CHIP_ERROR_INVALID_ARGUMENT;
78     }
79     mCurrentBuffer->SetDataLength(static_cast<uint16_t>(length));
80
81     return CHIP_NO_ERROR;
82 }
83
84 CHIP_ERROR TLVPacketBufferBackingStore::GetNewBuffer(chip::TLV::TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen)
85 {
86     if (!mUseChainedBuffers)
87     {
88         return CHIP_ERROR_NO_MEMORY;
89     }
90
91     mCurrentBuffer.Advance();
92     if (mCurrentBuffer.IsNull())
93     {
94         mCurrentBuffer = PacketBufferHandle::New(System::PacketBuffer::kMaxSizeWithoutReserve, 0);
95         if (mCurrentBuffer.IsNull())
96         {
97             return CHIP_ERROR_NO_MEMORY;
98         }
99         mHeadBuffer->AddToEnd(mCurrentBuffer.Retain());
100     }
101
102     if (mCurrentBuffer.IsNull())
103     {
104         bufStart = nullptr;
105         bufLen   = 0;
106     }
107     else
108     {
109         bufStart = mCurrentBuffer->Start();
110         bufLen   = mCurrentBuffer->MaxDataLength();
111     }
112
113     return CHIP_NO_ERROR;
114 }
115
116 } // namespace System
117 } // namespace chip