#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
+#include <fcntl.h>
#include <unistd.h>
#include <libubox/blob.h>
#include "libubus.h"
#include "ubusmsg.h"
-#define DEBUG 1
-
-#ifdef DEBUG
-#define DPRINTF(_format, ...) fprintf(stderr, "ubus: " _format, ## __VA_ARGS__)
-#else
-#define DPRINTF(...) do {} while(0)
-#endif
-
#define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
return err;
}
+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:
+ /* turn off non-blocking mode */
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) &
+ ~O_NONBLOCK);
+ 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);
+}
+
static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
struct blob_attr *msg, int cmd, uint32_t peer)
{
iov[1].iov_base = (char *) msg;
iov[1].iov_len = blob_raw_len(msg);
- return writev(ctx->sock.fd, iov, 2);
+ return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
}
static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
{
memset(req, 0, sizeof(*req));
+ if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
+ return -1;
+
INIT_LIST_HEAD(&req->list);
INIT_LIST_HEAD(&req->pending);
req->ctx = ctx;
if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
return false;
- if (blob_raw_len(hdr->data) + sizeof(*hdr) > UBUS_MAX_MSGLEN)
+ if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
return false;
return true;
req.peer = hdr->peer;
req.seq = hdr->seq;
ret = obj->methods[method].handler(ctx, obj, &req,
- obj->methods[method].name,
+ blob_data(attrbuf[UBUS_ATTR_METHOD]),
attrbuf[UBUS_ATTR_DATA]);
send:
case UBUS_MSG_INVOKE:
ubus_process_invoke(ctx, hdr);
break;
- default:
- DPRINTF("unknown message type: %d\n", hdr->type);
- break;
}
}
blob_buf_init(&b, 0);
if (path)
blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
- ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0);
+
+ if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
lookup.req.raw_data_cb = ubus_lookup_cb;
lookup.req.priv = priv;
lookup.cb = cb;
blob_buf_init(&b, 0);
if (path)
blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
- ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0);
+
+ if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
req.raw_data_cb = ubus_lookup_id_cb;
req.priv = id;
return 0;
}
-void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
+int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
struct blob_attr *msg, struct ubus_request *req)
{
blob_buf_init(&b, 0);
if (msg)
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
- ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj);
+ if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ return 0;
}
int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
return UBUS_STATUS_INVALID_ARGUMENT;
}
- ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0);
+ if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
req.raw_data_cb = ubus_add_object_cb;
req.priv = obj;
ret = ubus_complete_request(ctx, &req);
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
- ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0);
+
+ if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
req.raw_data_cb = ubus_remove_object_cb;
req.priv = obj;
ret = ubus_complete_request(ctx, &req);
if (pattern)
blobmsg_add_string(&b2, "pattern", pattern);
- ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
+ return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
NULL, NULL);
-
- return 0;
}
+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);
+
+ if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ return ubus_complete_request(ctx, &req);
+}
-void ubus_default_connection_lost(struct ubus_context *ctx)
+static void ubus_default_connection_lost(struct ubus_context *ctx)
{
if (ctx->sock.registered)
uloop_end();