ADD_DEFINITIONS(-I..)
ADD_EXECUTABLE(server server.c)
-TARGET_LINK_LIBRARIES(server ubus ubox)
+TARGET_LINK_LIBRARIES(server ubus ubox blobmsg_json)
ADD_EXECUTABLE(client client.c)
TARGET_LINK_LIBRARIES(client ubus ubox)
.subscribe_cb = test_client_subscribe_cb,
};
+static void test_client_notify_cb(struct uloop_timeout *timeout)
+{
+ static int counter = 0;
+ int err;
+
+ blob_buf_init(&b, 0);
+ blobmsg_add_u32(&b, "counter", counter++);
+
+ err = ubus_notify(ctx, &test_client_object, "ping", b.head, 1000);
+ if (err)
+ fprintf(stderr, "Notify failed: %s\n", ubus_strerror(err));
+
+ uloop_timeout_set(timeout, 1000);
+}
+
+static struct uloop_timeout notify_timer = {
+ .cb = test_client_notify_cb,
+};
+
static void client_main(void)
{
uint32_t id;
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "id", test_client_object.id);
ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
+ test_client_notify_cb(¬ify_timer);
uloop_run();
}
#include <unistd.h>
+#include <libubox/blobmsg_json.h>
#include "libubus.h"
static struct ubus_context *ctx;
enum {
WATCH_ID,
+ WATCH_COUNTER,
__WATCH_MAX
};
static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
[WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
+ [WATCH_COUNTER] = { .name = "counter", .type = BLOBMSG_TYPE_INT32 },
};
static void
fprintf(stderr, "Object %08x went away\n", id);
}
+static int
+test_notify(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ char *str;
+
+ str = blobmsg_format_json(msg, true);
+ fprintf(stderr, "Received notification '%s': %s\n", method, str);
+ free(str);
+
+ return 0;
+}
+
static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
return UBUS_STATUS_INVALID_ARGUMENT;
test_event.remove_cb = test_handle_remove;
+ test_event.cb = test_notify;
ret = ubus_subscribe(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
return ret;