2011-06-17 16:35:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 2.1
|
|
|
|
* as published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
2011-02-11 00:36:10 +01:00
|
|
|
#include <fcntl.h>
|
2010-12-06 03:51:58 +01:00
|
|
|
#include <unistd.h>
|
2011-09-09 17:53:22 +02:00
|
|
|
#include <poll.h>
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
#include <libubox/blob.h>
|
|
|
|
#include <libubox/blobmsg.h>
|
|
|
|
#include <libubox/usock.h>
|
|
|
|
|
|
|
|
#include "libubus.h"
|
|
|
|
#include "ubusmsg.h"
|
|
|
|
|
|
|
|
#define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
|
|
|
|
|
|
|
|
const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
|
|
|
|
[UBUS_STATUS_OK] = "Success",
|
|
|
|
[UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
|
|
|
|
[UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
|
2011-01-31 03:26:53 +01:00
|
|
|
[UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
|
2010-12-06 03:51:58 +01:00
|
|
|
[UBUS_STATUS_NOT_FOUND] = "Not found",
|
|
|
|
[UBUS_STATUS_NO_DATA] = "No response",
|
2011-02-05 19:53:14 +01:00
|
|
|
[UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
|
2011-02-11 02:40:39 +01:00
|
|
|
[UBUS_STATUS_TIMEOUT] = "Request timed out",
|
2011-09-05 03:16:59 +02:00
|
|
|
[UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
|
2011-09-05 03:20:07 +02:00
|
|
|
[UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
|
2012-05-29 00:56:37 +02:00
|
|
|
[UBUS_STATUS_CONNECTION_FAILED] = "Connection failed",
|
2010-12-06 03:51:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct blob_buf b;
|
|
|
|
|
|
|
|
static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
|
|
|
|
[UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
|
|
|
|
[UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
|
|
|
|
[UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
|
2011-01-31 03:53:09 +01:00
|
|
|
[UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
|
2010-12-06 03:51:58 +01:00
|
|
|
};
|
|
|
|
static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
|
|
|
|
|
|
|
|
struct ubus_pending_data {
|
|
|
|
struct list_head list;
|
|
|
|
int type;
|
|
|
|
struct blob_attr data[];
|
|
|
|
};
|
|
|
|
|
2011-10-27 14:03:19 +02:00
|
|
|
struct ubus_pending_invoke {
|
|
|
|
struct list_head list;
|
|
|
|
struct ubus_msghdr hdr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ubus_process_pending_invoke(struct ubus_context *ctx);
|
|
|
|
|
2011-01-31 03:07:42 +01:00
|
|
|
static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
|
|
|
|
{
|
|
|
|
const uint32_t *id1 = k1, *id2 = k2;
|
|
|
|
|
|
|
|
if (*id1 < *id2)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return *id1 > *id2;
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:28:14 +01:00
|
|
|
static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
|
|
|
|
return attrbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ubus_strerror(int error)
|
|
|
|
{
|
|
|
|
static char err[32];
|
|
|
|
|
|
|
|
if (error < 0 || error >= __UBUS_STATUS_LAST)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!__ubus_strerror[error])
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
return __ubus_strerror[error];
|
|
|
|
|
|
|
|
out:
|
|
|
|
sprintf(err, "Unknown error: %d", error);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-09-09 17:53:22 +02:00
|
|
|
static void wait_data(int fd, bool write)
|
|
|
|
{
|
|
|
|
struct pollfd pfd = { .fd = fd };
|
|
|
|
|
|
|
|
pfd.events = write ? POLLOUT : POLLIN;
|
|
|
|
poll(&pfd, 1, 0);
|
|
|
|
}
|
|
|
|
|
2011-02-11 00:36:10 +01:00
|
|
|
static int writev_retry(int fd, struct iovec *iov, int iov_len)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int cur_len = writev(fd, iov, iov_len);
|
|
|
|
if (cur_len < 0) {
|
|
|
|
switch(errno) {
|
|
|
|
case EAGAIN:
|
2011-09-09 17:53:22 +02:00
|
|
|
wait_data(fd, true);
|
2011-02-11 00:36:10 +01:00
|
|
|
break;
|
|
|
|
case EINTR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len += cur_len;
|
|
|
|
while (cur_len >= iov->iov_len) {
|
|
|
|
cur_len -= iov->iov_len;
|
|
|
|
iov_len--;
|
|
|
|
iov++;
|
|
|
|
if (!cur_len || !iov_len)
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
iov->iov_len -= cur_len;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2011-01-31 02:41:32 +01:00
|
|
|
static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
|
|
|
|
struct blob_attr *msg, int cmd, uint32_t peer)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct ubus_msghdr hdr;
|
|
|
|
struct iovec iov[2] = {
|
|
|
|
STATIC_IOV(hdr)
|
|
|
|
};
|
|
|
|
|
|
|
|
hdr.version = 0;
|
|
|
|
hdr.type = cmd;
|
2011-01-31 02:41:32 +01:00
|
|
|
hdr.seq = seq;
|
2010-12-06 03:51:58 +01:00
|
|
|
hdr.peer = peer;
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
msg = b.head;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov[1].iov_base = (char *) msg;
|
|
|
|
iov[1].iov_len = blob_raw_len(msg);
|
|
|
|
|
2011-02-11 00:36:10 +01:00
|
|
|
return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 18:27:23 +01:00
|
|
|
static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
|
|
|
|
struct blob_attr *msg, int cmd, uint32_t peer)
|
2011-01-31 02:41:32 +01:00
|
|
|
{
|
|
|
|
memset(req, 0, sizeof(*req));
|
|
|
|
|
2011-02-07 02:35:01 +01:00
|
|
|
if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
|
|
|
|
return -1;
|
|
|
|
|
2011-01-31 02:41:32 +01:00
|
|
|
INIT_LIST_HEAD(&req->list);
|
|
|
|
INIT_LIST_HEAD(&req->pending);
|
2011-01-31 03:07:42 +01:00
|
|
|
req->ctx = ctx;
|
2011-01-31 02:41:32 +01:00
|
|
|
req->peer = peer;
|
|
|
|
req->seq = ++ctx->request_seq;
|
|
|
|
return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
static bool recv_retry(int fd, struct iovec *iov, bool wait)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
while (iov->iov_len > 0) {
|
2011-09-09 17:53:22 +02:00
|
|
|
if (wait)
|
|
|
|
wait_data(fd, false);
|
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
bytes = read(fd, iov->iov_base, iov->iov_len);
|
|
|
|
if (bytes < 0) {
|
|
|
|
bytes = 0;
|
2011-02-06 02:19:54 +01:00
|
|
|
if (uloop_cancelled)
|
|
|
|
return false;
|
2010-12-06 03:51:58 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
if (errno != EAGAIN)
|
2010-12-06 03:51:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!wait && !bytes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wait = true;
|
|
|
|
iov->iov_len -= bytes;
|
|
|
|
iov->iov_base += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
|
|
|
|
{
|
|
|
|
if (hdr->version != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
|
|
|
|
return false;
|
|
|
|
|
2011-02-07 01:52:40 +01:00
|
|
|
if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
|
2010-12-06 03:51:58 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
static bool get_next_msg(struct ubus_context *ctx)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
|
|
|
|
|
|
|
|
/* receive header + start attribute */
|
|
|
|
iov.iov_len += sizeof(struct blob_attr);
|
2011-02-11 02:40:39 +01:00
|
|
|
if (!recv_retry(ctx->sock.fd, &iov, false))
|
2010-12-06 03:51:58 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
iov.iov_len = blob_len(ctx->msgbuf.hdr.data);
|
|
|
|
if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ubus_validate_hdr(&ctx->msgbuf.hdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
|
|
|
|
{
|
|
|
|
ubus_parse_msg(hdr->data);
|
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_STATUS])
|
|
|
|
return false;
|
|
|
|
|
2011-02-06 21:37:37 +01:00
|
|
|
*ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
|
2010-12-06 03:51:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-31 17:18:10 +01:00
|
|
|
static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
|
|
|
|
{
|
|
|
|
struct blob_attr **attr;
|
|
|
|
|
|
|
|
if (req->raw_data_cb)
|
|
|
|
req->raw_data_cb(req, type, data);
|
|
|
|
|
|
|
|
if (!req->data_cb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
attr = ubus_parse_msg(data);
|
|
|
|
req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
static void ubus_process_req_data(struct ubus_request *req)
|
|
|
|
{
|
|
|
|
struct ubus_pending_data *data;
|
|
|
|
|
|
|
|
while (!list_empty(&req->pending)) {
|
|
|
|
data = list_first_entry(&req->pending,
|
|
|
|
struct ubus_pending_data, list);
|
|
|
|
list_del(&data->list);
|
2011-01-31 02:52:12 +01:00
|
|
|
if (!req->cancelled)
|
2011-01-31 17:18:10 +01:00
|
|
|
req_data_cb(req, data->type, data->data);
|
2010-12-06 03:51:58 +01:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_req_complete_cb(struct ubus_request *req)
|
|
|
|
{
|
|
|
|
ubus_complete_handler_t cb = req->complete_cb;
|
|
|
|
|
|
|
|
if (!cb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
req->complete_cb = NULL;
|
|
|
|
cb(req, req->status_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr *hdr)
|
|
|
|
{
|
|
|
|
int ret = UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
if (!list_empty(&req->list))
|
2011-10-27 13:46:05 +02:00
|
|
|
list_del_init(&req->list);
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
ubus_get_status(hdr, &ret);
|
|
|
|
req->peer = hdr->peer;
|
|
|
|
req->status_msg = true;
|
|
|
|
req->status_code = ret;
|
|
|
|
if (!req->blocked)
|
|
|
|
ubus_req_complete_cb(req);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
|
|
|
|
{
|
|
|
|
struct ubus_pending_data *data;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!req->blocked) {
|
|
|
|
req->blocked = true;
|
2011-01-31 17:18:10 +01:00
|
|
|
req_data_cb(req, hdr->type, hdr->data);
|
2010-12-06 03:51:58 +01:00
|
|
|
ubus_process_req_data(req);
|
|
|
|
req->blocked = false;
|
|
|
|
|
|
|
|
if (req->status_msg)
|
|
|
|
ubus_req_complete_cb(req);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = blob_raw_len(hdr->data);
|
|
|
|
data = calloc(1, sizeof(*data) + len);
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
data->type = hdr->type;
|
|
|
|
memcpy(data->data, hdr->data, len);
|
|
|
|
list_add(&data->list, &req->pending);
|
|
|
|
}
|
|
|
|
|
2011-01-31 02:41:32 +01:00
|
|
|
static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct ubus_request *req;
|
|
|
|
|
|
|
|
list_for_each_entry(req, &ctx->requests, list) {
|
2011-01-31 02:41:32 +01:00
|
|
|
if (seq != req->seq || peer != req->peer)
|
2010-12-06 03:51:58 +01:00
|
|
|
continue;
|
|
|
|
|
2011-01-31 02:41:32 +01:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
|
|
|
|
{
|
2011-01-31 03:26:53 +01:00
|
|
|
struct ubus_request_data req;
|
|
|
|
struct ubus_object *obj;
|
2011-01-31 02:41:32 +01:00
|
|
|
uint32_t objid = 0;
|
2011-01-31 03:26:53 +01:00
|
|
|
int method;
|
2011-01-31 02:41:32 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-10-10 16:47:49 +02:00
|
|
|
req.peer = hdr->peer;
|
|
|
|
req.seq = hdr->seq;
|
2011-01-31 02:41:32 +01:00
|
|
|
ubus_parse_msg(hdr->data);
|
|
|
|
|
2011-01-31 16:15:55 +01:00
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
2011-02-06 21:37:37 +01:00
|
|
|
objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
|
2011-01-31 16:15:55 +01:00
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_METHOD]) {
|
2011-01-31 03:26:53 +01:00
|
|
|
ret = UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = avl_find_element(&ctx->objects, &objid, obj, avl);
|
|
|
|
if (!obj) {
|
|
|
|
ret = UBUS_STATUS_NOT_FOUND;
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (method = 0; method < obj->n_methods; method++)
|
2011-02-05 23:23:44 +01:00
|
|
|
if (!obj->methods[method].name ||
|
|
|
|
!strcmp(obj->methods[method].name,
|
2011-01-31 03:26:53 +01:00
|
|
|
blob_data(attrbuf[UBUS_ATTR_METHOD])))
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
ret = UBUS_STATUS_METHOD_NOT_FOUND;
|
|
|
|
goto send;
|
|
|
|
|
|
|
|
found:
|
|
|
|
req.object = objid;
|
2011-01-31 17:18:10 +01:00
|
|
|
ret = obj->methods[method].handler(ctx, obj, &req,
|
2011-02-07 01:25:28 +01:00
|
|
|
blob_data(attrbuf[UBUS_ATTR_METHOD]),
|
2011-01-31 03:26:53 +01:00
|
|
|
attrbuf[UBUS_ATTR_DATA]);
|
2011-01-31 02:41:32 +01:00
|
|
|
|
2011-01-31 03:26:53 +01:00
|
|
|
send:
|
2011-01-31 02:41:32 +01:00
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
|
2011-10-10 16:47:49 +02:00
|
|
|
ubus_send_msg(ctx, req.seq, b.head, UBUS_MSG_STATUS, req.peer);
|
2011-01-31 02:41:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
|
|
|
|
{
|
2011-10-27 14:03:19 +02:00
|
|
|
struct ubus_pending_invoke *pending;
|
2011-01-31 02:41:32 +01:00
|
|
|
struct ubus_request *req;
|
|
|
|
|
|
|
|
switch(hdr->type) {
|
|
|
|
case UBUS_MSG_STATUS:
|
|
|
|
req = ubus_find_request(ctx, hdr->seq, hdr->peer);
|
|
|
|
if (!req)
|
2010-12-06 03:51:58 +01:00
|
|
|
break;
|
2011-01-31 02:41:32 +01:00
|
|
|
|
|
|
|
ubus_process_req_status(req, hdr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UBUS_MSG_DATA:
|
|
|
|
req = ubus_find_request(ctx, hdr->seq, hdr->peer);
|
2011-01-31 17:18:10 +01:00
|
|
|
if (req && (req->data_cb || req->raw_data_cb))
|
2011-01-31 02:41:32 +01:00
|
|
|
ubus_req_data(req, hdr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UBUS_MSG_INVOKE:
|
2011-10-27 14:03:19 +02:00
|
|
|
if (ctx->stack_depth > 2) {
|
|
|
|
pending = calloc(1, sizeof(*pending) +
|
|
|
|
blob_raw_len(hdr->data));
|
|
|
|
|
|
|
|
if (!pending)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(&pending->hdr, hdr, sizeof(*hdr) +
|
|
|
|
blob_raw_len(hdr->data));
|
|
|
|
list_add(&pending->list, &ctx->pending);
|
|
|
|
} else {
|
|
|
|
ubus_process_invoke(ctx, hdr);
|
|
|
|
}
|
2011-01-31 02:41:32 +01:00
|
|
|
break;
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 14:03:19 +02:00
|
|
|
static void ubus_process_pending_invoke(struct ubus_context *ctx)
|
|
|
|
{
|
|
|
|
struct ubus_pending_invoke *pending, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(pending, tmp, &ctx->pending, list) {
|
|
|
|
list_del(&pending->list);
|
|
|
|
ubus_process_msg(ctx, &pending->hdr);
|
|
|
|
free(pending);
|
|
|
|
if (ctx->stack_depth > 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
|
|
|
|
{
|
|
|
|
if (!list_empty(&req->list))
|
|
|
|
return;
|
|
|
|
|
2011-01-31 02:52:12 +01:00
|
|
|
req->cancelled = true;
|
|
|
|
ubus_process_req_data(req);
|
2011-10-27 13:46:05 +02:00
|
|
|
list_del_init(&req->list);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
|
|
|
|
{
|
|
|
|
if (!list_empty(&req->list))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_add(&req->list, &ctx->requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_handle_data(struct uloop_fd *u, unsigned int events)
|
|
|
|
{
|
|
|
|
struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
|
|
|
|
struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
while (get_next_msg(ctx)) {
|
2010-12-06 03:51:58 +01:00
|
|
|
ubus_process_msg(ctx, hdr);
|
2011-02-11 02:40:39 +01:00
|
|
|
if (uloop_cancelled)
|
|
|
|
break;
|
|
|
|
}
|
2011-01-31 16:15:55 +01:00
|
|
|
|
|
|
|
if (u->eof)
|
|
|
|
ctx->connection_lost(ctx);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
static void ubus_sync_req_cb(struct ubus_request *req, int ret)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
2011-02-11 02:40:39 +01:00
|
|
|
req->status_msg = true;
|
|
|
|
req->status_code = ret;
|
|
|
|
uloop_end();
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
struct ubus_sync_req_cb {
|
|
|
|
struct uloop_timeout timeout;
|
|
|
|
struct ubus_request *req;
|
|
|
|
};
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout)
|
|
|
|
{
|
|
|
|
struct ubus_sync_req_cb *cb;
|
2011-01-31 02:52:12 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
cb = container_of(timeout, struct ubus_sync_req_cb, timeout);
|
|
|
|
ubus_sync_req_cb(cb->req, UBUS_STATUS_TIMEOUT);
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
struct ubus_sync_req_cb cb;
|
|
|
|
ubus_complete_handler_t complete_cb = req->complete_cb;
|
|
|
|
bool registered = ctx->sock.registered;
|
|
|
|
int status = UBUS_STATUS_NO_DATA;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
if (!registered) {
|
|
|
|
uloop_init();
|
|
|
|
ubus_add_uloop(ctx);
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
if (timeout) {
|
|
|
|
memset(&cb, 0, sizeof(cb));
|
|
|
|
cb.req = req;
|
|
|
|
cb.timeout.cb = ubus_sync_req_timeout_cb;
|
|
|
|
uloop_timeout_set(&cb.timeout, timeout);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
2011-02-11 02:40:39 +01:00
|
|
|
|
|
|
|
ubus_complete_request_async(ctx, req);
|
|
|
|
req->complete_cb = ubus_sync_req_cb;
|
|
|
|
|
2011-10-27 14:03:19 +02:00
|
|
|
ctx->stack_depth++;
|
2011-10-27 13:46:05 +02:00
|
|
|
while (!req->status_msg) {
|
|
|
|
bool cancelled = uloop_cancelled;
|
|
|
|
uloop_cancelled = false;
|
|
|
|
uloop_run();
|
|
|
|
uloop_cancelled = cancelled;
|
|
|
|
}
|
2011-10-27 14:03:19 +02:00
|
|
|
ctx->stack_depth--;
|
2011-02-11 02:40:39 +01:00
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
uloop_timeout_cancel(&cb.timeout);
|
|
|
|
|
|
|
|
if (req->status_msg)
|
|
|
|
status = req->status_code;
|
|
|
|
|
|
|
|
req->complete_cb = complete_cb;
|
|
|
|
if (req->complete_cb)
|
|
|
|
req->complete_cb(req, status);
|
|
|
|
|
|
|
|
if (!registered)
|
|
|
|
uloop_fd_delete(&ctx->sock);
|
|
|
|
|
2011-10-27 14:03:19 +02:00
|
|
|
if (!ctx->stack_depth)
|
|
|
|
ubus_process_pending_invoke(ctx);
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
return status;
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 18:26:24 +01:00
|
|
|
struct ubus_lookup_request {
|
|
|
|
struct ubus_request req;
|
|
|
|
ubus_lookup_handler_t cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_lookup_request *req;
|
|
|
|
struct ubus_object_data obj;
|
|
|
|
struct blob_attr **attr;
|
|
|
|
|
|
|
|
req = container_of(ureq, struct ubus_lookup_request, req);
|
|
|
|
attr = ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
|
|
|
|
!attr[UBUS_ATTR_OBJTYPE])
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
2011-02-06 21:37:37 +01:00
|
|
|
obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
|
2011-01-31 18:26:24 +01:00
|
|
|
obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
|
2011-02-06 21:37:37 +01:00
|
|
|
obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
|
2011-01-31 18:26:24 +01:00
|
|
|
obj.signature = attr[UBUS_ATTR_SIGNATURE];
|
|
|
|
req->cb(ureq->ctx, &obj, ureq->priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_lookup(struct ubus_context *ctx, const char *path,
|
|
|
|
ubus_lookup_handler_t cb, void *priv)
|
|
|
|
{
|
|
|
|
struct ubus_lookup_request lookup;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
if (path)
|
|
|
|
blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
|
2011-02-07 02:35:01 +01:00
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-01-31 18:26:24 +01:00
|
|
|
lookup.req.raw_data_cb = ubus_lookup_cb;
|
|
|
|
lookup.req.priv = priv;
|
|
|
|
lookup.cb = cb;
|
2011-02-11 02:40:39 +01:00
|
|
|
return ubus_complete_request(ctx, &lookup.req, 0);
|
2011-01-31 18:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct blob_attr **attr;
|
|
|
|
uint32_t *id = req->priv;
|
|
|
|
|
|
|
|
attr = ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attr[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
2011-02-06 21:37:37 +01:00
|
|
|
*id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
|
2011-01-31 18:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
if (path)
|
|
|
|
blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
|
2011-02-07 02:35:01 +01:00
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-01-31 18:26:24 +01:00
|
|
|
req.raw_data_cb = ubus_lookup_id_cb;
|
|
|
|
req.priv = id;
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
return ubus_complete_request(ctx, &req, 0);
|
2011-01-31 18:26:24 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 17:18:10 +01:00
|
|
|
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
|
|
|
|
struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
|
|
|
|
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
|
|
|
|
ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer);
|
|
|
|
if (ret < 0)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-07 02:35:01 +01:00
|
|
|
int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
|
2010-12-06 03:51:58 +01:00
|
|
|
struct blob_attr *msg, struct ubus_request *req)
|
|
|
|
{
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
|
|
|
|
blob_put_string(&b, UBUS_ATTR_METHOD, method);
|
2011-01-31 17:26:29 +01:00
|
|
|
if (msg)
|
|
|
|
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-07 02:35:01 +01:00
|
|
|
if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
return 0;
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
|
2011-02-11 02:40:39 +01:00
|
|
|
struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
|
|
|
|
int timeout)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
|
|
|
|
ubus_invoke_async(ctx, obj, method, msg, &req);
|
|
|
|
req.data_cb = cb;
|
|
|
|
req.priv = priv;
|
2011-02-11 02:40:39 +01:00
|
|
|
return ubus_complete_request(ctx, &req, timeout);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-02-06 21:33:03 +01:00
|
|
|
static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct ubus_object *obj = req->priv;
|
|
|
|
|
|
|
|
ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
2011-02-06 21:37:37 +01:00
|
|
|
obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
if (attrbuf[UBUS_ATTR_OBJTYPE])
|
2011-02-06 21:37:37 +01:00
|
|
|
obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
|
2011-01-31 03:07:42 +01:00
|
|
|
|
|
|
|
obj->avl.key = &obj->id;
|
|
|
|
avl_insert(&req->ctx->objects, &obj->avl);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-03-27 20:03:18 +02:00
|
|
|
static void ubus_push_method_data(const struct ubus_method *m)
|
|
|
|
{
|
|
|
|
void *mtbl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mtbl = blobmsg_open_table(&b, m->name);
|
|
|
|
|
|
|
|
for (i = 0; i < m->n_policy; i++)
|
|
|
|
blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
|
|
|
|
|
|
|
|
blobmsg_close_table(&b, mtbl);
|
2010-12-06 03:51:58 +01:00
|
|
|
}
|
|
|
|
|
2011-03-27 20:03:18 +02:00
|
|
|
static bool ubus_push_object_type(const struct ubus_object_type *type)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
2011-03-27 20:03:18 +02:00
|
|
|
void *s;
|
|
|
|
int i;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
|
|
|
|
|
2011-03-27 20:03:18 +02:00
|
|
|
for (i = 0; i < type->n_methods; i++)
|
|
|
|
ubus_push_method_data(&type->methods[i]);
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
blob_nest_end(&b, s);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-19 21:09:35 +02:00
|
|
|
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
|
2011-02-05 19:53:14 +01:00
|
|
|
if (obj->name && obj->type) {
|
|
|
|
blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
|
|
|
|
|
|
|
|
if (obj->type->id)
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
|
|
|
|
else if (!ubus_push_object_type(obj->type))
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-02-07 02:35:01 +01:00
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-02-06 21:33:03 +01:00
|
|
|
req.raw_data_cb = ubus_add_object_cb;
|
2010-12-06 03:51:58 +01:00
|
|
|
req.priv = obj;
|
2011-02-11 02:40:39 +01:00
|
|
|
ret = ubus_complete_request(ctx, &req, 0);
|
2010-12-06 03:51:58 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!obj->id)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-06 21:46:45 +01:00
|
|
|
static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_object *obj = req->priv;
|
|
|
|
|
|
|
|
ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->id = 0;
|
|
|
|
|
|
|
|
if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
|
|
|
|
obj->type->id = 0;
|
|
|
|
|
|
|
|
avl_delete(&req->ctx->objects, &obj->avl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
|
2011-02-07 02:35:01 +01:00
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-02-06 21:46:45 +01:00
|
|
|
req.raw_data_cb = ubus_remove_object_cb;
|
|
|
|
req.priv = obj;
|
2011-02-11 02:40:39 +01:00
|
|
|
ret = ubus_complete_request(ctx, &req, 0);
|
2011-02-06 21:46:45 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (obj->id)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-05 23:23:44 +01:00
|
|
|
static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
|
|
|
|
struct ubus_request_data *req,
|
|
|
|
const char *method, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_event_handler *ev;
|
|
|
|
|
|
|
|
ev = container_of(obj, struct ubus_event_handler, obj);
|
|
|
|
ev->cb(ctx, ev, method, msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct ubus_method event_method = {
|
|
|
|
.name = NULL,
|
|
|
|
.handler = ubus_event_cb,
|
|
|
|
};
|
|
|
|
|
|
|
|
int ubus_register_event_handler(struct ubus_context *ctx,
|
|
|
|
struct ubus_event_handler *ev,
|
2011-02-05 19:53:14 +01:00
|
|
|
const char *pattern)
|
|
|
|
{
|
2011-02-05 23:23:44 +01:00
|
|
|
struct ubus_object *obj = &ev->obj;
|
2011-02-05 19:53:14 +01:00
|
|
|
struct blob_buf b2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!obj->id) {
|
2011-02-05 23:23:44 +01:00
|
|
|
obj->methods = &event_method;
|
|
|
|
obj->n_methods = 1;
|
|
|
|
|
2011-02-05 19:53:14 +01:00
|
|
|
if (!!obj->name ^ !!obj->type)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2012-05-19 21:09:35 +02:00
|
|
|
ret = ubus_add_object(ctx, obj);
|
2011-02-05 19:53:14 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use a second buffer, ubus_invoke() overwrites the primary one */
|
|
|
|
memset(&b2, 0, sizeof(b2));
|
|
|
|
blob_buf_init(&b2, 0);
|
|
|
|
blobmsg_add_u32(&b2, "object", obj->id);
|
|
|
|
if (pattern)
|
|
|
|
blobmsg_add_string(&b2, "pattern", pattern);
|
|
|
|
|
2011-02-11 01:21:07 +01:00
|
|
|
return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
|
2011-02-11 02:40:39 +01:00
|
|
|
NULL, NULL, 0);
|
2011-02-05 19:53:14 +01:00
|
|
|
}
|
|
|
|
|
2012-05-19 21:09:35 +02:00
|
|
|
enum {
|
|
|
|
WATCH_ID,
|
|
|
|
WATCH_NOTIFY,
|
|
|
|
__WATCH_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct blobmsg_policy watch_policy[] = {
|
|
|
|
[WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
|
|
|
|
[WATCH_NOTIFY] = { .name = "notify", .type = BLOBMSG_TYPE_STRING },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int ubus_watch_cb(struct ubus_context *ctx, struct ubus_object *obj,
|
|
|
|
struct ubus_request_data *req,
|
|
|
|
const char *method, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_watch_object *w;
|
|
|
|
struct blob_attr *tb[__WATCH_MAX];
|
|
|
|
|
|
|
|
blobmsg_parse(watch_policy, ARRAY_SIZE(watch_policy), tb, blob_data(msg), blob_len(msg));
|
|
|
|
|
|
|
|
if (!tb[WATCH_ID] || !tb[WATCH_NOTIFY])
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
if (req->peer)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
w = container_of(obj, struct ubus_watch_object, obj);
|
|
|
|
w->cb(ctx, w, blobmsg_get_u32(tb[WATCH_ID]));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct ubus_method watch_method = {
|
|
|
|
.name = NULL,
|
|
|
|
.handler = ubus_watch_cb,
|
|
|
|
};
|
|
|
|
|
|
|
|
int ubus_register_watch_object(struct ubus_context *ctx, struct ubus_watch_object *w_obj)
|
|
|
|
{
|
|
|
|
struct ubus_object *obj = &w_obj->obj;
|
|
|
|
|
|
|
|
obj->methods = &watch_method;
|
|
|
|
obj->n_methods = 1;
|
|
|
|
|
|
|
|
return ubus_add_object(ctx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
__ubus_watch_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, const char *method, int type)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_TARGET, id);
|
|
|
|
if (method)
|
|
|
|
blob_put_string(&b, UBUS_ATTR_METHOD, method);
|
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
return ubus_complete_request(ctx, &req, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_watch_object_add(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
|
|
|
|
{
|
|
|
|
return __ubus_watch_request(ctx, &obj->obj, id, "event", UBUS_MSG_ADD_WATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_watch_object_remove(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
|
|
|
|
{
|
|
|
|
return __ubus_watch_request(ctx, &obj->obj, id, NULL, UBUS_MSG_REMOVE_WATCH);
|
|
|
|
}
|
|
|
|
|
2011-02-07 01:25:28 +01:00
|
|
|
int ubus_send_event(struct ubus_context *ctx, const char *id,
|
|
|
|
struct blob_attr *data)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
|
|
|
|
blob_put_string(&b, UBUS_ATTR_METHOD, "send");
|
|
|
|
s = blob_nest_start(&b, UBUS_ATTR_DATA);
|
|
|
|
blobmsg_add_string(&b, "id", id);
|
|
|
|
blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
|
|
|
|
blob_nest_end(&b, s);
|
|
|
|
|
2011-02-07 02:35:01 +01:00
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
2011-02-11 02:40:39 +01:00
|
|
|
return ubus_complete_request(ctx, &req, 0);
|
2011-02-07 01:25:28 +01:00
|
|
|
}
|
2011-02-05 19:53:14 +01:00
|
|
|
|
2012-05-29 00:56:37 +02:00
|
|
|
static void
|
|
|
|
ubus_refresh_state(struct ubus_context *ctx)
|
2011-01-31 16:15:55 +01:00
|
|
|
{
|
2012-05-29 00:56:37 +02:00
|
|
|
struct ubus_object *obj, *tmp;
|
|
|
|
|
|
|
|
/* clear all type IDs, they need to be registered again */
|
|
|
|
avl_for_each_element(&ctx->objects, obj, avl)
|
|
|
|
obj->type->id = 0;
|
|
|
|
|
|
|
|
/* push out all objects again */
|
|
|
|
avl_for_each_element_safe(&ctx->objects, obj, avl, tmp) {
|
|
|
|
obj->id = 0;
|
|
|
|
avl_delete(&ctx->objects, &obj->avl);
|
|
|
|
ubus_add_object(ctx, obj);
|
|
|
|
}
|
2011-01-31 16:15:55 +01:00
|
|
|
}
|
|
|
|
|
2012-05-29 00:56:37 +02:00
|
|
|
int ubus_reconnect(struct ubus_context *ctx, const char *path)
|
2010-12-06 03:51:58 +01:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct ubus_msghdr hdr;
|
|
|
|
struct blob_attr data;
|
|
|
|
} hdr;
|
|
|
|
struct blob_attr *buf;
|
2012-05-29 00:56:37 +02:00
|
|
|
int ret = UBUS_STATUS_UNKNOWN_ERROR;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
if (!path)
|
|
|
|
path = UBUS_UNIX_SOCKET;
|
|
|
|
|
2012-05-29 00:56:37 +02:00
|
|
|
if (ctx->sock.fd >= 0) {
|
|
|
|
if (ctx->sock.registered)
|
|
|
|
uloop_fd_delete(&ctx->sock);
|
|
|
|
|
|
|
|
close(ctx->sock.fd);
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
|
2011-01-31 19:55:07 +01:00
|
|
|
if (ctx->sock.fd < 0)
|
2012-05-29 00:56:37 +02:00
|
|
|
return UBUS_STATUS_CONNECTION_FAILED;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-01-31 19:55:07 +01:00
|
|
|
if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
|
2012-05-29 00:56:37 +02:00
|
|
|
goto out_close;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-01-31 19:55:07 +01:00
|
|
|
if (!ubus_validate_hdr(&hdr.hdr))
|
2012-05-29 00:56:37 +02:00
|
|
|
goto out_close;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2011-01-31 19:55:07 +01:00
|
|
|
if (hdr.hdr.type != UBUS_MSG_HELLO)
|
2012-05-29 00:56:37 +02:00
|
|
|
goto out_close;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
buf = calloc(1, blob_raw_len(&hdr.data));
|
|
|
|
if (!buf)
|
2012-05-29 00:56:37 +02:00
|
|
|
goto out_close;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
memcpy(buf, &hdr.data, sizeof(hdr.data));
|
2011-01-31 19:55:07 +01:00
|
|
|
if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
|
2012-05-29 00:56:37 +02:00
|
|
|
goto out_free;
|
2010-12-06 03:51:58 +01:00
|
|
|
|
|
|
|
ctx->local_id = hdr.hdr.peer;
|
2012-05-29 00:56:37 +02:00
|
|
|
if (!ctx->local_id)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
ret = UBUS_STATUS_OK;
|
|
|
|
fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
|
|
|
|
|
|
|
|
ubus_refresh_state(ctx);
|
|
|
|
|
|
|
|
out_free:
|
2010-12-06 03:51:58 +01:00
|
|
|
free(buf);
|
2012-05-29 00:56:37 +02:00
|
|
|
out_close:
|
|
|
|
if (ret)
|
|
|
|
close(ctx->sock.fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-06 03:51:58 +01:00
|
|
|
|
2012-05-29 00:56:37 +02:00
|
|
|
static void ubus_default_connection_lost(struct ubus_context *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->sock.registered)
|
|
|
|
uloop_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ubus_context *ubus_connect(const char *path)
|
|
|
|
{
|
|
|
|
struct ubus_context *ctx;
|
|
|
|
|
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx->sock.fd = -1;
|
|
|
|
ctx->sock.cb = ubus_handle_data;
|
2011-01-31 16:15:55 +01:00
|
|
|
ctx->connection_lost = ubus_default_connection_lost;
|
|
|
|
|
2011-01-31 03:07:42 +01:00
|
|
|
INIT_LIST_HEAD(&ctx->requests);
|
2011-10-27 14:03:19 +02:00
|
|
|
INIT_LIST_HEAD(&ctx->pending);
|
2011-01-31 03:07:42 +01:00
|
|
|
avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
|
2012-05-29 00:56:37 +02:00
|
|
|
if (ubus_reconnect(ctx, path)) {
|
|
|
|
free(ctx);
|
|
|
|
ctx = NULL;
|
|
|
|
}
|
2011-09-09 17:53:22 +02:00
|
|
|
|
2010-12-06 03:51:58 +01:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ubus_free(struct ubus_context *ctx)
|
|
|
|
{
|
|
|
|
close(ctx->sock.fd);
|
|
|
|
free(ctx);
|
|
|
|
}
|