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
;
19 static struct ubus_subscriber test_event
;
20 static struct blob_buf b
;
28 static const struct blobmsg_policy hello_policy
[] = {
29 [HELLO_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
30 [HELLO_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_STRING
},
33 struct hello_request
{
34 struct ubus_request_data req
;
35 struct uloop_timeout timeout
;
39 static void test_hello_reply(struct uloop_timeout
*t
)
41 struct hello_request
*req
= container_of(t
, struct hello_request
, timeout
);
44 blobmsg_add_string(&b
, "message", req
->data
);
45 ubus_send_reply(ctx
, &req
->req
, b
.head
);
46 ubus_complete_deferred_request(ctx
, &req
->req
, 0);
50 static int test_hello(struct ubus_context
*ctx
, struct ubus_object
*obj
,
51 struct ubus_request_data
*req
, const char *method
,
52 struct blob_attr
*msg
)
54 struct hello_request
*hreq
;
55 struct blob_attr
*tb
[__HELLO_MAX
];
56 const char *format
= "%s received a message: %s";
57 const char *msgstr
= "(unknown)";
59 blobmsg_parse(hello_policy
, ARRAY_SIZE(hello_policy
), tb
, blob_data(msg
), blob_len(msg
));
62 msgstr
= blobmsg_data(tb
[HELLO_MSG
]);
64 hreq
= calloc(1, sizeof(*hreq
) + strlen(format
) + strlen(obj
->name
) + strlen(msgstr
) + 1);
65 sprintf(hreq
->data
, format
, obj
->name
, msgstr
);
66 ubus_defer_request(ctx
, req
, &hreq
->req
);
67 hreq
->timeout
.cb
= test_hello_reply
;
68 uloop_timeout_set(&hreq
->timeout
, 1000);
78 static const struct blobmsg_policy watch_policy
[__WATCH_MAX
] = {
79 [WATCH_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
83 test_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
86 fprintf(stderr
, "Object %08x went away\n", id
);
89 static int test_watch(struct ubus_context
*ctx
, struct ubus_object
*obj
,
90 struct ubus_request_data
*req
, const char *method
,
91 struct blob_attr
*msg
)
93 struct blob_attr
*tb
[__WATCH_MAX
];
96 blobmsg_parse(watch_policy
, __WATCH_MAX
, tb
, blob_data(msg
), blob_len(msg
));
98 return UBUS_STATUS_INVALID_ARGUMENT
;
100 test_event
.remove_cb
= test_handle_remove
;
101 ret
= ubus_subscribe(ctx
, &test_event
, blobmsg_get_u32(tb
[WATCH_ID
]));
102 fprintf(stderr
, "Watching object %08x: %s\n", blobmsg_get_u32(tb
[WATCH_ID
]), ubus_strerror(ret
));
106 static const struct ubus_method test_methods
[] = {
107 UBUS_METHOD("hello", test_hello
, hello_policy
),
108 UBUS_METHOD("watch", test_watch
, watch_policy
),
111 static struct ubus_object_type test_object_type
=
112 UBUS_OBJECT_TYPE("test", test_methods
);
114 static struct ubus_object test_object
= {
116 .type
= &test_object_type
,
117 .methods
= test_methods
,
118 .n_methods
= ARRAY_SIZE(test_methods
),
121 static void server_main(void)
125 ret
= ubus_add_object(ctx
, &test_object
);
127 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
129 ret
= ubus_register_subscriber(ctx
, &test_event
);
131 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
136 int main(int argc
, char **argv
)
138 const char *ubus_socket
= NULL
;
141 while ((ch
= getopt(argc
, argv
, "cs:")) != -1) {
144 ubus_socket
= optarg
;
156 ctx
= ubus_connect(ubus_socket
);
158 fprintf(stderr
, "Failed to connect to ubus\n");