]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_wireless_info.c
Fix build under FreeBSD after recent changes for Linux.
[i3/i3status] / src / print_wireless_info.c
index cd95562f01d3eac979db2abf79485fc70e0b7cb0..a3ab6701eb89c8d113310a0f81beb5aafa77957b 100644 (file)
@@ -65,7 +65,9 @@
 typedef struct {
     int flags;
     char essid[IW_ESSID_MAX_SIZE + 1];
+#ifdef LINUX
     uint8_t bssid[ETH_ALEN];
+#endif
     int quality;
     int quality_max;
     int quality_average;
@@ -77,6 +79,7 @@ typedef struct {
     double frequency;
 } wireless_info_t;
 
+#ifdef LINUX
 // Like iw_print_bitrate, but without the dependency on libiw.
 static void print_bitrate(char *buffer, int buflen, int bitrate) {
     const int kilo = 1e3;
@@ -100,6 +103,7 @@ static void print_bitrate(char *buffer, int buflen, int bitrate) {
     snprintf(buffer, buflen, "%g %cb/s", rate / divisor, scale);
 }
 
+// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
 static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) {
 #define NOISE_FLOOR_DBM -90
 #define SIGNAL_MAX_DBM -20
@@ -113,9 +117,9 @@ static uint32_t nl80211_xbm_to_percent(int32_t xbm, uint32_t divisor) {
     return 100 - 70 * (((float)SIGNAL_MAX_DBM - (float)xbm) / ((float)SIGNAL_MAX_DBM - (float)NOISE_FLOOR_DBM));
 }
 
-#define WLAN_EID_SSID 0
-
+// Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c
 static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *ssid_len) {
+#define WLAN_EID_SSID 0
     *ssid = NULL;
     *ssid_len = 0;
 
@@ -125,7 +129,7 @@ static void find_ssid(uint8_t *ies, uint32_t ies_len, uint8_t **ssid, uint32_t *
     }
     if (ies_len < 2)
         return;
-    if (ies_len < 2 + ies[1])
+    if (ies_len < (uint32_t)(2 + ies[1]))
         return;
 
     *ssid_len = ies[1];
@@ -144,7 +148,7 @@ static int gwi_sta_cb(struct nl_msg *msg, void *data) {
     };
 
     static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
-            [NL80211_RATE_INFO_BITRATE32] = {.type = NLA_U32},
+            [NL80211_RATE_INFO_BITRATE] = {.type = NLA_U16},
     };
 
     if (nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
@@ -162,19 +166,19 @@ static int gwi_sta_cb(struct nl_msg *msg, void *data) {
     if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, sinfo[NL80211_STA_INFO_RX_BITRATE], rate_policy))
         return NL_SKIP;
 
-    if (rinfo[NL80211_RATE_INFO_BITRATE32] == NULL)
+    if (rinfo[NL80211_RATE_INFO_BITRATE] == NULL)
         return NL_SKIP;
 
-    // NL80211_RATE_INFO_BITRATE32 is specified in units of 100 kbit/s, but iw
+    // NL80211_RATE_INFO_BITRATE is specified in units of 100 kbit/s, but iw
     // used to specify bit/s, so we convert to use the same code path.
-    info->bitrate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]) * 100 * 1000;
+    info->bitrate = (int)nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]) * 100 * 1000;
 
     return NL_SKIP;
 }
 
 static int gwi_scan_cb(struct nl_msg *msg, void *data) {
     wireless_info_t *info = data;
-    struct genlmsghdr *gnlh = genlmsg_hdr(nlmsg_hdr(msg));
+    struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
     struct nlattr *tb[NL80211_ATTR_MAX + 1];
     struct nlattr *bss[NL80211_BSS_MAX + 1];
     struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
@@ -250,6 +254,7 @@ static int gwi_scan_cb(struct nl_msg *msg, void *data) {
 
     return NL_SKIP;
 }
+#endif
 
 static int get_wireless_info(const char *interface, wireless_info_t *info) {
     memset(info, 0, sizeof(wireless_info_t));
@@ -479,7 +484,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
         if (BEGINS_WITH(walk + 1, "quality")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) {
                 if (info.quality_max)
-                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.quality, info.quality_max));
+                    outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.quality, info.quality_max), pct_mark);
                 else
                     outwalk += sprintf(outwalk, "%d", info.quality);
             } else {
@@ -491,7 +496,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
         if (BEGINS_WITH(walk + 1, "signal")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) {
                 if (info.signal_level_max)
-                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.signal_level, info.signal_level_max));
+                    outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark);
                 else
                     outwalk += sprintf(outwalk, "%d dBm", info.signal_level);
             } else {
@@ -503,7 +508,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
         if (BEGINS_WITH(walk + 1, "noise")) {
             if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) {
                 if (info.noise_level_max)
-                    outwalk += sprintf(outwalk, "%03d%%", PERCENT_VALUE(info.noise_level, info.noise_level_max));
+                    outwalk += sprintf(outwalk, "%03d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark);
                 else
                     outwalk += sprintf(outwalk, "%d dBm", info.noise_level);
             } else {