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端到端通信。