OpenWRT UCI API的使用 (libuci庫的使用)


摘自:http://www.openwrt.pro/post-37.html

OpenWRT UCI API的使用

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值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM