From: Felix Fietkau Date: Fri, 23 Jan 2015 23:36:55 +0000 (+0100) Subject: device: move config apply handling to a helper function X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=b9d1f678c1f705b5635de31d333353a64ff6799d;p=oweals%2Fnetifd.git device: move config apply handling to a helper function Signed-off-by: Felix Fietkau --- diff --git a/device.c b/device.c index 6eb1486..f50fb85 100644 --- 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); diff --git a/device.h b/device.h index 0772f5f..ca4cc11 100644 --- 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);