5 static struct ubus_context *ctx;
8 static const struct ubus_signature test_object_sig[] = {
9 UBUS_METHOD_START("hello"),
10 UBUS_TABLE_START(NULL),
11 UBUS_FIELD(INT32, "id"),
12 UBUS_FIELD(STRING, "msg"),
17 static struct ubus_object_type test_object_type =
18 UBUS_OBJECT_TYPE("test", test_object_sig);
26 static const struct blobmsg_policy hello_policy[] = {
27 [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
28 [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
31 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
32 struct ubus_request_data *req, const char *method,
33 struct blob_attr *msg)
35 struct blob_attr *tb[HELLO_LAST];
36 char *msgstr = "(unknown)";
39 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
42 msgstr = blobmsg_data(tb[HELLO_MSG]);
45 strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
46 sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
47 blobmsg_add_string_buffer(&b);
48 ubus_send_reply(ctx, req, b.head);
52 static const struct ubus_method test_methods[] = {
53 { .name = "hello", .handler = test_hello },
56 static struct ubus_object test_object = {
58 .type = &test_object_type,
59 .methods = test_methods,
60 .n_methods = ARRAY_SIZE(test_methods),
63 static struct ubus_object test_object2 = {
65 .type = &test_object_type,
66 .methods = test_methods,
67 .n_methods = ARRAY_SIZE(test_methods),
70 int main(int argc, char **argv)
72 const char *progname, *ubus_socket = NULL;
78 while ((ch = getopt(argc, argv, "s:")) != -1) {
91 ctx = ubus_connect(ubus_socket);
93 fprintf(stderr, "Failed to connect to ubus\n");
97 ret = ubus_add_object(ctx, &test_object);
99 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
101 ret = ubus_add_object(ctx, &test_object2);
103 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));