add code for sending events and fix the code for receiving events
[project/ubus.git] / ubusd_event.c
1 #include "ubusd.h"
2
3 static struct avl_tree patterns;
4 static LIST_HEAD(catch_all);
5 static struct ubus_object *event_obj;
6 static int event_seq = 0;
7
8 enum evs_type {
9 EVS_PATTERN,
10 EVS_CATCHALL
11 };
12
13 struct event_source {
14 struct list_head list;
15 struct ubus_object *obj;
16 enum evs_type type;
17 union {
18 struct {
19 struct avl_node avl;
20 } pattern;
21 struct {
22 struct list_head list;
23 } catchall;
24 };
25 };
26
27 struct event_pattern {
28 struct event_source evs;
29 struct list_head list;
30 };
31
32 struct event_catchall {
33 struct event_source evs;
34
35 struct list_head list;
36 struct ubus_object *obj;
37 };
38
39 static void ubusd_delete_event_source(struct event_source *evs)
40 {
41 list_del(&evs->list);
42 switch (evs->type) {
43 case EVS_PATTERN:
44 avl_delete(&patterns, &evs->pattern.avl);
45 break;
46 case EVS_CATCHALL:
47 list_del(&evs->catchall.list);
48 break;
49 }
50 free(evs);
51 }
52
53 void ubusd_event_cleanup_object(struct ubus_object *obj)
54 {
55 struct event_source *ev;
56
57 while (!list_empty(&obj->events)) {
58 ev = list_first_entry(&obj->events, struct event_source, list);
59 ubusd_delete_event_source(ev);
60 }
61 }
62
63 enum {
64 EVREG_PATTERN,
65 EVREG_OBJECT,
66 EVREG_LAST,
67 };
68
69 static struct blobmsg_policy evr_policy[] = {
70 [EVREG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
71 [EVREG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
72 };
73
74
75 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
76 {
77 struct event_source *evs;
78
79 evs = calloc(1, sizeof(*evs) + datalen);
80 list_add(&evs->list, &obj->events);
81 evs->obj = obj;
82 evs->type = type;
83 return evs;
84 }
85
86 static int ubusd_alloc_catchall(struct ubus_object *obj)
87 {
88 struct event_source *evs;
89
90 evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
91 list_add(&evs->catchall.list, &catch_all);
92
93 return 0;
94 }
95
96 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
97 {
98 struct event_source *ev;
99 struct ubus_object *obj;
100 struct blob_attr *attr[EVREG_LAST];
101 const char *pattern;
102 uint32_t id;
103
104 blobmsg_parse(evr_policy, EVREG_LAST, attr, blob_data(msg), blob_len(msg));
105 if (!attr[EVREG_OBJECT])
106 return UBUS_STATUS_INVALID_ARGUMENT;
107
108 id = blobmsg_get_u32(attr[EVREG_OBJECT]);
109 if (id < UBUS_SYSTEM_OBJECT_MAX)
110 return UBUS_STATUS_PERMISSION_DENIED;
111
112 obj = ubusd_find_object(id);
113 if (!obj)
114 return UBUS_STATUS_NOT_FOUND;
115
116 if (obj->client != cl)
117 return UBUS_STATUS_PERMISSION_DENIED;
118
119 if (!attr[EVREG_PATTERN])
120 return ubusd_alloc_catchall(obj);
121
122 pattern = blobmsg_data(attr[EVREG_PATTERN]);
123 ev = ubusd_alloc_event_source(obj, EVS_PATTERN, strlen(pattern) + 1);
124 ev->pattern.avl.key = (void *) (ev + 1);
125 strcpy(ev->pattern.avl.key, pattern);
126 avl_insert(&patterns, &ev->pattern.avl);
127
128 return 0;
129 }
130
131 enum {
132 EVMSG_ID,
133 EVMSG_DATA,
134 EVMSG_LAST,
135 };
136
137 static struct blobmsg_policy ev_policy[] = {
138 [EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
139 [EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
140 };
141
142 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_object *obj,
143 const char *id, struct blob_attr *msg)
144 {
145 uint32_t *objid_ptr;
146
147 if (*ub) {
148 objid_ptr = blob_data(blob_data((*ub)->data));
149 *objid_ptr = htonl(obj->id.id);
150 } else {
151 blob_buf_init(&b, 0);
152 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
153 blob_put_string(&b, UBUS_ATTR_METHOD, id);
154 blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
155
156 *ub = ubus_msg_new(b.head, blob_raw_len(b.head), true);
157
158 (*ub)->hdr.type = UBUS_MSG_INVOKE;
159 (*ub)->hdr.peer = 0;
160 }
161 (*ub)->hdr.seq = ++event_seq;
162 ubus_msg_send(obj->client, *ub, false);
163 }
164
165 static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
166 {
167 struct ubus_msg_buf *ub = NULL;
168 struct event_source *ev;
169 struct blob_attr *attr[EVMSG_LAST];
170 const char *id;
171
172 blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
173 if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
174 return UBUS_STATUS_INVALID_ARGUMENT;
175
176 id = blobmsg_data(attr[EVMSG_ID]);
177 list_for_each_entry(ev, &catch_all, catchall.list) {
178 /* do not loop back events */
179 if (ev->obj->client == cl)
180 continue;
181
182 ubusd_send_event_msg(&ub, ev->obj, id, attr[EVMSG_DATA]);
183 }
184
185 if (ub)
186 ubus_msg_free(ub);
187
188 return 0;
189 }
190
191 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
192 {
193 if (!strcmp(method, "register"))
194 return ubusd_alloc_event_pattern(cl, msg);
195
196 if (!strcmp(method, "send"))
197 return ubusd_send_event(cl, msg);
198
199 return UBUS_STATUS_INVALID_COMMAND;
200 }
201
202 void ubusd_event_init(void)
203 {
204 ubus_init_string_tree(&patterns, true);
205 event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
206 event_obj->recv_msg = ubusd_event_recv;
207 }
208