Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2024 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 : #pragma once
18 :
19 : #include <cstdint>
20 : #include <vector>
21 :
22 : #include <lib/core/CHIPError.h>
23 : #include <lib/core/TLVBackingStore.h>
24 : #include <lib/core/TLVCommon.h>
25 :
26 : namespace chip {
27 : namespace TLV {
28 :
29 : // Implementation of TLVWriter that writes to a std::vector, automatically
30 : // resizing the vector as needed when writing.
31 : // Users of TlvVectorWriter may call any public API of TLVWriter, except for the
32 : // Init functions.
33 : // This class is not thread-safe, it must be constructed, used, and destroyed on
34 : // a single thread.
35 : class TlvVectorWriter : public TLVWriter
36 : {
37 : public:
38 : // All data will be written to and read from the provided buffer, which must
39 : // outlive this object.
40 : TlvVectorWriter(std::vector<uint8_t> & buffer);
41 : TlvVectorWriter(const TlvVectorWriter &) = delete;
42 : TlvVectorWriter & operator=(const TlvVectorWriter &) = delete;
43 : ~TlvVectorWriter();
44 :
45 : private:
46 : class TlvVectorBuffer : public TLVBackingStore
47 : {
48 : public:
49 : TlvVectorBuffer(std::vector<uint8_t> & buffer);
50 : TlvVectorBuffer(const TlvVectorBuffer &) = delete;
51 : TlvVectorBuffer & operator=(const TlvVectorBuffer &) = delete;
52 : ~TlvVectorBuffer() override;
53 :
54 : // TLVBackingStore implementation:
55 0 : CHIP_ERROR OnInit(TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen) override
56 : {
57 0 : return CHIP_ERROR_NOT_IMPLEMENTED;
58 : }
59 0 : CHIP_ERROR GetNextBuffer(TLVReader & reader, const uint8_t *& bufStart, uint32_t & bufLen) override
60 : {
61 0 : return CHIP_ERROR_NOT_IMPLEMENTED;
62 : }
63 : CHIP_ERROR OnInit(TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen) override;
64 : CHIP_ERROR GetNewBuffer(TLVWriter & writer, uint8_t *& bufStart, uint32_t & bufLen) override;
65 : CHIP_ERROR FinalizeBuffer(TLVWriter & writer, uint8_t * bufStart, uint32_t bufLen) override;
66 :
67 : private:
68 : void ResizeWriteBuffer(uint8_t *& bufStart, uint32_t & bufLen);
69 :
70 : // mWritingBuffer is the mutable buffer exposed via the TLVBackingStore
71 : // interface. When FinalizeBuffer is called the contents of mWritingBuffer
72 : // are appended to mFinalBuffer and mWritingBuffer is cleared. This allows
73 : // for reading all written data from a single, contiguous buffer
74 : // (mFinalBuffer).
75 : std::vector<uint8_t> mWritingBuffer;
76 : std::vector<uint8_t> & mFinalBuffer;
77 : };
78 :
79 : TlvVectorBuffer mVectorBuffer;
80 : };
81 :
82 : } // namespace TLV
83 : } // namespace chip
|