+enum {
+ DEV_NAME,
+ __DEV_MAX,
+};
+
+static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
+ [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
+};
+
+static int
+netifd_dev_status(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[__DEV_MAX];
+
+ blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
+
+ if (tb[DEV_NAME]) {
+ dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
+ if (!dev)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+ }
+
+ blob_buf_init(&b, 0);
+ device_dump_status(&b, dev);
+ ubus_send_reply(ctx, req, b.head);
+
+ 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;
+}
+
+enum {
+ DEV_STATE_NAME,
+ DEV_STATE_DEFER,
+ __DEV_STATE_MAX,
+};
+
+static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
+ [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
+ [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
+};
+
+static int
+netifd_handle_set_state(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[__DEV_STATE_MAX];
+ struct blob_attr *cur;
+
+ blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
+
+ cur = tb[DEV_STATE_NAME];
+ if (!cur)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ dev = device_get(blobmsg_data(cur), false);
+ if (!dev)
+ return UBUS_STATUS_NOT_FOUND;
+
+ cur = tb[DEV_STATE_DEFER];
+ if (cur)
+ device_set_deferred(dev, !!blobmsg_get_u8(cur));
+
+ return 0;
+}
+
+static struct ubus_method dev_object_methods[] = {
+ UBUS_METHOD("status", netifd_dev_status, dev_policy),
+ UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
+ UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
+};
+
+static struct ubus_object_type dev_object_type =
+ UBUS_OBJECT_TYPE("device", dev_object_methods);
+
+static struct ubus_object dev_object = {
+ .name = "network.device",
+ .type = &dev_object_type,
+ .methods = dev_object_methods,
+ .n_methods = ARRAY_SIZE(dev_object_methods),
+};
+
+static void
+netifd_ubus_add_fd(void)
+{
+ ubus_add_uloop(ctx);
+ system_fd_set_cloexec(ctx->sock.fd);
+}
+
+static void
+netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
+{
+ static struct uloop_timeout retry = {
+ .cb = netifd_ubus_reconnect_timer,
+ };
+ int t = 2;
+
+ if (ubus_reconnect(ctx, ubus_path) != 0) {
+ DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
+ uloop_timeout_set(&retry, t * 1000);
+ return;
+ }
+
+ DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
+ netifd_ubus_add_fd();
+}
+
+static void
+netifd_ubus_connection_lost(struct ubus_context *ctx)
+{
+ netifd_ubus_reconnect_timer(NULL);
+}
+