+static int
+__ubus_watch_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, const char *method, int type)
+{
+ struct ubus_request req;
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
+ blob_put_int32(&b, UBUS_ATTR_TARGET, id);
+ if (method)
+ blob_put_string(&b, UBUS_ATTR_METHOD, method);
+
+ if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ return ubus_complete_request(ctx, &req, 0);
+
+}
+
+int ubus_watch_object_add(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
+{
+ return __ubus_watch_request(ctx, &obj->obj, id, "event", UBUS_MSG_ADD_WATCH);
+}
+
+int ubus_watch_object_remove(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
+{
+ return __ubus_watch_request(ctx, &obj->obj, id, NULL, UBUS_MSG_REMOVE_WATCH);
+}
+
+int ubus_send_event(struct ubus_context *ctx, const char *id,
+ struct blob_attr *data)
+{
+ struct ubus_request req;
+ void *s;
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
+ blob_put_string(&b, UBUS_ATTR_METHOD, "send");
+ s = blob_nest_start(&b, UBUS_ATTR_DATA);
+ blobmsg_add_string(&b, "id", id);
+ blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
+ blob_nest_end(&b, s);
+
+ if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ return ubus_complete_request(ctx, &req, 0);
+}
+
+static void
+ubus_refresh_state(struct ubus_context *ctx)
+{
+ struct ubus_object *obj, *tmp;
+
+ /* clear all type IDs, they need to be registered again */
+ avl_for_each_element(&ctx->objects, obj, avl)
+ obj->type->id = 0;
+
+ /* push out all objects again */
+ avl_for_each_element_safe(&ctx->objects, obj, avl, tmp) {
+ obj->id = 0;
+ avl_delete(&ctx->objects, &obj->avl);
+ ubus_add_object(ctx, obj);
+ }
+}
+
+int ubus_reconnect(struct ubus_context *ctx, const char *path)