Openwrt UCI API的使用
来源:互联网 发布:腾讯官网软件下载 编辑:程序博客网 时间:2024/05/18 02:08
OpenWRT UCI API的使用
UCI 是OpenWRT为实现配置集中化而引入的一个软件包, 通过修改UCI,可以实现对OpenWRT的绝对部分配置的修改.LUCI(OpenWRT 的WEB配置界面)也是通过读UCI配置文件的操作来实现用户对路由的配置的。通过掌握UCI的API的使用,可以方便地将您的软件的配置接口集成到LUCI中.原博主:http://blog.csdn.net/bywayboy/article/details/20866287
LUCI配置文件简介
LUCI的配置文件一般存储在 /etc/config目录下。比如网络配置文件则是 /etc/config/network 无线的配置文件是 /etc/config/wireless. 跟多配置文件的含义参考官方 WIKI
基本概念
UCI上下文: struct uci_context *
包(Package): 一个包对应一个UCI格式的文件.类型是 struct uci_package *
节(Section): 一个配置文件的节点. 类型是 struct uci_list *
值(Value):一个节下面可能包含多个值 一个值具有一个名字.
UCI配置文件的基本操作.
首先您需要引入头文件
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <uci.h>
- static struct uci_context * ctx = NULL; //定义一个UCI上下文的静态变量.
- /*********************************************
- * 载入配置文件,并遍历Section.
- */
- bool load_config()
- {
- struct uci_package * pkg = NULL;
- struct uci_element *e;
- ctx = uci_alloc_context(); // 申请一个UCI上下文.
- if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
- goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.
- /*遍历UCI的每一个节*/
- uci_foreach_element(&pkg->sections, e)
- {
- struct uci_section *s = uci_to_section(e);
- // 将一个 element 转换为 section类型, 如果节点有名字,则 s->anonymous 为 false.
- // 此时通过 s->e->name 来获取.
- // 此时 您可以通过 uci_lookup_option()来获取 当前节下的一个值.
- if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))
- {
- ip = strdup(value) //如果您想持有该变量值,一定要拷贝一份。当 pkg销毁后value的内存会被释放。
- }
- // 如果您不确定是 string类型 可以先使用 uci_lookup_option() 函数得到Option 然后再判断.
- // Option 的类型有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 两种.
- }
- uci_unload(ctx, pkg); // 释放 pkg
- cleanup:
- uci_free_context(ctx);
- ctx = NULL;
- }
遍历一个UCI_TYPE_LIST 类型.
加入现在有一个如下的配置文件:
- config "server" "webserver"
- list "index" "index.html"
- list "index" "index.php"
- list "index" "default.html"
代码片:
- // s 为 section.
- struct uci_option * o = uci_lookup_option(ctx, s, "index");
- if ((NULL != o) && (UCI_TYPE_LIST == o->type)) //o存在 且 类型是 UCI_TYPE_LIST则可以继续.
- {
- struct uci_element *e;
- uci_foreach_element(&o->v.list, e)
- {
- //这里会循环遍历 list
- // e->name 的值依次是 index.html, index.php, default.html
- }
- }
写配置
UCI提供了一个简洁的办法来操作配置信息,例如有一个配置文件
- #文件名: testconfig
- config 'servver'
- option 'value' '123' # 我们想修改 'value' 的值为 '456'
代码如下:
- struct uci_context * ctx = uci_alloc_context(); //申请上下文
- struct uci_ptr ptr ={
- .package = "config",
- .section = "servver",
- .option = "value",
- .value = "256",
- };
- uci_set(_ctx,&ptr); //写入配置
- uci_commit(_ctx, &ptr.p, false); //提交保存更改
- uci_unload(_ctx,ptr.p); //卸载包
- uci_free_context(ctx); //释放上下文
依照上面的例子,我们可以举一反三, uci_ptr 用来指定信息.而是用uci_set则是写入信息.同类的函数有如下几个: 针对list的操作:
- uci_add_list() // 添加一个list 值
- uci_del_list() // 删除一个list 值
- uci_delete() // 删除一个option值
1 0
- OpenWRT UCI API的使用
- OpenWRT UCI API的使用
- OpenWRT UCI API的使用
- Openwrt UCI API的使用
- [转载]OpenWRT UCI API的使用
- openwrt uci的使用
- OpenWRT UCI API的使用——C语言
- openwrt UCI API lua wiki
- Openwrt的uci操作
- OpenWrt的UCI系统
- C代码调用uci的API读openwrt配置文件指南
- openwrt uci api续: 找出匿名节点的"名字"
- openwrt uci api: 配置文件匿名节点的操作(2)
- Openwrt下C调用UCI API
- 【智能路由器】C代码调用uci的API读openwrt配置文件指南
- 为什么openwrt要使用Luci (lua + uci)
- ubuntu 编译独立与openwrt的uci
- [openwrt] uci 的shell和lua接口
- mysql数据的备份恢复和还原
- Xcode真机调试常碰到的问题
- javascript库之Mustache库使用说明
- Android自定义控件——FloatLayout
- unittest 'module' object has no attribute 'TestCase'--自己坑自己
- Openwrt UCI API的使用
- android系统简介
- mysql高可用浅析
- LINUX下动态库调用静态库的方法
- 表层+深层
- JVM原理及底层探索
- rk3288 编写Android.mk 形式内置APK
- local storage (H5本地存储)
- mysql在数据库表中查询表名和字段名