17 #include <libubox/blobmsg_json.h>
23 static struct blob_buf
b;
32 [
HELLO_ID] = { .name =
"id", .type = BLOBMSG_TYPE_INT32 },
33 [
HELLO_MSG] = { .name =
"msg", .type = BLOBMSG_TYPE_STRING },
49 data = alloca(strlen(
req->data) + 32);
50 sprintf(
data,
"msg%d: %s\n", ++
req->idx,
req->data);
57 uloop_timeout_set(&
req->timeout, 1000);
66 blobmsg_add_string(&
b,
"message",
req->data);
69 if (pipe(fds) == -1) {
70 fprintf(stderr,
"Failed to create pipe\n");
83 struct blob_attr *msg)
87 const char format[] =
"%s received a message: %s";
88 const char *msgstr =
"(unknown)";
95 size_t len =
sizeof(*hreq) +
sizeof(format) + strlen(obj->
name) + strlen(msgstr) + 1;
96 hreq = calloc(1, len);
100 snprintf(hreq->
data, len, format, obj->
name, msgstr);
103 uloop_timeout_set(&hreq->
timeout, 1000);
115 [
WATCH_ID] = { .name =
"id", .type = BLOBMSG_TYPE_INT32 },
116 [
WATCH_COUNTER] = { .name =
"counter", .type = BLOBMSG_TYPE_INT32 },
123 fprintf(stderr,
"Object %08x went away\n",
id);
129 struct blob_attr *msg)
134 str = blobmsg_format_json(msg,
true);
135 fprintf(stderr,
"Received notification '%s': %s\n", method, str);
144 struct blob_attr *msg)
167 [
COUNT_TO] = { .name =
"to", .type = BLOBMSG_TYPE_INT32 },
168 [
COUNT_STRING] = { .name =
"string", .type = BLOBMSG_TYPE_STRING },
173 struct blob_attr *msg)
183 num = blobmsg_get_u32(tb[
COUNT_TO]);
190 blob_buf_init(&
b, 0);
191 blobmsg_add_u32(&
b,
"rc", strcmp(s1, s2));
220 fprintf(stderr,
"Failed to add object: %s\n",
ubus_strerror(ret));
224 fprintf(stderr,
"Failed to add watch handler: %s\n",
ubus_strerror(ret));
229 int main(
int argc,
char **argv)
231 const char *ubus_socket = NULL;
234 while ((ch = getopt(argc, argv,
"cs:")) != -1) {
237 ubus_socket = optarg;
245 signal(SIGPIPE, SIG_IGN);
249 fprintf(stderr,
"Failed to connect to ubus\n");
char * count_to_number(uint32_t num)
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, struct blob_attr *msg)
int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
struct ubus_context * ubus_connect(const char *path)
void ubus_free(struct ubus_context *ctx)
const char * ubus_strerror(int error)
static void ubus_defer_request(struct ubus_context *ctx, struct ubus_request_data *req, struct ubus_request_data *new_req)
#define UBUS_OBJECT_TYPE(_name, _methods)
static void ubus_request_set_fd(struct ubus_context *ctx, struct ubus_request_data *req, int fd)
static void ubus_add_uloop(struct ubus_context *ctx)
#define UBUS_METHOD(_name, _handler, _policy)
static struct ubus_object_type test_object_type
static const struct blobmsg_policy count_policy[__COUNT_MAX]
int main(int argc, char **argv)
static void test_hello_fd_reply(struct uloop_timeout *t)
static struct ubus_object test_object
static struct ubus_context * ctx
static int test_count(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
static const struct blobmsg_policy watch_policy[__WATCH_MAX]
static int test_notify(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
static struct ubus_subscriber test_event
static const struct blobmsg_policy hello_policy[]
static const struct ubus_method test_methods[]
static void server_main(void)
static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
static void test_hello_reply(struct uloop_timeout *t)
static int test_watch(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
static void test_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id)
struct ubus_request_data req
struct uloop_timeout timeout
const struct ubus_method * methods
ubus_remove_handler_t remove_cb
@ UBUS_STATUS_INVALID_ARGUMENT
@ UBUS_STATUS_UNKNOWN_ERROR