diff options
author | Felix Fietkau <nbd@openwrt.org> | 2011-10-18 20:41:23 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2011-10-18 20:41:23 +0200 |
commit | ab4a8a357bae08a7bb3d835299fe53098dbdfbf7 (patch) | |
tree | c276ae98b2c3535085116f9836fba4dd210a6d0c /ubus.c | |
parent | 8b3e051a993acd3dcbc76818cc2726716a72cff7 (diff) |
add support for alias devices, which are activated based on hotplug events containing the real device name
Diffstat (limited to 'ubus.c')
-rw-r--r-- | ubus.c | 51 |
1 files changed, 50 insertions, 1 deletions
@@ -80,8 +80,57 @@ netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +enum { + ALIAS_ATTR_ALIAS, + ALIAS_ATTR_DEV, + __ALIAS_ATTR_MAX, +}; + +static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = { + [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY }, + [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING }, +}; + +static int +netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct device *dev = NULL; + struct blob_attr *tb[__ALIAS_ATTR_MAX]; + struct blob_attr *cur; + int rem; + + blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[ALIAS_ATTR_ALIAS]) + return UBUS_STATUS_INVALID_ARGUMENT; + + if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) { + dev = device_get(blobmsg_data(cur), true); + if (!dev) + return UBUS_STATUS_NOT_FOUND; + } + + blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) { + if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) + goto error; + + if (!blobmsg_check_attr(cur, NULL)) + goto error; + + alias_notify_device(blobmsg_data(cur), dev); + } + return 0; + +error: + device_free_unused(dev); + return UBUS_STATUS_INVALID_ARGUMENT; +} + static struct ubus_method dev_object_methods[] = { - UBUS_METHOD("status", netifd_dev_status, dev_policy) + UBUS_METHOD("status", netifd_dev_status, dev_policy), + UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs), }; static struct ubus_object_type dev_object_type = |