invoke实现进程间的通信方式是一种端到端的通信方式,需要提供一个server端和一个clinet端。
//server.c
#include
#include
#include
#include "libubus.h"
static struct ubus_context *ctx;
static struct blob_buf b;
static int hello_hello(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
enum
{
HELLO_ID,
HELLO_MSG,
__HELLO_MAX
};
static const struct blobmsg_policy hello_policy[] = {
[HELLO_ID] = {.name = "id", .type = BLOBMSG_TYPE_INT32},
[HELLO_MSG] = {.name = "msg", .type = BLOBMSG_TYPE_STRING},
};
static const struct ubus_method hello_methods[] = {
UBUS_METHOD("hello", hello_hello, hello_policy),
};
static struct ubus_object_type hello_object_type =
UBUS_OBJECT_TYPE("hello_test", hello_methods);
static struct ubus_object hello_object = {
.name = "hello_test",
.type = &hello_object_type,
.methods = hello_methods,
.n_methods = ARRAY_SIZE(hello_methods),
};
static int hello_hello(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__HELLO_MAX];
const char *msgstr = "(unknown)";
blob_buf_init(&b, 0);
blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
if (tb[HELLO_MSG])
msgstr = blobmsg_data(tb[HELLO_MSG]);
printf("===============%s\n", msgstr);
blobmsg_add_string(&b, "name", msgstr); // ubus调用封装成blobmsg_json格式返回,其实就是一种json格式
blobmsg_add_u32(&b, "result", 0);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static void server_main(void)
{
int ret;
ret = ubus_add_object(ctx, &hello_object);
if (ret)
fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
uloop_run();
}
int main(int argc, char **argv)
{
const char *ubus_socket = NULL;
uloop_init();
ctx = ubus_connect(ubus_socket);
if (!ctx)
{
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop(ctx);
server_main();
ubus_free(ctx);
uloop_done();
}
client.c
//client.c
#include
#include
#include
#include "libubus.h"
static struct ubus_context *ctx;
static struct blob_buf b;
static int hello_hello(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
enum
{
HELLO_ID,
HELLO_MSG,
__HELLO_MAX
};
static const struct blobmsg_policy hello_policy[] = {
[HELLO_ID] = {.name = "id", .type = BLOBMSG_TYPE_INT32},
[HELLO_MSG] = {.name = "msg", .type = BLOBMSG_TYPE_STRING},
};
static const struct ubus_method hello_methods[] = {
UBUS_METHOD("hello", hello_hello, hello_policy),
};
static struct ubus_object_type hello_object_type =
UBUS_OBJECT_TYPE("hello_test", hello_methods);
static struct ubus_object hello_object = {
.name = "hello_test",
.type = &hello_object_type,
.methods = hello_methods,
.n_methods = ARRAY_SIZE(hello_methods),
};
static int hello_hello(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__HELLO_MAX];
const char *msgstr = "(unknown)";
blob_buf_init(&b, 0);
blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
if (tb[HELLO_MSG])
msgstr = blobmsg_data(tb[HELLO_MSG]);
printf("===============%s\n", msgstr);
blobmsg_add_string(&b, "msg", msgstr);
blobmsg_add_u32(&b, "result", 0);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static void server_main(void)
{
int ret;
ret = ubus_add_object(ctx, &hello_object);
if (ret)
fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
uloop_run();
}
static void scanreq_prog_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
char *str;
if (!msg)
return;
struct blob_attr *tb[__HELLO_MAX];
blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
//str = blobmsg_format_json_indent(msg, true, 0 ? -1 : 0); //获取有缩进的json字符串,一般用于答应
str = blobmsg_format_json(msg, true); //返回json字符串
printf("------------%s\n", str);
/*
* TODO 调用json库对json字符串进行解析
*/
free(str);
}
static int client_ubus_call()
{
unsigned int id;
int ret;
int timeout = 30;
blob_buf_init(&b, 0);
/* 需要传递的参数 */
blobmsg_add_u32(&b, hello_policy[HELLO_ID].name, 1);
blobmsg_add_string(&b, hello_policy[HELLO_MSG].name, "wuxiaorong");
ret = ubus_lookup_id(ctx, "hello_test", &id);
if (ret != UBUS_STATUS_OK)
{
printf("lookup scan_prog failed\n");
return ret;
}
else
{
printf("lookup scan_prog successs\n");
}
ubus_invoke(ctx, id, "hello", b.head, scanreq_prog_cb, NULL, timeout * 1000);
printf("====end============\n");
}
int main(int argc, char **argv)
{
const char *ubus_socket = NULL;
uloop_init();
ctx = ubus_connect(ubus_socket);
if (!ctx)
{
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
client_ubus_call();
ubus_free(ctx);
return 0;
}