[openwrt] ubus实现进程间通信举例

本文详细介绍了在openwrt系统中,利用ubus进行进程间通信的三种方式:1)invoke方式实现端对端通信;2)subscribe/notify方式实现订阅和广播;3)event方式实现事件通知。提供了具体的代码示例,帮助理解ubus在不同场景下的应用。
摘要由CSDN通过智能技术生成

上一篇文章介绍了ubus的组件和实现原理,本文通过代码实例介绍使用ubus进行进程间通信的三种方式。

1. invoke的方式实现端对端通信

最简单的情景就是一个提供服务的server端,一个请求服务的client端,client请求server的服务。
下面的例子中,server注册了一个名为“scan_prog”的对象,该对象中提供一个“scan”方法:
ubus_invoke.h:
#ifndef __UBUS_INVOKE_H__
#define __UBUS_INVOKE_H__
#include <json/json.h>
#include <libubox/blobmsg_json.h>


struct prog_attr {
	char name[64];
	int chn_id;
};
#define PROG_MAX	8


#endif  /* __UBUS_INVOKE_H__ */

invoke_server.c:

#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <json/json.h>
#include <libubox/blobmsg_json.h>
#include "ubus_invoke.h"

static struct ubus_context * ctx = NULL;
static struct blob_buf b;
static const char * sock_path;

static struct prog_attr uri_list[PROG_MAX] = 
{
	{"program_beijing", 1},
	{"program_guangzhou", 2},
	{"program_shanghai", 3},
	{"", -1},
};

enum
{
	SCAN_CHNID,
	SCAN_POLICY_MAX,
};

static const struct blobmsg_policy scan_policy[SCAN_POLICY_MAX] = {
	[SCAN_CHNID] = {.name = "chnID", .type = BLOBMSG_TYPE_INT32},
};

static int ubus_start_scan(struct ubus_context *ctx, struct ubus_object *obj,
		      struct ubus_request_data *req, const char *method,
		      struct blob_attr *msg)
{
	int ret = -1;
	void * json_list = NULL;
	void * json_uri = NULL;
	int idx;

	blob_buf_init(&b, 0);
	
	struct blob_attr *tb[SCAN_POLICY_MAX];
	blobmsg_parse(scan_policy, SCAN_POLICY_MAX, tb, blob_data(msg), blob_len(msg));

	/*
	本例子中,如果请求特定的节目号,返回节目名称。
	如果请求节目号是0,则返回所有节目列表。
	*/
	if (tb[SCAN_CHNID] != NULL)
	{
		int chnid = blobmsg_get_u32(tb[SCAN_CHNID]);

		if (chnid == 0)
		{
			json_uri = blobmsg_open_array(&b, "prog_list");
			for (idx = 0; idx < PROG_MAX; idx++)
			{
				if ('\0' != uri_list[idx].name[0])
				{
					json_list = blobmsg_open_table(&b, NULL);
					blobmsg_add_string(&b, "name", uri_list[idx].name);
					blobmsg_add_u32(&b, "channel", uri_list[idx].chn_id);
					blobmsg_close_table(&b, json_list);
				}
			}
			blobmsg_close_array(&b, json_uri);
			ret = 0;
		}
		else
		{
			for (idx = 0; idx < PROG_MAX; idx++)
			{
				if ('\0' != uri_list[idx].name[0] && uri_list[idx].chn_id == chnid)
				{
					blobmsg_add_string(&b, "name", uri_list[idx].name);
					ret = 0;
				}
			}
		}
	}
	
	blobmsg_add_u32(&b, "result", ret);
	ubus_send_reply(ctx, req, b.head);
	
	return 0;
}

/* 方法列表 */
static struct ubus_method scan_methods[] = 
{
	UBUS_METHOD("scan", ubus_start_scan, scan_policy),
};

/* type目前没有实际用处 */
static struct ubus_object_type scan_obj_type
= UBUS_OBJECT_TYPE("scan_prog", scan_methods);

static struct ubus_object scan_obj = 
{
	.name = "scan_prog", /* 对象的名字 */
	.type = &scan_obj_type,
	.methods = scan_methods,
	.n_methods = ARRAY_SIZE(scan_methods),
};

static void ubus_add_fd(void)
{
	ubus_add_uloop(ctx);

#ifdef FD_CLOEXEC
	fcntl(ctx->sock.fd, F_SE
  • 28
    点赞
  • 144
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
OpenWrt 中,ubus 是一种进程间通信系统,它允许应用程序之间进行简单的、安全的通信。它基于 Unix 套接字和 JSON-RPC 协议,并且是 OpenWrt 的一个重要组成部分,因为它提供了许多 OpenWrt 的核心功能。 在本教程中,我们将介绍如何使用 ubusOpenWrt 中进行进程间通信。 ## 安装 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 事件来接收通知。ubusOpenWrt 的一个重要组成部分,可以让应用程序之间进行简单的、安全的通信。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值