add support for overriding the socket name
[project/ubus.git] / ubus-example.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 enum {
20         HELLO_ID,
21         HELLO_MSG,
22         HELLO_LAST
23 };
24
25 static const struct blobmsg_policy hello_policy[] = {
26         [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
27         [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
28 };
29
30 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
31                       struct ubus_request_data *req, const char *method,
32                       struct blob_attr *msg)
33 {
34         struct blob_attr *tb[HELLO_LAST];
35         char *msgstr = "(unknown)";
36         char *strbuf;
37
38         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
39
40         if (tb[HELLO_MSG])
41                 msgstr = blobmsg_data(tb[HELLO_MSG]);
42
43         blob_buf_init(&b, 0);
44         strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
45         sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
46         blobmsg_add_string_buffer(&b);
47         ubus_send_reply(ctx, req, b.head);
48         return 0;
49 }
50
51 static const struct ubus_method test_methods[] = {
52         { .name = "hello", .handler = test_hello },
53 };
54
55 static struct ubus_object test_object = {
56         .name = "test",
57         .type = &test_object_type,
58         .methods = test_methods,
59         .n_methods = ARRAY_SIZE(test_methods),
60 };
61
62 static struct ubus_object test_object2 = {
63         .name = "test2",
64         .type = &test_object_type,
65         .methods = test_methods,
66         .n_methods = ARRAY_SIZE(test_methods),
67 };
68
69 int main(int argc, char **argv)
70 {
71         int ret;
72
73         ctx = ubus_connect(NULL);
74         if (!ctx) {
75                 fprintf(stderr, "Failed to connect to ubus\n");
76                 return -1;
77         }
78
79         fprintf(stderr, "Connected as ID 0x%08x\n", ctx->local_id);
80
81         fprintf(stderr, "Publishing object\n");
82         ret = ubus_publish(ctx, &test_object);
83         if (ret)
84                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
85         else {
86                 fprintf(stderr, "Object ID: %08x\n", test_object.id);
87                 fprintf(stderr, "Object Type ID: %08x\n", test_object.type->id);
88         }
89
90         fprintf(stderr, "Publishing object\n");
91         ret = ubus_publish(ctx, &test_object2);
92         if (ret)
93                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
94         else {
95                 fprintf(stderr, "Object ID: %08x\n", test_object2.id);
96                 fprintf(stderr, "Object Type ID: %08x\n", test_object2.type->id);
97         }
98         uloop_init();
99         ubus_add_uloop(ctx);
100         uloop_run();
101         uloop_done();
102
103         ubus_free(ctx);
104         return 0;
105 }