UCI是Unified Configuration Interface的缩写,翻译成中文就是统一配置接口,用途就是为OpenWrt提供一个集中控制的接口。OpenWrt实现的这个工具,能够让你的不管是Lua还是PHP程序,或者SHELL程序或C程序,只要执行命令传输参数就能达到修改系统参数的目的。
UCI 是OpenWRT为实现配置集中化而引入的一个软件包, 通过修改UCI,可以实现对OpenWRT的绝对部分配置的修改.LUCI(OpenWRT 的WEB配置界面)也是通过
读UCI配置文件的操作来实现用户对路由的配置的。通过掌握UCI的API的使用,可以方便地将您的软件的配置接口集成到LUCI中.
LUCI配置文件简介
LUCI的配置文件一般存储在/etc/config/目录下。比如无线配置/etc/config/wireless.
基本概念
UCI 上下文:struct uci_context *
包(package):一个包对应的一个UCI格式的文件,类型是struct uci_package *
节(section):一个配置文件的节点,类型是struct uci_list *
值(value):一个节下面可能包含多个值,一个值只有一个名称
基本的数据结构
struct uci_package:包结构体。它对应一个配置文件内容
struct uci_package
{
struct uci_element e;
struct uci_list sections;
struct uci_context *ctx;
bool has_delta;
char *path;
/* private: */
struct uci_backend *backend;
void *priv;
int n_section;
struct uci_list delta;
struct uci_list saved_delta;
};
struct uci_section:节点结构体,它对应配置文件中的节
struct uci_section
{
struct uci_element e;
struct uci_list options;
struct uci_package *package;
bool anonymous;
char *type;
};
struct uci_option
{
struct uci_element e;
struct uci_section *section;
enum uci_option_type type;
union {
struct uci_list list;
char *string;
} v;
};
struct uci_ptr:元素位置指针结构,用来查询并保存对应位置元素
struct uci_ptr
{
enum uci_type target;
enum {
UCI_LOOKUP_DONE = (1 << 0),
UCI_LOOKUP_COMPLETE = (1 << 1),
UCI_LOOKUP_EXTENDED = (1 << 2),
} flags;
struct uci_package *p;
struct uci_section *s;
struct uci_option *o;
struct uci_element *last;
const char *package;
const char *section;
const char *option;
const char *value;
};
struct uci_context:uci上下文结构,贯穿查询、更改配置文件全过程
struct uci_context
{
/* list of config packages */
struct uci_list root;
/* parser context, use for error handling only */
struct uci_parse_context *pctx;
/* backend for import and export */
struct uci_backend *backend;
struct uci_list backends;
/* uci runtime flags */
enum uci_flags flags;
char *confdir;
char *savedir;
/* search path for delta files */
struct uci_list delta_path;
/* private: */
int err;
const char *func;
jmp_buf trap;
bool internal, nested;
char *buf;
int bufsz;
};
几个基本的API函数
/**
* uci_alloc_context: Allocate a new uci context
*/
struct uci_context *uci_alloc_context(void); //动态申请一个uci的上下文结构
/**
* uci_free_context: Free the uci context including all of its data
*/
void uci_free_context(struct uci_context *ctx); //释放由uci_alloc_context申请的uci上下文结构且包括它的所有数据
/**
* uci_lookup_ptr: Split an uci tuple string and look up an element tree
* @ctx: uci context
* @ptr: lookup result struct
* @str: uci tuple string to look up
* @extended: allow extended syntax lookup
*
* if extended is set to true, uci_lookup_ptr supports the following
* extended syntax:
*
* Examples:
* network.@interface[0].ifname ('ifname' option of the first interface section)
* network.@interface[-1] (last interface section)
* Note: uci_lookup_ptr will automatically load a config package if necessary
* @str must not be constant, as it will be modified and used for the strings inside @ptr,
* thus it must also be available as long as @ptr is in use.
*/
int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended); //由给定的元组查找元素