Openwrt/LEDE/Gargoyle 下用c调用uci
时间:2017-9-11 10:38 热度:5462° 评论:0 条

有机友问起,无聊折腾的时候就顺便写一写c下调用uci(统一配置接口)api的方法吧。
openwrt最方便的一个功能应该就属uci了,不管你是直接读写配置文件还是利用api都能很方便的完成各种操作。特别是最近你们不都想把华硕app移植到openwrt上嘛。
其实利用uci就能很顺利的完成,参考华硕app的协议即可
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uci.h>
#define UCI_CONFIG_FILE "./wireless"
/*********************************************
* 载入配置文件,并遍历Section.
*/
static char * seek_value(char *file,char *section,char*option)
{
struct uci_context * ctx = NULL;
struct uci_package * pkg = NULL;
struct uci_element *e;
const char *value;
char *return_value =NULL;
if (file == NULL || section == NULL || option == NULL){
return NULL;
}
ctx = uci_alloc_context(); // 申请一个UCI上下文.
if (UCI_OK != uci_load(ctx, file, &pkg)){
printf("uci_load %s fail\n",file);
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(strcmp(section,e->name)==0 ){
value = uci_lookup_option_string(ctx, s, option);
if (NULL != value)
return_value = 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;
return return_value;
}
static int fix_value(char *file,char *section,char*option,char *value)
{
if(file == NULL || section == NULL || option == NULL || value == NULL){
return -1;
}
struct uci_context * ctx = uci_alloc_context(); //申请上下文
struct uci_ptr ptr ={
.package = file,
.section = section,
.option = option,
.value = value,
};
uci_set(ctx,&ptr); //写入配置
uci_commit(ctx, &ptr.p, false); //提交保存更改
uci_unload(ctx,ptr.p); //卸载包
uci_free_context(ctx); //释放上下文
return 0;
}
static int delete_option(char *file,char *section,char*option)
{
if(file == NULL || section == NULL || option == NULL){
return -1;
}
struct uci_context * ctx = uci_alloc_context(); //申请上下文
struct uci_ptr ptr ={
.package = file,
.section = section,
.option = option,
};
uci_delete(ctx,&ptr); //写入配置
uci_commit(ctx, &ptr.p, false); //提交保存更改
uci_unload(ctx,ptr.p); //卸载包
uci_free_context(ctx); //释放上下文
return 0;
}
static int delete_section(char *file,char *section)
{
if(file == NULL || section == NULL){
return -1;
}
struct uci_context * ctx = uci_alloc_context(); //申请上下文
struct uci_ptr ptr ={
.package = file,
.section = section,
};
uci_delete(ctx,&ptr); //写入配置
uci_commit(ctx, &ptr.p, false); //提交保存更改
uci_unload(ctx,ptr.p); //卸载包
uci_free_context(ctx); //释放上下文
return 0;
}
int main()
{
char *value = seek_value(UCI_CONFIG_FILE,"default_radio0","key");
if(value){
printf("key = %s\n",value);
free(value);
}
fix_value(UCI_CONFIG_FILE,"default_radio0","key","asd123456");
//delete_option(UCI_CONFIG_FILE,"wan","ipaddr");
delete_section(UCI_CONFIG_FILE,"wan1");
return 0;
}


捐赠支持:如果觉得这篇文章对您有帮助,请“扫一扫”鼓励作者!
相关文章本文作者:沁雨寒 文章标题: Openwrt/LEDE/Gargoyle 下用c调用uci
本文地址:https://blog.sxx1314.com/openwrt/509.html
版权声明:若无注明,本文皆为“unix 软硬件 技术宅 ”原创,转载请保留文章出处。百度已收录
本文地址:https://blog.sxx1314.com/openwrt/509.html
版权声明:若无注明,本文皆为“unix 软硬件 技术宅 ”原创,转载请保留文章出处。百度已收录















