2 * Copyright (C) 2011 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 "ubusd_obj.h"
17 struct avl_tree obj_types
;
18 struct avl_tree objects
;
21 static void ubus_unref_object_type(struct ubus_object_type
*type
)
23 struct ubus_method
*m
;
25 if (--type
->refcount
> 0)
28 while (!list_empty(&type
->methods
)) {
29 m
= list_first_entry(&type
->methods
, struct ubus_method
, list
);
34 ubus_free_id(&obj_types
, &type
->id
);
38 static bool ubus_create_obj_method(struct ubus_object_type
*type
, struct blob_attr
*attr
)
40 struct ubus_method
*m
;
41 int bloblen
= blob_raw_len(attr
);
43 m
= calloc(1, sizeof(*m
) + bloblen
);
47 list_add_tail(&m
->list
, &type
->methods
);
48 memcpy(m
->data
, attr
, bloblen
);
49 m
->name
= blobmsg_name(m
->data
);
54 static struct ubus_object_type
*ubus_create_obj_type(struct blob_attr
*sig
)
56 struct ubus_object_type
*type
;
57 struct blob_attr
*pos
;
60 type
= calloc(1, sizeof(*type
));
63 if (!ubus_alloc_id(&obj_types
, &type
->id
, 0))
66 INIT_LIST_HEAD(&type
->methods
);
68 blob_for_each_attr(pos
, sig
, rem
) {
69 if (!blobmsg_check_attr(pos
, true))
72 if (!ubus_create_obj_method(type
, pos
))
79 ubus_unref_object_type(type
);
87 static struct ubus_object_type
*ubus_get_obj_type(uint32_t obj_id
)
89 struct ubus_object_type
*type
;
92 id
= ubus_find_id(&obj_types
, obj_id
);
96 type
= container_of(id
, struct ubus_object_type
, id
);
101 struct ubus_object
*ubusd_create_object_internal(struct ubus_object_type
*type
, uint32_t id
)
103 struct ubus_object
*obj
;
105 obj
= calloc(1, sizeof(*obj
));
109 if (!ubus_alloc_id(&objects
, &obj
->id
, id
))
113 INIT_LIST_HEAD(&obj
->list
);
114 INIT_LIST_HEAD(&obj
->events
);
115 INIT_LIST_HEAD(&obj
->watchers
);
116 INIT_LIST_HEAD(&obj
->watched
);
127 struct ubus_object
*ubusd_create_object(struct ubus_client
*cl
, struct blob_attr
**attr
)
129 struct ubus_object
*obj
;
130 struct ubus_object_type
*type
= NULL
;
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
]);
137 obj
= ubusd_create_object_internal(type
, 0);
139 ubus_unref_object_type(type
);
144 if (attr
[UBUS_ATTR_OBJPATH
]) {
145 obj
->path
.key
= strdup(blob_data(attr
[UBUS_ATTR_OBJPATH
]));
149 if (avl_insert(&path
, &obj
->path
) != 0) {
150 free((void *) obj
->path
.key
);
151 obj
->path
.key
= NULL
;
154 ubusd_send_obj_event(obj
, true);
158 list_add(&obj
->list
, &cl
->objects
);
163 ubusd_free_object(obj
);
167 void ubus_watch_new(struct ubus_object
*obj
, struct ubus_object
*target
, const char *method
)
169 struct ubus_watch
*w
;
171 w
= calloc(1, sizeof(*w
) + strlen(method
) + 1);
177 list_add(&w
->watcher_list
, &target
->watchers
);
178 list_add(&w
->watched_list
, &obj
->watched
);
179 strcpy(w
->method
, method
);
182 void ubus_watch_free(struct ubus_watch
*w
)
184 list_del(&w
->watcher_list
);
185 list_del(&w
->watched_list
);
189 void ubusd_free_object(struct ubus_object
*obj
)
191 struct ubus_watch
*w
, *tmp
;
193 list_for_each_entry_safe(w
, tmp
, &obj
->watched
, watched_list
) {
196 list_for_each_entry_safe(w
, tmp
, &obj
->watchers
, watcher_list
) {
197 ubus_proto_notify_watch(w
);
200 ubusd_event_cleanup_object(obj
);
202 ubusd_send_obj_event(obj
, false);
203 avl_delete(&path
, &obj
->path
);
204 free((void *) obj
->path
.key
);
206 if (!list_empty(&obj
->list
))
207 list_del(&obj
->list
);
208 ubus_free_id(&objects
, &obj
->id
);
210 ubus_unref_object_type(obj
->type
);
214 static void __init
ubusd_obj_init(void)
216 ubus_init_id_tree(&objects
);
217 ubus_init_id_tree(&obj_types
);
218 ubus_init_string_tree(&path
, false);