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

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

#include
#include 
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

你可能感兴趣的:(openwrt)