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",
41 [UBUS_STATUS_CONNECTION_FAILED
] = "Connection failed",
44 static struct blob_buf b
;
46 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
47 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
48 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
49 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
50 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
52 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
54 struct ubus_pending_data
{
55 struct list_head list
;
57 struct blob_attr data
[];
60 struct ubus_pending_invoke
{
61 struct list_head list
;
62 struct ubus_msghdr hdr
;
65 static void ubus_process_pending_invoke(struct ubus_context
*ctx
);
67 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
69 const uint32_t *id1
= k1
, *id2
= k2
;
77 static struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
79 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
83 const char *ubus_strerror(int error
)
87 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
90 if (!__ubus_strerror
[error
])
93 return __ubus_strerror
[error
];
96 sprintf(err
, "Unknown error: %d", error
);
100 static void wait_data(int fd
, bool write
)
102 struct pollfd pfd
= { .fd
= fd
};
104 pfd
.events
= write
? POLLOUT
: POLLIN
;
108 static int writev_retry(int fd
, struct iovec
*iov
, int iov_len
)
113 int cur_len
= writev(fd
, iov
, iov_len
);
127 while (cur_len
>= iov
->iov_len
) {
128 cur_len
-= iov
->iov_len
;
131 if (!cur_len
|| !iov_len
)
134 iov
->iov_len
-= cur_len
;
138 static int ubus_send_msg(struct ubus_context
*ctx
, uint32_t seq
,
139 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
141 struct ubus_msghdr hdr
;
142 struct iovec iov
[2] = {
152 blob_buf_init(&b
, 0);
156 iov
[1].iov_base
= (char *) msg
;
157 iov
[1].iov_len
= blob_raw_len(msg
);
159 return writev_retry(ctx
->sock
.fd
, iov
, ARRAY_SIZE(iov
));
162 static int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
163 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
165 memset(req
, 0, sizeof(*req
));
167 if (msg
&& blob_pad_len(msg
) > UBUS_MAX_MSGLEN
)
170 INIT_LIST_HEAD(&req
->list
);
171 INIT_LIST_HEAD(&req
->pending
);
174 req
->seq
= ++ctx
->request_seq
;
175 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
178 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
182 while (iov
->iov_len
> 0) {
184 wait_data(fd
, false);
186 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
201 iov
->iov_len
-= bytes
;
202 iov
->iov_base
+= bytes
;
208 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
210 if (hdr
->version
!= 0)
213 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
216 if (blob_pad_len(hdr
->data
) > UBUS_MAX_MSGLEN
)
222 static bool get_next_msg(struct ubus_context
*ctx
)
224 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
226 /* receive header + start attribute */
227 iov
.iov_len
+= sizeof(struct blob_attr
);
228 if (!recv_retry(ctx
->sock
.fd
, &iov
, false))
231 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
232 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
235 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
238 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
240 ubus_parse_msg(hdr
->data
);
242 if (!attrbuf
[UBUS_ATTR_STATUS
])
245 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
249 static void req_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*data
)
251 struct blob_attr
**attr
;
253 if (req
->raw_data_cb
)
254 req
->raw_data_cb(req
, type
, data
);
259 attr
= ubus_parse_msg(data
);
260 req
->data_cb(req
, type
, attr
[UBUS_ATTR_DATA
]);
263 static void ubus_process_req_data(struct ubus_request
*req
)
265 struct ubus_pending_data
*data
;
267 while (!list_empty(&req
->pending
)) {
268 data
= list_first_entry(&req
->pending
,
269 struct ubus_pending_data
, list
);
270 list_del(&data
->list
);
272 req_data_cb(req
, data
->type
, data
->data
);
277 static void ubus_req_complete_cb(struct ubus_request
*req
)
279 ubus_complete_handler_t cb
= req
->complete_cb
;
284 req
->complete_cb
= NULL
;
285 cb(req
, req
->status_code
);
288 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
290 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
292 if (!list_empty(&req
->list
))
293 list_del_init(&req
->list
);
295 ubus_get_status(hdr
, &ret
);
296 req
->peer
= hdr
->peer
;
297 req
->status_msg
= true;
298 req
->status_code
= ret
;
300 ubus_req_complete_cb(req
);
305 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
307 struct ubus_pending_data
*data
;
312 req_data_cb(req
, hdr
->type
, hdr
->data
);
313 ubus_process_req_data(req
);
314 req
->blocked
= false;
317 ubus_req_complete_cb(req
);
322 len
= blob_raw_len(hdr
->data
);
323 data
= calloc(1, sizeof(*data
) + len
);
327 data
->type
= hdr
->type
;
328 memcpy(data
->data
, hdr
->data
, len
);
329 list_add(&data
->list
, &req
->pending
);
332 static struct ubus_request
*ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
334 struct ubus_request
*req
;
336 list_for_each_entry(req
, &ctx
->requests
, list
) {
337 if (seq
!= req
->seq
|| peer
!= req
->peer
)
345 static void ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
347 struct ubus_request_data req
;
348 struct ubus_object
*obj
;
353 req
.peer
= hdr
->peer
;
355 ubus_parse_msg(hdr
->data
);
357 if (!attrbuf
[UBUS_ATTR_OBJID
])
360 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
362 if (!attrbuf
[UBUS_ATTR_METHOD
]) {
363 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
367 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
369 ret
= UBUS_STATUS_NOT_FOUND
;
373 for (method
= 0; method
< obj
->n_methods
; method
++)
374 if (!obj
->methods
[method
].name
||
375 !strcmp(obj
->methods
[method
].name
,
376 blob_data(attrbuf
[UBUS_ATTR_METHOD
])))
380 ret
= UBUS_STATUS_METHOD_NOT_FOUND
;
385 ret
= obj
->methods
[method
].handler(ctx
, obj
, &req
,
386 blob_data(attrbuf
[UBUS_ATTR_METHOD
]),
387 attrbuf
[UBUS_ATTR_DATA
]);
390 blob_buf_init(&b
, 0);
391 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
392 blob_put_int32(&b
, UBUS_ATTR_OBJID
, objid
);
393 ubus_send_msg(ctx
, req
.seq
, b
.head
, UBUS_MSG_STATUS
, req
.peer
);
396 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
398 struct ubus_pending_invoke
*pending
;
399 struct ubus_request
*req
;
402 case UBUS_MSG_STATUS
:
403 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
407 ubus_process_req_status(req
, hdr
);
411 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
412 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
413 ubus_req_data(req
, hdr
);
416 case UBUS_MSG_INVOKE
:
417 if (ctx
->stack_depth
> 2) {
418 pending
= calloc(1, sizeof(*pending
) +
419 blob_raw_len(hdr
->data
));
424 memcpy(&pending
->hdr
, hdr
, sizeof(*hdr
) +
425 blob_raw_len(hdr
->data
));
426 list_add(&pending
->list
, &ctx
->pending
);
428 ubus_process_invoke(ctx
, hdr
);
434 static void ubus_process_pending_invoke(struct ubus_context
*ctx
)
436 struct ubus_pending_invoke
*pending
, *tmp
;
438 list_for_each_entry_safe(pending
, tmp
, &ctx
->pending
, list
) {
439 list_del(&pending
->list
);
440 ubus_process_msg(ctx
, &pending
->hdr
);
442 if (ctx
->stack_depth
> 2)
447 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
449 if (!list_empty(&req
->list
))
452 req
->cancelled
= true;
453 ubus_process_req_data(req
);
454 list_del_init(&req
->list
);
457 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
459 if (!list_empty(&req
->list
))
462 list_add(&req
->list
, &ctx
->requests
);
465 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
467 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
468 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
470 while (get_next_msg(ctx
)) {
471 ubus_process_msg(ctx
, hdr
);
477 ctx
->connection_lost(ctx
);
480 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
482 req
->status_msg
= true;
483 req
->status_code
= ret
;
487 struct ubus_sync_req_cb
{
488 struct uloop_timeout timeout
;
489 struct ubus_request
*req
;
492 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
494 struct ubus_sync_req_cb
*cb
;
496 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
497 ubus_sync_req_cb(cb
->req
, UBUS_STATUS_TIMEOUT
);
500 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
503 struct ubus_sync_req_cb cb
;
504 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
505 bool registered
= ctx
->sock
.registered
;
506 int status
= UBUS_STATUS_NO_DATA
;
514 memset(&cb
, 0, sizeof(cb
));
516 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
517 uloop_timeout_set(&cb
.timeout
, timeout
);
520 ubus_complete_request_async(ctx
, req
);
521 req
->complete_cb
= ubus_sync_req_cb
;
524 while (!req
->status_msg
) {
525 bool cancelled
= uloop_cancelled
;
526 uloop_cancelled
= false;
528 uloop_cancelled
= cancelled
;
533 uloop_timeout_cancel(&cb
.timeout
);
536 status
= req
->status_code
;
538 req
->complete_cb
= complete_cb
;
539 if (req
->complete_cb
)
540 req
->complete_cb(req
, status
);
543 uloop_fd_delete(&ctx
->sock
);
545 if (!ctx
->stack_depth
)
546 ubus_process_pending_invoke(ctx
);
551 struct ubus_lookup_request
{
552 struct ubus_request req
;
553 ubus_lookup_handler_t cb
;
556 static void ubus_lookup_cb(struct ubus_request
*ureq
, int type
, struct blob_attr
*msg
)
558 struct ubus_lookup_request
*req
;
559 struct ubus_object_data obj
;
560 struct blob_attr
**attr
;
562 req
= container_of(ureq
, struct ubus_lookup_request
, req
);
563 attr
= ubus_parse_msg(msg
);
565 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_OBJPATH
] ||
566 !attr
[UBUS_ATTR_OBJTYPE
])
569 memset(&obj
, 0, sizeof(obj
));
570 obj
.id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
571 obj
.path
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
572 obj
.type_id
= blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]);
573 obj
.signature
= attr
[UBUS_ATTR_SIGNATURE
];
574 req
->cb(ureq
->ctx
, &obj
, ureq
->priv
);
577 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
578 ubus_lookup_handler_t cb
, void *priv
)
580 struct ubus_lookup_request lookup
;
582 blob_buf_init(&b
, 0);
584 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
586 if (ubus_start_request(ctx
, &lookup
.req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
587 return UBUS_STATUS_INVALID_ARGUMENT
;
589 lookup
.req
.raw_data_cb
= ubus_lookup_cb
;
590 lookup
.req
.priv
= priv
;
592 return ubus_complete_request(ctx
, &lookup
.req
, 0);
595 static void ubus_lookup_id_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
597 struct blob_attr
**attr
;
598 uint32_t *id
= req
->priv
;
600 attr
= ubus_parse_msg(msg
);
602 if (!attr
[UBUS_ATTR_OBJID
])
605 *id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
608 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
)
610 struct ubus_request req
;
612 blob_buf_init(&b
, 0);
614 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
616 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
617 return UBUS_STATUS_INVALID_ARGUMENT
;
619 req
.raw_data_cb
= ubus_lookup_id_cb
;
622 return ubus_complete_request(ctx
, &req
, 0);
625 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
626 struct blob_attr
*msg
)
630 blob_buf_init(&b
, 0);
631 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
632 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
633 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
635 return UBUS_STATUS_NO_DATA
;
640 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
641 struct blob_attr
*msg
, struct ubus_request
*req
)
643 blob_buf_init(&b
, 0);
644 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
645 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
647 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
649 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
650 return UBUS_STATUS_INVALID_ARGUMENT
;
655 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
656 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
659 struct ubus_request req
;
661 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
664 return ubus_complete_request(ctx
, &req
, timeout
);
667 static void ubus_add_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
669 struct ubus_object
*obj
= req
->priv
;
673 if (!attrbuf
[UBUS_ATTR_OBJID
])
676 obj
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
678 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
679 obj
->type
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
681 obj
->avl
.key
= &obj
->id
;
682 avl_insert(&req
->ctx
->objects
, &obj
->avl
);
685 static void ubus_push_method_data(const struct ubus_method
*m
)
690 mtbl
= blobmsg_open_table(&b
, m
->name
);
692 for (i
= 0; i
< m
->n_policy
; i
++)
693 blobmsg_add_u32(&b
, m
->policy
[i
].name
, m
->policy
[i
].type
);
695 blobmsg_close_table(&b
, mtbl
);
698 static bool ubus_push_object_type(const struct ubus_object_type
*type
)
703 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
705 for (i
= 0; i
< type
->n_methods
; i
++)
706 ubus_push_method_data(&type
->methods
[i
]);
708 blob_nest_end(&b
, s
);
713 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
715 struct ubus_request req
;
718 blob_buf_init(&b
, 0);
720 if (obj
->name
&& obj
->type
) {
721 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
724 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
725 else if (!ubus_push_object_type(obj
->type
))
726 return UBUS_STATUS_INVALID_ARGUMENT
;
729 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_ADD_OBJECT
, 0) < 0)
730 return UBUS_STATUS_INVALID_ARGUMENT
;
732 req
.raw_data_cb
= ubus_add_object_cb
;
734 ret
= ubus_complete_request(ctx
, &req
, 0);
739 return UBUS_STATUS_NO_DATA
;
744 static void ubus_remove_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
746 struct ubus_object
*obj
= req
->priv
;
750 if (!attrbuf
[UBUS_ATTR_OBJID
])
755 if (attrbuf
[UBUS_ATTR_OBJTYPE
] && obj
->type
)
758 avl_delete(&req
->ctx
->objects
, &obj
->avl
);
761 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
763 struct ubus_request req
;
766 blob_buf_init(&b
, 0);
767 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
769 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_REMOVE_OBJECT
, 0) < 0)
770 return UBUS_STATUS_INVALID_ARGUMENT
;
772 req
.raw_data_cb
= ubus_remove_object_cb
;
774 ret
= ubus_complete_request(ctx
, &req
, 0);
779 return UBUS_STATUS_NO_DATA
;
784 static int ubus_event_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
785 struct ubus_request_data
*req
,
786 const char *method
, struct blob_attr
*msg
)
788 struct ubus_event_handler
*ev
;
790 ev
= container_of(obj
, struct ubus_event_handler
, obj
);
791 ev
->cb(ctx
, ev
, method
, msg
);
795 static const struct ubus_method event_method
= {
797 .handler
= ubus_event_cb
,
800 int ubus_register_event_handler(struct ubus_context
*ctx
,
801 struct ubus_event_handler
*ev
,
804 struct ubus_object
*obj
= &ev
->obj
;
809 obj
->methods
= &event_method
;
812 if (!!obj
->name
^ !!obj
->type
)
813 return UBUS_STATUS_INVALID_ARGUMENT
;
815 ret
= ubus_add_object(ctx
, obj
);
820 /* use a second buffer, ubus_invoke() overwrites the primary one */
821 memset(&b2
, 0, sizeof(b2
));
822 blob_buf_init(&b2
, 0);
823 blobmsg_add_u32(&b2
, "object", obj
->id
);
825 blobmsg_add_string(&b2
, "pattern", pattern
);
827 return ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_EVENT
, "register", b2
.head
,
837 static const struct blobmsg_policy watch_policy
[] = {
838 [WATCH_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_INT32
},
839 [WATCH_NOTIFY
] = { .name
= "notify", .type
= BLOBMSG_TYPE_STRING
},
843 static int ubus_watch_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
844 struct ubus_request_data
*req
,
845 const char *method
, struct blob_attr
*msg
)
847 struct ubus_watch_object
*w
;
848 struct blob_attr
*tb
[__WATCH_MAX
];
850 blobmsg_parse(watch_policy
, ARRAY_SIZE(watch_policy
), tb
, blob_data(msg
), blob_len(msg
));
852 if (!tb
[WATCH_ID
] || !tb
[WATCH_NOTIFY
])
853 return UBUS_STATUS_INVALID_ARGUMENT
;
856 return UBUS_STATUS_INVALID_ARGUMENT
;
858 w
= container_of(obj
, struct ubus_watch_object
, obj
);
859 w
->cb(ctx
, w
, blobmsg_get_u32(tb
[WATCH_ID
]));
863 static const struct ubus_method watch_method
= {
865 .handler
= ubus_watch_cb
,
868 int ubus_register_watch_object(struct ubus_context
*ctx
, struct ubus_watch_object
*w_obj
)
870 struct ubus_object
*obj
= &w_obj
->obj
;
872 obj
->methods
= &watch_method
;
875 return ubus_add_object(ctx
, obj
);
879 __ubus_watch_request(struct ubus_context
*ctx
, struct ubus_object
*obj
, uint32_t id
, const char *method
, int type
)
881 struct ubus_request req
;
883 blob_buf_init(&b
, 0);
884 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
885 blob_put_int32(&b
, UBUS_ATTR_TARGET
, id
);
887 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
889 if (ubus_start_request(ctx
, &req
, b
.head
, type
, 0) < 0)
890 return UBUS_STATUS_INVALID_ARGUMENT
;
892 return ubus_complete_request(ctx
, &req
, 0);
896 int ubus_watch_object_add(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
)
898 return __ubus_watch_request(ctx
, &obj
->obj
, id
, "event", UBUS_MSG_ADD_WATCH
);
901 int ubus_watch_object_remove(struct ubus_context
*ctx
, struct ubus_watch_object
*obj
, uint32_t id
)
903 return __ubus_watch_request(ctx
, &obj
->obj
, id
, NULL
, UBUS_MSG_REMOVE_WATCH
);
906 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
907 struct blob_attr
*data
)
909 struct ubus_request req
;
912 blob_buf_init(&b
, 0);
913 blob_put_int32(&b
, UBUS_ATTR_OBJID
, UBUS_SYSTEM_OBJECT_EVENT
);
914 blob_put_string(&b
, UBUS_ATTR_METHOD
, "send");
915 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
916 blobmsg_add_string(&b
, "id", id
);
917 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, "data", blob_data(data
), blob_len(data
));
918 blob_nest_end(&b
, s
);
920 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_INVOKE
, UBUS_SYSTEM_OBJECT_EVENT
) < 0)
921 return UBUS_STATUS_INVALID_ARGUMENT
;
923 return ubus_complete_request(ctx
, &req
, 0);
927 ubus_refresh_state(struct ubus_context
*ctx
)
929 struct ubus_object
*obj
, *tmp
;
931 /* clear all type IDs, they need to be registered again */
932 avl_for_each_element(&ctx
->objects
, obj
, avl
)
935 /* push out all objects again */
936 avl_for_each_element_safe(&ctx
->objects
, obj
, avl
, tmp
) {
938 avl_delete(&ctx
->objects
, &obj
->avl
);
939 ubus_add_object(ctx
, obj
);
943 int ubus_reconnect(struct ubus_context
*ctx
, const char *path
)
946 struct ubus_msghdr hdr
;
947 struct blob_attr data
;
949 struct blob_attr
*buf
;
950 int ret
= UBUS_STATUS_UNKNOWN_ERROR
;
953 path
= UBUS_UNIX_SOCKET
;
955 if (ctx
->sock
.fd
>= 0) {
956 if (ctx
->sock
.registered
)
957 uloop_fd_delete(&ctx
->sock
);
962 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
963 if (ctx
->sock
.fd
< 0)
964 return UBUS_STATUS_CONNECTION_FAILED
;
966 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
969 if (!ubus_validate_hdr(&hdr
.hdr
))
972 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
)
975 buf
= calloc(1, blob_raw_len(&hdr
.data
));
979 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
980 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
))
983 ctx
->local_id
= hdr
.hdr
.peer
;
987 ret
= UBUS_STATUS_OK
;
988 fcntl(ctx
->sock
.fd
, F_SETFL
, fcntl(ctx
->sock
.fd
, F_GETFL
) | O_NONBLOCK
);
990 ubus_refresh_state(ctx
);
1001 static void ubus_default_connection_lost(struct ubus_context
*ctx
)
1003 if (ctx
->sock
.registered
)
1007 struct ubus_context
*ubus_connect(const char *path
)
1009 struct ubus_context
*ctx
;
1011 ctx
= calloc(1, sizeof(*ctx
));
1016 ctx
->sock
.cb
= ubus_handle_data
;
1017 ctx
->connection_lost
= ubus_default_connection_lost
;
1019 INIT_LIST_HEAD(&ctx
->requests
);
1020 INIT_LIST_HEAD(&ctx
->pending
);
1021 avl_init(&ctx
->objects
, ubus_cmp_id
, false, NULL
);
1022 if (ubus_reconnect(ctx
, path
)) {
1030 void ubus_free(struct ubus_context
*ctx
)
1032 close(ctx
->sock
.fd
);