欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-09 18:02:21
...

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

#include<stdio.h>
#include <uci.h>
static struct uci_context * ctx = NULL; 
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;
}

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

相关标签: openwrt