add a callback for handling lost ubus connections
[project/ubus.git] / libubus.c
index e770ce3..64190f7 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_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",
 };
        [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_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];
 
 };
 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
 
@@ -265,14 +267,47 @@ 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)
 {
 
 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;
        uint32_t objid = 0;
+       int method;
        int ret = 0;
 
        ubus_parse_msg(hdr->data);
 
        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_OBJID])
+               return;
+
+       objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+
+       if (!attrbuf[UBUS_ATTR_METHOD]) {
+               ret = UBUS_STATUS_INVALID_ARGUMENT;
+               goto send;
+       }
+
+       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);
        blob_buf_init(&b, 0);
        blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
        blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
@@ -332,6 +367,9 @@ static void ubus_handle_data(struct uloop_fd *u, unsigned int events)
 
        while (get_next_msg(ctx, false))
                ubus_process_msg(ctx, hdr);
 
        while (get_next_msg(ctx, false))
                ubus_process_msg(ctx, hdr);
+
+       if (u->eof)
+               ctx->connection_lost(ctx);
 }
 
 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req)
 }
 
 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req)
@@ -497,6 +535,12 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
        return 0;
 }
 
        return 0;
 }
 
+void ubus_default_connection_lost(struct ubus_context *ctx)
+{
+       if (ctx->sock.registered)
+               uloop_end();
+}
+
 struct ubus_context *ubus_connect(const char *path)
 {
        struct ubus_context *ctx;
 struct ubus_context *ubus_connect(const char *path)
 {
        struct ubus_context *ctx;
@@ -548,6 +592,8 @@ struct ubus_context *ubus_connect(const char *path)
        ctx->local_id = hdr.hdr.peer;
        free(buf);
 
        ctx->local_id = hdr.hdr.peer;
        free(buf);
 
+       ctx->connection_lost = ubus_default_connection_lost;
+
        INIT_LIST_HEAD(&ctx->requests);
        avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
 
        INIT_LIST_HEAD(&ctx->requests);
        avl_init(&ctx->objects, ubus_cmp_id, false, NULL);