]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - link.c
iw: event: handle interface add/del
[thirdparty/iw.git] / link.c
diff --git a/link.c b/link.c
index 11ee0aa14be474b32cd178350eaf62c96a1b1a90..1ed7f631a121a0e41b0b4ba9312c2d23cb136365 100644 (file)
--- a/link.c
+++ b/link.c
@@ -1,7 +1,6 @@
 #include <net/if.h>
 #include <errno.h>
 #include <string.h>
-#include <ctype.h>
 #include <stdbool.h>
 
 #include <netlink/genl/genl.h>
@@ -98,14 +97,14 @@ static int link_bss_handler(struct nl_msg *msg, void *arg)
 }
 
 static int handle_scan_for_link(struct nl80211_state *state,
-                               struct nl_cb *cb,
                                struct nl_msg *msg,
-                               int argc, char **argv)
+                               int argc, char **argv,
+                               enum id_input id)
 {
        if (argc > 0)
                return 1;
 
-       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, link_bss_handler, &lr);
+       register_handler(link_bss_handler, &lr);
        return 0;
 }
 
@@ -114,7 +113,6 @@ static int print_link_sta(struct nl_msg *msg, void *arg)
        struct nlattr *tb[NL80211_ATTR_MAX + 1];
        struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
-       struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
        struct nlattr *binfo[NL80211_STA_BSS_PARAM_MAX + 1];
        static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
                [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
@@ -123,18 +121,12 @@ static int print_link_sta(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
                [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
                [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
+               [NL80211_STA_INFO_RX_BITRATE] = { .type = NLA_NESTED },
                [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
                [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
                [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
                [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
        };
-
-       static 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 },
-               [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
-       };
        static struct nla_policy bss_policy[NL80211_STA_BSS_PARAM_MAX + 1] = {
                [NL80211_STA_BSS_PARAM_CTS_PROT] = { .type = NLA_FLAG },
                [NL80211_STA_BSS_PARAM_SHORT_PREAMBLE] = { .type = NLA_FLAG },
@@ -169,25 +161,17 @@ static int print_link_sta(struct nl_msg *msg, void *arg)
                printf("\tsignal: %d dBm\n",
                        (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
 
+       if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
+               char buf[100];
+
+               parse_bitrate(sinfo[NL80211_STA_INFO_RX_BITRATE], buf, sizeof(buf));
+               printf("\trx bitrate: %s\n", buf);
+       }
        if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
-               if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
-                                    sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
-                       fprintf(stderr, "failed to parse nested rate attributes!\n");
-               } else {
-                       printf("\ttx bitrate: ");
-                       if (rinfo[NL80211_RATE_INFO_BITRATE]) {
-                               int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
-                               printf("%d.%d MBit/s", rate / 10, rate % 10);
-                       }
+               char buf[100];
 
-                       if (rinfo[NL80211_RATE_INFO_MCS])
-                               printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
-                       if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
-                               printf(" 40Mhz");
-                       if (rinfo[NL80211_RATE_INFO_SHORT_GI])
-                               printf(" short GI");
-                       printf("\n");
-               }
+               parse_bitrate(sinfo[NL80211_STA_INFO_TX_BITRATE], buf, sizeof(buf));
+               printf("\ttx bitrate: %s\n", buf);
        }
 
        if (sinfo[NL80211_STA_INFO_BSS_PARAM]) {
@@ -220,9 +204,9 @@ static int print_link_sta(struct nl_msg *msg, void *arg)
 }
 
 static int handle_link_sta(struct nl80211_state *state,
-                          struct nl_cb *cb,
                           struct nl_msg *msg,
-                          int argc, char **argv)
+                          int argc, char **argv,
+                          enum id_input id)
 {
        unsigned char mac_addr[ETH_ALEN];
 
@@ -242,15 +226,16 @@ static int handle_link_sta(struct nl80211_state *state,
 
        NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
 
-       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_link_sta, NULL);
+       register_handler(print_link_sta, NULL);
 
        return 0;
  nla_put_failure:
        return -ENOBUFS;
 }
 
-static int handle_link(struct nl80211_state *state, struct nl_cb *cb,
-                      struct nl_msg *msg, int argc, char **argv)
+static int handle_link(struct nl80211_state *state,
+                      struct nl_msg *msg, int argc, char **argv,
+                      enum id_input id)
 {
        char *link_argv[] = {
                NULL,
@@ -269,7 +254,7 @@ static int handle_link(struct nl80211_state *state, struct nl_cb *cb,
        int err;
 
        link_argv[0] = argv[0];
-       err = handle_cmd(state, II_NETDEV, 3, link_argv);
+       err = handle_cmd(state, id, 3, link_argv);
        if (err)
                return err;
 
@@ -284,7 +269,7 @@ static int handle_link(struct nl80211_state *state, struct nl_cb *cb,
 
        station_argv[0] = argv[0];
        station_argv[3] = bssid_buf;
-       return handle_cmd(state, II_NETDEV, 4, station_argv);
+       return handle_cmd(state, id, 4, station_argv);
 }
 TOPLEVEL(link, NULL, 0, 0, CIB_NETDEV, handle_link,
         "Print information about the current link, if any.");