2 * Copyright (C) 2011 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.
14 #include <sys/types.h>
16 #include <sys/socket.h>
21 #include <libubox/blob.h>
22 #include <libubox/blobmsg.h>
23 #include <libubox/usock.h>
28 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
30 const char *__ubus_strerror
[__UBUS_STATUS_LAST
] = {
31 [UBUS_STATUS_OK
] = "Success",
32 [UBUS_STATUS_INVALID_COMMAND
] = "Invalid command",
33 [UBUS_STATUS_INVALID_ARGUMENT
] = "Invalid argument",
34 [UBUS_STATUS_METHOD_NOT_FOUND
] = "Method not found",
35 [UBUS_STATUS_NOT_FOUND
] = "Not found",
36 [UBUS_STATUS_NO_DATA
] = "No response",
37 [UBUS_STATUS_PERMISSION_DENIED
] = "Permission denied",
38 [UBUS_STATUS_TIMEOUT
] = "Request timed out",
39 [UBUS_STATUS_NOT_SUPPORTED
] = "Operation not supported",
40 [UBUS_STATUS_UNKNOWN_ERROR
] = "Unknown error",
43 static struct blob_buf b
;
45 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
46 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
47 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
48 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
49 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
51 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
53 struct ubus_pending_data
{
54 struct list_head list
;
56 struct blob_attr data
[];
59 struct ubus_pending_invoke
{
60 struct list_head list
;
61 struct ubus_msghdr hdr
;
64 static void ubus_process_pending_invoke(struct ubus_context
*ctx
);
66 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
68 const uint32_t *id1
= k1
, *id2
= k2
;
76 static struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
78 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
82 const char *ubus_strerror(int error
)
86 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
89 if (!__ubus_strerror
[error
])
92 return __ubus_strerror
[error
];
95 sprintf(err
, "Unknown error: %d", error
);
99 static void wait_data(int fd
, bool write
)
101 struct pollfd pfd
= { .fd
= fd
};
103 pfd
.events
= write
? POLLOUT
: POLLIN
;
107 static int writev_retry(int fd
, struct iovec
*iov
, int iov_len
)
112 int cur_len
= writev(fd
, iov
, iov_len
);
126 while (cur_len
>= iov
->iov_len
) {
127 cur_len
-= iov
->iov_len
;
130 if (!cur_len
|| !iov_len
)
133 iov
->iov_len
-= cur_len
;
137 static int ubus_send_msg(struct ubus_context
*ctx
, uint32_t seq
,
138 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
140 struct ubus_msghdr hdr
;
141 struct iovec iov
[2] = {
151 blob_buf_init(&b
, 0);
155 iov
[1].iov_base
= (char *) msg
;
156 iov
[1].iov_len
= blob_raw_len(msg
);
158 return writev_retry(ctx
->sock
.fd
, iov
, ARRAY_SIZE(iov
));
161 static int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
162 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
164 memset(req
, 0, sizeof(*req
));
166 if (msg
&& blob_pad_len(msg
) > UBUS_MAX_MSGLEN
)
169 INIT_LIST_HEAD(&req
->list
);
170 INIT_LIST_HEAD(&req
->pending
);
173 req
->seq
= ++ctx
->request_seq
;
174 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
177 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
181 while (iov
->iov_len
> 0) {
183 wait_data(fd
, false);
185 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
200 iov
->iov_len
-= bytes
;
201 iov
->iov_base
+= bytes
;
207 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
209 if (hdr
->version
!= 0)
212 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
215 if (blob_pad_len(hdr
->data
) > UBUS_MAX_MSGLEN
)
221 static bool get_next_msg(struct ubus_context
*ctx
)
223 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
225 /* receive header + start attribute */
226 iov
.iov_len
+= sizeof(struct blob_attr
);
227 if (!recv_retry(ctx
->sock
.fd
, &iov
, false))
230 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
231 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
234 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
237 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
239 ubus_parse_msg(hdr
->data
);
241 if (!attrbuf
[UBUS_ATTR_STATUS
])
244 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
248 static void req_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*data
)
250 struct blob_attr
**attr
;
252 if (req
->raw_data_cb
)
253 req
->raw_data_cb(req
, type
, data
);
258 attr
= ubus_parse_msg(data
);
259 req
->data_cb(req
, type
, attr
[UBUS_ATTR_DATA
]);
262 static void ubus_process_req_data(struct ubus_request
*req
)
264 struct ubus_pending_data
*data
;
266 while (!list_empty(&req
->pending
)) {
267 data
= list_first_entry(&req
->pending
,
268 struct ubus_pending_data
, list
);
269 list_del(&data
->list
);
271 req_data_cb(req
, data
->type
, data
->data
);
276 static void ubus_req_complete_cb(struct ubus_request
*req
)
278 ubus_complete_handler_t cb
= req
->complete_cb
;
283 req
->complete_cb
= NULL
;
284 cb(req
, req
->status_code
);
287 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
289 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
291 if (!list_empty(&req
->list
))
292 list_del_init(&req
->list
);
294 ubus_get_status(hdr
, &ret
);
295 req
->peer
= hdr
->peer
;
296 req
->status_msg
= true;
297 req
->status_code
= ret
;
299 ubus_req_complete_cb(req
);
304 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
306 struct ubus_pending_data
*data
;
311 req_data_cb(req
, hdr
->type
, hdr
->data
);
312 ubus_process_req_data(req
);
313 req
->blocked
= false;
316 ubus_req_complete_cb(req
);
321 len
= blob_raw_len(hdr
->data
);
322 data
= calloc(1, sizeof(*data
) + len
);
326 data
->type
= hdr
->type
;
327 memcpy(data
->data
, hdr
->data
, len
);
328 list_add(&data
->list
, &req
->pending
);
331 static struct ubus_request
*ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
333 struct ubus_request
*req
;
335 list_for_each_entry(req
, &ctx
->requests
, list
) {
336 if (seq
!= req
->seq
|| peer
!= req
->peer
)
344 static void ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
346 struct ubus_request_data req
;
347 struct ubus_object
*obj
;
352 req
.peer
= hdr
->peer
;
354 ubus_parse_msg(hdr
->data
);
356 if (!attrbuf
[UBUS_ATTR_OBJID
])
359 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
361 if (!attrbuf
[UBUS_ATTR_METHOD
]) {
362 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
366 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
368 ret
= UBUS_STATUS_NOT_FOUND
;
372 for (method
= 0; method
< obj
->n_methods
; method
++)
373 if (!obj
->methods
[method
].name
||
374 !strcmp(obj
->methods
[method
].name
,
375 blob_data(attrbuf
[UBUS_ATTR_METHOD
])))
379 ret
= UBUS_STATUS_METHOD_NOT_FOUND
;
384 ret
= obj
->methods
[method
].handler(ctx
, obj
, &req
,
385 blob_data(attrbuf
[UBUS_ATTR_METHOD
]),
386 attrbuf
[UBUS_ATTR_DATA
]);
389 blob_buf_init(&b
, 0);
390 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
391 blob_put_int32(&b
, UBUS_ATTR_OBJID
, objid
);
392 ubus_send_msg(ctx
, req
.seq
, b
.head
, UBUS_MSG_STATUS
, req
.peer
);
395 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
397 struct ubus_pending_invoke
*pending
;
398 struct ubus_request
*req
;
401 case UBUS_MSG_STATUS
:
402 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
406 ubus_process_req_status(req
, hdr
);
410 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
411 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
412 ubus_req_data(req
, hdr
);
415 case UBUS_MSG_INVOKE
:
416 if (ctx
->stack_depth
> 2) {
417 pending
= calloc(1, sizeof(*pending
) +
418 blob_raw_len(hdr
->data
));
423 memcpy(&pending
->hdr
, hdr
, sizeof(*hdr
) +
424 blob_raw_len(hdr
->data
));
425 list_add(&pending
->list
, &ctx
->pending
);
427 ubus_process_invoke(ctx
, hdr
);
433 static void ubus_process_pending_invoke(struct ubus_context
*ctx
)
435 struct ubus_pending_invoke
*pending
, *tmp
;
437 list_for_each_entry_safe(pending
, tmp
, &ctx
->pending
, list
) {
438 list_del(&pending
->list
);
439 ubus_process_msg(ctx
, &pending
->hdr
);
441 if (ctx
->stack_depth
> 2)
446 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
448 if (!list_empty(&req
->list
))
451 req
->cancelled
= true;
452 ubus_process_req_data(req
);
453 list_del_init(&req
->list
);
456 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
458 if (!list_empty(&req
->list
))
461 list_add(&req
->list
, &ctx
->requests
);
464 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
466 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
467 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
469 while (get_next_msg(ctx
)) {
470 ubus_process_msg(ctx
, hdr
);
476 ctx
->connection_lost(ctx
);
479 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
481 req
->status_msg
= true;
482 req
->status_code
= ret
;
486 struct ubus_sync_req_cb
{
487 struct uloop_timeout timeout
;
488 struct ubus_request
*req
;
491 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
493 struct ubus_sync_req_cb
*cb
;
495 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
496 ubus_sync_req_cb(cb
->req
, UBUS_STATUS_TIMEOUT
);
499 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
502 struct ubus_sync_req_cb cb
;
503 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
504 bool registered
= ctx
->sock
.registered
;
505 int status
= UBUS_STATUS_NO_DATA
;
513 memset(&cb
, 0, sizeof(cb
));
515 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
516 uloop_timeout_set(&cb
.timeout
, timeout
);
519 ubus_complete_request_async(ctx
, req
);
520 req
->complete_cb
= ubus_sync_req_cb
;
523 while (!req
->status_msg
) {
524 bool cancelled
= uloop_cancelled
;
525 uloop_cancelled
= false;
527 uloop_cancelled
= cancelled
;
532 uloop_timeout_cancel(&cb
.timeout
);
535 status
= req
->status_code
;
537 req
->complete_cb
= complete_cb
;
538 if (req
->complete_cb
)
539 req
->complete_cb(req
, status
);
542 uloop_fd_delete(&ctx
->sock
);
544 if (!ctx
->stack_depth
)
545 ubus_process_pending_invoke(ctx
);
550 struct ubus_lookup_request
{
551 struct ubus_request req
;
552 ubus_lookup_handler_t cb
;
555 static void ubus_lookup_cb(struct ubus_request
*ureq
, int type
, struct blob_attr
*msg
)
557 struct ubus_lookup_request
*req
;
558 struct ubus_object_data obj
;
559 struct blob_attr
**attr
;
561 req
= container_of(ureq
, struct ubus_lookup_request
, req
);
562 attr
= ubus_parse_msg(msg
);
564 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_OBJPATH
] ||
565 !attr
[UBUS_ATTR_OBJTYPE
])
568 memset(&obj
, 0, sizeof(obj
));
569 obj
.id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
570 obj
.path
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
571 obj
.type_id
= blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]);
572 obj
.signature
= attr
[UBUS_ATTR_SIGNATURE
];
573 req
->cb(ureq
->ctx
, &obj
, ureq
->priv
);
576 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
577 ubus_lookup_handler_t cb
, void *priv
)
579 struct ubus_lookup_request lookup
;
581 blob_buf_init(&b
, 0);
583 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
585 if (ubus_start_request(ctx
, &lookup
.req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
586 return UBUS_STATUS_INVALID_ARGUMENT
;
588 lookup
.req
.raw_data_cb
= ubus_lookup_cb
;
589 lookup
.req
.priv
= priv
;
591 return ubus_complete_request(ctx
, &lookup
.req
, 0);
594 static void ubus_lookup_id_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
596 struct blob_attr
**attr
;
597 uint32_t *id
= req
->priv
;
599 attr
= ubus_parse_msg(msg
);
601 if (!attr
[UBUS_ATTR_OBJID
])
604 *id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
607 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
)
609 struct ubus_request req
;
611 blob_buf_init(&b
, 0);
613 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
615 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
616 return UBUS_STATUS_INVALID_ARGUMENT
;
618 req
.raw_data_cb
= ubus_lookup_id_cb
;
621 return ubus_complete_request(ctx
, &req
, 0);
624 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
625 struct blob_attr
*msg
)
629 blob_buf_init(&b
, 0);
630 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
631 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
632 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
634 return UBUS_STATUS_NO_DATA
;
639 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
640 struct blob_attr
*msg
, struct ubus_request
*req
)
642 blob_buf_init(&b
, 0);
643 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
644 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
646 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
648 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
649 return UBUS_STATUS_INVALID_ARGUMENT
;
654 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
655 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
658 struct ubus_request req
;
660 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
663 return ubus_complete_request(ctx
, &req
, timeout
);
666 static void ubus_add_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
668 struct ubus_object
*obj
= req
->priv
;
672 if (!attrbuf
[UBUS_ATTR_OBJID
])
675 obj
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
677 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
678 obj
->type
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
680 obj
->avl
.key
= &obj
->id
;
681 avl_insert(&req
->ctx
->objects
, &obj
->avl
);
684 static void ubus_push_method_data(const struct ubus_method
*m
)
689 mtbl
= blobmsg_open_table(&b
, m
->name
);
691 for (i
= 0; i
< m
->n_policy
; i
++)
692 blobmsg_add_u32(&b
, m
->policy
[i
].name
, m
->policy
[i
].type
);
694 blobmsg_close_table(&b
, mtbl
);
697 static bool ubus_push_object_type(const struct ubus_object_type
*type
)
702 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
704 for (i
= 0; i
< type
->n_methods
; i
++)
705 ubus_push_method_data(&type
->methods
[i
]);
707 blob_nest_end(&b
, s
);
712 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
714 struct ubus_request req
;
717 blob_buf_init(&b
, 0);
719 if (obj
->name
&& obj
->type
) {
720 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
723 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
724 else if (!ubus_push_object_type(obj
->type
))
725 return UBUS_STATUS_INVALID_ARGUMENT
;
728 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_ADD_OBJECT
, 0) < 0)
729 return UBUS_STATUS_INVALID_ARGUMENT
;
731 req
.raw_data_cb
= ubus_add_object_cb
;
733 ret
= ubus_complete_request(ctx
, &req
, 0);
738 return UBUS_STATUS_NO_DATA
;
743 static void ubus_remove_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
745 struct ubus_object
*obj
= req
->priv
;
749 if (!attrbuf
[UBUS_ATTR_OBJID
])
754 if (attrbuf
[UBUS_ATTR_OBJTYPE
] && obj
->type
)
757 avl_delete(&req
->ctx
->objects
, &obj
->avl
);
760 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
762 struct ubus_request req
;
765 blob_buf_init(&b
, 0);
766 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
768 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_REMOVE_OBJECT
, 0) < 0)
769 return UBUS_STATUS_INVALID_ARGUMENT
;
771 req
.raw_data_cb
= ubus_remove_object_cb
;
773 ret
= ubus_complete_request(ctx
, &req
, 0);
778 return UBUS_STATUS_NO_DATA
;
783 static int ubus_event_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
784 struct ubus_request_data
*req
,
785 const char *method
, struct blob_attr
*msg
)
787 struct ubus_event_handler
*ev
;
789 ev
= container_of(obj
, struct ubus_event_handler
, obj
);
790 ev
->cb(ctx
, ev
, method
, msg
);
794 static const struct ubus_method event_method
= {
796 .handler
= ubus_event_cb
,
799 int ubus_register_event_handler(struct ubus_context
*ctx
,
800 struct ubus_event_handler
*ev
,
803 struct ubus_object
*obj
= &ev
->obj
;
808 obj
->methods
= &event_method
;
811 if (!!obj
->name
^ !!obj
->type
)
812 return UBUS_STATUS_INVALID_ARGUMENT
;
814 ret
= ubus_add_object(ctx
, obj
);
819 /* use a second buffer, ubus_invoke() overwrites the primary one */
820 memset(&b2
, 0, sizeof(b2
));
821 blob_buf_init(&b2
, 0);
822 blobmsg_add_u32(&b2
, "object", obj
->id
);
824 blobmsg_add_string(&b2
, "pattern", pattern
);
826 return ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_EVENT
, "register", b2
.head
,
836 static const struct blobmsg_policy watch_policy
[] = {
837 [WATCH_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
838 [WATCH_NOTIFY
] = { .name
= "notify", .type
= BLOBMSG_TYPE_STRING
},
842 static int ubus_watch_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
843 struct ubus_request_data
*req
,
844 const char *method
, struct blob_attr
*msg
)
846 struct ubus_watch_object
*w
;
847 struct blob_attr
*tb
[__WATCH_MAX
];
849 blobmsg_parse(watch_policy
, ARRAY_SIZE(watch_policy
), tb
, blob_data(msg
), blob_len(msg
));
851 if (!tb
[WATCH_ID
] || !tb
[WATCH_NOTIFY
])
852 return UBUS_STATUS_INVALID_ARGUMENT
;
855 return UBUS_STATUS_INVALID_ARGUMENT
;
857 w
= container_of(obj
, struct ubus_watch_object
, obj
);
858 w
->cb(ctx
, w
, blobmsg_get_u32(tb
[WATCH_ID
]));
862 static const struct ubus_method watch_method
= {
864 .handler
= ubus_watch_cb
,
867 int ubus_register_watch_object(struct ubus_context
*ctx
, struct ubus_watch_object
*w_obj
)
869 struct ubus_object
*obj
= &w_obj
->obj
;
871 obj
->methods
= &watch_method
;
874 return ubus_add_object(ctx
, obj
);
878 __ubus_watch_request(struct ubus_context
*ctx
, struct ubus_object
*obj
, uint32_t id
, const char *method
, int type
)
880 struct ubus_request req
;
882 blob_buf_init(&b
, 0);
883 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
884 blob_put_int32(&b
, UBUS_ATTR_TARGET
, id
);
886 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
888 if (ubus_start_request(ctx
, &req
, b
.head
, type
, 0) < 0)
889 return UBUS_STATUS_INVALID_ARGUMENT
;
891 return ubus_complete_request(ctx
, &req
, 0);
895 int ubus_watch_object_add(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
)
897 return __ubus_watch_request(ctx
, &obj
->obj
, id
, "event", UBUS_MSG_ADD_WATCH
);
900 int ubus_watch_object_remove(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
)
902 return __ubus_watch_request(ctx
, &obj
->obj
, id
, NULL
, UBUS_MSG_REMOVE_WATCH
);
905 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
906 struct blob_attr
*data
)
908 struct ubus_request req
;
911 blob_buf_init(&b
, 0);
912 blob_put_int32(&b
, UBUS_ATTR_OBJID
, UBUS_SYSTEM_OBJECT_EVENT
);
913 blob_put_string(&b
, UBUS_ATTR_METHOD
, "send");
914 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
915 blobmsg_add_string(&b
, "id", id
);
916 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, "data", blob_data(data
), blob_len(data
));
917 blob_nest_end(&b
, s
);
919 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_INVOKE
, UBUS_SYSTEM_OBJECT_EVENT
) < 0)
920 return UBUS_STATUS_INVALID_ARGUMENT
;
922 return ubus_complete_request(ctx
, &req
, 0);
925 static void ubus_default_connection_lost(struct ubus_context
*ctx
)
927 if (ctx
->sock
.registered
)
931 struct ubus_context
*ubus_connect(const char *path
)
933 struct ubus_context
*ctx
;
935 struct ubus_msghdr hdr
;
936 struct blob_attr data
;
938 struct blob_attr
*buf
;
941 path
= UBUS_UNIX_SOCKET
;
943 ctx
= calloc(1, sizeof(*ctx
));
947 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
948 if (ctx
->sock
.fd
< 0)
951 ctx
->sock
.cb
= ubus_handle_data
;
953 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
956 if (!ubus_validate_hdr(&hdr
.hdr
))
959 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
)
962 buf
= calloc(1, blob_raw_len(&hdr
.data
));
966 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
967 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
))
970 ctx
->local_id
= hdr
.hdr
.peer
;
973 ctx
->connection_lost
= ubus_default_connection_lost
;
975 INIT_LIST_HEAD(&ctx
->requests
);
976 INIT_LIST_HEAD(&ctx
->pending
);
977 avl_init(&ctx
->objects
, ubus_cmp_id
, false, NULL
);
982 fcntl(ctx
->sock
.fd
, F_SETFL
, fcntl(ctx
->sock
.fd
, F_GETFL
) | O_NONBLOCK
);
996 void ubus_free(struct ubus_context
*ctx
)