5 static struct ubus_context *ctx;
14 static const struct blobmsg_policy hello_policy[] = {
15 [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
16 [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
19 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
20 struct ubus_request_data *req, const char *method,
21 struct blob_attr *msg)
23 struct blob_attr *tb[HELLO_LAST];
24 char *msgstr = "(unknown)";
27 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
30 msgstr = blobmsg_data(tb[HELLO_MSG]);
33 strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
34 sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
35 blobmsg_add_string_buffer(&b);
36 ubus_send_reply(ctx, req, b.head);
40 static const struct ubus_method test_methods[] = {
41 UBUS_METHOD("hello", test_hello, hello_policy),
44 static struct ubus_object_type test_object_type =
45 UBUS_OBJECT_TYPE("test", test_methods);
47 static struct ubus_object test_object = {
49 .type = &test_object_type,
50 .methods = test_methods,
51 .n_methods = ARRAY_SIZE(test_methods),
54 static struct ubus_object test_object2 = {
56 .type = &test_object_type,
57 .methods = test_methods,
58 .n_methods = ARRAY_SIZE(test_methods),
61 int main(int argc, char **argv)
63 const char *progname, *ubus_socket = NULL;
69 while ((ch = getopt(argc, argv, "s:")) != -1) {
82 ctx = ubus_connect(ubus_socket);
84 fprintf(stderr, "Failed to connect to ubus\n");
88 ret = ubus_add_object(ctx, &test_object);
90 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
92 ret = ubus_add_object(ctx, &test_object2);
94 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));