Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #ifndef _TLV_H |
| 2 | #define _TLV_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 7 | #include <osmocom/core/msgb.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 8 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 9 | /*! \defgroup tlv GSM L3 compatible TLV parser |
| 10 | * @{ |
| 11 | */ |
| 12 | /*! \file tlv.h */ |
| 13 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 14 | /* Terminology / wording |
| 15 | tag length value (in bits) |
| 16 | |
| 17 | V - - 8 |
| 18 | LV - 8 N * 8 |
| 19 | TLV 8 8 N * 8 |
| 20 | TL16V 8 16 N * 8 |
| 21 | TLV16 8 8 N * 16 |
| 22 | TvLV 8 8/16 N * 8 |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 23 | vTvLV 8/16 8/16 N * 8 |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 24 | |
| 25 | */ |
| 26 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 27 | /*! \brief gross length of a LV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | #define LV_GROSS_LEN(x) (x+1) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 29 | /*! \brief gross length of a TLV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 30 | #define TLV_GROSS_LEN(x) (x+2) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 31 | /*! \brief gross length of a TLV16 type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 32 | #define TLV16_GROSS_LEN(x) ((2*x)+2) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 33 | /*! \brief gross length of a TL16V type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | #define TL16V_GROSS_LEN(x) (x+3) |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 35 | /*! \brief gross length of a L16TV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 36 | #define L16TV_GROSS_LEN(x) (x+3) |
| 37 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 38 | /*! \brief maximum length of TLV of one byte length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | #define TVLV_MAX_ONEBYTE 0x7f |
| 40 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 41 | /*! \brief gross length of a TVLV type field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | static inline uint16_t TVLV_GROSS_LEN(uint16_t len) |
| 43 | { |
| 44 | if (len <= TVLV_MAX_ONEBYTE) |
| 45 | return TLV_GROSS_LEN(len); |
| 46 | else |
| 47 | return TL16V_GROSS_LEN(len); |
| 48 | } |
| 49 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 50 | /*! \brief gross length of vTvL header (tag+len) */ |
| 51 | static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 52 | { |
| 53 | uint16_t ret = 2; |
| 54 | |
| 55 | if (tag > TVLV_MAX_ONEBYTE) |
| 56 | ret++; |
| 57 | |
| 58 | if (len > TVLV_MAX_ONEBYTE) |
| 59 | ret++; |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | /*! \brief gross length of vTvLV (tag+len+val) */ |
| 65 | static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len) |
| 66 | { |
| 67 | uint16_t ret; |
| 68 | |
| 69 | if (len <= TVLV_MAX_ONEBYTE) |
| 70 | return TLV_GROSS_LEN(len); |
| 71 | else |
| 72 | return TL16V_GROSS_LEN(len); |
| 73 | |
| 74 | if (tag > TVLV_MAX_ONEBYTE) |
| 75 | ret += 1; |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 80 | /* TLV generation */ |
| 81 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 82 | /*! \brief put (append) a LV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 83 | static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, |
| 84 | const uint8_t *val) |
| 85 | { |
| 86 | *buf++ = len; |
| 87 | memcpy(buf, val, len); |
| 88 | return buf + len; |
| 89 | } |
| 90 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 91 | /*! \brief put (append) a TLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 92 | static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 93 | const uint8_t *val) |
| 94 | { |
| 95 | *buf++ = tag; |
| 96 | *buf++ = len; |
| 97 | memcpy(buf, val, len); |
| 98 | return buf + len; |
| 99 | } |
| 100 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 101 | /*! \brief put (append) a TLV16 field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 102 | static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, |
| 103 | const uint16_t *val) |
| 104 | { |
| 105 | *buf++ = tag; |
| 106 | *buf++ = len; |
| 107 | memcpy(buf, val, len*2); |
| 108 | return buf + len*2; |
| 109 | } |
| 110 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 111 | /*! \brief put (append) a TL16V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 112 | static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 113 | const uint8_t *val) |
| 114 | { |
| 115 | *buf++ = tag; |
| 116 | *buf++ = len >> 8; |
| 117 | *buf++ = len & 0xff; |
| 118 | memcpy(buf, val, len); |
| 119 | return buf + len*2; |
| 120 | } |
| 121 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 122 | /*! \brief put (append) a TvLV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 123 | static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, |
| 124 | const uint8_t *val) |
| 125 | { |
| 126 | uint8_t *ret; |
| 127 | |
| 128 | if (len <= TVLV_MAX_ONEBYTE) { |
| 129 | ret = tlv_put(buf, tag, len, val); |
| 130 | buf[1] |= 0x80; |
| 131 | } else |
| 132 | ret = tl16v_put(buf, tag, len, val); |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 137 | /*! \brief put (append) a variable-length tag or variable-length length * */ |
| 138 | static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag) |
| 139 | { |
| 140 | if (tag > TVLV_MAX_ONEBYTE) { |
| 141 | /* two-byte TAG */ |
| 142 | *buf++ = 0x80 | (tag >> 8); |
| 143 | *buf++ = (tag & 0xff); |
| 144 | } else |
| 145 | *buf++ = tag; |
| 146 | |
| 147 | return buf; |
| 148 | } |
| 149 | |
| 150 | /* \brief put (append) vTvL (GAN) field (tag + length)*/ |
| 151 | static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len) |
| 152 | { |
| 153 | uint8_t *ret; |
| 154 | |
| 155 | ret = vt_gan_put(buf, tag); |
| 156 | return vt_gan_put(ret, len); |
| 157 | } |
| 158 | |
| 159 | /* \brief put (append) vTvLV (GAN) field (tag + length + val) */ |
| 160 | static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len, |
| 161 | const uint8_t *val) |
| 162 | { |
| 163 | uint8_t *ret; |
| 164 | |
| 165 | ret = vtvl_gan_put(buf, tag, len ); |
| 166 | |
| 167 | memcpy(ret, val, len); |
| 168 | ret = buf + len; |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 173 | /*! \brief put (append) a TLV16 field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 174 | static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) |
| 175 | { |
| 176 | uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 177 | return tlv16_put(buf, tag, len, val); |
| 178 | } |
| 179 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 180 | /*! \brief put (append) a TL16V field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 181 | static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 182 | const uint8_t *val) |
| 183 | { |
| 184 | uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 185 | return tl16v_put(buf, tag, len, val); |
| 186 | } |
| 187 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 188 | /*! \brief put (append) a TvLV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 189 | static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, |
| 190 | const uint8_t *val) |
| 191 | { |
| 192 | uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len)); |
| 193 | return tvlv_put(buf, tag, len, val); |
| 194 | } |
| 195 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 196 | /*! \brief put (append) a vTvLV field to \ref msgb */ |
| 197 | static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag, |
| 198 | uint16_t len, const uint8_t *val) |
| 199 | { |
| 200 | uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 201 | return vtvlv_gan_put(buf, tag, len, val); |
| 202 | } |
| 203 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 204 | /*! \brief put (append) a L16TV field to \ref msgb */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 205 | static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, |
| 206 | const uint8_t *val) |
| 207 | { |
| 208 | uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len)); |
| 209 | |
| 210 | *buf++ = len >> 8; |
| 211 | *buf++ = len & 0xff; |
| 212 | *buf++ = tag; |
| 213 | memcpy(buf, val, len); |
| 214 | return buf + len; |
| 215 | } |
| 216 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 217 | /*! \brief put (append) a V field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 218 | static inline uint8_t *v_put(uint8_t *buf, uint8_t val) |
| 219 | { |
| 220 | *buf++ = val; |
| 221 | return buf; |
| 222 | } |
| 223 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 224 | /*! \brief put (append) a TV field */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 225 | static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, |
| 226 | uint8_t val) |
| 227 | { |
| 228 | *buf++ = tag; |
| 229 | *buf++ = val; |
| 230 | return buf; |
| 231 | } |
| 232 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 233 | /*! \brief put (append) a TVfixed field */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 234 | static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, |
| 235 | unsigned int len, const uint8_t *val) |
| 236 | { |
| 237 | *buf++ = tag; |
| 238 | memcpy(buf, val, len); |
| 239 | return buf + len; |
| 240 | } |
| 241 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 242 | /*! \brief put (append) a TV16 field |
| 243 | * \param[in,out] buf data buffer |
| 244 | * \param[in] tag Tag value |
| 245 | * \param[in] val Value (in host byte order!) |
| 246 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 247 | static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, |
| 248 | uint16_t val) |
| 249 | { |
| 250 | *buf++ = tag; |
| 251 | *buf++ = val >> 8; |
| 252 | *buf++ = val & 0xff; |
| 253 | return buf; |
| 254 | } |
| 255 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 256 | /*! \brief put (append) a LV field to a \ref msgb |
| 257 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 258 | static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) |
| 259 | { |
| 260 | uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); |
| 261 | return lv_put(buf, len, val); |
| 262 | } |
| 263 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 264 | /*! \brief put (append) a TLV field to a \ref msgb |
| 265 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 266 | static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 267 | { |
| 268 | uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 269 | return tlv_put(buf, tag, len, val); |
| 270 | } |
| 271 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 272 | /*! \brief put (append) a TV field to a \ref msgb |
| 273 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 274 | static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) |
| 275 | { |
| 276 | uint8_t *buf = msgb_put(msg, 2); |
| 277 | return tv_put(buf, tag, val); |
| 278 | } |
| 279 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 280 | /*! \brief put (append) a TVfixed field to a \ref msgb |
| 281 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | 63196de | 2011-03-05 14:32:50 +0100 | [diff] [blame] | 282 | static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, |
| 283 | unsigned int len, const uint8_t *val) |
| 284 | { |
| 285 | uint8_t *buf = msgb_put(msg, 1+len); |
| 286 | return tv_fixed_put(buf, tag, len, val); |
| 287 | } |
| 288 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 289 | /*! \brief put (append) a V field to a \ref msgb |
| 290 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 291 | static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) |
| 292 | { |
| 293 | uint8_t *buf = msgb_put(msg, 1); |
| 294 | return v_put(buf, val); |
| 295 | } |
| 296 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 297 | /*! \brief put (append) a TV16 field to a \ref msgb |
| 298 | * \returns pointer to first byte after newly-put information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 299 | static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) |
| 300 | { |
| 301 | uint8_t *buf = msgb_put(msg, 3); |
| 302 | return tv16_put(buf, tag, val); |
| 303 | } |
| 304 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 305 | /*! \brief push (prepend) a TLV field to a \ref msgb |
| 306 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 307 | static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) |
| 308 | { |
| 309 | uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 310 | tlv_put(buf, tag, len, val); |
| 311 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 314 | /*! \brief push (prepend) a TV field to a \ref msgb |
| 315 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 316 | static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) |
| 317 | { |
| 318 | uint8_t *buf = msgb_push(msg, 2); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 319 | tv_put(buf, tag, val); |
| 320 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 323 | /*! \brief push (prepend) a TV16 field to a \ref msgb |
| 324 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 325 | static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) |
| 326 | { |
| 327 | uint8_t *buf = msgb_push(msg, 3); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 328 | tv16_put(buf, tag, val); |
| 329 | return buf; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 332 | /*! \brief push (prepend) a TvLV field to a \ref msgb |
| 333 | * \returns pointer to first byte of newly-pushed information */ |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 334 | static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, |
| 335 | const uint8_t *val) |
| 336 | { |
| 337 | uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len)); |
Harald Welte | 2c02043 | 2012-01-22 23:03:38 +0100 | [diff] [blame] | 338 | tvlv_put(buf, tag, len, val); |
| 339 | return buf; |
Harald Welte | 3415d41 | 2010-02-21 19:03:41 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 342 | /* \brief push (prepend) a vTvL header to a \ref msgb |
| 343 | */ |
| 344 | static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag, |
| 345 | uint16_t len) |
| 346 | { |
| 347 | uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len)); |
| 348 | vtvl_gan_put(buf, tag, len); |
| 349 | return buf; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag, |
| 354 | uint16_t len, const uint8_t *val) |
| 355 | { |
| 356 | uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len)); |
| 357 | vtvlv_gan_put(buf, tag, len, val); |
| 358 | return buf; |
| 359 | } |
| 360 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 361 | /* TLV parsing */ |
| 362 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 363 | /*! \brief Entry in a TLV parser array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 364 | struct tlv_p_entry { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 365 | uint16_t len; /*!< \brief length */ |
| 366 | const uint8_t *val; /*!< \brief pointer to value */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 367 | }; |
| 368 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 369 | /*! \brief TLV type */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 370 | enum tlv_type { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 371 | TLV_TYPE_NONE, /*!< \brief no type */ |
| 372 | TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */ |
| 373 | TLV_TYPE_T, /*!< \brief tag-only */ |
| 374 | TLV_TYPE_TV, /*!< \brief tag-value (8bit) */ |
| 375 | TLV_TYPE_TLV, /*!< \brief tag-length-value */ |
| 376 | TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */ |
| 377 | TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */ |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 378 | TLV_TYPE_SINGLE_TV, /*!< \brief tag and value (both 4 bit) in 1 byte */ |
| 379 | TLV_TYPE_vTvLV_GAN, /*!< \brief variable-length tag, variable-length length */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 380 | }; |
| 381 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 382 | /*! \brief Definition of a single IE (Information Element) */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 383 | struct tlv_def { |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 384 | enum tlv_type type; /*!< \brief TLV type */ |
| 385 | uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 386 | }; |
| 387 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 388 | /*! \brief Definition of All 256 IE / TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 389 | struct tlv_definition { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 390 | struct tlv_def def[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 391 | }; |
| 392 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 393 | /*! \brief result of the TLV parser */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 394 | struct tlv_parsed { |
Harald Welte | e0aa5bb | 2011-07-16 15:42:46 +0200 | [diff] [blame] | 395 | struct tlv_p_entry lv[256]; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | extern struct tlv_definition tvlv_att_def; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 399 | extern struct tlv_definition vtvlv_gan_att_def; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 400 | |
| 401 | int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, |
| 402 | const struct tlv_definition *def, |
| 403 | const uint8_t *buf, int buf_len); |
| 404 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 405 | const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2); |
| 406 | /* take a master (src) tlvdev and fill up all empty slots in 'dst' */ |
| 407 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src); |
| 408 | |
| 409 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 410 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 411 | #define TLVP_VAL(x, y) (x)->lv[y].val |
| 412 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 413 | /*! @} */ |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 414 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 415 | #endif /* _TLV_H */ |