X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=120b1f0a3d009ae08ad377f0fe24643387f645f8;hp=31fc4673c9ce2867f9e026bf358fd2995d10a0b7;hb=fc205d64fcd70cda2dc9176ce2662d71c51370ab;hpb=1f8dc227cdd425f1313f564fd074c614bb1954a3 diff --git a/device.c b/device.c index 31fc467..120b1f0 100644 --- a/device.c +++ b/device.c @@ -3,14 +3,95 @@ #include #include -#include +#include +#include +#include #include "netifd.h" #include "system.h" +#include "config.h" static struct avl_tree devices; -static void API_CTOR dev_init(void) +enum { + DEV_ATTR_NAME, + DEV_ATTR_TYPE, + DEV_ATTR_MTU, + DEV_ATTR_MACADDR, + DEV_ATTR_TXQUEUELEN, + __DEV_ATTR_MAX, +}; + +static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = { + [DEV_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING }, + [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING }, + [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 }, + [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING }, + [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 }, +}; + +const struct config_param_list device_attr_list = { + .n_params = __DEV_ATTR_MAX, + .params = dev_attrs, +}; + +static void +device_init_settings(struct device *dev, struct blob_attr **tb) +{ + struct blob_attr *cur; + struct ether_addr *ea; + + dev->flags = 0; + + if ((cur = tb[DEV_ATTR_MTU])) { + dev->mtu = blobmsg_get_u32(cur); + dev->flags |= DEV_OPT_MTU; + } + + if ((cur = tb[DEV_ATTR_TXQUEUELEN])) { + dev->txqueuelen = blobmsg_get_u32(cur); + dev->flags |= DEV_OPT_TXQUEUELEN; + } + + if ((cur = tb[DEV_ATTR_MACADDR])) { + ea = ether_aton(blob_data(cur)); + if (ea) { + memcpy(dev->macaddr, ea, sizeof(dev->macaddr)); + dev->flags |= DEV_OPT_MACADDR; + } + } +} + +struct device * +device_create(struct blob_attr *attr, struct uci_section *s) +{ + struct blob_attr *tb[__DEV_ATTR_MAX]; + struct blob_attr *cur; + struct device *dev = NULL; + const char *name; + + blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr)); + if (!tb[DEV_ATTR_NAME]) + return NULL; + + name = blobmsg_data(tb[DEV_ATTR_NAME]); + if ((cur = tb[DEV_ATTR_TYPE])) { + if (!strcmp(blobmsg_data(cur), "bridge")) + dev = bridge_create(name, s); + } else { + dev = get_device(name, true); + } + + if (!dev) + return NULL; + + device_init_settings(dev, tb); + + return dev; +} + + +static void __init dev_init(void) { avl_init(&devices, avl_strcmp, false, NULL); } @@ -194,7 +275,7 @@ void remove_device_user(struct device_user *dep) } void -cleanup_devices(void) +device_free_all(void) { struct device *dev, *tmp;