7 #include <osmocore/msgb.h>
9 /* Terminology / wording
10 tag length value (in bits)
21 #define LV_GROSS_LEN(x) (x+1)
22 #define TLV_GROSS_LEN(x) (x+2)
23 #define TLV16_GROSS_LEN(x) ((2*x)+2)
24 #define TL16V_GROSS_LEN(x) (x+3)
25 #define L16TV_GROSS_LEN(x) (x+3)
27 #define TVLV_MAX_ONEBYTE 0x7f
29 static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
31 if (len <= TVLV_MAX_ONEBYTE)
32 return TLV_GROSS_LEN(len);
34 return TL16V_GROSS_LEN(len);
39 static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
43 memcpy(buf, val, len);
47 static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
52 memcpy(buf, val, len);
56 static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
61 memcpy(buf, val, len*2);
65 static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
71 memcpy(buf, val, len);
75 static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
80 if (len <= TVLV_MAX_ONEBYTE) {
81 ret = tlv_put(buf, tag, len, val);
84 ret = tl16v_put(buf, tag, len, val);
89 static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
91 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
92 return tlv16_put(buf, tag, len, val);
95 static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
98 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
99 return tl16v_put(buf, tag, len, val);
102 static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
105 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
106 return tvlv_put(buf, tag, len, val);
109 static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
112 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
117 memcpy(buf, val, len);
121 static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
127 static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
135 /* 'val' is still in host byte order! */
136 static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
145 static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
147 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
148 return lv_put(buf, len, val);
151 static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
153 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
154 return tlv_put(buf, tag, len, val);
157 static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
159 uint8_t *buf = msgb_put(msg, 2);
160 return tv_put(buf, tag, val);
163 static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
165 uint8_t *buf = msgb_put(msg, 1);
166 return v_put(buf, val);
169 static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
171 uint8_t *buf = msgb_put(msg, 3);
172 return tv16_put(buf, tag, val);
175 static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
177 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
178 return tlv_put(buf, tag, len, val);
181 static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
183 uint8_t *buf = msgb_push(msg, 2);
184 return tv_put(buf, tag, val);
187 static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
189 uint8_t *buf = msgb_push(msg, 3);
190 return tv16_put(buf, tag, val);
193 static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
196 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
197 return tvlv_put(buf, tag, len, val);
223 struct tlv_definition {
224 struct tlv_def def[0xff];
228 struct tlv_p_entry lv[0xff];
231 extern struct tlv_definition tvlv_att_def;
233 int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
234 const struct tlv_definition *def,
235 const uint8_t *buf, int buf_len);
236 int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
237 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
238 /* take a master (src) tlvdev and fill up all empty slots in 'dst' */
239 void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
241 #define TLVP_PRESENT(x, y) ((x)->lv[y].val)
242 #define TLVP_LEN(x, y) (x)->lv[y].len
243 #define TLVP_VAL(x, y) (x)->lv[y].val