Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1873257
  • 博文数量: 376
  • 博客积分: 2147
  • 博客等级: 大尉
  • 技术积分: 3642
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-06 10:47
文章分类

全部博文(376)

文章存档

2019年(3)

2017年(28)

2016年(15)

2015年(17)

2014年(182)

2013年(16)

2012年(115)

我的朋友

分类: 嵌入式

2014-11-19 15:19:43

原文地址:http://blog.csdn.net/bywayboy/article/details/20866287

OpenWRT UCI API的使用

UCI 是OpenWRT为实现配置集中化而引入的一个软件包, 通过修改UCI,可以实现对OpenWRT的绝对部分配置的修改.LUCI(OpenWRT 的WEB配置界面)也是通过读UCI配置文件的操作来实现用户对路由的配置的。通过掌握UCI的API的使用,可以方便地将您的软件的配置接口集成到LUCI中.


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配置文件的基本操作.


首先您需要引入头文件



  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6.   
  7. static struct uci_context * ctx = NULL; //定义一个UCI上下文的静态变量.  
  8. /********************************************* 
  9. *   载入配置文件,并遍历Section. 
  10. */  
  11. bool load_config()  
  12. {  
  13.     struct uci_package * pkg = NULL;  
  14.     struct uci_element *e;  
  15.   
  16.   
  17.     ctx = uci_alloc_context(); // 申请一个UCI上下文.  
  18.     if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))  
  19.         goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.  
  20.   
  21.   
  22.     /*遍历UCI的每一个节*/  
  23.     uci_foreach_element(&pkg->sections, e)  
  24.     {  
  25.         struct uci_section *s = uci_to_section(e);  
  26.         // 将一个 element 转换为 section类型, 如果节点有名字,则 s->anonymous 为 false.  
  27.         // 此时通过 s->e->name 来获取.  
  28.         // 此时 您可以通过 uci_lookup_option()来获取 当前节下的一个值.  
  29.         if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))  
  30.         {  
  31.             ip = strdup(value) //如果您想持有该变量值,一定要拷贝一份。当 pkg销毁后value的内存会被释放。  
  32.         }  
  33.         // 如果您不确定是 string类型 可以先使用 uci_lookup_option() 函数得到Option 然后再判断.  
  34.         // Option 的类型有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 两种.  
  35.   
  36.   
  37.     }  
  38.     uci_unload(ctx, pkg); // 释放 pkg   
  39. cleanup:  
  40.     uci_free_context(ctx);  
  41.     ctx = NULL;  
  42. }  


遍历一个UCI_TYPE_LIST 类型.



加入现在有一个如下的配置文件:


[plain] view plaincopy
  1. config  "server" "webserver"  
  2.     list    "index" "index.html"  
  3.     list    "index" "index.php"  
  4.     list    "index" "default.html"  


代码片:


  1. // s 为 section.  
  2. struct uci_option * o = uci_lookup_option(ctx, s, "index");  
  3. if ((NULL != o) && (UCI_TYPE_LIST == o->type)) //o存在 且 类型是 UCI_TYPE_LIST则可以继续.  
  4. {  
  5.     struct uci_element *e;  
  6.     uci_foreach_element(&o->v.list, e)  
  7.     {  
  8.         //这里会循环遍历 list  
  9.         // e->name 的值依次是 index.html, index.php, default.html  
  10.     }  
  11. }  


写配置



UCI提供了一个简洁的办法来操作配置信息,例如有一个配置文件


[plain] view plaincopy
  1. #文件名: testconfig  
  2. config  'servver'  
  3.     option  'value' '123' # 我们想修改 'value' 的值为 '456'  


代码如下:


  1. struct uci_context * ctx = uci_alloc_context(); //申请上下文  
  2. struct uci_ptr ptr ={  
  3.     .package = "config",  
  4.     .section = "servver",  
  5.     .option = "value",  
  6.     .value = "256",  
  7. };  
  8. uci_set(_ctx,&ptr); //写入配置  
  9. uci_commit(_ctx, &ptr.p, false); //提交保存更改  
  10. uci_unload(_ctx,ptr.p); //卸载包  
  11.   
  12.   
  13. uci_free_context(ctx); //释放上下文  


依照上面的例子,我们可以举一反三, uci_ptr 用来指定信息.而是用uci_set则是写入信息.同类的函数有如下几个: 针对list的操作:
  1. uci_add_list()  // 添加一个list 值  
  2. uci_del_list()  // 删除一个list 值  
  3. uci_delete()    // 删除一个option值  




阅读(26663) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~