当前位置:网站首页>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;
}边栏推荐
- Zhang Chi's class: Notice on the time of CAQ Six Sigma test in 2022
- leetcode:890. Find and replace mode [two dict records set]
- An error occurred while downloading the remote file The errormessage
- Matlab 6-DOF manipulator forward and inverse motion
- NOI openjudge 计算2的N次方
- 报表工具的二次革命
- June training day 6 - sliding window
- The second day of June training - string
- 上位机开发(固件下载软件之需求分析)
- esp32 hosted
猜你喜欢

Node. Detailed installation tutorial of CPM and cnpm (including error resolution)

8. 表单标签

Kali与编程:如何快速搭建OWASP网站安全实验靶场?
![leetcode:890. Find and replace mode [two dict records set]](/img/a8/e1ea743bbd8d2aca4de3e3ac3b4ac3.png)
leetcode:890. Find and replace mode [two dict records set]

报表工具的二次革命

Junior high school education, less than 3k, to 30k+ monthly salary, how wonderful life is without restrictions

5 ROS simulation modeling (4-navigation navigation simulation)

Curry carries the fourth game of the warriors against the Celtics

库里扛起了勇士对凯尔特人的第四场
![[image denoising] salt and pepper noise image denoising based on Gaussian filter, mean filter, median filter and bilateral filter with matlab code attached](/img/f2/16db0b11d4e69946ec45b67ab41b81.png)
[image denoising] salt and pepper noise image denoising based on Gaussian filter, mean filter, median filter and bilateral filter with matlab code attached
随机推荐
leetcode:890. Find and replace mode [two dict records set]
libprint2
Vscode Common plug - in
d的扩大@nogc
初中学历,从不到3K,到月薪30K+,不设限的人生有多精彩
MySQL multiple SQL batch operations (crud) in JDBC
sql server 2019安装出现错误,如何解决
美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战
3 strings, containers, and arrays
Jackson XML is directly converted to JSON without writing entity classes manually
Matlab 6-DOF manipulator forward and inverse motion
六月集训 第九日——位运算
Torch models trained in higher versions report errors in lower versions
Scons compiling imgui
Detailed explanation of convirt paper (medical pictures)
5 statement
1.3-1.9 summary
【图像去噪】基于高斯滤波、均值滤波、中值滤波、双边滤波四种滤波实现椒盐噪声图像去噪附matlab代码
leetcode:890. 查找和替换模式【两个dict记录双射(set)】
android studio 利用数据库实现登录注册界面功能