5 static struct ubus_context *ctx;
8 static const struct ubus_signature test_object_sig[] = {
9 UBUS_METHOD_START("hello"),
11 UBUS_TABLE_START(NULL),
12 UBUS_FIELD(INT32, "id"),
13 UBUS_FIELD(STRING, "msg"),
18 static struct ubus_object_type test_object_type =
19 UBUS_OBJECT_TYPE("test", test_object_sig);
27 static const struct blobmsg_policy hello_policy[] = {
28 [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
29 [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
32 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
33 struct ubus_request_data *req, const char *method,
34 struct blob_attr *msg)
36 struct blob_attr *tb[HELLO_LAST];
37 char *msgstr = "(unknown)";
40 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
43 msgstr = blobmsg_data(tb[HELLO_MSG]);
46 strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
47 sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
48 blobmsg_add_string_buffer(&b);
49 ubus_send_reply(ctx, req, b.head);
53 static const struct ubus_method test_methods[] = {
54 { .name = "hello", .handler = test_hello },
57 static struct ubus_object test_object = {
59 .type = &test_object_type,
60 .methods = test_methods,
61 .n_methods = ARRAY_SIZE(test_methods),
64 static struct ubus_object test_object2 = {
66 .type = &test_object_type,
67 .methods = test_methods,
68 .n_methods = ARRAY_SIZE(test_methods),
71 int main(int argc, char **argv)
73 const char *progname, *ubus_socket = NULL;
79 while ((ch = getopt(argc, argv, "s:")) != -1) {
92 ctx = ubus_connect(ubus_socket);
94 fprintf(stderr, "Failed to connect to ubus\n");
98 ret = ubus_add_object(ctx, &test_object);
100 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
102 ret = ubus_add_object(ctx, &test_object2);
104 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));