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
);
85 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
87 req
->status_msg
= true;
88 req
->status_code
= ret
;
92 struct ubus_sync_req_cb
{
93 struct uloop_timeout timeout
;
94 struct ubus_request
*req
;
97 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
99 struct ubus_sync_req_cb
*cb
;
101 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
102 ubus_sync_req_cb(cb
->req
, UBUS_STATUS_TIMEOUT
);
105 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
108 struct ubus_sync_req_cb cb
;
109 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
110 bool registered
= ctx
->sock
.registered
;
111 int status
= UBUS_STATUS_NO_DATA
;
119 memset(&cb
, 0, sizeof(cb
));
121 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
122 uloop_timeout_set(&cb
.timeout
, timeout
);
125 ubus_complete_request_async(ctx
, req
);
126 req
->complete_cb
= ubus_sync_req_cb
;
129 while (!req
->status_msg
) {
130 bool cancelled
= uloop_cancelled
;
131 uloop_cancelled
= false;
133 uloop_cancelled
= cancelled
;
138 uloop_timeout_cancel(&cb
.timeout
);
141 status
= req
->status_code
;
143 req
->complete_cb
= complete_cb
;
144 if (req
->complete_cb
)
145 req
->complete_cb(req
, status
);
148 uloop_fd_delete(&ctx
->sock
);
150 if (!ctx
->stack_depth
)
151 ubus_process_pending_msg(ctx
);
156 void ubus_complete_deferred_request(struct ubus_context
*ctx
, struct ubus_request_data
*req
, int ret
)
158 blob_buf_init(&b
, 0);
159 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
160 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
161 ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_STATUS
, req
->peer
);
164 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
165 struct blob_attr
*msg
)
169 blob_buf_init(&b
, 0);
170 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
171 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
172 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
174 return UBUS_STATUS_NO_DATA
;
179 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
180 struct blob_attr
*msg
, struct ubus_request
*req
)
182 blob_buf_init(&b
, 0);
183 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
184 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
186 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
188 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
189 return UBUS_STATUS_INVALID_ARGUMENT
;
194 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
195 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
198 struct ubus_request req
;
200 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
203 return ubus_complete_request(ctx
, &req
, timeout
);
206 static void ubus_req_complete_cb(struct ubus_request
*req
)
208 ubus_complete_handler_t cb
= req
->complete_cb
;
213 req
->complete_cb
= NULL
;
214 cb(req
, req
->status_code
);
217 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
219 struct blob_attr
**attrbuf
= ubus_parse_msg(hdr
->data
);
221 if (!attrbuf
[UBUS_ATTR_STATUS
])
224 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
229 ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
231 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
233 if (!list_empty(&req
->list
))
234 list_del_init(&req
->list
);
236 ubus_get_status(hdr
, &ret
);
237 req
->peer
= hdr
->peer
;
238 req
->status_msg
= true;
239 req
->status_code
= ret
;
241 ubus_req_complete_cb(req
);
247 ubus_process_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
249 struct ubus_pending_data
*data
;
254 req_data_cb(req
, hdr
->type
, hdr
->data
);
255 __ubus_process_req_data(req
);
256 req
->blocked
= false;
259 ubus_req_complete_cb(req
);
264 len
= blob_raw_len(hdr
->data
);
265 data
= calloc(1, sizeof(*data
) + len
);
269 data
->type
= hdr
->type
;
270 memcpy(data
->data
, hdr
->data
, len
);
271 list_add(&data
->list
, &req
->pending
);
274 static struct ubus_request
*
275 ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
277 struct ubus_request
*req
;
279 list_for_each_entry(req
, &ctx
->requests
, list
) {
280 if (seq
!= req
->seq
|| peer
!= req
->peer
)
288 void __hidden
ubus_process_req_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
290 struct ubus_request
*req
;
293 case UBUS_MSG_STATUS
:
294 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
298 ubus_process_req_status(req
, hdr
);
302 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
303 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
304 ubus_process_req_data(req
, hdr
);