• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 TLV_BASE_H
17 #define TLV_BASE_H
18 
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef enum {
26     OPERA_SUCC = 0,
27     PARAM_ERR = 1001,
28     MALLOC_FAIL = 1002,
29     MEMCPY_ERR = 1003,
30     MEMSET_ERR = 1004,
31     OPERA_FAIL = 1005,
32     TAG_NOT_EXIST = 1006,
33 } ErrCode;
34 
35 typedef struct {
36     int32_t type;
37     uint32_t length;
38     uint8_t *value;
39 } TlvType;
40 
41 typedef struct {
42     TlvType *value;
43 } TlvObject;
44 
45 typedef struct tagTlvListNode {
46     TlvObject data;
47     struct tagTlvListNode *next;
48 } TlvListNode;
49 
50 
51 typedef union {
52     uint64_t u64;
53     uint32_t u32[2];
54 } SwapUint64;
55 
56 // short convert endian
57 uint16_t Ntohs(uint16_t data);
58 
59 // uint32 convert endian
60 uint32_t Ntohl(uint32_t data);
61 
62 // uint64 convert endian
63 uint64_t Ntohll(uint64_t data);
64 
65 TlvListNode *CreateTlvList(void);
66 int32_t DestroyTlvList(TlvListNode *list);
67 TlvType *CreateTlvType(int32_t type, uint32_t length, const void *value);
68 int32_t AddTlvNode(TlvListNode *list, const TlvObject *object);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #endif // TLV_BASE_H