+static struct ubus_object iface_object = {
+ .name = "network.interface",
+ .type = &iface_object_type,
+ .n_methods = ARRAY_SIZE(iface_object_methods),
+};
+
+static void netifd_add_object(struct ubus_object *obj)
+{
+ int ret = ubus_add_object(ubus_ctx, obj);
+
+ if (ret != 0)
+ fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
+}
+
+static const struct blobmsg_policy iface_policy = {
+ .name = "interface",
+ .type = BLOBMSG_TYPE_STRING,
+};
+
+static int
+netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct interface *iface;
+ struct blob_attr *tb;
+ int i;
+
+ blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
+ if (!tb)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
+ if (!iface)
+ return UBUS_STATUS_NOT_FOUND;
+
+ for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
+ ubus_handler_t cb;
+
+ if (strcmp(method, iface_object_methods[i].name) != 0)
+ continue;
+
+ cb = iface_object_methods[i].handler;
+ return cb(ctx, &iface->ubus, req, method, msg);
+ }
+
+ return UBUS_STATUS_INVALID_ARGUMENT;
+}
+
+static void netifd_add_iface_object(void)
+{
+ struct ubus_method *methods;
+ int i;
+
+ methods = calloc(1, sizeof(iface_object_methods));
+ memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
+ iface_object.methods = methods;
+
+ for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
+ if (methods[i].handler == netifd_handle_dump)
+ continue;
+
+ methods[i].handler = netifd_handle_iface;
+ methods[i].policy = &iface_policy;
+ methods[i].n_policy = 1;
+ }
+ netifd_add_object(&iface_object);
+}
+
+static struct wireless_device *
+get_wdev(struct blob_attr *msg, int *ret)
+{
+ struct blobmsg_policy wdev_policy = {
+ .name = "device",
+ .type = BLOBMSG_TYPE_STRING,
+ };
+ struct blob_attr *dev_attr;
+ struct wireless_device *wdev = NULL;
+
+
+ blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
+ if (!dev_attr) {
+ *ret = UBUS_STATUS_INVALID_ARGUMENT;
+ return NULL;
+ }
+
+ wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
+ if (!wdev) {
+ *ret = UBUS_STATUS_NOT_FOUND;
+ return NULL;
+ }
+
+ *ret = 0;
+ return wdev;
+}
+
+static int
+netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct wireless_device *wdev;
+ int ret;
+
+ wdev = get_wdev(msg, &ret);
+ if (ret == UBUS_STATUS_NOT_FOUND)
+ return ret;
+
+ if (wdev) {
+ wireless_device_set_up(wdev);
+ } else {
+ vlist_for_each_element(&wireless_devices, wdev, node)
+ wireless_device_set_up(wdev);
+ }
+
+ return 0;
+}
+
+static int
+netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct wireless_device *wdev;
+ int ret;
+
+ wdev = get_wdev(msg, &ret);
+ if (ret == UBUS_STATUS_NOT_FOUND)
+ return ret;
+
+ if (wdev) {
+ wireless_device_set_down(wdev);
+ } else {
+ vlist_for_each_element(&wireless_devices, wdev, node)
+ wireless_device_set_down(wdev);
+ }
+
+ return 0;
+}
+
+static int
+netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct wireless_device *wdev;
+ int ret;
+
+ wdev = get_wdev(msg, &ret);
+ if (ret == UBUS_STATUS_NOT_FOUND)
+ return ret;
+
+ blob_buf_init(&b, 0);
+ if (wdev) {
+ wireless_device_status(wdev, &b);
+ } else {
+ vlist_for_each_element(&wireless_devices, wdev, node)
+ wireless_device_status(wdev, &b);
+ }
+ ubus_send_reply(ctx, req, b.head);
+ return 0;
+}
+
+static int
+netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct wireless_device *wdev;
+ int ret;
+
+ wdev = get_wdev(msg, &ret);
+ if (ret == UBUS_STATUS_NOT_FOUND)
+ return ret;
+
+ blob_buf_init(&b, 0);
+ if (wdev) {
+ wireless_device_get_validate(wdev, &b);
+ } else {
+ vlist_for_each_element(&wireless_devices, wdev, node)
+ wireless_device_get_validate(wdev, &b);
+ }
+ ubus_send_reply(ctx, req, b.head);
+ return 0;
+}
+
+static int
+netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct wireless_device *wdev;
+ int ret;
+
+ wdev = get_wdev(msg, &ret);
+ if (!wdev)
+ return ret;
+
+ return wireless_device_notify(wdev, msg, req);
+}
+
+static struct ubus_method wireless_object_methods[] = {
+ { .name = "up", .handler = netifd_handle_wdev_up },
+ { .name = "down", .handler = netifd_handle_wdev_down },
+ { .name = "status", .handler = netifd_handle_wdev_status },
+ { .name = "notify", .handler = netifd_handle_wdev_notify },
+ { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
+};
+
+static struct ubus_object_type wireless_object_type =
+ UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
+
+
+static struct ubus_object wireless_object = {
+ .name = "network.wireless",
+ .type = &wireless_object_type,
+ .methods = wireless_object_methods,
+ .n_methods = ARRAY_SIZE(wireless_object_methods),
+};
+
+int
+netifd_ubus_init(const char *path)
+{
+ uloop_init();
+ ubus_path = path;
+
+ ubus_ctx = ubus_connect(path);
+ if (!ubus_ctx)
+ return -EIO;
+
+ DPRINTF("connected as %08x\n", ubus_ctx->local_id);
+ ubus_ctx->connection_lost = netifd_ubus_connection_lost;
+ netifd_ubus_add_fd();
+
+ netifd_add_object(&main_object);
+ netifd_add_object(&dev_object);
+ netifd_add_object(&wireless_object);
+ netifd_add_iface_object();
+
+ return 0;
+}
+
+void
+netifd_ubus_done(void)
+{
+ ubus_free(ubus_ctx);
+}
+