file: use blob_buf_free()
[project/rpcd.git] / iwinfo.c
index 3faf662..325c07a 100644 (file)
--- a/iwinfo.c
+++ b/iwinfo.c
@@ -1,7 +1,7 @@
 /*
  * rpcd - UBUS RPC server
  *
 /*
  * rpcd - UBUS RPC server
  *
- *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013-2014 Jo-Philipp Wich <jow@openwrt.org>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
 #include <libubus.h>
 #include <iwinfo.h>
 #include <iwinfo/utils.h>
 #include <libubus.h>
 #include <iwinfo.h>
 #include <iwinfo/utils.h>
+#include <net/ethernet.h>
+
+#ifdef linux
+#include <netinet/ether.h>
+#endif
 
 #include <rpcd/plugin.h>
 
 
 #include <rpcd/plugin.h>
 
@@ -38,6 +43,37 @@ static const struct blobmsg_policy rpc_device_policy[__RPC_D_MAX] = {
        [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
 };
 
        [RPC_D_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
 };
 
+enum {
+       RPC_A_DEVICE,
+       RPC_A_MACADDR,
+       __RPC_A_MAX,
+};
+
+static const struct blobmsg_policy rpc_assoclist_policy[__RPC_A_MAX] = {
+       [RPC_A_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
+       [RPC_A_MACADDR] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
+};
+
+enum {
+       RPC_U_SECTION,
+       __RPC_U_MAX
+};
+
+static const struct blobmsg_policy rpc_uci_policy[__RPC_U_MAX] = {
+       [RPC_U_SECTION] = { .name = "section", .type = BLOBMSG_TYPE_STRING },
+};
+
+static int
+__rpc_iwinfo_open(struct blob_attr *device)
+{
+       if (!device)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       ifname = blobmsg_data(device);
+       iw = iwinfo_backend(ifname);
+
+       return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
+}
 
 static int
 rpc_iwinfo_open(struct blob_attr *msg)
 
 static int
 rpc_iwinfo_open(struct blob_attr *msg)
@@ -47,13 +83,7 @@ rpc_iwinfo_open(struct blob_attr *msg)
        blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
                      blob_data(msg), blob_len(msg));
 
        blobmsg_parse(rpc_device_policy, __RPC_D_MAX, tb,
                      blob_data(msg), blob_len(msg));
 
-       if (!tb[RPC_D_DEVICE])
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       ifname = blobmsg_data(tb[RPC_D_DEVICE]);
-       iw = iwinfo_backend(ifname);
-
-       return iw ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
+       return __rpc_iwinfo_open(tb[RPC_D_DEVICE]);
 }
 
 static void
 }
 
 static void
@@ -206,6 +236,9 @@ rpc_iwinfo_call_hwmodes(const char *name)
        {
                c = blobmsg_open_array(&buf, name);
 
        {
                c = blobmsg_open_array(&buf, name);
 
+               if (modes & IWINFO_80211_AC)
+                       blobmsg_add_string(&buf, NULL, "ac");
+
                if (modes & IWINFO_80211_A)
                        blobmsg_add_string(&buf, NULL, "a");
 
                if (modes & IWINFO_80211_A)
                        blobmsg_add_string(&buf, NULL, "a");
 
@@ -352,23 +385,36 @@ rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
        char mac[18];
        char res[IWINFO_BUFSIZE];
        struct iwinfo_assoclist_entry *a;
        char mac[18];
        char res[IWINFO_BUFSIZE];
        struct iwinfo_assoclist_entry *a;
+       struct ether_addr *macaddr = NULL;
        void *c, *d, *e;
        void *c, *d, *e;
+       struct blob_attr *tb[__RPC_A_MAX];
+       bool found = false;
 
 
-       rv = rpc_iwinfo_open(msg);
+       blobmsg_parse(rpc_assoclist_policy, __RPC_A_MAX, tb,
+                     blob_data(msg), blob_len(msg));
 
 
+       rv = __rpc_iwinfo_open(tb[RPC_A_DEVICE]);
        if (rv)
                return rv;
 
        if (rv)
                return rv;
 
+       if (tb[RPC_A_MACADDR])
+               macaddr = ether_aton(blobmsg_data(tb[RPC_A_MACADDR]));
+
        blob_buf_init(&buf, 0);
 
        blob_buf_init(&buf, 0);
 
-       c = blobmsg_open_array(&buf, "results");
+       if (!macaddr)
+               c = blobmsg_open_array(&buf, "results");
 
        if (!iw->assoclist(ifname, res, &len) && (len > 0))
        {
                for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
                {
                        a = (struct iwinfo_assoclist_entry *)&res[i];
 
        if (!iw->assoclist(ifname, res, &len) && (len > 0))
        {
                for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
                {
                        a = (struct iwinfo_assoclist_entry *)&res[i];
-                       d = blobmsg_open_table(&buf, NULL);
+
+                       if (!macaddr)
+                               d = blobmsg_open_table(&buf, NULL);
+                       else if (memcmp(macaddr, a->mac, ETH_ALEN) != 0)
+                               continue;
 
                        snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
                                         a->mac[0], a->mac[1], a->mac[2],
 
                        snprintf(mac, sizeof(mac), "%02X:%02X:%02X:%02X:%02X:%02X",
                                         a->mac[0], a->mac[1], a->mac[2],
@@ -381,23 +427,30 @@ rpc_iwinfo_assoclist(struct ubus_context *ctx, struct ubus_object *obj,
 
                        e = blobmsg_open_table(&buf, "rx");
                        blobmsg_add_u32(&buf, "rate", a->rx_rate.rate);
 
                        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_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_close_table(&buf, e);
 
                        e = blobmsg_open_table(&buf, "tx");
                        blobmsg_add_u32(&buf, "rate", a->tx_rate.rate);
                        blobmsg_add_u8(&buf, "40mhz", a->rx_rate.is_40mhz);
                        blobmsg_add_u8(&buf, "short_gi", a->rx_rate.is_short_gi);
                        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_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_close_table(&buf, e);
 
                        blobmsg_add_u8(&buf, "40mhz", a->tx_rate.is_40mhz);
                        blobmsg_add_u8(&buf, "short_gi", a->tx_rate.is_short_gi);
                        blobmsg_close_table(&buf, e);
 
-                       blobmsg_close_table(&buf, d);
+                       found = true;
+                       if (!macaddr)
+                               blobmsg_close_table(&buf, d);
+                       else
+                               break;
                }
        }
 
                }
        }
 
-       blobmsg_close_array(&buf, c);
+       if (!macaddr)
+               blobmsg_close_array(&buf, c);
+       else if (!found)
+               return UBUS_STATUS_NOT_FOUND;
 
        ubus_send_reply(ctx, req, buf.head);
 
 
        ubus_send_reply(ctx, req, buf.head);
 
@@ -588,6 +641,55 @@ rpc_iwinfo_countrylist(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int
 }
 
 static int
+rpc_iwinfo_phyname(struct ubus_context *ctx, struct ubus_object *obj,
+                   struct ubus_request_data *req, const char *method,
+                   struct blob_attr *msg)
+{
+       int i;
+       bool found = false;
+       char res[IWINFO_BUFSIZE];
+       const struct iwinfo_ops *ops;
+       struct blob_attr *tb[__RPC_U_MAX];
+       const char *backends[] = {
+               "nl80211",
+               "madwifi",
+               "wl"
+       };
+
+       blobmsg_parse(rpc_uci_policy, __RPC_U_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (!tb[RPC_U_SECTION])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       for (i = 0; i < ARRAY_SIZE(backends); i++)
+       {
+               ops = iwinfo_backend_by_name(backends[i]);
+
+               if (!ops || !ops->lookup_phy)
+                       continue;
+
+               if (!ops->lookup_phy(blobmsg_get_string(tb[RPC_U_SECTION]), res))
+               {
+                       found = true;
+                       break;
+               }
+       }
+
+       if (found)
+       {
+               blob_buf_init(&buf, 0);
+               blobmsg_add_string(&buf, "phyname", res);
+
+               ubus_send_reply(ctx, req, buf.head);
+       }
+
+       rpc_iwinfo_close();
+
+       return found ? UBUS_STATUS_OK : UBUS_STATUS_NOT_FOUND;
+}
+
+static int
 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
                    struct ubus_request_data *req, const char *method,
                    struct blob_attr *msg)
 rpc_iwinfo_devices(struct ubus_context *ctx, struct ubus_object *obj,
                    struct ubus_request_data *req, const char *method,
                    struct blob_attr *msg)
@@ -630,13 +732,14 @@ static int
 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
 {
        static const struct ubus_method iwinfo_methods[] = {
 rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
 {
        static const struct ubus_method iwinfo_methods[] = {
-               { .name = "devices", .handler = rpc_iwinfo_devices },
+               UBUS_METHOD_NOARG("devices", rpc_iwinfo_devices),
                UBUS_METHOD("info",        rpc_iwinfo_info,        rpc_device_policy),
                UBUS_METHOD("scan",        rpc_iwinfo_scan,        rpc_device_policy),
                UBUS_METHOD("info",        rpc_iwinfo_info,        rpc_device_policy),
                UBUS_METHOD("scan",        rpc_iwinfo_scan,        rpc_device_policy),
-               UBUS_METHOD("assoclist",   rpc_iwinfo_assoclist,   rpc_device_policy),
+               UBUS_METHOD("assoclist",   rpc_iwinfo_assoclist,   rpc_assoclist_policy),
                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("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("phyname",     rpc_iwinfo_phyname,     rpc_uci_policy),
        };
 
        static struct ubus_object_type iwinfo_type =
        };
 
        static struct ubus_object_type iwinfo_type =
@@ -652,6 +755,6 @@ rpc_iwinfo_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
        return ubus_add_object(ctx, &obj);
 }
 
        return ubus_add_object(ctx, &obj);
 }
 
-const struct rpc_plugin rpc_plugin = {
+struct rpc_plugin rpc_plugin = {
        .init = rpc_iwinfo_api_init
 };
        .init = rpc_iwinfo_api_init
 };