devices: add device id for Realtek RTL8188CU and RTL8188FTV
[project/iwinfo.git] / iwinfo_utils.c
index c7713edb25715c8d6d5a65425c2d0bdedb2c7cef..d96cbb345e68fb9b9a4d6edcc3067b5a192afd23 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
@@ -208,13 +280,19 @@ struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
 
        while (fgets(buf, sizeof(buf) - 1, db) != NULL)
        {
+               if (buf[0] == '#')
+                       continue;
+
                memset(&e, 0, sizeof(e));
 
                if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
                               &e.vendor_id, &e.device_id,
                               &e.subsystem_vendor_id, &e.subsystem_device_id,
                               &e.txpower_offset, &e.frequency_offset,
-                              e.vendor_name, e.device_name) < 8)
+                              e.vendor_name, e.device_name) != 8 &&
+                       sscanf(buf, "\"%127[^\"]\" %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
+                              e.compatible, &e.txpower_offset, &e.frequency_offset,
+                              e.vendor_name, e.device_name) != 5)
                        continue;
 
                if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
@@ -231,6 +309,9 @@ struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
                        (e.subsystem_device_id != id->subsystem_device_id))
                        continue;
 
+               if (strcmp(e.compatible, id->compatible))
+                       continue;
+
                rv = &e;
                break;
        }