ubus进程间的通信方式invoke

invoke实现进程间的通信方式是一种端到端的通信方式,需要提供一个server端和一个clinet端。

//server.c
#include <unistd.h>
#include <signal.h>

#include <libubox/blobmsg_json.h>
#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 <unistd.h>
#include <signal.h>

#include <libubox/blobmsg_json.h>
#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;
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 OpenWrt 中,ubus 是一种进程通信系统,它允许应用程序之进行简单的、安全的通信。它基于 Unix 套接字和 JSON-RPC 协议,并且是 OpenWrt 的一个重要组成部分,因为它提供了许多 OpenWrt 的核心功能。 在本教程中,我们将介绍如何使用 ubus 在 OpenWrt 中进行进程通信。 ## 安装 ubus 首先,我们需要在 OpenWrt 中安装 ubus。可以通过以下命令来安装: ``` opkg update opkg install ubus ``` ## 使用 ubus ### ubus 命令行工具 ubus 命令行工具提供了一个简单的方式来与 ubus 服务进行交互。以下是一些常用的 ubus 命令: - `ubus call`:调用一个 ubus 方法。 - `ubus list`:列出所有可用的 ubus 方法。 - `ubus monitor`:监听 ubus 事件。 例如,要列出所有可用的 ubus 方法,可以运行以下命令: ``` ubus list ``` 你将看到一份所有可用的 ubus 方法的列表。 ### ubus 库 如果你希望在应用程序中使用 ubus,可以使用 ubus 库。ubus 库是一个 C 库,允许应用程序通过编程方式调用 ubus 方法。 要使用 ubus 库,需要包含 `libubus.h` 头文件,并链接 `libubus` 库。以下是一个简单的例子: ```c #include <libubus.h> int main() { struct ubus_context *ctx = ubus_connect(NULL); if (!ctx) { printf("Failed to connect to ubus\n"); return 1; } struct blob_buf buf; blob_buf_init(&buf, 0); blobmsg_add_string(&buf, "message", "Hello, world!"); struct ubus_request req; ubus_request_init(ctx, &req, "example_method", &buf.head); if (ubus_invoke(ctx, &req)) { printf("Failed to invoke ubus method\n"); return 1; } return 0; } ``` 在这个例子中,我们连接到 ubus 服务,并调用了一个名为 `example_method` 的 ubus 方法,传递了一个包含 `message` 字段的 blob 对象。当 ubus 方法被调用时,它会收到这个包含消息的 blob 对象,并且可以进行相应的操作。 ### ubus 事件 除了调用 ubus 方法之外,ubus 还支持事件。应用程序可以向 ubus 注册事件,并在事件发生时接收通知。以下是一个简单的例子: ```c #include <libubus.h> static void event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg) { printf("Received event: %s\n", type); } int main() { struct ubus_context *ctx = ubus_connect(NULL); if (!ctx) { printf("Failed to connect to ubus\n"); return 1; } struct ubus_event_handler handler = { .cb = event_handler, }; ubus_register_event_handler(ctx, &handler, "example_event"); while (1) { ubus_handle_event(ctx); } return 0; } ``` 在这个例子中,我们向 ubus 注册了一个名为 `example_event` 的事件,并在事件发生时打印出了事件的类型。 ## 总结 在本教程中,我们介绍了 OpenWrt 中的 ubus 进程通信系统,并且演示了如何使用 ubus 命令行工具和 ubus 库进行进程通信。此外,我们还展示了如何使用 ubus 事件来接收通知。ubus 是 OpenWrt 的一个重要组成部分,可以让应用程序之进行简单的、安全的通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值