当前位置:网站首页>openwrt uci c api

openwrt uci c api

2022-06-12 07:03:00 Handsome without friends~

One :UCI File syntax

config 'section-type' 'section'

        option 'key' 'value'

        list    'list_key'  'list_value'

config node With keywords config The first line is used to represent the current node
            section-type Node type
            section The name of the node
option Options Represents an element in a node
            key key
            value value
list List Options Represents a set of parameters in the form of a list .
           list_key List key
           list_value List value

Two :c api The document says

1) libuci No matter how many read and write configuration files , All configuration files must exist , libuci Not responsible for disk file creation .

2) libuci The context will save the configuration information of all configuration files , When open in your program uci After the context , You use... On the console uci The information entered on the command line is also included

3)  When you need to modify / Add one more option when , option Where section Must already exist , Otherwise, the write will fail

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;// File name /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;
}

3、 ... and :c api File read

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 instructions

OpenWRT UCI API Using experience of

LibUCI-Documentation

原网站

版权声明
本文为[Handsome without friends~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010603039943.html