Revert "system-linux: re-apply ethtool on phy attachment"
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 55daec1b74450478262beb02c9a12104731a943c..f8662c2a7b78be1f3a817affdd4a8e17124d5d9c 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -27,7 +27,7 @@
 struct ubus_context *ubus_ctx = NULL;
 static struct blob_buf b;
 static const char *ubus_path;
-struct ubus_subscriber udebug_sub;
+static struct udebug_ubus udebug;
 
 /* global object */
 
@@ -435,12 +435,12 @@ netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
        int t = 2;
 
        if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
-               D(SYSTEM, "failed to reconnect, trying again in %d seconds\n", t);
+               D(SYSTEM, "failed to reconnect, trying again in %d seconds", t);
                uloop_timeout_set(&retry, t * 1000);
                return;
        }
 
-       D(SYSTEM, "reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
+       D(SYSTEM, "reconnected to ubus, new id: %08x", ubus_ctx->local_id);
        netifd_ubus_add_fd();
 }
 
@@ -1366,82 +1366,16 @@ netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
        return ubus_invoke(ubus_ctx, id, method, msg, data_cb, data, 3000);
 }
 
-static struct blob_attr *
-find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
-{
-       struct blobmsg_policy policy = { name, type };
-       struct blob_attr *ret;
-
-       if (!attr)
-               return NULL;
-
-       blobmsg_parse_attr(&policy, 1, &ret, attr);
-
-       return ret;
-}
-
-static void
-netifd_udebug_config_cb(struct blob_attr *data)
-{
-       enum {
-               CFG_ATTR_ENABLED,
-               __CFG_ATTR_MAX
-       };
-       static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
-               [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
-       };
-       struct blob_attr *tb[__CFG_ATTR_MAX];
-       bool en;
-
-       data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
-       data = find_attr(data, "netifd", BLOBMSG_TYPE_TABLE);
-       if (!data)
-               return;
-
-       blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
-       if (!tb[CFG_ATTR_ENABLED])
-               return;
-
-       en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
-       netifd_udebug_set_enabled(en);
-}
-
-static int
-netifd_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
-                       struct ubus_request_data *req, const char *method,
-                       struct blob_attr *msg)
-{
-       netifd_udebug_config_cb(msg);
-
-       return 0;
-}
-
-static void
-netifd_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
-{
-       netifd_udebug_config_cb(msg);
-}
-
-static bool
-netifd_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
-                    const char *path)
-{
-       return !strcmp(path, "udebug");
-}
-
 int
 netifd_ubus_init(const char *path)
 {
-       uint32_t id;
-
-       uloop_init();
        ubus_path = path;
 
        ubus_ctx = ubus_connect(path);
        if (!ubus_ctx)
                return -EIO;
 
-       D(SYSTEM, "connected as %08x\n", ubus_ctx->local_id);
+       D(SYSTEM, "connected as %08x", ubus_ctx->local_id);
        ubus_ctx->connection_lost = netifd_ubus_connection_lost;
        netifd_ubus_add_fd();
 
@@ -1450,13 +1384,7 @@ netifd_ubus_init(const char *path)
        netifd_add_object(&wireless_object);
        netifd_add_iface_object();
 
-       udebug_sub.cb = netifd_udebug_notify_cb;
-       udebug_sub.new_obj_cb = netifd_udebug_sub_cb;
-       ubus_register_subscriber(ubus_ctx, &udebug_sub);
-       if (ubus_lookup_id(ubus_ctx, "udebug", &id) == 0) {
-               ubus_subscribe(ubus_ctx, &udebug_sub, id);
-               ubus_invoke(ubus_ctx, id, "get_config", NULL, netifd_udebug_req_cb, NULL, 1000);
-       }
+       udebug_ubus_init(&udebug, ubus_ctx, "netifd", netifd_udebug_config);
 
        return 0;
 }
@@ -1464,6 +1392,7 @@ netifd_ubus_init(const char *path)
 void
 netifd_ubus_done(void)
 {
+       udebug_ubus_free(&udebug);
        ubus_free(ubus_ctx);
 }
 
@@ -1501,7 +1430,7 @@ netifd_ubus_add_interface(struct interface *iface)
        obj->methods = iface_object_methods;
        obj->n_methods = ARRAY_SIZE(iface_object_methods);
        if (ubus_add_object(ubus_ctx, &iface->ubus)) {
-               D(SYSTEM, "failed to publish ubus object for interface '%s'\n", iface->name);
+               D(SYSTEM, "failed to publish ubus object for interface '%s'", iface->name);
                free(name);
                obj->name = NULL;
        }
@@ -1516,3 +1445,45 @@ netifd_ubus_remove_interface(struct interface *iface)
        ubus_remove_object(ubus_ctx, &iface->ubus);
        free((void *) iface->ubus.name);
 }
+
+static void
+netifd_ubus_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       struct blob_attr *srv, *in, *t, *data;
+       procd_data_cb cb = req->priv;
+       int rem, rem2, rem3, rem4;
+
+       blobmsg_for_each_attr(srv, msg, rem) {
+               if (!blobmsg_check_attr(srv, true) ||
+                   blobmsg_type(srv) != BLOBMSG_TYPE_TABLE)
+                       continue;
+               blobmsg_for_each_attr(in, srv, rem2) {
+                       if (!blobmsg_check_attr(in , true) ||
+                               blobmsg_type(in) != BLOBMSG_TYPE_TABLE)
+                               continue;
+                       blobmsg_for_each_attr(t, in, rem3) {
+                               if (!blobmsg_check_attr(t, true) ||
+                                       blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
+                                       continue;
+                               blobmsg_for_each_attr(data, t, rem4) {
+                                       if (!blobmsg_check_attr(t, true) ||
+                                               blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
+                                               continue;
+                                       cb(data);
+                               }
+                       }
+               }
+       }
+}
+
+void netifd_ubus_get_procd_data(const char *type, procd_data_cb cb)
+{
+       uint32_t id;
+
+       if (ubus_lookup_id(ubus_ctx, "service", &id))
+               return;
+
+       blob_buf_init(&b, 0);
+       blobmsg_add_string(&b, "type", type);
+       ubus_invoke(ubus_ctx, id, "get_data", b.head, netifd_ubus_data_cb, cb, 30000);
+}