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