2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
18 static struct ubus_context *ctx;
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 },
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)
36 struct blob_attr *tb[HELLO_LAST];
37 char *msgstr = "(unknown)";
40 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
43 msgstr = blobmsg_data(tb[HELLO_MSG]);
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);
53 static const struct ubus_method test_methods[] = {
54 UBUS_METHOD("hello", test_hello, hello_policy),
57 static struct ubus_object_type test_object_type =
58 UBUS_OBJECT_TYPE("test", test_methods);
60 static struct ubus_object test_object = {
62 .type = &test_object_type,
63 .methods = test_methods,
64 .n_methods = ARRAY_SIZE(test_methods),
67 static struct ubus_object test_object2 = {
69 .type = &test_object_type,
70 .methods = test_methods,
71 .n_methods = ARRAY_SIZE(test_methods),
74 int main(int argc, char **argv)
76 const char *ubus_socket = NULL;
80 while ((ch = getopt(argc, argv, "s:")) != -1) {
93 ctx = ubus_connect(ubus_socket);
95 fprintf(stderr, "Failed to connect to ubus\n");
99 ret = ubus_add_object(ctx, &test_object);
101 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
103 ret = ubus_add_object(ctx, &test_object2);
105 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));