Ubus安装超详细保姆级,附测试结果

一、本文使用系统

基于Ubuntu20.04 64位进行搭建

内核版本:5.15.0-117-generic

gcc编译器版本:9.40

cmake版本:3.16.3

二、安装依赖

sudo apt-get install git cmake autoconf

sudo apt-get install libjson-c-dev

sudo apt install lua5.1

sudo apt install liblua5.1-0-dev

sudo apt install libjson-c-dev

sudo apt-get install libssl-dev

注意:ubuntu20.4使用命令行安装的cmake是3.16 ,20.04以下的系统均低于3.10以下,到libubox编译时候会报错,提示需要3.12或更高的版本,此时需要进入官网下载源码,安装高版本的cmake。

三、最新cmake3.30.1安装方法

1.官网下载对应的包:Index of /files (cmake.org)

2.解压到当前目录下,并进入cmake3.30.1文件夹中

3.执行引导文件:

./bootstrap

4.进行编译:

make

5.进行安装:

sudo make install

6.查看版本号:

cmake –version

四、下载源码

git clone https://github.com/json-c/json-c.git

git clone http://git.openwrt.org/project/libubox.git

git clone https://git.openwrt.org/project/ubus.git

注意:下载的源码是下载到在当前目录下

五、编译安装json-c

cd json-c
mkdir build
cd build
cmake ../

注意:可以 ls 查看是否有 Makefile

make
sudo make install
cd /usr/local/include
sudo mv json-c json

注意:把json-c移动到../json下了

六、编译安装libubox

cd libubox/
mkdir build
vi CMakeLists.txt

修改 OPTION(BUILD_LUA “build Luaplugin” ON) -----> ON 改为 OFF。在当前行上面添加下面数据:

include_directories("/usr/local/include")

link_directories("/usr/local/lib")

修改文件/libubox/examples/json_script-example.c

第四行 #include <json/json.h> #修改这行

注意:修改就是刚刚移动的json-c的目录,此修改是修正头文件的目录

jshn.c、blobmsg_json.c这两个代码也需要进行更改

把json.h 更改成json/json.h路径的头文件

继续执行下面命令:

cd build
cmake ../
make
sudo make install

七、编译安装ubus

cd ubus
mkdir build
vim CMakeLists.txt

修改 OPTION(BUILD_LUA “build Luaplugin” ON) -----> ON 改为 OFF。在当前行上面添加以下代码:

include_directories("/usr/local/include")

link_directories("/usr/local/lib")

cd build
cmake ../
make
sudo make install

八、检查安装状态

1.查看ubusd是否安装:

which ubusd

2.手动启动ubusd:

sudo ubusd &      

注意:必须使用root权限,不然会出错

3. 查看ubusd是否运行:

ps aux | grep ubusd

注意:可能出现的问题:ubusd: error while loading shared libraries: libubox.so:cannot open shared object file

解决办法:

(1).这个是因为系统没有找到libubox.so的位置,使用以下命令查找以下查找一下

find /usr/local/lib /usr/lib /lib -name "libubox.so*"

(2)更新环境变量

如果libubox.so位于 /usr/local/lib  执行以下代码

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

(3) 为了使此更改在每次会话中永久生效,将上述行添加到 ~/.bashrc 文件中:              

echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc

source ~/.bashrc

(4)刷新共享库

sudo ldconfig

九、测试

1.服务器端:root权限下

编写服务器代码:确保ubus正确启动并注册接口,然后在root模式下进行编译,执行

编译方法:

gcc -o ubus_service ubus_service.c -lubus -lubox

2.客户端:root权限下

              终端输入:

ubus call test_service say_hello '{"name": "World"}'

              来测试ubus是否注册成功并可以正常通信,

              成功返回结果如下图:

至此ubus安装及测试全部完毕了

  • 19
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的发布订阅模式的ubus通信测试代码,你可以参考一下: 服务端代码: ```c #include <libubox/uloop.h> #include <libubus.h> static struct ubus_context *ctx; static void test_subscribe_handler(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { const char *data = blobmsg_get_string(msg, "data"); printf("Received data: %s\n", data); } static const struct ubus_method test_methods[] = { { .name = "subscribe", .handler = test_subscribe_handler }, }; static struct ubus_object_type test_object_type = UBUS_OBJECT_TYPE("test", test_methods); static struct ubus_object test_object = { .name = "test", .type = &test_object_type, .methods = test_methods, .n_methods = ARRAY_SIZE(test_methods), }; static void test_notify_handler(struct ubus_context *ctx, struct ubus_subscriber *sub, uint32_t id, struct blob_attr *msg) { const char *data = blobmsg_get_string(msg, "data"); printf("Received data: %s\n", data); } int main(int argc, char **argv) { ctx = ubus_connect(NULL); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); if (ubus_add_object(ctx, &test_object) != 0) { fprintf(stderr, "Failed to add ubus object\n"); ubus_free(ctx); return -1; } struct ubus_subscriber subscriber; memset(&subscriber, 0, sizeof(subscriber)); subscriber.cb = test_notify_handler; if (ubus_subscribe(ctx, &subscriber, "test.subscribe") != 0) { fprintf(stderr, "Failed to subscribe\n"); ubus_free(ctx); return -1; } uloop_run(); return 0; } ``` 客户端代码: ```c #include <libubox/uloop.h> #include <libubus.h> static struct ubus_context *ctx; static void test_notify_handler(struct ubus_context *ctx, struct ubus_subscriber *sub, uint32_t id, struct blob_attr *msg) { const char *data = blobmsg_get_string(msg, "data"); printf("Received data: %s\n", data); } int main(int argc, char **argv) { ctx = ubus_connect(NULL); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); struct ubus_subscriber subscriber; memset(&subscriber, 0, sizeof(subscriber)); subscriber.cb = test_notify_handler; if (ubus_subscribe(ctx, &subscriber, "test.subscribe") != 0) { fprintf(stderr, "Failed to subscribe\n"); ubus_free(ctx); return -1; } struct blob_buf buf; blob_buf_init(&buf, 0); blobmsg_add_string(&buf, "data", "Hello, world!"); if (ubus_notify(ctx, "test.subscribe", buf.head, 0, NULL) != 0) { fprintf(stderr, "Failed to send notification\n"); ubus_free(ctx); return -1; } uloop_run(); return 0; } ``` 这个例子中,服务端会创建一个ubus对象并添加到ubus上下文中,同时会添加一个订阅处理函数和一个订阅者。当客户端发送一个通知时,服务端订阅处理函数会被调用,并且订阅者也会收到通知。客户端会先订阅test.subscribe事件,然后发送一条带有"data"字段的通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值