add a simple status callback for network interfaces
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 16d5e88..0723300 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -4,6 +4,7 @@
 #include "ubus.h"
 
 static struct ubus_context *ctx = NULL;
+static struct blob_buf b;
 
 /* global object */
 
@@ -130,9 +131,37 @@ static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
+                               struct ubus_request_data *req, const char *method,
+                               struct blob_attr *msg)
+{
+       struct interface *iface;
+
+       iface = container_of(obj, struct interface, ubus);
+
+       blob_buf_init(&b, 0);
+       blobmsg_add_u8(&b, "up", iface->up);
+       blobmsg_add_u8(&b, "active", iface->active);
+       blobmsg_add_u8(&b, "autostart", iface->autostart);
+       if (iface->main_dev.dev) {
+               struct device *dev = iface->main_dev.dev;
+
+               if (dev->avl.key)
+                       blobmsg_add_string(&b, "device", dev->ifname);
+               else
+                       /* use a different field for virtual devices */
+                       blobmsg_add_string(&b, "link", dev->ifname);
+       }
+
+       ubus_send_reply(ctx, req, b.head);
+
+       return 0;
+}
+
 static struct ubus_method iface_object_methods[] = {
        { .name = "up", .handler = netifd_handle_up },
        { .name = "down", .handler = netifd_handle_down },
+       { .name = "status", .handler = netifd_handle_status },
 };