cmake: Add ubox, blobmsg_json libraries and include dirs lookup
[project/ubus.git] / libubus-sub.c
1 /*
2 * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include "libubus.h"
15 #include "libubus-internal.h"
16
17 static int ubus_subscriber_cb(struct ubus_context *ctx, struct ubus_object *obj,
18 struct ubus_request_data *req,
19 const char *method, struct blob_attr *msg)
20 {
21 struct ubus_subscriber *s;
22
23 s = container_of(obj, struct ubus_subscriber, obj);
24 if (s->cb)
25 return s->cb(ctx, obj, req, method, msg);
26 return 0;
27 }
28
29 const struct ubus_method watch_method __hidden = {
30 .name = NULL,
31 .handler = ubus_subscriber_cb,
32 };
33
34 int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
35 {
36 struct ubus_object *obj = &s->obj;
37
38 obj->methods = &watch_method;
39 obj->n_methods = 1;
40
41 return ubus_add_object(ctx, obj);
42 }
43
44 static int
45 __ubus_subscribe_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, int type)
46 {
47 struct ubus_request req;
48
49 blob_buf_init(&b, 0);
50 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
51 blob_put_int32(&b, UBUS_ATTR_TARGET, id);
52
53 if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
54 return UBUS_STATUS_INVALID_ARGUMENT;
55
56 return ubus_complete_request(ctx, &req, 0);
57
58 }
59
60 int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
61 {
62 return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_SUBSCRIBE);
63 }
64
65 int ubus_unsubscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
66 {
67 return __ubus_subscribe_request(ctx, &obj->obj, id, UBUS_MSG_UNSUBSCRIBE);
68 }
69