static struct blob_buf b;
static struct ubus_context *ctx;
+static uint32_t objid;
static void receive_lookup(struct ubus_request *req, int type, struct blob_attr *msg)
{
}
}
+static void store_objid(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+ struct blob_attr **attr;
+
+ attr = ubus_parse_msg(msg);
+ if (!attr[UBUS_ATTR_OBJID])
+ return;
+
+ objid = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+}
+
+static uint32_t get_object(const char *name)
+{
+ struct ubus_request req;
+
+ blob_buf_init(&b, 0);
+ blob_put_string(&b, UBUS_ATTR_OBJPATH, name);
+ ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0);
+ req.data_cb = store_objid;
+ if (ubus_complete_request(ctx, &req))
+ return 0;
+
+ return objid;
+}
+
static int usage(char *prog)
{
fprintf(stderr,
if (argc < 2)
return usage(argv[0]);
- blob_buf_init(&b, 0);
-
if (!strcmp(cmd, "list")) {
+ blob_buf_init(&b, 0);
+
if (argc == 3)
blob_put_string(&b, UBUS_ATTR_OBJPATH, argv[2]);
if (argc < 4 || argc > 5)
return usage(argv[0]);
- blob_put_string(&b, UBUS_ATTR_OBJPATH, argv[2]);
+ if (get_object(argv[2]) == 0) {
+ fprintf(stderr, "Object not found\n");
+ return 1;
+ }
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
blob_put_string(&b, UBUS_ATTR_METHOD, argv[3]);
- ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, 0);
+ ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, objid);
} else {
return usage(argv[0]);
}
return err;
}
-int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
- struct blob_attr *msg, int cmd, uint32_t peer)
+static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
+ struct blob_attr *msg, int cmd, uint32_t peer)
{
struct ubus_msghdr hdr;
struct iovec iov[2] = {
STATIC_IOV(hdr)
};
- memset(req, 0, sizeof(*req));
hdr.version = 0;
hdr.type = cmd;
- hdr.seq = ++ctx->request_seq;
+ hdr.seq = seq;
hdr.peer = peer;
- req->peer = hdr.peer;
- req->seq = hdr.seq;
-
if (!msg) {
blob_buf_init(&b, 0);
msg = b.head;
iov[1].iov_base = (char *) msg;
iov[1].iov_len = blob_raw_len(msg);
- INIT_LIST_HEAD(&req->list);
- INIT_LIST_HEAD(&req->pending);
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)
+{
+ memset(req, 0, sizeof(*req));
+
+ INIT_LIST_HEAD(&req->list);
+ INIT_LIST_HEAD(&req->pending);
+ req->peer = peer;
+ req->seq = ++ctx->request_seq;
+ return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
+}
+
static bool recv_retry(int fd, struct iovec *iov, bool wait)
{
int bytes;
list_add(&data->list, &req->pending);
}
-static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
+static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer)
{
struct ubus_request *req;
list_for_each_entry(req, &ctx->requests, list) {
- if (hdr->seq != req->seq || hdr->peer != req->peer)
+ if (seq != req->seq || peer != req->peer)
continue;
- switch(hdr->type) {
- case UBUS_MSG_STATUS:
- ubus_process_req_status(req, hdr);
- return;
- case UBUS_MSG_DATA:
- if (req->data_cb)
- ubus_req_data(req, hdr);
- break;
- default:
- DPRINTF("unknown message type: %d\n", hdr->type);
+ return req;
+ }
+ return NULL;
+}
+
+static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
+{
+ uint32_t objid = 0;
+ int ret = 0;
+
+ ubus_parse_msg(hdr->data);
+
+ if (attrbuf[UBUS_ATTR_OBJID])
+ objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
+ ubus_send_msg(ctx, hdr->seq, b.head, UBUS_MSG_STATUS, hdr->peer);
+}
+
+static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
+{
+ struct ubus_request *req;
+
+ switch(hdr->type) {
+ case UBUS_MSG_STATUS:
+ req = ubus_find_request(ctx, hdr->seq, hdr->peer);
+ if (!req)
break;
- }
+
+ ubus_process_req_status(req, hdr);
+ break;
+
+ case UBUS_MSG_DATA:
+ req = ubus_find_request(ctx, hdr->seq, hdr->peer);
+ if (req && req->data_cb)
+ ubus_req_data(req, hdr);
+ break;
+
+ case UBUS_MSG_INVOKE:
+ ubus_process_invoke(ctx, hdr);
+ break;
+ default:
+ DPRINTF("unknown message type: %d\n", hdr->type);
+ break;
}
}
ubus_req_data(req, hdr);
continue;
default:
- DPRINTF("unknown message type: %d\n", hdr->type);
- continue;
+ goto skip;
}
skip:
}
}
-void ubus_invoke_path_async(struct ubus_context *ctx, const char *path, const char *method,
- struct blob_attr *msg, struct ubus_request *req)
-{
- blob_buf_init(&b, 0);
- blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
- blob_put_string(&b, UBUS_ATTR_METHOD, method);
- blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
-
- ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, 0);
-}
-
-int ubus_invoke_path(struct ubus_context *ctx, const char *path, const char *method,
- struct blob_attr *msg, ubus_data_handler_t cb, void *priv)
-{
- struct ubus_request req;
-
- ubus_invoke_path_async(ctx, path, method, msg, &req);
- req.data_cb = cb;
- req.priv = priv;
- return ubus_complete_request(ctx, &req);
-}
-
void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
struct blob_attr *msg, struct ubus_request *req)
{
blob_put_string(&b, UBUS_ATTR_METHOD, method);
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
- ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, 0);
+ ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj);
}
int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
}
ctx->local_id = hdr.hdr.peer;
+ INIT_LIST_HEAD(&ctx->requests);
free(buf);
if (!ctx->local_id) {
/* invoke a method on a specific object */
int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
struct blob_attr *msg, ubus_data_handler_t cb, void *priv);
-int ubus_invoke_path(struct ubus_context *ctx, const char *path, const char *method,
- struct blob_attr *msg, ubus_data_handler_t cb, void *priv);
/* asynchronous version of ubus_invoke() */
void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
struct blob_attr *msg, struct ubus_request *req);
-void ubus_invoke_path_async(struct ubus_context *ctx, const char *path, const char *method,
- struct blob_attr *msg, struct ubus_request *req);
/* make an object visible to remote connections */
int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj);
}
obj->type = type;
+ obj->client = cl;
list_add(&obj->list, &cl->objects);
return obj;
struct list_head list;
struct ubus_object_type *type;
-
struct avl_node path;
+
+ struct ubus_client *client;
};
struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr);
[UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
[UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
[UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
+ [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
+ [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
};
struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub)
{
- return UBUS_STATUS_NOT_FOUND;
+ struct ubus_object *obj = NULL;
+ struct blob_attr **attr;
+ const char *method;
+
+ attr = ubus_parse_msg(ub->data);
+ if (!attr[UBUS_ATTR_METHOD])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ if (attr[UBUS_ATTR_OBJID]) {
+ struct ubus_id *id;
+ id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
+ if (id)
+ obj = container_of(id, struct ubus_object, id);
+ } else if (attr[UBUS_ATTR_OBJPATH]) {
+ const char *objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
+ obj = avl_find_element(&path, objpath, obj, path);
+ }
+ if (!obj)
+ return UBUS_STATUS_NOT_FOUND;
+
+ method = blob_data(attr[UBUS_ATTR_METHOD]);
+ blob_buf_init(&b, 0);
+ blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
+ blob_put_string(&b, UBUS_ATTR_METHOD, method);
+ if (attr[UBUS_ATTR_DATA])
+ blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
+ blob_len(attr[UBUS_ATTR_DATA]));
+
+ ubus_msg_free(ub);
+
+ ub = ubus_reply_from_blob(ub, true);
+ if (!ub)
+ return UBUS_STATUS_NO_DATA;
+
+ ub->hdr.type = UBUS_MSG_INVOKE;
+ ub->hdr.peer = cl->id.id;
+ ubus_msg_send(obj->client, ub);
+
+ return -1;
+}
+
+static int ubusd_handle_status(struct ubus_client *cl, struct ubus_msg_buf *ub)
+{
+ struct blob_attr **attr;
+
+ attr = ubus_parse_msg(ub->data);
+ if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_STATUS])
+ goto error;
+
+ cl = ubusd_get_client_by_id(ub->hdr.peer);
+ if (!cl)
+ goto error;
+
+ ub->hdr.peer = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+ ubus_msg_send(cl, ub);
+ return -1;
+
+error:
+ ubus_msg_free(ub);
+ return -1;
}
static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
[UBUS_MSG_PUBLISH] = ubusd_handle_publish,
[UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
[UBUS_MSG_INVOKE] = ubusd_handle_invoke,
+ [UBUS_MSG_STATUS] = ubusd_handle_status,
};
void ubusd_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
else
ret = UBUS_STATUS_INVALID_COMMAND;
+ if (ret == -1)
+ return;
+
ubus_msg_free(ub);
*retmsg_data = htonl(ret);