[PATCH] libosmocore[master]: tlv: add TLV_GET() and TLV_GET_MINLEN()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Wed Apr 18 00:38:44 UTC 2018


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/7842

to look at the new patch set (#3).

tlv: add TLV_GET() and TLV_GET_MINLEN()

Rationale: so far we use code like

  if (TLVP_PRESENT(&tp, VERY_LONG_ENUM_VALUE_NAME_THAT_NEVER_ENDS)) {
  	val = TLVP_VAL(&tp, VERY_LONG_ENUM_VALUE_NAME_THAT_NEVER_ENDS);
	len = TLVP_LEN(&tp, VERY_L0NG_ENUM_VALUE_NAME_TH4T_NEVER_EMDS);
  }

This is a) very long and b) prone to picking the wrong name one of the three
times, which would use the wrong length or val without necessarily being
noticed. A safer and shorter, more readable pattern is:

  struct tlv_p_entry *e = TVLP_GET(&tp, VERY_LONG_ENUM_VALUE_NAME_THAT_NEVER_ENDS);
  if (!e)
        return -ENOENT;
  hexdump(e->val, e->len);

Change-Id: I445de17fc2daa3ab051f5708dd0cc185b23dc048
---
M include/osmocom/gsm/tlv.h
1 file changed, 21 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/42/7842/3

diff --git a/include/osmocom/gsm/tlv.h b/include/osmocom/gsm/tlv.h
index 996f6aa..a130dc8 100644
--- a/include/osmocom/gsm/tlv.h
+++ b/include/osmocom/gsm/tlv.h
@@ -446,6 +446,27 @@
 #define TLVP_PRES_LEN(tp, tag, min_len) \
 	(TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
 
+/*! Return pointer to a TLV element if it is present.
+ * Usage:
+ *   struct tlv_p_entry *e = TVLP_GET(&tp, TAG);
+ *   if (!e)
+ *         return -ENOENT;
+ *   hexdump(e->val, e->len);
+ * \param[in] _tp  pointer to \ref tlv_parsed.
+ * \param[in] tag  IE tag to return.
+ * \returns struct tlv_p_entry pointer, or NULL if not present.
+ */
+#define TLVP_GET(_tp, tag)	(TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
+
+/*! Like TLVP_GET(), but enforcing a minimum val length.
+ * \param[in] _tp  pointer to \ref tlv_parsed.
+ * \param[in] tag  IE tag to return.
+ * \param[in] min_len  Minimum value length in bytes.
+ * \returns struct tlv_p_entry pointer, or NULL if not present or too short.
+ */
+#define TLVP_GET_MINLEN(_tp, tag, min_len) \
+	(TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
+
 /*! Align given TLV element with 16 bit value to an even address
  *  \param[in] tp pointer to \ref tlv_parsed
  *  \param[in] pos element to return

-- 
To view, visit https://gerrit.osmocom.org/7842
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I445de17fc2daa3ab051f5708dd0cc185b23dc048
Gerrit-PatchSet: 3
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list