想要编写UCI需要下载uci,h这个头文件。
static int lc_uci_set(const char *str);
static char *lc_uci_get(const char *str);
static int lc_uci_add(const char *str);
static int lc_uci_del(char *str);
unsigned char deal_str(char * p);
int write_uci_config_cmd(unitstatic int lc_uci_set(const char *str)
{
char *s;
int ret = UCI_OK;
s = strdup(str);
if (uci_lookup_ptr(ctx, &ptr, s, true) != UCI_OK)
{
goto ERR_SET;
}
ret = uci_set(ctx, &ptr);
ERR_SET:
free(s);
return ret;
}
static char *lc_uci_get(const char *str)
{
struct uci_ptr p;
char *s;
s = strdup(str);
if (uci_lookup_ptr(ctx, &p, s, true) != UCI_OK)
{
free(s);
return NULL;
}
if (p.o)
{
free(s);
return p.o->v.string;
}
return NULL;
}
static int lc_uci_add(const char *str)
{
char *s;
int ret;
s = strdup(str);
uci_lookup_ptr(ctx, &ptr, s, true);
ret = uci_set(ctx, &ptr);
ret = uci_save(ctx, ptr.p);
uci_commit(ctx, &ptr.p, false);
}
static int lc_uci_del(char *str)
{
char *s;
struct uci_ptr p;
s = strdup(str);
uci_lookup_ptr(ctx, &p, s, true);
uci_delete(ctx, &p);
uci_commit(ctx, &p.p, false);
}
int convert(char a)
{
if (a >= '0' && a <= '9') {
return a - '0';
} else if (a >= 'A' && a <= 'F') {
return a - 'A' + 10;
} else if (a >= 'a' && a <= 'f') {
return a - 'a' + 10;
} else {
return -1;
}
}
unsigned char deal_str(char * p)
{
unsigned char i,k;
while (*p)
{
i = convert(*p);
if (i >= 0) {
k = k * 16 + i;
}
p++;
}
return k;
}
int read_uci_config_info(unit_info *read_info)
{
int i=0;
char *value=NULL ;
char * temp1=NULL;
unsigned char temp2[3]={'\0'};
struct uci_element *e;
ctx = uci_alloc_context(); //To apply for the uci environment
uci_load(ctx, UCI_CONFIG_FILE, &pkg); //Load the package p
uci_foreach_element(&pkg->sections, e)
{
struct uci_section *s = uci_to_section(e);
if(e->name !=NULL)
{
temp1 = e->name;
strncpy(temp2,temp1,2);
//printf("========%s\n",temp2);
//printf("X=======%x\n",deal_str(temp2));
read_info[i].ID[0]=deal_str(temp2);
strncpy(temp2,temp1+2,2);
read_info[i].ID[1]=deal_str(temp2);
strncpy(temp2,temp1+4,2);
read_info[i].ID[2]=deal_str(temp2);
strncpy(temp2,temp1+6,2);
read_info[i].ID[3]=deal_str(temp2);
strncpy(temp2,temp1+8,2);
read_info[i].ID[4]=deal_str(temp2);
strncpy(temp2,temp1+10,2);
read_info[i].ID[5]=deal_str(temp2);
strncpy(temp2,temp1+12,2);
read_info[i].ID[6]=deal_str(temp2);
}
value = uci_lookup_option_string(ctx, s,"type");
if(value != NULL)
{
read_info[i].Type = atoi(value);
printf("in struct %#x\n",read_info[i].Type);
}
value = uci_lookup_option_string(ctx, s, "name");
if(value != NULL)
{
strcpy(read_info[i].Name,value);
printf("%s\n",read_info[i].Name);
}
i++;
}
uci_unload(ctx, pkg); // unload package
uci_free_context(ctx); //free uci environment
printf("read uci config finish!\n");
return 0;
}
int write_uci_config_info(unit_info *write_info,int i)
{
char cmd[128]={'\0'};
ctx = uci_alloc_context(); //To apply for the uci environment
uci_load(ctx, UCI_CONFIG_FILE, &pkg); //Load the package p
sprintf(cmd,"unit.%.2x%.2x%.2x%.2x%.2x%.2x%.2x=ID",
write_info[i].ID[0],
write_info[i].ID[1],
write_info[i].ID[2],
write_info[i].ID[3],
write_info[i].ID[4],
write_info[i].ID[5],
write_info[i].ID[6]);
lc_uci_add(cmd);
sprintf(cmd,"unit.%.2x%.2x%.2x%.2x%.2x%.2x%.2x.type=%d",
write_info[i].ID[0],
write_info[i].ID[1],
write_info[i].ID[2],
write_info[i].ID[3],
write_info[i].ID[4],
write_info[i].ID[5],
write_info[i].ID[6],
write_info[i].Type);
lc_uci_add(cmd);
if(write_info[i].Name)
{
sprintf(cmd,"unit.%.2x%.2x%.2x%.2x%.2x%.2x%.2x.name=%s",
write_info[i].ID[0],
write_info[i].ID[1],
write_info[i].ID[2],
write_info[i].ID[3],
write_info[i].ID[4],
write_info[i].ID[5],
write_info[i].ID[6],
write_info[i].Name);
lc_uci_add(cmd);
}
uci_unload(ctx, pkg); // unload package
uci_free_context(ctx); //free uci environment
printf("write uci config finish\n");
return 0;
}
int read_server_config_info(char *str)
{
ctx=uci_alloc_context();
uci_load(ctx,SERVER_CONFIG_FILE,&pkg);
strcpy(str,lc_uci_get("server.server.address"));
//printf("%s\n",str);
uci_unload(ctx,pkg);
uci_free_context(ctx);
printf("read server config finish\n");
return 0;
}_info *write_info,int i);