14-Openwrt ubus

ubus是OpenWrt中的进程间通信机制,ubusd实现server,其他进程实现client,例如ubus(cli)、netifd、procd,两个client通信需要通过server转发。

ubus提供了创建socket client端的接口,并且提供了三种现成的客户端供用户直接使用:

  1. 为shell脚本提供的client端。
  2. 为lua脚本提供的client接口。
  3. 为C语言提供的client接口。

这边主要对C语言和shell进行说明

1 ubus method

ubusd已经由openwrt实现了,所以我们就实现client端的ubus,也是基于uloop来实现的,所以其三步骤也是必须的:

void ubus_test(void)
{    
    uloop_init();
    ztest_ubus_init();
    uloop_run();
    ztest_ubus_clean();
    uloop_done();
}

ubus的调用步骤ubus_connect、ubus_add_uloop、ubus_add_object

int ztest_ubus_init(void)
{
    int ret;

    g_ubus_ctx = ubus_connect(NULL);
    if (!g_ubus_ctx) {
        ULOG_ERR("Failed to connect to ubus\n");
        return -1;
    }

    g_ubus_ctx->connection_lost = ztest_ubus_connection_lost;
    ubus_add_uloop(g_ubus_ctx);

    ret = ubus_add_object(g_ubus_ctx, &ztest_object);
    if (ret)
        ULOG_ERR("Failed to add zboard object: %s\n", ubus_strerror(ret));

    return ret;
}

我们主要实现object里面的methods内容,ztest_methods里面定义了当前ubus的所有方法,及每个方法被调用时所访问的回掉函数。

ubus对client端的消息处理抽象出“对象(object)”和“方法(method)”的概念。一个对象中包含多个方法,client需要向server注册收到特定json消息时的处理方法。对象和方法都有自己的名字,发送请求方只需在消息中指定要调用的对象和方法的名字即可。

static const struct ubus_method ztest_methods[] = {
    { .name = "set_test", .handler = ztest_set_test },
    { .name = "get_test", .handler = ztest_get_test },
};

static struct ubus_object_type ztest_object_type =
    UBUS_OBJECT_TYPE("ztest", ztest_methods);

static struct ubus_object ztest_object = {
    .name = "ztest",
    .type = &ztest_object_type,
    .methods = ztest_methods,
    .n_methods = ARRAY_SIZE(ztest_methods),
};

当方法都定义好后,就是现实方法里面的内容,此时就要用到libubox里面的blob和blobmsg等接口进行封装和解析数据格式了。

ubus对client和server之间通信的消息格式进行了定义:client和server都必须将消息封装成json消息格式

enum {
    ZTEST_STATUS,
    ZTEST_PORT,
    ZTEST_MAX
};

static const struct blobmsg_policy zboard_policy[ZTEST_MAX] = {
    [ZTEST_STATUS] = { .name = "status", .type = BLOBMSG_TYPE_INT32 },
    [ZTEST_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
};

static uint32_t g_wan_status;
static uint32_t g_wan_port;

static int ztest_set_test(struct ubus_context *ctx, struct ubus_object *obj,
                        struct ubus_request_data *req, const char *method,
                        struct blob_attr *msg)
{
    struct blob_attr *tb[ZTEST_MAX];

    blobmsg_parse(zboard_policy, ZTEST_MAX, tb, blob_data(msg), blob_len(msg));

    if (!tb[ZTEST_STATUS] || !tb[ZTEST_PORT])
        return UBUS_STATUS_INVALID_ARGUMENT;

    g_wan_status = blobmsg_get_u32(tb[ZTEST_STATUS]);
    g_wan_port = blobmsg_get_u32(tb[ZTEST_PORT]);

    return 0;
}

static int ztest_get_test(struct ubus_context *ctx, struct ubus_object *obj,
                        struct ubus_request_data *req, const char *method,
                        struct blob_attr *msg)
{
    blob_buf_init(&g_bbuf, 0);
    blobmsg_add_u32(&g_bbuf, "status", g_wan_status);
    blobmsg_add_u32(&g_bbuf, "port", g_wan_port);
    ubus_send_reply(ctx, req, g_bbuf.head);

    return 0;
}

ubus的测试我们一般用shell的方式,直接在命令行使用ubus工具测试,如下:

root@zihome:/usr/sbin# ubus list
log
network
network.device
network.interface
network.interface.lan
network.interface.loopback
network.interface.wan
network.wireless
nlwifi.wireless
service
session
system
uci
zboard
zdetect
ztest

获取方法method:

root@zihome:/usr/sbin# ubus list ztest -v
'ztest' @78782e2e
        "set_test":{}
        "get_test":{}

设置消息message:

root@zihome:/usr/sbin# ubus call ztest get_test
{
        "status": 0,
        "port": 0
}
root@zihome:/usr/sbin# ubus call ztest set_test '{"status": 1, "port": 1}'
root@zihome:/usr/sbin# ubus call ztest get_test
{
        "status": 1,
        "port": 1
}

2.ubus_event

ubus的监听事件很经常用,一个进程设置监听后,当别的进程发出通知事件时,就会进入到监听回调函数里面,用来用进程间的通讯非常有用。

static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
                          const char *type, struct blob_attr *msg)
{
    struct blob_attr *tb[ZTEST_MAX];

    ULOG_INFO("receive_event\n");
    blobmsg_parse(zboard_policy, ZTEST_MAX, tb, blob_data(msg), blob_len(msg));

    if (!tb[ZTEST_STATUS] || !tb[ZTEST_PORT])
        return;

    g_wan_status = blobmsg_get_u32(tb[ZTEST_STATUS]);
    g_wan_port = blobmsg_get_u32(tb[ZTEST_PORT]);
}

int ztest_ubus_event_init(void)
{
    int ret;
    static struct ubus_event_handler listener;
    
    memset(&listener, 0, sizeof(listener));
    listener.cb = receive_event;
    ret = ubus_register_event_handler(g_ubus_ctx, &listener, "test_event");
    if (ret){
        ULOG_ERR("ubus_register_event_handler error %s", ubus_strerror(ret));
    }
    return ret;
}

使用shell发送event,进行测试

root@zihome:/usr/sbin# ubus call ztest get_test
{
        "status": 0,
        "port": 0
}
root@zihome:/usr/sbin# ubus send test_event '{"status": 1, "port": 3}'
root@zihome:/usr/sbin# ubus call ztest get_test
{
        "status": 1,
        "port": 3
}

使用shell监听listen事件

root@zihome:/usr/sbin# ubus listen
{ "test_event": {"status":1,"port":3} }
{ "test_event": {"status":1,"port":5} }

代码位于github:https://github.com/creatorly/TestCode/tree/master/openwrt/ztest

参考博客:
https://blog.csdn.net/iampisfan/article/details/78107903

https://blog.csdn.net/jasonchen_gbd/article/details/45627967

https://blog.csdn.net/iampisfan/article/details/78107903
ubus同样基于这套流程,其中ubusd实现server,其他进程实现client,例如ubus(cli)、netifd、procd;
两个client通信需要通过server转发。

OpenWrt源码分析之libubox:
https://blog.csdn.net/iampisfan/article/details/78108100

OpenWrt netifd学习笔记:
https://blog.csdn.net/jasonchen_gbd/article/details/74990247

你可能感兴趣的:(Openwrt)