add helper function for adding/removing devices to interfaces
authorFelix Fietkau <nbd@openwrt.org>
Sat, 19 Oct 2013 14:43:22 +0000 (16:43 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 22 Oct 2013 12:10:33 +0000 (14:10 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
interface.c
interface.h
ubus.c

index 5649324d44bc50a57c4acfffdc2ed69f9ad7ca1a..23c70326131dc6a5e0d2e45700178d04d95fb6e9 100644 (file)
@@ -745,6 +745,37 @@ interface_add_link(struct interface *iface, struct device *dev)
        return 0;
 }
 
+int
+interface_handle_link(struct interface *iface, const char *name, bool add)
+{
+       struct device *dev;
+       int ret;
+
+       device_lock();
+
+       dev = device_get(name, add ? 2 : 0);
+       if (!dev) {
+               ret = UBUS_STATUS_NOT_FOUND;
+               goto out;
+       }
+
+       if (add) {
+               device_set_present(dev, true);
+               if (iface->device_config)
+                       device_set_config(dev, &simple_device_type, iface->config);
+
+               system_if_apply_settings(dev, &dev->settings);
+               ret = interface_add_link(iface, dev);
+       } else {
+               ret = interface_remove_link(iface, dev);
+       }
+
+out:
+       device_unlock();
+
+       return ret;
+}
+
 int
 interface_set_up(struct interface *iface)
 {
index b845c9b67e021a83b8073f2c61837e791df7bfe3..4b7de5997d3055e1e85938230d3017f0356fa433 100644 (file)
@@ -167,6 +167,7 @@ void interface_remove_user(struct interface_user *dep);
 
 int interface_add_link(struct interface *iface, struct device *dev);
 int interface_remove_link(struct interface *iface, struct device *dev);
+int interface_handle_link(struct interface *iface, const char *name, bool add);
 
 void interface_add_error(struct interface *iface, const char *subsystem,
                         const char *code, const char **data, int n_data);
diff --git a/ubus.c b/ubus.c
index 31a83a9291a608bef94aaebc927ca82017ac2ba4..b75eaeca7cf52c0ba7c82ec6a235516eedac61bf 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -762,9 +762,7 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct blob_attr *tb[__DEV_MAX];
        struct interface *iface;
-       struct device *dev;
        bool add = !strncmp(method, "add", 3);
-       int ret;
 
        iface = container_of(obj, struct interface, ubus);
 
@@ -773,29 +771,7 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[DEV_NAME])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       device_lock();
-
-       dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
-       if (!dev) {
-               ret = UBUS_STATUS_NOT_FOUND;
-               goto out;
-       }
-
-       if (add) {
-               device_set_present(dev, true);
-               if (iface->device_config)
-                       device_set_config(dev, &simple_device_type, iface->config);
-
-               system_if_apply_settings(dev, &dev->settings);
-               ret = interface_add_link(iface, dev);
-       } else {
-               ret = interface_remove_link(iface, dev);
-       }
-
-out:
-       device_unlock();
-
-       return ret;
+       return interface_handle_link(iface, blobmsg_data(tb[DEV_NAME]), add);
 }