3 static struct ubus_context *ctx;
6 static const struct ubus_signature test_object_sig[] = {
7 UBUS_METHOD_START("hello"),
9 UBUS_TABLE_START(NULL),
10 UBUS_FIELD(INT32, "id"),
11 UBUS_FIELD(STRING, "msg"),
16 static struct ubus_object_type test_object_type =
17 UBUS_OBJECT_TYPE("test", test_object_sig);
25 static const struct blobmsg_policy hello_policy[] = {
26 [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
27 [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
30 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
31 struct ubus_request_data *req, const char *method,
32 struct blob_attr *msg)
34 struct blob_attr *tb[HELLO_LAST];
35 char *msgstr = "(unknown)";
38 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
41 msgstr = blobmsg_data(tb[HELLO_MSG]);
44 strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
45 sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
46 blobmsg_add_string_buffer(&b);
47 ubus_send_reply(ctx, req, b.head);
51 static const struct ubus_method test_methods[] = {
52 { .name = "hello", .handler = test_hello },
55 static struct ubus_object test_object = {
57 .type = &test_object_type,
58 .methods = test_methods,
59 .n_methods = ARRAY_SIZE(test_methods),
62 static struct ubus_object test_object2 = {
64 .type = &test_object_type,
65 .methods = test_methods,
66 .n_methods = ARRAY_SIZE(test_methods),
69 int main(int argc, char **argv)
73 ctx = ubus_connect(NULL);
75 fprintf(stderr, "Failed to connect to ubus\n");
79 fprintf(stderr, "Connected as ID 0x%08x\n", ctx->local_id);
81 fprintf(stderr, "Publishing object\n");
82 ret = ubus_publish(ctx, &test_object);
84 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
86 fprintf(stderr, "Object ID: %08x\n", test_object.id);
87 fprintf(stderr, "Object Type ID: %08x\n", test_object.type->id);
90 fprintf(stderr, "Publishing object\n");
91 ret = ubus_publish(ctx, &test_object2);
93 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
95 fprintf(stderr, "Object ID: %08x\n", test_object2.id);
96 fprintf(stderr, "Object Type ID: %08x\n", test_object2.type->id);