main: add a command line option for dumping remote node data
[project/usteer.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index f38ce5368dfa0a39df3a6fef1f26200eabdbcef3..fe363840b1ee7dbd1f1c754e4ea4ef41b360768a 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -29,6 +29,7 @@
 #include "event.h"
 
 static struct blob_buf b;
+static KVLIST(host_info, kvlist_blob_len);
 
 static int
 usteer_ubus_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
@@ -266,27 +267,39 @@ usteer_ubus_set_config(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
-static void
-usteer_dump_node(struct usteer_node *node)
+void usteer_dump_node(struct blob_buf *buf, struct usteer_node *node)
 {
        void *c;
 
-       c = blobmsg_open_table(&b, usteer_node_name(node));
-       blobmsg_add_u32(&b, "freq", node->freq);
-       blobmsg_add_u32(&b, "n_assoc", node->n_assoc);
-       blobmsg_add_u32(&b, "noise", node->noise);
-       blobmsg_add_u32(&b, "load", node->load);
-       blobmsg_add_u32(&b, "max_assoc", node->max_assoc);
+       c = blobmsg_open_table(buf, usteer_node_name(node));
+       blobmsg_add_u32(buf, "freq", node->freq);
+       blobmsg_add_u32(buf, "n_assoc", node->n_assoc);
+       blobmsg_add_u32(buf, "noise", node->noise);
+       blobmsg_add_u32(buf, "load", node->load);
+       blobmsg_add_u32(buf, "max_assoc", node->max_assoc);
        if (node->rrm_nr)
-               blobmsg_add_field(&b, BLOBMSG_TYPE_ARRAY, "rrm_nr",
+               blobmsg_add_field(buf, BLOBMSG_TYPE_ARRAY, "rrm_nr",
                                  blobmsg_data(node->rrm_nr),
                                  blobmsg_data_len(node->rrm_nr));
        if (node->node_info)
-               blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "node_info",
+               blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "node_info",
                                  blob_data(node->node_info),
                                  blob_len(node->node_info));
 
-       blobmsg_close_table(&b, c);
+       blobmsg_close_table(buf, c);
+}
+
+void usteer_dump_host(struct blob_buf *buf, struct usteer_remote_host *host)
+{
+       void *c;
+
+       c = blobmsg_open_table(buf, host->addr);
+       blobmsg_add_u32(buf, "id", (uint32_t)(uintptr_t)host->avl.key);
+       if (host->host_info)
+               blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, "host_info",
+                                 blobmsg_data(host->host_info),
+                                 blobmsg_len(host->host_info));
+       blobmsg_close_table(buf, c);
 }
 
 static int
@@ -299,7 +312,24 @@ usteer_ubus_local_info(struct ubus_context *ctx, struct ubus_object *obj,
        blob_buf_init(&b, 0);
 
        for_each_local_node(node)
-               usteer_dump_node(node);
+               usteer_dump_node(&b, node);
+
+       ubus_send_reply(ctx, req, b.head);
+
+       return 0;
+}
+
+static int
+usteer_ubus_remote_hosts(struct ubus_context *ctx, struct ubus_object *obj,
+                        struct ubus_request_data *req, const char *method,
+                        struct blob_attr *msg)
+{
+       struct usteer_remote_host *host;
+
+       blob_buf_init(&b, 0);
+
+       avl_for_each_element(&remote_hosts, host, avl)
+               usteer_dump_host(&b, host);
 
        ubus_send_reply(ctx, req, b.head);
 
@@ -316,7 +346,7 @@ usteer_ubus_remote_info(struct ubus_context *ctx, struct ubus_object *obj,
        blob_buf_init(&b, 0);
 
        for_each_remote_node(rn)
-               usteer_dump_node(&rn->node);
+               usteer_dump_node(&b, &rn->node);
 
        ubus_send_reply(ctx, req, b.head);
 
@@ -340,20 +370,36 @@ static const struct blobmsg_policy del_node_data_policy[] = {
 };
 
 static void
-__usteer_ubus_update_node_data(struct usteer_local_node *ln, struct blob_attr *data,
-                              bool delete)
+usteer_update_kvlist_data(struct kvlist *kv, struct blob_attr *data,
+                         bool delete)
 {
        struct blob_attr *cur;
        int rem;
 
        blobmsg_for_each_attr(cur, data, rem) {
                if (delete)
-                       kvlist_delete(&ln->node_info, blobmsg_get_string(cur));
+                       kvlist_delete(kv, blobmsg_get_string(cur));
                else
-                       kvlist_set(&ln->node_info, blobmsg_name(cur), cur);
+                       kvlist_set(kv, blobmsg_name(cur), cur);
        }
+}
+
+static void
+usteer_update_kvlist_blob(struct blob_attr **dest, struct kvlist *kv)
+{
+       struct blob_attr *val;
+       const char *name;
+
+       blob_buf_init(&b, 0);
+       kvlist_for_each(kv, name, val)
+               blobmsg_add_field(&b, blobmsg_type(val), name,
+                                 blobmsg_data(val), blobmsg_len(val));
+
+       val = b.head;
+       if (!blobmsg_len(val))
+               val = NULL;
 
-       usteer_local_node_update_node_info(ln);
+       usteer_node_set_blob(dest, val);
 }
 
 static int
@@ -385,19 +431,21 @@ usteer_ubus_update_node_data(struct ubus_context *ctx, struct ubus_object *obj,
                if (!ln)
                        return UBUS_STATUS_NOT_FOUND;
 
-               __usteer_ubus_update_node_data(ln, val, delete);
+               usteer_update_kvlist_data(&ln->node_info, val, delete);
+               usteer_update_kvlist_blob(&ln->node.node_info, &ln->node_info);
 
                return 0;
        }
 
-       avl_for_each_element(&local_nodes, ln, node.avl)
-               __usteer_ubus_update_node_data(ln, val, delete);
+       usteer_update_kvlist_data(&host_info, val, delete);
+       usteer_update_kvlist_blob(&host_info_blob, &host_info);
 
        return 0;
 }
 
 static const struct ubus_method usteer_methods[] = {
        UBUS_METHOD_NOARG("local_info", usteer_ubus_local_info),
+       UBUS_METHOD_NOARG("remote_hosts", usteer_ubus_remote_hosts),
        UBUS_METHOD_NOARG("remote_info", usteer_ubus_remote_info),
        UBUS_METHOD_NOARG("get_clients", usteer_ubus_get_clients),
        UBUS_METHOD("get_client_info", usteer_ubus_get_client_info, client_arg),