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 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
53 if (!list_empty(&req
->list
))
56 req
->cancelled
= true;
57 __ubus_process_req_data(req
);
58 list_del_init(&req
->list
);
61 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
63 if (!list_empty(&req
->list
))
66 list_add(&req
->list
, &ctx
->requests
);
69 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
71 req
->status_msg
= true;
72 req
->status_code
= ret
;
76 struct ubus_sync_req_cb
{
77 struct uloop_timeout timeout
;
78 struct ubus_request
*req
;
81 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
83 struct ubus_sync_req_cb
*cb
;
85 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
86 ubus_sync_req_cb(cb
->req
, UBUS_STATUS_TIMEOUT
);
89 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
92 struct ubus_sync_req_cb cb
;
93 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
94 bool registered
= ctx
->sock
.registered
;
95 int status
= UBUS_STATUS_NO_DATA
;
103 memset(&cb
, 0, sizeof(cb
));
105 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
106 uloop_timeout_set(&cb
.timeout
, timeout
);
109 ubus_complete_request_async(ctx
, req
);
110 req
->complete_cb
= ubus_sync_req_cb
;
113 while (!req
->status_msg
) {
114 bool cancelled
= uloop_cancelled
;
115 uloop_cancelled
= false;
117 uloop_cancelled
= cancelled
;
122 uloop_timeout_cancel(&cb
.timeout
);
125 status
= req
->status_code
;
127 req
->complete_cb
= complete_cb
;
128 if (req
->complete_cb
)
129 req
->complete_cb(req
, status
);
132 uloop_fd_delete(&ctx
->sock
);
134 if (!ctx
->stack_depth
)
135 ubus_process_pending_msg(ctx
);
140 void ubus_complete_deferred_request(struct ubus_context
*ctx
, struct ubus_request_data
*req
, int ret
)
142 blob_buf_init(&b
, 0);
143 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
144 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
145 ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_STATUS
, req
->peer
);
148 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
149 struct blob_attr
*msg
)
153 blob_buf_init(&b
, 0);
154 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
155 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
156 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
158 return UBUS_STATUS_NO_DATA
;
163 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
164 struct blob_attr
*msg
, struct ubus_request
*req
)
166 blob_buf_init(&b
, 0);
167 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
168 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
170 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
172 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
173 return UBUS_STATUS_INVALID_ARGUMENT
;
178 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
179 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
182 struct ubus_request req
;
184 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
187 return ubus_complete_request(ctx
, &req
, timeout
);
190 static void ubus_req_complete_cb(struct ubus_request
*req
)
192 ubus_complete_handler_t cb
= req
->complete_cb
;
197 req
->complete_cb
= NULL
;
198 cb(req
, req
->status_code
);
201 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
203 struct blob_attr
**attrbuf
= ubus_parse_msg(hdr
->data
);
205 if (!attrbuf
[UBUS_ATTR_STATUS
])
208 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
213 ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
215 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
217 if (!list_empty(&req
->list
))
218 list_del_init(&req
->list
);
220 ubus_get_status(hdr
, &ret
);
221 req
->peer
= hdr
->peer
;
222 req
->status_msg
= true;
223 req
->status_code
= ret
;
225 ubus_req_complete_cb(req
);
231 ubus_process_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
233 struct ubus_pending_data
*data
;
238 req_data_cb(req
, hdr
->type
, hdr
->data
);
239 __ubus_process_req_data(req
);
240 req
->blocked
= false;
243 ubus_req_complete_cb(req
);
248 len
= blob_raw_len(hdr
->data
);
249 data
= calloc(1, sizeof(*data
) + len
);
253 data
->type
= hdr
->type
;
254 memcpy(data
->data
, hdr
->data
, len
);
255 list_add(&data
->list
, &req
->pending
);
258 static struct ubus_request
*
259 ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
261 struct ubus_request
*req
;
263 list_for_each_entry(req
, &ctx
->requests
, list
) {
264 if (seq
!= req
->seq
|| peer
!= req
->peer
)
272 void __hidden
ubus_process_req_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
274 struct ubus_request
*req
;
277 case UBUS_MSG_STATUS
:
278 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
282 ubus_process_req_status(req
, hdr
);
286 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
287 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
288 ubus_process_req_data(req
, hdr
);