Line data Source code
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 <lib/support/SafeInt.h> 27 : 28 : namespace chip { 29 : namespace System { 30 : 31 96 : CHIP_ERROR TLVPacketBufferBackingStore::OnInit(chip::TLV::TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen) 32 : { 33 96 : bufStart = mHeadBuffer->Start(); 34 96 : bufLen = mHeadBuffer->DataLength(); 35 96 : return CHIP_NO_ERROR; 36 : } 37 : 38 0 : CHIP_ERROR TLVPacketBufferBackingStore::GetNextBuffer(chip::TLV::TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen) 39 : { 40 0 : if (mUseChainedBuffers) 41 : { 42 0 : mCurrentBuffer.Advance(); 43 : } 44 : else 45 : { 46 0 : mCurrentBuffer = nullptr; 47 : } 48 : 49 0 : if (mCurrentBuffer.IsNull()) 50 : { 51 0 : bufStart = nullptr; 52 0 : bufLen = 0; 53 : } 54 : else 55 : { 56 0 : bufStart = mCurrentBuffer->Start(); 57 0 : bufLen = mCurrentBuffer->DataLength(); 58 : } 59 : 60 0 : return CHIP_NO_ERROR; 61 : } 62 : 63 19033 : CHIP_ERROR TLVPacketBufferBackingStore::OnInit(chip::TLV::TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen) 64 : { 65 19033 : bufStart = mHeadBuffer->Start() + mHeadBuffer->DataLength(); 66 19033 : bufLen = mHeadBuffer->AvailableDataLength(); 67 19033 : return CHIP_NO_ERROR; 68 : } 69 : 70 23085 : CHIP_ERROR TLVPacketBufferBackingStore::FinalizeBuffer(chip::TLV::TLVWriter & writer, uint8_t * bufStart, uint32_t dataLen) 71 : { 72 23085 : uint8_t * endPtr = bufStart + dataLen; 73 : 74 23085 : intptr_t length = endPtr - mCurrentBuffer->Start(); 75 23085 : if (!CanCastTo<uint16_t>(length)) 76 : { 77 0 : return CHIP_ERROR_INVALID_ARGUMENT; 78 : } 79 23085 : mCurrentBuffer->SetDataLength(static_cast<uint16_t>(length), mHeadBuffer); 80 : 81 23085 : return CHIP_NO_ERROR; 82 : } 83 : 84 2586 : CHIP_ERROR TLVPacketBufferBackingStore::GetNewBuffer(chip::TLV::TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen) 85 : { 86 2586 : if (!mUseChainedBuffers) 87 : { 88 2204 : return CHIP_ERROR_NO_MEMORY; 89 : } 90 : 91 382 : mCurrentBuffer.Advance(); 92 382 : if (mCurrentBuffer.IsNull()) 93 : { 94 382 : mCurrentBuffer = PacketBufferHandle::New(System::PacketBuffer::kMaxSizeWithoutReserve, 0); 95 382 : if (mCurrentBuffer.IsNull()) 96 : { 97 0 : return CHIP_ERROR_NO_MEMORY; 98 : } 99 382 : mHeadBuffer->AddToEnd(mCurrentBuffer.Retain()); 100 : } 101 : 102 382 : if (mCurrentBuffer.IsNull()) 103 : { 104 0 : bufStart = nullptr; 105 0 : bufLen = 0; 106 : } 107 : else 108 : { 109 382 : bufStart = mCurrentBuffer->Start(); 110 382 : bufLen = mCurrentBuffer->MaxDataLength(); 111 : } 112 : 113 382 : return CHIP_NO_ERROR; 114 : } 115 : 116 : } // namespace System 117 : } // namespace chip