openwrt用户空间进中通信常用的通信方式是使用ubus通信,下面给出一个例子,是客户端向服务器请求ap的个数。
服务器端代码
#include <unistd.h>
#include <signal.h>
#include "libubus.h"
static struct ubus_context *ctx;
static struct blob_buf b;
enum {
REQ_STATION_COUNT,
REQ_AP_NAME,
__REQ_MAX
};
/* 接收的解析格式, client发送过来的调用是 blobmsg_add_u32(&b, "getcnt", ap_index); */
static const struct blobmsg_policy fun2_message_parse_policy[__REQ_MAX] = {
[REQ_STATION_COUNT] = { .name = "getcnt", .type = BLOBMSG_TYPE_INT32 },
[REQ_STATION_COUNT] = { .name = "apname", .type = BLOBMSG_TYPE_STRING },
};
static int fun2_handler(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__REQ_MAX];
//模拟ap数组中station的个数
int ap_array[] = {-1, 11111,22222,33333};
int ap_index;
blobmsg_parse(fun2_message_parse_policy, __REQ_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[REQ_STATION_COUNT])
return UBUS_STATUS_INVALID_ARGUMENT;
ap_index = blobmsg_get_u32(tb[REQ_STATION_COUNT]);
fprintf(stdout, "someone ask the ap[%d]' info \n", ap_index);
if (ap_index > (sizeof(ap_array) / sizeof(int)))
ap_index = 0;
blob_buf_init(&b, 0);
//返回客户端请求的station个数
blobmsg_add_u32(&b, "stacnt", ap_array[ap_index]);
//发送
ubus_send_reply(ctx, req, b.head);
return 0;
}
static const struct ubus_method test_methods[] = {
UBUS_METHOD("fun2", fun2_handler, fun2_message_parse_policy),
};
static struct ubus_object_type test_object_type =
UBUS_OBJECT_TYPE("server_fun", test_methods);
//定义一个ubus 对象。
//其他进程通过调用相应字符串给执行对应的回调函数
//注册成功后可以通过命令ubus list -v 查看到obj
//' server_fun' @79a8beac
// "fun2" : {"getcnt":"Iterger","apname":"String"}
static struct ubus_object test_object = {
.name = "server_fun",
.type = &test_object_type,
.methods = test_methods,
.n_methods = ARRAY_SIZE(test_methods),
};
static void server_main(void)
{
int ret;
//向ubusd新增一个对象,之后test_object在ubusd中有一个id号,其他进程通过该id号来向test_object发送消息。
ret = ubus_add_object(ctx, &test_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;
int ch;
while ((ch = getopt(argc, argv, "cs:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
default:
break;
}
}
argc -= optind;
argv += optind;
uloop_init();
signal(SIGPIPE, SIG_IGN);
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();
return 0;
}
客户端代码
#include <sys/time.h>
#include <unistd.h>
#include "libubus.h"
static struct ubus_context *ctx;
static struct blob_buf b;
enum {
RETURN_CODE,
__RETURN_MAX,
};
static const struct blobmsg_policy return_policy[__RETURN_MAX] = {
[RETURN_CODE] = { .name = "stacnt", .type = BLOBMSG_TYPE_INT32 },
};
static void get_result_data_cb(struct ubus_request *req,
int type, struct blob_attr *msg)
{
struct blob_attr *tb[__RETURN_MAX];
int rc;
//返回的数据也是有tlv格式的,按照return_policy格式进行解析。
blobmsg_parse(return_policy, __RETURN_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RETURN_CODE]) {
fprintf(stderr, "No return code received from server\n");
return;
}
rc = blobmsg_get_u32(tb[RETURN_CODE]);
fprintf(stderr, "get station number is %d \n", rc);
}
static void client_main(void)
{
static struct ubus_request req;
uint32_t id;
int ret;
int ap_index=2;
//查找ubusd上的server_fun对象。该对象由server端注册
if (ubus_lookup_id(ctx, "server_fun", &id)) {
fprintf(stderr, "Failed to look up test object\n");
return;
}
//b 为全局变量,初始化
blob_buf_init(&b, 0);
//添加发送buffer的tlv,格式由server_main.c的fun2_message_parse_policy定义
blobmsg_add_u32(&b, "getcnt", ap_index);
//触发server端注册的函数,并接收返回值,处理函数为get_result_data_cb
ubus_invoke(ctx, id, "fun2", b.head, get_result_data_cb, NULL, 3000);
}
int main(int argc, char **argv)
{
const char *ubus_socket = NULL;
int ch;
while ((ch = getopt(argc, argv, "cs:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
default:
break;
}
}
argc -= optind;
argv += optind;
uloop_init();
ctx = ubus_connect(ubus_socket);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop(ctx);
client_main();
ubus_free(ctx);
uloop_done();
return 0;
}
以上代码:服务器端向ubusd注册了个server_fun的服务器对象,客户端通过server_fun ubus对象与服务器端通信。
其他:ubus shell调用格式
ubus send linkinspect '{"ac_cmd":1}'