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.
15 #include "libubus-internal.h"
17 struct ubus_pending_data
{
18 struct list_head list
;
20 struct blob_attr data
[];
23 static void req_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*data
)
25 struct blob_attr
**attr
;
28 req
->raw_data_cb(req
, type
, data
);
33 attr
= ubus_parse_msg(data
);
34 req
->data_cb(req
, type
, attr
[UBUS_ATTR_DATA
]);
37 static void __ubus_process_req_data(struct ubus_request
*req
)
39 struct ubus_pending_data
*data
;
41 while (!list_empty(&req
->pending
)) {
42 data
= list_first_entry(&req
->pending
,
43 struct ubus_pending_data
, list
);
44 list_del(&data
->list
);
46 req_data_cb(req
, data
->type
, data
->data
);
51 int __hidden
ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
52 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
54 memset(req
, 0, sizeof(*req
));
56 if (msg
&& blob_pad_len(msg
) > UBUS_MAX_MSGLEN
)
59 INIT_LIST_HEAD(&req
->list
);
60 INIT_LIST_HEAD(&req
->pending
);
63 req
->seq
= ++ctx
->request_seq
;
64 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
67 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
69 if (!list_empty(&req
->list
))
72 req
->cancelled
= true;
73 __ubus_process_req_data(req
);
74 list_del_init(&req
->list
);
77 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
79 if (!list_empty(&req
->list
))
82 list_add(&req
->list
, &ctx
->requests
);
86 ubus_req_complete_cb(struct ubus_request
*req
)
88 ubus_complete_handler_t cb
= req
->complete_cb
;
93 req
->complete_cb
= NULL
;
94 cb(req
, req
->status_code
);
98 ubus_set_req_status(struct ubus_request
*req
, int ret
)
100 if (!list_empty(&req
->list
))
101 list_del_init(&req
->list
);
103 req
->status_msg
= true;
104 req
->status_code
= ret
;
106 ubus_req_complete_cb(req
);
109 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
111 req
->status_msg
= true;
112 req
->status_code
= ret
;
116 struct ubus_sync_req_cb
{
117 struct uloop_timeout timeout
;
118 struct ubus_request
*req
;
121 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
123 struct ubus_sync_req_cb
*cb
;
125 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
126 ubus_set_req_status(cb
->req
, UBUS_STATUS_TIMEOUT
);
129 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
132 struct ubus_sync_req_cb cb
;
133 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
134 bool registered
= ctx
->sock
.registered
;
135 int status
= UBUS_STATUS_NO_DATA
;
143 memset(&cb
, 0, sizeof(cb
));
145 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
146 uloop_timeout_set(&cb
.timeout
, timeout
);
149 ubus_complete_request_async(ctx
, req
);
150 req
->complete_cb
= ubus_sync_req_cb
;
153 while (!req
->status_msg
) {
154 bool cancelled
= uloop_cancelled
;
155 uloop_cancelled
= false;
157 uloop_cancelled
= cancelled
;
162 uloop_timeout_cancel(&cb
.timeout
);
165 status
= req
->status_code
;
167 req
->complete_cb
= complete_cb
;
168 if (req
->complete_cb
)
169 req
->complete_cb(req
, status
);
172 uloop_fd_delete(&ctx
->sock
);
174 if (!ctx
->stack_depth
)
175 ubus_process_pending_msg(ctx
);
180 void ubus_complete_deferred_request(struct ubus_context
*ctx
, struct ubus_request_data
*req
, int ret
)
182 blob_buf_init(&b
, 0);
183 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
184 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
185 ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_STATUS
, req
->peer
);
188 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
189 struct blob_attr
*msg
)
193 blob_buf_init(&b
, 0);
194 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
195 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
196 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
198 return UBUS_STATUS_NO_DATA
;
203 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
204 struct blob_attr
*msg
, struct ubus_request
*req
)
206 blob_buf_init(&b
, 0);
207 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
208 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
210 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
212 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
213 return UBUS_STATUS_INVALID_ARGUMENT
;
218 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
219 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
222 struct ubus_request req
;
224 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
227 return ubus_complete_request(ctx
, &req
, timeout
);
231 ubus_notify_complete_cb(struct ubus_request
*req
, int ret
)
233 struct ubus_notify_request
*nreq
;
235 nreq
= container_of(req
, struct ubus_notify_request
, req
);
236 if (!nreq
->complete_cb
)
239 nreq
->complete_cb(nreq
, 0, 0);
243 __ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
244 const char *type
, struct blob_attr
*msg
,
245 struct ubus_notify_request
*req
, bool reply
)
247 memset(req
, 0, sizeof(*req
));
249 blob_buf_init(&b
, 0);
250 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
251 blob_put_string(&b
, UBUS_ATTR_METHOD
, type
);
254 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, true);
257 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
259 if (ubus_start_request(ctx
, &req
->req
, b
.head
, UBUS_MSG_NOTIFY
, obj
->id
) < 0)
260 return UBUS_STATUS_INVALID_ARGUMENT
;
262 /* wait for status message from ubusd first */
263 req
->req
.notify
= true;
265 req
->id
[0] = obj
->id
;
266 req
->req
.complete_cb
= ubus_notify_complete_cb
;
271 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
272 const char *type
, struct blob_attr
*msg
,
273 struct ubus_notify_request
*req
)
275 return __ubus_notify_async(ctx
, obj
, type
, msg
, req
, true);
278 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
279 const char *type
, struct blob_attr
*msg
, int timeout
)
281 struct ubus_notify_request req
;
284 ret
= __ubus_notify_async(ctx
, obj
, type
, msg
, &req
, timeout
>= 0);
291 return ubus_complete_request(ctx
, &req
.req
, timeout
);
294 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
296 struct blob_attr
**attrbuf
= ubus_parse_msg(hdr
->data
);
298 if (!attrbuf
[UBUS_ATTR_STATUS
])
301 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
306 ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
308 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
310 ubus_get_status(hdr
, &ret
);
311 req
->peer
= hdr
->peer
;
312 ubus_set_req_status(req
, ret
);
318 ubus_process_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
320 struct ubus_pending_data
*data
;
325 req_data_cb(req
, hdr
->type
, hdr
->data
);
326 __ubus_process_req_data(req
);
327 req
->blocked
= false;
330 ubus_req_complete_cb(req
);
335 len
= blob_raw_len(hdr
->data
);
336 data
= calloc(1, sizeof(*data
) + len
);
340 data
->type
= hdr
->type
;
341 memcpy(data
->data
, hdr
->data
, len
);
342 list_add(&data
->list
, &req
->pending
);
346 ubus_find_notify_id(struct ubus_notify_request
*n
, uint32_t objid
)
348 uint32_t pending
= n
->pending
;
351 for (i
= 0; pending
; i
++, pending
>>= 1) {
355 if (n
->id
[i
] == objid
)
362 static struct ubus_request
*
363 ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
, int *id
)
365 struct ubus_request
*req
;
367 list_for_each_entry(req
, &ctx
->requests
, list
) {
368 struct ubus_notify_request
*nreq
;
369 nreq
= container_of(req
, struct ubus_notify_request
, req
);
378 *id
= ubus_find_notify_id(nreq
, peer
);
381 } else if (peer
!= req
->peer
)
389 static void ubus_process_notify_status(struct ubus_request
*req
, int id
, struct ubus_msghdr
*hdr
)
391 struct ubus_notify_request
*nreq
;
392 struct blob_attr
**tb
;
393 struct blob_attr
*cur
;
397 nreq
= container_of(req
, struct ubus_notify_request
, req
);
398 nreq
->pending
&= ~(1 << id
);
401 /* first id: ubusd's status message with a list of ids */
402 tb
= ubus_parse_msg(hdr
->data
);
403 if (tb
[UBUS_ATTR_SUBSCRIBERS
]) {
404 blob_for_each_attr(cur
, tb
[UBUS_ATTR_SUBSCRIBERS
], rem
) {
405 if (!blob_check_type(blob_data(cur
), blob_len(cur
), BLOB_ATTR_INT32
))
408 nreq
->pending
|= (1 << idx
);
409 nreq
->id
[idx
] = blob_get_int32(cur
);
412 if (idx
== UBUS_MAX_NOTIFY_PEERS
+ 1)
417 ubus_get_status(hdr
, &ret
);
419 nreq
->status_cb(nreq
, id
, ret
);
423 ubus_set_req_status(req
, 0);
426 void __hidden
ubus_process_req_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
428 struct ubus_request
*req
;
432 case UBUS_MSG_STATUS
:
433 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
438 ubus_process_notify_status(req
, id
, hdr
);
440 ubus_process_req_status(req
, hdr
);
444 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
445 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
446 ubus_process_req_data(req
, hdr
);