+struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
+{
+ struct ubus_client *cl;
+
+ cl = calloc(1, sizeof(*cl));
+ if (!cl)
+ return NULL;
+
+ INIT_LIST_HEAD(&cl->objects);
+ cl->sock.fd = fd;
+ cl->sock.cb = cb;
+
+ if (!ubus_alloc_id(&clients, &cl->id, 0))
+ goto free;
+
+ if (!ubusd_send_hello(cl))
+ goto delete;
+
+ return cl;
+
+delete:
+ ubus_free_id(&clients, &cl->id);
+free:
+ free(cl);
+ return NULL;
+}
+
+void ubusd_proto_free_client(struct ubus_client *cl)
+{
+ struct ubus_object *obj;
+
+ while (!list_empty(&cl->objects)) {
+ obj = list_first_entry(&cl->objects, struct ubus_object, list);
+ ubusd_free_object(obj);
+ }
+
+ ubus_free_id(&clients, &cl->id);
+}
+
+void ubus_proto_notify_watch(struct ubus_watch *w)
+{
+ struct ubus_msg_buf *ub;
+ void *data;
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, w->watcher->id.id);
+ blob_put_string(&b, UBUS_ATTR_METHOD, w->method);
+
+ data = blob_nest_start(&b, UBUS_ATTR_DATA);
+ blobmsg_add_string(&b, "notify", "remove");
+ blobmsg_add_u32(&b, "id", w->watched->id.id);
+ blobmsg_add_u32(&b, "peer", w->watched->client->id.id);
+ blob_nest_end(&b, data);
+
+ ub = ubus_msg_from_blob(false);
+ ubus_msg_init(ub, UBUS_MSG_INVOKE, ++w->watcher->invoke_seq, 0);
+ ubus_msg_send(w->watcher->client, ub, true);
+
+ ubus_watch_free(w);
+}
+