当前位置: 首页 > 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();
}

测试:

总结:

        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.xdnf.cn/news/210547.html

相关文章:

  • 开放平台架构方案- GraphQL 详细解释
  • 2025年- H13-Lc120-189.轮转数组(普通数组)---java版
  • Cliosoft安装
  • 【AI学习】李宏毅新课《DeepSeek-R1 这类大语言模型是如何进行「深度思考」(Reasoning)的?》的部分纪要
  • 大屏 UI 设计:解锁视觉盛宴的奥秘
  • Microsoft .NET Framework 3.5 离线安装包 下载
  • python celery框架结合django的使用
  • 爬虫学习笔记(五)---数据解析之re
  • 【最新 MCP 战神手册 09】利用资源和提示增强上下文
  • Linux批量管理:Ansible自动化运维指南
  • 飞蛾扑火算法优化+Transformer四模型回归打包(内含MFO-Transformer-LSTM及单独模型)
  • 开源Kotlin从零单排0基础完美入门教程
  • 第十六届蓝桥杯 2025 C/C++组 破解信息
  • 绿色版的notepad++怎么加入到右键菜单里
  • 深度学习---pytorch搭建深度学习模型(附带图片五分类实例)
  • 【docker】启动临时MongoDB容器、挂载数据卷运行数据库服务,并通过备份文件恢复MongoDB数据库备份数据
  • MCP 架构全解析:Host、Client 与 Server 的协同机制
  • Spring MVC 中解决中文乱码问题
  • 解决STM32H743单片机USB_HOST+FATF操作usb文件
  • 代码随想录算法训练营 Day35 动态规划Ⅲ 0-1背包问题
  • Python数据处理:文件的自动化重命名与整合
  • JavaWeb:后端web基础(TomcatServletHTTP)
  • 当跨网文件传输遇上医疗级安全筛查
  • <c++>使用detectMultiScale的时候出现opencv.dll冲突
  • Docker容器资源控制--CGroup
  • 公路风险落图,道路点任意经纬度里程求解
  • 2. python协程/异步编程详解
  • 【软考-高级】【信息系统项目管理师】【论文基础】沟通管理过程输入输出及工具技术的使用方法
  • python的turtle库实现四叶草
  • Reactor框架介绍