openwrt开发之利用uci库读取配置文件例子

openwrt开发之利用uci库读取配置文件例子

#include<stdio.h>
#include <uci.h>
static struct uci_context * ctx = NULL; 
#define UCI_CONFIG_FILE "/etc/config/wireless";
void getWirelessConfig(char *SSID,char *password,char *encrypt)
{
	char *ssid;
	char *psword;
	char *Encrypt;
	struct uci_package * pkg = NULL;
	struct uci_element *e;
	ctx = uci_alloc_context();
	if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
		goto cleanup;
	uci_foreach_element(&pkg->sections, e)
	{
		struct uci_section *s = uci_to_section(e);
		DBG_vPrintf(1,"Wireless config name:%s",s->e.name);
		if(!strncmp(s->e.name,"24G",strlen(s->e.name))){
			 if (NULL != (ssid = uci_lookup_option_string(ctx, s, "ssid")))
			 	{
			 		strncpy(SSID,ssid,strlen(ssid));
					DBG_vPrintf(1,"SSID :%s",SSID); 
			 	}
			 if (NULL != (psword = uci_lookup_option_string(ctx, s, "key")))
			 	{
			 		strncpy(password,psword,strlen(psword));
					DBG_vPrintf(1,"password :%s",password); 
			 	}
			 if (NULL != (Encrypt = uci_lookup_option_string(ctx, s, "encryption")))
			 	{
			 		strncpy(encrypt,Encrypt,strlen(Encrypt));
					DBG_vPrintf(1,"encrypt :%s",encrypt); 
			 	}
		}
	}
	uci_unload(ctx, pkg); 
	cleanup:
		uci_free_context(ctx);
		ctx = NULL;
}
void set_config()
{
	struct uci_package * pkg = NULL;
	struct uci_element *e;
	ctx = uci_alloc_context();
	if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
		goto cleanup;
	struct uci_ptr ptr ={
    			.package = "config",
    			.section = "guest24G",
    			.option = "enable",
    			.value = "1",
			};
	uci_set(ctx,&ptr); //写入配置
	uci_commit(ctx, &ptr.p, false); //提交保存更改
	uci_unload(ctx,ptr.p); //卸载包
	system("/etc/init.d/network restart");
	cleanup:
		uci_free_context(ctx);
		ctx = NULL;
	
}

注明:记得在Makefile中添加依赖uci

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值