3 #include <sys/socket.h>
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
8 #include <libubox/usock.h>
16 #define DPRINTF(_format, ...) fprintf(stderr, "ubus: " _format, ## __VA_ARGS__)
18 #define DPRINTF(...) do {} while(0)
21 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
23 const char *__ubus_strerror
[__UBUS_STATUS_LAST
] = {
24 [UBUS_STATUS_OK
] = "Success",
25 [UBUS_STATUS_INVALID_COMMAND
] = "Invalid command",
26 [UBUS_STATUS_INVALID_ARGUMENT
] = "Invalid argument",
27 [UBUS_STATUS_NOT_FOUND
] = "Not found",
28 [UBUS_STATUS_NO_DATA
] = "No response",
31 static struct blob_buf b
;
33 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
34 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
35 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
36 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
38 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
40 struct ubus_pending_data
{
41 struct list_head list
;
43 struct blob_attr data
[];
46 struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
48 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
52 const char *ubus_strerror(int error
)
56 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
59 if (!__ubus_strerror
[error
])
62 return __ubus_strerror
[error
];
65 sprintf(err
, "Unknown error: %d", error
);
69 static int ubus_send_msg(struct ubus_context
*ctx
, uint32_t seq
,
70 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
72 struct ubus_msghdr hdr
;
73 struct iovec iov
[2] = {
87 iov
[1].iov_base
= (char *) msg
;
88 iov
[1].iov_len
= blob_raw_len(msg
);
90 return writev(ctx
->sock
.fd
, iov
, 2);
93 int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
94 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
96 memset(req
, 0, sizeof(*req
));
98 INIT_LIST_HEAD(&req
->list
);
99 INIT_LIST_HEAD(&req
->pending
);
101 req
->seq
= ++ctx
->request_seq
;
102 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
105 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
109 while (iov
->iov_len
> 0) {
110 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
116 if (errno
!= EAGAIN
) {
125 iov
->iov_len
-= bytes
;
126 iov
->iov_base
+= bytes
;
132 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
134 if (hdr
->version
!= 0)
137 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
140 if (blob_raw_len(hdr
->data
) + sizeof(*hdr
) > UBUS_MAX_MSGLEN
)
146 static bool get_next_msg(struct ubus_context
*ctx
, bool wait
)
148 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
150 /* receive header + start attribute */
151 iov
.iov_len
+= sizeof(struct blob_attr
);
152 if (!recv_retry(ctx
->sock
.fd
, &iov
, wait
))
155 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
156 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
159 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
162 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
164 ubus_parse_msg(hdr
->data
);
166 if (!attrbuf
[UBUS_ATTR_STATUS
])
169 *ret
= blob_get_int32(attrbuf
[UBUS_ATTR_STATUS
]);
173 static void ubus_process_req_data(struct ubus_request
*req
)
175 struct ubus_pending_data
*data
;
177 while (!list_empty(&req
->pending
)) {
178 data
= list_first_entry(&req
->pending
,
179 struct ubus_pending_data
, list
);
180 list_del(&data
->list
);
181 req
->data_cb(req
, data
->type
, data
->data
);
186 static void ubus_req_complete_cb(struct ubus_request
*req
)
188 ubus_complete_handler_t cb
= req
->complete_cb
;
193 req
->complete_cb
= NULL
;
194 cb(req
, req
->status_code
);
197 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
199 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
201 if (!list_empty(&req
->list
))
202 list_del(&req
->list
);
204 ubus_get_status(hdr
, &ret
);
205 req
->peer
= hdr
->peer
;
206 req
->status_msg
= true;
207 req
->status_code
= ret
;
209 ubus_req_complete_cb(req
);
214 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
216 struct ubus_pending_data
*data
;
221 req
->data_cb(req
, hdr
->type
, hdr
->data
);
222 ubus_process_req_data(req
);
223 req
->blocked
= false;
226 ubus_req_complete_cb(req
);
231 len
= blob_raw_len(hdr
->data
);
232 data
= calloc(1, sizeof(*data
) + len
);
236 data
->type
= hdr
->type
;
237 memcpy(data
->data
, hdr
->data
, len
);
238 list_add(&data
->list
, &req
->pending
);
241 static struct ubus_request
*ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
243 struct ubus_request
*req
;
245 list_for_each_entry(req
, &ctx
->requests
, list
) {
246 if (seq
!= req
->seq
|| peer
!= req
->peer
)
254 static void ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
259 ubus_parse_msg(hdr
->data
);
261 if (attrbuf
[UBUS_ATTR_OBJID
])
262 objid
= blob_get_int32(attrbuf
[UBUS_ATTR_OBJID
]);
264 blob_buf_init(&b
, 0);
265 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
266 blob_put_int32(&b
, UBUS_ATTR_OBJID
, objid
);
267 ubus_send_msg(ctx
, hdr
->seq
, b
.head
, UBUS_MSG_STATUS
, hdr
->peer
);
270 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
272 struct ubus_request
*req
;
275 case UBUS_MSG_STATUS
:
276 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
280 ubus_process_req_status(req
, hdr
);
284 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
285 if (req
&& req
->data_cb
)
286 ubus_req_data(req
, hdr
);
289 case UBUS_MSG_INVOKE
:
290 ubus_process_invoke(ctx
, hdr
);
293 DPRINTF("unknown message type: %d\n", hdr
->type
);
298 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
300 if (!list_empty(&req
->list
))
303 list_del(&req
->list
);
306 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
308 if (!list_empty(&req
->list
))
311 list_add(&req
->list
, &ctx
->requests
);
314 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
316 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
317 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
319 while (get_next_msg(ctx
, false))
320 ubus_process_msg(ctx
, hdr
);
323 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
325 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
327 if (!list_empty(&req
->list
))
328 list_del(&req
->list
);
332 return req
->status_code
;
334 if (!get_next_msg(ctx
, true))
335 return UBUS_STATUS_NO_DATA
;
337 if (hdr
->seq
!= req
->seq
|| hdr
->peer
!= req
->peer
)
341 case UBUS_MSG_STATUS
:
342 return ubus_process_req_status(req
, hdr
);
345 ubus_req_data(req
, hdr
);
352 ubus_process_msg(ctx
, hdr
);
356 void ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
357 struct blob_attr
*msg
, struct ubus_request
*req
)
359 blob_buf_init(&b
, 0);
360 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
361 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
362 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
364 ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
);
367 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
368 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
)
370 struct ubus_request req
;
372 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
375 return ubus_complete_request(ctx
, &req
);
378 static void ubus_publish_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
380 struct ubus_object
*obj
= req
->priv
;
384 if (!attrbuf
[UBUS_ATTR_OBJID
])
387 obj
->id
= blob_get_int32(attrbuf
[UBUS_ATTR_OBJID
]);
389 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
390 obj
->type
->id
= blob_get_int32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
393 static bool ubus_push_table_data(const struct ubus_signature
**sig
, int *rem
, bool array
)
395 const struct ubus_signature
*cur
;
403 case UBUS_SIGNATURE_END
:
405 case BLOBMSG_TYPE_INT32
:
406 case BLOBMSG_TYPE_STRING
:
407 blobmsg_add_u32(&b
, cur
->name
, cur
->type
);
409 case BLOBMSG_TYPE_TABLE
:
410 case BLOBMSG_TYPE_ARRAY
:
411 nest_type
= cur
->type
== BLOBMSG_TYPE_ARRAY
;
412 nest
= blobmsg_open_nested(&b
, cur
->name
, nest_type
);
413 if (!ubus_push_table_data(sig
, rem
, nest_type
))
415 blobmsg_close_table(&b
, nest
);
426 static bool ubus_push_object_type(struct ubus_object_type
*type
)
429 int rem
= type
->n_signature
;
430 const struct ubus_signature
*sig
= type
->signature
;
432 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
434 if (sig
->type
!= UBUS_SIGNATURE_METHOD
)
437 m
= blobmsg_open_table(&b
, sig
->name
);
441 if (!ubus_push_table_data(&sig
, &rem
, false))
444 blobmsg_close_table(&b
, m
);
446 blob_nest_end(&b
, s
);
451 int ubus_publish(struct ubus_context
*ctx
, struct ubus_object
*obj
)
453 struct ubus_request req
;
456 if (obj
->id
|| !obj
->name
|| !obj
->type
)
457 return UBUS_STATUS_INVALID_ARGUMENT
;
459 blob_buf_init(&b
, 0);
460 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
462 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->parent
->id
);
465 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
466 else if (!ubus_push_object_type(obj
->type
))
467 return UBUS_STATUS_INVALID_ARGUMENT
;
469 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_PUBLISH
, 0);
470 req
.data_cb
= ubus_publish_cb
;
472 ret
= ubus_complete_request(ctx
, &req
);
477 return UBUS_STATUS_NO_DATA
;
482 struct ubus_context
*ubus_connect(const char *path
)
484 struct ubus_context
*ctx
;
486 struct ubus_msghdr hdr
;
487 struct blob_attr data
;
489 struct blob_attr
*buf
;
492 path
= UBUS_UNIX_SOCKET
;
494 ctx
= calloc(1, sizeof(*ctx
));
498 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
499 if (ctx
->sock
.fd
< 0) {
500 DPRINTF("Failed to connect to server\n");
503 ctx
->sock
.cb
= ubus_handle_data
;
505 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
)) {
506 DPRINTF("Failed to read initial message data\n");
510 if (!ubus_validate_hdr(&hdr
.hdr
)) {
511 DPRINTF("Failed to validate initial message header\n");
515 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
) {
516 DPRINTF("Unexpected initial message\n");
520 buf
= calloc(1, blob_raw_len(&hdr
.data
));
524 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
525 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
)) {
526 DPRINTF("Failed to retrieve initial message data\n");
530 ctx
->local_id
= hdr
.hdr
.peer
;
531 INIT_LIST_HEAD(&ctx
->requests
);
534 if (!ctx
->local_id
) {
535 DPRINTF("Failed to get local peer id\n");
551 void ubus_free(struct ubus_context
*ctx
)