X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=libubus.c;h=223a6bbb0139c17ba8175d7bc5f4f736b4042cef;hp=4c3d2c6d4bd9d34a990192f415433e75a4eabeed;hb=f87a9dc69e6f47a047311544d693eef739e20806;hpb=768d282ae9334351635679ba0dce073e5d9d8e1b diff --git a/libubus.c b/libubus.c index 4c3d2c6..223a6bb 100644 --- a/libubus.c +++ b/libubus.c @@ -27,6 +27,7 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = { [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found", [UBUS_STATUS_NOT_FOUND] = "Not found", [UBUS_STATUS_NO_DATA] = "No response", + [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied", }; static struct blob_buf b; @@ -605,21 +606,21 @@ static bool ubus_push_object_type(struct ubus_object_type *type) return true; } -int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) +static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) { struct ubus_request req; int ret; - if (obj->id || !obj->name || !obj->type) - return UBUS_STATUS_INVALID_ARGUMENT; - blob_buf_init(&b, 0); - blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name); - if (obj->type->id) - blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id); - else if (!ubus_push_object_type(obj->type)) - return UBUS_STATUS_INVALID_ARGUMENT; + if (obj->name && obj->type) { + blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name); + + if (obj->type->id) + blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id); + else if (!ubus_push_object_type(obj->type)) + return UBUS_STATUS_INVALID_ARGUMENT; + } ubus_start_request(ctx, &req, b.head, UBUS_MSG_PUBLISH, 0); req.raw_data_cb = ubus_publish_cb; @@ -634,6 +635,43 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) return 0; } +int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) +{ + if (!obj->name || !obj->type) + return UBUS_STATUS_INVALID_ARGUMENT; + + return __ubus_publish(ctx, obj); +} + +int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj, + const char *pattern) +{ + struct blob_buf b2; + int ret; + + if (!obj->id) { + if (!!obj->name ^ !!obj->type) + return UBUS_STATUS_INVALID_ARGUMENT; + + ret = __ubus_publish(ctx, obj); + if (ret) + return ret; + } + + /* use a second buffer, ubus_invoke() overwrites the primary one */ + memset(&b2, 0, sizeof(b2)); + blob_buf_init(&b2, 0); + blobmsg_add_u32(&b2, "object", obj->id); + if (pattern) + blobmsg_add_string(&b2, "pattern", pattern); + + ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head, + NULL, NULL); + + return 0; +} + + void ubus_default_connection_lost(struct ubus_context *ctx) { if (ctx->sock.registered) @@ -657,36 +695,27 @@ struct ubus_context *ubus_connect(const char *path) goto error; ctx->sock.fd = usock(USOCK_UNIX, path, NULL); - if (ctx->sock.fd < 0) { - DPRINTF("Failed to connect to server\n"); + if (ctx->sock.fd < 0) goto error_free; - } + ctx->sock.cb = ubus_handle_data; - if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { - DPRINTF("Failed to read initial message data\n"); + if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto error_close; - } - if (!ubus_validate_hdr(&hdr.hdr)) { - DPRINTF("Failed to validate initial message header\n"); + if (!ubus_validate_hdr(&hdr.hdr)) goto error_close; - } - if (hdr.hdr.type != UBUS_MSG_HELLO) { - DPRINTF("Unexpected initial message\n"); + if (hdr.hdr.type != UBUS_MSG_HELLO) goto error_close; - } buf = calloc(1, blob_raw_len(&hdr.data)); if (!buf) goto error_close; memcpy(buf, &hdr.data, sizeof(hdr.data)); - if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf)) { - DPRINTF("Failed to retrieve initial message data\n"); + if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf)) goto error_free_buf; - } ctx->local_id = hdr.hdr.peer; free(buf); @@ -696,10 +725,8 @@ struct ubus_context *ubus_connect(const char *path) INIT_LIST_HEAD(&ctx->requests); avl_init(&ctx->objects, ubus_cmp_id, false, NULL); - if (!ctx->local_id) { - DPRINTF("Failed to get local peer id\n"); + if (!ctx->local_id) goto error_close; - } return ctx;