iwinfo: constify string map arg for rpc_iwinfo_call_int()
[project/rpcd.git] / iwinfo.c
index 1849196bad38bd3853618ce755eeb8c8dcf8741a..a4d8ec666a8a0e7a3a4652a84b301babceaaf88c 100644 (file)
--- a/iwinfo.c
+++ b/iwinfo.c
@@ -96,7 +96,7 @@ rpc_iwinfo_close(void)
 
 static void
 rpc_iwinfo_call_int(const char *name, int (*func)(const char *, int *),
-                    const char **map)
+                    const char * const *map)
 {
        int rv;
 
@@ -131,7 +131,7 @@ rpc_iwinfo_call_hardware_id(const char *name)
 static void
 rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
 {
-       int ciph;
+       int ciph, wpa_version;
        void *c, *d;
 
        c = blobmsg_open_table(&buf, name);
@@ -156,15 +156,9 @@ rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
                {
                        d = blobmsg_open_array(&buf, "wpa");
 
-                       if (e->wpa_version > 2)
-                       {
-                               blobmsg_add_u32(&buf, NULL, 1);
-                               blobmsg_add_u32(&buf, NULL, 2);
-                       }
-                       else
-                       {
-                               blobmsg_add_u32(&buf, NULL, e->wpa_version);
-                       }
+                       for (wpa_version = 1; wpa_version <= 3; wpa_version++)
+                               if (e->wpa_version & (1 << (wpa_version - 1)))
+                                       blobmsg_add_u32(&buf, NULL, wpa_version);
 
                        blobmsg_close_array(&buf, d);
 
@@ -177,6 +171,12 @@ rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
                        if (e->auth_suites & IWINFO_KMGMT_8021x)
                                blobmsg_add_string(&buf, NULL, "802.1x");
 
+                       if (e->auth_suites & IWINFO_KMGMT_SAE)
+                               blobmsg_add_string(&buf, NULL, "sae");
+
+                       if (e->auth_suites & IWINFO_KMGMT_OWE)
+                               blobmsg_add_string(&buf, NULL, "owe");
+
                        if (!e->auth_suites ||
                                (e->auth_suites & IWINFO_KMGMT_NONE))
                                blobmsg_add_string(&buf, NULL, "none");
@@ -199,6 +199,9 @@ rpc_iwinfo_add_encryption(const char *name, struct iwinfo_crypto_entry *e)
                if (ciph & IWINFO_CIPHER_CCMP)
                        blobmsg_add_string(&buf, NULL, "ccmp");
 
+               if (ciph & IWINFO_CIPHER_GCMP)
+                       blobmsg_add_string(&buf, NULL, "gcmp");
+
                if (ciph & IWINFO_CIPHER_WRAP)
                        blobmsg_add_string(&buf, NULL, "wrap");
 
@@ -257,6 +260,21 @@ rpc_iwinfo_call_htmodes(const char *name)
                if (modes & IWINFO_HTMODE_VHT160)
                        blobmsg_add_string(&buf, NULL, "VHT160");
 
+               if (modes & IWINFO_HTMODE_HE20)
+                       blobmsg_add_string(&buf, NULL, "HE20");
+
+               if (modes & IWINFO_HTMODE_HE40)
+                       blobmsg_add_string(&buf, NULL, "HE40");
+
+               if (modes & IWINFO_HTMODE_HE80)
+                       blobmsg_add_string(&buf, NULL, "HE80");
+
+               if (modes & IWINFO_HTMODE_HE80_80)
+                       blobmsg_add_string(&buf, NULL, "HE80+80");
+
+               if (modes & IWINFO_HTMODE_HE160)
+                       blobmsg_add_string(&buf, NULL, "HE160");
+
                blobmsg_close_array(&buf, c);
        }
 }
@@ -271,9 +289,15 @@ rpc_iwinfo_call_hwmodes(const char *name)
        {
                c = blobmsg_open_array(&buf, name);
 
+               if (modes & IWINFO_80211_AD)
+                       blobmsg_add_string(&buf, NULL, "ad");
+
                if (modes & IWINFO_80211_AC)
                        blobmsg_add_string(&buf, NULL, "ac");
 
+               if (modes & IWINFO_80211_AX)
+                       blobmsg_add_string(&buf, NULL, "ax");
+
                if (modes & IWINFO_80211_A)
                        blobmsg_add_string(&buf, NULL, "a");
 
@@ -290,6 +314,74 @@ rpc_iwinfo_call_hwmodes(const char *name)
        }
 }
 
+static void rpc_iwinfo_call_hw_ht_mode()
+{
+       const char *hwmode_str;
+       const char *htmode_str;
+       int32_t htmode = 0;
+       int modes;
+
+       if (!iw->hwmodelist(ifname, &modes) && (modes == IWINFO_80211_AD)) {
+               blobmsg_add_string(&buf, "hwmode", "ad");
+               return;
+       }
+
+       if (iw->htmode(ifname, &htmode))
+               return;
+
+       switch (htmode) {
+               case IWINFO_HTMODE_HT20:
+                       htmode_str = "HT20";
+                       hwmode_str = "n";
+                       break;
+               case IWINFO_HTMODE_HT40:
+                       htmode_str = "HT40";
+                       hwmode_str = "n";
+                       break;
+               case IWINFO_HTMODE_VHT80:
+                       htmode_str = "VHT80";
+                       hwmode_str = "ac";
+                       break;
+               case IWINFO_HTMODE_VHT80_80:
+                       htmode_str = "VHT80+80";
+                       hwmode_str = "ac";
+                       break;
+               case IWINFO_HTMODE_VHT160:
+                       htmode_str = "VHT160";
+                       hwmode_str = "ac";
+                       break;
+               case IWINFO_HTMODE_HE20:
+                       htmode_str = "HE20";
+                       hwmode_str = "ax";
+                       break;
+               case IWINFO_HTMODE_HE40:
+                       htmode_str = "HE40";
+                       hwmode_str = "ax";
+                       break;
+               case IWINFO_HTMODE_HE80:
+                       htmode_str = "HE80";
+                       hwmode_str = "ax";
+                       break;
+               case IWINFO_HTMODE_HE80_80:
+                       htmode_str = "HE80+80";
+                       hwmode_str = "ax";
+                       break;
+               case IWINFO_HTMODE_HE160:
+                       htmode_str = "HE160";
+                       hwmode_str = "ax";
+                       break;
+               case IWINFO_HTMODE_NOHT:
+                       htmode_str = "20";
+                       hwmode_str = "a/g";
+                       break;
+               default:
+                       htmode_str = hwmode_str = "unknown";
+                       break;
+       }
+       blobmsg_add_string(&buf, "hwmode", hwmode_str);
+       blobmsg_add_string(&buf, "htmode", htmode_str);
+}
+
 static void
 rpc_iwinfo_call_str(const char *name, int (*func)(const char *, char *))
 {
@@ -322,6 +414,8 @@ rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
 
        rpc_iwinfo_call_int("mode", iw->mode, IWINFO_OPMODE_NAMES);
        rpc_iwinfo_call_int("channel", iw->channel, NULL);
+       rpc_iwinfo_call_int("center_chan1", iw->center_chan1, NULL);
+       rpc_iwinfo_call_int("center_chan2", iw->center_chan2, NULL);
 
        rpc_iwinfo_call_int("frequency", iw->frequency, NULL);
        rpc_iwinfo_call_int("frequency_offset", iw->frequency_offset, NULL);
@@ -341,6 +435,8 @@ rpc_iwinfo_info(struct ubus_context *ctx, struct ubus_object *obj,
        rpc_iwinfo_call_htmodes("htmodes");
        rpc_iwinfo_call_hwmodes("hwmodes");
 
+       rpc_iwinfo_call_hw_ht_mode();
+
        c = blobmsg_open_table(&buf, "hardware");
        rpc_iwinfo_call_hardware_id("id");
        rpc_iwinfo_call_str("name", iw->hardware_name);
@@ -359,7 +455,7 @@ rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
                 struct blob_attr *msg)
 {
        int i, rv, len;
-       void *c, *d;
+       void *c, *d, *t;
        char mac[18];
        char res[IWINFO_BUFSIZE];
        struct iwinfo_scanlist_entry *e;
@@ -397,6 +493,22 @@ rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
                        blobmsg_add_u32(&buf, "quality", e->quality);
                        blobmsg_add_u32(&buf, "quality_max", e->quality_max);
 
+                       if (e->ht_chan_info.primary_chan) {
+                               t = blobmsg_open_table(&buf, "ht_operation");
+                               blobmsg_add_u32(&buf, "primary_channel", e->ht_chan_info.primary_chan);
+                               blobmsg_add_string(&buf, "secondary_channel_offset", ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
+                               blobmsg_add_u32(&buf, "channel_width", ht_chan_width[e->ht_chan_info.chan_width]);
+                               blobmsg_close_table(&buf, t);
+                       }
+
+                       if (e->vht_chan_info.center_chan_1) {
+                               t = blobmsg_open_table(&buf, "vht_operation");
+                               blobmsg_add_u32(&buf, "channel_width", vht_chan_width[e->vht_chan_info.chan_width]);
+                               blobmsg_add_u32(&buf, "center_freq_1", e->vht_chan_info.center_chan_1);
+                               blobmsg_add_u32(&buf, "center_freq_2", e->vht_chan_info.center_chan_2);
+                               blobmsg_close_table(&buf, t);
+                       }
+
                        rpc_iwinfo_add_encryption("encryption", &e->crypto);
 
                        blobmsg_close_table(&buf, d);
@@ -412,6 +524,33 @@ rpc_iwinfo_scan(struct ubus_context *ctx, struct ubus_object *obj,
        return UBUS_STATUS_OK;
 }
 
+static void
+rpc_iwinfo_add_rateinfo(struct iwinfo_rate_entry *r)
+{
+       blobmsg_add_u8(&buf, "ht", r->is_ht);
+       blobmsg_add_u8(&buf, "vht", r->is_vht);
+       blobmsg_add_u8(&buf, "he", r->is_he);
+       blobmsg_add_u32(&buf, "mhz", r->mhz);
+       blobmsg_add_u32(&buf, "rate", r->rate);
+
+       if (r->is_ht) {
+               blobmsg_add_u32(&buf, "mcs", r->mcs);
+               blobmsg_add_u8(&buf, "40mhz", r->is_40mhz);
+               blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
+       }
+       else if (r->is_vht) {
+               blobmsg_add_u32(&buf, "mcs", r->mcs);
+               blobmsg_add_u32(&buf, "nss", r->nss);
+               blobmsg_add_u8(&buf, "short_gi", r->is_short_gi);
+       }
+       else if (r->is_he) {
+               blobmsg_add_u32(&buf, "mcs", r->mcs);
+               blobmsg_add_u32(&buf, "nss", r->nss);
+               blobmsg_add_u32(&buf, "he_gi", r->he_gi);
+               blobmsg_add_u32(&buf, "he_dcm", r->he_dcm);
+       }
+}
+
 static int
 rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
                      struct ubus_request_data *req, const char *method,
@@ -422,7 +561,7 @@ rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
        char res[IWINFO_BUFSIZE];
        struct iwinfo_assoclist_entry *a;
        struct ether_addr *macaddr = NULL;
-       void *c, *d, *e;
+       void *c = NULL, *d, *e;
        struct blob_attr *tb[__RPC_A_MAX];
        bool found = false;
 
@@ -458,21 +597,38 @@ rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
 
                        blobmsg_add_string(&buf, "mac", mac);
                        blobmsg_add_u32(&buf, "signal", a->signal);
+                       blobmsg_add_u32(&buf, "signal_avg", a->signal_avg);
                        blobmsg_add_u32(&buf, "noise", a->noise);
                        blobmsg_add_u32(&buf, "inactive", a->inactive);
+                       blobmsg_add_u32(&buf, "connected_time", a->connected_time);
+                       blobmsg_add_u32(&buf, "thr", a->thr);
+                       blobmsg_add_u8(&buf, "authorized", a->is_authorized);
+                       blobmsg_add_u8(&buf, "authenticated", a->is_authenticated);
+                       blobmsg_add_string(&buf, "preamble", a->is_preamble_short ? "short" : "long");
+                       blobmsg_add_u8(&buf, "wme", a->is_wme);
+                       blobmsg_add_u8(&buf, "mfp", a->is_mfp);
+                       blobmsg_add_u8(&buf, "tdls", a->is_tdls);
+
+                       blobmsg_add_u16(&buf, "mesh llid", a->llid);
+                       blobmsg_add_u16(&buf, "mesh plid", a->plid);
+                       blobmsg_add_string(&buf, "mesh plink", a->plink_state);
+                       blobmsg_add_string(&buf, "mesh local PS", a->local_ps);
+                       blobmsg_add_string(&buf, "mesh peer PS", a->peer_ps);
+                       blobmsg_add_string(&buf, "mesh non-peer PS", a->nonpeer_ps);
 
                        e = blobmsg_open_table(&buf, "rx");
-                       blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
-                       blobmsg_add_u32(&buf, "mcs", a->rx_rate.mcs);
-                       blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
-                       blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
+                       blobmsg_add_u64(&buf, "drop_misc", a->rx_drop_misc);
+                       blobmsg_add_u32(&buf, "packets", a->rx_packets);
+                       blobmsg_add_u32(&buf, "bytes", a->rx_bytes);
+                       rpc_iwinfo_add_rateinfo(&a->rx_rate);
                        blobmsg_close_table(&buf, e);
 
                        e = blobmsg_open_table(&buf, "tx");
-                       blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
-                       blobmsg_add_u32(&buf, "mcs", a->tx_rate.mcs);
-                       blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
-                       blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
+                       blobmsg_add_u32(&buf, "failed", a->tx_failed);
+                       blobmsg_add_u32(&buf, "retries", a->tx_retries);
+                       blobmsg_add_u32(&buf, "packets", a->tx_packets);
+                       blobmsg_add_u32(&buf, "bytes", a->tx_bytes);
+                       rpc_iwinfo_add_rateinfo(&a->tx_rate);
                        blobmsg_close_table(&buf, e);
 
                        found = true;
@@ -495,6 +651,45 @@ rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
        return UBUS_STATUS_OK;
 }
 
+static int
+rpc_iwinfo_survey(struct ubus_context *ctx, struct ubus_object *obj,
+                    struct ubus_request_data *req, const char *method,
+                    struct blob_attr *msg)
+{
+       char res[IWINFO_BUFSIZE];
+       struct iwinfo_survey_entry *e;
+       void *c, *d;
+       int i, rv, len;
+
+       blob_buf_init(&buf, 0);
+
+       rv = rpc_iwinfo_open(msg);
+
+       c = blobmsg_open_array(&buf, "results");
+
+       if (rv || iw->survey(ifname, res, &len) || len < 0)
+               return UBUS_STATUS_OK;
+
+       for (i = 0; i < len; i += sizeof(struct iwinfo_survey_entry)) {
+               e = (struct iwinfo_survey_entry *)&res[i];
+
+               d = blobmsg_open_table(&buf, NULL);
+               blobmsg_add_u32(&buf, "mhz", e->mhz);
+               blobmsg_add_u32(&buf, "noise", e->noise);
+               blobmsg_add_u64(&buf, "active_time", e->active_time);
+               blobmsg_add_u64(&buf, "busy_time", e->busy_time);
+               blobmsg_add_u64(&buf, "busy_time_ext", e->busy_time_ext);
+               blobmsg_add_u64(&buf, "rx_time", e->rxtime);
+               blobmsg_add_u64(&buf, "tx_time", e->txtime);
+               blobmsg_close_table(&buf, d);
+       }
+
+       blobmsg_close_array(&buf, c);
+       ubus_send_reply(ctx, req, buf.head);
+       rpc_iwinfo_close();
+       return UBUS_STATUS_OK;
+}
+
 static int
 rpc_iwinfo_freqlist(struct ubus_context *ctx, struct ubus_object *obj,
                     struct ubus_request_data *req, const char *method,
@@ -775,11 +970,12 @@ rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
                UBUS_METHOD("freqlist",    rpc_iwinfo_freqlist,    rpc_device_policy),
                UBUS_METHOD("txpowerlist", rpc_iwinfo_txpowerlist, rpc_device_policy),
                UBUS_METHOD("countrylist", rpc_iwinfo_countrylist, rpc_device_policy),
+               UBUS_METHOD("survey",      rpc_iwinfo_survey,      rpc_device_policy),
                UBUS_METHOD("phyname",     rpc_iwinfo_phyname,     rpc_uci_policy),
        };
 
        static struct ubus_object_type iwinfo_type =
-               UBUS_OBJECT_TYPE("luci-rpc-iwinfo", iwinfo_methods);
+               UBUS_OBJECT_TYPE("rpcd-plugin-iwinfo", iwinfo_methods);
 
        static struct ubus_object obj = {
                .name = "iwinfo",