7 #include <osmocom/core/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 static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
136 unsigned int len, const uint8_t *val)
139 memcpy(buf, val, len);
143 /* 'val' is still in host byte order! */
144 static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
153 static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
155 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
156 return lv_put(buf, len, val);
159 static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
161 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
162 return tlv_put(buf, tag, len, val);
165 static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
167 uint8_t *buf = msgb_put(msg, 2);
168 return tv_put(buf, tag, val);
171 static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
172 unsigned int len, const uint8_t *val)
174 uint8_t *buf = msgb_put(msg, 1+len);
175 return tv_fixed_put(buf, tag, len, val);
178 static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
180 uint8_t *buf = msgb_put(msg, 1);
181 return v_put(buf, val);
184 static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
186 uint8_t *buf = msgb_put(msg, 3);
187 return tv16_put(buf, tag, val);
190 static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
192 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
193 return tlv_put(buf, tag, len, val);
196 static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
198 uint8_t *buf = msgb_push(msg, 2);
199 return tv_put(buf, tag, val);
202 static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
204 uint8_t *buf = msgb_push(msg, 3);
205 return tv16_put(buf, tag, val);
208 static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
211 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
212 return tvlv_put(buf, tag, len, val);
238 struct tlv_definition {
239 struct tlv_def def[256];
243 struct tlv_p_entry lv[256];
246 extern struct tlv_definition tvlv_att_def;
248 int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
249 const struct tlv_definition *def,
250 const uint8_t *buf, int buf_len);
251 int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
252 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
253 /* take a master (src) tlvdev and fill up all empty slots in 'dst' */
254 void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
256 #define TLVP_PRESENT(x, y) ((x)->lv[y].val)
257 #define TLVP_LEN(x, y) (x)->lv[y].len
258 #define TLVP_VAL(x, y) (x)->lv[y].val