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.
17 #include <libubox/ustream.h>
22 static struct ubus_context
*ctx
;
23 static struct blob_buf b
;
25 static void test_client_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
27 fprintf(stderr
, "Subscribers active: %d\n", obj
->has_subscribers
);
30 static struct ubus_object test_client_object
= {
31 .subscribe_cb
= test_client_subscribe_cb
,
34 static void test_client_notify_cb(struct uloop_timeout
*timeout
)
36 static int counter
= 0;
38 struct timeval tv1
, tv2
;
44 blobmsg_add_u32(&b
, "counter", counter
++);
46 gettimeofday(&tv1
, NULL
);
47 for (i
= 0; i
< max
; i
++)
48 err
= ubus_notify(ctx
, &test_client_object
, "ping", b
.head
, 1000);
49 gettimeofday(&tv2
, NULL
);
51 fprintf(stderr
, "Notify failed: %s\n", ubus_strerror(err
));
53 delta
= (tv2
.tv_sec
- tv1
.tv_sec
) * 1000000 + (tv2
.tv_usec
- tv1
.tv_usec
);
54 fprintf(stderr
, "Avg time per iteration: %ld usec\n", delta
/ max
);
56 uloop_timeout_set(timeout
, 1000);
64 static const struct blobmsg_policy return_policy
[__RETURN_MAX
] = {
65 [RETURN_CODE
] = { .name
= "rc", .type
= BLOBMSG_TYPE_INT32
},
68 static void test_count_data_cb(struct ubus_request
*req
,
69 int type
, struct blob_attr
*msg
)
71 struct blob_attr
*tb
[__RETURN_MAX
];
73 uint32_t count_to
= *(uint32_t *)req
->priv
;
75 blobmsg_parse(return_policy
, __RETURN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
77 if (!tb
[RETURN_CODE
]) {
78 fprintf(stderr
, "No return code received from server\n");
81 rc
= blobmsg_get_u32(tb
[RETURN_CODE
]);
83 fprintf(stderr
, "Corruption of data with count up to '%u'\n", count_to
);
85 fprintf(stderr
, "Server validated our count up to '%u'\n", count_to
);
88 static void test_count(struct uloop_timeout
*timeout
)
92 COUNT_TO_MAX
= 1000000,
97 static uint32_t count_to
= 100000;
98 static int count_progression
= PROGRESSION
;
101 if (count_to
<= COUNT_TO_MIN
)
102 count_progression
= PROGRESSION
;
103 else if (count_to
>= COUNT_TO_MAX
)
104 count_progression
= -PROGRESSION
;
106 count_to
+= count_progression
;
108 s
= count_to_number(count_to
);
110 fprintf(stderr
, "Could not allocate memory to count up to '%u'\n", count_to
);
114 fprintf(stderr
, "Sending count up to '%u'; string has length '%u'\n",
115 count_to
, (uint32_t)strlen(s
));
116 blob_buf_init(&b
, 0);
117 blobmsg_add_u32(&b
, "to", count_to
);
118 blobmsg_add_string(&b
, "string", s
);
120 if (ubus_lookup_id(ctx
, "test", &id
)) {
121 fprintf(stderr
, "Failed to look up test object\n");
125 ubus_invoke(ctx
, id
, "count", b
.head
, test_count_data_cb
, &count_to
, 5000);
129 uloop_timeout_set(timeout
, 2000);
132 static struct uloop_timeout notify_timer
= {
133 .cb
= test_client_notify_cb
,
136 static struct uloop_timeout count_timer
= {
140 static void test_client_fd_data_cb(struct ustream
*s
, int bytes
)
145 data
= ustream_get_read_buf(s
, &len
);
149 sep
= strchr(data
, '\n');
154 fprintf(stderr
, "Got line: %s\n", data
);
155 ustream_consume(s
, sep
+ 1 - data
);
158 static void test_client_fd_cb(struct ubus_request
*req
, int fd
)
160 static struct ustream_fd test_fd
;
162 fprintf(stderr
, "Got fd from the server, watching...\n");
164 test_fd
.stream
.notify_read
= test_client_fd_data_cb
;
165 ustream_fd_init(&test_fd
, fd
);
168 static void test_client_complete_cb(struct ubus_request
*req
, int ret
)
170 fprintf(stderr
, "completed request, ret: %d\n", ret
);
173 static void client_main(void)
175 static struct ubus_request req
;
179 ret
= ubus_add_object(ctx
, &test_client_object
);
181 fprintf(stderr
, "Failed to add_object object: %s\n", ubus_strerror(ret
));
185 if (ubus_lookup_id(ctx
, "test", &id
)) {
186 fprintf(stderr
, "Failed to look up test object\n");
190 blob_buf_init(&b
, 0);
191 blobmsg_add_u32(&b
, "id", test_client_object
.id
);
192 ubus_invoke(ctx
, id
, "watch", b
.head
, NULL
, 0, 3000);
193 test_client_notify_cb(¬ify_timer
);
195 blob_buf_init(&b
, 0);
196 blobmsg_add_string(&b
, "msg", "blah");
197 ubus_invoke_async(ctx
, id
, "hello", b
.head
, &req
);
198 req
.fd_cb
= test_client_fd_cb
;
199 req
.complete_cb
= test_client_complete_cb
;
200 ubus_complete_request_async(ctx
, &req
);
202 uloop_timeout_set(&count_timer
, 2000);
207 int main(int argc
, char **argv
)
209 const char *ubus_socket
= NULL
;
212 while ((ch
= getopt(argc
, argv
, "cs:")) != -1) {
215 ubus_socket
= optarg
;
227 ctx
= ubus_connect(ubus_socket
);
229 fprintf(stderr
, "Failed to connect to ubus\n");