+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;
+ cl->pending_msg_fd = -1;
+
+ 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_notify_subscription(struct ubus_object *obj)
+{
+ bool active = !list_empty(&obj->subscribers);
+ struct ubus_msg_buf *ub;
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
+ blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
+
+ ub = ubus_msg_from_blob(false);
+ ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
+ ubus_msg_send(obj->client, ub, true);
+}
+
+void ubus_notify_unsubscribe(struct ubus_subscription *s)
+{
+ struct ubus_msg_buf *ub;
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
+ blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
+
+ ub = ubus_msg_from_blob(false);
+ ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
+ ubus_msg_send(s->subscriber->client, ub, true);
+
+ ubus_unsubscribe(s);
+}
+