libubus: add method to policy
[project/ubus.git] / listener.c
1 #include "libubus.h"
2
3 static struct ubus_context *ctx;
4
5 static const struct ubus_signature test_object_sig[] = {
6         UBUS_METHOD_START("hello"),
7           UBUS_ARRAY("test"),
8                 UBUS_TABLE_START(NULL),
9                   UBUS_FIELD(INT32, "id"),
10                   UBUS_FIELD(STRING, "msg"),
11                 UBUS_TABLE_END(),
12         UBUS_METHOD_END(),
13 };
14
15 static struct ubus_object_type test_object_type =
16         UBUS_OBJECT_TYPE("test", test_object_sig);
17
18 static int test_hello(struct ubus_object *obj, struct ubus_request_data *req,
19                           const char *method, struct blob_attr *msg)
20 {
21         fprintf(stderr, "Hello, world!\n");
22         return 0;
23 }
24
25 static const struct ubus_method test_methods[] = {
26         { .name = "hello", .handler = test_hello },
27 };
28
29 static struct ubus_object test_object = {
30         .name = "test",
31         .type = &test_object_type,
32         .methods = test_methods,
33         .n_methods = ARRAY_SIZE(test_methods),
34 };
35
36 static struct ubus_object test_object2 = {
37         .name = "test2",
38         .type = &test_object_type,
39         .methods = test_methods,
40         .n_methods = ARRAY_SIZE(test_methods),
41 };
42
43 int main(int argc, char **argv)
44 {
45         int ret;
46
47         ctx = ubus_connect(NULL);
48         if (!ctx) {
49                 fprintf(stderr, "Failed to connect to ubus\n");
50                 return -1;
51         }
52
53         fprintf(stderr, "Connected as ID 0x%08x\n", ctx->local_id);
54
55         fprintf(stderr, "Publishing object\n");
56         ret = ubus_publish(ctx, &test_object);
57         if (ret)
58                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
59         else {
60                 fprintf(stderr, "Object ID: %08x\n", test_object.id);
61                 fprintf(stderr, "Object Type ID: %08x\n", test_object.type->id);
62         }
63
64         fprintf(stderr, "Publishing object\n");
65         ret = ubus_publish(ctx, &test_object2);
66         if (ret)
67                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
68         else {
69                 fprintf(stderr, "Object ID: %08x\n", test_object2.id);
70                 fprintf(stderr, "Object Type ID: %08x\n", test_object2.type->id);
71         }
72         uloop_init();
73         uloop_fd_add(&ctx->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
74         uloop_run();
75
76         ubus_free(ctx);
77         return 0;
78 }