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_watch_object test_event
;
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 static int test_hello(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
37 struct blob_attr
*tb
[__HELLO_MAX
];
38 char *msgstr
= "(unknown)";
41 blobmsg_parse(hello_policy
, ARRAY_SIZE(hello_policy
), tb
, blob_data(msg
), blob_len(msg
));
44 msgstr
= blobmsg_data(tb
[HELLO_MSG
]);
47 strbuf
= blobmsg_alloc_string_buffer(&b
, "message", 64 + strlen(obj
->name
) + strlen(msgstr
));
48 sprintf(strbuf
, "%s received a message: %s", obj
->name
, msgstr
);
49 blobmsg_add_string_buffer(&b
);
50 ubus_send_reply(ctx
, req
, b
.head
);
59 static const struct blobmsg_policy watch_policy
[__WATCH_MAX
] = {
60 [WATCH_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
63 static void test_handle_event(struct ubus_context
*ctx
, struct ubus_watch_object
*w
,
66 fprintf(stderr
, "Object %08x went away\n", id
);
69 static int test_watch(struct ubus_context
*ctx
, struct ubus_object
*obj
,
70 struct ubus_request_data
*req
, const char *method
,
71 struct blob_attr
*msg
)
73 struct blob_attr
*tb
[__WATCH_MAX
];
76 blobmsg_parse(watch_policy
, __WATCH_MAX
, tb
, blob_data(msg
), blob_len(msg
));
78 return UBUS_STATUS_INVALID_ARGUMENT
;
80 test_event
.cb
= test_handle_event
;
81 ret
= ubus_watch_object_add(ctx
, &test_event
, blobmsg_get_u32(tb
[WATCH_ID
]));
82 fprintf(stderr
, "Watching object %08x: %s\n", blobmsg_get_u32(tb
[WATCH_ID
]), ubus_strerror(ret
));
86 static const struct ubus_method test_methods
[] = {
87 UBUS_METHOD("hello", test_hello
, hello_policy
),
88 UBUS_METHOD("watch", test_watch
, watch_policy
),
91 static struct ubus_object_type test_object_type
=
92 UBUS_OBJECT_TYPE("test", test_methods
);
94 static struct ubus_object test_object
= {
96 .type
= &test_object_type
,
97 .methods
= test_methods
,
98 .n_methods
= ARRAY_SIZE(test_methods
),
101 static struct ubus_object test_client_object
= {
102 .type
= &test_object_type
,
103 .methods
= test_methods
,
104 .n_methods
= ARRAY_SIZE(test_methods
),
107 static void server_main(void)
111 ret
= ubus_add_object(ctx
, &test_object
);
113 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
115 ret
= ubus_register_watch_object(ctx
, &test_event
);
117 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
122 static void client_main(void)
127 ret
= ubus_add_object(ctx
, &test_client_object
);
129 fprintf(stderr
, "Failed to add_object object: %s\n", ubus_strerror(ret
));
133 if (ubus_lookup_id(ctx
, test_object
.name
, &id
)) {
134 fprintf(stderr
, "Failed to look up test object\n");
138 blob_buf_init(&b
, 0);
139 blobmsg_add_u32(&b
, "id", test_client_object
.id
);
140 ubus_invoke(ctx
, id
, "watch", b
.head
, NULL
, 0, 3000);
144 int main(int argc
, char **argv
)
146 const char *ubus_socket
= NULL
;
150 while ((ch
= getopt(argc
, argv
, "cs:")) != -1) {
153 ubus_socket
= optarg
;
168 ctx
= ubus_connect(ubus_socket
);
170 fprintf(stderr
, "Failed to connect to ubus\n");