Initial import
[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 struct ubus_object test_object = {
19         .name = "test",
20         .type = &test_object_type,
21 };
22
23 static struct ubus_object test_object2 = {
24         .name = "test2",
25         .type = &test_object_type,
26 };
27
28 int main(int argc, char **argv)
29 {
30         int ret;
31
32         ctx = ubus_connect(NULL);
33         if (!ctx) {
34                 fprintf(stderr, "Failed to connect to ubus\n");
35                 return -1;
36         }
37
38         fprintf(stderr, "Connected as ID 0x%08x\n", ctx->local_id);
39
40         fprintf(stderr, "Publishing object\n");
41         ret = ubus_publish(ctx, &test_object);
42         if (ret)
43                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
44         else {
45                 fprintf(stderr, "Object ID: %08x\n", test_object.id);
46                 fprintf(stderr, "Object Type ID: %08x\n", test_object.type->id);
47         }
48
49         fprintf(stderr, "Publishing object\n");
50         ret = ubus_publish(ctx, &test_object2);
51         if (ret)
52                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
53         else {
54                 fprintf(stderr, "Object ID: %08x\n", test_object2.id);
55                 fprintf(stderr, "Object Type ID: %08x\n", test_object2.type->id);
56         }
57         uloop_init();
58         uloop_fd_add(&ctx->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
59         uloop_run();
60
61         ubus_free(ctx);
62         return 0;
63 }