summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Heider <a.heider@gmail.com>2022-11-22 09:45:23 +0100
committerJo-Philipp Wich <jo@mein.io>2022-12-16 00:09:07 +0100
commite084781afc99075c4154614b49d99d3308d8c8ee (patch)
tree8a4bf01c5dac19a402ab0069bc5c0a1011663dee
parentd09a77a3695b9e684c74fbe66809449ad92b0e96 (diff)
downloadiwinfo-e084781afc99075c4154614b49d99d3308d8c8ee.tar.gz
utils: add helper functions to get names by values
Some defines/enums use bits, while some functions only set a single one. Make it less painful to get to a name for those. This avoids hardcoding bit lists for consumers. Signed-off-by: Andre Heider <a.heider@gmail.com>
-rw-r--r--include/iwinfo/utils.h3
-rw-r--r--iwinfo_utils.c38
2 files changed, 41 insertions, 0 deletions
diff --git a/include/iwinfo/utils.h b/include/iwinfo/utils.h
index e9b8f1d..a7645de 100644
--- a/include/iwinfo/utils.h
+++ b/include/iwinfo/utils.h
@@ -43,6 +43,9 @@ static inline int iwinfo_mbm2dbm(int gain)
return gain / 100;
}
+const char * const iwinfo_band_name(int mask);
+const char * const iwinfo_htmode_name(int mask);
+
size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len);
int iwinfo_htmode_is_ht(int htmode);
int iwinfo_htmode_is_vht(int htmode);
diff --git a/iwinfo_utils.c b/iwinfo_utils.c
index c7713ed..83f049a 100644
--- a/iwinfo_utils.c
+++ b/iwinfo_utils.c
@@ -77,6 +77,44 @@ int iwinfo_mw2dbm(int in)
return (int)res;
}
+static int iwinfo_bit(int value, int max)
+{
+ int i;
+
+ if (max > 31 || !(value & ((1 << max) - 1)))
+ return -1;
+
+ for (i = 0; i < max; i++)
+ {
+ if (value & 1)
+ break;
+
+ value >>= 1;
+ }
+
+ return i;
+}
+
+static const char * const iwinfo_name(int mask, int max, const char * const names[])
+{
+ int index = iwinfo_bit(mask, max);
+
+ if (index < 0)
+ return NULL;
+
+ return names[index];
+}
+
+const char * const iwinfo_band_name(int mask)
+{
+ return iwinfo_name(mask, IWINFO_BAND_COUNT, IWINFO_BAND_NAMES);
+}
+
+const char * const iwinfo_htmode_name(int mask)
+{
+ return iwinfo_name(mask, IWINFO_HTMODE_COUNT, IWINFO_HTMODE_NAMES);
+}
+
size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len)
{
// bit numbers as per IWINFO_80211_*: ad ac ax a b g n