[UBUS_STATUS_OK] = "Success",
[UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
[UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
+ [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
[UBUS_STATUS_NOT_FOUND] = "Not found",
[UBUS_STATUS_NO_DATA] = "No response",
+ [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
};
static struct blob_buf b;
[UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
[UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
[UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
+ [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
};
static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
struct blob_attr data[];
};
-struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
+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;
+}
+
+static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
{
blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
return attrbuf;
return writev(ctx->sock.fd, iov, 2);
}
-int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
- struct blob_attr *msg, int cmd, uint32_t peer)
+static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
+ struct blob_attr *msg, int cmd, uint32_t peer)
{
memset(req, 0, sizeof(*req));
INIT_LIST_HEAD(&req->list);
INIT_LIST_HEAD(&req->pending);
+ req->ctx = ctx;
req->peer = peer;
req->seq = ++ctx->request_seq;
return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
return true;
}
+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]);
+}
+
static void ubus_process_req_data(struct ubus_request *req)
{
struct ubus_pending_data *data;
data = list_first_entry(&req->pending,
struct ubus_pending_data, list);
list_del(&data->list);
- req->data_cb(req, data->type, data->data);
+ if (!req->cancelled)
+ req_data_cb(req, data->type, data->data);
free(data);
}
}
if (!req->blocked) {
req->blocked = true;
- req->data_cb(req, hdr->type, hdr->data);
+ req_data_cb(req, hdr->type, hdr->data);
ubus_process_req_data(req);
req->blocked = false;
static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
{
+ struct ubus_request_data req;
+ struct ubus_object *obj;
uint32_t objid = 0;
+ int method;
int ret = 0;
ubus_parse_msg(hdr->data);
- if (attrbuf[UBUS_ATTR_OBJID])
- objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+ if (!attrbuf[UBUS_ATTR_OBJID])
+ return;
+
+ objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+
+ if (!attrbuf[UBUS_ATTR_METHOD]) {
+ 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++)
+ if (!strcmp(obj->methods[method].name,
+ blob_data(attrbuf[UBUS_ATTR_METHOD])))
+ goto found;
+
+ /* not found */
+ ret = UBUS_STATUS_METHOD_NOT_FOUND;
+ goto send;
+found:
+ req.object = objid;
+ req.peer = hdr->peer;
+ req.seq = hdr->seq;
+ ret = obj->methods[method].handler(ctx, obj, &req,
+ obj->methods[method].name,
+ attrbuf[UBUS_ATTR_DATA]);
+
+send:
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
case UBUS_MSG_DATA:
req = ubus_find_request(ctx, hdr->seq, hdr->peer);
- if (req && req->data_cb)
+ if (req && (req->data_cb || req->raw_data_cb))
ubus_req_data(req, hdr);
break;
if (!list_empty(&req->list))
return;
+ req->cancelled = true;
+ ubus_process_req_data(req);
list_del(&req->list);
}
while (get_next_msg(ctx, false))
ubus_process_msg(ctx, hdr);
+
+ if (u->eof)
+ ctx->connection_lost(ctx);
}
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req)
if (req->status_msg)
return req->status_code;
+ if (req->cancelled)
+ return UBUS_STATUS_NO_DATA;
+
if (!get_next_msg(ctx, true))
return UBUS_STATUS_NO_DATA;
case UBUS_MSG_STATUS:
return ubus_process_req_status(req, hdr);
case UBUS_MSG_DATA:
- if (req->data_cb)
+ if (req->data_cb || req->raw_data_cb)
ubus_req_data(req, hdr);
continue;
default:
}
}
+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));
+ obj.id = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+ obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
+ obj.type_id = blob_get_int32(attr[UBUS_ATTR_OBJTYPE]);
+ 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);
+ ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0);
+ lookup.req.raw_data_cb = ubus_lookup_cb;
+ lookup.req.priv = priv;
+ lookup.cb = cb;
+ return ubus_complete_request(ctx, &lookup.req);
+}
+
+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;
+
+ *id = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+}
+
+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);
+ ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0);
+ req.raw_data_cb = ubus_lookup_id_cb;
+ req.priv = id;
+
+ return ubus_complete_request(ctx, &req);
+}
+
+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;
+}
+
void 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);
blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
blob_put_string(&b, UBUS_ATTR_METHOD, method);
- blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
+ 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 (attrbuf[UBUS_ATTR_OBJTYPE])
obj->type->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJTYPE]);
+
+ obj->avl.key = &obj->id;
+ avl_insert(&req->ctx->objects, &obj->avl);
}
static bool ubus_push_table_data(const struct ubus_signature **sig, int *rem, bool array)
return true;
}
-int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
{
struct ubus_request req;
int ret;
- if (obj->id || !obj->name || !obj->type)
- return UBUS_STATUS_INVALID_ARGUMENT;
-
blob_buf_init(&b, 0);
- blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
- if (obj->parent)
- blob_put_int32(&b, UBUS_ATTR_OBJID, obj->parent->id);
- 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;
+ 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;
+ }
ubus_start_request(ctx, &req, b.head, UBUS_MSG_PUBLISH, 0);
- req.data_cb = ubus_publish_cb;
+ req.raw_data_cb = ubus_publish_cb;
req.priv = obj;
ret = ubus_complete_request(ctx, &req);
if (ret)
return 0;
}
+int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+{
+ if (!obj->name || !obj->type)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ return __ubus_publish(ctx, obj);
+}
+
+int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj,
+ const char *pattern)
+{
+ struct blob_buf b2;
+ int ret;
+
+ if (!obj->id) {
+ if (!!obj->name ^ !!obj->type)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ ret = __ubus_publish(ctx, obj);
+ 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);
+
+ ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
+ NULL, NULL);
+
+ return 0;
+}
+
+
+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;
goto error;
ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
- if (ctx->sock.fd < 0) {
- DPRINTF("Failed to connect to server\n");
+ if (ctx->sock.fd < 0)
goto error_free;
- }
+
ctx->sock.cb = ubus_handle_data;
- if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
- DPRINTF("Failed to read initial message data\n");
+ if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
goto error_close;
- }
- if (!ubus_validate_hdr(&hdr.hdr)) {
- DPRINTF("Failed to validate initial message header\n");
+ if (!ubus_validate_hdr(&hdr.hdr))
goto error_close;
- }
- if (hdr.hdr.type != UBUS_MSG_HELLO) {
- DPRINTF("Unexpected initial message\n");
+ if (hdr.hdr.type != UBUS_MSG_HELLO)
goto error_close;
- }
buf = calloc(1, blob_raw_len(&hdr.data));
if (!buf)
goto error_close;
memcpy(buf, &hdr.data, sizeof(hdr.data));
- if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf)) {
- DPRINTF("Failed to retrieve initial message data\n");
+ if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
goto error_free_buf;
- }
ctx->local_id = hdr.hdr.peer;
- INIT_LIST_HEAD(&ctx->requests);
free(buf);
- if (!ctx->local_id) {
- DPRINTF("Failed to get local peer id\n");
+ ctx->connection_lost = ubus_default_connection_lost;
+
+ INIT_LIST_HEAD(&ctx->requests);
+ avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
+
+ if (!ctx->local_id)
goto error_close;
- }
return ctx;