2 * Copyright (C) 2011-2012 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.
15 #include "libubus-internal.h"
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
)
21 struct ubus_subscriber
*s
;
23 s
= container_of(obj
, struct ubus_subscriber
, obj
);
24 s
->cb(ctx
, obj
, req
, method
, msg
);
28 static const struct ubus_method watch_method
= {
30 .handler
= ubus_subscriber_cb
,
33 int ubus_register_subscriber(struct ubus_context
*ctx
, struct ubus_subscriber
*s
)
35 struct ubus_object
*obj
= &s
->obj
;
37 obj
->methods
= &watch_method
;
40 return ubus_add_object(ctx
, obj
);
44 __ubus_subscribe_request(struct ubus_context
*ctx
, struct ubus_object
*obj
, uint32_t id
, const char *method
, int type
)
46 struct ubus_request req
;
49 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
50 blob_put_int32(&b
, UBUS_ATTR_TARGET
, id
);
52 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
54 if (ubus_start_request(ctx
, &req
, b
.head
, type
, 0) < 0)
55 return UBUS_STATUS_INVALID_ARGUMENT
;
57 return ubus_complete_request(ctx
, &req
, 0);
61 int ubus_subscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
)
63 return __ubus_subscribe_request(ctx
, &obj
->obj
, id
, "event", UBUS_MSG_SUBSCRIBE
);
66 int ubus_unsubscribe(struct ubus_context
*ctx
, struct ubus_subscriber
*obj
, uint32_t id
)
68 return __ubus_subscribe_request(ctx
, &obj
->obj
, id
, NULL
, UBUS_MSG_UNSUBSCRIBE
);
71 void __hidden
ubus_process_unsubscribe(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
73 struct ubus_subscriber
*s
;
74 struct blob_attr
**attrbuf
;
75 struct ubus_object
*obj
;
78 attrbuf
= ubus_parse_msg(hdr
->data
);
79 if (!attrbuf
[UBUS_ATTR_OBJID
] || !attrbuf
[UBUS_ATTR_TARGET
])
82 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
83 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
87 if (obj
->methods
!= &watch_method
)
90 s
= container_of(obj
, struct ubus_subscriber
, obj
);
91 s
->remove_cb(ctx
, s
, blob_get_u32(attrbuf
[UBUS_ATTR_TARGET
]));
94 void __hidden
ubus_process_notify(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
96 struct blob_attr
**attrbuf
;
97 struct ubus_object
*obj
;
100 attrbuf
= ubus_parse_msg(hdr
->data
);
101 if (!attrbuf
[UBUS_ATTR_OBJID
] || !attrbuf
[UBUS_ATTR_ACTIVE
])
104 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
105 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
109 obj
->has_subscribers
= blob_get_u8(attrbuf
[UBUS_ATTR_ACTIVE
]);
110 if (obj
->subscribe_cb
)
111 obj
->subscribe_cb(ctx
, obj
);