【智能路由器】系列文章连接
http://blog.csdn.net/u012819339/article/category/5803489
介绍
本篇博客是对 【智能路由器】C代码调用uci的API读openwrt配置文件指南 文章的补充
网上依旧有一些网友有C代码调用uci设置的需求,这里整理了arvik以前项目中编写的c语言调用uci的两个接口,供大家在项目中参考使用
接口
接口 uci_set_str
原型:
/*
设置uci配置项 , 保存并且提交更改到文件
arg: 设置参数
eg.
wireless.@wifi-iface[0]=wifi-iface
wireless.radio0.path=platform/ar934x_wmac
wireless.radio0.newabc=arvik
返回值:
成功返回UCI_OK, 失败返回其它值
*/
int uci_set_str(const char *arg);
C代码调用uci分别设置wireless.@wifi-iface[0]=wifi-iface
和wireless.radio0.path=platform/ar934x_wmac
, 使用方法:
// 以下函数相当于执行 uci set wireless.@wifi-iface[0]=wifi-iface 再加上 uci commit wireless.@wifi-iface[0] 两个命令
uci_set_str("wireless.@wifi-iface[0]=wifi-iface");
//以下函数相当于执行 uci set wireless.radio0.path=platform/ar934x_wmac 再加上 uci commit wireless.radio0.path=platform/ar934x_wmac 两个命令
uci_set_str("wireless.radio0.path=platform/ar934x_wma");
接口 uci_get_str
原型:
/*
获取uci配置项
arg: 获取该参数下的值
eg.
wireless.@wifi-iface[0]
wireless.radio0.path
out: 获取的值存储区
返回值:
成功返回UCI_OK, 失败返回其它值
*/
int uci_get_str(const char *arg, char *out);
C代码调用uci获取wireless.radio0.path
, 使用方法:
int ret = -1;
char buf[1024]={0};
//以下函数相当于执行命令行 uci get wireless.radio0.path
ret = uci_get_str("wireless.radio0.path", buf); // buf中的值就是获取到的对应的值,如platform/ar934x_wmac
if(ret != UCI_OK)
{
...... // uci get error
}
源代码:
将以下文件包含进工程编译即可方便使用接口
ucili.c文件:
/**********************************************************
* file: ucili.h
* purpose: uci c call
* author: arvik
* blog: http://blog.csdn.net/u012819339
* email: 1216601195@qq.com
***********************************************************/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "ucili.h"
static int 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;
break;
}
return UCI_OK;
}
int uci_get_str(const char *arg, char *out)
{
struct uci_context *ctx;
struct uci_element *e;
struct uci_ptr ptr;
int ret = UCI_OK;
char *name = NULL;
if(arg == NULL || out == NULL) return UCI_ERR_INVAL;
name = strdup(arg);
if(name == NULL) return UCI_ERR_INVAL;
ctx = uci_alloc_context();
if (!ctx) {
free(name);
return UCI_ERR_MEM;
}
if (uci_lookup_ptr(ctx, &ptr, name, true) != UCI_OK) {
uci_free_context(ctx);
free(name);
return UCI_ERR_NOTFOUND;
}
if(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);
free(name);
return ret;
}
int uci_set_str(const char *arg)
{
struct uci_context *ctx;
struct uci_element *e;
struct uci_ptr ptr;
int ret = UCI_OK;
char *name = NULL;
if(arg == NULL) return UCI_ERR_INVAL;
name = strdup(arg);
if(name == NULL) return UCI_ERR_MEM;
ctx = uci_alloc_context();
if (!ctx) {
free(name);
return UCI_ERR_MEM;
}
if (uci_lookup_ptr(ctx, &ptr, name, true) != UCI_OK) {
uci_free_context(ctx);
free(name);
return UCI_ERR_NOTFOUND;
}
ret = uci_set(ctx, &ptr);
if(ret != UCI_OK)
{
uci_free_context(ctx);
free(name);
return ret;
}
ret = uci_save(ctx, ptr.p);
if(ret != UCI_OK)
{
uci_free_context(ctx);
free(name);
return ret;
}
ret = uci_commit(ctx, &ptr.p, false);
if(ret != UCI_OK)
{
uci_free_context(ctx);
free(name);
return ret;
}
uci_free_context(ctx);
free(name);
return ret;
}
ucili.h文件:
/**********************************************************
* file: ucili.h
* purpose: uci c call
* author: arvik
* blog: http://blog.csdn.net/u012819339
* email: 1216601195@qq.com
***********************************************************/
#ifndef _UCILI_H_
#define _UCILI_H_
#include "uci.h"
/*
设置uci配置项 , 保存并且提交更改到文件
arg: 设置参数
eg.
wireless.@wifi-iface[0]=wifi-iface
wireless.radio0.path=platform/ar934x_wmac
wireless.radio0.newabc=arvik
返回值:
成功返回UCI_OK, 失败返回其它值
*/
int uci_set_str(const char *arg);
/*
获取uci配置项
arg: 获取该参数下的值
eg.
wireless.@wifi-iface[0]
wireless.radio0.path
out: 获取的值存储区
返回值:
成功返回UCI_OK, 失败返回其它值
*/
int uci_get_str(const char *arg, char *out);
#endif
【智能路由器】系列文章连接
http://blog.csdn.net/u012819339/article/category/5803489