+ ev = calloc(1, sizeof(*ev) + len + 1);
+ if (!ev)
+ return UBUS_STATUS_NO_DATA;
+
+ list_add(&ev->list, &obj->events);
+ ev->obj = obj;
+ ev->partial = partial;
+ name = (char *) (ev + 1);
+ strcpy(name, pattern);
+ ev->avl.key = name;
+ avl_insert(&patterns, &ev->avl);
+
+ return 0;
+}
+
+static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl,
+ struct ubus_object *obj, const char *id,
+ event_fill_cb fill_cb, void *cb_priv)
+{
+ uint32_t *objid_ptr;
+
+ /* do not loop back events */
+ if (obj->client == cl)
+ return;
+
+ /* do not send duplicate events */
+ if (obj->event_seen == obj_event_seq)
+ return;
+
+ obj->event_seen = obj_event_seq;
+
+ if (!*ub) {
+ *ub = fill_cb(cb_priv, id);
+ (*ub)->hdr.type = UBUS_MSG_INVOKE;
+ (*ub)->hdr.peer = 0;
+ }
+
+ objid_ptr = blob_data(blob_data((*ub)->data));
+ *objid_ptr = htonl(obj->id.id);
+
+ (*ub)->hdr.seq = ++event_seq;
+ ubus_msg_send(obj->client, *ub, false);
+}
+
+static bool strmatch_len(const char *s1, const char *s2, int *len)
+{
+ for (*len = 0; s1[*len] == s2[*len]; (*len)++)
+ if (!s1[*len])
+ return true;
+
+ return false;
+}
+
+int ubusd_send_event(struct ubus_client *cl, const char *id,
+ event_fill_cb fill_cb, void *cb_priv)
+{
+ struct ubus_msg_buf *ub = NULL;
+ struct event_source *ev;
+ int match_len = 0;
+
+ obj_event_seq++;
+
+ /*
+ * Since this tree is sorted alphabetically, we can only expect to find
+ * matching entries as long as the number of matching characters
+ * between the pattern string and our string is monotonically increasing.
+ */
+ avl_for_each_element(&patterns, ev, avl) {
+ const char *key = ev->avl.key;
+ int cur_match_len;
+ bool full_match;
+
+ full_match = strmatch_len(id, key, &cur_match_len);
+ if (cur_match_len < match_len)
+ break;
+
+ match_len = cur_match_len;
+
+ if (!full_match) {
+ if (!ev->partial)
+ continue;
+
+ if (match_len != strlen(key))
+ continue;
+ }
+
+ ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
+ }
+
+ if (ub)
+ ubus_msg_free(ub);