add working method calls

This commit is contained in:
Felix Fietkau 2011-01-31 03:26:53 +01:00
parent 44096a0a34
commit 1a871e1e89
4 changed files with 63 additions and 9 deletions

View file

@ -24,6 +24,7 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
[UBUS_STATUS_OK] = "Success", [UBUS_STATUS_OK] = "Success",
[UBUS_STATUS_INVALID_COMMAND] = "Invalid command", [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
[UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument", [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
[UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
[UBUS_STATUS_NOT_FOUND] = "Not found", [UBUS_STATUS_NOT_FOUND] = "Not found",
[UBUS_STATUS_NO_DATA] = "No response", [UBUS_STATUS_NO_DATA] = "No response",
}; };
@ -265,14 +266,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) 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; int ret = 0;
ubus_parse_msg(hdr->data); ubus_parse_msg(hdr->data);
if (attrbuf[UBUS_ATTR_OBJID]) if (!attrbuf[UBUS_ATTR_METHOD] || !attrbuf[UBUS_ATTR_OBJID]) {
objid = blob_get_int32(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_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_STATUS, ret); blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
blob_put_int32(&b, UBUS_ATTR_OBJID, objid); blob_put_int32(&b, UBUS_ATTR_OBJID, objid);

View file

@ -11,7 +11,7 @@ struct ubus_object;
struct ubus_request; struct ubus_request;
struct ubus_request_data; struct ubus_request_data;
typedef void (*ubus_handler_t)(struct ubus_object *obj, typedef int (*ubus_handler_t)(struct ubus_object *obj,
struct ubus_request_data *req, struct ubus_request_data *req,
const char *method, struct blob_attr *msg); const char *method, struct blob_attr *msg);
typedef void (*ubus_data_handler_t)(struct ubus_request *req, typedef void (*ubus_data_handler_t)(struct ubus_request *req,
@ -52,6 +52,11 @@ struct ubus_object_type {
const struct ubus_signature *signature; const struct ubus_signature *signature;
}; };
struct ubus_method {
const char *name;
ubus_handler_t handler;
};
struct ubus_object { struct ubus_object {
struct avl_node avl; struct avl_node avl;
@ -60,6 +65,9 @@ struct ubus_object {
const char *path; const char *path;
struct ubus_object_type *type; struct ubus_object_type *type;
const struct ubus_method *methods;
int n_methods;
}; };
struct ubus_context { struct ubus_context {

View file

@ -15,14 +15,29 @@ static const struct ubus_signature test_object_sig[] = {
static struct ubus_object_type test_object_type = static struct ubus_object_type test_object_type =
UBUS_OBJECT_TYPE("test", test_object_sig); UBUS_OBJECT_TYPE("test", test_object_sig);
static int test_hello(struct ubus_object *obj, struct ubus_request_data *req,
const char *method, struct blob_attr *msg)
{
fprintf(stderr, "Hello, world!\n");
return 0;
}
static const struct ubus_method test_methods[] = {
{ .name = "hello", .handler = test_hello },
};
static struct ubus_object test_object = { static struct ubus_object test_object = {
.name = "test", .name = "test",
.type = &test_object_type, .type = &test_object_type,
.methods = test_methods,
.n_methods = ARRAY_SIZE(test_methods),
}; };
static struct ubus_object test_object2 = { static struct ubus_object test_object2 = {
.name = "test2", .name = "test2",
.type = &test_object_type, .type = &test_object_type,
.methods = test_methods,
.n_methods = ARRAY_SIZE(test_methods),
}; };
int main(int argc, char **argv) int main(int argc, char **argv)

View file

@ -64,6 +64,7 @@ enum ubus_msg_status {
UBUS_STATUS_OK, UBUS_STATUS_OK,
UBUS_STATUS_INVALID_COMMAND, UBUS_STATUS_INVALID_COMMAND,
UBUS_STATUS_INVALID_ARGUMENT, UBUS_STATUS_INVALID_ARGUMENT,
UBUS_STATUS_METHOD_NOT_FOUND,
UBUS_STATUS_NOT_FOUND, UBUS_STATUS_NOT_FOUND,
UBUS_STATUS_NO_DATA, UBUS_STATUS_NO_DATA,
__UBUS_STATUS_LAST __UBUS_STATUS_LAST