2 * Copyright (C) 2011-2012 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.
16 #include "libubus-internal.h"
19 ubus_process_unsubscribe(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
,
20 struct ubus_object
*obj
, struct blob_attr
**attrbuf
, int fd
)
22 struct ubus_subscriber
*s
;
24 if (!obj
|| !attrbuf
[UBUS_ATTR_TARGET
])
27 if (obj
->methods
!= &watch_method
)
30 s
= container_of(obj
, struct ubus_subscriber
, obj
);
32 s
->remove_cb(ctx
, s
, blob_get_u32(attrbuf
[UBUS_ATTR_TARGET
]));
38 ubus_process_notify(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
,
39 struct ubus_object
*obj
, struct blob_attr
**attrbuf
, int fd
)
41 if (!obj
|| !attrbuf
[UBUS_ATTR_ACTIVE
])
44 obj
->has_subscribers
= blob_get_u8(attrbuf
[UBUS_ATTR_ACTIVE
]);
45 if (obj
->subscribe_cb
)
46 obj
->subscribe_cb(ctx
, obj
);
51 ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
,
52 struct ubus_object
*obj
, struct blob_attr
**attrbuf
, int fd
)
54 struct ubus_request_data req
= {
61 bool no_reply
= false;
64 ret
= UBUS_STATUS_NOT_FOUND
;
68 if (!attrbuf
[UBUS_ATTR_METHOD
]) {
69 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
73 if (attrbuf
[UBUS_ATTR_NO_REPLY
])
74 no_reply
= blob_get_int8(attrbuf
[UBUS_ATTR_NO_REPLY
]);
79 if (attrbuf
[UBUS_ATTR_USER
] && attrbuf
[UBUS_ATTR_GROUP
]) {
80 req
.acl
.user
= blobmsg_get_string(attrbuf
[UBUS_ATTR_USER
]);
81 req
.acl
.group
= blobmsg_get_string(attrbuf
[UBUS_ATTR_GROUP
]);
82 req
.acl
.object
= obj
->name
;
84 for (method
= 0; method
< obj
->n_methods
; method
++)
85 if (!obj
->methods
[method
].name
||
86 !strcmp(obj
->methods
[method
].name
,
87 blob_data(attrbuf
[UBUS_ATTR_METHOD
])))
91 ret
= UBUS_STATUS_METHOD_NOT_FOUND
;
95 ret
= obj
->methods
[method
].handler(ctx
, obj
, &req
,
96 blob_data(attrbuf
[UBUS_ATTR_METHOD
]),
97 attrbuf
[UBUS_ATTR_DATA
]);
99 if (req
.deferred
|| no_reply
)
103 ubus_complete_deferred_request(ctx
, &req
, ret
);
107 void __hidden
ubus_process_obj_msg(struct ubus_context
*ctx
, struct ubus_msghdr_buf
*buf
, int fd
)
109 void (*cb
)(struct ubus_context
*, struct ubus_msghdr
*,
110 struct ubus_object
*, struct blob_attr
**, int fd
);
111 struct ubus_msghdr
*hdr
= &buf
->hdr
;
112 struct blob_attr
**attrbuf
;
113 struct ubus_object
*obj
;
115 void *prev_data
= NULL
;
116 attrbuf
= ubus_parse_msg(buf
->data
);
117 if (!attrbuf
[UBUS_ATTR_OBJID
])
120 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
121 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
124 case UBUS_MSG_INVOKE
:
125 cb
= ubus_process_invoke
;
127 case UBUS_MSG_UNSUBSCRIBE
:
128 cb
= ubus_process_unsubscribe
;
130 case UBUS_MSG_NOTIFY
:
131 cb
= ubus_process_notify
;
137 if (buf
== &ctx
->msgbuf
) {
138 prev_data
= buf
->data
;
142 cb(ctx
, hdr
, obj
, attrbuf
, fd
);
148 buf
->data
= prev_data
;
152 static void ubus_add_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
154 struct ubus_object
*obj
= req
->priv
;
155 struct blob_attr
**attrbuf
= ubus_parse_msg(msg
);
157 if (!attrbuf
[UBUS_ATTR_OBJID
])
160 obj
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
162 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
163 obj
->type
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
165 obj
->avl
.key
= &obj
->id
;
166 avl_insert(&req
->ctx
->objects
, &obj
->avl
);
169 static void ubus_push_method_data(const struct ubus_method
*m
)
174 mtbl
= blobmsg_open_table(&b
, m
->name
);
176 for (i
= 0; i
< m
->n_policy
; i
++) {
177 if (m
->mask
&& !(m
->mask
& (1 << i
)))
180 blobmsg_add_u32(&b
, m
->policy
[i
].name
, m
->policy
[i
].type
);
183 blobmsg_close_table(&b
, mtbl
);
186 static bool ubus_push_object_type(const struct ubus_object_type
*type
)
191 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
193 for (i
= 0; i
< type
->n_methods
; i
++)
194 ubus_push_method_data(&type
->methods
[i
]);
196 blob_nest_end(&b
, s
);
201 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
203 struct ubus_request req
;
206 blob_buf_init(&b
, 0);
208 if (obj
->name
&& obj
->type
) {
209 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
212 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
213 else if (!ubus_push_object_type(obj
->type
))
214 return UBUS_STATUS_INVALID_ARGUMENT
;
217 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_ADD_OBJECT
, 0) < 0)
218 return UBUS_STATUS_INVALID_ARGUMENT
;
220 req
.raw_data_cb
= ubus_add_object_cb
;
222 ret
= ubus_complete_request(ctx
, &req
, 0);
227 return UBUS_STATUS_NO_DATA
;
232 static void ubus_remove_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
234 struct ubus_object
*obj
= req
->priv
;
235 struct blob_attr
**attrbuf
= ubus_parse_msg(msg
);
237 if (!attrbuf
[UBUS_ATTR_OBJID
])
242 if (attrbuf
[UBUS_ATTR_OBJTYPE
] && obj
->type
)
245 avl_delete(&req
->ctx
->objects
, &obj
->avl
);
248 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
250 struct ubus_request req
;
253 blob_buf_init(&b
, 0);
254 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
256 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_REMOVE_OBJECT
, 0) < 0)
257 return UBUS_STATUS_INVALID_ARGUMENT
;
259 req
.raw_data_cb
= ubus_remove_object_cb
;
261 ret
= ubus_complete_request(ctx
, &req
, 0);
266 return UBUS_STATUS_NO_DATA
;