Openwrt UCI API的使用

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


首先您需要引入頭文件



[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <uci.h>  
  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 plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. config  "server" "webserver"  
  2.     list    "index" "index.html"  
  3.     list    "index" "index.php"  
  4.     list    "index" "default.html"  


代碼片:


[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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 plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #文件名: testconfig  
  2. config  'servver'  
  3.     option  'value' '123' # 我們想修改 'value' 的值爲 '456'  


代碼如下:


[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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的操作:
[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. uci_add_list()  // 添加一個list 值  
  2. uci_del_list()  // 刪除一個list 值  
  3. uci_delete()    // 刪除一個option值  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章