ref. http://www.tamabc.com/article/1543.html
OpenWRT UCI API的使用
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配置文件的基本操作.
首先您需要引入頭文件
#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值
沒有留言:
張貼留言
有敘述錯誤或者是觀念有問題歡迎指正