1 /* 2 * Copyright (c) 2025 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 SOFTBUS_TLV_UTILS_H 17 #define SOFTBUS_TLV_UTILS_H 18 19 #include <stdint.h> 20 #include "common_list.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif // __cplusplus 25 26 #define MAX_VALUE_LENGTH 512 27 #define MAX_TLV_BINARY_LENGTH 4096 28 29 enum BasicSize { 30 UINT8_T = 1, 31 UINT16_T = 2, 32 UINT32_T = 4, 33 }; 34 35 typedef struct { 36 uint8_t tSize; // the number of bytes occupied by the type filed 37 uint8_t lSize; // the number of bytes occupied by the length filed 38 ListNode mList; // tlv member list 39 uint32_t size; // the size of total tlv buffer 40 } TlvObject; 41 42 TlvObject *CreateTlvObject(uint8_t tSize, uint8_t lSize); 43 void DestroyTlvObject(TlvObject *obj); 44 45 int32_t AddTlvMember(TlvObject *obj, uint32_t type, uint32_t length, const uint8_t *value); 46 // note: the memory of value no need to be released, it will be released while DestroyTlvObject() called. 47 int32_t GetTlvMember(TlvObject *obj, uint32_t type, uint32_t *length, uint8_t **value); 48 49 // note: the memory of output need be released by calling SoftBusFree(). 50 int32_t GetTlvBinary(TlvObject *obj, uint8_t **output, uint32_t *outputSize); 51 int32_t SetTlvBinary(TlvObject *obj, const uint8_t *input, uint32_t inputSize); 52 53 // note: buffer is [IN] param. 54 int32_t GetTlvMemberWithSpecifiedBuffer(TlvObject *obj, uint32_t type, uint8_t *buffer, uint32_t size); 55 // note: buffer is [IN] param, size is [IN/OUT] param. 56 int32_t GetTlvMemberWithEstimatedBuffer(TlvObject *obj, uint32_t type, uint8_t *buffer, uint32_t *size); 57 58 // note: while calling the following API, no need to handle 'Network Byte Order'. 59 int32_t AddTlvMemberU8(TlvObject *obj, uint32_t type, uint8_t value); 60 int32_t GetTlvMemberU8(TlvObject *obj, uint32_t type, uint8_t *value); 61 62 int32_t AddTlvMemberU16(TlvObject *obj, uint32_t type, uint16_t value); 63 int32_t GetTlvMemberU16(TlvObject *obj, uint32_t type, uint16_t *value); 64 65 int32_t AddTlvMemberU32(TlvObject *obj, uint32_t type, uint32_t value); 66 int32_t GetTlvMemberU32(TlvObject *obj, uint32_t type, uint32_t *value); 67 68 int32_t AddTlvMemberU64(TlvObject *obj, uint32_t type, uint64_t value); 69 int32_t GetTlvMemberU64(TlvObject *obj, uint32_t type, uint64_t *value); 70 71 // traverse each tlv member 72 typedef struct { 73 ListNode node; 74 uint32_t type; 75 uint32_t length; 76 uint8_t value[0]; 77 } TlvMember; 78 79 #define TLV_FOR_EACH_ENTRY(tlv, tlvObj, type, member) \ 80 LIST_FOR_EACH_ENTRY(tlv, &((tlvObj)->mList), type, member) 81 82 #ifdef __cplusplus 83 } 84 #endif // __cplusplus 85 #endif // SOFTBUS_TLV_UTILS_H 86