cli: fix marking the active channel
[project/iwinfo.git] / iwinfo_utils.c
index 63ae78c4715d875613da417c928347044c37d237..a342b6a19fa526d94aa90f9e24b604ce703878c1 100644 (file)
@@ -77,6 +77,78 @@ 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);
+}
+
+uint32_t iwinfo_band2ghz(uint8_t band)
+{
+       switch (band)
+       {
+       case IWINFO_BAND_24:
+               return 2;
+       case IWINFO_BAND_5:
+               return 5;
+       case IWINFO_BAND_6:
+               return 6;
+       case IWINFO_BAND_60:
+               return 60;
+       }
+
+       return 0;
+}
+
+uint8_t iwinfo_ghz2band(uint32_t ghz)
+{
+       switch (ghz)
+       {
+       case 2:
+               return IWINFO_BAND_24;
+       case 5:
+               return IWINFO_BAND_5;
+       case 6:
+               return IWINFO_BAND_6;
+       case 60:
+               return IWINFO_BAND_60;
+       }
+
+       return 0;
+}
+
 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
@@ -102,6 +174,48 @@ size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len)
        return res;
 }
 
+int iwinfo_htmode_is_ht(int htmode)
+{
+       switch (htmode)
+       {
+       case IWINFO_HTMODE_HT20:
+       case IWINFO_HTMODE_HT40:
+               return 1;
+       }
+
+       return 0;
+}
+
+int iwinfo_htmode_is_vht(int htmode)
+{
+       switch (htmode)
+       {
+       case IWINFO_HTMODE_VHT20:
+       case IWINFO_HTMODE_VHT40:
+       case IWINFO_HTMODE_VHT80:
+       case IWINFO_HTMODE_VHT80_80:
+       case IWINFO_HTMODE_VHT160:
+               return 1;
+       }
+
+       return 0;
+}
+
+int iwinfo_htmode_is_he(int htmode)
+{
+       switch (htmode)
+       {
+       case IWINFO_HTMODE_HE20:
+       case IWINFO_HTMODE_HE40:
+       case IWINFO_HTMODE_HE80:
+       case IWINFO_HTMODE_HE80_80:
+       case IWINFO_HTMODE_HE160:
+               return 1;
+       }
+
+       return 0;
+}
+
 int iwinfo_ifup(const char *ifname)
 {
        struct ifreq ifr;