4 struct avl_tree obj_types
;
5 struct avl_tree objects
;
8 static void ubus_unref_object_type(struct ubus_object_type
*type
)
10 struct ubus_method
*m
;
12 if (--type
->refcount
> 0)
15 while (!list_empty(&type
->methods
)) {
16 m
= list_first_entry(&type
->methods
, struct ubus_method
, list
);
21 ubus_free_id(&obj_types
, &type
->id
);
25 static bool ubus_create_obj_method(struct ubus_object_type
*type
, struct blob_attr
*attr
)
27 struct ubus_method
*m
;
28 int bloblen
= blob_raw_len(attr
);
30 m
= calloc(1, sizeof(*m
) + bloblen
);
34 list_add(&m
->list
, &type
->methods
);
35 memcpy(m
->data
, attr
, bloblen
);
36 m
->name
= blobmsg_name(m
->data
);
41 static struct ubus_object_type
*ubus_create_obj_type(struct blob_attr
*sig
)
43 struct ubus_object_type
*type
;
44 struct blob_attr
*pos
;
47 type
= calloc(1, sizeof(*type
));
50 if (!ubus_alloc_id(&obj_types
, &type
->id
, 0))
53 INIT_LIST_HEAD(&type
->methods
);
55 blob_for_each_attr(pos
, sig
, rem
) {
56 if (!blobmsg_check_attr(pos
, true))
59 if (!ubus_create_obj_method(type
, pos
))
66 ubus_unref_object_type(type
);
74 static struct ubus_object_type
*ubus_get_obj_type(uint32_t obj_id
)
76 struct ubus_object_type
*type
;
79 id
= ubus_find_id(&obj_types
, obj_id
);
83 type
= container_of(id
, struct ubus_object_type
, id
);
88 struct ubus_object
*ubusd_create_object_internal(struct ubus_object_type
*type
, uint32_t id
)
90 struct ubus_object
*obj
;
92 obj
= calloc(1, sizeof(*obj
));
96 if (!ubus_alloc_id(&objects
, &obj
->id
, id
))
100 INIT_LIST_HEAD(&obj
->list
);
101 INIT_LIST_HEAD(&obj
->events
);
112 struct ubus_object
*ubusd_create_object(struct ubus_client
*cl
, struct blob_attr
**attr
)
114 struct ubus_object
*obj
;
115 struct ubus_object_type
*type
= NULL
;
117 if (attr
[UBUS_ATTR_OBJTYPE
])
118 type
= ubus_get_obj_type(blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]));
119 else if (attr
[UBUS_ATTR_SIGNATURE
])
120 type
= ubus_create_obj_type(attr
[UBUS_ATTR_SIGNATURE
]);
122 if (!!type
^ !!attr
[UBUS_ATTR_OBJPATH
])
125 obj
= ubusd_create_object_internal(type
, 0);
127 ubus_unref_object_type(type
);
132 if (attr
[UBUS_ATTR_OBJPATH
]) {
133 obj
->path
.key
= strdup(blob_data(attr
[UBUS_ATTR_OBJPATH
]));
137 if (avl_insert(&path
, &obj
->path
) != 0) {
139 obj
->path
.key
= NULL
;
145 list_add(&obj
->list
, &cl
->objects
);
150 ubusd_free_object(obj
);
154 void ubusd_free_object(struct ubus_object
*obj
)
156 ubusd_event_cleanup_object(obj
);
158 avl_delete(&path
, &obj
->path
);
161 if (!list_empty(&obj
->list
))
162 list_del(&obj
->list
);
163 ubus_free_id(&objects
, &obj
->id
);
165 ubus_unref_object_type(obj
->type
);
169 static void __init
ubusd_obj_init(void)
171 ubus_init_id_tree(&objects
);
172 ubus_init_id_tree(&obj_types
);
173 ubus_init_string_tree(&path
, false);