libubus: add method to policy
[project/ubus.git] / libubus.c
index b889e5a..d90dc1f 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -24,6 +24,7 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
        [UBUS_STATUS_OK] = "Success",
        [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
        [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
+       [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
        [UBUS_STATUS_NOT_FOUND] = "Not found",
        [UBUS_STATUS_NO_DATA] = "No response",
 };
@@ -34,6 +35,7 @@ static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
        [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
        [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
        [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
+       [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
 };
 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
 
@@ -43,6 +45,16 @@ struct ubus_pending_data {
        struct blob_attr data[];
 };
 
+static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
+{
+       const uint32_t *id1 = k1, *id2 = k2;
+
+       if (*id1 < *id2)
+               return -1;
+       else
+               return *id1 > *id2;
+}
+
 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
 {
        blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
@@ -97,6 +109,7 @@ int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
 
        INIT_LIST_HEAD(&req->list);
        INIT_LIST_HEAD(&req->pending);
+       req->ctx = ctx;
        req->peer = peer;
        req->seq = ++ctx->request_seq;
        return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
@@ -254,14 +267,43 @@ static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t
 
 static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
 {
+       struct ubus_request_data req;
+       struct ubus_object *obj;
        uint32_t objid = 0;
+       int method;
        int ret = 0;
 
        ubus_parse_msg(hdr->data);
 
-       if (attrbuf[UBUS_ATTR_OBJID])
-               objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+       if (!attrbuf[UBUS_ATTR_METHOD] || !attrbuf[UBUS_ATTR_OBJID]) {
+               ret = UBUS_STATUS_INVALID_ARGUMENT;
+               goto send;
+       }
+
+       objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+       obj = avl_find_element(&ctx->objects, &objid, obj, avl);
+       if (!obj) {
+               ret = UBUS_STATUS_NOT_FOUND;
+               goto send;
+       }
+
+       for (method = 0; method < obj->n_methods; method++)
+               if (!strcmp(obj->methods[method].name,
+                           blob_data(attrbuf[UBUS_ATTR_METHOD])))
+                       goto found;
+
+       /* not found */
+       ret = UBUS_STATUS_METHOD_NOT_FOUND;
+       goto send;
 
+found:
+       req.object = objid;
+       req.peer = hdr->peer;
+       req.seq = hdr->seq;
+       ret = obj->methods[method].handler(obj, &req, obj->methods[method].name,
+                                          attrbuf[UBUS_ATTR_DATA]);
+
+send:
        blob_buf_init(&b, 0);
        blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
        blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
@@ -394,6 +436,9 @@ static void ubus_publish_cb(struct ubus_request *req, int type, struct blob_attr
 
        if (attrbuf[UBUS_ATTR_OBJTYPE])
                obj->type->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJTYPE]);
+
+       obj->avl.key = &obj->id;
+       avl_insert(&req->ctx->objects, &obj->avl);
 }
 
 static bool ubus_push_table_data(const struct ubus_signature **sig, int *rem, bool array)
@@ -464,8 +509,6 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
 
        blob_buf_init(&b, 0);
        blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
-       if (obj->parent)
-               blob_put_int32(&b, UBUS_ATTR_OBJID, obj->parent->id);
 
        if (obj->type->id)
                blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
@@ -534,9 +577,11 @@ struct ubus_context *ubus_connect(const char *path)
        }
 
        ctx->local_id = hdr.hdr.peer;
-       INIT_LIST_HEAD(&ctx->requests);
        free(buf);
 
+       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");
                goto error_close;