watch add/remove -> subscribe/unsubscribe:
[project/ubus.git] / ubusd_obj.c
1 /*
2 * Copyright (C) 2011 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 "ubusd.h"
15 #include "ubusd_obj.h"
16
17 struct avl_tree obj_types;
18 struct avl_tree objects;
19 struct avl_tree path;
20
21 static void ubus_unref_object_type(struct ubus_object_type *type)
22 {
23 struct ubus_method *m;
24
25 if (--type->refcount > 0)
26 return;
27
28 while (!list_empty(&type->methods)) {
29 m = list_first_entry(&type->methods, struct ubus_method, list);
30 list_del(&m->list);
31 free(m);
32 }
33
34 ubus_free_id(&obj_types, &type->id);
35 free(type);
36 }
37
38 static bool ubus_create_obj_method(struct ubus_object_type *type, struct blob_attr *attr)
39 {
40 struct ubus_method *m;
41 int bloblen = blob_raw_len(attr);
42
43 m = calloc(1, sizeof(*m) + bloblen);
44 if (!m)
45 return false;
46
47 list_add_tail(&m->list, &type->methods);
48 memcpy(m->data, attr, bloblen);
49 m->name = blobmsg_name(m->data);
50
51 return true;
52 }
53
54 static struct ubus_object_type *ubus_create_obj_type(struct blob_attr *sig)
55 {
56 struct ubus_object_type *type;
57 struct blob_attr *pos;
58 int rem;
59
60 type = calloc(1, sizeof(*type));
61 type->refcount = 1;
62
63 if (!ubus_alloc_id(&obj_types, &type->id, 0))
64 goto error_free;
65
66 INIT_LIST_HEAD(&type->methods);
67
68 blob_for_each_attr(pos, sig, rem) {
69 if (!blobmsg_check_attr(pos, true))
70 goto error_unref;
71
72 if (!ubus_create_obj_method(type, pos))
73 goto error_unref;
74 }
75
76 return type;
77
78 error_unref:
79 ubus_unref_object_type(type);
80 return NULL;
81
82 error_free:
83 free(type);
84 return NULL;
85 }
86
87 static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
88 {
89 struct ubus_object_type *type;
90 struct ubus_id *id;
91
92 id = ubus_find_id(&obj_types, obj_id);
93 if (!id)
94 return NULL;
95
96 type = container_of(id, struct ubus_object_type, id);
97 type->refcount++;
98 return type;
99 }
100
101 struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
102 {
103 struct ubus_object *obj;
104
105 obj = calloc(1, sizeof(*obj));
106 if (!obj)
107 return NULL;
108
109 if (!ubus_alloc_id(&objects, &obj->id, id))
110 goto error_free;
111
112 obj->type = type;
113 INIT_LIST_HEAD(&obj->list);
114 INIT_LIST_HEAD(&obj->events);
115 INIT_LIST_HEAD(&obj->subscribers);
116 INIT_LIST_HEAD(&obj->target_list);
117 if (type)
118 type->refcount++;
119
120 return obj;
121
122 error_free:
123 free(obj);
124 return NULL;
125 }
126
127 struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
128 {
129 struct ubus_object *obj;
130 struct ubus_object_type *type = NULL;
131
132 if (attr[UBUS_ATTR_OBJTYPE])
133 type = ubus_get_obj_type(blob_get_u32(attr[UBUS_ATTR_OBJTYPE]));
134 else if (attr[UBUS_ATTR_SIGNATURE])
135 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
136
137 obj = ubusd_create_object_internal(type, 0);
138 if (type)
139 ubus_unref_object_type(type);
140
141 if (!obj)
142 return NULL;
143
144 if (attr[UBUS_ATTR_OBJPATH]) {
145 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
146 if (!obj->path.key)
147 goto free;
148
149 if (avl_insert(&path, &obj->path) != 0) {
150 free((void *) obj->path.key);
151 obj->path.key = NULL;
152 goto free;
153 }
154 ubusd_send_obj_event(obj, true);
155 }
156
157 obj->client = cl;
158 list_add(&obj->list, &cl->objects);
159
160 return obj;
161
162 free:
163 ubusd_free_object(obj);
164 return NULL;
165 }
166
167 void ubus_subscribe(struct ubus_object *obj, struct ubus_object *target, const char *method)
168 {
169 struct ubus_subscription *s;
170
171 s = calloc(1, sizeof(*s) + strlen(method) + 1);
172 if (!s)
173 return;
174
175 s->subscriber = obj;
176 s->target = target;
177 list_add(&s->list, &target->subscribers);
178 list_add(&s->target_list, &obj->target_list);
179 strcpy(s->method, method);
180 }
181
182 void ubus_unsubscribe(struct ubus_subscription *s)
183 {
184 list_del(&s->list);
185 list_del(&s->target_list);
186 free(s);
187 }
188
189 void ubusd_free_object(struct ubus_object *obj)
190 {
191 struct ubus_subscription *s, *tmp;
192
193 list_for_each_entry_safe(s, tmp, &obj->target_list, target_list) {
194 ubus_unsubscribe(s);
195 }
196 list_for_each_entry_safe(s, tmp, &obj->subscribers, list) {
197 ubus_notify_unsubscribe(s);
198 }
199
200 ubusd_event_cleanup_object(obj);
201 if (obj->path.key) {
202 ubusd_send_obj_event(obj, false);
203 avl_delete(&path, &obj->path);
204 free((void *) obj->path.key);
205 }
206 if (!list_empty(&obj->list))
207 list_del(&obj->list);
208 ubus_free_id(&objects, &obj->id);
209 if (obj->type)
210 ubus_unref_object_type(obj->type);
211 free(obj);
212 }
213
214 static void __init ubusd_obj_init(void)
215 {
216 ubus_init_id_tree(&objects);
217 ubus_init_id_tree(&obj_types);
218 ubus_init_string_tree(&path, false);
219 ubusd_event_init();
220 }