tlv_msg.h (3995B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: tlv_msg.h 8 * Purpose: TLV Message Utility 9 */ 10 11 #ifndef _TLV_MSG_H_ 12 #define _TLV_MSG_H_ 13 14 #include <sal/types.h> 15 #include <bcm/types.h> 16 17 18 /* 19 * TLV MESSAGE 20 * 21 * +--------------+-----------------------------------------+--------------+ 22 * | HEADER | TLV(s) | TLV_TYPE_END | 23 * +--------------+-----------------------------------------+--------------+ 24 * 25 * 26 * TLV 27 * 0 7 8 23 24 28 * +------+--------------+-----------------------------------+ 29 * | TYPE | LENGTH | VALUE | 30 * +------+--------------+-----------------------------------+ 31 * |<------------- LENGTH------------->| 32 * 33 * 34 * TLV Message 35 * - The TLV message header is optional and is defined by the application. 36 * - A TLV message contains 0 or more TLV elements. 37 * - A TLV message is terminated by a special TLV of type TLV_TYPE_END, 38 * and there is no length field. 39 * 40 * TLV Fields 41 * Type - indicates type of information, defined as "tlv_msg_type_t". 42 * Type 0 is reserved. 43 * Length - is the size, in bytes, of the data to follow for this element, 44 * defined as "tlv_msg_length_t". 45 * Value - is the variable sized set of bytes which contains 46 * data for this element. 47 * 48 * NOTES: 49 * - Application must know the header format and 'get' all values 50 * expected in the header before retrieving the TLVs. 51 * - Application does not need to 'get' all values in the TLV in order 52 * to move to the next TLV. 53 */ 54 55 typedef uint8 tlv_msg_type_t; 56 typedef uint16 tlv_msg_length_t; 57 58 /* TLV type to indicates end of message */ 59 #define TLV_TYPE_END 0 60 61 typedef struct tlv_msg_s { 62 uint8 *start; /* Start of message in buffer */ 63 uint8 *end; /* End of message in buffer */ 64 uint8 *buffer_end; /* End of buffer */ 65 uint8 *cur_ptr; /* Current ptr in buffer to write/read */ 66 uint8 *tlv_length_ptr; /* Ptr to current TLV length in buffer */ 67 tlv_msg_length_t tlv_length; /* Length for current TLV */ 68 int tlv_left; /* Bytes left to read in current TLV */ 69 } tlv_msg_t; 70 71 72 /* TLV message struct initializer */ 73 extern void tlv_msg_t_init(tlv_msg_t *msg); 74 75 /* TLV message buffer */ 76 extern int tlv_msg_buffer_set(tlv_msg_t *msg, uint8 *buffer, int length); 77 78 /* TLVs */ 79 extern int tlv_msg_add(tlv_msg_t *msg, tlv_msg_type_t type); 80 extern int tlv_msg_get(tlv_msg_t *msg, tlv_msg_type_t *type, 81 tlv_msg_length_t *length); 82 83 /* 84 * Value adders 85 * 86 * Adds values into a TLV message. If there are multiple values associated 87 * with a TLV message header or a TLV type, the routine of the correct 88 * type must be called for each one. 89 */ 90 extern int tlv_msg_uint8_add(tlv_msg_t *msg, uint8 value); 91 extern int tlv_msg_uint16_add(tlv_msg_t *msg, uint16 value); 92 extern int tlv_msg_uint32_add(tlv_msg_t *msg, uint32 value); 93 extern int tlv_msg_string_add(tlv_msg_t *msg, const char *value); 94 95 /* 96 * Value getters 97 * 98 * Gets values from a TLV message. If there are multiple values 99 * associated with a TLV message header or a TLV type, the 100 * getter must be called in the same order and use the same type 101 * as the corresponding adder. 102 */ 103 extern int tlv_msg_uint8_get(tlv_msg_t *msg, uint8 *value); 104 extern int tlv_msg_uint16_get(tlv_msg_t *msg, uint16 *value); 105 extern int tlv_msg_uint32_get(tlv_msg_t *msg, uint32 *value); 106 extern int tlv_msg_string_get(tlv_msg_t *msg, int value_max, char *value); 107 108 extern int tlv_msg_length(tlv_msg_t *msg, int *length); 109 110 extern int tlv_msg_resize(tlv_msg_t *msg, uint8 *buffer, int length); 111 112 113 #endif /* _TLV_MSG_H_ */