devices: add device id for Realtek RTL8188CU and RTL8188FTV
[project/iwinfo.git] / iwinfo_nl80211.c
index 6b2a87ad6157fd615c72e101284e10e2ccc3bc1c..220024955acce647410a8992f7fa19ae183c6b86 100644 (file)
@@ -367,7 +367,7 @@ static int nl80211_phy_idx_from_macaddr(const char *opt)
        if (!opt)
                return -1;
 
-       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*");   /**/
+       snprintf(buf, sizeof(buf), "/sys/class/ieee80211/*");
        if (glob(buf, 0, NULL, &gl))
                return -1;
 
@@ -807,14 +807,13 @@ nla_put_failure:
 
 static char * nl80211_phy2ifname(const char *ifname)
 {
-       int ifidx = -1, cifidx = -1, phyidx = -1;
+       int ifidx = -1, cifidx, lmode = 1, clmode, phyidx;
        char buffer[512];
        static char nif[IFNAMSIZ] = { 0 };
-
        DIR *d;
        struct dirent *e;
 
-       /* Only accept phy name of the form phy%d or radio%d */
+       /* Only accept phy name in the form of phy%d or radio%d */
        if (!ifname)
                return NULL;
 
@@ -832,18 +831,26 @@ static char * nl80211_phy2ifname(const char *ifname)
                {
                        snprintf(buffer, sizeof(buffer),
                                 "/sys/class/net/%s/phy80211/index", e->d_name);
+                       if (nl80211_readint(buffer) != phyidx)
+                               continue;
 
-                       if (nl80211_readint(buffer) == phyidx)
-                       {
-                               snprintf(buffer, sizeof(buffer),
-                                        "/sys/class/net/%s/ifindex", e->d_name);
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/ifindex", e->d_name);
+                       cifidx = nl80211_readint(buffer);
 
-                               if ((cifidx = nl80211_readint(buffer)) >= 0 &&
-                                   ((ifidx < 0) || (cifidx < ifidx)))
-                               {
-                                       ifidx = cifidx;
-                                       strncpy(nif, e->d_name, sizeof(nif) - 1);
-                               }
+                       if (cifidx < 0)
+                               continue;
+
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/link_mode", e->d_name);
+                       clmode = nl80211_readint(buffer);
+
+                       /* prefer non-supplicant-based devices */
+                       if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1)))
+                       {
+                               ifidx = cifidx;
+                               lmode = clmode;
+                               strncpy(nif, e->d_name, sizeof(nif) - 1);
                        }
                }
 
@@ -863,7 +870,7 @@ static int nl80211_get_mode_cb(struct nl_msg *msg, void *arg)
                IWINFO_OPMODE_CLIENT,           /* managed */
                IWINFO_OPMODE_MASTER,           /* AP */
                IWINFO_OPMODE_AP_VLAN,          /* AP/VLAN */
-               IWINFO_OPMODE_WDS,                      /* WDS */
+               IWINFO_OPMODE_WDS,              /* WDS */
                IWINFO_OPMODE_MONITOR,          /* monitor */
                IWINFO_OPMODE_MESHPOINT,        /* mesh point */
                IWINFO_OPMODE_P2P_CLIENT,       /* P2P-client */
@@ -1242,7 +1249,7 @@ static int nl80211_get_ssid_bssid_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *bss[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+       static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
                [NL80211_BSS_INFORMATION_ELEMENTS] = { 0 },
                [NL80211_BSS_STATUS]               = { .type = NLA_U32 },
        };
@@ -1366,7 +1373,7 @@ static int nl80211_get_frequency_scan_cb(struct nl_msg *msg, void *arg)
        struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *binfo[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+       static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
                [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
                [NL80211_BSS_STATUS]    = { .type = NLA_U32 },
        };
@@ -1543,10 +1550,12 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
 
-       static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
+       static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
                [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
                [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_RX_BYTES64]    = { .type = NLA_U64    },
+               [NL80211_STA_INFO_TX_BYTES64]    = { .type = NLA_U64    },
                [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
@@ -1556,7 +1565,7 @@ static int nl80211_fill_signal_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
        };
 
-       static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
+       static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
                [NL80211_RATE_INFO_BITRATE]      = { .type = NLA_U16  },
                [NL80211_RATE_INFO_MCS]          = { .type = NLA_U8   },
                [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
@@ -1655,7 +1664,7 @@ static int nl80211_get_noise_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
 
-       static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
+       static const struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
                [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
                [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
        };
@@ -1753,7 +1762,7 @@ static int nl80211_check_wepkey(const char *key)
        return 0;
 }
 
-static struct {
+static const struct {
        const char *match;
        int version;
        int suite;
@@ -1810,7 +1819,7 @@ static void parse_wpa_suites(const char *str, int defversion,
        }
 }
 
-static struct {
+static const struct {
        const char *match;
        int cipher;
 } wpa_cipher_strings[] = {
@@ -2092,7 +2101,7 @@ static int nl80211_get_survey_cb(struct nl_msg *msg, void *arg)
        struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
        int rc;
 
-       static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
+       static const struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
                [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
                [NL80211_SURVEY_INFO_NOISE]  = { .type = NLA_U8     },
                [NL80211_SURVEY_INFO_TIME] = { .type = NLA_U64   },
@@ -2197,7 +2206,7 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
        struct nl80211_sta_flag_update *sta_flags;
 
-       static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
+       static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
                [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
                [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
@@ -2207,6 +2216,8 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_SIGNAL_AVG]    = { .type = NLA_U8     },
                [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_RX_BYTES64]    = { .type = NLA_U64    },
+               [NL80211_STA_INFO_TX_BYTES64]    = { .type = NLA_U64    },
                [NL80211_STA_INFO_TX_RETRIES]    = { .type = NLA_U32    },
                [NL80211_STA_INFO_TX_FAILED]     = { .type = NLA_U32    },
                [NL80211_STA_INFO_CONNECTED_TIME]= { .type = NLA_U32    },
@@ -2224,7 +2235,7 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_NONPEER_PM]    = { .type = NLA_U32    },
        };
 
-       static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
+       static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
                [NL80211_RATE_INFO_BITRATE]      = { .type = NLA_U16    },
                [NL80211_RATE_INFO_MCS]          = { .type = NLA_U8     },
                [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG   },
@@ -2270,10 +2281,14 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                                      sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy))
                        nl80211_parse_rateinfo(rinfo, &e->tx_rate);
 
-               if (sinfo[NL80211_STA_INFO_RX_BYTES])
+               if (sinfo[NL80211_STA_INFO_RX_BYTES64])
+                       e->rx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_RX_BYTES64]);
+               else if (sinfo[NL80211_STA_INFO_RX_BYTES])
                        e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
 
-               if (sinfo[NL80211_STA_INFO_TX_BYTES])
+               if (sinfo[NL80211_STA_INFO_TX_BYTES64])
+                       e->tx_bytes = nla_get_u64(sinfo[NL80211_STA_INFO_TX_BYTES64]);
+               else if (sinfo[NL80211_STA_INFO_TX_BYTES])
                        e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
 
                if (sinfo[NL80211_STA_INFO_TX_RETRIES])
@@ -2406,7 +2421,7 @@ static int nl80211_get_txpwrlist_cb(struct nl_msg *msg, void *arg)
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
        struct nlattr *band, *freq;
 
-       static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
+       static const struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
                [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
                [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
                [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
@@ -2559,7 +2574,7 @@ static void nl80211_get_scanlist_ie(struct nlattr **bss,
                                iwinfo_parse_rsn(&e->crypto, ie + 6, ie[1] - 4,
                                                 IWINFO_CIPHER_TKIP, IWINFO_KMGMT_PSK);
                        break;
-               case 61: /* HT oeration */
+               case 61: /* HT operation */
                        if (ie[1] >= 3) {
                                e->ht_chan_info.primary_chan = ie[2];
                                e->ht_chan_info.secondary_chan_off = ie[3] & 0x3;
@@ -2589,7 +2604,7 @@ static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
        struct nlattr **tb = nl80211_parse(msg);
        struct nlattr *bss[NL80211_BSS_MAX + 1];
 
-       static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+       static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
                [NL80211_BSS_TSF]                  = { .type = NLA_U64 },
                [NL80211_BSS_FREQUENCY]            = { .type = NLA_U32 },
                [NL80211_BSS_BSSID]                = { 0 },
@@ -3414,12 +3429,12 @@ static int nl80211_get_ifcomb_cb(struct nl_msg *msg, void *arg)
 
        nla_for_each_nested(comb, attr[NL80211_ATTR_INTERFACE_COMBINATIONS], comb_rem)
        {
-               static struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
+               static const struct nla_policy iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
                        [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
                        [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
                };
                struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB+1];
-               static struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
+               static const struct nla_policy iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
                        [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
                        [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
                };