[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",
};
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);
struct ubus_request;
struct ubus_request_data;
-typedef void (*ubus_handler_t)(struct ubus_object *obj,
- struct ubus_request_data *req,
- const char *method, struct blob_attr *msg);
+typedef int (*ubus_handler_t)(struct ubus_object *obj,
+ struct ubus_request_data *req,
+ const char *method, struct blob_attr *msg);
typedef void (*ubus_data_handler_t)(struct ubus_request *req,
int type, struct blob_attr *msg);
typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
const struct ubus_signature *signature;
};
+struct ubus_method {
+ const char *name;
+ ubus_handler_t handler;
+};
+
struct ubus_object {
struct avl_node avl;
const char *path;
struct ubus_object_type *type;
+
+ const struct ubus_method *methods;
+ int n_methods;
};
struct ubus_context {
static const struct ubus_signature test_object_sig[] = {
UBUS_METHOD_START("hello"),
UBUS_ARRAY("test"),
- UBUS_TABLE_START(NULL),
- UBUS_FIELD(INT32, "id"),
- UBUS_FIELD(STRING, "msg"),
- UBUS_TABLE_END(),
+ UBUS_TABLE_START(NULL),
+ UBUS_FIELD(INT32, "id"),
+ UBUS_FIELD(STRING, "msg"),
+ UBUS_TABLE_END(),
UBUS_METHOD_END(),
};
static struct ubus_object_type test_object_type =
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 = {
.name = "test",
.type = &test_object_type,
+ .methods = test_methods,
+ .n_methods = ARRAY_SIZE(test_methods),
};
static struct ubus_object test_object2 = {
.name = "test2",
.type = &test_object_type,
+ .methods = test_methods,
+ .n_methods = ARRAY_SIZE(test_methods),
};
int main(int argc, char **argv)
UBUS_STATUS_OK,
UBUS_STATUS_INVALID_COMMAND,
UBUS_STATUS_INVALID_ARGUMENT,
+ UBUS_STATUS_METHOD_NOT_FOUND,
UBUS_STATUS_NOT_FOUND,
UBUS_STATUS_NO_DATA,
__UBUS_STATUS_LAST