add copyright/license information
[project/ubus.git] / ubus-example.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15
16 #include "libubus.h"
17
18 static struct ubus_context *ctx;
19 struct blob_buf b;
20
21 enum {
22         HELLO_ID,
23         HELLO_MSG,
24         HELLO_LAST
25 };
26
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 },
30 };
31
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)
35 {
36         struct blob_attr *tb[HELLO_LAST];
37         char *msgstr = "(unknown)";
38         char *strbuf;
39
40         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
41
42         if (tb[HELLO_MSG])
43                 msgstr = blobmsg_data(tb[HELLO_MSG]);
44
45         blob_buf_init(&b, 0);
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);
50         return 0;
51 }
52
53 static const struct ubus_method test_methods[] = {
54         UBUS_METHOD("hello", test_hello, hello_policy),
55 };
56
57 static struct ubus_object_type test_object_type =
58         UBUS_OBJECT_TYPE("test", test_methods);
59
60 static struct ubus_object test_object = {
61         .name = "test",
62         .type = &test_object_type,
63         .methods = test_methods,
64         .n_methods = ARRAY_SIZE(test_methods),
65 };
66
67 static struct ubus_object test_object2 = {
68         .name = "test2",
69         .type = &test_object_type,
70         .methods = test_methods,
71         .n_methods = ARRAY_SIZE(test_methods),
72 };
73
74 int main(int argc, char **argv)
75 {
76         const char *progname, *ubus_socket = NULL;
77         int ret = 0;
78         int ch;
79
80         progname = argv[0];
81
82         while ((ch = getopt(argc, argv, "s:")) != -1) {
83                 switch (ch) {
84                 case 's':
85                         ubus_socket = optarg;
86                         break;
87                 default:
88                         break;
89                 }
90         }
91
92         argc -= optind;
93         argv += optind;
94
95         ctx = ubus_connect(ubus_socket);
96         if (!ctx) {
97                 fprintf(stderr, "Failed to connect to ubus\n");
98                 return -1;
99         }
100
101         ret = ubus_add_object(ctx, &test_object);
102         if (ret)
103                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
104
105         ret = ubus_add_object(ctx, &test_object2);
106         if (ret)
107                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
108
109         uloop_init();
110         ubus_add_uloop(ctx);
111         uloop_run();
112         uloop_done();
113
114         ubus_free(ctx);
115         return 0;
116 }