• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __H_TLV_H__
17 #define __H_TLV_H__
18 
19 #include "common.h"
20 
21 namespace Hdc {
22 #define TLV_HEAD_SIZE (sizeof(uint32_t) + sizeof(uint32_t))
23 #define TLV_VALUE_MAX_LEN (16 * 1024 * 1024)
24 
25 class TlvBuf {
26 public:
27     TlvBuf();
28     // construct a empty TlvBuf object with valid tags
29     explicit  TlvBuf(std::set<uint32_t> validtags);
30     TlvBuf(const uint8_t *tlvs, const uint32_t size);
31     // construct a TlvBuf object from a TLV buffer with valid tags
32     TlvBuf(const uint8_t *tlvs, const uint32_t size, const std::set<uint32_t> validtags);
33     ~TlvBuf();
34 public:
35     void Clear(void);
36     bool Append(const uint32_t tag, const string &val);
37     bool Append(const uint32_t tag, const uint32_t len, const uint8_t *val);
38     // calculate total size for net tlv size, not contain the size of field 'TLV::uint8_t *val'
39     uint32_t GetBufSize(void) const;
40     bool CopyToBuf(uint8_t *dst, const uint32_t size) const;
41     // // the caller must free the memory pointed by the return value if not null
42     bool FindTlv(const uint32_t tag, uint32_t &len, uint8_t *&val) const;
43     // // the caller must free the memory pointed by the return value if not null
44     bool FindTlv(const uint32_t tag, string &val) const;
45     // // if return true, invalid_tags is empty, else the invalid tags will bring out by invalid_tags
46     bool ContainInvalidTag(void) const;
47     void Display(void) const;
48 private:
49     // key is the tag
50     std::map<uint32_t, std::vector<uint8_t>> mTlvMap;
51     std::set<uint32_t> mValidTags;
52 };
53 
54 }
55 
56 #endif
57