}
}
+static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+ if (!msg)
+ return;
+
+ fprintf(stderr, "%s\n", blobmsg_format_json(msg, true));
+}
+
static void store_objid(struct ubus_request *req, int type, struct blob_attr *msg)
{
struct blob_attr **attr;
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;
+ req.raw_data_cb = store_objid;
if (ubus_complete_request(ctx, &req))
return 0;
blob_put_string(&b, UBUS_ATTR_OBJPATH, argv[2]);
ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0);
- req.data_cb = receive_lookup;
+ req.raw_data_cb = receive_lookup;
} else if (!strcmp(cmd, "call")) {
if (argc < 4 || argc > 5)
return usage(argv[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, objid);
+ req.data_cb = receive_data;
} else {
return usage(argv[0]);
}
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;
struct ubus_pending_data, list);
list_del(&data->list);
if (!req->cancelled)
- req->data_cb(req, data->type, data->data);
+ 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;
req.object = objid;
req.peer = hdr->peer;
req.seq = hdr->seq;
- ret = obj->methods[method].handler(obj, &req, obj->methods[method].name,
+ ret = obj->methods[method].handler(ctx, obj, &req,
+ obj->methods[method].name,
attrbuf[UBUS_ATTR_DATA]);
send:
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;
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:
}
}
+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)
{
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)
#include "ubusmsg.h"
#include "ubus_common.h"
+struct ubus_context;
struct ubus_msg_src;
struct ubus_object;
struct ubus_request;
struct ubus_request_data;
-typedef int (*ubus_handler_t)(struct ubus_object *obj,
+typedef int (*ubus_handler_t)(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req,
const char *method, struct blob_attr *msg);
typedef void (*ubus_data_handler_t)(struct ubus_request *req,
uint32_t peer;
uint32_t seq;
+ ubus_data_handler_t raw_data_cb;
ubus_data_handler_t data_cb;
ubus_complete_handler_t complete_cb;
/* make an object visible to remote connections */
int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj);
-
+/* send a reply to an incoming object method call */
+int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
+ struct blob_attr *msg);
#include "libubus.h"
static struct ubus_context *ctx;
+struct blob_buf b;
static const struct ubus_signature test_object_sig[] = {
UBUS_METHOD_START("hello"),
static struct ubus_object_type test_object_type =
UBUS_OBJECT_TYPE("test", test_object_sig);
-static int test_hello(struct ubus_object *obj, struct ubus_request_data *req,
- const char *method, struct blob_attr *msg)
+static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
{
- fprintf(stderr, "Hello, world!\n");
+ blob_buf_init(&b, 0);
+ blobmsg_add_string(&b, "message", "Hello, world!\n");
+ ubus_send_reply(ctx, req, b.head);
return 0;
}