当前位置: 首页 > news >正文

UBUS 通信接口的使用——添加一个object对象(ubus call)

1,引入

        ubus提供了一种多进程通信的机制。存在一个守护进程ubusd,所以进程都注册到ubusd,ubusd进行消息的接收、分发管理。

ubus对多线程支持的不好,例如在多个线程中去请求同一个服务,就有可能出现不可预知的结果。

ubus通信一共有三种实现方式:①端对端通信  ②订阅/通知  ③事件(广播)

2,ubus命令的使用

ubus list :列出所有对象   
ubus -v list network.interface.lan : 查看指定对象的详细信息
ubus call network.interface.lan status :执行指定对象的方法并获取返回结果
ubus listen [事件类型]: 实时接收指定或所有 ubus 事件 
ubus send test.event '{"message":"Hello World"}'  : 发送自定义事件

  ubus subscribe mytest : 订阅mytest事件

3,向UBUS注册一个对象

主要步骤:uloop_init(); ubus_connect(NULL);ubus_add_uloop(ser_ctx);初始化object,ubus_add_object(ser_ctx, &sub_object);

代码实现:

int get_no_arg_info(struct ubus_context *ctx, struct ubus_object *obj,struct ubus_request_data *req, const char *method,struct blob_attr *msg)
{struct blob_buf b = {};blob_buf_init(&b, 0);blobmsg_add_string(&b, "obj_name", obj->name);blobmsg_add_u32(&b, "pid", (uint32_t)getpid());blobmsg_add_u32(&b, "uptime", (uint32_t)time(NULL));ubus_send_reply(ctx, req, b.head);blob_buf_free(&b);return 0;
}enum parm_num {T_ID = 0,T_NAME,T_AGE,T_MAX
};static const struct blobmsg_policy t_policy[] = {[T_ID]   = { .name = "id", .type = BLOBMSG_TYPE_INT32 },[T_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },[T_AGE]  = { .name = "age", .type = BLOBMSG_TYPE_INT32 },
};int get_arg_info(struct ubus_context *ctx, struct ubus_object *obj,struct ubus_request_data *req, const char *method,struct blob_attr *msg)
{int i = 0;struct blob_attr *tb[T_MAX];blobmsg_parse(t_policy, ARRAY_SIZE(t_policy), tb, blob_data(msg), blob_len(msg));for(i = 0; i < T_MAX; i++){if(!tb[i]){fprintf(stderr, "Failed to parse arg '%d'.\n", i);return -1;}}printf("-->id:   %d\n", blobmsg_get_u32(tb[T_ID]));printf("-->name: %s\n", blobmsg_get_string(tb[T_NAME]));printf("-->age:  %d\n", blobmsg_get_u32(tb[T_AGE]));return 0;
}static const struct ubus_method test_obj_methods[] = {UBUS_METHOD_NOARG("get_no_arg_info", get_no_arg_info),UBUS_METHOD( "get_arg_info", get_arg_info, t_policy),
};static struct ubus_object_type test_obj_type = UBUS_OBJECT_TYPE("test.service", test_obj_methods);static struct ubus_object test_object = {.name = "test.service",.type = &test_obj_type,.methods = test_obj_methods,.n_methods = ARRAY_SIZE(test_obj_methods),
};int main()
{int ret = 0;struct ubus_context *mytest_ctx = NULL;uloop_init();mytest_ctx = ubus_connect(NULL);if(!mytest_ctx) {fprintf(stderr, "Failed to connect ubus.\n");return ERROR;}ret = ubus_add_object(mytest_ctx, &test_object);if(ret) {fprintf(stderr, "Failed to add 'mytest' obj.\n");return ERROR;}ubus_add_uloop(mytest_ctx);uloop_run();ubus_free(mytest_ctx);uloop_done();
}

测试:

补充:实现了ubus call 去获取test.service

void mytest_reply_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{struct blob_buf buf = {};char *str;if (!msg) {printf("No response received\n");return;}str = blobmsg_format_json(msg, true);if (str) {printf("\nReceived response:\n%s\n", str);free(str);}
}int main()
{int ret = 0;int i = 0;int mydemo_id = 0;struct ubus_context *myclient_ctx = NULL;struct ubus_request req;uloop_init();myclient_ctx =  ubus_connect(NULL);if(!myclient_ctx) {fprintf(stderr, "Failed to connect ubus.\n");return ERROR;}ret = ubus_lookup_id(myclient_ctx, "test.service", &mydemo_id);if (ret) {fprintf(stderr, "Failed to lookup test object: %s\n", ubus_strerror(ret));return ERROR;}ubus_add_uloop(myclient_ctx);#if 0 //不带参数请求test.service ->get_no_arg_info方法ret = ubus_invoke(myclient_ctx, mydemo_id, "get_no_arg_info", NULL, mytest_reply_cb, &req, 1000);if (ret) {fprintf(stderr, "Failed to call get_no_arg_info: %s\n", ubus_strerror(ret));return ret;}//ubus_complete_request(myclient_ctx, &req, 1000);#else //带参数请求test.service ->get_no_arg_info方法struct blob_buf msg = {};blob_buf_init(&msg, 0);blobmsg_add_string(&msg, "name", "Hello");blobmsg_add_u32(&msg, "id", 12);blobmsg_add_u32(&msg, "age", 36);memset( &req, 0, sizeof(req));ret = ubus_invoke(myclient_ctx, mydemo_id, "get_arg_info", msg.head, mytest_reply_cb, &req, 1000);if (ret) {fprintf(stderr, "Failed to call get_no_arg_info: %s\n", ubus_strerror(ret));return ret;}//ubus_complete_request(myclient_ctx, &req, 1000);blob_buf_free(&msg);#endifuloop_run();ubus_free(myclient_ctx);uloop_done();}

 结果:

该打印由get_arg_info打印,我发送过去的msg。

总结:

        1,实现了向UBUS添加一个object的代码,需要依赖Ubus头文件:#include <libubox/blobmsg_json.h>  #include <libubus.h>

        2,比较繁琐的点是在test_object这个结构体的初始化,无参数调用使用UBUS_METHOD_NOARG, 有参数调用使用:UBUS_METHOD,此时UBUS_METHOD的后两个参数不能填NULL,否则段错误。

        3,使用UBUS 命令call,其内部ubus_invoke,实现UBUS端到端通信。


http://www.mrgr.cn/news/100717.html

相关文章:

  • uml类关系(实现、继承,聚合、组合,依赖、关联)
  • SpringCloud学习笔记
  • 推荐系统中 Label 回收机制之【时间窗口设计】
  • Unity-Shader详解-其三
  • 实现一个简单回调列表
  • 51c自动驾驶~合集37
  • Docker安装的mysql限制ip访问
  • ACTF2025 - WEB Excellent-Site
  • 二叉树知识点
  • 软考高项(信息系统项目管理师)第 4 版全章节核心考点解析(第4版课程精华版)
  • 复旦大学发布全球首款二维半导体芯片——无极
  • LangChain入门(四) 部署应用程序
  • 2. python协程/异步编程详解
  • 细说STM32单片机FreeRTOS互斥量及其编程实例
  • Harbor默认Redis与Notary组件弱口令漏洞分析与修复指南
  • NVIDIA高级辅助驾驶领域的创新实践与云计算教育启示
  • 双系统安装 ios放同一个u盘 ventory使用+windows安装,双系统互相访问中间盘 切换默认启动系统
  • 数据分析1
  • 【大模型】Coze AI 智能体工作流从配置到使用实战详解
  • 软考高项(信息系统项目管理师)第 4 版全章节核心考点解析(力扬老师课程精华版)