2 * Copyright (C) 2011-2014 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.
17 #include <libubox/blobmsg_json.h>
21 static struct ubus_context
*ctx
;
22 static struct ubus_subscriber test_event
;
23 static struct blob_buf b
;
31 static const struct blobmsg_policy hello_policy
[] = {
32 [HELLO_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
33 [HELLO_MSG
] = { .name
= "msg", .type
= BLOBMSG_TYPE_STRING
},
36 struct hello_request
{
37 struct ubus_request_data req
;
38 struct uloop_timeout timeout
;
44 static void test_hello_fd_reply(struct uloop_timeout
*t
)
46 struct hello_request
*req
= container_of(t
, struct hello_request
, timeout
);
49 data
= alloca(strlen(req
->data
) + 32);
50 sprintf(data
, "msg%d: %s\n", ++req
->idx
, req
->data
);
51 if (write(req
->fd
, data
, strlen(data
)) < 0) {
57 uloop_timeout_set(&req
->timeout
, 1000);
60 static void test_hello_reply(struct uloop_timeout
*t
)
62 struct hello_request
*req
= container_of(t
, struct hello_request
, timeout
);
66 blobmsg_add_string(&b
, "message", req
->data
);
67 ubus_send_reply(ctx
, &req
->req
, b
.head
);
69 if (pipe(fds
) == -1) {
70 fprintf(stderr
, "Failed to create pipe\n");
73 ubus_request_set_fd(ctx
, &req
->req
, fds
[0]);
74 ubus_complete_deferred_request(ctx
, &req
->req
, 0);
77 req
->timeout
.cb
= test_hello_fd_reply
;
78 test_hello_fd_reply(t
);
81 static int test_hello(struct ubus_context
*ctx
, struct ubus_object
*obj
,
82 struct ubus_request_data
*req
, const char *method
,
83 struct blob_attr
*msg
)
85 struct hello_request
*hreq
;
86 struct blob_attr
*tb
[__HELLO_MAX
];
87 const char *format
= "%s received a message: %s";
88 const char *msgstr
= "(unknown)";
90 blobmsg_parse(hello_policy
, ARRAY_SIZE(hello_policy
), tb
, blob_data(msg
), blob_len(msg
));
93 msgstr
= blobmsg_data(tb
[HELLO_MSG
]);
95 hreq
= calloc(1, sizeof(*hreq
) + strlen(format
) + strlen(obj
->name
) + strlen(msgstr
) + 1);
96 sprintf(hreq
->data
, format
, obj
->name
, msgstr
);
97 ubus_defer_request(ctx
, req
, &hreq
->req
);
98 hreq
->timeout
.cb
= test_hello_reply
;
99 uloop_timeout_set(&hreq
->timeout
, 1000);
110 static const struct blobmsg_policy watch_policy
[__WATCH_MAX
] = {
111 [WATCH_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
112 [WATCH_COUNTER
] = { .name
= "counter", .type
= BLOBMSG_TYPE_INT32
},
116 test_handle_remove(struct ubus_context
*ctx
, struct ubus_subscriber
*s
,
119 fprintf(stderr
, "Object %08x went away\n", id
);
123 test_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
124 struct ubus_request_data
*req
, const char *method
,
125 struct blob_attr
*msg
)
130 str
= blobmsg_format_json(msg
, true);
131 fprintf(stderr
, "Received notification '%s': %s\n", method
, str
);
138 static int test_watch(struct ubus_context
*ctx
, struct ubus_object
*obj
,
139 struct ubus_request_data
*req
, const char *method
,
140 struct blob_attr
*msg
)
142 struct blob_attr
*tb
[__WATCH_MAX
];
145 blobmsg_parse(watch_policy
, __WATCH_MAX
, tb
, blob_data(msg
), blob_len(msg
));
147 return UBUS_STATUS_INVALID_ARGUMENT
;
149 test_event
.remove_cb
= test_handle_remove
;
150 test_event
.cb
= test_notify
;
151 ret
= ubus_subscribe(ctx
, &test_event
, blobmsg_get_u32(tb
[WATCH_ID
]));
152 fprintf(stderr
, "Watching object %08x: %s\n", blobmsg_get_u32(tb
[WATCH_ID
]), ubus_strerror(ret
));
162 static const struct blobmsg_policy count_policy
[__COUNT_MAX
] = {
163 [COUNT_TO
] = { .name
= "to", .type
= BLOBMSG_TYPE_INT32
},
164 [COUNT_STRING
] = { .name
= "string", .type
= BLOBMSG_TYPE_STRING
},
167 static int test_count(struct ubus_context
*ctx
, struct ubus_object
*obj
,
168 struct ubus_request_data
*req
, const char *method
,
169 struct blob_attr
*msg
)
171 struct blob_attr
*tb
[__COUNT_MAX
];
175 blobmsg_parse(count_policy
, __COUNT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
176 if (!tb
[COUNT_TO
] || !tb
[COUNT_STRING
])
177 return UBUS_STATUS_INVALID_ARGUMENT
;
179 num
= blobmsg_get_u32(tb
[COUNT_TO
]);
180 s1
= blobmsg_get_string(tb
[COUNT_STRING
]);
181 s2
= count_to_number(num
);
184 return UBUS_STATUS_UNKNOWN_ERROR
;
186 blob_buf_init(&b
, 0);
187 blobmsg_add_u32(&b
, "rc", strcmp(s1
, s2
));
188 ubus_send_reply(ctx
, req
, b
.head
);
194 static const struct ubus_method test_methods
[] = {
195 UBUS_METHOD("hello", test_hello
, hello_policy
),
196 UBUS_METHOD("watch", test_watch
, watch_policy
),
197 UBUS_METHOD("count", test_count
, count_policy
),
200 static struct ubus_object_type test_object_type
=
201 UBUS_OBJECT_TYPE("test", test_methods
);
203 static struct ubus_object test_object
= {
205 .type
= &test_object_type
,
206 .methods
= test_methods
,
207 .n_methods
= ARRAY_SIZE(test_methods
),
210 static void server_main(void)
214 ret
= ubus_add_object(ctx
, &test_object
);
216 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
218 ret
= ubus_register_subscriber(ctx
, &test_event
);
220 fprintf(stderr
, "Failed to add watch handler: %s\n", ubus_strerror(ret
));
225 int main(int argc
, char **argv
)
227 const char *ubus_socket
= NULL
;
230 while ((ch
= getopt(argc
, argv
, "cs:")) != -1) {
233 ubus_socket
= optarg
;
244 signal(SIGPIPE
, SIG_IGN
);
246 ctx
= ubus_connect(ubus_socket
);
248 fprintf(stderr
, "Failed to connect to ubus\n");