1. 程式人生 > >Openwrt UCI API的使用

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


首先您需要引入標頭檔案



  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <uci.h>
  5. staticstruct
     uci_context * ctx = NULL; //定義一個UCI上下文的靜態變數.
  6. /********************************************* 
  7. *   載入配置檔案,並遍歷Section. 
  8. */
  9. bool load_config()  
  10. {  
  11.     struct uci_package * pkg = NULL;  
  12.     struct uci_element *e;  
  13.     ctx = uci_alloc_context(); // 申請一個UCI上下文.
  14.     if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))  
  15.         goto cleanup; //如果開啟UCI檔案失敗,則跳到末尾 清理 UCI 上下文.
  16.     /*遍歷UCI的每一個節*/
  17.     uci_foreach_element(&pkg->sections, e)  
  18.     {  
  19.         struct uci_section *s = uci_to_section(e);  
  20.         // 將一個 element 轉換為 section型別, 如果節點有名字,則 s->anonymous 為 false.
  21.         // 此時通過 s->e->name 來獲取.
  22.         // 此時 您可以通過 uci_lookup_option()來獲取 當前節下的一個值.
  23.         if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))  
  24.         {  
  25.             ip = strdup(value) //如果您想持有該變數值,一定要拷貝一份。當 pkg銷燬後value的記憶體會被釋放。
  26.         }  
  27.         // 如果您不確定是 string型別 可以先使用 uci_lookup_option() 函式得到Option 然後再判斷.
  28.         // Option 的型別有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 兩種.
  29.     }  
  30.     uci_unload(ctx, pkg); // 釋放 pkg 
  31. cleanup:  
  32.     uci_free_context(ctx);  
  33.     ctx = NULL;  
  34. }  


遍歷一個UCI_TYPE_LIST 型別.



加入現在有一個如下的配置檔案:


  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提供了一個簡潔的辦法來操作配置資訊,例如有一個配置檔案


  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. uci_free_context(ctx); //釋放上下文


依照上面的例子,我們可以舉一反三, uci_ptr 用來指定資訊.而是用uci_set則是寫入資訊.同類的函式有如下幾個: 針對list的操作:
  1. uci_add_list()  // 新增一個list 值
  2. uci_del_list()  // 刪除一個list 值
  3. uci_delete()    // 刪除一個option值