diff --git a/include/osmocom/gsm/tlv.h b/include/osmocom/gsm/tlv.h index f659411fc..7b41d2d73 100644 --- a/include/osmocom/gsm/tlv.h +++ b/include/osmocom/gsm/tlv.h @@ -6,6 +6,11 @@ #include +/*! \defgroup tlv GSM L3 compatible TLV parser + * @{ + */ +/*! \file tlv.h */ + /* Terminology / wording tag length value (in bits) @@ -18,14 +23,21 @@ */ +/*! \brief gross length of a LV type field */ #define LV_GROSS_LEN(x) (x+1) +/*! \brief gross length of a TLV type field */ #define TLV_GROSS_LEN(x) (x+2) +/*! \brief gross length of a TLV16 type field */ #define TLV16_GROSS_LEN(x) ((2*x)+2) +/*! \brief gross length of a TL16V type field */ #define TL16V_GROSS_LEN(x) (x+3) +/*! \brief gross length of a L16TV type field */ #define L16TV_GROSS_LEN(x) (x+3) +/*! \brief maximum length of TLV of one byte length */ #define TVLV_MAX_ONEBYTE 0x7f +/*! \brief gross length of a TVLV type field */ static inline uint16_t TVLV_GROSS_LEN(uint16_t len) { if (len <= TVLV_MAX_ONEBYTE) @@ -36,6 +48,7 @@ static inline uint16_t TVLV_GROSS_LEN(uint16_t len) /* TLV generation */ +/*! \brief put (append) a LV field */ static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, const uint8_t *val) { @@ -44,6 +57,7 @@ static inline uint8_t *lv_put(uint8_t *buf, uint8_t len, return buf + len; } +/*! \brief put (append) a TLV field */ static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, const uint8_t *val) { @@ -53,6 +67,7 @@ static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len, return buf + len; } +/*! \brief put (append) a TLV16 field */ static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, const uint16_t *val) { @@ -62,6 +77,7 @@ static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len, return buf + len*2; } +/*! \brief put (append) a TL16V field */ static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, const uint8_t *val) { @@ -72,6 +88,7 @@ static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len, return buf + len*2; } +/*! \brief put (append) a TvLV field */ static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, const uint8_t *val) { @@ -86,12 +103,14 @@ static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len, return ret; } +/*! \brief put (append) a TLV16 field to \ref msgb */ static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val) { uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); return tlv16_put(buf, tag, len, val); } +/*! \brief put (append) a TL16V field to \ref msgb */ static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val) { @@ -99,6 +118,7 @@ static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t le return tl16v_put(buf, tag, len, val); } +/*! \brief put (append) a TvLV field to \ref msgb */ static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val) { @@ -106,6 +126,7 @@ static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len return tvlv_put(buf, tag, len, val); } +/*! \brief put (append) a L16TV field to \ref msgb */ static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag, const uint8_t *val) { @@ -118,12 +139,14 @@ static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t ta return buf + len; } +/*! \brief put (append) a V field */ static inline uint8_t *v_put(uint8_t *buf, uint8_t val) { *buf++ = val; return buf; } +/*! \brief put (append) a TV field */ static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, uint8_t val) { @@ -132,6 +155,7 @@ static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag, return buf; } +/*! \brief put (append) a TVfixed field */ static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, unsigned int len, const uint8_t *val) { @@ -140,7 +164,11 @@ static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag, return buf + len; } -/* 'val' is still in host byte order! */ +/*! \brief put (append) a TV16 field + * \param[in,out] buf data buffer + * \param[in] tag Tag value + * \param[in] val Value (in host byte order!) + */ static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, uint16_t val) { @@ -150,24 +178,28 @@ static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag, return buf; } +/*! \brief put (append) a LV field to a \ref msgb */ static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val) { uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len)); return lv_put(buf, len, val); } +/*! \brief put (append) a TLV field to a \ref msgb */ static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) { uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); return tlv_put(buf, tag, len, val); } +/*! \brief put (append) a TV field to a \ref msgb */ static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val) { uint8_t *buf = msgb_put(msg, 2); return tv_put(buf, tag, val); } +/*! \brief put (append) a TVfixed field to a \ref msgb */ static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, unsigned int len, const uint8_t *val) { @@ -175,36 +207,42 @@ static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag, return tv_fixed_put(buf, tag, len, val); } +/*! \brief put (append) a V field to a \ref msgb */ static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val) { uint8_t *buf = msgb_put(msg, 1); return v_put(buf, val); } +/*! \brief put (append) a TV16 field to a \ref msgb */ static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val) { uint8_t *buf = msgb_put(msg, 3); return tv16_put(buf, tag, val); } +/*! \brief push (prepend) a TLV field to a \ref msgb */ static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val) { uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); return tlv_put(buf, tag, len, val); } +/*! \brief push (prepend) a TV field to a \ref msgb */ static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val) { uint8_t *buf = msgb_push(msg, 2); return tv_put(buf, tag, val); } +/*! \brief push (prepend) a TV16 field to a \ref msgb */ static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val) { uint8_t *buf = msgb_push(msg, 3); return tv16_put(buf, tag, val); } +/*! \brief push (prepend) a TvLV field to a \ref msgb */ static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len, const uint8_t *val) { @@ -214,31 +252,36 @@ static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t le /* TLV parsing */ +/*! \brief Entry in a TLV parser array */ struct tlv_p_entry { - uint16_t len; - const uint8_t *val; + uint16_t len; /*!< \brief length */ + const uint8_t *val; /*!< \brief pointer to value */ }; +/*! \brief TLV type */ enum tlv_type { - TLV_TYPE_NONE, - TLV_TYPE_FIXED, - TLV_TYPE_T, - TLV_TYPE_TV, - TLV_TYPE_TLV, - TLV_TYPE_TL16V, - TLV_TYPE_TvLV, - TLV_TYPE_SINGLE_TV + TLV_TYPE_NONE, /*!< \brief no type */ + TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */ + TLV_TYPE_T, /*!< \brief tag-only */ + TLV_TYPE_TV, /*!< \brief tag-value (8bit) */ + TLV_TYPE_TLV, /*!< \brief tag-length-value */ + TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */ + TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */ + TLV_TYPE_SINGLE_TV /*!< \brief tag and value (both 4 bit) in 1 byte */ }; +/*! \brief Definition of a single IE (Information Element) */ struct tlv_def { - enum tlv_type type; - uint8_t fixed_len; + enum tlv_type type; /*!< \brief TLV type */ + uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */ }; +/*! \brief Definition of All 256 IE / TLV */ struct tlv_definition { struct tlv_def def[256]; }; +/*! \brief result of the TLV parser */ struct tlv_parsed { struct tlv_p_entry lv[256]; }; @@ -257,4 +300,6 @@ void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src) #define TLVP_LEN(x, y) (x)->lv[y].len #define TLVP_VAL(x, y) (x)->lv[y].val +/*! }@ */ + #endif /* _TLV_H */ diff --git a/src/gsm/tlv_parser.c b/src/gsm/tlv_parser.c index 1e4c6b5f6..c832d56f6 100644 --- a/src/gsm/tlv_parser.c +++ b/src/gsm/tlv_parser.c @@ -3,8 +3,14 @@ #include #include +/*! \addtogroup tlv + * @{ + */ +/*! \file tlv.c */ + struct tlv_definition tvlv_att_def; +/*! \brief Dump pasred TLV structure to stdout */ int tlv_dump(struct tlv_parsed *dec) { int i; @@ -17,14 +23,14 @@ int tlv_dump(struct tlv_parsed *dec) return 0; } -/* o_tag: output: tag found - * o_len: output: length of the data - * o_val: output: pointer to the data - * def: input: a structure defining the valid TLV tags / configurations - * buf: input: the input data buffer to be parsed - * buf_len: input: the length of the input data buffer - * - * Also, returns the number of bytes consumed by the TLV entry +/*! \brief Parse a single TLV encoded IE + * \param[out] o_tag the tag of the IE that was found + * \param[out] o_len length of the IE that was found + * \param[out] o_val pointer to the data of the IE that was found + * \param[in] def structure defining the valid TLV tags / configurations + * \param[in] buf the input data buffer to be parsed + * \param[in] buf_len length of the input data buffer + * \returns number of bytes consumed by the TLV entry / IE parsed */ int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, const struct tlv_definition *def, @@ -101,12 +107,14 @@ int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val, return len; } -/* dec: output: a caller-allocated pointer to a struct tlv_parsed, - * def: input: a structure defining the valid TLV tags / configurations - * buf: input: the input data buffer to be parsed - * buf_len: input: the length of the input data buffer - * lv_tag: input: an initial LV tag at the start of the buffer - * lv_tag2: input: a second initial LV tag following lv_tag +/*! \brief Parse an entire buffer of TLV encoded Information Eleemnts + * \param[out] dec caller-allocated pointer to \ref tlv_parsed + * \param[in] def structure defining the valid TLV tags / configurations + * \param[in] buf the input data buffer to be parsed + * \param[in] buf_len length of the input data buffer + * \param[in] lv_tag an initial LV tag at the start of the buffer + * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag + * \returns number of bytes consumed by the TLV entry / IE parsed */ int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, const uint8_t *buf, int buf_len, uint8_t lv_tag, @@ -158,7 +166,7 @@ int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, return num_parsed; } -/* take a master (src) tlvdev and fill up all empty slots in 'dst' */ +/*! \brief take a master (src) tlvdev and fill up all empty slots in 'dst' */ void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src) { int i; @@ -177,3 +185,5 @@ static __attribute__((constructor)) void on_dso_load_tlv(void) for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++) tvlv_att_def.def[i].type = TLV_TYPE_TvLV; } + +/*! }@ */