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;
14 struct list_head list;
15 struct ubus_object *obj;
22 struct list_head list;
27 struct event_pattern {
28 struct event_source evs;
29 struct list_head list;
32 struct event_catchall {
33 struct event_source evs;
35 struct list_head list;
36 struct ubus_object *obj;
39 static void ubusd_delete_event_source(struct event_source *evs)
44 avl_delete(&patterns, &evs->pattern.avl);
47 list_del(&evs->catchall.list);
53 void ubusd_event_cleanup_object(struct ubus_object *obj)
55 struct event_source *ev;
57 while (!list_empty(&obj->events)) {
58 ev = list_first_entry(&obj->events, struct event_source, list);
59 ubusd_delete_event_source(ev);
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 },
75 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
77 struct event_source *evs;
79 evs = calloc(1, sizeof(*evs) + datalen);
80 list_add(&evs->list, &obj->events);
86 static int ubusd_alloc_catchall(struct ubus_object *obj)
88 struct event_source *evs;
90 evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
91 list_add(&evs->catchall.list, &catch_all);
96 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
98 struct event_source *ev;
99 struct ubus_object *obj;
100 struct blob_attr *attr[EVREG_LAST];
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;
108 id = blobmsg_get_u32(attr[EVREG_OBJECT]);
109 if (id < UBUS_SYSTEM_OBJECT_MAX)
110 return UBUS_STATUS_PERMISSION_DENIED;
112 obj = ubusd_find_object(id);
114 return UBUS_STATUS_NOT_FOUND;
116 if (obj->client != cl)
117 return UBUS_STATUS_PERMISSION_DENIED;
119 if (!attr[EVREG_PATTERN])
120 return ubusd_alloc_catchall(obj);
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);
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 },
142 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_object *obj,
143 const char *id, struct blob_attr *msg)
148 objid_ptr = blob_data(blob_data((*ub)->data));
149 *objid_ptr = htonl(obj->id.id);
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));
156 *ub = ubus_msg_new(b.head, blob_raw_len(b.head), true);
158 (*ub)->hdr.type = UBUS_MSG_INVOKE;
161 (*ub)->hdr.seq = ++event_seq;
162 ubus_msg_send(obj->client, *ub, false);
165 static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
167 struct ubus_msg_buf *ub = NULL;
168 struct event_source *ev;
169 struct blob_attr *attr[EVMSG_LAST];
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;
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)
182 ubusd_send_event_msg(&ub, ev->obj, id, attr[EVMSG_DATA]);
191 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
193 if (!strcmp(method, "register"))
194 return ubusd_alloc_event_pattern(cl, msg);
196 if (!strcmp(method, "send"))
197 return ubusd_send_event(cl, msg);
199 return UBUS_STATUS_INVALID_COMMAND;
202 void ubusd_event_init(void)
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;