device: move config apply handling to a helper function
authorFelix Fietkau <nbd@openwrt.org>
Fri, 23 Jan 2015 23:36:55 +0000 (00:36 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 23 Jan 2015 23:36:55 +0000 (00:36 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
device.c
device.h

index 6eb148694975438c39b1c86ffd4d465292bc4a4d..f50fb8505e1b7653659d63ffb895cbf3f2421b98 100644 (file)
--- a/device.c
+++ b/device.c
@@ -586,11 +586,18 @@ device_init_pending(void)
        }
 }
 
-static enum dev_change_type
-device_reload_config(struct device *dev, struct blob_attr *attr)
+enum dev_change_type
+device_set_config(struct device *dev, const struct device_type *type,
+                 struct blob_attr *attr)
 {
        struct blob_attr *tb[__DEV_ATTR_MAX];
-       const struct uci_blob_param_list *cfg = dev->type->config_params;
+       const struct uci_blob_param_list *cfg = type->config_params;
+
+       if (type != dev->type)
+               return DEV_CONFIG_RECREATE;
+
+       if (dev->type->reload)
+               return dev->type->reload(dev, attr);
 
        if (uci_blob_check_equal(dev->config, attr, cfg))
                return DEV_CONFIG_NO_CHANGE;
@@ -609,16 +616,37 @@ device_reload_config(struct device *dev, struct blob_attr *attr)
 }
 
 enum dev_change_type
-device_set_config(struct device *dev, const struct device_type *type,
-                 struct blob_attr *attr)
+device_apply_config(struct device *dev, const struct device_type *type,
+                   struct blob_attr *config)
 {
-       if (type != dev->type)
-               return DEV_CONFIG_RECREATE;
+       enum dev_change_type change;
 
-       if (dev->type->reload)
-               return dev->type->reload(dev, attr);
+       change = device_set_config(dev, type, config);
+       if (dev->external) {
+               system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
+               change = DEV_CONFIG_APPLIED;
+       }
 
-       return device_reload_config(dev, attr);
+       switch (change) {
+               case DEV_CONFIG_RESTART:
+               case DEV_CONFIG_APPLIED:
+                       D(DEVICE, "Device '%s': config applied\n", dev->ifname);
+                       free(dev->config);
+                       dev->config = config;
+                       if (change == DEV_CONFIG_RESTART && dev->present) {
+                               device_set_present(dev, false);
+                               device_set_present(dev, true);
+                       }
+                       break;
+               case DEV_CONFIG_NO_CHANGE:
+                       D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
+                       free(config);
+                       break;
+               case DEV_CONFIG_RECREATE:
+                       break;
+       }
+
+       return change;
 }
 
 static void
@@ -682,30 +710,14 @@ device_create(const char *name, const struct device_type *type,
        odev = device_get(name, false);
        if (odev) {
                odev->current_config = true;
-               change = device_set_config(odev, type, config);
-               if (odev->external) {
-                       system_if_apply_settings(odev, &odev->settings, odev->settings.flags);
-                       change = DEV_CONFIG_APPLIED;
-               }
+               change = device_apply_config(odev, type, config);
                switch (change) {
-               case DEV_CONFIG_RESTART:
-               case DEV_CONFIG_APPLIED:
-                       D(DEVICE, "Device '%s': config applied\n", odev->ifname);
-                       free(odev->config);
-                       odev->config = config;
-                       if (change == DEV_CONFIG_RESTART && odev->present) {
-                               device_set_present(odev, false);
-                               device_set_present(odev, true);
-                       }
-                       return odev;
-               case DEV_CONFIG_NO_CHANGE:
-                       D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
-                       free(config);
-                       return odev;
                case DEV_CONFIG_RECREATE:
                        D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
                        device_delete(odev);
                        break;
+               default:
+                       return odev;
                }
        } else
                D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
index 0772f5f4b0aa3cb18f973225a3ab76618c2ca3a9..ca4cc1184e7144874fd088a06f9003b775df3cdf 100644 (file)
--- a/device.h
+++ b/device.h
@@ -192,6 +192,9 @@ void device_init_pending(void);
 enum dev_change_type
 device_set_config(struct device *dev, const struct device_type *type,
                  struct blob_attr *attr);
+enum dev_change_type
+device_apply_config(struct device *dev, const struct device_type *type,
+                   struct blob_attr *config);
 
 void device_reset_config(void);
 void device_reset_old(void);