openwrt uci c api

15 篇文章 2 订阅

一:UCI文件语法

config 'section-type' 'section'

        option 'key' 'value'

        list    'list_key'  'list_value'

config 节点 以关键字 config 开始的一行用来代表当前节点
            section-type 节点类型
            section 节点名称
option 选项 表示节点中的一个元素
            key 键
            value 值
list 列表选项 表示列表形式的一组参数。
           list_key 列表键
           list_value 列表值

二:c api 文件写

1) libuci无论数读写配置文件, 配置文件都必须存在, libuci不负责磁盘文件建立。

2) libuci上下文会保存全部配置文件的配置信息, 当你程序中打开uci上下文后,你在控制台使用uci命令行输入的信息也包含在其中

3) 当你需要修改/增加一个option时, option所在的section必须已经存在, 否则写入会失败

int UCI::setValue(const char *key, const char *value)
{
	if (key == nullptr || value == nullptr)
	{
		return UCI_ERR_INVAL;
	}

	struct uci_context *_ctx = uci_alloc_context();
	if (_ctx == nullptr)
	{
		return UCI_ERR_INVAL;
	}

	struct uci_ptr *ptr = (struct uci_ptr *)malloc(sizeof(struct uci_ptr));
	if (ptr == nullptr)
	{
		uci_free_context(_ctx);
		return UCI_ERR_INVAL;
	}
	memset(ptr, 0, sizeof(struct uci_ptr));
	ptr->package = _fileName;//文件名称/etc/config/network
	ptr->section = _sectionName;//lan 
	ptr->option = key;//ipaddr 
	ptr->value = value;//127.0.0.1

	int ret = UCI_OK;
	ret = uci_set(_ctx, ptr);
	if (ret != UCI_OK)
	{
		uci_perror(_ctx, "uci_set Perror:");
		uci_free_context(_ctx);
		free(ptr);
		return ret;
	}

	ret = uci_commit(_ctx, &ptr->p, false);
	if (ret != UCI_OK)
	{
		uci_perror(_ctx, "uci_commit Perror:");
		uci_free_context(_ctx);
		free(ptr);
		return ret;
	}

	ret = uci_unload(_ctx, ptr->p);
	if (ret != UCI_OK)
	{
		uci_perror(_ctx, "uci_commit Perror:");
		uci_free_context(_ctx);
		free(ptr);
		return ret;
	}

	uci_free_context(_ctx);
	free(ptr);
	return UCI_OK;
}

三:c api 文件读

int UCI::getValue(const char *key, char *out)
{
	struct uci_context *ctx;
	struct uci_element *e;
	struct uci_ptr ptr;
	int ret = UCI_OK;
	char name[1024] = "robot.main.";

	if (key == NULL || out == NULL)
	{
		return UCI_ERR_INVAL;
	}

	ctx = uci_alloc_context();
	if (!ctx)
	{
		return UCI_ERR_MEM;
	}

	strcat(name, key);
	if (uci_lookup_ptr(ctx, &ptr, name, true) != UCI_OK)
	{
		uci_free_context(ctx);
		return UCI_ERR_NOTFOUND;
	}

	if (2 /*UCI_LOOKUP_COMPLETE*/ & ptr.flags)
	{
		e = ptr.last;
		switch (e->type)
		{
		case UCI_TYPE_SECTION:
			ret = UCI_ERR_INVAL;
			break;
		case UCI_TYPE_OPTION:
			ret = uci_get_value(ptr.o, out);
			break;
		default:
			ret = UCI_ERR_NOTFOUND;
			break;
		}
	}
	else
	{
		ret = UCI_ERR_NOTFOUND;
	}

	uci_free_context(ctx);
	return ret;
}


int UCI::uci_get_value(struct uci_option *o, char *out)
{
	struct uci_element *e;
	const char *delimiter = " ";
	bool sep = false;

	switch (o->type)
	{
	case UCI_TYPE_STRING:
		strcpy(out, o->v.string);
		break;
	case UCI_TYPE_LIST:
		uci_foreach_element(&o->v.list, e)
		{
			if (sep)
			{
				strcat(out, delimiter);
			}
			strcat(out, e->name);
			sep = true;
		}
		break;
	default:
		return UCI_ERR_INVAL;
	}

	return UCI_OK;
}

UCI用法说明

OpenWRT UCI API的使用经验

LibUCI-Documentation

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenWRT是一个基于Linux的嵌入式操作系统,它提供了一套名为UCI(Unified Configuration Interface)的配置接口,用于管理系统的配置文件。UCI提供了C语言接口,使开发者可以通过编程方式读取和修改系统配置。 在使用OpenWRTUCI C语言接口之前,需要包含相应的头文件和链接相关的库文件。头文件是`uci.h`,库文件是`libuci.so`。 下面是一个简单的例子,演示如何使用UCI C语言接口读取和修改配置: ```c #include <stdio.h> #include <uci.h> int main() { struct uci_context *ctx = uci_alloc_context(); if (!ctx) { fprintf(stderr, "Failed to allocate UCI context\n"); return 1; } struct uci_package *pkg; if (uci_load(ctx, "wireless", &pkg) != UCI_OK) { fprintf(stderr, "Failed to load wireless package\n"); uci_free_context(ctx); return 1; } struct uci_element *elem; uci_foreach_element(&pkg->sections, elem) { struct uci_section *section = uci_to_section(elem); const char *name = section->e.name; const char *option_value = uci_lookup_option_string(ctx, section, "option_name"); printf("Section: %s\n", name); printf("Option value: %s\n", option_value); } uci_unload(ctx, pkg); uci_free_context(ctx); return 0; } ``` 上述代码中,首先通过`uci_alloc_context()`函数分配一个UCI上下文对象。然后使用`uci_load()`函数加载指定的配置包(这里是"wireless")。接着使用`uci_foreach_element()`函数遍历配置包中的所有节(section),并通过`uci_lookup_option_string()`函数获取指定选项(option)的值。最后,使用`uci_unload()`函数卸载配置包,并通过`uci_free_context()`函数释放UCI上下文对象。 以上是一个简单的示例,你可以根据具体的需求进一步扩展和修改代码。希望对你有帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值